Switch to gengetopt's group options and add more documentation.
[dss.git] / dss.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <assert.h>
5 #include <errno.h>
6 #include <sys/types.h>
7 #include <signal.h>
8 #include <ctype.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <inttypes.h>
12 #include <time.h>
13 #include <sys/wait.h>
14 #include <fnmatch.h>
15 #include <limits.h>
16
17
18 #include "gcc-compat.h"
19 #include "cmdline.h"
20 #include "log.h"
21 #include "string.h"
22 #include "error.h"
23 #include "fd.h"
24 #include "exec.h"
25
26
27 struct gengetopt_args_info conf;
28 char *dss_error_txt = NULL;
29
30 DEFINE_DSS_ERRLIST;
31
32
33 /* a litte cpp magic helps to DRY */
34 #define COMMANDS \
35 COMMAND(ls) \
36 COMMAND(create) \
37 COMMAND(prune) \
38 COMMAND(run)
39 #define COMMAND(x) int com_ ##x(int, char * const * const);
40 COMMANDS
41 #undef COMMAND
42 #define COMMAND(x) if (conf.x ##_given) return com_ ##x(argc, argv);
43 int call_command_handler(int argc, char * const * const argv)
44 {
45 COMMANDS
46 return -E_INVALID_COMMAND;
47 }
48 #undef COMMAND
49 #undef COMMANDS
50
51 /*
52 * complete, not being deleted: 1204565370-1204565371.Sun_Mar_02_2008_14_33-Sun_Mar_02_2008_14_43
53 * complete, being deleted: 1204565370-1204565371.being_deleted
54 * incomplete, not being deleted: 1204565370-incomplete
55 * incomplete, being deleted: 1204565370-incomplete.being_deleted
56 */
57 enum snapshot_status_flags {
58 SS_COMPLETE = 1,
59 SS_BEING_DELETED = 2,
60 };
61
62 struct snapshot {
63 char *name;
64 int64_t creation_time;
65 int64_t completion_time;
66 enum snapshot_status_flags flags;
67 unsigned interval;
68 };
69
70 int is_snapshot(const char *dirname, int64_t now, struct snapshot *s)
71 {
72 int i, ret;
73 char *dash, *dot, *tmp;
74 int64_t num;
75
76 assert(dirname);
77 dash = strchr(dirname, '-');
78 if (!dash || !dash[1] || dash == dirname)
79 return 0;
80 for (i = 0; dirname[i] != '-'; i++)
81 if (!isdigit(dirname[i]))
82 return 0;
83 tmp = dss_strdup(dirname);
84 tmp[i] = '\0';
85 ret = dss_atoi64(tmp, &num);
86 free(tmp);
87 if (ret < 0) {
88 free(dss_error_txt);
89 return 0;
90 }
91 assert(num >= 0);
92 if (num > now)
93 return 0;
94 s->creation_time = num;
95 //DSS_DEBUG_LOG("%s start time: %lli\n", dirname, (long long)s->creation_time);
96 s->interval = (long long) ((now - s->creation_time)
97 / conf.unit_interval_arg / 24 / 3600);
98 if (!strcmp(dash + 1, "incomplete")) {
99 s->completion_time = -1;
100 s->flags = 0; /* neither complete, nor being deleted */
101 goto success;
102 }
103 if (!strcmp(dash + 1, "incomplete.being_deleted")) {
104 s->completion_time = -1;
105 s->flags = SS_BEING_DELETED; /* mot cpmplete, being deleted */
106 goto success;
107 }
108 tmp = dash + 1;
109 dot = strchr(tmp, '.');
110 if (!dot || !dot[1] || dot == tmp)
111 return 0;
112 for (i = 0; tmp[i] != '.'; i++)
113 if (!isdigit(tmp[i]))
114 return 0;
115 tmp = dss_strdup(dash + 1);
116 tmp[i] = '\0';
117 ret = dss_atoi64(tmp, &num);
118 free(tmp);
119 if (ret < 0) {
120 free(dss_error_txt);
121 return 0;
122 }
123 if (num > now)
124 return 0;
125 s->completion_time = num;
126 s->flags = SS_COMPLETE;
127 if (strcmp(dot + 1, "being_deleted"))
128 s->flags |= SS_BEING_DELETED;
129 success:
130 s->name = dss_strdup(dirname);
131 return 1;
132 }
133
134 int64_t get_current_time(void)
135 {
136 time_t now;
137 time(&now);
138 DSS_DEBUG_LOG("now: %lli\n", (long long) now);
139 return (int64_t)now;
140 }
141
142 char *incomplete_name(int64_t start)
143 {
144 return make_message("%lli-incomplete", (long long)start);
145 }
146
147 char *being_deleted_name(struct snapshot *s)
148 {
149 if (s->flags & SS_COMPLETE)
150 return make_message("%lli-%lli.being_deleted",
151 (long long)s->creation_time,
152 (long long)s->completion_time);
153 return make_message("%lli-incomplete.being_deleted",
154 (long long)s->creation_time);
155 }
156
157 int complete_name(int64_t start, int64_t end, char **result)
158 {
159 struct tm start_tm, end_tm;
160 time_t *start_seconds = (time_t *) (uint64_t *)&start; /* STFU, gcc */
161 time_t *end_seconds = (time_t *) (uint64_t *)&end; /* STFU, gcc */
162 char start_str[200], end_str[200];
163
164 if (!localtime_r(start_seconds, &start_tm)) {
165 make_err_msg("%lli", (long long)start);
166 return -E_LOCALTIME;
167 }
168 if (!localtime_r(end_seconds, &end_tm)) {
169 make_err_msg("%lli", (long long)end);
170 return -E_LOCALTIME;
171 }
172 if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm)) {
173 make_err_msg("%lli", (long long)start);
174 return -E_STRFTIME;
175 }
176 if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm)) {
177 make_err_msg("%lli", (long long)end);
178 return -E_STRFTIME;
179 }
180 *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
181 start_str, end_str);
182 return 1;
183 }
184
185 struct snapshot_list {
186 int64_t now;
187 unsigned num_snapshots;
188 unsigned array_size;
189 struct snapshot **snapshots;
190 /**
191 * Array of size num_intervals + 1
192 *
193 * It contains the number of snapshots in each interval. interval_count[num_intervals]
194 * is the number of snapshots which belong to any interval greater than num_intervals.
195 */
196 unsigned *interval_count;
197 };
198
199 #define FOR_EACH_SNAPSHOT(s, i, sl) \
200 for ((i) = 0; (i) < (sl)->num_snapshots && ((s) = (sl)->snapshots[(i)]); (i)++)
201
202
203
204 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
205
206 static int compare_snapshots(const void *a, const void *b)
207 {
208 struct snapshot *s1 = *(struct snapshot **)a;
209 struct snapshot *s2 = *(struct snapshot **)b;
210 return NUM_COMPARE(s2->creation_time, s1->creation_time);
211 }
212
213 /** Compute the minimum of \a a and \a b. */
214 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
215
216 int add_snapshot(const char *dirname, void *private)
217 {
218 struct snapshot_list *sl = private;
219 struct snapshot s;
220 int ret = is_snapshot(dirname, sl->now, &s);
221
222 if (!ret)
223 return 1;
224 if (sl->num_snapshots >= sl->array_size) {
225 sl->array_size = 2 * sl->array_size + 1;
226 sl->snapshots = dss_realloc(sl->snapshots,
227 sl->array_size * sizeof(struct snapshot *));
228 }
229 sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
230 *(sl->snapshots[sl->num_snapshots]) = s;
231 sl->interval_count[DSS_MIN(s.interval, conf.num_intervals_arg)]++;
232 sl->num_snapshots++;
233 return 1;
234 }
235
236 void get_snapshot_list(struct snapshot_list *sl)
237 {
238 sl->now = get_current_time();
239 sl->num_snapshots = 0;
240 sl->array_size = 0;
241 sl->snapshots = NULL;
242 sl->interval_count = dss_calloc((conf.num_intervals_arg + 1) * sizeof(unsigned));
243 for_each_subdir(add_snapshot, sl);
244 qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
245 compare_snapshots);
246 }
247
248 void free_snapshot_list(struct snapshot_list *sl)
249 {
250 int i;
251 struct snapshot *s;
252
253 FOR_EACH_SNAPSHOT(s, i, sl) {
254 free(s->name);
255 free(s);
256 }
257 free(sl->interval_count);
258 free(sl->snapshots);
259 }
260
261 /**
262 * Print a log message about the exit status of a child.
263 */
264 void log_termination_msg(pid_t pid, int status)
265 {
266 if (WIFEXITED(status))
267 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
268 WEXITSTATUS(status));
269 else if (WIFSIGNALED(status))
270 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
271 WTERMSIG(status));
272 else
273 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
274 }
275
276 int wait_for_process(pid_t pid, int *status)
277 {
278 int ret;
279
280 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
281 for (;;) {
282 ret = waitpid(pid, status, 0);
283 if (ret >= 0 || errno != EINTR)
284 break;
285 }
286 if (ret < 0) {
287 ret = -ERRNO_TO_DSS_ERROR(errno);
288 make_err_msg("failed to wait for process %d", (int)pid);
289 } else
290 log_termination_msg(pid, *status);
291 return ret;
292 }
293
294 int remove_snapshot(struct snapshot *s, pid_t *pid)
295 {
296 int fds[3] = {0, 0, 0};
297 char *new_name = being_deleted_name(s);
298 int ret = dss_rename(s->name, new_name);
299 char *argv[] = {"rm", "-rf", new_name, NULL};
300
301 if (ret < 0)
302 goto out;
303 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
304 ret = dss_exec(pid, argv[0], argv, fds);
305 out:
306 free(new_name);
307 return ret;
308 }
309
310 int remove_redundant_snapshot(struct snapshot_list *sl,
311 int dry_run, pid_t *pid)
312 {
313 int ret, i, interval;
314 struct snapshot *s;
315 unsigned missing = 0;
316
317 DSS_INFO_LOG("looking for intervals containing too many snapshots\n");
318 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
319 unsigned keep = 1<<(conf.num_intervals_arg - interval - 1);
320 unsigned num = sl->interval_count[interval];
321 struct snapshot *victim = NULL, *prev = NULL;
322 int64_t score = LONG_MAX;
323
324 if (keep >= num)
325 missing += keep - num;
326 DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
327 interval, keep, num, missing);
328 if (keep + missing >= num)
329 continue;
330 /* redundant snapshot in this interval, pick snapshot with lowest score */
331 FOR_EACH_SNAPSHOT(s, i, sl) {
332 int64_t this_score;
333
334 DSS_DEBUG_LOG("checking %s\n", s->name);
335 if (s->interval > interval) {
336 prev = s;
337 continue;
338 }
339 if (s->interval < interval)
340 break;
341 if (!victim) {
342 victim = s;
343 prev = s;
344 continue;
345 }
346 assert(prev);
347 /* check if s is a better victim */
348 this_score = s->creation_time - prev->creation_time;
349 assert(this_score >= 0);
350 DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
351 if (this_score < score) {
352 score = this_score;
353 victim = s;
354 }
355 prev = s;
356 }
357 assert(victim);
358 if (dry_run) {
359 printf("%s would be removed (interval = %i)\n",
360 victim->name, victim->interval);
361 continue;
362 }
363 ret = remove_snapshot(victim, pid);
364 return ret < 0? ret : 1;
365 }
366 return 0;
367 }
368
369 int remove_old_snapshot(struct snapshot_list *sl, int dry_run, pid_t *pid)
370 {
371 int i, ret;
372 struct snapshot *s;
373
374 DSS_INFO_LOG("looking for snapshots belonging to intervals greater than %d\n",
375 conf.num_intervals_arg);
376 FOR_EACH_SNAPSHOT(s, i, sl) {
377 if (s->interval <= conf.num_intervals_arg)
378 continue;
379 if (dry_run) {
380 printf("%s would be removed (interval = %i)\n",
381 s->name, s->interval);
382 continue;
383 }
384 ret = remove_snapshot(s, pid);
385 if (ret < 0)
386 return ret;
387 return 1;
388 }
389 return 0;
390 }
391
392 int wait_for_rm_process(pid_t pid)
393 {
394 int status, es, ret = wait_for_process(pid, &status);
395 if (ret < 0)
396 return ret;
397 if (!WIFEXITED(status)) {
398 ret = E_INVOLUNTARY_EXIT;
399 make_err_msg("rm process %d died involuntary", (int)pid);
400 return ret;
401 }
402 es = WEXITSTATUS(status);
403 if (es) {
404 ret = -E_BAD_EXIT_CODE;
405 make_err_msg("rm process %d returned %d", (int)pid, es);
406 return ret;
407 }
408 return 1;
409 }
410
411 int com_run(int argc, char * const * argv)
412 {
413 return 42;
414 }
415
416 int com_prune(int argc, char * const * argv)
417 {
418 int ret, dry_run = 0;
419 struct snapshot_list sl;
420 pid_t pid;
421
422 if (argc > 2) {
423 make_err_msg("too many arguments");
424 return -E_SYNTAX;
425 }
426 if (argc == 2) {
427 if (strcmp(argv[1], "-d")) {
428 make_err_msg("%s", argv[1]);
429 return -E_SYNTAX;
430 }
431 dry_run = 1;
432 }
433 for (;;) {
434 get_snapshot_list(&sl);
435 ret = remove_old_snapshot(&sl, dry_run, &pid);
436 free_snapshot_list(&sl);
437 if (ret < 0)
438 return ret;
439 if (!ret)
440 break;
441 ret = wait_for_rm_process(pid);
442 if (ret < 0)
443 goto out;
444 }
445 for (;;) {
446 get_snapshot_list(&sl);
447 ret = remove_redundant_snapshot(&sl, dry_run, &pid);
448 free_snapshot_list(&sl);
449 if (ret < 0)
450 return ret;
451 if (!ret)
452 break;
453 ret = wait_for_rm_process(pid);
454 if (ret < 0)
455 goto out;
456 }
457 return 1;
458 out:
459 return ret;
460 }
461
462 struct newest_snapshot_data {
463 char * newest_name;
464 int64_t newest_creation_time;
465 int64_t now;
466 };
467
468 int get_newest_complete(const char *dirname, void *private)
469 {
470 struct newest_snapshot_data *nsd = private;
471 struct snapshot s;
472 int ret = is_snapshot(dirname, nsd->now, &s);
473
474 if (ret <= 0)
475 return 1;
476 if (s.creation_time < nsd->newest_creation_time)
477 return 1;
478 nsd->newest_creation_time = s.creation_time;
479 free(nsd->newest_name);
480 nsd->newest_name = s.name;
481 return 1;
482 }
483
484 __malloc char *name_of_newest_complete_snapshot(void)
485 {
486 struct newest_snapshot_data nsd = {
487 .now = get_current_time(),
488 .newest_creation_time = -1
489 };
490 for_each_subdir(get_newest_complete, &nsd);
491 return nsd.newest_name;
492 }
493
494 void create_rsync_argv(char ***argv, int64_t *num)
495 {
496 char *logname, *newest = name_of_newest_complete_snapshot();
497 int i = 0, j;
498
499 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
500 (*argv)[i++] = dss_strdup("rsync");
501 (*argv)[i++] = dss_strdup("-aq");
502 (*argv)[i++] = dss_strdup("--delete");
503 for (j = 0; j < conf.rsync_option_given; j++)
504 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
505 if (newest) {
506 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
507 (*argv)[i++] = make_message("--link-dest=../%s", newest);
508 free(newest);
509 } else
510 DSS_INFO_LOG("no previous snapshot found");
511 if (conf.exclude_patterns_given) {
512 (*argv)[i++] = dss_strdup("--exclude-from");
513 (*argv)[i++] = dss_strdup(conf.exclude_patterns_arg);
514
515 }
516 logname = dss_logname();
517 if (conf.remote_user_given && !strcmp(conf.remote_user_arg, logname))
518 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
519 else
520 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
521 conf.remote_user_arg : logname,
522 conf.remote_host_arg, conf.source_dir_arg);
523 free(logname);
524 *num = get_current_time();
525 (*argv)[i++] = incomplete_name(*num);
526 (*argv)[i++] = NULL;
527 for (j = 0; j < i; j++)
528 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
529 }
530
531 void free_rsync_argv(char **argv)
532 {
533 int i;
534 for (i = 0; argv[i]; i++)
535 free(argv[i]);
536 free(argv);
537 }
538
539 int create_snapshot(char **argv, pid_t *pid)
540 {
541 int fds[3] = {0, 0, 0};
542
543 return dss_exec(pid, argv[0], argv, fds);
544 }
545
546 int rename_incomplete_snapshot(int64_t start)
547 {
548 char *old_name, *new_name;
549 int ret;
550
551 ret = complete_name(start, get_current_time(), &new_name);
552 if (ret < 0)
553 return ret;
554 old_name = incomplete_name(start);
555 ret = dss_rename(old_name, new_name);
556 if (ret >= 0)
557 DSS_NOTICE_LOG("%s -> %s\n", old_name, new_name);
558 free(old_name);
559 free(new_name);
560 return ret;
561 }
562
563 int com_create(int argc, __a_unused char * const * argv)
564 {
565 int ret, status, es;
566 char **rsync_argv;
567 int64_t snapshot_num;
568 pid_t pid;
569
570 if (argc != 1) {
571 ret = -E_SYNTAX;
572 make_err_msg("create: no args expected, %d given", argc - 1);
573 return ret;
574 }
575 create_rsync_argv(&rsync_argv, &snapshot_num);
576 DSS_NOTICE_LOG("creating snapshot %lli\n", (long long)snapshot_num);
577 ret = create_snapshot(rsync_argv, &pid);
578 if (ret < 0)
579 goto out;
580 ret = wait_for_process(pid, &status);
581 if (ret < 0)
582 goto out;
583 if (!WIFEXITED(status)) {
584 ret = E_INVOLUNTARY_EXIT;
585 make_err_msg("rsync process %d died involuntary", (int)pid);
586 goto out;
587 }
588 es = WEXITSTATUS(status);
589 if (es != 0 && es != 23 && es != 24) {
590 ret = -E_BAD_EXIT_CODE;
591 make_err_msg("rsync process %d returned %d", (int)pid, es);
592 goto out;
593 }
594 ret = rename_incomplete_snapshot(snapshot_num);
595 out:
596 free_rsync_argv(rsync_argv);
597 return ret;
598 }
599
600 int com_ls(int argc, __a_unused char * const * argv)
601 {
602 int i, ret;
603 struct snapshot_list sl;
604 struct snapshot *s;
605 if (argc != 1) {
606 ret = -E_SYNTAX;
607 make_err_msg("ls: no args expected, %d given", argc - 1);
608 return ret;
609 }
610 get_snapshot_list(&sl);
611 FOR_EACH_SNAPSHOT(s, i, &sl)
612 printf("%u\t%s\n", s->interval, s->name);
613 free_snapshot_list(&sl);
614 return 1;
615 }
616
617 /* TODO: Unlink pid file */
618 __noreturn void clean_exit(int status)
619 {
620 //kill(0, SIGTERM);
621 free(dss_error_txt);
622 exit(status);
623 }
624
625 __printf_2_3 void dss_log(int ll, const char* fmt,...)
626 {
627 va_list argp;
628 if (ll < conf.loglevel_arg)
629 return;
630 va_start(argp, fmt);
631 vfprintf(stderr, fmt, argp);
632 va_end(argp);
633 }
634
635 int read_config_file(void)
636 {
637 int ret;
638 char *config_file;
639 struct stat statbuf;
640
641 if (conf.config_file_given)
642 config_file = dss_strdup(conf.config_file_arg);
643 else {
644 char *home = get_homedir();
645 config_file = make_message("%s/.dssrc", home);
646 free(home);
647 }
648 ret = stat(config_file, &statbuf);
649 if (ret && conf.config_file_given) {
650 ret = -ERRNO_TO_DSS_ERROR(errno);
651 make_err_msg("failed to stat config file %s", config_file);
652 goto out;
653 }
654 if (!ret) {
655 struct cmdline_parser_params params = {
656 .override = 0,
657 .initialize = 0,
658 .check_required = 0,
659 .check_ambiguity = 0
660 };
661 cmdline_parser_config_file(config_file, &conf, &params);
662 }
663 if (!conf.source_dir_given || !conf.dest_dir_given) {
664 ret = -E_SYNTAX;
665 make_err_msg("you need to specify both source_dir and dest_dir");
666 goto out;
667 }
668 ret = 1;
669 out:
670 free(config_file);
671 return ret;
672 }
673
674 int check_config(void)
675 {
676 if (conf.unit_interval_arg <= 0) {
677 make_err_msg("bad unit interval: %i", conf.unit_interval_arg);
678 return -E_INVALID_NUMBER;
679 }
680 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
681 if (conf.num_intervals_arg <= 0) {
682 make_err_msg("bad number of intervals %i", conf.num_intervals_arg);
683 return -E_INVALID_NUMBER;
684 }
685 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
686 return 1;
687 }
688
689 int main(int argc, char **argv)
690 {
691 int ret;
692
693 cmdline_parser(argc, argv, &conf); /* aborts on errors */
694 if (!conf.inputs_num) {
695 ret = -E_SYNTAX;
696 make_err_msg("no command given");
697 goto out;
698 }
699 ret = read_config_file();
700 if (ret < 0)
701 goto out;
702 ret = check_config();
703 if (ret < 0)
704 goto out;
705 ret = dss_chdir(conf.dest_dir_arg);
706 if (ret < 0)
707 goto out;
708 ret = call_command_handler(conf.inputs_num, conf.inputs);
709 out:
710 if (ret < 0)
711 log_err_msg(EMERG, -ret);
712 clean_exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);
713 }