diff options
author | Anastasis Grammenos <anastasis.gramm2@gmail.com> | 2018-10-27 00:49:06 +0300 |
---|---|---|
committer | Anastasis Grammenos <anastasis.gramm2@gmail.com> | 2018-10-27 00:49:06 +0300 |
commit | 401820e28b74e09bffdeaa25fcf6de518487a433 (patch) | |
tree | e0a6b4d0fafb4718a3eacee983e4694996f1f570 /src/dblayer.c | |
parent | 2e1a9e0832b53c0f03a71ba76fdd76612517d03c (diff) | |
download | ck-401820e28b74e09bffdeaa25fcf6de518487a433.tar.gz ck-401820e28b74e09bffdeaa25fcf6de518487a433.tar.bz2 ck-401820e28b74e09bffdeaa25fcf6de518487a433.zip |
restore
Diffstat (limited to 'src/dblayer.c')
-rw-r--r-- | src/dblayer.c | 105 |
1 files changed, 105 insertions, 0 deletions
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; +} |