aboutsummaryrefslogtreecommitdiffstats
path: root/src/restore.c
blob: c90cb82fcf1642c9a8cc0058e10617a3ce4fec6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#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) {
  list_rewind(from);
  list_rewind(to);
  if (list_size(from) > 0
      && list_size(to) > 0
      && list_size(from) == list_size(to)) {
    do {
      if (util_file_exists(list_get(to), NULL)
          || !util_is_file_link(list_get(to))) {
        ERR("File %s already exists.", list_get(to));
        sERR("No links were created.");
        return -1;
      }
    } while (list_next(to));
    list_rewind(to);
    while (1) {
      if (util_symlink_file(list_get(from), list_get(to))) {
        ERR("FATAL could not link %s -> %s", list_get(from), list_get(to));
        sERR("Process stopping.");
        return -1;
      }
      hLOG("Linking: %s -> %s", list_get(from), list_get(to));
      if (util_own_grp_copy(list_get(to), list_get(from))) {
        return -1;
      }
      if (!list_next(from)) {
        break;
      }
      if (!list_next(to)) {
        break;
      }
    }
  }
  return 0;
}

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;
}