From 401820e28b74e09bffdeaa25fcf6de518487a433 Mon Sep 17 00:00:00 2001 From: Anastasis Grammenos Date: Sat, 27 Oct 2018 00:49:06 +0300 Subject: restore --- src/dblayer.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) (limited to 'src/dblayer.c') diff --git a/src/dblayer.c b/src/dblayer.c index acdf339..33e1682 100644 --- a/src/dblayer.c +++ b/src/dblayer.c @@ -842,3 +842,108 @@ int del_transaction_try(DB *db, char *arg, int conf) { __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; + } + list_add(from, filePath); + list_add(to, /*path*/ (char *)sqlite3_column_text(stmt, 0)); + } + 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