1 /* SPDX-License-Identifier: GPL-2.0 */
12 #include "gcc-compat.h"
20 * Wrapper for isdigit.
23 * The values should be cast to an unsigned char first, then to int.
24 * Why? Because the isdigit (as do all other is/to functions/macros)
25 * expect a number from 0 upto and including 255 as their (int) argument.
26 * Because char is signed on most systems, casting it to int immediately
27 * gives the functions an argument between -128 and 127 (inclusive),
28 * which they will use as an array index, and which will thus fail
29 * horribly for characters which have their most significant bit set.
31 #define dss_isdigit(c) isdigit((int)(unsigned char)(c))
35 * Return the desired number of snapshots of an interval.
37 unsigned desired_number_of_snapshots(int interval_num, int num_intervals)
41 assert(interval_num >= 0);
43 if (interval_num >= num_intervals)
45 n = num_intervals - interval_num - 1;
49 /* return: Whether dirname is a snapshot directory (0: no, 1: yes) */
50 static int is_snapshot(const char *dirname, int64_t now, int unit_interval,
54 char *dash, *dot, *tmp;
58 dash = strchr(dirname, '-');
59 if (!dash || !dash[1] || dash == dirname)
61 for (i = 0; dirname[i] != '-'; i++)
62 if (!dss_isdigit(dirname[i]))
64 tmp = dss_strdup(dirname);
66 ret = dss_atoi64(tmp, &num);
73 s->creation_time = num;
74 s->interval = (long long) ((now - s->creation_time)
75 / unit_interval / 24 / 3600);
76 if (!strcmp(dash + 1, "incomplete")) {
77 s->completion_time = -1;
78 s->flags = 0; /* neither complete, nor being deleted */
81 if (!strcmp(dash + 1, "incomplete.being_deleted")) {
82 s->completion_time = -1;
83 s->flags = SS_BEING_DELETED; /* not complete, being deleted */
87 dot = strchr(tmp, '.');
88 if (!dot || !dot[1] || dot == tmp)
90 for (i = 0; tmp[i] != '.'; i++)
91 if (!dss_isdigit(tmp[i]))
93 tmp = dss_strdup(dash + 1);
95 ret = dss_atoi64(tmp, &num);
99 if (num > now || num < s->creation_time)
101 s->completion_time = num;
102 s->flags = SS_COMPLETE;
103 if (!strcmp(dot + 1, "being_deleted"))
104 s->flags |= SS_BEING_DELETED;
106 s->name = dss_strdup(dirname);
110 struct add_snapshot_data {
113 struct snapshot_list *sl;
116 /** Compute the minimum of \a a and \a b. */
117 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
119 static int add_snapshot(const char *dirname, void *private)
121 struct add_snapshot_data *asd = private;
122 struct snapshot_list *sl = asd->sl;
124 int ret = is_snapshot(dirname, sl->now, asd->unit_interval, &s);
128 if (sl->num_snapshots >= sl->array_size) {
129 sl->array_size = 2 * sl->array_size + 1;
130 sl->snapshots = dss_realloc(sl->snapshots,
131 sl->array_size * sizeof(struct snapshot *));
133 sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
134 *(sl->snapshots[sl->num_snapshots]) = s;
135 sl->interval_count[DSS_MIN(s.interval, asd->num_intervals)]++;
140 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
142 static int compare_snapshots(const void *a, const void *b)
144 struct snapshot *s1 = *(struct snapshot * const *)a;
145 struct snapshot *s2 = *(struct snapshot * const *)b;
146 return NUM_COMPARE(s2->creation_time, s1->creation_time);
149 /* The returned snapshot list is sorted by creation time. */
150 void get_snapshot_list(struct snapshot_list *sl, int unit_interval,
153 struct add_snapshot_data asd;
154 asd.unit_interval = unit_interval;
155 asd.num_intervals = num_intervals;
157 sl->now = get_current_time();
158 sl->num_snapshots = 0;
160 sl->snapshots = NULL;
161 sl->interval_count = dss_calloc((num_intervals + 1) * sizeof(unsigned));
162 for_each_subdir(add_snapshot, &asd);
163 qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
167 void free_snapshot_list(struct snapshot_list *sl)
172 FOR_EACH_SNAPSHOT(s, i, sl) {
176 free(sl->interval_count);
177 sl->interval_count = NULL;
179 sl->snapshots = NULL;
180 sl->num_snapshots = 0;
183 __malloc char *incomplete_name(int64_t start)
185 return make_message("%" PRId64 "-incomplete", start);
188 __malloc char *being_deleted_name(struct snapshot *s)
190 if (s->flags & SS_COMPLETE)
191 return make_message("%" PRId64 "-%" PRId64 ".being_deleted",
192 s->creation_time, s->completion_time);
193 return make_message("%" PRId64 "-incomplete.being_deleted", s->creation_time);
196 int complete_name(int64_t start, int64_t end, char **result)
198 struct tm start_tm, end_tm;
199 time_t *start_seconds = (time_t *) (uint64_t *)&start; /* STFU, gcc */
200 time_t *end_seconds = (time_t *) (uint64_t *)&end; /* STFU, gcc */
201 char start_str[200], end_str[200];
203 if (!localtime_r(start_seconds, &start_tm))
205 if (!localtime_r(end_seconds, &end_tm))
207 if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm))
209 if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm))
211 *result = make_message("%" PRId64 "-%" PRId64 ".%s-%s", start, end,
216 __malloc char *name_of_newest_complete_snapshot(struct snapshot_list *sl)
222 FOR_EACH_SNAPSHOT_REVERSE(s, i, sl) {
223 if (s->flags != SS_COMPLETE) /* incomplete or being deleted */
225 name = dss_strdup(s->name);
231 int num_complete_snapshots(struct snapshot_list *sl)
236 FOR_EACH_SNAPSHOT(s, i, sl)
237 if (s->flags & SS_COMPLETE)