-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.c
More file actions
453 lines (349 loc) · 8.24 KB
/
config.c
File metadata and controls
453 lines (349 loc) · 8.24 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "error.h"
#include "config.h"
#define DEFAULT_DELAY 180
#define DEFAULT_LOGFILE "nodesync.log"
#define EQ(str1, str2) (strcmp(str1, str2) == 0)
int delay = DEFAULT_DELAY;
static rnode_t cfg_new_rnode(void)
{
return (rnode_t)malloc(sizeof(struct rnode));
}
static cfg_t cfg_new_item(void)
{
return (cfg_t)malloc(sizeof(struct config_item));
}
static cfg_t cfg_create_item(void)
{
cfg_t p;
p = cfg_new_item();
check(p , "Could not create the cfg_t structure")
if(error_isset)
return NULL;
p->n_excludes = 0;
p->n_rnodes = 0;
p->wpath = NULL;
p->backup_dir = NULL;
p->rsync_args = NULL;
p->excludes = NULL;
p->rnode = NULL;
return p;
}
static void cfg_add_item(cfg_t *cfg_l, cfg_t x)
{
if(*cfg_l == NULL) {
x->next = NULL;
*cfg_l = x;
} else {
x->next = *cfg_l;
*cfg_l = x;
}
}
static int cfg_set_wpath(cfg_t cfg_i, char *value)
{
debug("> adding (%s)", value);
cfg_i->wpath = strdup(value);
debug("> added");
return 0;
}
static int cfg_set_rsync_path(cfg_t cfg_i, char *value)
{
debug("> adding (%s)", value);
cfg_i->rsync_path = strdup(value);
debug("> added");
return 0;
}
static int cfg_set_rsync_args(cfg_t cfg_i, char *value)
{
debug("> adding (%s)", value);
cfg_i->rsync_args = strdup(value);
debug("> added");
return 0;
}
static int cfg_set_remote_node(cfg_t x, char *value)
{
rnode_t rnode;
char *node;
char *dir;
debug("> adding (%s)", value);
rnode = cfg_new_rnode();
node = strtok(value, ":");
dir = strtok(NULL, ":");
rnode->node = strdup(node);
rnode->dir = strdup(dir);
if(x->rnode == NULL) {
rnode->next = NULL;
x->rnode = rnode;
} else {
rnode->next = x->rnode;
x->rnode = rnode;
}
x->n_rnodes++;
debug("> node (%s)", x->rnode->node);
debug("> dir (%s)", x->rnode->dir);
debug("> added");
return 0;
}
static int cfg_set_exclude_dir(cfg_t cfg_i, char *value)
{
debug("> adding (%s)", value);
if(cfg_i->n_excludes == 0) {
cfg_i->excludes = malloc(sizeof(char **));
} else {
cfg_i->excludes = realloc(cfg_i->excludes, cfg_i->n_excludes + 1);
}
cfg_i->excludes[cfg_i->n_excludes] = strdup(value);
cfg_i->n_excludes++;
debug("> added");
return 0;
}
static int cfg_set_backup_directory(cfg_t cfg_i, char *value)
{
debug("> adding (%s)", value);
cfg_i->backup_dir = strdup(value);
debug("> added");
return 0;
}
static nodesync_f cfg_set_logfile(char *name)
{
return fopen(name, "wa");
}
static int get_size(int fd)
{
struct stat st;
return (fstat(fd, &st) == 0) ? st.st_size : -1;
}
static int directive_is_global(const char *directive)
{
int i;
int found;
for(found = 0, i = 0; glob_directives[i] != NULL && found != 1 ; i++)
if(EQ(glob_directives[i], directive))
found = 1;
return found;
}
static int directive_is_local(const char *directive)
{
int i;
int found;
for(found = 0, i = 0; locl_directives[i] != NULL && found != 1 ; i++)
if(EQ(locl_directives[i], directive))
found = 1;
return found;
}
static cfg_option check_directive(const char *directive)
{
if(EQ(directive, "watch_config"))
return GLOB_CFG;
else if(EQ(directive, "wpath"))
return W_PATH;
else if(EQ(directive, "excludes"))
return EXCLUDES;
else if(EQ(directive, "rnodes"))
return RNODES;
else if(EQ(directive, "logfile"))
return LOG_FILE;
else if(EQ(directive, "local_backup_directory"))
return BACKUP_DIR;
else if(EQ(directive, "edelay"))
return DELAY;
else if(EQ(directive, "rsync"))
return RSYNC;
else if(EQ(directive, "args"))
return R_ARGS;
else
return NONE;
}
static int is_valid_pair(char c, int quoted)
{
if(c == ' ' && quoted)
return 1;
else
return (c != ',' && c != '\n' && c != '}' && c != '"' && c != ' ' && c != '\t');
}
static int is_valid_key(char c)
{
return (c != ' ' && c != '\t' && c != '\n');
}
#ifdef DEBUG
void walk_through(cfg_t cfg_i)
{
cfg_t p;
rnode_t x;
int i;
printf("\n\n-------------------\n");
printf("Dump w structure: \n\n");
for(p = cfg_i; p != NULL ;p = p->next) {
printf("wpath > %s\n", p->wpath);
printf("backup_dir > %s\n", p->backup_dir);
printf("r_args > %s\n", p->rsync_args);
for(i = 0; i < p->n_excludes; i++)
printf("excludes (%d) > %s\n", i, p->excludes[i]);
for(x = p->rnode ; x != NULL ; x = x->next) {
printf("node > %s\n", x->node);
printf("dir > %s\n", x->dir);
}
i = 0;
printf("\n-------------------");
printf("\n\n");
}
}
#endif
void clean_cfg(cfg_t *w)
{
cfg_t x;
while(*w != NULL) {
x = *w;
*w = x->next;
free(x);
}
}
cfg_t load_cfg(int fd)
{
int ret;
int len;
int size;
int bytes;
int block;
int token;
int quotes;
int nl;
char directive[80];
char option[256];
char *buffer;
char *p;
char *ap;
cfg_state state;
cfg_option cfg_directive;
cfg_t cfg_i, x;
cfg_i = x = NULL;
nl = 1;
block = quotes = token = 0;
state = GET_GLOBAL_KEY;
size = get_size(fd);
buffer = calloc(size + 1, sizeof(char));
bytes = read(fd, buffer, size);
if(bytes != size) {
fprintf(stderr, "Could not read the whole file\n");
goto fatal_error;
}
logfile = cfg_set_logfile(DEFAULT_LOGFILE);
check(logfile, "Could not open the file (%s)", DEFAULT_LOGFILE);
if(error_isset)
goto fatal_error;
p = buffer;
while(*p != '\0') {
p += strspn(p, " \t");
if(*p == '\n') {
p++;
nl++;
if(state == GET_PAIR) {
if(block)
state = GET_LOCAL_KEY;
else
state = GET_GLOBAL_KEY;
token = 0;
}
continue;
}
if(*p == '#') {
while(*p++ != '\n')
;
p--;
continue;
}
if(*p == '"') {
quotes = quotes ? 0 : 1;
p++;
continue;
}
if(*p == '=') {
if(token && !block) {
fprintf(stderr, "Error in line %d: double '=' found\n", nl);
goto fatal_error;
}
state = GET_PAIR;
token = 1;
p++;
continue;
}
if(*p == '{') {
if(block) {
fprintf(stderr, "Error in line %d: found '{', but expected '}'\n", nl);
goto fatal_error;
} else {
block = 1;
state = GET_LOCAL_KEY;
p++;
continue;
}
}
if(*p == '}') {
if(block) {
block = 0;
token = 0;
state = GET_GLOBAL_KEY;
p++;
continue;
} else {
fprintf(stderr, "Error in line %d: found '}', but '{' was not found before\n", nl);
goto fatal_error;
}
}
if(state == GET_GLOBAL_KEY || state == GET_LOCAL_KEY) {
if(is_valid_key(*p)) {
ap = p;
while(is_valid_key(*p++))
;
len = (p - ap) -1;
strncpy(directive, ap, len);
directive[len] = '\0';
cfg_directive = check_directive(directive);
if(cfg_directive == NONE) { /* Bad directive */
log_err("\"%s\" not recognized as a directive", directive);
goto fatal_error;
}
if(state == GET_GLOBAL_KEY) { /* Is really global? */
ret = directive_is_global(directive);
check(ret, "\"%s\" out of scope", directive);
if(error_isset)
goto fatal_error;
if(cfg_directive == GLOB_CFG) {
x = cfg_create_item();
cfg_add_item(&cfg_i, x);
}
} else { /* Is really local? */
ret = directive_is_local(directive);
check(ret, "\"%s\" out of scope", directive);
if(error_isset)
goto fatal_error;
}
p--;
continue;
}
} else {
if(is_valid_pair(*p, quotes)) {
ap = p;
while(is_valid_pair(*p++, quotes))
;
len = (p - ap) -1;
strncpy(option, ap, len);
option[len] = '\0';
if(cfg_directive != DELAY && cfg_directive != LOG_FILE)
directives[cfg_directive].add_value(x, option);
p--;
continue;
}
}
}
free(buffer);
return cfg_i;
fatal_error:
free(buffer);
clean_cfg(&cfg_i);
return NULL;
}