Loglevel adjustments.
[dss.git] / snap.c
1 #include <stdlib.h>
2 #include <assert.h>
3 #include <inttypes.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <stdio.h>
7 #include <errno.h>
8 #include <time.h>
9
10 #include "gcc-compat.h"
11 #include "error.h"
12 #include "snap.h"
13 #include "string.h"
14 #include "time.h"
15 #include "fd.h"
16
17 /**
18 * Return the desired number of snapshots of an interval.
19 */
20 unsigned desired_number_of_snapshots(int interval_num, int num_intervals)
21 {
22 unsigned n;
23
24 assert(interval_num >= 0);
25
26 if (interval_num >= num_intervals)
27 return 0;
28 n = num_intervals - interval_num - 1;
29 return 1 << n;
30 }
31
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,
34 struct snapshot *s)
35 {
36 int i, ret;
37 char *dash, *dot, *tmp;
38 int64_t num;
39
40 assert(dirname);
41 dash = strchr(dirname, '-');
42 if (!dash || !dash[1] || dash == dirname)
43 return 0;
44 for (i = 0; dirname[i] != '-'; i++)
45 if (!isdigit(dirname[i]))
46 return 0;
47 tmp = dss_strdup(dirname);
48 tmp[i] = '\0';
49 ret = dss_atoi64(tmp, &num);
50 free(tmp);
51 if (ret < 0)
52 return 0;
53 assert(num >= 0);
54 if (num > now)
55 return 0;
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 */
63 goto success;
64 }
65 if (!strcmp(dash + 1, "incomplete.being_deleted")) {
66 s->completion_time = -1;
67 s->flags = SS_BEING_DELETED; /* mot cpmplete, being deleted */
68 goto success;
69 }
70 tmp = dash + 1;
71 dot = strchr(tmp, '.');
72 if (!dot || !dot[1] || dot == tmp)
73 return 0;
74 for (i = 0; tmp[i] != '.'; i++)
75 if (!isdigit(tmp[i]))
76 return 0;
77 tmp = dss_strdup(dash + 1);
78 tmp[i] = '\0';
79 ret = dss_atoi64(tmp, &num);
80 free(tmp);
81 if (ret < 0)
82 return 0;
83 if (num > now)
84 return 0;
85 s->completion_time = num;
86 s->flags = SS_COMPLETE;
87 if (!strcmp(dot + 1, "being_deleted"))
88 s->flags |= SS_BEING_DELETED;
89 success:
90 s->name = dss_strdup(dirname);
91 return 1;
92 }
93
94 struct add_snapshot_data {
95 int unit_interval;
96 int num_intervals;
97 struct snapshot_list *sl;
98 };
99
100 /** Compute the minimum of \a a and \a b. */
101 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
102
103 static int add_snapshot(const char *dirname, void *private)
104 {
105 struct add_snapshot_data *asd = private;
106 struct snapshot_list *sl = asd->sl;
107 struct snapshot s;
108 int ret = is_snapshot(dirname, sl->now, asd->unit_interval, &s);
109
110 if (!ret)
111 return 1;
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 *));
116 }
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)]++;
120 sl->num_snapshots++;
121 return 1;
122 }
123
124 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
125
126 static int compare_snapshots(const void *a, const void *b)
127 {
128 struct snapshot *s1 = *(struct snapshot **)a;
129 struct snapshot *s2 = *(struct snapshot **)b;
130 return NUM_COMPARE(s2->creation_time, s1->creation_time);
131 }
132
133
134 void get_snapshot_list(struct snapshot_list *sl, int unit_interval,
135 int num_intervals)
136 {
137 struct add_snapshot_data asd = {
138 .unit_interval = unit_interval,
139 .num_intervals = num_intervals,
140 .sl = sl
141 };
142 sl->now = get_current_time();
143 sl->num_snapshots = 0;
144 sl->array_size = 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 *),
149 compare_snapshots);
150 }
151
152 void free_snapshot_list(struct snapshot_list *sl)
153 {
154 int i;
155 struct snapshot *s;
156
157 FOR_EACH_SNAPSHOT(s, i, sl) {
158 free(s->name);
159 free(s);
160 }
161 free(sl->interval_count);
162 sl->interval_count = NULL;
163 free(sl->snapshots);
164 sl->snapshots = NULL;
165 sl->num_snapshots = 0;
166 }
167
168 __malloc char *incomplete_name(int64_t start)
169 {
170 return make_message("%lli-incomplete", (long long)start);
171 }
172
173 __malloc char *being_deleted_name(struct snapshot *s)
174 {
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);
181 }
182
183 int complete_name(int64_t start, int64_t end, char **result)
184 {
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];
189
190 if (!localtime_r(start_seconds, &start_tm))
191 return -E_LOCALTIME;
192 if (!localtime_r(end_seconds, &end_tm))
193 return -E_LOCALTIME;
194 if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm))
195 return -E_STRFTIME;
196 if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm))
197 return -E_STRFTIME;
198 *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
199 start_str, end_str);
200 return 1;
201 }
202
203 __malloc char *name_of_newest_complete_snapshot(struct snapshot_list *sl)
204 {
205 struct snapshot *s;
206 int i;
207 char *name = NULL;
208
209 FOR_EACH_SNAPSHOT_REVERSE(s, i, sl) {
210 if (s->flags != SS_COMPLETE) /* incomplete or being deleted */
211 continue;
212 name = dss_strdup(s->name);
213 break;
214 }
215 return name;
216 }
217