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);
95 __printf_1_2 void msg(const char* fmt,...)
97 FILE *outfd = conf.daemon_given? logfile : stdout;
100 vfprintf(outfd, fmt, argp);
104 int is_snapshot(const char *dirname, int64_t now, struct snapshot *s)
107 char *dash, *dot, *tmp;
111 dash = strchr(dirname, '-');
112 if (!dash || !dash[1] || dash == dirname)
114 for (i = 0; dirname[i] != '-'; i++)
115 if (!isdigit(dirname[i]))
117 tmp = dss_strdup(dirname);
119 ret = dss_atoi64(tmp, &num);
128 s->creation_time = num;
129 //DSS_DEBUG_LOG("%s start time: %lli\n", dirname, (long long)s->creation_time);
130 s->interval = (long long) ((now - s->creation_time)
131 / conf.unit_interval_arg / 24 / 3600);
132 if (!strcmp(dash + 1, "incomplete")) {
133 s->completion_time = -1;
134 s->flags = 0; /* neither complete, nor being deleted */
137 if (!strcmp(dash + 1, "incomplete.being_deleted")) {
138 s->completion_time = -1;
139 s->flags = SS_BEING_DELETED; /* mot cpmplete, being deleted */
143 dot = strchr(tmp, '.');
144 if (!dot || !dot[1] || dot == tmp)
146 for (i = 0; tmp[i] != '.'; i++)
147 if (!isdigit(tmp[i]))
149 tmp = dss_strdup(dash + 1);
151 ret = dss_atoi64(tmp, &num);
159 s->completion_time = num;
160 s->flags = SS_COMPLETE;
161 if (strcmp(dot + 1, "being_deleted"))
162 s->flags |= SS_BEING_DELETED;
164 s->name = dss_strdup(dirname);
168 int64_t get_current_time(void)
172 DSS_DEBUG_LOG("now: %lli\n", (long long) now);
176 char *incomplete_name(int64_t start)
178 return make_message("%lli-incomplete", (long long)start);
181 char *being_deleted_name(struct snapshot *s)
183 if (s->flags & SS_COMPLETE)
184 return make_message("%lli-%lli.being_deleted",
185 (long long)s->creation_time,
186 (long long)s->completion_time);
187 return make_message("%lli-incomplete.being_deleted",
188 (long long)s->creation_time);
191 int complete_name(int64_t start, int64_t end, char **result)
193 struct tm start_tm, end_tm;
194 time_t *start_seconds = (time_t *) (uint64_t *)&start; /* STFU, gcc */
195 time_t *end_seconds = (time_t *) (uint64_t *)&end; /* STFU, gcc */
196 char start_str[200], end_str[200];
198 if (!localtime_r(start_seconds, &start_tm)) {
199 make_err_msg("%lli", (long long)start);
202 if (!localtime_r(end_seconds, &end_tm)) {
203 make_err_msg("%lli", (long long)end);
206 if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm)) {
207 make_err_msg("%lli", (long long)start);
210 if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm)) {
211 make_err_msg("%lli", (long long)end);
214 *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
219 struct snapshot_list {
221 unsigned num_snapshots;
223 struct snapshot **snapshots;
225 * Array of size num_intervals + 1
227 * It contains the number of snapshots in each interval. interval_count[num_intervals]
228 * is the number of snapshots which belong to any interval greater than num_intervals.
230 unsigned *interval_count;
233 #define FOR_EACH_SNAPSHOT(s, i, sl) \
234 for ((i) = 0; (i) < (sl)->num_snapshots && ((s) = (sl)->snapshots[(i)]); (i)++)
238 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
240 static int compare_snapshots(const void *a, const void *b)
242 struct snapshot *s1 = *(struct snapshot **)a;
243 struct snapshot *s2 = *(struct snapshot **)b;
244 return NUM_COMPARE(s2->creation_time, s1->creation_time);
247 /** Compute the minimum of \a a and \a b. */
248 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
250 int add_snapshot(const char *dirname, void *private)
252 struct snapshot_list *sl = private;
254 int ret = is_snapshot(dirname, sl->now, &s);
258 if (sl->num_snapshots >= sl->array_size) {
259 sl->array_size = 2 * sl->array_size + 1;
260 sl->snapshots = dss_realloc(sl->snapshots,
261 sl->array_size * sizeof(struct snapshot *));
263 sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
264 *(sl->snapshots[sl->num_snapshots]) = s;
265 sl->interval_count[DSS_MIN(s.interval, conf.num_intervals_arg)]++;
270 void get_snapshot_list(struct snapshot_list *sl)
272 sl->now = get_current_time();
273 sl->num_snapshots = 0;
275 sl->snapshots = NULL;
276 sl->interval_count = dss_calloc((conf.num_intervals_arg + 1) * sizeof(unsigned));
277 for_each_subdir(add_snapshot, sl);
278 qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
282 void free_snapshot_list(struct snapshot_list *sl)
287 FOR_EACH_SNAPSHOT(s, i, sl) {
291 free(sl->interval_count);
296 * Print a log message about the exit status of a child.
298 void log_termination_msg(pid_t pid, int status)
300 if (WIFEXITED(status))
301 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
302 WEXITSTATUS(status));
303 else if (WIFSIGNALED(status))
304 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
307 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
310 int wait_for_process(pid_t pid, int *status)
314 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
316 ret = waitpid(pid, status, 0);
317 if (ret >= 0 || errno != EINTR)
321 ret = -ERRNO_TO_DSS_ERROR(errno);
322 make_err_msg("failed to wait for process %d", (int)pid);
324 log_termination_msg(pid, *status);
328 int remove_snapshot(struct snapshot *s, pid_t *pid)
330 int fds[3] = {0, 0, 0};
331 char *new_name = being_deleted_name(s);
332 int ret = dss_rename(s->name, new_name);
333 char *argv[] = {"rm", "-rf", new_name, NULL};
337 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
338 ret = dss_exec(pid, argv[0], argv, fds);
344 int remove_redundant_snapshot(struct snapshot_list *sl,
345 int dry_run, pid_t *pid)
347 int ret, i, interval;
349 unsigned missing = 0;
351 DSS_INFO_LOG("looking for intervals containing too many snapshots\n");
352 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
353 unsigned keep = 1<<(conf.num_intervals_arg - interval - 1);
354 unsigned num = sl->interval_count[interval];
355 struct snapshot *victim = NULL, *prev = NULL;
356 int64_t score = LONG_MAX;
359 missing += keep - num;
360 DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
361 interval, keep, num, missing);
362 if (keep + missing >= num)
364 /* redundant snapshot in this interval, pick snapshot with lowest score */
365 FOR_EACH_SNAPSHOT(s, i, sl) {
368 DSS_DEBUG_LOG("checking %s\n", s->name);
369 if (s->interval > interval) {
373 if (s->interval < interval)
381 /* check if s is a better victim */
382 this_score = s->creation_time - prev->creation_time;
383 assert(this_score >= 0);
384 DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
385 if (this_score < score) {
393 msg("%s would be removed (interval = %i)\n",
394 victim->name, victim->interval);
397 ret = remove_snapshot(victim, pid);
398 return ret < 0? ret : 1;
403 int remove_old_snapshot(struct snapshot_list *sl, int dry_run, pid_t *pid)
408 DSS_INFO_LOG("looking for snapshots belonging to intervals greater than %d\n",
409 conf.num_intervals_arg);
410 FOR_EACH_SNAPSHOT(s, i, sl) {
411 if (s->interval <= conf.num_intervals_arg)
414 msg("%s would be removed (interval = %i)\n",
415 s->name, s->interval);
418 ret = remove_snapshot(s, pid);
426 int wait_for_rm_process(pid_t pid)
428 int status, es, ret = wait_for_process(pid, &status);
431 if (!WIFEXITED(status)) {
432 ret = E_INVOLUNTARY_EXIT;
433 make_err_msg("rm process %d died involuntary", (int)pid);
436 es = WEXITSTATUS(status);
438 ret = -E_BAD_EXIT_CODE;
439 make_err_msg("rm process %d returned %d", (int)pid, es);
452 int ret, dry_run = 0;
453 struct snapshot_list sl;
457 get_snapshot_list(&sl);
458 ret = remove_old_snapshot(&sl, dry_run, &pid);
459 free_snapshot_list(&sl);
464 ret = wait_for_rm_process(pid);
469 get_snapshot_list(&sl);
470 ret = remove_redundant_snapshot(&sl, dry_run, &pid);
471 free_snapshot_list(&sl);
476 ret = wait_for_rm_process(pid);
485 struct newest_snapshot_data {
487 int64_t newest_creation_time;
491 int get_newest_complete(const char *dirname, void *private)
493 struct newest_snapshot_data *nsd = private;
495 int ret = is_snapshot(dirname, nsd->now, &s);
499 if (s.creation_time < nsd->newest_creation_time)
501 nsd->newest_creation_time = s.creation_time;
502 free(nsd->newest_name);
503 nsd->newest_name = s.name;
507 __malloc char *name_of_newest_complete_snapshot(void)
509 struct newest_snapshot_data nsd = {
510 .now = get_current_time(),
511 .newest_creation_time = -1
513 for_each_subdir(get_newest_complete, &nsd);
514 return nsd.newest_name;
517 void create_rsync_argv(char ***argv, int64_t *num)
519 char *logname, *newest = name_of_newest_complete_snapshot();
522 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
523 (*argv)[i++] = dss_strdup("rsync");
524 (*argv)[i++] = dss_strdup("-aq");
525 (*argv)[i++] = dss_strdup("--delete");
526 for (j = 0; j < conf.rsync_option_given; j++)
527 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
529 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
530 (*argv)[i++] = make_message("--link-dest=../%s", newest);
533 DSS_INFO_LOG("no previous snapshot found");
534 if (conf.exclude_patterns_given) {
535 (*argv)[i++] = dss_strdup("--exclude-from");
536 (*argv)[i++] = dss_strdup(conf.exclude_patterns_arg);
539 logname = dss_logname();
540 if (conf.remote_user_given && !strcmp(conf.remote_user_arg, logname))
541 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
543 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
544 conf.remote_user_arg : logname,
545 conf.remote_host_arg, conf.source_dir_arg);
547 *num = get_current_time();
548 (*argv)[i++] = incomplete_name(*num);
550 for (j = 0; j < i; j++)
551 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
554 void free_rsync_argv(char **argv)
557 for (i = 0; argv[i]; i++)
562 int create_snapshot(char **argv, pid_t *pid)
564 int fds[3] = {0, 0, 0};
566 return dss_exec(pid, argv[0], argv, fds);
569 int rename_incomplete_snapshot(int64_t start)
571 char *old_name, *new_name;
574 ret = complete_name(start, get_current_time(), &new_name);
577 old_name = incomplete_name(start);
578 ret = dss_rename(old_name, new_name);
580 DSS_NOTICE_LOG("%s -> %s\n", old_name, new_name);
590 int64_t snapshot_num;
593 create_rsync_argv(&rsync_argv, &snapshot_num);
594 DSS_NOTICE_LOG("creating snapshot %lli\n", (long long)snapshot_num);
595 ret = create_snapshot(rsync_argv, &pid);
598 ret = wait_for_process(pid, &status);
601 if (!WIFEXITED(status)) {
602 ret = E_INVOLUNTARY_EXIT;
603 make_err_msg("rsync process %d died involuntary", (int)pid);
606 es = WEXITSTATUS(status);
607 if (es != 0 && es != 23 && es != 24) {
608 ret = -E_BAD_EXIT_CODE;
609 make_err_msg("rsync process %d returned %d", (int)pid, es);
612 ret = rename_incomplete_snapshot(snapshot_num);
614 free_rsync_argv(rsync_argv);
621 struct snapshot_list sl;
623 get_snapshot_list(&sl);
624 FOR_EACH_SNAPSHOT(s, i, &sl)
625 msg("%u\t%s\n", s->interval, s->name);
626 free_snapshot_list(&sl);
630 /* TODO: Unlink pid file */
631 __noreturn void clean_exit(int status)
638 int read_config_file(void)
644 if (conf.config_file_given)
645 config_file = dss_strdup(conf.config_file_arg);
647 char *home = get_homedir();
648 config_file = make_message("%s/.dssrc", home);
651 ret = stat(config_file, &statbuf);
652 if (ret && conf.config_file_given) {
653 ret = -ERRNO_TO_DSS_ERROR(errno);
654 make_err_msg("failed to stat config file %s", config_file);
658 struct cmdline_parser_params params = {
664 cmdline_parser_config_file(config_file, &conf, ¶ms);
666 if (!conf.source_dir_given || !conf.dest_dir_given) {
668 make_err_msg("you need to specify both source_dir and dest_dir");
677 int check_config(void)
679 if (conf.unit_interval_arg <= 0) {
680 make_err_msg("bad unit interval: %i", conf.unit_interval_arg);
681 return -E_INVALID_NUMBER;
683 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
684 if (conf.num_intervals_arg <= 0) {
685 make_err_msg("bad number of intervals %i", conf.num_intervals_arg);
686 return -E_INVALID_NUMBER;
688 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
692 int main(int argc, char **argv)
696 cmdline_parser(argc, argv, &conf); /* aborts on errors */
697 if (conf.inputs_num) {
699 make_err_msg("additional non-options given");
702 ret = read_config_file();
705 ret = check_config();
708 if (conf.logfile_given) {
709 logfile = open_log(conf.logfile_arg);
710 log_welcome(conf.loglevel_arg);
712 if (conf.daemon_given)
714 ret = dss_chdir(conf.dest_dir_arg);
717 ret = call_command_handler();
720 log_err_msg(EMERG, -ret);
721 clean_exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);