diff --git a/Makefile b/Makefile index eeb2ead..3bbe610 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/src/db_get_user_by_id.c b/src/db_get_user_by_id.c index 5c7448e..033ecd4 100644 --- a/src/db_get_user_by_id.c +++ b/src/db_get_user_by_id.c @@ -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 } diff --git a/src/db_get_user_by_username.c b/src/db_get_user_by_username.c index 8063c8f..5f14757 100644 --- a/src/db_get_user_by_username.c +++ b/src/db_get_user_by_username.c @@ -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 } diff --git a/src/db_get_user_field_by_id.c b/src/db_get_user_field_by_id.c index d1e57d9..378e782 100644 --- a/src/db_get_user_field_by_id.c +++ b/src/db_get_user_field_by_id.c @@ -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 } diff --git a/src/db_insert_user.c b/src/db_insert_user.c index 55d62e5..d53d0db 100644 --- a/src/db_insert_user.c +++ b/src/db_insert_user.c @@ -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 } diff --git a/src/file.c b/src/file.c index c0488c8..d9f21db 100644 --- a/src/file.c +++ b/src/file.c @@ -1,10 +1,14 @@ #include "file.h" +#include +#include #include #include #include +#include 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; +} diff --git a/src/file.h b/src/file.h index 9ac6d5d..817f267 100644 --- a/src/file.h +++ b/src/file.h @@ -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);