index.html.in: Remove Link to top of the page.
[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 * Return the desired number of snapshots of an interval.
24 */
25 unsigned desired_number_of_snapshots(int interval_num, int num_intervals)
26 {
27 unsigned n;
28
29 assert(interval_num >= 0);
30
31 if (interval_num >= num_intervals)
32 return 0;
33 n = num_intervals - interval_num - 1;
34 return 1 << n;
35 }
36
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,
39 struct snapshot *s)
40 {
41 int i, ret;
42 char *dash, *dot, *tmp;
43 int64_t num;
44
45 assert(dirname);
46 dash = strchr(dirname, '-');
47 if (!dash || !dash[1] || dash == dirname)
48 return 0;
49 for (i = 0; dirname[i] != '-'; i++)
50 if (!isdigit(dirname[i]))
51 return 0;
52 tmp = dss_strdup(dirname);
53 tmp[i] = '\0';
54 ret = dss_atoi64(tmp, &num);
55 free(tmp);
56 if (ret < 0)
57 return 0;
58 assert(num >= 0);
59 if (num > now)
60 return 0;
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 */
68 goto success;
69 }
70 if (!strcmp(dash + 1, "incomplete.being_deleted")) {
71 s->completion_time = -1;
72 s->flags = SS_BEING_DELETED; /* mot cpmplete, being deleted */
73 goto success;
74 }
75 tmp = dash + 1;
76 dot = strchr(tmp, '.');
77 if (!dot || !dot[1] || dot == tmp)
78 return 0;
79 for (i = 0; tmp[i] != '.'; i++)
80 if (!isdigit(tmp[i]))
81 return 0;
82 tmp = dss_strdup(dash + 1);
83 tmp[i] = '\0';
84 ret = dss_atoi64(tmp, &num);
85 free(tmp);
86 if (ret < 0)
87 return 0;
88 if (num > now)
89 return 0;
90 s->completion_time = num;
91 s->flags = SS_COMPLETE;
92 if (!strcmp(dot + 1, "being_deleted"))
93 s->flags |= SS_BEING_DELETED;
94 success:
95 s->name = dss_strdup(dirname);
96 return 1;
97 }
98
99 struct add_snapshot_data {
100 int unit_interval;
101 int num_intervals;
102 struct snapshot_list *sl;
103 };
104
105 /** Compute the minimum of \a a and \a b. */
106 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
107
108 static int add_snapshot(const char *dirname, void *private)
109 {
110 struct add_snapshot_data *asd = private;
111 struct snapshot_list *sl = asd->sl;
112 struct snapshot s;
113 int ret = is_snapshot(dirname, sl->now, asd->unit_interval, &s);
114
115 if (!ret)
116 return 1;
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 *));
121 }
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)]++;
125 sl->num_snapshots++;
126 return 1;
127 }
128
129 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
130
131 static int compare_snapshots(const void *a, const void *b)
132 {
133 struct snapshot *s1 = *(struct snapshot **)a;
134 struct snapshot *s2 = *(struct snapshot **)b;
135 return NUM_COMPARE(s2->creation_time, s1->creation_time);
136 }
137
138
139 void get_snapshot_list(struct snapshot_list *sl, int unit_interval,
140 int num_intervals)
141 {
142 struct add_snapshot_data asd = {
143 .unit_interval = unit_interval,
144 .num_intervals = num_intervals,
145 .sl = sl
146 };
147 sl->now = get_current_time();
148 sl->num_snapshots = 0;
149 sl->array_size = 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 *),
154 compare_snapshots);
155 }
156
157 void free_snapshot_list(struct snapshot_list *sl)
158 {
159 int i;
160 struct snapshot *s;
161
162 FOR_EACH_SNAPSHOT(s, i, sl) {
163 free(s->name);
164 free(s);
165 }
166 free(sl->interval_count);
167 sl->interval_count = NULL;
168 free(sl->snapshots);
169 sl->snapshots = NULL;
170 sl->num_snapshots = 0;
171 }
172
173 __malloc char *incomplete_name(int64_t start)
174 {
175 return make_message("%lli-incomplete", (long long)start);
176 }
177
178 __malloc char *being_deleted_name(struct snapshot *s)
179 {
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);
186 }
187
188 int complete_name(int64_t start, int64_t end, char **result)
189 {
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];
194
195 if (!localtime_r(start_seconds, &start_tm))
196 return -E_LOCALTIME;
197 if (!localtime_r(end_seconds, &end_tm))
198 return -E_LOCALTIME;
199 if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm))
200 return -E_STRFTIME;
201 if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm))
202 return -E_STRFTIME;
203 *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
204 start_str, end_str);
205 return 1;
206 }
207
208 __malloc char *name_of_newest_complete_snapshot(struct snapshot_list *sl)
209 {
210 struct snapshot *s;
211 int i;
212 char *name = NULL;
213
214 FOR_EACH_SNAPSHOT_REVERSE(s, i, sl) {
215 if (s->flags != SS_COMPLETE) /* incomplete or being deleted */
216 continue;
217 name = dss_strdup(s->name);
218 break;
219 }
220 return name;
221 }
222