some fixes
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 10s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Failing after 8s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Successful in 11s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 10s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Failing after 8s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Successful in 11s
This commit is contained in:
1 parent
dc05f8f861
commit
72a78d2e7a
7 files changed
+66
-24
No files matched your search
@@ -29,7 +29,7 @@ $(TARGET): $(OBJS)
|
||||
$(CROSS_COMPILE)$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET) $(shell find . -type f -name '*.log') users.db
|
||||
rm -f $(OBJS) $(TARGET) $(shell find . -type f -name '*.log') users.db users_db.json
|
||||
|
||||
test: $(TARGET)
|
||||
bash ./tests.sh
|
||||
@@ -1,3 +1,4 @@
|
||||
#include "cJSON.h"
|
||||
#include "database.h"
|
||||
#include "sql.h"
|
||||
#include "user.h"
|
||||
@@ -68,9 +69,15 @@ User *db_get_user_by_id(const int id) {
|
||||
}
|
||||
if (found == nullptr) {
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
// if (user != nullptr) cJSON_Delete(user);
|
||||
cJSON_Delete(users);
|
||||
free(data);
|
||||
return nullptr;
|
||||
}
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
// if (user != nullptr) cJSON_Delete(user);
|
||||
cJSON_Delete(users);
|
||||
free(data);
|
||||
return found;
|
||||
#endif
|
||||
}
|
||||
@@ -69,9 +69,15 @@ User *db_get_user_by_username(const char *username) {
|
||||
}
|
||||
if (found == nullptr) {
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
// if (user != nullptr) cJSON_Delete(user);
|
||||
cJSON_Delete(users);
|
||||
free(data);
|
||||
return nullptr;
|
||||
}
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
// if (user != nullptr) cJSON_Delete(user);
|
||||
cJSON_Delete(users);
|
||||
free(data);
|
||||
return found;
|
||||
#endif
|
||||
}
|
||||
@@ -70,9 +70,15 @@ char *db_get_user_field_by_id(const int id, const char *field) {
|
||||
}
|
||||
if (found == nullptr) {
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
// if (user != nullptr) cJSON_Delete(user);
|
||||
cJSON_Delete(users);
|
||||
free(data);
|
||||
return nullptr;
|
||||
}
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
// if (user != nullptr) cJSON_Delete(user);
|
||||
cJSON_Delete(users);
|
||||
free(data);
|
||||
return found->password;
|
||||
#endif
|
||||
}
|
||||
+19
-23
@@ -70,29 +70,25 @@ User *db_insert_user(const char *username, const char *email,
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
cJSON *users = cJSON_Parse(data);
|
||||
char *old_data = cJSON_Print(users);
|
||||
if (old_data == NULL) {
|
||||
printf("ERR");
|
||||
}
|
||||
cJSON *user = nullptr;
|
||||
int users_size = 0;
|
||||
User *found = nullptr;
|
||||
bool found = false;
|
||||
cJSON_ArrayForEach(user, users) {
|
||||
cJSON *user_id = cJSON_GetObjectItem(user, "id");
|
||||
cJSON *user_username = cJSON_GetObjectItem(user, "username");
|
||||
cJSON *user_password = cJSON_GetObjectItem(user, "password");
|
||||
cJSON *user_email = cJSON_GetObjectItem(user, "email");
|
||||
cJSON *user_group = cJSON_GetObjectItem(user, "group");
|
||||
cJSON *user_is_superuser = cJSON_GetObjectItem(user, "is_superuser");
|
||||
if (!strcmp(user_username->valuestring, username)) {
|
||||
found = new_user(user_id->valueint, user_username->valuestring,
|
||||
user_password->valuestring, user_email->valuestring,
|
||||
user_group->valuestring, user_is_superuser->valueint);
|
||||
if (!strcmp(user_username->valuestring, username) &&
|
||||
!strcmp(user_password->valuestring, password) &&
|
||||
!strcmp(user_email->valuestring, email) &&
|
||||
!strcmp(user_group->valuestring, group)) {
|
||||
found = true;
|
||||
}
|
||||
users_size++;
|
||||
}
|
||||
if (found != nullptr) {
|
||||
return nullptr;
|
||||
if (found == true) {
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
cJSON *new_user_data = cJSON_CreateObject();
|
||||
cJSON *new_user_id = cJSON_CreateNumber(users_size);
|
||||
@@ -100,7 +96,7 @@ User *db_insert_user(const char *username, const char *email,
|
||||
cJSON *new_user_password = cJSON_CreateString(password);
|
||||
cJSON *new_user_email = cJSON_CreateString(email);
|
||||
cJSON *new_user_group = cJSON_CreateString(group);
|
||||
cJSON *new_user_is_superuser = cJSON_CreateBool(*is_superuser);
|
||||
cJSON *new_user_is_superuser = cJSON_CreateBool(is_superuser?1:0);
|
||||
cJSON_AddItemToObject(new_user_data, "id", new_user_id);
|
||||
cJSON_AddItemToObject(new_user_data, "username", new_user_username);
|
||||
cJSON_AddItemToObject(new_user_data, "password", new_user_password);
|
||||
@@ -108,20 +104,20 @@ User *db_insert_user(const char *username, const char *email,
|
||||
cJSON_AddItemToObject(new_user_data, "group", new_user_group);
|
||||
cJSON_AddItemToObject(new_user_data, "is_superuser", new_user_is_superuser);
|
||||
char *new_user_data_string = cJSON_Print(new_user_data);
|
||||
printf("%s\n", new_user_data_string);
|
||||
if (users_size == 0) {
|
||||
users = cJSON_CreateArray();
|
||||
users = cJSON_CreateArray();
|
||||
}
|
||||
cJSON_AddItemToArray(users, new_user_data);
|
||||
char *new_data = cJSON_Print(users);
|
||||
if (new_data == NULL) {
|
||||
printf("ERR");
|
||||
}
|
||||
// printf("%s\n", strdup(new_data));
|
||||
// write_file("users_db.json", new_data);
|
||||
free(new_data);
|
||||
write_file("users_db.json", new_data);
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
User *return_user = new_user(users_size, username, email, password, group, is_superuser);
|
||||
User *return_user =
|
||||
new_user(users_size, username, email, password, group, is_superuser);
|
||||
free(new_data);
|
||||
free(data);
|
||||
free(new_user_data_string);
|
||||
cJSON_Delete(new_user_data);
|
||||
if (user != nullptr) cJSON_Delete(user);
|
||||
return return_user;
|
||||
#endif
|
||||
}
|
||||
+25
@@ -1,10 +1,14 @@
|
||||
#include "file.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
char *read_file(const char *path) {
|
||||
create_file_if_not_exists(path);
|
||||
FILE *fp = fopen(path, "rb");
|
||||
if (fp == NULL) {
|
||||
perror("Error opening file");
|
||||
@@ -36,6 +40,7 @@ char *read_file(const char *path) {
|
||||
}
|
||||
|
||||
char *write_file(const char *path, const char *data) {
|
||||
create_file_if_not_exists(path);
|
||||
FILE *fp = fopen(path, "w");
|
||||
if (fp == NULL) {
|
||||
printf("%s\n", path);
|
||||
@@ -50,3 +55,23 @@ char *write_file(const char *path, const char *data) {
|
||||
char *ret = strdup(data);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int create_file_if_not_exists(const char *filename) {
|
||||
if (!check_file_exists(filename)) {
|
||||
return 0;
|
||||
}
|
||||
FILE *file = fopen(filename, "w");
|
||||
if (file == nullptr) {
|
||||
perror("Error creating file");
|
||||
return -1;
|
||||
}
|
||||
fclose(file);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int check_file_exists(const char *filename) {
|
||||
if (access(filename, F_OK) == 0) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@@ -2,3 +2,5 @@
|
||||
|
||||
char *read_file(const char *path);
|
||||
char *write_file(const char *path, const char *data);
|
||||
int create_file_if_not_exists(const char *filename);
|
||||
int check_file_exists(const char *filename);
|
||||
Reference in new issue
Block a user