18 #include "gcc-compat.h"
28 struct gengetopt_args_info conf;
29 char *dss_error_txt = NULL;
35 /* a litte cpp magic helps to DRY */
41 #define COMMAND(x) int com_ ##x(void);
44 #define COMMAND(x) if (conf.x ##_given) return com_ ##x();
45 int call_command_handler(void)
48 DSS_EMERG_LOG("BUG: did not find command handler\n");
55 * complete, not being deleted: 1204565370-1204565371.Sun_Mar_02_2008_14_33-Sun_Mar_02_2008_14_43
56 * complete, being deleted: 1204565370-1204565371.being_deleted
57 * incomplete, not being deleted: 1204565370-incomplete
58 * incomplete, being deleted: 1204565370-incomplete.being_deleted
60 enum snapshot_status_flags {
67 int64_t creation_time;
68 int64_t completion_time;
69 enum snapshot_status_flags flags;
73 __printf_2_3 void dss_log(int ll, const char* fmt,...)
81 if (ll < conf.loglevel_arg)
83 outfd = logfile? logfile : stderr;
86 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
87 fprintf(outfd, "%s ", str);
88 if (conf.loglevel_arg <= INFO)
89 fprintf(outfd, "%i: ", ll);
91 vfprintf(outfd, fmt, argp);
96 * Print a message either to stdout or to the log file.
98 __printf_1_2 void dss_msg(const char* fmt,...)
100 FILE *outfd = conf.daemon_given? logfile : stdout;
103 vfprintf(outfd, fmt, argp);
107 int is_snapshot(const char *dirname, int64_t now, struct snapshot *s)
110 char *dash, *dot, *tmp;
114 dash = strchr(dirname, '-');
115 if (!dash || !dash[1] || dash == dirname)
117 for (i = 0; dirname[i] != '-'; i++)
118 if (!isdigit(dirname[i]))
120 tmp = dss_strdup(dirname);
122 ret = dss_atoi64(tmp, &num);
131 s->creation_time = num;
132 //DSS_DEBUG_LOG("%s start time: %lli\n", dirname, (long long)s->creation_time);
133 s->interval = (long long) ((now - s->creation_time)
134 / conf.unit_interval_arg / 24 / 3600);
135 if (!strcmp(dash + 1, "incomplete")) {
136 s->completion_time = -1;
137 s->flags = 0; /* neither complete, nor being deleted */
140 if (!strcmp(dash + 1, "incomplete.being_deleted")) {
141 s->completion_time = -1;
142 s->flags = SS_BEING_DELETED; /* mot cpmplete, being deleted */
146 dot = strchr(tmp, '.');
147 if (!dot || !dot[1] || dot == tmp)
149 for (i = 0; tmp[i] != '.'; i++)
150 if (!isdigit(tmp[i]))
152 tmp = dss_strdup(dash + 1);
154 ret = dss_atoi64(tmp, &num);
162 s->completion_time = num;
163 s->flags = SS_COMPLETE;
164 if (strcmp(dot + 1, "being_deleted"))
165 s->flags |= SS_BEING_DELETED;
167 s->name = dss_strdup(dirname);
171 int64_t get_current_time(void)
175 DSS_DEBUG_LOG("now: %lli\n", (long long) now);
179 char *incomplete_name(int64_t start)
181 return make_message("%lli-incomplete", (long long)start);
184 char *being_deleted_name(struct snapshot *s)
186 if (s->flags & SS_COMPLETE)
187 return make_message("%lli-%lli.being_deleted",
188 (long long)s->creation_time,
189 (long long)s->completion_time);
190 return make_message("%lli-incomplete.being_deleted",
191 (long long)s->creation_time);
194 int complete_name(int64_t start, int64_t end, char **result)
196 struct tm start_tm, end_tm;
197 time_t *start_seconds = (time_t *) (uint64_t *)&start; /* STFU, gcc */
198 time_t *end_seconds = (time_t *) (uint64_t *)&end; /* STFU, gcc */
199 char start_str[200], end_str[200];
201 if (!localtime_r(start_seconds, &start_tm)) {
202 make_err_msg("%lli", (long long)start);
205 if (!localtime_r(end_seconds, &end_tm)) {
206 make_err_msg("%lli", (long long)end);
209 if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm)) {
210 make_err_msg("%lli", (long long)start);
213 if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm)) {
214 make_err_msg("%lli", (long long)end);
217 *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
222 struct snapshot_list {
224 unsigned num_snapshots;
226 struct snapshot **snapshots;
228 * Array of size num_intervals + 1
230 * It contains the number of snapshots in each interval. interval_count[num_intervals]
231 * is the number of snapshots which belong to any interval greater than num_intervals.
233 unsigned *interval_count;
236 #define FOR_EACH_SNAPSHOT(s, i, sl) \
237 for ((i) = 0; (i) < (sl)->num_snapshots && ((s) = (sl)->snapshots[(i)]); (i)++)
241 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
243 static int compare_snapshots(const void *a, const void *b)
245 struct snapshot *s1 = *(struct snapshot **)a;
246 struct snapshot *s2 = *(struct snapshot **)b;
247 return NUM_COMPARE(s2->creation_time, s1->creation_time);
250 /** Compute the minimum of \a a and \a b. */
251 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
253 int add_snapshot(const char *dirname, void *private)
255 struct snapshot_list *sl = private;
257 int ret = is_snapshot(dirname, sl->now, &s);
261 if (sl->num_snapshots >= sl->array_size) {
262 sl->array_size = 2 * sl->array_size + 1;
263 sl->snapshots = dss_realloc(sl->snapshots,
264 sl->array_size * sizeof(struct snapshot *));
266 sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
267 *(sl->snapshots[sl->num_snapshots]) = s;
268 sl->interval_count[DSS_MIN(s.interval, conf.num_intervals_arg)]++;
273 void get_snapshot_list(struct snapshot_list *sl)
275 sl->now = get_current_time();
276 sl->num_snapshots = 0;
278 sl->snapshots = NULL;
279 sl->interval_count = dss_calloc((conf.num_intervals_arg + 1) * sizeof(unsigned));
280 for_each_subdir(add_snapshot, sl);
281 qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
285 void free_snapshot_list(struct snapshot_list *sl)
290 FOR_EACH_SNAPSHOT(s, i, sl) {
294 free(sl->interval_count);
299 * Print a log message about the exit status of a child.
301 void log_termination_msg(pid_t pid, int status)
303 if (WIFEXITED(status))
304 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
305 WEXITSTATUS(status));
306 else if (WIFSIGNALED(status))
307 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
310 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
313 int wait_for_process(pid_t pid, int *status)
317 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
319 ret = waitpid(pid, status, 0);
320 if (ret >= 0 || errno != EINTR)
324 ret = -ERRNO_TO_DSS_ERROR(errno);
325 make_err_msg("failed to wait for process %d", (int)pid);
327 log_termination_msg(pid, *status);
331 int remove_snapshot(struct snapshot *s, pid_t *pid)
333 int fds[3] = {0, 0, 0};
334 char *new_name = being_deleted_name(s);
335 int ret = dss_rename(s->name, new_name);
336 char *argv[] = {"rm", "-rf", new_name, NULL};
340 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
341 ret = dss_exec(pid, argv[0], argv, fds);
347 int remove_redundant_snapshot(struct snapshot_list *sl, pid_t *pid)
349 int ret, i, interval;
351 unsigned missing = 0;
353 DSS_INFO_LOG("looking for intervals containing too many snapshots\n");
354 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
355 unsigned keep = 1<<(conf.num_intervals_arg - interval - 1);
356 unsigned num = sl->interval_count[interval];
357 struct snapshot *victim = NULL, *prev = NULL;
358 int64_t score = LONG_MAX;
361 missing += keep - num;
362 DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
363 interval, keep, num, missing);
364 if (keep + missing >= num)
366 /* redundant snapshot in this interval, pick snapshot with lowest score */
367 FOR_EACH_SNAPSHOT(s, i, sl) {
370 DSS_DEBUG_LOG("checking %s\n", s->name);
371 if (s->interval > interval) {
375 if (s->interval < interval)
383 /* check if s is a better victim */
384 this_score = s->creation_time - prev->creation_time;
385 assert(this_score >= 0);
386 DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
387 if (this_score < score) {
394 if (conf.dry_run_given) {
395 dss_msg("%s would be removed (interval = %i)\n",
396 victim->name, victim->interval);
399 ret = remove_snapshot(victim, pid);
400 return ret < 0? ret : 1;
405 int remove_old_snapshot(struct snapshot_list *sl, pid_t *pid)
410 DSS_INFO_LOG("looking for snapshots belonging to intervals greater than %d\n",
411 conf.num_intervals_arg);
412 FOR_EACH_SNAPSHOT(s, i, sl) {
413 if (s->interval <= conf.num_intervals_arg)
415 if (conf.dry_run_given) {
416 dss_msg("%s would be removed (interval = %i)\n",
417 s->name, s->interval);
420 ret = remove_snapshot(s, pid);
428 int wait_for_rm_process(pid_t pid)
430 int status, es, ret = wait_for_process(pid, &status);
433 if (!WIFEXITED(status)) {
434 ret = E_INVOLUNTARY_EXIT;
435 make_err_msg("rm process %d died involuntary", (int)pid);
438 es = WEXITSTATUS(status);
440 ret = -E_BAD_EXIT_CODE;
441 make_err_msg("rm process %d returned %d", (int)pid, es);
449 if (conf.dry_run_given) {
450 make_err_msg("dry_run not supported by this command");
459 struct snapshot_list sl;
463 get_snapshot_list(&sl);
464 ret = remove_old_snapshot(&sl, &pid);
465 free_snapshot_list(&sl);
470 ret = wait_for_rm_process(pid);
475 get_snapshot_list(&sl);
476 ret = remove_redundant_snapshot(&sl, &pid);
477 free_snapshot_list(&sl);
482 ret = wait_for_rm_process(pid);
491 struct newest_snapshot_data {
493 int64_t newest_creation_time;
497 int get_newest_complete(const char *dirname, void *private)
499 struct newest_snapshot_data *nsd = private;
501 int ret = is_snapshot(dirname, nsd->now, &s);
505 if (s.creation_time < nsd->newest_creation_time)
507 nsd->newest_creation_time = s.creation_time;
508 free(nsd->newest_name);
509 nsd->newest_name = s.name;
513 __malloc char *name_of_newest_complete_snapshot(void)
515 struct newest_snapshot_data nsd = {
516 .now = get_current_time(),
517 .newest_creation_time = -1
519 for_each_subdir(get_newest_complete, &nsd);
520 return nsd.newest_name;
523 void create_rsync_argv(char ***argv, int64_t *num)
525 char *logname, *newest = name_of_newest_complete_snapshot();
528 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
529 (*argv)[i++] = dss_strdup("rsync");
530 (*argv)[i++] = dss_strdup("-aq");
531 (*argv)[i++] = dss_strdup("--delete");
532 for (j = 0; j < conf.rsync_option_given; j++)
533 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
535 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
536 (*argv)[i++] = make_message("--link-dest=../%s", newest);
539 DSS_INFO_LOG("no previous snapshot found");
540 if (conf.exclude_patterns_given) {
541 (*argv)[i++] = dss_strdup("--exclude-from");
542 (*argv)[i++] = dss_strdup(conf.exclude_patterns_arg);
545 logname = dss_logname();
546 if (conf.remote_user_given && !strcmp(conf.remote_user_arg, logname))
547 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
549 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
550 conf.remote_user_arg : logname,
551 conf.remote_host_arg, conf.source_dir_arg);
553 *num = get_current_time();
554 (*argv)[i++] = incomplete_name(*num);
556 for (j = 0; j < i; j++)
557 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
560 void free_rsync_argv(char **argv)
563 for (i = 0; argv[i]; i++)
568 int create_snapshot(char **argv, pid_t *pid)
570 int fds[3] = {0, 0, 0};
572 return dss_exec(pid, argv[0], argv, fds);
575 int rename_incomplete_snapshot(int64_t start)
577 char *old_name, *new_name;
580 ret = complete_name(start, get_current_time(), &new_name);
583 old_name = incomplete_name(start);
584 ret = dss_rename(old_name, new_name);
586 DSS_NOTICE_LOG("%s -> %s\n", old_name, new_name);
596 int64_t snapshot_num;
599 create_rsync_argv(&rsync_argv, &snapshot_num);
600 if (conf.dry_run_given) {
603 for (i = 0; rsync_argv[i]; i++) {
605 msg = make_message("%s%s%s", tmp? tmp : "",
606 tmp? " " : "", rsync_argv[i]);
609 dss_msg("%s\n", msg);
613 DSS_NOTICE_LOG("creating snapshot %lli\n", (long long)snapshot_num);
614 ret = create_snapshot(rsync_argv, &pid);
617 ret = wait_for_process(pid, &status);
620 if (!WIFEXITED(status)) {
621 ret = E_INVOLUNTARY_EXIT;
622 make_err_msg("rsync process %d died involuntary", (int)pid);
625 es = WEXITSTATUS(status);
626 if (es != 0 && es != 23 && es != 24) {
627 ret = -E_BAD_EXIT_CODE;
628 make_err_msg("rsync process %d returned %d", (int)pid, es);
631 ret = rename_incomplete_snapshot(snapshot_num);
633 free_rsync_argv(rsync_argv);
640 struct snapshot_list sl;
642 get_snapshot_list(&sl);
643 FOR_EACH_SNAPSHOT(s, i, &sl)
644 dss_msg("%u\t%s\n", s->interval, s->name);
645 free_snapshot_list(&sl);
649 /* TODO: Unlink pid file */
650 __noreturn void clean_exit(int status)
657 int read_config_file(void)
663 if (conf.config_file_given)
664 config_file = dss_strdup(conf.config_file_arg);
666 char *home = get_homedir();
667 config_file = make_message("%s/.dssrc", home);
670 ret = stat(config_file, &statbuf);
671 if (ret && conf.config_file_given) {
672 ret = -ERRNO_TO_DSS_ERROR(errno);
673 make_err_msg("failed to stat config file %s", config_file);
677 struct cmdline_parser_params params = {
683 cmdline_parser_config_file(config_file, &conf, ¶ms);
685 if (!conf.source_dir_given || !conf.dest_dir_given) {
687 make_err_msg("you need to specify both source_dir and dest_dir");
696 int check_config(void)
698 if (conf.unit_interval_arg <= 0) {
699 make_err_msg("bad unit interval: %i", conf.unit_interval_arg);
700 return -E_INVALID_NUMBER;
702 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
703 if (conf.num_intervals_arg <= 0) {
704 make_err_msg("bad number of intervals %i", conf.num_intervals_arg);
705 return -E_INVALID_NUMBER;
707 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
711 int main(int argc, char **argv)
715 cmdline_parser(argc, argv, &conf); /* aborts on errors */
716 if (conf.inputs_num) {
718 make_err_msg("additional non-options given");
721 ret = read_config_file();
724 ret = check_config();
727 if (conf.logfile_given) {
728 logfile = open_log(conf.logfile_arg);
729 log_welcome(conf.loglevel_arg);
731 if (conf.daemon_given)
733 ret = dss_chdir(conf.dest_dir_arg);
736 ret = call_command_handler();
739 log_err_msg(EMERG, -ret);
740 clean_exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);