10 #include "gcc-compat.h"
18 * Return the desired number of snapshots of an interval.
20 unsigned desired_number_of_snapshots(int interval_num, int num_intervals)
24 assert(interval_num >= 0);
26 if (interval_num >= num_intervals)
28 n = num_intervals - interval_num - 1;
32 /* return: Whether dirname is a snapshot directory (0: no, 1: yes) */
33 static int is_snapshot(const char *dirname, int64_t now, int unit_interval,
37 char *dash, *dot, *tmp;
41 dash = strchr(dirname, '-');
42 if (!dash || !dash[1] || dash == dirname)
44 for (i = 0; dirname[i] != '-'; i++)
45 if (!isdigit(dirname[i]))
47 tmp = dss_strdup(dirname);
49 ret = dss_atoi64(tmp, &num);
56 s->creation_time = num;
57 //DSS_DEBUG_LOG("%s start time: %lli\n", dirname, (long long)s->creation_time);
58 s->interval = (long long) ((now - s->creation_time)
59 / unit_interval / 24 / 3600);
60 if (!strcmp(dash + 1, "incomplete")) {
61 s->completion_time = -1;
62 s->flags = 0; /* neither complete, nor being deleted */
65 if (!strcmp(dash + 1, "incomplete.being_deleted")) {
66 s->completion_time = -1;
67 s->flags = SS_BEING_DELETED; /* mot cpmplete, being deleted */
71 dot = strchr(tmp, '.');
72 if (!dot || !dot[1] || dot == tmp)
74 for (i = 0; tmp[i] != '.'; i++)
77 tmp = dss_strdup(dash + 1);
79 ret = dss_atoi64(tmp, &num);
85 s->completion_time = num;
86 s->flags = SS_COMPLETE;
87 if (!strcmp(dot + 1, "being_deleted"))
88 s->flags |= SS_BEING_DELETED;
90 s->name = dss_strdup(dirname);
94 struct add_snapshot_data {
97 struct snapshot_list *sl;
100 /** Compute the minimum of \a a and \a b. */
101 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
103 static int add_snapshot(const char *dirname, void *private)
105 struct add_snapshot_data *asd = private;
106 struct snapshot_list *sl = asd->sl;
108 int ret = is_snapshot(dirname, sl->now, asd->unit_interval, &s);
112 if (sl->num_snapshots >= sl->array_size) {
113 sl->array_size = 2 * sl->array_size + 1;
114 sl->snapshots = dss_realloc(sl->snapshots,
115 sl->array_size * sizeof(struct snapshot *));
117 sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
118 *(sl->snapshots[sl->num_snapshots]) = s;
119 sl->interval_count[DSS_MIN(s.interval, asd->num_intervals)]++;
124 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
126 static int compare_snapshots(const void *a, const void *b)
128 struct snapshot *s1 = *(struct snapshot **)a;
129 struct snapshot *s2 = *(struct snapshot **)b;
130 return NUM_COMPARE(s2->creation_time, s1->creation_time);
134 void get_snapshot_list(struct snapshot_list *sl, int unit_interval,
137 struct add_snapshot_data asd = {
138 .unit_interval = unit_interval,
139 .num_intervals = num_intervals,
142 sl->now = get_current_time();
143 sl->num_snapshots = 0;
145 sl->snapshots = NULL;
146 sl->interval_count = dss_calloc((num_intervals + 1) * sizeof(unsigned));
147 for_each_subdir(add_snapshot, &asd);
148 qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
152 void free_snapshot_list(struct snapshot_list *sl)
157 FOR_EACH_SNAPSHOT(s, i, sl) {
161 free(sl->interval_count);
162 sl->interval_count = NULL;
164 sl->snapshots = NULL;
165 sl->num_snapshots = 0;
168 __malloc char *incomplete_name(int64_t start)
170 return make_message("%lli-incomplete", (long long)start);
173 __malloc char *being_deleted_name(struct snapshot *s)
175 if (s->flags & SS_COMPLETE)
176 return make_message("%lli-%lli.being_deleted",
177 (long long)s->creation_time,
178 (long long)s->completion_time);
179 return make_message("%lli-incomplete.being_deleted",
180 (long long)s->creation_time);
183 int complete_name(int64_t start, int64_t end, char **result)
185 struct tm start_tm, end_tm;
186 time_t *start_seconds = (time_t *) (uint64_t *)&start; /* STFU, gcc */
187 time_t *end_seconds = (time_t *) (uint64_t *)&end; /* STFU, gcc */
188 char start_str[200], end_str[200];
190 if (!localtime_r(start_seconds, &start_tm))
192 if (!localtime_r(end_seconds, &end_tm))
194 if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm))
196 if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm))
198 *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
203 __malloc char *name_of_newest_complete_snapshot(struct snapshot_list *sl)
209 FOR_EACH_SNAPSHOT_REVERSE(s, i, sl) {
210 if (s->flags != SS_COMPLETE) /* incomplete or being deleted */
212 name = dss_strdup(s->name);