diff options
Diffstat (limited to 'src/restore.c')
-rw-r--r-- | src/restore.c | 83 |
1 files changed, 78 insertions, 5 deletions
diff --git a/src/restore.c b/src/restore.c index cb8e6c9..a02f6f3 100644 --- a/src/restore.c +++ b/src/restore.c @@ -1,13 +1,21 @@ +/* restore.c - the restore action --------------------------------------*- C -*- + * + * This file is part of ck, the config keeper + * + * ----------------------------------------------------------------------------- + * + * Copyright (C) 2018 Anastasis Grammenos + * GPLv3 (see LICENCE for the full notice) + * + * -------------------------------------------------------------------------- */ #include <libgen.h> -#include "actions.h" #include "dblayer.h" -#include "queries.h" #include "ckerrlog.h" ERRLOG(restore); -int restore_make_links(cklist *from, cklist *to) { +static int restore_make_links(cklist *from, cklist *to) { list_rewind(from); list_rewind(to); if (list_size(from) > 0 @@ -44,7 +52,7 @@ int restore_make_links(cklist *from, cklist *to) { return 0; } -int restore_configs_exists(DB *db, Conf *conf, const char *pName, cklist *from, cklist *to) { +static int restore_configs_exists(DB *db, Conf *conf, const char *pName, cklist *from, cklist *to) { sqlite3_stmt *stmt; int rc; @@ -95,7 +103,7 @@ int restore_configs_exists(DB *db, Conf *conf, const char *pName, cklist *from, return !err_flag; } -int restore_all_exist(DB *db, Conf *conf, cklist *from, cklist *to) { +static 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"); @@ -113,3 +121,68 @@ int restore_all_exist(DB *db, Conf *conf, cklist *from, cklist *to) { list_free(programs); return !err_flag; } + +int run_RESTORE(UserOpt *opt, Conf *conf) { + DB db; + if (open_DB(&db, opt)) { + return -1; + } + cklist *from = list_make_new(); + cklist *to = list_make_new(); + int err_flag = 0; + if (strcmp(list_get(opt->args), "-p") == 0) { + if (list_next(opt->args)) { + if (program_exists(&db, list_get(opt->args))) { + if (restore_configs_exists(&db, conf, list_get(opt->args), from, to)) { + hLOG("Restoring links for %s...", list_get(opt->args)); + } + else { + err_flag = 1; + } + } + else { + ERR("Program %s does not exist", list_get(opt->args)); + err_flag = 1; + } + } + else { + sERR("-p needs a program name"); + err_flag = 1; + } + } + else if (strcmp(list_get(opt->args), "all") == 0) { + if (!list_next(opt->args)) { + if (restore_all_exist(&db, conf, from, to)) { + hLOG("Restoring all links..."); + } + else { + err_flag = 1; + } + } + else { + sERR("Wrong argument"); + err_flag = 1; + } + } + else { + sERR("Wrong argument"); + err_flag = 1; + } + close_DB(&db); + int rc = -1; + if (!err_flag) { + rc = restore_make_links(from, to); + if (rc) { + sERR("Restore failed."); + } + } + list_free(from); + list_free(to); + return rc; +} + +void print_RESTORE_help() { + ckhelp("ck restore -p PROGRAM_NAME"); + ckhelp("ck restore all"); + report_help(); +} |