Fix data dir and its files creation

This commit is contained in:
Night Kaly 2024-09-20 18:23:34 +01:00
parent eb3a859e97
commit 68f5fd5f7c
Signed by: night0721
GPG key ID: 957D67B8DB7A119B
3 changed files with 12 additions and 15 deletions

View file

@ -11,7 +11,6 @@ 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

@ -70,7 +70,6 @@ char *replace_home(char *str)
char *newstr = memalloc(strlen(str) + strlen(home)); char *newstr = memalloc(strlen(str) + strlen(home));
/* replace ~ with home */ /* replace ~ with home */
snprintf(newstr, strlen(str) + strlen(home), "%s%s", home, str + 1); snprintf(newstr, strlen(str) + strlen(home), "%s%s", home, str + 1);
free(str);
return newstr; return newstr;
} }
@ -123,24 +122,21 @@ 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_log = memalloc(PATH_MAX); char *client_log = memalloc(PATH_MAX);
snprintf(client_log, PATH_MAX, "%s/%s", CLIENT_DATA_DIR, "/zen.log"); char *data_dir = replace_home(CLIENT_DATA_DIR);
snprintf(client_log, PATH_MAX, "%s/%s", data_dir, "zen.log");
printf("log: %s\n", client_log);
free(data_dir);
if (access(client_log, W_OK) != 0) { if (access(client_log, W_OK) != 0) {
/* If log file doesn't exist, most likely data dir won't exist too */ /* If log file doesn't exist, most likely data dir won't exist too */
create_data_dir(); mkdir_p(CLIENT_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

@ -32,10 +32,12 @@ int sqlite_init()
char *err_msg = 0; char *err_msg = 0;
char *zen_db = memalloc(PATH_MAX); char *zen_db = memalloc(PATH_MAX);
snprintf(zen_db, PATH_MAX, "%s/%s", CLIENT_DATA_DIR, "/zen.db"); char *data_dir = replace_home(CLIENT_DATA_DIR);
snprintf(zen_db, PATH_MAX, "%s/%s", data_dir, "data.db");
free(data_dir);
if (access(zen_db, W_OK) != 0) { if (access(zen_db, W_OK) != 0) {
/* If log file doesn't exist, most likely data dir won't exist too */ /* If data file doesn't exist, most likely data dir won't exist too */
create_data_dir(); mkdir_p(CLIENT_DATA_DIR);
} }
int rc = sqlite3_open(zen_db, &db); int rc = sqlite3_open(zen_db, &db);