18 #include "gcc-compat.h"
27 struct gengetopt_args_info conf;
28 char *dss_error_txt = NULL;
32 /** Defines one dss command. */
33 struct server_command {
34 /** The name of the command. */
36 /** Pointer to the function that handles the command. */
37 int (*handler)(int, char * const * const);
40 /* a litte cpp magic helps to DRY */
41 #define SERVER_COMMANDS \
43 SERVER_COMMAND(create) \
44 SERVER_COMMAND(prune) \
45 SERVER_COMMAND(daemon)
46 #define SERVER_COMMAND(x) int com_ ##x(int, char * const * const);
49 #define SERVER_COMMAND(x) {.name = #x, .handler = com_ ## x},
50 static struct server_command command_list[] = {
52 {.name = NULL, .handler = NULL}
57 * complete, not being deleted: 1204565370-1204565371.Sun_Mar_02_2008_14_33-Sun_Mar_02_2008_14_43
58 * complete, being deleted: 1204565370-1204565371.being_deleted
59 * incomplete, not being deleted: 1204565370-incomplete
60 * incomplete, being deleted: 1204565370-incomplete.being_deleted
62 enum snapshot_status_flags {
69 int64_t creation_time;
70 int64_t completion_time;
71 enum snapshot_status_flags flags;
75 int is_snapshot(const char *dirname, int64_t now, struct snapshot *s)
78 char *dash, *dot, *tmp;
82 dash = strchr(dirname, '-');
83 if (!dash || !dash[1] || dash == dirname)
85 for (i = 0; dirname[i] != '-'; i++)
86 if (!isdigit(dirname[i]))
88 tmp = dss_strdup(dirname);
90 ret = dss_atoi64(tmp, &num);
99 s->creation_time = num;
100 //DSS_DEBUG_LOG("%s start time: %lli\n", dirname, (long long)s->creation_time);
101 s->interval = (long long) ((now - s->creation_time)
102 / conf.unit_interval_arg / 24 / 3600);
103 if (!strcmp(dash + 1, "incomplete")) {
104 s->completion_time = -1;
105 s->flags = 0; /* neither complete, nor being deleted */
108 if (!strcmp(dash + 1, "incomplete.being_deleted")) {
109 s->completion_time = -1;
110 s->flags = SS_BEING_DELETED; /* mot cpmplete, being deleted */
114 dot = strchr(tmp, '.');
115 if (!dot || !dot[1] || dot == tmp)
117 for (i = 0; tmp[i] != '.'; i++)
118 if (!isdigit(tmp[i]))
120 tmp = dss_strdup(dash + 1);
122 ret = dss_atoi64(tmp, &num);
130 s->completion_time = num;
131 s->flags = SS_COMPLETE;
132 if (strcmp(dot + 1, "being_deleted"))
133 s->flags |= SS_BEING_DELETED;
135 s->name = dss_strdup(dirname);
139 int64_t get_current_time(void)
143 DSS_DEBUG_LOG("now: %lli\n", (long long) now);
147 char *incomplete_name(int64_t start)
149 return make_message("%lli-incomplete", (long long)start);
152 char *being_deleted_name(struct snapshot *s)
154 if (s->flags & SS_COMPLETE)
155 return make_message("%lli-%lli.being_deleted",
156 (long long)s->creation_time,
157 (long long)s->completion_time);
158 return make_message("%lli-incomplete.being_deleted",
159 (long long)s->creation_time);
162 int complete_name(int64_t start, int64_t end, char **result)
164 struct tm start_tm, end_tm;
165 time_t *start_seconds = (time_t *) (uint64_t *)&start; /* STFU, gcc */
166 time_t *end_seconds = (time_t *) (uint64_t *)&end; /* STFU, gcc */
167 char start_str[200], end_str[200];
169 if (!localtime_r(start_seconds, &start_tm)) {
170 make_err_msg("%lli", (long long)start);
173 if (!localtime_r(end_seconds, &end_tm)) {
174 make_err_msg("%lli", (long long)end);
177 if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm)) {
178 make_err_msg("%lli", (long long)start);
181 if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm)) {
182 make_err_msg("%lli", (long long)end);
185 *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
190 struct snapshot_list {
192 unsigned num_snapshots;
194 struct snapshot **snapshots;
196 * Array of size num_intervals + 1
198 * It contains the number of snapshots in each interval. interval_count[num_intervals]
199 * is the number of snapshots which belong to any interval greater than num_intervals.
201 unsigned *interval_count;
204 #define FOR_EACH_SNAPSHOT(s, i, sl) \
205 for ((i) = 0; (i) < (sl)->num_snapshots && ((s) = (sl)->snapshots[(i)]); (i)++)
209 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
211 static int compare_snapshots(const void *a, const void *b)
213 struct snapshot *s1 = *(struct snapshot **)a;
214 struct snapshot *s2 = *(struct snapshot **)b;
215 return NUM_COMPARE(s2->creation_time, s1->creation_time);
218 /** Compute the minimum of \a a and \a b. */
219 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
221 int add_snapshot(const char *dirname, void *private)
223 struct snapshot_list *sl = private;
225 int ret = is_snapshot(dirname, sl->now, &s);
229 if (sl->num_snapshots >= sl->array_size) {
230 sl->array_size = 2 * sl->array_size + 1;
231 sl->snapshots = dss_realloc(sl->snapshots,
232 sl->array_size * sizeof(struct snapshot *));
234 sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
235 *(sl->snapshots[sl->num_snapshots]) = s;
236 sl->interval_count[DSS_MIN(s.interval, conf.num_intervals_arg)]++;
241 void get_snapshot_list(struct snapshot_list *sl)
243 sl->now = get_current_time();
244 sl->num_snapshots = 0;
246 sl->snapshots = NULL;
247 sl->interval_count = dss_calloc((conf.num_intervals_arg + 1) * sizeof(unsigned));
248 for_each_subdir(add_snapshot, sl);
249 qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
253 void free_snapshot_list(struct snapshot_list *sl)
258 FOR_EACH_SNAPSHOT(s, i, sl) {
262 free(sl->interval_count);
267 * Print a log message about the exit status of a child.
269 void log_termination_msg(pid_t pid, int status)
271 if (WIFEXITED(status))
272 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
273 WEXITSTATUS(status));
274 else if (WIFSIGNALED(status))
275 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
278 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
281 int wait_for_process(pid_t pid, int *status)
285 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
287 ret = waitpid(pid, status, 0);
288 if (ret >= 0 || errno != EINTR)
292 ret = -ERRNO_TO_DSS_ERROR(errno);
293 make_err_msg("failed to wait for process %d", (int)pid);
295 log_termination_msg(pid, *status);
299 int remove_snapshot(struct snapshot *s, pid_t *pid)
301 int fds[3] = {0, 0, 0};
302 char *new_name = being_deleted_name(s);
303 int ret = dss_rename(s->name, new_name);
304 char *argv[] = {"rm", "-rf", new_name, NULL};
308 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
309 ret = dss_exec(pid, argv[0], argv, fds);
315 int remove_redundant_snapshot(struct snapshot_list *sl,
316 int dry_run, pid_t *pid)
318 int ret, i, interval;
320 unsigned missing = 0;
322 DSS_INFO_LOG("looking for intervals containing too many snapshots\n");
323 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
324 unsigned keep = 1<<(conf.num_intervals_arg - interval - 1);
325 unsigned num = sl->interval_count[interval];
326 struct snapshot *victim = NULL, *prev = NULL;
327 int64_t score = LONG_MAX;
330 missing += keep - num;
331 DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
332 interval, keep, num, missing);
333 if (keep + missing >= num)
335 /* redundant snapshot in this interval, pick snapshot with lowest score */
336 FOR_EACH_SNAPSHOT(s, i, sl) {
339 DSS_DEBUG_LOG("checking %s\n", s->name);
340 if (s->interval > interval) {
344 if (s->interval < interval)
352 /* check if s is a better victim */
353 this_score = s->creation_time - prev->creation_time;
354 assert(this_score >= 0);
355 DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
356 if (this_score < score) {
364 printf("%s would be removed (interval = %i)\n",
365 victim->name, victim->interval);
368 ret = remove_snapshot(victim, pid);
369 return ret < 0? ret : 1;
374 int remove_old_snapshot(struct snapshot_list *sl, int dry_run, pid_t *pid)
379 DSS_INFO_LOG("looking for snapshots belonging to intervals greater than %d\n",
380 conf.num_intervals_arg);
381 FOR_EACH_SNAPSHOT(s, i, sl) {
382 if (s->interval <= conf.num_intervals_arg)
385 printf("%s would be removed (interval = %i)\n",
386 s->name, s->interval);
389 ret = remove_snapshot(s, pid);
397 int wait_for_rm_process(pid_t pid)
399 int status, es, ret = wait_for_process(pid, &status);
402 if (!WIFEXITED(status)) {
403 ret = E_INVOLUNTARY_EXIT;
404 make_err_msg("rm process %d died involuntary", (int)pid);
407 es = WEXITSTATUS(status);
409 ret = -E_BAD_EXIT_CODE;
410 make_err_msg("rm process %d returned %d", (int)pid, es);
416 int com_prune(int argc, char * const * argv)
418 int ret, dry_run = 0;
419 struct snapshot_list sl;
423 make_err_msg("too many arguments");
427 if (strcmp(argv[1], "-d")) {
428 make_err_msg("%s", argv[1]);
434 get_snapshot_list(&sl);
435 ret = remove_old_snapshot(&sl, dry_run, &pid);
436 free_snapshot_list(&sl);
441 ret = wait_for_rm_process(pid);
446 get_snapshot_list(&sl);
447 ret = remove_redundant_snapshot(&sl, dry_run, &pid);
448 free_snapshot_list(&sl);
453 ret = wait_for_rm_process(pid);
462 struct newest_snapshot_data {
464 int64_t newest_creation_time;
468 int get_newest_complete(const char *dirname, void *private)
470 struct newest_snapshot_data *nsd = private;
472 int ret = is_snapshot(dirname, nsd->now, &s);
476 if (s.creation_time < nsd->newest_creation_time)
478 nsd->newest_creation_time = s.creation_time;
479 free(nsd->newest_name);
480 nsd->newest_name = s.name;
484 __malloc char *name_of_newest_complete_snapshot(void)
486 struct newest_snapshot_data nsd = {
487 .now = get_current_time(),
488 .newest_creation_time = -1
490 for_each_subdir(get_newest_complete, &nsd);
491 return nsd.newest_name;
494 void create_rsync_argv(char ***argv, int64_t *num)
496 char *logname, *newest = name_of_newest_complete_snapshot();
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]);
506 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
507 (*argv)[i++] = make_message("--link-dest=../%s", newest);
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);
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);
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);
524 *num = get_current_time();
525 (*argv)[i++] = incomplete_name(*num);
527 for (j = 0; j < i; j++)
528 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
531 void free_rsync_argv(char **argv)
534 for (i = 0; argv[i]; i++)
539 int create_snapshot(char **argv, pid_t *pid)
541 int fds[3] = {0, 0, 0};
543 return dss_exec(pid, argv[0], argv, fds);
546 int rename_incomplete_snapshot(int64_t start)
548 char *old_name, *new_name;
551 ret = complete_name(start, get_current_time(), &new_name);
554 old_name = incomplete_name(start);
555 ret = dss_rename(old_name, new_name);
557 DSS_NOTICE_LOG("%s -> %s\n", old_name, new_name);
563 int com_create(int argc, __a_unused char * const * argv)
567 int64_t snapshot_num;
572 make_err_msg("create: no args expected, %d given", argc - 1);
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);
580 ret = wait_for_process(pid, &status);
583 if (!WIFEXITED(status)) {
584 ret = E_INVOLUNTARY_EXIT;
585 make_err_msg("rsync process %d died involuntary", (int)pid);
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);
594 ret = rename_incomplete_snapshot(snapshot_num);
596 free_rsync_argv(rsync_argv);
600 int com_ls(int argc, __a_unused char * const * argv)
603 struct snapshot_list sl;
607 make_err_msg("ls: no args expected, %d given", argc - 1);
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);
617 /* TODO: Unlink pid file */
618 __noreturn void clean_exit(int status)
625 __printf_2_3 void dss_log(int ll, const char* fmt,...)
628 if (ll < conf.loglevel_arg)
631 vfprintf(stderr, fmt, argp);
635 int read_config_file(void)
641 if (conf.config_file_given)
642 config_file = dss_strdup(conf.config_file_arg);
644 char *home = get_homedir();
645 config_file = make_message("%s/.dssrc", home);
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);
655 struct cmdline_parser_params params = {
661 cmdline_parser_config_file(config_file, &conf, ¶ms);
663 if (!conf.source_dir_given || !conf.dest_dir_given) {
665 make_err_msg("you need to specify both source_dir and dest_dir");
674 int check_config(void)
676 if (conf.unit_interval_arg <= 0) {
677 make_err_msg("bad unit interval: %i", conf.unit_interval_arg);
678 return -E_INVALID_NUMBER;
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;
685 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
689 int main(int argc, char **argv)
693 cmdline_parser(argc, argv, &conf); /* aborts on errors */
694 if (!conf.inputs_num) {
696 make_err_msg("no command given");
699 ret = read_config_file();
702 ret = check_config();
705 ret = dss_chdir(conf.dest_dir_arg);
708 for (i = 0; command_list[i].name; i++) {
709 if (strcmp(command_list[i].name, conf.inputs[0]))
711 ret = command_list[i].handler(conf.inputs_num, conf.inputs);
714 ret = -E_INVALID_COMMAND;
715 make_err_msg("%s", conf.inputs[0]);
718 log_err_msg(EMERG, -ret);
719 clean_exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);