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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
/* actionhelper.c - helper routines for ck actions --------------------*- 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 "actionhelper.h"
#include "confparser.h"
#include "ckerrlog.h"
ERRLOG(action);
char add_err[STR_M] = "";
int add_err_message(char *err) {
if (!str_is_empty(add_err)) {
if (err) {
strcpy(err, add_err);
}
return 1;
}
return 0;
}
void link_config(const AddOpt *opt, const char* newPath) {
printf("Linking %s -> %s\n", newPath, opt->confPath);
if (util_symlink_file(newPath, opt->confPath) != 0) {
strcpy(add_err, "Could not link file.");
}
}
int move_config(const AddOpt *opt, char *progDir, char *ret) {
char newPath[STR_L] = "";
char *tmp = strdup(opt->confPath);
str_join_dirname_with_basename(newPath, progDir, basename(tmp));
free(tmp);
if (util_file_exists(newPath, NULL)) {
strcpy(add_err, "File already exists");\
return -1;
}
strcpy(ret, newPath);
printf("Moving %s -> %s\n", opt->confPath, newPath);
if (util_move_file(opt->confPath, newPath) != 0) {
strcpy(add_err, "Could not move file.");
return -1;
}
return 0;
}
AddOpt add_make_options(cklist *args) {
list_rewind(args);
/* since we are here, the first two arguments must exist */
AddOpt addOpt = {
.progName = list_get(args),
.secret = 0,
.prime = 0,
.err = ADD_NO_ERR
};
list_next(args);
if (!util_is_file_rw(list_get(args))
|| !util_is_file_link(list_get(args))) {
addOpt.err = ADD_ERR_WRONG_CONFIG;
return addOpt;
}
realpath(list_get(args), addOpt.confPath);
while (list_next(args)) {
if (strcmp(list_get(args), "-s") == 0 && addOpt.secret == 0) {
addOpt.secret = 1;
} else if (strcmp(list_get(args), "-p") == 0 && addOpt.prime == 0) {
addOpt.prime = 1;
} else {
addOpt.err = ADD_ERR_WRONG_FLAGS;
return addOpt;
}
}
list_rewind(args);
return addOpt;
}
void add_print_opts(AddOpt *opt) {
printf("Program:\t%s\nConfig:\t\t%s\n", opt->progName, opt->confPath);
if (opt->prime && opt->secret) {
printf("Options:\tsecret, primary\n");
} else if (opt->prime) {
printf("Options:\tprimary\n");
} else if (opt->secret) {
printf("Options:\tsecret\n");
}
}
void get_or_make_program_dir(const AddOpt *opt, const Conf *conf, char *ret) {
char tmp[STR_L] = "";
str_join_dirname_with_basename(tmp, opt->secret ? conf->scrt_dir : conf->vc_dir, opt->progName);
if (!util_file_exists(tmp, NULL)) {
util_mkdir(tmp);
}
strcpy(ret, tmp);
}
void add_make_link(const AddOpt *opt, const Conf *conf) {
char progDir[STR_L] = "";
get_or_make_program_dir(opt, conf, progDir);
char newPath[STR_L] = "";
move_config(opt, progDir, newPath);
if (add_err_message(NULL)) {
return;
}
link_config(opt, newPath);
if (add_err_message(NULL)) {
return;
}
}
void edit_print_suggested_configs(DB *db, const char *pName) {
char name[STR_M] = "";
strcat(name, pName);
strcat(name, ":");
cklist *paths = list_make_and_add(name);
get_program_paths(db, paths, pName, 1, 0, NULL);
list_print(paths);
list_free(paths);
}
int init_create_config_file(UserOpt *opt) {
char absVCdir[STR_L] = "";
if (!util_file_exists(list_get_at(opt->args, 0), absVCdir)) {
ERR("Version control directory: %s does not exist.", list_get_at(opt->args, 0));
return 1;
}
char absSRdir[STR_L] = "";
if (!util_file_exists(list_get_at(opt->args, 1), absSRdir)) {
ERR("Secret directory: %s does not exist.", list_get_at(opt->args, 1));
return 1;
}
if (!util_file_exists(opt->confDir, NULL)) {
util_mkdir(opt->confDir);
}
char confName[STR_L] = "";
make_config_name(confName, opt->confDir);
FILE *f;
if ((f = fopen(confName, "w")) == NULL) {
return 1;
}
char tmp[STR_L] = "";
strcpy(tmp, "version_control_dir = ");
strcat(tmp, absVCdir);
strcat(tmp, "\n");
fputs(tmp, f);
strcpy(tmp, "secret_dir = ");
strcat(tmp, absSRdir);
strcat(tmp, "\n");
fputs(tmp, f);
strcpy(tmp, "home_dir = ");
strcat(tmp, getenv("HOME"));
strcat(tmp, "\n");
fputs(tmp, f);
fclose(f);
return 0;
}
ListOpt list_make_options(cklist *args) {
list_rewind(args);
ListOpt listOpt = {
._lt = LT_TREE,
._lst = LST_PLAIN,
.pName = NULL,
.attr = 0,
.bName = 0,
.err = 0
};
if (list_size(args)) {
do {
if (strcmp(list_get(args), "-a") == 0) {
listOpt.attr = 1;
continue;
}
if (strcmp(list_get(args), "-b") == 0) {
listOpt.bName = 1;
continue;
}
if (strcmp(list_get(args), "-t") == 0) {
if (!list_next(args)) {
listOpt.err = 1;
break;
}
if (strcmp(list_get(args), "plain") == 0) {
listOpt._lst = LST_PLAIN;
}
else if (strcmp(list_get(args), "lisp") == 0) {
listOpt._lst = LST_LISP;
}
else if (strcmp(list_get(args), "python") == 0) {
listOpt._lst = LST_PYTHON;
}
else {
listOpt.err = 1;
}
}
else if (strcmp(list_get(args), "paths") == 0) {
listOpt._lt = LT_PATH;
}
else if (strcmp(list_get(args), "programs") == 0) {
listOpt._lt = LT_PROGRAM;
}
else if (strcmp(list_get(args), "tree") == 0) {
listOpt._lt = LT_TREE;
}
else if (strcmp(list_get(args), "ckconf") == 0) {
listOpt._lt = LT_CKCONF;
}
else if (strcmp(list_get(args), "-p") == 0) {
if (list_next(args)) {
listOpt._lt = LT_PROG_CONFS;
listOpt.pName = list_get(args);
}
else {
listOpt.err = 1;
break;
}
}
else {
listOpt.err = 1;
}
} while(list_next(args));
}
list_rewind(args);
return listOpt;
}
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;
}
/*****************/
/* PRINT RESULTS */
/*****************/
void print_INIT_result(int err) {
if (!err) {
hLOG("Initialized empty ckdb.");
}
}
void print_ADD_result(int err) {
if (!err) {
hLOG("ckdb updated succesfully.");
return;
}
sERR("Could not complete add transaction.");
}
void print_DEL_result(int err) {
if (!err) {
hLOG("ckdb updated succesfully.");
return;
}
sERR("Could not complete delete transaction.");
}
void print_EDIT_result(int err) {
UNUSED(err);
}
void print_LIST_result(int err) {
UNUSED(err);
}
void print_SEARCH_result(int err) {
UNUSED(err);
}
void print_HELP_result(int err) {
UNUSED(err);
}
void print_RESTORE_result(int err) {
UNUSED(err);
}
void print_INIT_help() {
HELP("ck init VERSION_CONTROL_DIR SECRET_DIR");
}
void print_ADD_help() {
HELP("ck add PROGRAM_NAME CONFIG_PATH [-p] [-s]");
}
void print_DEL_help() {
HELP("ck delete PROGRAM_NAME [CONFIG_BASENAME]");
}
void print_EDIT_help() {
HELP("ck edit PROGRAM_NAME [CONFIG_BASENAME]");
}
void print_LIST_help() {
ckhelp("ck list tree [-a] [-b]");
ckhelp("ck list -p PROGRAM_NAME [-t list-type] [-a] [-b]");
ckhelp("ck list programs [-t list-type] [-a] [-b]");
ckhelp("ck list paths [-t list-type] [-a] [-b]");
ckhelp("ck list ckconf");
report_help();
}
void print_SEARCH_help() {
HELP("ck search SEARCH_TERM");
}
void print_HELP_help() {
HELP("ck help action");
}
void print_RESTORE_help() {
ckhelp("ck restore -p PROGRAM_NAME");
ckhelp("ck restore all");
report_help();
}
void print_conf_help(void) {
HELP("ck [-v|--verbose] [-c|--config DIR] action [...]");
}
void print_verbose_help(void) {
HELP("ck [-v|--verbose] [-c|--config DIR] action [...]");
}
|