From 94bc38df829c4816e629c7dcaed31b1e7c75bc4b Mon Sep 17 00:00:00 2001 From: gramanas Date: Mon, 16 Apr 2018 14:34:30 +0300 Subject: Add asan option to cmake finish init without any asan warnings --- src/dblayer.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) (limited to 'src/dblayer.c') diff --git a/src/dblayer.c b/src/dblayer.c index 180cd93..a40fd85 100644 --- a/src/dblayer.c +++ b/src/dblayer.c @@ -6,17 +6,30 @@ #include #include #include +#include #include "dblayer.h" -int db_exists() { - FILE *db_file; +const char * const DB_NAME = "/ckdb"; - if ((db_file = fopen("/home/gramanas/.ck/ckdb", "rb")) == NULL) { - return 0; +char *make_db_name(char * confPath) { + char *db_path = strdup(confPath); + + db_path = realloc(db_path, strlen(confPath)+strlen(DB_NAME)+1); + strcat(db_path, DB_NAME); + + return db_path; +} + +int db_exists(UserOpt *opt) { + char *db_path = make_db_name(opt->confDir); + int ret; + ret = 0; + if (access(db_path, F_OK) == 0) { + ret = 1; } - fclose(db_file); - return 1; + free(db_path); + return ret; } // check if db has the correct tables @@ -62,24 +75,27 @@ void close_DB(DB *db) { sqlite3_close(db->ptr); } -DB init_make_DB() { +DB init_make_DB(UserOpt *opt) { sqlite3 *db; int rc; - rc = sqlite3_open("/home/gramanas/.ck/ckdb", &db); - - if (rc) { + char *db_path = make_db_name(opt->confDir); + rc = sqlite3_open(db_path, &db); + free(db_path); + if (rc != SQLITE_OK) { return empty_DB(SQL_ERR_NO_DB_FILE); } return new_DB(db); } -DB open_DB() { +DB open_DB(UserOpt *opt) { sqlite3 *db; int rc; - rc = sqlite3_open("/home/gramanas/.ck/ckdb", &db); + char *db_path = make_db_name(opt->confDir); + rc = sqlite3_open(db_path, &db); + free(db_path); if (rc) { return empty_DB(SQL_ERR_NO_DB_FILE); -- cgit v1.2.3