52f060b8a8dd790194925ac481e44af32a808973
[dss.git] / snap.h
1 /*
2  * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** The state of snapshot creation. */
8 enum {
9         /** We are ready to take the next snapshot. */
10         SCS_READY,
11         /** The pre-creation hook has been started. */
12         SCS_PRE_HOOK_RUNNING,
13         /** The pre-creation hook exited successfully. */
14         SCS_PRE_HOOK_SUCCESS,
15         /** The rsync process is running. */
16         SCS_RSYNC_RUNNING,
17         /** The rsync process exited successfully. */
18         SCS_RSYNC_SUCCESS,
19         /** The post-create hook has been started. */
20         SCS_POST_HOOK_RUNNING,
21 };
22
23 /**
24  * The status of a snapshot.
25  *
26  * The snapshot directories come in four different flavours, depending
27  * on how the two staus flags are set. Examples:
28  *
29  * Complete, not being deleted: 1204565370-1204565371.Sun_Mar_02_2008_14_33-Sun_Mar_02_2008_14_43.
30  * Complete, being deleted: 1204565370-1204565371.being_deleted.
31  * Incomplete, not being deleted: 1204565370-incomplete.
32  * incomplete, being deleted: 1204565370-incomplete.being_deleted.
33  */
34 enum snapshot_status_flags {
35         /** The rsync process terminated successfully. */
36         SS_COMPLETE = 1,
37         /** The rm process is running to remove this snapshot. */
38         SS_BEING_DELETED = 2,
39 };
40
41 /** Desribes one snapshot */
42 struct snapshot {
43         /** The name of the directory, relative to the destination dir. */
44         char *name;
45         /** Seconds after the epoch when this snapshot was created. */
46         int64_t creation_time;
47         /**
48          * Seconds after the epoch when creation of this snapshot completed.
49          * Only meaningful if the SS_COMPLTE bit is set.
50          */
51         int64_t completion_time;
52         /** See \ref snapshot_status_flags. */
53         enum snapshot_status_flags flags;
54         /** The interval this snapshot belongs to. */
55         unsigned interval;
56 };
57
58 struct snapshot_list {
59         int64_t now;
60         unsigned num_snapshots;
61         unsigned array_size;
62         struct snapshot **snapshots;
63         /**
64          * Array of size num_intervals + 1
65          *
66          * It contains the number of snapshots in each interval. interval_count[num_intervals]
67          * is the number of snapshots which belong to any interval greater than num_intervals.
68          */
69         unsigned *interval_count;
70 };
71
72 /** Iterate over all snapshots in a snapshot list. */
73 #define FOR_EACH_SNAPSHOT(s, i, sl) \
74         for ((i) = 0; (i) < (sl)->num_snapshots && ((s) = (sl)->snapshots[(i)]); (i)++)
75
76 /** Iterate backwards over all snapshots in a snapshot list. */
77 #define FOR_EACH_SNAPSHOT_REVERSE(s, i, sl) \
78         for ((i) = (sl)->num_snapshots; (i) > 0 && ((s) = (sl)->snapshots[(i - 1)]); (i)--)
79
80
81 unsigned desired_number_of_snapshots(int interval_num, int num_intervals);
82 void get_snapshot_list(struct snapshot_list *sl, int unit_interval,
83                 int num_intervals);
84 void free_snapshot_list(struct snapshot_list *sl);
85 __malloc char *incomplete_name(int64_t start);
86 __malloc char *being_deleted_name(struct snapshot *s);
87 int complete_name(int64_t start, int64_t end, char **result);
88 __malloc char *name_of_newest_complete_snapshot(struct snapshot_list *sl);
89
90 /**
91  * Get the oldest snapshot in a snapshot list.
92  */
93 _static_inline_ struct snapshot *get_oldest_snapshot(struct snapshot_list *sl)
94 {
95         if (!sl->num_snapshots)
96                 return NULL;
97         return sl->snapshots[0];
98 }