some refactor
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 14s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 16s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Successful in 13s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make-Without-sqlite3 (push) Successful in 9s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake-Without-sqlite3 (push) Successful in 14s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson-Without-sqlite3 (push) Successful in 13s

This commit is contained in:
wt committed 2025-10-24 14:18:46 +07:00
1 parent bed968b4e4
commit 39ef249f6f
35 files changed
+884 -691

No files matched your search

+2 -1
View File
@@ -2,7 +2,7 @@ CC = gcc
CFLAGS = -Wall -g -std=gnu2x -DMG_TLS=MG_TLS_OPENSSL -ffunction-sections -fdata-sections
LDFLAGS = -Wl,--gc-sections
INCLUDES = -I./src/lib -I./src
SRCS = $(wildcard src/*/*.c src/*.c)
SRCS = $(wildcard src/lib/*.c src/file_db/*.c src/*.c)
OBJS = $(SRCS:.c=.o)
TARGET = auth_service
BUILD_DIR=./build
@@ -14,6 +14,7 @@ WERROR ?= 0
ifeq ($(USE_SQLITE3), 1)
CFLAGS += -DUSE_SQLITE3
LIBS += -lsqlite3
SRCS = $(wildcard src/lib/*.c src/sqlite_db/*.c src/file_db/*.c src/*.c)
endif
ifeq ($(WERROR), 1)
+45 -15
View File
@@ -7,21 +7,7 @@ extra_c_link_args = ['-Wl,--gc-sections']
sqlite3_dep = dependency('sqlite3', required: false)
use_sqlite3 = get_option('use_sqlite3')
use_werror = get_option('use_werror')
if use_sqlite3
if not sqlite3_dep.found()
error('SQLite3 requested but not found')
endif
deps += sqlite3_dep
add_project_arguments('-DUSE_SQLITE3', language: 'c')
endif
if use_werror
add_project_arguments('-Werror', language: 'c')
endif
executable(
'auth_service',
sources = [
'src/main.c',
'src/user.c',
'src/add_user.c',
@@ -52,6 +38,50 @@ executable(
'src/cors.c',
'src/db_get_user_field_by_id.c',
'src/db_update_user_field.c',
]
with_filedb_sources = [
'src/file_db/filedb.c',
'src/file_db/filedb_delete_user.c',
'src/file_db/filedb_get_all_users.c',
'src/file_db/filedb_get_user_by_id.c',
'src/file_db/filedb_get_user_by_username.c',
'src/file_db/filedb_get_user_field_by_id.c',
'src/file_db/filedb_insert_user.c',
'src/file_db/filedb_update_user.c',
'src/file_db/filedb_update_user_field.c',
]
with_sqlite3_sources = [
'src/sqlite_db/sqlite.c',
'src/sqlite_db/sqlite_delete_user.c',
'src/sqlite_db/sqlite_get_all_users.c',
'src/sqlite_db/sqlite_get_user_by_id.c',
'src/sqlite_db/sqlite_get_user_by_username.c',
'src/sqlite_db/sqlite_get_user_field_by_id.c',
'src/sqlite_db/sqlite_init_db.c',
'src/sqlite_db/sqlite_insert_user.c',
'src/sqlite_db/sqlite_migrate_db.c',
'src/sqlite_db/sqlite_update_user.c',
'src/sqlite_db/sqlite_update_user_field.c',
]
if use_sqlite3
if not sqlite3_dep.found()
error('SQLite3 requested but not found')
endif
deps += sqlite3_dep
sources += with_sqlite3_sources
add_project_arguments('-DUSE_SQLITE3', language: 'c')
endif
if use_werror
add_project_arguments('-Werror', language: 'c')
endif
executable(
'auth_service',
sources + with_filedb_sources,
include_directories: include_dirs,
dependencies: deps,
c_args: extra_c_args,
+3 -25
View File
@@ -5,25 +5,12 @@
#include <string.h>
#ifdef USE_SQLITE3
#include "config.h"
#include "sql.h"
#endif
#ifdef USE_SQLITE3
sqlite3 *db = nullptr;
pthread_mutex_t db_mutex = PTHREAD_MUTEX_INITIALIZER;
#else
pthread_mutex_t users_file_mutex = PTHREAD_MUTEX_INITIALIZER;
#include "sqlite_db/sqlite.h"
#endif
void init_db() {
#ifdef USE_SQLITE3
if (sqlite3_open(cm_get_parameter_as_string(config, "database"), &db) !=
SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
sqlite_init_db();
#else
return;
#endif
@@ -31,16 +18,7 @@ void init_db() {
void migrate_db() {
#ifdef USE_SQLITE3
char *err_msg = 0;
pthread_mutex_lock(&db_mutex);
if (sqlite3_exec(db, CREATE_TABLE_USERS, nullptr, nullptr, &err_msg) !=
SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", err_msg);
sqlite3_free(err_msg);
sqlite3_close(db);
exit(1);
}
pthread_mutex_unlock(&db_mutex);
sqlite_migrate_db();
#else
return;
#endif
-10
View File
@@ -1,15 +1,5 @@
#pragma once
#include <pthread.h>
#ifdef USE_SQLITE3
#include <sqlite3.h>
extern sqlite3 *db;
extern pthread_mutex_t db_mutex;
#else
extern pthread_mutex_t users_file_mutex;
#endif
void init_db();
void migrate_db();
char *json_escape(const char *str);
+4 -68
View File
@@ -1,79 +1,15 @@
#include "database.h"
#include "user.h"
#ifdef USE_SQLITE3
#include "sql.h"
#include "sqlite_db/sqlite.h"
#else
#include "file.h"
#include "file_db/filedb.h"
#endif
bool db_delete_user(const int id) {
#ifdef USE_SQLITE3
pthread_mutex_lock(&db_mutex);
if (!db) {
fprintf(stderr, "Database not initialized\n");
pthread_mutex_unlock(&db_mutex);
return false;
}
sqlite3_stmt *stmt;
int rc;
rc = sqlite3_prepare_v2(db, DELETE_USER_SQL, -1, &stmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
pthread_mutex_unlock(&db_mutex);
return false;
}
sqlite3_bind_int(stmt, 1, id);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
pthread_mutex_unlock(&db_mutex);
return false;
}
int changes = sqlite3_changes(db);
sqlite3_finalize(stmt);
if (changes > 0) {
printf("User deleted successfully. ID: %d\n", id);
} else {
printf("No user found with ID: %d\n", id);
pthread_mutex_unlock(&db_mutex);
return false;
}
pthread_mutex_unlock(&db_mutex);
return true;
return sqlite_delete_user(id);
#else
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
int index = 0;
bool found = false;
cJSON_ArrayForEach(user, users) {
cJSON *user_id = cJSON_GetObjectItem(user, "id");
if (user_id->valueint == id) {
cJSON_DeleteItemFromArray(users, index);
found = true;
break;
}
index++;
}
if (!found) {
pthread_mutex_unlock(&users_file_mutex);
return false;
}
char *new_data = cJSON_Print(users);
write_file("users_db.json", new_data);
free(new_data);
free(data);
cJSON_Delete(users);
pthread_mutex_unlock(&users_file_mutex);
return true;
return filedb_delete_user(id);
#endif
}
+4 -99
View File
@@ -1,110 +1,15 @@
#include "database.h"
#include "user.h"
#ifdef USE_SQLITE3
#include "sql.h"
#include "sqlite_db/sqlite.h"
#else
#include "file.h"
#include "file_db/filedb.h"
#endif
char *db_get_all_users() {
#ifdef USE_SQLITE3
pthread_mutex_lock(&db_mutex);
if (!db) {
fprintf(stderr, "Database not initialized\n");
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
sqlite3_stmt *stmt;
int rc;
rc = sqlite3_prepare_v2(db, SELECT_ALL_USERS_SQL, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
char *result = malloc(1024);
if (!result) {
sqlite3_finalize(stmt);
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
strcpy(result, "[");
size_t result_size = strlen(result);
size_t result_capacity = 1024;
int first_record = 1;
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
int id = sqlite3_column_int(stmt, 0);
const unsigned char *username = sqlite3_column_text(stmt, 1);
const unsigned char *email = sqlite3_column_text(stmt, 2);
const unsigned char *group = sqlite3_column_text(stmt, 4);
int is_superuser = sqlite3_column_int(stmt, 5);
char *escaped_username = json_escape((const char *) username);
char *escaped_email = json_escape((const char *) email);
char *escaped_group = json_escape((const char *) group);
if (!escaped_username || !escaped_email || !escaped_group) {
free(escaped_username);
free(escaped_email);
free(escaped_group);
sqlite3_finalize(stmt);
return NULL;
}
char record[512];
if (sqlite3_column_type(stmt, 2) == SQLITE_NULL) {
snprintf(record, sizeof(record),
"%s{\"id\":%d,\"username\":\"%s\",\"email\":\"%s\",\"group\":"
"\"%s\",\"is_superuser\":%s}",
first_record ? "" : ",", id, escaped_username, escaped_email,
escaped_group, is_superuser ? "true" : "false");
} else {
snprintf(record, sizeof(record),
"%s{\"id\":%d,\"username\":\"%s\",\"email\":\"%s\",\"group\":"
"\"%s\",\"is_superuser\":%s}",
first_record ? "" : ",", id, escaped_username, escaped_email,
escaped_group, is_superuser ? "true" : "false");
}
free(escaped_username);
free(escaped_email);
free(escaped_group);
size_t record_len = strlen(record);
if (result_size + record_len + 10 > result_capacity) {
result_capacity *= 2;
char *new_result = realloc(result, result_capacity);
if (!new_result) {
free(result);
sqlite3_finalize(stmt);
return nullptr;
}
result = new_result;
}
strcat(result, record);
result_size += record_len;
first_record = 0;
}
if (rc != SQLITE_DONE) {
fprintf(stderr, "Error reading data: %s\n", sqlite3_errmsg(db));
free(result);
sqlite3_finalize(stmt);
return NULL;
}
strcat(result, "]");
sqlite3_finalize(stmt);
pthread_mutex_unlock(&db_mutex);
return result;
return sqlite_get_all_users();
#else
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
pthread_mutex_unlock(&users_file_mutex);
return data;
return filedb_get_all_users();
#endif
}
+4 -73
View File
@@ -1,84 +1,15 @@
#include "database.h"
#include "user.h"
#ifdef USE_SQLITE3
#include "sql.h"
#include "sqlite_db/sqlite.h"
#else
#include "cJSON.h"
#include "file.h"
#include "file_db/filedb.h"
#endif
User *db_get_user_by_id(const int id) {
#ifdef USE_SQLITE3
User *user = nullptr;
pthread_mutex_lock(&db_mutex);
if (!db) {
fprintf(stderr, "Database not initialized\n");
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db, SELECT_USER_BY_ID_SQL, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
sqlite3_bind_int(stmt, 1, id);
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
int id = sqlite3_column_int(stmt, 0);
const unsigned char *db_username = sqlite3_column_text(stmt, 1);
const unsigned char *db_email = sqlite3_column_text(stmt, 2);
const unsigned char *db_password = sqlite3_column_text(stmt, 3);
const unsigned char *db_group = sqlite3_column_text(stmt, 4);
int db_is_superuser = sqlite3_column_int(stmt, 5);
user = new_user(id, (char *) db_username, (char *) db_password,
(char *) db_email, (char *) db_group, db_is_superuser);
}
if (rc != SQLITE_DONE) {
fprintf(stderr, "Error reading data: %s\n", sqlite3_errmsg(db));
free_user(user);
sqlite3_finalize(stmt);
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
pthread_mutex_unlock(&db_mutex);
return user;
return sqlite_get_user_by_id(id);
#else
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
User *found = nullptr;
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 (user_id->valueint == id) {
found = new_user(user_id->valueint, user_username->valuestring,
user_password->valuestring, user_email->valuestring,
user_group->valuestring, user_is_superuser->valueint);
break;
}
}
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;
return filedb_get_user_by_id(id);
#endif
}
+4 -73
View File
@@ -1,84 +1,15 @@
#include "database.h"
#include "user.h"
#ifdef USE_SQLITE3
#include "sql.h"
#include "sqlite_db/sqlite.h"
#else
#include "cJSON.h"
#include "file.h"
#include "file_db/filedb.h"
#endif
User *db_get_user_by_username(const char *username) {
#ifdef USE_SQLITE3
User *user = nullptr;
pthread_mutex_lock(&db_mutex);
if (!db) {
fprintf(stderr, "Database not initialized\n");
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
sqlite3_stmt *stmt;
int rc =
sqlite3_prepare_v2(db, SELECT_USER_BY_USERNAME_SQL, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
int id = sqlite3_column_int(stmt, 0);
const unsigned char *db_username = sqlite3_column_text(stmt, 1);
const unsigned char *db_email = sqlite3_column_text(stmt, 2);
const unsigned char *db_password = sqlite3_column_text(stmt, 3);
const unsigned char *db_group = sqlite3_column_text(stmt, 4);
int db_is_superuser = sqlite3_column_int(stmt, 5);
user = new_user(id, (char *) db_username, (char *) db_password,
(char *) db_email, (char *) db_group, db_is_superuser);
}
if (rc != SQLITE_DONE) {
fprintf(stderr, "Error reading data: %s\n", sqlite3_errmsg(db));
free_user(user);
sqlite3_finalize(stmt);
return nullptr;
}
pthread_mutex_unlock(&db_mutex);
return user;
return sqlite_get_user_by_username(username);
#else
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
User *found = nullptr;
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);
break;
}
}
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;
return filedb_get_user_by_username(username);
#endif
}
+4 -73
View File
@@ -1,84 +1,15 @@
#include <pthread.h>
#include "database.h"
#include "user.h"
#ifdef USE_SQLITE3
#include "sql.h"
#include "sqlite_db/sqlite.h"
#else
#include "file.h"
#include "file_db/filedb.h"
#endif
char *db_get_user_field_by_id(const int id, const char *field) {
#ifdef USE_SQLITE3
char *password = nullptr;
pthread_mutex_lock(&db_mutex);
if (!db) {
fprintf(stderr, "Database not initialized\n");
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
char sql[256];
snprintf(sql, sizeof(sql), SELECT_USER_FIELD_SQL, field);
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
sqlite3_bind_int(stmt, 1, id);
rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW) {
const unsigned char *db_password = sqlite3_column_text(stmt, 0);
password = malloc(strlen((const char *) db_password) + 1);
if (password) {
strcpy(password, (const char *) db_password);
}
sqlite3_finalize(stmt);
} else if (rc == SQLITE_DONE) {
fprintf(stderr, "Can't find user");
sqlite3_finalize(stmt);
pthread_mutex_unlock(&db_mutex);
return nullptr;
} else {
fprintf(stderr, "Error executing query: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
pthread_mutex_unlock(&db_mutex);
return password;
return sqlite_get_user_field_by_id(id, field);
#else
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
User *found = nullptr;
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 (user_id->valueint == id) {
found = new_user(user_id->valueint, user_username->valuestring,
user_password->valuestring, user_email->valuestring,
user_group->valuestring, user_is_superuser->valueint);
break;
}
}
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;
return filedb_get_user_field_by_id(id, field);
#endif
}
+4 -106
View File
@@ -1,119 +1,17 @@
#include "database.h"
#include "user.h"
#ifdef USE_SQLITE3
#include "sql.h"
#include "sqlite_db/sqlite.h"
#else
#include "cJSON.h"
#include "file.h"
#include "file_db/filedb.h"
#endif
User *db_insert_user(const char *username, const char *email,
const char *password, const char *group,
const bool *is_superuser) {
#ifdef USE_SQLITE3
pthread_mutex_lock(&db_mutex);
if (!db) {
fprintf(stderr, "Databse not initialized\n");
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
if (!username || !email || !password) {
fprintf(stderr, "username, email, password connot be empty\n");
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
const char *sql = nullptr;
sqlite3_stmt *stmt;
int rc;
if (!group || !is_superuser) {
sql = INSERT_USER_USERNAME_EMAIL_PASSWORD_SQL;
} else if (!group) {
sql = INSERT_USER_USERNAME_EMAIL_PASSWORD_GROUP_SQL;
} else {
sql = INSERT_USER_FULL_SQL;
}
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, email, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, password, -1, SQLITE_STATIC);
if (group) {
sqlite3_bind_text(stmt, 4, group, -1, SQLITE_STATIC);
}
if (is_superuser) {
sqlite3_bind_int(stmt, 5, *is_superuser);
}
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
int inserted_id = sqlite3_last_insert_rowid(db);
sqlite3_finalize(stmt);
printf("User inserted successfully. ID: %d\n", inserted_id);
pthread_mutex_unlock(&db_mutex);
User *new_db_user =
new_user(inserted_id, username, email, password, group, is_superuser);
return new_db_user;
return sqlite_insert_user(username, email, password, group, is_superuser);
#else
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
int users_size = 0;
bool found = false;
cJSON_ArrayForEach(user, users) {
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");
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 == true) {
pthread_mutex_unlock(&users_file_mutex);
return nullptr;
}
cJSON *new_user_data = cJSON_CreateObject();
cJSON_AddNumberToObject(new_user_data, "id", users_size);
cJSON_AddStringToObject(new_user_data, "username", username);
cJSON_AddStringToObject(new_user_data, "password", password);
cJSON_AddStringToObject(new_user_data, "email", email);
cJSON_AddStringToObject(new_user_data, "group", group);
cJSON_AddBoolToObject(new_user_data, "is_superuser", *is_superuser);
char *new_user_data_string = cJSON_Print(new_user_data);
if (users_size == 0) {
users = cJSON_CreateArray();
}
cJSON_AddItemToArray(users, new_user_data);
char *new_data = cJSON_Print(users);
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);
free(new_data);
free(data);
free(new_user_data_string);
// cJSON_Delete(new_user_data);
cJSON_Delete(users);
if (user != nullptr) cJSON_Delete(user);
return return_user;
return filedb_insert_user(username, email, password, group, is_superuser);
#endif
}
+6 -84
View File
@@ -1,97 +1,19 @@
#include <pthread.h>
#include "database.h"
#include "user.h"
#ifdef USE_SQLITE3
#include "sql.h"
#include "sqlite_db/sqlite.h"
#else
#include "cJSON.h"
#include "file.h"
#include "file_db/filedb.h"
#endif
User *db_update_user(const int userid, const char *username, const char *email,
const char *password, const char *group,
const bool *is_superuser) {
#ifdef USE_SQLITE3
pthread_mutex_lock(&db_mutex);
if (!db) {
fprintf(stderr, "Databse not initialized\n");
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
if (!username || !email || !password || !group || !is_superuser) {
fprintf(stderr,
"username, email, password, group, is_superuser connot be empty\n");
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
sqlite3_stmt *stmt;
int rc;
rc = sqlite3_prepare_v2(db, UPDATE_USER_FULL_SQL, -1, &stmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, email, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, password, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 4, group, -1, SQLITE_STATIC);
sqlite3_bind_int(stmt, 5, *is_superuser);
sqlite3_bind_int(stmt, 6, userid);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
int changes = sqlite3_changes(db);
sqlite3_finalize(stmt);
if (changes > 0) {
printf("User updated successfully. ID: %d\n", userid);
} else {
printf("No user found with ID: %d\n", userid);
return nullptr;
}
pthread_mutex_unlock(&db_mutex);
return new_user(userid, username, email, password, group, is_superuser);
return sqlite_update_user(userid, username, email, password, group,
is_superuser);
#else
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
cJSON_ArrayForEach(user, users) {
cJSON *user_id = cJSON_GetObjectItem(user, "id");
if (user_id->valueint == userid) {
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_SetValuestring(user_username, username);
cJSON_SetValuestring(user_password, password);
cJSON_SetValuestring(user_email, email);
cJSON_SetValuestring(user_group, group);
cJSON_DeleteItemFromObject(user, "is_superuser");
cJSON_AddBoolToObject(user, "is_superuser", *is_superuser);
char *new_data = cJSON_Print(users);
write_file("users_db.json", new_data);
free(new_data);
free(data);
cJSON_Delete(users);
User *ret =
new_user(userid, username, password, email, group, is_superuser);
pthread_mutex_unlock(&users_file_mutex);
return ret;
}
}
free(data);
cJSON_Delete(users);
pthread_mutex_unlock(&users_file_mutex);
return nullptr;
return filedb_update_user(userid, username, email, password, group,
is_superuser);
#endif
}
+4 -64
View File
@@ -1,75 +1,15 @@
#include "database.h"
#include "user.h"
#ifdef USE_SQLITE3
#include "sql.h"
#include "sqlite_db/sqlite.h"
#else
#include "cJSON.h"
#include "file.h"
#include "file_db/filedb.h"
#endif
bool db_update_user_field(const int id, const char *field, const char *value) {
#ifdef USE_SQLITE3
pthread_mutex_lock(&db_mutex);
if (!db || !field || !value) {
fprintf(stderr, "invalid parameters\n");
pthread_mutex_unlock(&db_mutex);
return false;
}
char sql[256];
snprintf(sql, sizeof(sql), UPDATE_USER_FIELD_SQL, field);
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "failed to prepare statement: %s\n", sqlite3_errmsg(db));
pthread_mutex_unlock(&db_mutex);
return false;
}
sqlite3_bind_text(stmt, 1, value, -1, SQLITE_STATIC);
sqlite3_bind_int(stmt, 2, id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
if (rc != SQLITE_DONE) {
fprintf(stderr, "execution failed: %s\n", sqlite3_errmsg(db));
pthread_mutex_unlock(&db_mutex);
return false;
}
pthread_mutex_unlock(&db_mutex);
return sqlite3_changes(db) > 0;
return sqlite_update_user_field(id, field, value);
#else
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
cJSON_ArrayForEach(user, users) {
cJSON *user_id = cJSON_GetObjectItem(user, "id");
if (user_id->valueint == id) {
cJSON *user_field = cJSON_GetObjectItem(user, field);
if (cJSON_IsNumber(user_field)) {
// cJSON_SetNumberValue(user_field, value);
return false;
} else if (cJSON_IsBool(user_field)) {
// cJSON_DeleteItemFromObject(user, field);
// cJSON_AddBoolToObject(user, field, value);
return false;
} else if (cJSON_IsString(user_field)) {
cJSON_SetValuestring(user_field, value);
}
char *new_data = cJSON_Print(users);
write_file("users_db.json", new_data);
free(new_data);
free(data);
cJSON_Delete(users);
pthread_mutex_unlock(&users_file_mutex);
return true;
}
}
free(data);
cJSON_Delete(users);
pthread_mutex_unlock(&users_file_mutex);
return false;
return filedb_update_user_field(id, field, value);
#endif
}
+4
View File
@@ -0,0 +1,4 @@
#include "filedb.h"
#include <pthread.h>
pthread_mutex_t users_file_mutex = PTHREAD_MUTEX_INITIALIZER;
+19
View File
@@ -0,0 +1,19 @@
#pragma once
#include "../user.h"
extern pthread_mutex_t users_file_mutex;
bool filedb_delete_user(const int id);
char *filedb_get_all_users();
User *filedb_get_user_by_id(const int id);
User *filedb_get_user_by_username(const char *username);
char *filedb_get_user_field_by_id(const int id, const char *field);
User *filedb_insert_user(const char *username, const char *email,
const char *password, const char *group,
const bool *is_superuser);
User *filedb_update_user(const int userid, const char *username,
const char *email, const char *password,
const char *group, const bool *is_superuser);
bool filedb_update_user_field(const int id, const char *field,
const char *value);
+34
View File
@@ -0,0 +1,34 @@
#include <pthread.h>
#include <stdlib.h>
#include "../file.h"
#include "cJSON.h"
#include "filedb.h"
bool filedb_delete_user(const int id) {
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
int index = 0;
bool found = false;
cJSON_ArrayForEach(user, users) {
cJSON *user_id = cJSON_GetObjectItem(user, "id");
if (user_id->valueint == id) {
cJSON_DeleteItemFromArray(users, index);
found = true;
break;
}
index++;
}
if (!found) {
pthread_mutex_unlock(&users_file_mutex);
return false;
}
char *new_data = cJSON_Print(users);
write_file("users_db.json", new_data);
free(new_data);
free(data);
cJSON_Delete(users);
pthread_mutex_unlock(&users_file_mutex);
return true;
}
+10
View File
@@ -0,0 +1,10 @@
#include <pthread.h>
#include "../file.h"
#include "filedb.h"
char *filedb_get_all_users() {
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
pthread_mutex_unlock(&users_file_mutex);
return data;
}
+37
View File
@@ -0,0 +1,37 @@
#include <pthread.h>
#include "../file.h"
#include "filedb.h"
User *filedb_get_user_by_id(const int id) {
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
User *found = nullptr;
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 (user_id->valueint == id) {
found = new_user(user_id->valueint, user_username->valuestring,
user_password->valuestring, user_email->valuestring,
user_group->valuestring, user_is_superuser->valueint);
break;
}
}
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;
}
+37
View File
@@ -0,0 +1,37 @@
#include <pthread.h>
#include "../file.h"
#include "filedb.h"
User *filedb_get_user_by_username(const char *username) {
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
User *found = nullptr;
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);
break;
}
}
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;
}
+37
View File
@@ -0,0 +1,37 @@
#include <pthread.h>
#include "../file.h"
#include "filedb.h"
char *filedb_get_user_field_by_id(const int id, const char *field) {
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
User *found = nullptr;
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 (user_id->valueint == id) {
found = new_user(user_id->valueint, user_username->valuestring,
user_password->valuestring, user_email->valuestring,
user_group->valuestring, user_is_superuser->valueint);
break;
}
}
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;
}
+55
View File
@@ -0,0 +1,55 @@
#include <pthread.h>
#include "../file.h"
#include "filedb.h"
User *filedb_insert_user(const char *username, const char *email,
const char *password, const char *group,
const bool *is_superuser) {
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
int users_size = 0;
bool found = false;
cJSON_ArrayForEach(user, users) {
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");
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 == true) {
pthread_mutex_unlock(&users_file_mutex);
return nullptr;
}
cJSON *new_user_data = cJSON_CreateObject();
cJSON_AddNumberToObject(new_user_data, "id", users_size);
cJSON_AddStringToObject(new_user_data, "username", username);
cJSON_AddStringToObject(new_user_data, "password", password);
cJSON_AddStringToObject(new_user_data, "email", email);
cJSON_AddStringToObject(new_user_data, "group", group);
cJSON_AddBoolToObject(new_user_data, "is_superuser", *is_superuser);
char *new_user_data_string = cJSON_Print(new_user_data);
if (users_size == 0) {
users = cJSON_CreateArray();
}
cJSON_AddItemToArray(users, new_user_data);
char *new_data = cJSON_Print(users);
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);
free(new_data);
free(data);
free(new_user_data_string);
// cJSON_Delete(new_user_data);
cJSON_Delete(users);
if (user != nullptr) cJSON_Delete(user);
return return_user;
}
+40
View File
@@ -0,0 +1,40 @@
#include <pthread.h>
#include "../file.h"
#include "filedb.h"
User *filedb_update_user(const int userid, const char *username,
const char *email, const char *password,
const char *group, const bool *is_superuser) {
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
cJSON_ArrayForEach(user, users) {
cJSON *user_id = cJSON_GetObjectItem(user, "id");
if (user_id->valueint == userid) {
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_SetValuestring(user_username, username);
cJSON_SetValuestring(user_password, password);
cJSON_SetValuestring(user_email, email);
cJSON_SetValuestring(user_group, group);
cJSON_DeleteItemFromObject(user, "is_superuser");
cJSON_AddBoolToObject(user, "is_superuser", *is_superuser);
char *new_data = cJSON_Print(users);
write_file("users_db.json", new_data);
free(new_data);
free(data);
cJSON_Delete(users);
User *ret =
new_user(userid, username, password, email, group, is_superuser);
pthread_mutex_unlock(&users_file_mutex);
return ret;
}
}
free(data);
cJSON_Delete(users);
pthread_mutex_unlock(&users_file_mutex);
return nullptr;
}
+38
View File
@@ -0,0 +1,38 @@
#include <pthread.h>
#include "../file.h"
#include "filedb.h"
bool filedb_update_user_field(const int id, const char *field,
const char *value) {
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
cJSON_ArrayForEach(user, users) {
cJSON *user_id = cJSON_GetObjectItem(user, "id");
if (user_id->valueint == id) {
cJSON *user_field = cJSON_GetObjectItem(user, field);
if (cJSON_IsNumber(user_field)) {
// cJSON_SetNumberValue(user_field, value);
return false;
} else if (cJSON_IsBool(user_field)) {
// cJSON_DeleteItemFromObject(user, field);
// cJSON_AddBoolToObject(user, field, value);
return false;
} else if (cJSON_IsString(user_field)) {
cJSON_SetValuestring(user_field, value);
}
char *new_data = cJSON_Print(users);
write_file("users_db.json", new_data);
free(new_data);
free(data);
cJSON_Delete(users);
pthread_mutex_unlock(&users_file_mutex);
return true;
}
}
free(data);
cJSON_Delete(users);
pthread_mutex_unlock(&users_file_mutex);
return false;
}
View File
File renamed without changes.
+5
View File
@@ -0,0 +1,5 @@
#include "sqlite.h"
#include "pthread.h"
sqlite3 *db = nullptr;
pthread_mutex_t db_mutex = PTHREAD_MUTEX_INITIALIZER;
+23
View File
@@ -0,0 +1,23 @@
#pragma once
#include <sqlite3.h>
#include "../user.h"
extern sqlite3 *db;
extern pthread_mutex_t db_mutex;
void sqlite_init_db();
void sqlite_migrate_db();
bool sqlite_delete_user(const int id);
char *sqlite_get_all_users();
User *sqlite_get_user_by_id(const int id);
User *sqlite_get_user_by_username(const char *username);
char *sqlite_get_user_field_by_id(const int id, const char *field);
User *sqlite_insert_user(const char *username, const char *email,
const char *password, const char *group,
const bool *is_superuser);
User *sqlite_update_user(const int userid, const char *username,
const char *email, const char *password,
const char *group, const bool *is_superuser);
bool sqlite_update_user_field(const int id, const char *field,
const char *value);
+46
View File
@@ -0,0 +1,46 @@
#include "sqlite.h"
#include "sql.h"
#include <stdio.h>
#include "pthread.h"
bool sqlite_delete_user(const int id) {
pthread_mutex_lock(&db_mutex);
if (!db) {
fprintf(stderr, "Database not initialized\n");
pthread_mutex_unlock(&db_mutex);
return false;
}
sqlite3_stmt *stmt;
int rc;
rc = sqlite3_prepare_v2(db, DELETE_USER_SQL, -1, &stmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
pthread_mutex_unlock(&db_mutex);
return false;
}
sqlite3_bind_int(stmt, 1, id);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
pthread_mutex_unlock(&db_mutex);
return false;
}
int changes = sqlite3_changes(db);
sqlite3_finalize(stmt);
if (changes > 0) {
printf("User deleted successfully. ID: %d\n", id);
} else {
printf("No user found with ID: %d\n", id);
pthread_mutex_unlock(&db_mutex);
return false;
}
pthread_mutex_unlock(&db_mutex);
return true;
}
+102
View File
@@ -0,0 +1,102 @@
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../database.h"
#include "sql.h"
#include "sqlite.h"
char *sqlite_get_all_users() {
pthread_mutex_lock(&db_mutex);
if (!db) {
fprintf(stderr, "Database not initialized\n");
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
sqlite3_stmt *stmt;
int rc;
rc = sqlite3_prepare_v2(db, SELECT_ALL_USERS_SQL, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
char *result = malloc(1024);
if (!result) {
sqlite3_finalize(stmt);
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
strcpy(result, "[");
size_t result_size = strlen(result);
size_t result_capacity = 1024;
int first_record = 1;
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
int id = sqlite3_column_int(stmt, 0);
const unsigned char *username = sqlite3_column_text(stmt, 1);
const unsigned char *email = sqlite3_column_text(stmt, 2);
const unsigned char *group = sqlite3_column_text(stmt, 4);
int is_superuser = sqlite3_column_int(stmt, 5);
char *escaped_username = json_escape((const char *) username);
char *escaped_email = json_escape((const char *) email);
char *escaped_group = json_escape((const char *) group);
if (!escaped_username || !escaped_email || !escaped_group) {
free(escaped_username);
free(escaped_email);
free(escaped_group);
sqlite3_finalize(stmt);
return NULL;
}
char record[512];
if (sqlite3_column_type(stmt, 2) == SQLITE_NULL) {
snprintf(record, sizeof(record),
"%s{\"id\":%d,\"username\":\"%s\",\"email\":\"%s\",\"group\":"
"\"%s\",\"is_superuser\":%s}",
first_record ? "" : ",", id, escaped_username, escaped_email,
escaped_group, is_superuser ? "true" : "false");
} else {
snprintf(record, sizeof(record),
"%s{\"id\":%d,\"username\":\"%s\",\"email\":\"%s\",\"group\":"
"\"%s\",\"is_superuser\":%s}",
first_record ? "" : ",", id, escaped_username, escaped_email,
escaped_group, is_superuser ? "true" : "false");
}
free(escaped_username);
free(escaped_email);
free(escaped_group);
size_t record_len = strlen(record);
if (result_size + record_len + 10 > result_capacity) {
result_capacity *= 2;
char *new_result = realloc(result, result_capacity);
if (!new_result) {
free(result);
sqlite3_finalize(stmt);
return nullptr;
}
result = new_result;
}
strcat(result, record);
result_size += record_len;
first_record = 0;
}
if (rc != SQLITE_DONE) {
fprintf(stderr, "Error reading data: %s\n", sqlite3_errmsg(db));
free(result);
sqlite3_finalize(stmt);
return NULL;
}
strcat(result, "]");
sqlite3_finalize(stmt);
pthread_mutex_unlock(&db_mutex);
return result;
}
+44
View File
@@ -0,0 +1,44 @@
#include "sql.h"
#include "sqlite.h"
#include "pthread.h"
User *sqlite_get_user_by_id(const int id) {
User *user = nullptr;
pthread_mutex_lock(&db_mutex);
if (!db) {
fprintf(stderr, "Database not initialized\n");
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db, SELECT_USER_BY_ID_SQL, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
sqlite3_bind_int(stmt, 1, id);
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
int id = sqlite3_column_int(stmt, 0);
const unsigned char *db_username = sqlite3_column_text(stmt, 1);
const unsigned char *db_email = sqlite3_column_text(stmt, 2);
const unsigned char *db_password = sqlite3_column_text(stmt, 3);
const unsigned char *db_group = sqlite3_column_text(stmt, 4);
int db_is_superuser = sqlite3_column_int(stmt, 5);
user = new_user(id, (char *) db_username, (char *) db_password,
(char *) db_email, (char *) db_group, db_is_superuser);
}
if (rc != SQLITE_DONE) {
fprintf(stderr, "Error reading data: %s\n", sqlite3_errmsg(db));
free_user(user);
sqlite3_finalize(stmt);
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
pthread_mutex_unlock(&db_mutex);
return user;
}
@@ -0,0 +1,44 @@
#include "sql.h"
#include "sqlite.h"
#include "pthread.h"
User *sqlite_get_user_by_username(const char *username) {
User *user = nullptr;
pthread_mutex_lock(&db_mutex);
if (!db) {
fprintf(stderr, "Database not initialized\n");
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
sqlite3_stmt *stmt;
int rc =
sqlite3_prepare_v2(db, SELECT_USER_BY_USERNAME_SQL, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
int id = sqlite3_column_int(stmt, 0);
const unsigned char *db_username = sqlite3_column_text(stmt, 1);
const unsigned char *db_email = sqlite3_column_text(stmt, 2);
const unsigned char *db_password = sqlite3_column_text(stmt, 3);
const unsigned char *db_group = sqlite3_column_text(stmt, 4);
int db_is_superuser = sqlite3_column_int(stmt, 5);
user = new_user(id, (char *) db_username, (char *) db_password,
(char *) db_email, (char *) db_group, db_is_superuser);
}
if (rc != SQLITE_DONE) {
fprintf(stderr, "Error reading data: %s\n", sqlite3_errmsg(db));
free_user(user);
sqlite3_finalize(stmt);
return nullptr;
}
pthread_mutex_unlock(&db_mutex);
return user;
}
@@ -0,0 +1,44 @@
#include "pthread.h"
#include "sql.h"
#include "sqlite.h"
char *sqlite_get_user_field_by_id(const int id, const char *field) {
char *password = nullptr;
pthread_mutex_lock(&db_mutex);
if (!db) {
fprintf(stderr, "Database not initialized\n");
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
char sql[256];
snprintf(sql, sizeof(sql), SELECT_USER_FIELD_SQL, field);
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
sqlite3_bind_int(stmt, 1, id);
rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW) {
const unsigned char *db_password = sqlite3_column_text(stmt, 0);
password = malloc(strlen((const char *) db_password) + 1);
if (password) {
strcpy(password, (const char *) db_password);
}
sqlite3_finalize(stmt);
} else if (rc == SQLITE_DONE) {
fprintf(stderr, "Can't find user");
sqlite3_finalize(stmt);
pthread_mutex_unlock(&db_mutex);
return nullptr;
} else {
fprintf(stderr, "Error executing query: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
pthread_mutex_unlock(&db_mutex);
return password;
}
+11
View File
@@ -0,0 +1,11 @@
#include "../config.h"
#include "sqlite.h"
void sqlite_init_db() {
if (sqlite3_open(cm_get_parameter_as_string(config, "database"), &db) !=
SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
}
+63
View File
@@ -0,0 +1,63 @@
#include "pthread.h"
#include "sql.h"
#include "sqlite.h"
User *sqlite_insert_user(const char *username, const char *email,
const char *password, const char *group,
const bool *is_superuser) {
pthread_mutex_lock(&db_mutex);
if (!db) {
fprintf(stderr, "Databse not initialized\n");
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
if (!username || !email || !password) {
fprintf(stderr, "username, email, password connot be empty\n");
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
const char *sql = nullptr;
sqlite3_stmt *stmt;
int rc;
if (!group || !is_superuser) {
sql = INSERT_USER_USERNAME_EMAIL_PASSWORD_SQL;
} else if (!group) {
sql = INSERT_USER_USERNAME_EMAIL_PASSWORD_GROUP_SQL;
} else {
sql = INSERT_USER_FULL_SQL;
}
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, email, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, password, -1, SQLITE_STATIC);
if (group) {
sqlite3_bind_text(stmt, 4, group, -1, SQLITE_STATIC);
}
if (is_superuser) {
sqlite3_bind_int(stmt, 5, *is_superuser);
}
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
int inserted_id = sqlite3_last_insert_rowid(db);
sqlite3_finalize(stmt);
printf("User inserted successfully. ID: %d\n", inserted_id);
pthread_mutex_unlock(&db_mutex);
User *new_db_user =
new_user(inserted_id, username, email, password, group, is_superuser);
return new_db_user;
}
+16
View File
@@ -0,0 +1,16 @@
#include "pthread.h"
#include "sql.h"
#include "sqlite.h"
void sqlite_migrate_db() {
char *err_msg = 0;
pthread_mutex_lock(&db_mutex);
if (sqlite3_exec(db, CREATE_TABLE_USERS, nullptr, nullptr, &err_msg) !=
SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", err_msg);
sqlite3_free(err_msg);
sqlite3_close(db);
exit(1);
}
pthread_mutex_unlock(&db_mutex);
}
+55
View File
@@ -0,0 +1,55 @@
#include "pthread.h"
#include "sql.h"
#include "sqlite.h"
User *sqlite_update_user(const int userid, const char *username,
const char *email, const char *password,
const char *group, const bool *is_superuser) {
pthread_mutex_lock(&db_mutex);
if (!db) {
fprintf(stderr, "Databse not initialized\n");
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
if (!username || !email || !password || !group || !is_superuser) {
fprintf(stderr,
"username, email, password, group, is_superuser connot be empty\n");
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
sqlite3_stmt *stmt;
int rc;
rc = sqlite3_prepare_v2(db, UPDATE_USER_FULL_SQL, -1, &stmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, email, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, password, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 4, group, -1, SQLITE_STATIC);
sqlite3_bind_int(stmt, 5, *is_superuser);
sqlite3_bind_int(stmt, 6, userid);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
pthread_mutex_unlock(&db_mutex);
return nullptr;
}
int changes = sqlite3_changes(db);
sqlite3_finalize(stmt);
if (changes > 0) {
printf("User updated successfully. ID: %d\n", userid);
} else {
printf("No user found with ID: %d\n", userid);
return nullptr;
}
pthread_mutex_unlock(&db_mutex);
return new_user(userid, username, email, password, group, is_superuser);
}
+36
View File
@@ -0,0 +1,36 @@
#include "pthread.h"
#include "sql.h"
#include "sqlite.h"
bool sqlite_update_user_field(const int id, const char *field,
const char *value) {
pthread_mutex_lock(&db_mutex);
if (!db || !field || !value) {
fprintf(stderr, "invalid parameters\n");
pthread_mutex_unlock(&db_mutex);
return false;
}
char sql[256];
snprintf(sql, sizeof(sql), UPDATE_USER_FIELD_SQL, field);
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "failed to prepare statement: %s\n", sqlite3_errmsg(db));
pthread_mutex_unlock(&db_mutex);
return false;
}
sqlite3_bind_text(stmt, 1, value, -1, SQLITE_STATIC);
sqlite3_bind_int(stmt, 2, id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
if (rc != SQLITE_DONE) {
fprintf(stderr, "execution failed: %s\n", sqlite3_errmsg(db));
pthread_mutex_unlock(&db_mutex);
return false;
}
pthread_mutex_unlock(&db_mutex);
return sqlite3_changes(db) > 0;
}