- unsigned n;
-
- assert(interval >= 0);
-
- if (interval >= conf.num_intervals_arg)
- return 0;
- n = conf.num_intervals_arg - interval - 1;
- return 1 << n;
-}
-
-/* return: Whether dirname is a snapshot directory (0: no, 1: yes) */
-int is_snapshot(const char *dirname, int64_t now, struct snapshot *s)
-{
- int i, ret;
- char *dash, *dot, *tmp;
- int64_t num;
-
- assert(dirname);
- dash = strchr(dirname, '-');
- if (!dash || !dash[1] || dash == dirname)
- return 0;
- for (i = 0; dirname[i] != '-'; i++)
- if (!isdigit(dirname[i]))
- return 0;
- tmp = dss_strdup(dirname);
- tmp[i] = '\0';
- ret = dss_atoi64(tmp, &num);
- free(tmp);
- if (ret < 0) {
- free(dss_error_txt);
- return 0;
- }
- assert(num >= 0);
- if (num > now)
- return 0;
- s->creation_time = num;
- //DSS_DEBUG_LOG("%s start time: %lli\n", dirname, (long long)s->creation_time);
- s->interval = (long long) ((now - s->creation_time)
- / conf.unit_interval_arg / 24 / 3600);
- if (!strcmp(dash + 1, "incomplete")) {
- s->completion_time = -1;
- s->flags = 0; /* neither complete, nor being deleted */
- goto success;
- }
- if (!strcmp(dash + 1, "incomplete.being_deleted")) {
- s->completion_time = -1;
- s->flags = SS_BEING_DELETED; /* mot cpmplete, being deleted */
- goto success;
- }
- tmp = dash + 1;
- dot = strchr(tmp, '.');
- if (!dot || !dot[1] || dot == tmp)
- return 0;
- for (i = 0; tmp[i] != '.'; i++)
- if (!isdigit(tmp[i]))
- return 0;
- tmp = dss_strdup(dash + 1);
- tmp[i] = '\0';
- ret = dss_atoi64(tmp, &num);
- free(tmp);
- if (ret < 0) {
- free(dss_error_txt);
- return 0;
- }
- if (num > now)
- return 0;
- s->completion_time = num;
- s->flags = SS_COMPLETE;
- if (!strcmp(dot + 1, "being_deleted"))
- s->flags |= SS_BEING_DELETED;
-success:
- s->name = dss_strdup(dirname);
- return 1;
-}
-
-int64_t get_current_time(void)
-{
- time_t now;
- time(&now);
- DSS_DEBUG_LOG("now: %lli\n", (long long) now);
- return (int64_t)now;
-}
-
-char *incomplete_name(int64_t start)
-{
- return make_message("%lli-incomplete", (long long)start);
-}
-
-char *being_deleted_name(struct snapshot *s)
-{
- if (s->flags & SS_COMPLETE)
- return make_message("%lli-%lli.being_deleted",
- (long long)s->creation_time,
- (long long)s->completion_time);
- return make_message("%lli-incomplete.being_deleted",
- (long long)s->creation_time);
-}
-
-int complete_name(int64_t start, int64_t end, char **result)
-{
- struct tm start_tm, end_tm;
- time_t *start_seconds = (time_t *) (uint64_t *)&start; /* STFU, gcc */
- time_t *end_seconds = (time_t *) (uint64_t *)&end; /* STFU, gcc */
- char start_str[200], end_str[200];
-
- if (!localtime_r(start_seconds, &start_tm))
- return -E_LOCALTIME;
- if (!localtime_r(end_seconds, &end_tm))
- return -E_LOCALTIME;
- if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm))
- return -E_STRFTIME;
- if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm))
- return -E_STRFTIME;
- *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
- start_str, end_str);
- return 1;
-}
-
-struct snapshot_list {
- int64_t now;
- unsigned num_snapshots;
- unsigned array_size;
- struct snapshot **snapshots;
- /**
- * Array of size num_intervals + 1
- *
- * It contains the number of snapshots in each interval. interval_count[num_intervals]
- * is the number of snapshots which belong to any interval greater than num_intervals.
- */
- unsigned *interval_count;
-};
-
-#define FOR_EACH_SNAPSHOT(s, i, sl) \
- for ((i) = 0; (i) < (sl)->num_snapshots && ((s) = (sl)->snapshots[(i)]); (i)++)
-
-#define FOR_EACH_SNAPSHOT_REVERSE(s, i, sl) \
- for ((i) = (sl)->num_snapshots; (i) > 0 && ((s) = (sl)->snapshots[(i - 1)]); (i)--)
-
-static inline struct snapshot *oldest_snapshot(struct snapshot_list *sl)
-{
- if (!sl->num_snapshots)
- return NULL;
- return sl->snapshots[0];
-}
-
-#define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
-
-static int compare_snapshots(const void *a, const void *b)
-{
- struct snapshot *s1 = *(struct snapshot **)a;
- struct snapshot *s2 = *(struct snapshot **)b;
- return NUM_COMPARE(s2->creation_time, s1->creation_time);
-}
-
-/** Compute the minimum of \a a and \a b. */
-#define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
-
-int add_snapshot(const char *dirname, void *private)
-{
- struct snapshot_list *sl = private;
- struct snapshot s;
- int ret = is_snapshot(dirname, sl->now, &s);
-
- if (!ret)
- return 1;
- if (sl->num_snapshots >= sl->array_size) {
- sl->array_size = 2 * sl->array_size + 1;
- sl->snapshots = dss_realloc(sl->snapshots,
- sl->array_size * sizeof(struct snapshot *));
- }
- sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
- *(sl->snapshots[sl->num_snapshots]) = s;
- sl->interval_count[DSS_MIN(s.interval, conf.num_intervals_arg)]++;
- sl->num_snapshots++;
- return 1;
-}
-
-void get_snapshot_list(struct snapshot_list *sl)
-{
- sl->now = get_current_time();
- sl->num_snapshots = 0;
- sl->array_size = 0;
- sl->snapshots = NULL;
- sl->interval_count = dss_calloc((conf.num_intervals_arg + 1) * sizeof(unsigned));
- for_each_subdir(add_snapshot, sl);
- qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
- compare_snapshots);
-}
-
-void free_snapshot_list(struct snapshot_list *sl)
-{
- int i;
- struct snapshot *s;
-
- FOR_EACH_SNAPSHOT(s, i, sl) {
- free(s->name);
- free(s);
- }
- free(sl->interval_count);
- sl->interval_count = NULL;
- free(sl->snapshots);
- sl->snapshots = NULL;
- sl->num_snapshots = 0;
-}
-
-void stop_rsync_process(void)
-{
- if (!rsync_pid || rsync_stopped)
- return;
- kill(SIGSTOP, rsync_pid);
- rsync_stopped = 1;
-}