2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
15 #include "gcc-compat.h"
23 * Return the desired number of snapshots of an interval.
25 unsigned desired_number_of_snapshots(int interval_num, int num_intervals)
29 assert(interval_num >= 0);
31 if (interval_num >= num_intervals)
33 n = num_intervals - interval_num - 1;
37 /* return: Whether dirname is a snapshot directory (0: no, 1: yes) */
38 static int is_snapshot(const char *dirname, int64_t now, int unit_interval,
42 char *dash, *dot, *tmp;
46 dash = strchr(dirname, '-');
47 if (!dash || !dash[1] || dash == dirname)
49 for (i = 0; dirname[i] != '-'; i++)
50 if (!isdigit(dirname[i]))
52 tmp = dss_strdup(dirname);
54 ret = dss_atoi64(tmp, &num);
61 s->creation_time = num;
62 //DSS_DEBUG_LOG("%s start time: %lli\n", dirname, (long long)s->creation_time);
63 s->interval = (long long) ((now - s->creation_time)
64 / unit_interval / 24 / 3600);
65 if (!strcmp(dash + 1, "incomplete")) {
66 s->completion_time = -1;
67 s->flags = 0; /* neither complete, nor being deleted */
70 if (!strcmp(dash + 1, "incomplete.being_deleted")) {
71 s->completion_time = -1;
72 s->flags = SS_BEING_DELETED; /* mot cpmplete, being deleted */
76 dot = strchr(tmp, '.');
77 if (!dot || !dot[1] || dot == tmp)
79 for (i = 0; tmp[i] != '.'; i++)
82 tmp = dss_strdup(dash + 1);
84 ret = dss_atoi64(tmp, &num);
90 s->completion_time = num;
91 s->flags = SS_COMPLETE;
92 if (!strcmp(dot + 1, "being_deleted"))
93 s->flags |= SS_BEING_DELETED;
95 s->name = dss_strdup(dirname);
99 struct add_snapshot_data {
102 struct snapshot_list *sl;
105 /** Compute the minimum of \a a and \a b. */
106 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
108 static int add_snapshot(const char *dirname, void *private)
110 struct add_snapshot_data *asd = private;
111 struct snapshot_list *sl = asd->sl;
113 int ret = is_snapshot(dirname, sl->now, asd->unit_interval, &s);
117 if (sl->num_snapshots >= sl->array_size) {
118 sl->array_size = 2 * sl->array_size + 1;
119 sl->snapshots = dss_realloc(sl->snapshots,
120 sl->array_size * sizeof(struct snapshot *));
122 sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
123 *(sl->snapshots[sl->num_snapshots]) = s;
124 sl->interval_count[DSS_MIN(s.interval, asd->num_intervals)]++;
129 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
131 static int compare_snapshots(const void *a, const void *b)
133 struct snapshot *s1 = *(struct snapshot **)a;
134 struct snapshot *s2 = *(struct snapshot **)b;
135 return NUM_COMPARE(s2->creation_time, s1->creation_time);
139 void get_snapshot_list(struct snapshot_list *sl, int unit_interval,
142 struct add_snapshot_data asd = {
143 .unit_interval = unit_interval,
144 .num_intervals = num_intervals,
147 sl->now = get_current_time();
148 sl->num_snapshots = 0;
150 sl->snapshots = NULL;
151 sl->interval_count = dss_calloc((num_intervals + 1) * sizeof(unsigned));
152 for_each_subdir(add_snapshot, &asd);
153 qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
157 void free_snapshot_list(struct snapshot_list *sl)
162 FOR_EACH_SNAPSHOT(s, i, sl) {
166 free(sl->interval_count);
167 sl->interval_count = NULL;
169 sl->snapshots = NULL;
170 sl->num_snapshots = 0;
173 __malloc char *incomplete_name(int64_t start)
175 return make_message("%lli-incomplete", (long long)start);
178 __malloc char *being_deleted_name(struct snapshot *s)
180 if (s->flags & SS_COMPLETE)
181 return make_message("%lli-%lli.being_deleted",
182 (long long)s->creation_time,
183 (long long)s->completion_time);
184 return make_message("%lli-incomplete.being_deleted",
185 (long long)s->creation_time);
188 int complete_name(int64_t start, int64_t end, char **result)
190 struct tm start_tm, end_tm;
191 time_t *start_seconds = (time_t *) (uint64_t *)&start; /* STFU, gcc */
192 time_t *end_seconds = (time_t *) (uint64_t *)&end; /* STFU, gcc */
193 char start_str[200], end_str[200];
195 if (!localtime_r(start_seconds, &start_tm))
197 if (!localtime_r(end_seconds, &end_tm))
199 if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm))
201 if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm))
203 *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
208 __malloc char *name_of_newest_complete_snapshot(struct snapshot_list *sl)
214 FOR_EACH_SNAPSHOT_REVERSE(s, i, sl) {
215 if (s->flags != SS_COMPLETE) /* incomplete or being deleted */
217 name = dss_strdup(s->name);