From 54ae271dfc5f8405233ed320b2e9bd821ee8210f Mon Sep 17 00:00:00 2001 From: gramanas Date: Fri, 16 Nov 2018 14:43:19 +0200 Subject: Code restructure/simplification, restore regression test --- src/dblayer.c | 830 ++++------------------------------------------------------ 1 file changed, 53 insertions(+), 777 deletions(-) (limited to 'src/dblayer.c') diff --git a/src/dblayer.c b/src/dblayer.c index a6e6193..12be997 100644 --- a/src/dblayer.c +++ b/src/dblayer.c @@ -11,16 +11,16 @@ #include #include "dblayer.h" -#include "dbhelper.h" +#include "queries.h" #include "ckutil.h" #include "ckerrlog.h" ERRLOG(ckdb); -const char * const DB_FILE_NAME = "/ckdb"; +static const char * const DB_FILE_NAME = "/ckdb"; /* figure out the database name */ -void make_db_name(char *ret, const char *confPath) { +static void make_db_name(char *ret, const char *confPath) { char db_path[STR_L] = ""; strcpy(db_path, confPath); strcat(db_path, DB_FILE_NAME); @@ -36,7 +36,7 @@ int db_exists(const UserOpt *opt) { } /* check if db has the correct tables */ -int check_initialized_DB(sqlite3 *db) { +static int check_initialized_DB(sqlite3 *db) { char sql[STR_M] = ""; dbh_form_query_select_all_tables(sql); sqlite3_stmt *stmt; @@ -67,14 +67,6 @@ int check_initialized_DB(sqlite3 *db) { return 0; } -DB empty_DB(SqlError err) { - return (DB){ .ptr = NULL, .error = err }; -} - -DB new_DB(sqlite3 *db) { - return (DB){ .ptr = db, .error = SQL_NO_ERR }; -} - void close_DB(DB *db) { sqlite3_close(db->ptr); } @@ -94,149 +86,17 @@ int open_DB(DB *db, const UserOpt *opt) { return -1; } - if (check_initialized_DB(db->ptr)) { - ERR("The database file is currupted. Run ck init anew."); - return -1; + if (opt->action != CKA_INIT) { + if (check_initialized_DB(db->ptr)) { + ERR("The database file is currupted. Run ck init anew."); + return -1; + } } LOG("Opened %s", db_path); return 0; } -int init_make_DB(DB *db, const UserOpt *opt) { - char db_path[STR_L] = ""; - int rc; - - make_db_name(db_path, opt->confDir); - rc = sqlite3_open(db_path, &db->ptr); - if (rc != SQLITE_OK) { - return -1; - } - - return 0; -} - -void init_make_tables(DB *db) { - char sql[STR_L] = ""; - dbh_form_query_make_tables(sql); - - int rc = sqlite3_exec(db->ptr, sql, 0, 0, 0); - if (rc != SQLITE_OK ) { - PRINT_ERR("Could not create empty db."); - db->error = SQL_ERR_SQLITE; - return; - } -} - -int get_next_valid_id_from_table(DB *db, const char* tableName) { - sqlite3_stmt *stmt; - int rc; - - char sql[STR_M] = ""; - dbh_form_query_select_id_from(sql, tableName); - - rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - return -1; - } - sqlite3_bind_text(stmt, 1, tableName, (int)strlen(tableName), 0); - - int id = 0; - while (sqlite3_step(stmt) == SQLITE_ROW) { - int a = sqlite3_column_int(stmt, 0); - if (a != id) { - break; - } - id++; - } - sqlite3_finalize(stmt); - return id; -} - -int insert_to_program_table(DB *db, const char *name) { - sqlite3_stmt *stmt; - int rc; - - char sql[STR_L] = ""; - dbh_form_query_insert_program(sql); - - rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - PRINT_ERR("while preparing insert to program sql."); - db->error = SQL_ERR_SQLITE; - return -1; - } - int id = get_next_valid_id_from_table(db, TBL_PROGRAM); - if (id == -1) { - db->error = SQL_ERR_SQLITE; - return -1; - } - sqlite3_bind_int(stmt, 1, id); - sqlite3_bind_text(stmt, 2, name, (int)strlen(name), 0); - if (sqlite3_step(stmt) != SQLITE_DONE) { - PRINT_ERR("while excecuting insert to program sql."); - db->error = SQL_ERR_SQLITE; - return -1; - } - sqlite3_finalize(stmt); - return id; -} - -int insert_to_config_table(DB *db, const char *path, const int secret, const int prime) { - sqlite3_stmt *stmt; - int rc; - - char sql[STR_L] = ""; - dbh_form_query_insert_config(sql); - rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - PRINT_ERR("Error while preparing insert to config sql."); - db->error = SQL_ERR_SQLITE; - return -1; - } - int id = get_next_valid_id_from_table(db, TBL_CONFIG); - if (id == -1) { - db->error = SQL_ERR_SQLITE; - return -1; - } - sqlite3_bind_int(stmt, 1, id); - sqlite3_bind_text(stmt, 2, path, (int)strlen(path), 0); - sqlite3_bind_int(stmt, 3, secret); - sqlite3_bind_int(stmt, 4, prime); - if (sqlite3_step(stmt) != SQLITE_DONE) { - PRINT_ERR("Error while excecuting insert to config sql."); - db->error = SQL_ERR_SQLITE; - return-1; - } - sqlite3_finalize(stmt); - return id; -} - -int insert_to_rel_table(DB *db, const int pid, const int cid) { - sqlite3_stmt *stmt; - int rc; - - char sql[STR_M] = ""; - dhb_form_query_insert_relationship(sql); - rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - db->error = SQL_ERR_SQLITE; - PRINT_ERR("while preparing insert to rel sql."); - return -1; - } - sqlite3_bind_int(stmt, 1, pid); - sqlite3_bind_int(stmt, 2, cid); - if (sqlite3_step(stmt) != SQLITE_DONE) { - db->error = SQL_ERR_SQLITE; - PRINT_ERR("while excecuting insert to rel sql."); - return-1; - } - sqlite3_finalize(stmt); - return 1; -} - -/* Returns -2 or error, -1 if program doesn't exist - * else the program ID */ int get_program_id(DB *db, const char* name) { sqlite3_stmt *stmt; int rc; @@ -288,12 +148,6 @@ int get_config_id(DB *db, const char* path) { return id; } -int add_insert_relationship(DB *db, const int pid, const int cid) { - // do checks - return insert_to_rel_table(db, pid, cid); -} - -/* Returns the path of the found config via *ret */ int program_has_primary_config(DB *db, const int pid, char *ret, int *sec) { sqlite3_stmt *stmt; int rc; @@ -335,185 +189,75 @@ int program_has_primary_config(DB *db, const int pid, char *ret, int *sec) { return 0; } -int add_get_or_insert_config_to_db(DB *db, const int pid, const char *path, const int secret, const int prime, const char *home) { - char tpath[STR_L] = ""; - if (!swap_home_with_tilde(tpath, path, home)) { - strcpy(tpath, path); - } - int cid = get_config_id(db, tpath); - if (cid == -2) { - db->error = SQL_ERR_SQLITE; - return -1; - } - /* If config doesnt exist insert it and return it's cid */ - if (cid == -1) { - if (program_has_primary_config(db, pid, NULL, NULL) && prime) { - db->error = SQL_ERR_PRIMARY_REDEFINITION; - return -1; - } - return insert_to_config_table(db, tpath, secret, prime); - } +int get_program_relations(DB *db, int pid) { + int count = -1; + sqlite3_stmt *stmt; + int rc; - /* If it exist it means the user has inserted the same path twice */ - db->error = SQL_CONFIG_PATH_EXISTS; - return -1; -} + char sql[STR_M] = ""; + dbh_form_query_count_program_relations(sql); -int add_get_or_insert_program_to_db(DB *db, const char *name) { - int pid = get_program_id(db, name); - if (pid == -2) { - db->error = SQL_ERR_SQLITE; + rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); + if (rc != SQLITE_OK) { return -1; } - if (pid == -1) { - return insert_to_program_table(db, name); - } - return pid; -} -int add_basename_exists(DB *db, const char *pName, const char *path) { - cklist *baseNames = list_make_new(); - get_program_paths(db, baseNames, pName, 1 /*basename */, 0, NULL); - char *tmp = strdup(path); - int rc = list_exists(baseNames, basename(tmp)); - free(tmp); - list_free(baseNames); - return rc; -} - -int add_transaction_try(DB *db, const AddOpt * const opt, const char *home) { - __BEGIN_TRANSACTION__ - int pid = add_get_or_insert_program_to_db(db, opt->progName); - if (db->error == SQL_ERR_SQLITE) { - PRINT_ERR("Could not insert program to db.\n"); - return 1; - } - if (add_basename_exists(db, opt->progName, opt->confPath)) { - ERR("Cannot have two configs with the same basename, for the same program."); - return 1; - } - int cid = add_get_or_insert_config_to_db(db, pid, opt->confPath, opt->secret, opt->prime, home); - if (db->error == SQL_ERR_SQLITE) { - PRINT_ERR("Could not insert config to db.\n"); - return 1; - } - else if (db->error == SQL_CONFIG_PATH_EXISTS) { - PRINT_ERR("This config already exists in the database.\n"); - return 1; - } - else if (db->error == SQL_ERR_PRIMARY_REDEFINITION) { - PRINT_ERR("This program already has a primary config.\n"); - return 1; - } - add_insert_relationship(db, pid, cid); - if (db->error == SQL_ERR_SQLITE) { - PRINT_ERR("rel update failed\n"); - return 1; + sqlite3_bind_int(stmt, 1, pid); + while (sqlite3_step(stmt) == SQLITE_ROW) { + count = sqlite3_column_int(stmt, 0); } - __END_TRANSACTION__ - return 0; -} - -int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secret) { - int pid = get_program_id(db, pName); - /* error */ - if (pid == -2) { + if (rc != SQLITE_OK) { return -1; } + sqlite3_finalize(stmt); + return count; +} +int get_config_number(DB *db, char* pName) { + int pid = get_program_id(db, pName); /* program exists */ if (pid > -1) { - char path[STR_M] = ""; - if (program_has_primary_config(db, pid, path, secret) == 1) { - if (!str_is_empty(path)) { - if (ret) { - str_make_ck_config_name(ret, path, pName); - } - return 0; - } - } + return get_program_relations(db, pid); } - - /* No prime config found */ return -1; } -int edit_get_config(DB *db, const char *pName, char *ret, const char *cName, int *sec) { - int pid = get_program_id(db, pName); - /* error */ - if (pid == -2) { - return -1; - } - - /* program exists */ - if (pid > -1) { - sqlite3_stmt *stmt; - int rc; - - char selection[STR_M] = COL_CONFIG_PATH; - strcat(selection, ", "); - strcat(selection, COL_CONFIG_SECRET); - char condition[STR_M] = TBL_PROGRAM; - strcat(condition, "."); - strcat(condition, COL_PROGRAM_ID); - - char sql[STR_L] = ""; - dbh_form_query_select_from_joined_eq(sql, selection, condition); +int get_pid_from_cid(DB *db, int cid) { + int pid = -1; + sqlite3_stmt *stmt; + int rc; - rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); - sqlite3_bind_int(stmt, 1, pid); - if (rc != SQLITE_OK) { - return -2; - } + char sql[STR_M] = ""; + dbh_form_query_get_pid_from_cid(sql); - int flag = -1; - while (sqlite3_step(stmt) == SQLITE_ROW) { - char confName[STR_M] = ""; - if (cName) { - char *tmp = strdup((char *)sqlite3_column_text(stmt, 0)); - if (strcmp(cName, basename(tmp)) == 0) { - flag = 0; - str_make_ck_config_name(confName, (char *)sqlite3_column_text(stmt, 0), pName); - strcpy(ret, confName); - if (sec) { - *sec = sqlite3_column_int(stmt, 1); - } - } - free(tmp); - break; - } - else { - /* Since we are here, it means there is only 1 config for the selected program */ - flag = 0; - str_make_ck_config_name(confName, (char *)sqlite3_column_text(stmt, 0), pName); - strcpy(ret, confName); - if (sec) { - *sec = sqlite3_column_int(stmt, 1); - } - break; - } - } - sqlite3_finalize(stmt); - return flag; + rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); + if (rc != SQLITE_OK) { + return -1; } - return -1; -} -static void decorate_entry(char *entry, int secret, int primary, const char *path) { - if (primary) { - strcat(entry, " [p]"); - } + sqlite3_bind_int(stmt, 1, cid); - if (secret) { - strcat(entry, " [s]"); + while (sqlite3_step(stmt) == SQLITE_ROW) { + pid = sqlite3_column_int(stmt, 0); } - /* root */ - if (util_is_link_owned_by_root(path)) { - strcat(entry, " [root]"); + + if (rc != SQLITE_OK) { + return -1; } + sqlite3_finalize(stmt); + return pid; } +void print_suggested_configs(DB *db, const char *pName) { + char name[STR_M] = ""; + strcat(name, pName); + strcat(name, ":"); + cklist *paths = list_make_and_add(name); + get_program_paths(db, paths, pName, 1, 0, NULL); + list_print(paths); + list_free(paths); +} int get_program_paths(DB *db, cklist *ckl, const char* pName, int bname, int attr, const char *home) { int pid = get_program_id(db, pName); @@ -573,471 +317,3 @@ int get_program_paths(DB *db, cklist *ckl, const char* pName, int bname, int att } return -1; } - -int list_get_paths(DB *db, cklist *ckl, int bName, int attr, const char *home) { - sqlite3_stmt *stmt; - int rc; - - char sql[STR_M] = ""; - dbh_form_query_select_paths_with_attributes(sql); - - rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - return -2; - } - - while (sqlite3_step(stmt) == SQLITE_ROW) { - char *tmp = strdup((char *)sqlite3_column_text(stmt, 0)); - char path[STR_L] = ""; - if (bName) { - strcat(path, basename(tmp)); - } - else { - char tpath[STR_L] = ""; - if (swap_tilde_with_home(tpath, tmp, home)) { - strcat(path, tpath); - } - else { - strcat(path, tmp); - } - } - free(tmp); - if (attr) { - decorate_entry(path, sqlite3_column_int(stmt, 1), - sqlite3_column_int(stmt, 2), - (char *)sqlite3_column_text(stmt, 0)); - } - list_add(ckl, path); - } - sqlite3_finalize(stmt); - - return 1; -} - -int list_get_programs(DB *db, cklist *ckl) { - sqlite3_stmt *stmt; - int rc; - - char sql[STR_M] = ""; - dbh_form_query_select_programs(sql); - - rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - return -2; - } - - while (sqlite3_step(stmt) == SQLITE_ROW) { - list_add(ckl, (char *)sqlite3_column_text(stmt, 0)); - } - sqlite3_finalize(stmt); - - return 1; -} - -int list_get_path_program_tree(DB *db, cklist *ckl, int bName, int attr, const char *home) { - sqlite3_stmt *stmt; - int rc; - - char sql[STR_M] = ""; - dbh_form_query_select_programs(sql); - - rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - return -2; - } - - while (sqlite3_step(stmt) == SQLITE_ROW) { - char programName[STR_M] = ""; - strcat(programName, (char *)sqlite3_column_text(stmt, 0)); - strcat(programName, ":"); - list_add(ckl, programName); - sqlite3_stmt *stmt2; - int rc2; - - char sql2[STR_L] = ""; - - char selection[STR_M] = COL_CONFIG_PATH; - strcat(selection, ","); - strcat(selection, COL_CONFIG_SECRET); - strcat(selection, ","); - strcat(selection, COL_CONFIG_PRIMARY); - - char condition[STR_M] = TBL_PROGRAM; - strcat(condition, "."); - strcat(condition, COL_PROGRAM_NAME); - - dbh_form_query_select_from_joined_eq(sql2, selection, condition); - - rc2 = sqlite3_prepare_v2(db->ptr, sql2, -1, &stmt2, 0); - if (rc2 != SQLITE_OK) { - return -2; - } - - sqlite3_bind_text(stmt2, 1, (char *)sqlite3_column_text(stmt, 0), -1, 0); - while (sqlite3_step(stmt2) == SQLITE_ROW) { - char treePath[STR_L] = "|- "; - char *tmp = strdup((char *)sqlite3_column_text(stmt2, 0)); - if (bName) { - strcat(treePath, basename(tmp)); - } - else { - char tpath[STR_L] = ""; - if (swap_tilde_with_home(tpath, tmp, home)) { - strcat(treePath, tpath); - } - else { - strcat(treePath, tmp); - } - } - free(tmp); - if (attr) { - decorate_entry(treePath, sqlite3_column_int(stmt2, 1), - sqlite3_column_int(stmt2, 2), - (char *)sqlite3_column_text(stmt2, 0)); - } - list_add(ckl, treePath); - } - sqlite3_finalize(stmt2); - } - sqlite3_finalize(stmt); - - return 1; -} - -int delete_prog(DB *db, int pid) { - sqlite3_stmt *stmt; - int rc; - - char sql[STR_M] = ""; - dbh_form_query_delete_x_from_y(sql, COL_PROGRAM_ID, TBL_PROGRAM); - - rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - return -1; - } - sqlite3_bind_int(stmt, 1, pid); - sqlite3_step(stmt); - if (rc != SQLITE_OK) { - return -1; - } - - sqlite3_finalize(stmt); - return 0; -} - -int delete_conf(DB *db, int cid) { - sqlite3_stmt *stmt; - int rc; - - char sql[STR_M] = ""; - dbh_form_query_delete_x_from_y(sql, COL_CONFIG_ID, TBL_CONFIG); - - rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - return -1; - } - sqlite3_bind_int(stmt, 1, cid); - sqlite3_step(stmt); - if (rc != SQLITE_OK) { - return -1; - } - - sqlite3_finalize(stmt); - return 0; -} - -int get_pid_from_cid(DB *db, int cid) { - int pid = -1; - sqlite3_stmt *stmt; - int rc; - - char sql[STR_M] = ""; - dbh_form_query_get_pid_from_cid(sql); - - rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - return -1; - } - - sqlite3_bind_int(stmt, 1, cid); - - while (sqlite3_step(stmt) == SQLITE_ROW) { - pid = sqlite3_column_int(stmt, 0); - } - - if (rc != SQLITE_OK) { - return -1; - } - sqlite3_finalize(stmt); - return pid; -} - -int get_program_relations(DB *db, int pid) { - int count = -1; - sqlite3_stmt *stmt; - int rc; - - char sql[STR_M] = ""; - dbh_form_query_count_program_relations(sql); - - rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - return -1; - } - - sqlite3_bind_int(stmt, 1, pid); - while (sqlite3_step(stmt) == SQLITE_ROW) { - count = sqlite3_column_int(stmt, 0); - } - - if (rc != SQLITE_OK) { - return -1; - } - sqlite3_finalize(stmt); - return count; -} - -int get_config_number(DB *db, char* pName) { - int pid = get_program_id(db, pName); - /* program exists */ - if (pid > -1) { - return get_program_relations(db, pid); - } - return -1; -} - -/* Removes the relationship of `cid` with the corresponding program. - * Returns the program's pid on succes, negative integer otherwise. - */ -int remove_conf_rel(DB *db, int cid) { - sqlite3_stmt *stmt; - int rc; - - int pid = get_pid_from_cid(db, cid); - - char sql[STR_M] = ""; - dbh_form_query_delete_x_from_y(sql, COL_REL_CONFIG_ID, TBL_REL); - rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - return -1; - } - sqlite3_bind_int(stmt, 1, cid); - sqlite3_step(stmt); - if (rc != SQLITE_OK) { - return -1; - } - - sqlite3_finalize(stmt); - - return pid; -} - -int remove_all_configs(DB *db, int pid) { - sqlite3_stmt *stmt; - int rc; - - char sql[STR_M] = ""; - dbh_form_query_select_from_joined_eq(sql, COL_REL_CONFIG_ID, COL_REL_PROGRAM_ID); - rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - return -2; - } - - sqlite3_bind_int(stmt, 1, pid); - - while (sqlite3_step(stmt) == SQLITE_ROW) { - delete_conf(db, sqlite3_column_int(stmt, 0)); - remove_conf_rel(db, sqlite3_column_int(stmt, 0)); - } - sqlite3_finalize(stmt); - - return 0; -} - -int get_cid_from_pname_and_basename(DB *db, const char *pName, const char *cBaseName) { - sqlite3_stmt *stmt; - int rc; - - char sql[STR_L] = ""; - char selection[STR_M] = TBL_CONFIG; - strcat(selection, "."); - strcat(selection, COL_CONFIG_ID); - strcat(selection, ", "); - strcat(selection, COL_CONFIG_PATH); - - char condition[STR_M] = TBL_PROGRAM; - strcat(condition, "."); - strcat(condition, COL_PROGRAM_NAME); - - dbh_form_query_select_from_joined_eq(sql, selection, condition); - rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - ERR("while preparing get_cid_from_pname_and_basename"); - db->error = SQL_ERR_SQLITE; - return -1; - } - - sqlite3_bind_text(stmt, 1, pName, -1, 0); - int _cid = -1; - while (sqlite3_step(stmt) == SQLITE_ROW) { - if (strcmp(cBaseName, basename((char *)sqlite3_column_text(stmt, 1))) == 0) { - _cid = sqlite3_column_int(stmt, 0); - break; - } - } - sqlite3_finalize(stmt); - return _cid; -} - -int del_transaction_try(DB *db, const char *pName, const char *cBaseName) { - __BEGIN_TRANSACTION__ - int pid = -1; - if (cBaseName) { - // del conf - int cid = get_cid_from_pname_and_basename(db, pName, cBaseName); - if (cid < 0) { - ERR("Config %s doesn't exist in the database.", cBaseName); - return -1; - } - if (delete_conf(db, cid)) { - ERR("Could not delete config %s from db.", cBaseName); - return -1; - } - // handle relations - pid = remove_conf_rel(db, cid); - HELP("Deleted %s config: %s", pName, cBaseName); - if (get_program_relations(db, pid) > 0) { - goto end; - } - } - else { - pid = get_program_id(db, pName); - } - if (pid < 0) { - ERR("Program not found in the db."); - return -1; - } - /* If we are deleting a program we should delete everything that - * refferences it (configs and relationships) */ - if (!cBaseName) { - remove_all_configs(db, pid); - } - if(delete_prog(db, pid)) { - ERR("Could not delete program %s from db.", pName); - return -1; - } - HELP("Deleted program %s", pName); - end: - __END_TRANSACTION__ - return 0; -} - -int restore_program(DB *db, Conf *conf, const char *pName) { - sqlite3_stmt *stmt; - int rc; - - char sql[STR_L] = ""; - - char selection[STR_M] = COL_CONFIG_PATH; - strcat(selection, ","); - strcat(selection, COL_CONFIG_SECRET); - - char condition[STR_M] = TBL_PROGRAM; - strcat(condition, "."); - strcat(condition, COL_PROGRAM_NAME); - - dbh_form_query_select_from_joined_eq(sql, selection, condition); - - rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - return 0; - } - - sqlite3_bind_text(stmt, 1, pName, -1, 0); - int err_flag = 0; - while (sqlite3_step(stmt) == SQLITE_ROW) { - int secret = sqlite3_column_int(stmt, 1); - char filePath[STR_L] = ""; - strcpy(filePath, secret ? conf->scrt_dir : conf->vc_dir); - strcat(filePath, "/"); - strcat(filePath, pName); - strcat(filePath, "/"); - strcat(filePath, basename((char *)sqlite3_column_text(stmt, 0))); - if (!util_is_file_rw(filePath)) { - sERR("%s does not exist or is not accessible.", filePath); - err_flag = 1; - } - } - sqlite3_finalize(stmt); - return err_flag; -} - -int restore_configs_exists(DB *db, Conf *conf, const char *pName, cklist *from, cklist *to) { - sqlite3_stmt *stmt; - int rc; - - char sql[STR_L] = ""; - - char selection[STR_M] = COL_CONFIG_PATH; - strcat(selection, ","); - strcat(selection, COL_CONFIG_SECRET); - - char condition[STR_M] = TBL_PROGRAM; - strcat(condition, "."); - strcat(condition, COL_PROGRAM_NAME); - - dbh_form_query_select_from_joined_eq(sql, selection, condition); - - rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - return 0; - } - - sqlite3_bind_text(stmt, 1, pName, -1, 0); - int err_flag = 0; - while (sqlite3_step(stmt) == SQLITE_ROW) { - char filePath[STR_L] = ""; - strcpy(filePath, /*secret*/ sqlite3_column_int(stmt, 1) ? conf->scrt_dir : conf->vc_dir); - strcat(filePath, "/"); - strcat(filePath, pName); - if (!util_is_dir(filePath)) { - sERR("%s is not a directory.", filePath); - err_flag = 1; - break; - } - strcat(filePath, "/"); - strcat(filePath, basename(/*path*/ (char *)sqlite3_column_text(stmt, 0))); - if (!util_is_file_rw(filePath)) { - sERR("%s does not exist or is not accessible.", filePath); - err_flag = 1; - break; - } - list_add(from, filePath); - char tpath[STR_L] = ""; - if (!swap_tilde_with_home(tpath, (char *)sqlite3_column_text(stmt, 0), conf->home_dir)) { - strcpy(tpath, (char *)sqlite3_column_text(stmt, 0)); - } - list_add(to, tpath); - } - sqlite3_finalize(stmt); - return !err_flag; -} - -int restore_all_exist(DB *db, Conf *conf, cklist *from, cklist *to) { - cklist *programs = list_make_new(); - if (list_get_programs(db, programs) != 1) { - ERR("No programs in ckdb"); - list_free(programs); - return 0; - } - int err_flag = 0; - if (list_size(programs) > 0) { - do { - if (!restore_configs_exists(db, conf, list_get(programs), from, to)) { - err_flag = 1; - } - } while(list_next(programs)); - } - list_free(programs); - return !err_flag; -} -- cgit v1.2.3