Remove database name config option, instead name can't be changed

Add create_data_dir function in util.c
This commit is contained in:
Night Kaly 2024-09-20 17:50:24 +01:00
parent 79defadf12
commit a927c5c36c
Signed by: night0721
GPG key ID: 957D67B8DB7A119B
4 changed files with 22 additions and 7 deletions

View file

@ -3,7 +3,6 @@
#define PORT 20247 #define PORT 20247
#define MAX_NAME 32 /* Max username length */ #define MAX_NAME 32 /* Max username length */
#define DATABASE_NAME "test.db"
#define MAX_DATA_LENGTH 8192 #define MAX_DATA_LENGTH 8192
/* Don't touch unless you know what you are doing */ /* Don't touch unless you know what you are doing */

View file

@ -11,6 +11,7 @@ void *memalloc(size_t size);
void *estrdup(void *str); void *estrdup(void *str);
char *replace_home(char *str); char *replace_home(char *str);
void mkdir_p(const char *destdir); void mkdir_p(const char *destdir);
void create_data_dir();
void write_log(int type, const char *fmt, ...); void write_log(int type, const char *fmt, ...);
void print_bin(const unsigned char *ptr, size_t length); void print_bin(const unsigned char *ptr, size_t length);

View file

@ -123,16 +123,24 @@ void mkdir_p(const char *destdir)
return; return;
} }
void create_data_dir()
{
char *client_data_dir = estrdup(CLIENT_DATA_DIR);
mkdir_p(client_data_dir);
client_data_dir = replace_home(client_data_dir);
free(client_data_dir);
}
void write_log(int type, const char *fmt, ...) void write_log(int type, const char *fmt, ...)
{ {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
char *client_data_dir = estrdup(CLIENT_DATA_DIR);
mkdir_p(client_data_dir);
client_data_dir = replace_home(client_data_dir);
char *client_log = memalloc(PATH_MAX); char *client_log = memalloc(PATH_MAX);
snprintf(client_log, PATH_MAX, "%s/%s", client_data_dir, "zen.log"); snprintf(client_log, PATH_MAX, "%s/%s", CLIENT_DATA_DIR, "zen.log");
free(client_data_dir); if (access(client_log, W_OK) != 0) {
/* If log file doesn't exist, most likely data dir won't exist too */
create_data_dir();
}
FILE *log = fopen(client_log, "a"); FILE *log = fopen(client_log, "a");
if (log != NULL) { if (log != NULL) {
time_t now = time(NULL); time_t now = time(NULL);

View file

@ -31,7 +31,14 @@ int sqlite_init()
sqlite3 *db; sqlite3 *db;
char *err_msg = 0; char *err_msg = 0;
int rc = sqlite3_open(DATABASE_NAME, &db); char *zen_db = memalloc(PATH_MAX);
snprintf(zen_db, PATH_MAX, "%s/%s", CLIENT_DATA_DIR, "zen.db");
if (access(zen_db, W_OK) != 0) {
/* If log file doesn't exist, most likely data dir won't exist too */
create_data_dir();
}
int rc = sqlite3_open(zen_db, &db);
if (rc != SQLITE_OK) { if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db)); fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));