Move snapshot helpers to new file snap.[ch].
[dss.git] / snap.h
1 enum {
2 /** We are ready to take the next snapshot. */
3 SCS_READY,
4 /** The pre-creation hook has been started. */
5 SCS_PRE_HOOK_RUNNING,
6 /** The pre-creation hook exited successfully. */
7 SCS_PRE_HOOK_SUCCESS,
8 /** The rsync process is running. */
9 SCS_RSYNC_RUNNING,
10 /** The rsync process exited successfully. */
11 SCS_RSYNC_SUCCESS,
12 /** The post-create hook has been started- */
13 SCS_POST_HOOK_RUNNING,
14 };
15
16 /*
17 * complete, not being deleted: 1204565370-1204565371.Sun_Mar_02_2008_14_33-Sun_Mar_02_2008_14_43
18 * complete, being deleted: 1204565370-1204565371.being_deleted
19 * incomplete, not being deleted: 1204565370-incomplete
20 * incomplete, being deleted: 1204565370-incomplete.being_deleted
21 */
22 enum snapshot_status_flags {
23 /** The rsync process terminated successfully. */
24 SS_COMPLETE = 1,
25 /** The rm process is running to remove this snapshot. */
26 SS_BEING_DELETED = 2,
27 };
28
29 struct snapshot {
30 char *name;
31 int64_t creation_time;
32 int64_t completion_time;
33 enum snapshot_status_flags flags;
34 unsigned interval;
35 };
36
37 struct snapshot_list {
38 int64_t now;
39 unsigned num_snapshots;
40 unsigned array_size;
41 struct snapshot **snapshots;
42 /**
43 * Array of size num_intervals + 1
44 *
45 * It contains the number of snapshots in each interval. interval_count[num_intervals]
46 * is the number of snapshots which belong to any interval greater than num_intervals.
47 */
48 unsigned *interval_count;
49 };
50
51 #define FOR_EACH_SNAPSHOT(s, i, sl) \
52 for ((i) = 0; (i) < (sl)->num_snapshots && ((s) = (sl)->snapshots[(i)]); (i)++)
53
54 #define FOR_EACH_SNAPSHOT_REVERSE(s, i, sl) \
55 for ((i) = (sl)->num_snapshots; (i) > 0 && ((s) = (sl)->snapshots[(i - 1)]); (i)--)
56
57
58 unsigned desired_number_of_snapshots(int interval_num, int num_intervals);
59 void get_snapshot_list(struct snapshot_list *sl, int unit_interval,
60 int num_intervals);
61 void free_snapshot_list(struct snapshot_list *sl);
62 __malloc char *incomplete_name(int64_t start);
63 __malloc char *being_deleted_name(struct snapshot *s);
64 int complete_name(int64_t start, int64_t end, char **result);
65 __malloc char *name_of_newest_complete_snapshot(struct snapshot_list *sl);
66
67 static inline struct snapshot *get_oldest_snapshot(struct snapshot_list *sl)
68 {
69 if (!sl->num_snapshots)
70 return NULL;
71 return sl->snapshots[0];
72 }