use C99-compliant format strings for consistency reasons
[dss.git] / snap.c
1 /*
2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6 #include <stdlib.h>
7 #include <assert.h>
8 #include <inttypes.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <stdio.h>
12 #include <errno.h>
13 #include <time.h>
14 #include <sys/time.h>
15
16 #include "gcc-compat.h"
17 #include "error.h"
18 #include "snap.h"
19 #include "string.h"
20 #include "time.h"
21 #include "fd.h"
22
23 /**
24 * Wrapper for isdigit.
25 * NetBSD needs this.
26 *
27 * The values should be cast to an unsigned char first, then to int.
28 * Why? Because the isdigit (as do all other is/to functions/macros)
29 * expect a number from 0 upto and including 255 as their (int) argument.
30 * Because char is signed on most systems, casting it to int immediately
31 * gives the functions an argument between -128 and 127 (inclusive),
32 * which they will use as an array index, and which will thus fail
33 * horribly for characters which have their most significant bit set.
34 */
35 #define dss_isdigit(c) isdigit((int)(unsigned char)(c))
36
37
38 /**
39 * Return the desired number of snapshots of an interval.
40 */
41 unsigned desired_number_of_snapshots(int interval_num, int num_intervals)
42 {
43 unsigned n;
44
45 assert(interval_num >= 0);
46
47 if (interval_num >= num_intervals)
48 return 0;
49 n = num_intervals - interval_num - 1;
50 return 1 << n;
51 }
52
53 /* return: Whether dirname is a snapshot directory (0: no, 1: yes) */
54 static int is_snapshot(const char *dirname, int64_t now, int unit_interval,
55 struct snapshot *s)
56 {
57 int i, ret;
58 char *dash, *dot, *tmp;
59 int64_t num;
60
61 assert(dirname);
62 dash = strchr(dirname, '-');
63 if (!dash || !dash[1] || dash == dirname)
64 return 0;
65 for (i = 0; dirname[i] != '-'; i++)
66 if (!dss_isdigit(dirname[i]))
67 return 0;
68 tmp = dss_strdup(dirname);
69 tmp[i] = '\0';
70 ret = dss_atoi64(tmp, &num);
71 free(tmp);
72 if (ret < 0)
73 return 0;
74 assert(num >= 0);
75 if (num > now)
76 return 0;
77 s->creation_time = num;
78 //DSS_DEBUG_LOG("%s start time: %lli\n", dirname, (long long)s->creation_time);
79 s->interval = (long long) ((now - s->creation_time)
80 / unit_interval / 24 / 3600);
81 if (!strcmp(dash + 1, "incomplete")) {
82 s->completion_time = -1;
83 s->flags = 0; /* neither complete, nor being deleted */
84 goto success;
85 }
86 if (!strcmp(dash + 1, "incomplete.being_deleted")) {
87 s->completion_time = -1;
88 s->flags = SS_BEING_DELETED; /* mot cpmplete, being deleted */
89 goto success;
90 }
91 tmp = dash + 1;
92 dot = strchr(tmp, '.');
93 if (!dot || !dot[1] || dot == tmp)
94 return 0;
95 for (i = 0; tmp[i] != '.'; i++)
96 if (!dss_isdigit(tmp[i]))
97 return 0;
98 tmp = dss_strdup(dash + 1);
99 tmp[i] = '\0';
100 ret = dss_atoi64(tmp, &num);
101 free(tmp);
102 if (ret < 0)
103 return 0;
104 if (num > now)
105 return 0;
106 s->completion_time = num;
107 s->flags = SS_COMPLETE;
108 if (!strcmp(dot + 1, "being_deleted"))
109 s->flags |= SS_BEING_DELETED;
110 success:
111 s->name = dss_strdup(dirname);
112 return 1;
113 }
114
115 struct add_snapshot_data {
116 int unit_interval;
117 int num_intervals;
118 struct snapshot_list *sl;
119 };
120
121 /** Compute the minimum of \a a and \a b. */
122 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
123
124 static int add_snapshot(const char *dirname, void *private)
125 {
126 struct add_snapshot_data *asd = private;
127 struct snapshot_list *sl = asd->sl;
128 struct snapshot s;
129 int ret = is_snapshot(dirname, sl->now, asd->unit_interval, &s);
130
131 if (!ret)
132 return 1;
133 if (sl->num_snapshots >= sl->array_size) {
134 sl->array_size = 2 * sl->array_size + 1;
135 sl->snapshots = dss_realloc(sl->snapshots,
136 sl->array_size * sizeof(struct snapshot *));
137 }
138 sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
139 *(sl->snapshots[sl->num_snapshots]) = s;
140 sl->interval_count[DSS_MIN(s.interval, asd->num_intervals)]++;
141 sl->num_snapshots++;
142 return 1;
143 }
144
145 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
146
147 static int compare_snapshots(const void *a, const void *b)
148 {
149 struct snapshot *s1 = *(struct snapshot **)a;
150 struct snapshot *s2 = *(struct snapshot **)b;
151 return NUM_COMPARE(s2->creation_time, s1->creation_time);
152 }
153
154
155 void get_snapshot_list(struct snapshot_list *sl, int unit_interval,
156 int num_intervals)
157 {
158 struct add_snapshot_data asd = {
159 .unit_interval = unit_interval,
160 .num_intervals = num_intervals,
161 .sl = sl
162 };
163 sl->now = get_current_time();
164 sl->num_snapshots = 0;
165 sl->array_size = 0;
166 sl->snapshots = NULL;
167 sl->interval_count = dss_calloc((num_intervals + 1) * sizeof(unsigned));
168 for_each_subdir(add_snapshot, &asd);
169 qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
170 compare_snapshots);
171 }
172
173 void free_snapshot_list(struct snapshot_list *sl)
174 {
175 int i;
176 struct snapshot *s;
177
178 FOR_EACH_SNAPSHOT(s, i, sl) {
179 free(s->name);
180 free(s);
181 }
182 free(sl->interval_count);
183 sl->interval_count = NULL;
184 free(sl->snapshots);
185 sl->snapshots = NULL;
186 sl->num_snapshots = 0;
187 }
188
189 __malloc char *incomplete_name(int64_t start)
190 {
191 return make_message("%" PRId64 "-incomplete", start);
192 }
193
194 __malloc char *being_deleted_name(struct snapshot *s)
195 {
196 if (s->flags & SS_COMPLETE)
197 return make_message("%" PRId64 "-%" PRId64 ".being_deleted",
198 s->creation_time, s->completion_time);
199 return make_message("%" PRId64 "-incomplete.being_deleted", s->creation_time);
200 }
201
202 int complete_name(int64_t start, int64_t end, char **result)
203 {
204 struct tm start_tm, end_tm;
205 time_t *start_seconds = (time_t *) (uint64_t *)&start; /* STFU, gcc */
206 time_t *end_seconds = (time_t *) (uint64_t *)&end; /* STFU, gcc */
207 char start_str[200], end_str[200];
208
209 if (!localtime_r(start_seconds, &start_tm))
210 return -E_LOCALTIME;
211 if (!localtime_r(end_seconds, &end_tm))
212 return -E_LOCALTIME;
213 if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm))
214 return -E_STRFTIME;
215 if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm))
216 return -E_STRFTIME;
217 *result = make_message("%" PRId64 "-%" PRId64 ".%s-%s", start, end,
218 start_str, end_str);
219 return 1;
220 }
221
222 __malloc char *name_of_newest_complete_snapshot(struct snapshot_list *sl)
223 {
224 struct snapshot *s;
225 int i;
226 char *name = NULL;
227
228 FOR_EACH_SNAPSHOT_REVERSE(s, i, sl) {
229 if (s->flags != SS_COMPLETE) /* incomplete or being deleted */
230 continue;
231 name = dss_strdup(s->name);
232 break;
233 }
234 return name;
235 }
236