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