diff options
Diffstat (limited to 'src/dblayer.c')
-rw-r--r-- | src/dblayer.c | 62 |
1 files changed, 51 insertions, 11 deletions
diff --git a/src/dblayer.c b/src/dblayer.c index f263b86..0e2ba5b 100644 --- a/src/dblayer.c +++ b/src/dblayer.c @@ -862,29 +862,64 @@ int remove_all_configs(DB *db, int pid) { return 0; } -int del_transaction_try(DB *db, char *arg, int conf) { +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 (conf) { + if (cBaseName) { // del conf - int cid = get_config_id(db, arg); + int cid = get_cid_from_pname_and_basename(db, pName, cBaseName); if (cid < 0) { - ERR("Config %s doesn't exist in the database.", arg); + ERR("Config %s doesn't exist in the database.", cBaseName); return -1; } if (delete_conf(db, cid)) { - ERR("Could not delete config from db."); + ERR("Could not delete config %s from db.", cBaseName); return -1; } // handle relations pid = remove_conf_rel(db, cid); - HELP("Deleted config: %s", arg); + HELP("Deleted %s config: %s", pName, cBaseName); if (get_program_relations(db, pid) > 0) { goto end; } } else { - pid = get_program_id(db, arg); + pid = get_program_id(db, pName); } if (pid < 0) { ERR("Program not found in the db."); @@ -892,14 +927,14 @@ int del_transaction_try(DB *db, char *arg, int conf) { } /* If we are deleting a program we should delete everything that * refferences it (configs and relationships) */ - if (!conf) { + if (!cBaseName) { remove_all_configs(db, pid); } if(delete_prog(db, pid)) { - ERR("Could not delete program from db."); + ERR("Could not delete program %s from db.", pName); return -1; } - HELP("Deleted program %s", conf ? "" : arg); + HELP("Deleted program %s", pName); end: __END_TRANSACTION__ return 0; @@ -983,9 +1018,14 @@ int restore_configs_exists(DB *db, Conf *conf, const char *pName, cklist *from, if (!util_is_file_rw(filePath)) { sERR("%s does not exist or is not accessible.", filePath); err_flag = 1; + break; } list_add(from, filePath); - list_add(to, /*path*/ (char *)sqlite3_column_text(stmt, 0)); + 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; |