8de76d459da1a8844a452fa576d706014b229be9
[dss.git] / snap.c
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <inttypes.h>
5 #include <string.h>
6 #include <ctype.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include <time.h>
10 #include <sys/time.h>
11
12 #include "gcc-compat.h"
13 #include "err.h"
14 #include "snap.h"
15 #include "str.h"
16 #include "tv.h"
17 #include "file.h"
18
19 /**
20  * Wrapper for isdigit.
21  * NetBSD needs this.
22  *
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.
30  */
31 #define dss_isdigit(c) isdigit((int)(unsigned char)(c))
32
33
34 /**
35  * Return the desired number of snapshots of an interval.
36  */
37 unsigned desired_number_of_snapshots(int interval_num, int num_intervals)
38 {
39         unsigned n;
40
41         assert(interval_num >= 0);
42
43         if (interval_num >= num_intervals)
44                 return 0;
45         n = num_intervals - interval_num - 1;
46         return 1 << n;
47 }
48
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,
51                 struct snapshot *s)
52 {
53         int i, ret;
54         char *dash, *dot, *tmp;
55         int64_t num;
56
57         assert(dirname);
58         dash = strchr(dirname, '-');
59         if (!dash || !dash[1] || dash == dirname)
60                 return 0;
61         for (i = 0; dirname[i] != '-'; i++)
62                 if (!dss_isdigit(dirname[i]))
63                         return 0;
64         tmp = dss_strdup(dirname);
65         tmp[i] = '\0';
66         ret = dss_atoi64(tmp, &num);
67         free(tmp);
68         if (ret < 0)
69                 return 0;
70         assert(num >= 0);
71         if (num > now)
72                 return 0;
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 */
79                 goto success;
80         }
81         if (!strcmp(dash + 1, "incomplete.being_deleted")) {
82                 s->completion_time = -1;
83                 s->flags = SS_BEING_DELETED; /* not complete, being deleted */
84                 goto success;
85         }
86         tmp = dash + 1;
87         dot = strchr(tmp, '.');
88         if (!dot || !dot[1] || dot == tmp)
89                 return 0;
90         for (i = 0; tmp[i] != '.'; i++)
91                 if (!dss_isdigit(tmp[i]))
92                         return 0;
93         tmp = dss_strdup(dash + 1);
94         tmp[i] = '\0';
95         ret = dss_atoi64(tmp, &num);
96         free(tmp);
97         if (ret < 0)
98                 return 0;
99         if (num > now || num < s->creation_time)
100                 return 0;
101         s->completion_time = num;
102         s->flags = SS_COMPLETE;
103         if (!strcmp(dot + 1, "being_deleted"))
104                 s->flags |= SS_BEING_DELETED;
105 success:
106         s->name = dss_strdup(dirname);
107         return 1;
108 }
109
110 struct add_snapshot_data {
111         int unit_interval;
112         int num_intervals;
113         struct snapshot_list *sl;
114 };
115
116 /** Compute the minimum of \a a and \a b. */
117 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
118
119 static int add_snapshot(const char *dirname, void *private)
120 {
121         struct add_snapshot_data *asd = private;
122         struct snapshot_list *sl = asd->sl;
123         struct snapshot s;
124         int ret = is_snapshot(dirname, sl->now, asd->unit_interval, &s);
125
126         if (!ret)
127                 return 1;
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 *));
132         }
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)]++;
136         sl->num_snapshots++;
137         return 1;
138 }
139
140 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
141
142 static int compare_snapshots(const void *a, const void *b)
143 {
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);
147 }
148
149
150 void get_snapshot_list(struct snapshot_list *sl, int unit_interval,
151                 int num_intervals)
152 {
153         struct add_snapshot_data asd;
154         asd.unit_interval = unit_interval;
155         asd.num_intervals = num_intervals;
156         asd.sl = sl;
157         sl->now = get_current_time();
158         sl->num_snapshots = 0;
159         sl->array_size = 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 *),
164                 compare_snapshots);
165 }
166
167 void free_snapshot_list(struct snapshot_list *sl)
168 {
169         int i;
170         struct snapshot *s;
171
172         FOR_EACH_SNAPSHOT(s, i, sl) {
173                 free(s->name);
174                 free(s);
175         }
176         free(sl->interval_count);
177         sl->interval_count = NULL;
178         free(sl->snapshots);
179         sl->snapshots = NULL;
180         sl->num_snapshots = 0;
181 }
182
183 __malloc char *incomplete_name(int64_t start)
184 {
185         return make_message("%" PRId64 "-incomplete", start);
186 }
187
188 __malloc char *being_deleted_name(struct snapshot *s)
189 {
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);
194 }
195
196 int complete_name(int64_t start, int64_t end, char **result)
197 {
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];
202
203         if (!localtime_r(start_seconds, &start_tm))
204                 return -E_LOCALTIME;
205         if (!localtime_r(end_seconds, &end_tm))
206                 return -E_LOCALTIME;
207         if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm))
208                 return -E_STRFTIME;
209         if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm))
210                 return -E_STRFTIME;
211         *result = make_message("%" PRId64 "-%" PRId64 ".%s-%s", start, end,
212                 start_str, end_str);
213         return 1;
214 }
215
216 __malloc char *name_of_newest_complete_snapshot(struct snapshot_list *sl)
217 {
218         struct snapshot *s;
219         int i;
220         char *name = NULL;
221
222         FOR_EACH_SNAPSHOT_REVERSE(s, i, sl) {
223                 if (s->flags != SS_COMPLETE) /* incomplete or being deleted */
224                         continue;
225                 name = dss_strdup(s->name);
226                 break;
227         }
228         return name;
229 }
230
231 int num_complete_snapshots(struct snapshot_list *sl)
232 {
233         struct snapshot *s;
234         int i, ret = 0;
235
236         FOR_EACH_SNAPSHOT(s, i, sl)
237                 if (s->flags & SS_COMPLETE)
238                         ret++;
239         return ret;
240 }