fully featured json file database
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 12s
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-Without-sqlite3 (push) Successful in 8s
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 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 12s
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-Without-sqlite3 (push) Successful in 8s
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 12s
This commit is contained in:
1 parent
5fda74ab1d
commit
bed968b4e4
11 files changed
+107
-76
No files matched your search
@@ -13,7 +13,6 @@
|
|||||||
sqlite3 *db = nullptr;
|
sqlite3 *db = nullptr;
|
||||||
pthread_mutex_t db_mutex = PTHREAD_MUTEX_INITIALIZER;
|
pthread_mutex_t db_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
#else
|
#else
|
||||||
FILE *users_file = nullptr;
|
|
||||||
pthread_mutex_t users_file_mutex = PTHREAD_MUTEX_INITIALIZER;
|
pthread_mutex_t users_file_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,6 @@
|
|||||||
extern sqlite3 *db;
|
extern sqlite3 *db;
|
||||||
extern pthread_mutex_t db_mutex;
|
extern pthread_mutex_t db_mutex;
|
||||||
#else
|
#else
|
||||||
#include <stdio.h>
|
|
||||||
extern FILE *users_file;
|
|
||||||
extern pthread_mutex_t users_file_mutex;
|
extern pthread_mutex_t users_file_mutex;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,12 @@
|
|||||||
#include "database.h"
|
#include "database.h"
|
||||||
|
#include "user.h"
|
||||||
|
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
#include "sql.h"
|
#include "sql.h"
|
||||||
#endif
|
#else
|
||||||
|
|
||||||
#ifndef USE_SQLITE3
|
|
||||||
#include "file.h"
|
#include "file.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "user.h"
|
|
||||||
|
|
||||||
bool db_delete_user(const int id) {
|
bool db_delete_user(const int id) {
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
pthread_mutex_lock(&db_mutex);
|
pthread_mutex_lock(&db_mutex);
|
||||||
|
|||||||
@@ -1,12 +1,9 @@
|
|||||||
#include "database.h"
|
#include "database.h"
|
||||||
|
#include "user.h"
|
||||||
|
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
#include "sql.h"
|
#include "sql.h"
|
||||||
#endif
|
#else
|
||||||
|
|
||||||
#include "user.h"
|
|
||||||
|
|
||||||
#ifndef USE_SQLITE3
|
|
||||||
#include "file.h"
|
#include "file.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,10 @@
|
|||||||
#ifndef USE_SQLITE3
|
|
||||||
#include "cJSON.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "database.h"
|
#include "database.h"
|
||||||
|
#include "user.h"
|
||||||
|
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
#include "sql.h"
|
#include "sql.h"
|
||||||
#endif
|
#else
|
||||||
|
#include "cJSON.h"
|
||||||
#include "user.h"
|
|
||||||
|
|
||||||
#ifndef USE_SQLITE3
|
|
||||||
#include "file.h"
|
#include "file.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,10 @@
|
|||||||
#ifndef USE_SQLITE3
|
|
||||||
#include "cJSON.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "database.h"
|
#include "database.h"
|
||||||
|
#include "user.h"
|
||||||
|
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
#include "sql.h"
|
#include "sql.h"
|
||||||
#endif
|
#else
|
||||||
|
#include "cJSON.h"
|
||||||
#include "user.h"
|
|
||||||
|
|
||||||
#ifndef USE_SQLITE3
|
|
||||||
#include "file.h"
|
#include "file.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -75,11 +69,11 @@ User *db_get_user_by_username(const char *username) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (found == nullptr) {
|
if (found == nullptr) {
|
||||||
pthread_mutex_unlock(&users_file_mutex);
|
pthread_mutex_unlock(&users_file_mutex);
|
||||||
// if (user != nullptr) cJSON_Delete(user);
|
// if (user != nullptr) cJSON_Delete(user);
|
||||||
cJSON_Delete(users);
|
cJSON_Delete(users);
|
||||||
free(data);
|
free(data);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&users_file_mutex);
|
pthread_mutex_unlock(&users_file_mutex);
|
||||||
// if (user != nullptr) cJSON_Delete(user);
|
// if (user != nullptr) cJSON_Delete(user);
|
||||||
|
|||||||
@@ -1,14 +1,10 @@
|
|||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
#include "database.h"
|
#include "database.h"
|
||||||
|
#include "user.h"
|
||||||
|
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
#include "sql.h"
|
#include "sql.h"
|
||||||
#endif
|
#else
|
||||||
|
|
||||||
#include "user.h"
|
|
||||||
|
|
||||||
#ifndef USE_SQLITE3
|
|
||||||
#include "file.h"
|
#include "file.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
+11
-22
@@ -1,16 +1,10 @@
|
|||||||
#ifndef USE_SQLITE3
|
|
||||||
#include "cJSON.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "database.h"
|
#include "database.h"
|
||||||
|
#include "user.h"
|
||||||
|
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
#include "sql.h"
|
#include "sql.h"
|
||||||
#endif
|
#else
|
||||||
|
#include "cJSON.h"
|
||||||
#include "user.h"
|
|
||||||
|
|
||||||
#ifndef USE_SQLITE3
|
|
||||||
#include "file.h"
|
#include "file.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -98,18 +92,12 @@ User *db_insert_user(const char *username, const char *email,
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
cJSON *new_user_data = cJSON_CreateObject();
|
cJSON *new_user_data = cJSON_CreateObject();
|
||||||
cJSON *new_user_id = cJSON_CreateNumber(users_size);
|
cJSON_AddNumberToObject(new_user_data, "id", users_size);
|
||||||
cJSON *new_user_username = cJSON_CreateString(username);
|
cJSON_AddStringToObject(new_user_data, "username", username);
|
||||||
cJSON *new_user_password = cJSON_CreateString(password);
|
cJSON_AddStringToObject(new_user_data, "password", password);
|
||||||
cJSON *new_user_email = cJSON_CreateString(email);
|
cJSON_AddStringToObject(new_user_data, "email", email);
|
||||||
cJSON *new_user_group = cJSON_CreateString(group);
|
cJSON_AddStringToObject(new_user_data, "group", group);
|
||||||
cJSON *new_user_is_superuser = cJSON_CreateBool(is_superuser?1:0);
|
cJSON_AddBoolToObject(new_user_data, "is_superuser", *is_superuser);
|
||||||
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);
|
|
||||||
cJSON_AddItemToObject(new_user_data, "email", new_user_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);
|
char *new_user_data_string = cJSON_Print(new_user_data);
|
||||||
if (users_size == 0) {
|
if (users_size == 0) {
|
||||||
users = cJSON_CreateArray();
|
users = cJSON_CreateArray();
|
||||||
@@ -123,7 +111,8 @@ User *db_insert_user(const char *username, const char *email,
|
|||||||
free(new_data);
|
free(new_data);
|
||||||
free(data);
|
free(data);
|
||||||
free(new_user_data_string);
|
free(new_user_data_string);
|
||||||
cJSON_Delete(new_user_data);
|
// cJSON_Delete(new_user_data);
|
||||||
|
cJSON_Delete(users);
|
||||||
if (user != nullptr) cJSON_Delete(user);
|
if (user != nullptr) cJSON_Delete(user);
|
||||||
return return_user;
|
return return_user;
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+40
-6
@@ -1,10 +1,14 @@
|
|||||||
#ifdef USE_SQLITE3
|
#include <pthread.h>
|
||||||
#include "database.h"
|
#include "database.h"
|
||||||
#include "sql.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "user.h"
|
#include "user.h"
|
||||||
|
|
||||||
|
#ifdef USE_SQLITE3
|
||||||
|
#include "sql.h"
|
||||||
|
#else
|
||||||
|
#include "cJSON.h"
|
||||||
|
#include "file.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
User *db_update_user(const int userid, const char *username, const char *email,
|
User *db_update_user(const int userid, const char *username, const char *email,
|
||||||
const char *password, const char *group,
|
const char *password, const char *group,
|
||||||
const bool *is_superuser) {
|
const bool *is_superuser) {
|
||||||
@@ -57,7 +61,37 @@ User *db_update_user(const int userid, const char *username, const char *email,
|
|||||||
pthread_mutex_unlock(&db_mutex);
|
pthread_mutex_unlock(&db_mutex);
|
||||||
return new_user(userid, username, email, password, group, is_superuser);
|
return new_user(userid, username, email, password, group, is_superuser);
|
||||||
#else
|
#else
|
||||||
User *asdf = new_user(0, "asdf", "asdf", "asdf", "asdf", false);
|
pthread_mutex_lock(&users_file_mutex);
|
||||||
return asdf;
|
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;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,13 @@
|
|||||||
#ifdef USE_SQLITE3
|
|
||||||
#include "database.h"
|
#include "database.h"
|
||||||
#include "sql.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "user.h"
|
#include "user.h"
|
||||||
|
|
||||||
|
#ifdef USE_SQLITE3
|
||||||
|
#include "sql.h"
|
||||||
|
#else
|
||||||
|
#include "cJSON.h"
|
||||||
|
#include "file.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
bool db_update_user_field(const int id, const char *field, const char *value) {
|
bool db_update_user_field(const int id, const char *field, const char *value) {
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
pthread_mutex_lock(&db_mutex);
|
pthread_mutex_lock(&db_mutex);
|
||||||
@@ -37,6 +40,36 @@ bool db_update_user_field(const int id, const char *field, const char *value) {
|
|||||||
pthread_mutex_unlock(&db_mutex);
|
pthread_mutex_unlock(&db_mutex);
|
||||||
return sqlite3_changes(db) > 0;
|
return sqlite3_changes(db) > 0;
|
||||||
#else
|
#else
|
||||||
return 0;
|
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;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
+1
-1
@@ -28,7 +28,7 @@ void register_user(struct mg_connection *c, struct mg_http_message *hm) {
|
|||||||
const bool is_superuser_value = is_superuser->valueint ? true : false;
|
const bool is_superuser_value = is_superuser->valueint ? true : false;
|
||||||
User *new_user_data = db_insert_user(
|
User *new_user_data = db_insert_user(
|
||||||
username->valuestring, email->valuestring, password->valuestring,
|
username->valuestring, email->valuestring, password->valuestring,
|
||||||
group->valuestring, (const bool *)is_superuser_value);
|
group->valuestring, &is_superuser_value);
|
||||||
if (new_user_data == nullptr) {
|
if (new_user_data == nullptr) {
|
||||||
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||||
MG_ESC("failed to create user"));
|
MG_ESC("failed to create user"));
|
||||||
|
|||||||
Reference in new issue
Block a user