2 * Copyright (C) 2008-2010 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
16 #include "gcc-compat.h"
24 * Wrapper for isdigit.
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.
35 #define dss_isdigit(c) isdigit((int)(unsigned char)(c))
39 * Return the desired number of snapshots of an interval.
41 unsigned desired_number_of_snapshots(int interval_num, int num_intervals)
45 assert(interval_num >= 0);
47 if (interval_num >= num_intervals)
49 n = num_intervals - interval_num - 1;
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,
58 char *dash, *dot, *tmp;
62 dash = strchr(dirname, '-');
63 if (!dash || !dash[1] || dash == dirname)
65 for (i = 0; dirname[i] != '-'; i++)
66 if (!dss_isdigit(dirname[i]))
68 tmp = dss_strdup(dirname);
70 ret = dss_atoi64(tmp, &num);
77 s->creation_time = num;
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 */
85 if (!strcmp(dash + 1, "incomplete.being_deleted")) {
86 s->completion_time = -1;
87 s->flags = SS_BEING_DELETED; /* not complete, being deleted */
91 dot = strchr(tmp, '.');
92 if (!dot || !dot[1] || dot == tmp)
94 for (i = 0; tmp[i] != '.'; i++)
95 if (!dss_isdigit(tmp[i]))
97 tmp = dss_strdup(dash + 1);
99 ret = dss_atoi64(tmp, &num);
103 if (num > now || num < s->creation_time)
105 s->completion_time = num;
106 s->flags = SS_COMPLETE;
107 if (!strcmp(dot + 1, "being_deleted"))
108 s->flags |= SS_BEING_DELETED;
110 s->name = dss_strdup(dirname);
114 struct add_snapshot_data {
117 struct snapshot_list *sl;
120 /** Compute the minimum of \a a and \a b. */
121 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
123 static int add_snapshot(const char *dirname, void *private)
125 struct add_snapshot_data *asd = private;
126 struct snapshot_list *sl = asd->sl;
128 int ret = is_snapshot(dirname, sl->now, asd->unit_interval, &s);
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 *));
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)]++;
144 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
146 static int compare_snapshots(const void *a, const void *b)
148 struct snapshot *s1 = *(struct snapshot * const *)a;
149 struct snapshot *s2 = *(struct snapshot * const *)b;
150 return NUM_COMPARE(s2->creation_time, s1->creation_time);
154 void get_snapshot_list(struct snapshot_list *sl, int unit_interval,
157 struct add_snapshot_data asd;
158 asd.unit_interval = unit_interval;
159 asd.num_intervals = num_intervals;
161 sl->now = get_current_time();
162 sl->num_snapshots = 0;
164 sl->snapshots = NULL;
165 sl->interval_count = dss_calloc((num_intervals + 1) * sizeof(unsigned));
166 for_each_subdir(add_snapshot, &asd);
167 qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
171 void free_snapshot_list(struct snapshot_list *sl)
176 FOR_EACH_SNAPSHOT(s, i, sl) {
180 free(sl->interval_count);
181 sl->interval_count = NULL;
183 sl->snapshots = NULL;
184 sl->num_snapshots = 0;
187 __malloc char *incomplete_name(int64_t start)
189 return make_message("%" PRId64 "-incomplete", start);
192 __malloc char *being_deleted_name(struct snapshot *s)
194 if (s->flags & SS_COMPLETE)
195 return make_message("%" PRId64 "-%" PRId64 ".being_deleted",
196 s->creation_time, s->completion_time);
197 return make_message("%" PRId64 "-incomplete.being_deleted", s->creation_time);
200 int complete_name(int64_t start, int64_t end, char **result)
202 struct tm start_tm, end_tm;
203 time_t *start_seconds = (time_t *) (uint64_t *)&start; /* STFU, gcc */
204 time_t *end_seconds = (time_t *) (uint64_t *)&end; /* STFU, gcc */
205 char start_str[200], end_str[200];
207 if (!localtime_r(start_seconds, &start_tm))
209 if (!localtime_r(end_seconds, &end_tm))
211 if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm))
213 if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm))
215 *result = make_message("%" PRId64 "-%" PRId64 ".%s-%s", start, end,
220 __malloc char *name_of_newest_complete_snapshot(struct snapshot_list *sl)
226 FOR_EACH_SNAPSHOT_REVERSE(s, i, sl) {
227 if (s->flags != SS_COMPLETE) /* incomplete or being deleted */
229 name = dss_strdup(s->name);
235 int num_complete_snapshots(struct snapshot_list *sl)
240 FOR_EACH_SNAPSHOT(s, i, sl)
241 if (s->flags & SS_COMPLETE)