Merge branch 'master' into rm_hook
[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 rsync process needs to be restarted. */
20         SCS_RSYNC_NEEDS_RESTART,
21         /** The post-create hook has been started. */
22         SCS_POST_HOOK_RUNNING,
23 };
24
25 /** The state of snapshot removal. */
26 enum {
27         /** No snapshot is currently being removed. */
28         SRS_READY,
29         /** The pre-removal hook has been started. */
30         SRS_PRE_HOOK_RUNNING,
31         /** The pre-remove hook failed, we're waiting to execute it again. */
32         SRS_PRE_HOOK_FAILURE,
33         /** The rm command is currently executing. */
34         SRS_RM_RUNNING,
35         /** The post-remove hook ist running. */
36         SRS_POST_HOOK_RUNNING,
37 };
38
39 /**
40  * The status of a snapshot.
41  *
42  * The snapshot directories come in four different flavours, depending
43  * on how the two staus flags are set. Examples:
44  *
45  * Complete, not being deleted: 1204565370-1204565371.Sun_Mar_02_2008_14_33-Sun_Mar_02_2008_14_43.
46  * Complete, being deleted: 1204565370-1204565371.being_deleted.
47  * Incomplete, not being deleted: 1204565370-incomplete.
48  * incomplete, being deleted: 1204565370-incomplete.being_deleted.
49  */
50 enum snapshot_status_flags {
51         /** The rsync process terminated successfully. */
52         SS_COMPLETE = 1,
53         /** The rm process is running to remove this snapshot. */
54         SS_BEING_DELETED = 2,
55 };
56
57 /** Describes one snapshot. */
58 struct snapshot {
59         /** The name of the directory, relative to the destination dir. */
60         char *name;
61         /** Seconds after the epoch when this snapshot was created. */
62         int64_t creation_time;
63         /**
64          * Seconds after the epoch when creation of this snapshot completed.
65          * Only meaningful if the SS_COMPLTE bit is set.
66          */
67         int64_t completion_time;
68         /** See \ref snapshot_status_flags. */
69         enum snapshot_status_flags flags;
70         /** The interval this snapshot belongs to. */
71         unsigned interval;
72 };
73
74 struct snapshot_list {
75         int64_t now;
76         unsigned num_snapshots;
77         unsigned array_size;
78         struct snapshot **snapshots;
79         /**
80          * Array of size num_intervals + 1
81          *
82          * It contains the number of snapshots in each interval. interval_count[num_intervals]
83          * is the number of snapshots which belong to any interval greater than num_intervals.
84          */
85         unsigned *interval_count;
86 };
87
88 /** Iterate over all snapshots in a snapshot list. */
89 #define FOR_EACH_SNAPSHOT(s, i, sl) \
90         for ((i) = 0; (i) < (sl)->num_snapshots && ((s) = (sl)->snapshots[(i)]); (i)++)
91
92 /** Iterate backwards over all snapshots in a snapshot list. */
93 #define FOR_EACH_SNAPSHOT_REVERSE(s, i, sl) \
94         for ((i) = (sl)->num_snapshots; (i) > 0 && ((s) = (sl)->snapshots[(i - 1)]); (i)--)
95
96
97 unsigned desired_number_of_snapshots(int interval_num, int num_intervals);
98 void get_snapshot_list(struct snapshot_list *sl, int unit_interval,
99                 int num_intervals);
100 void free_snapshot_list(struct snapshot_list *sl);
101 __malloc char *incomplete_name(int64_t start);
102 __malloc char *being_deleted_name(struct snapshot *s);
103 int complete_name(int64_t start, int64_t end, char **result);
104 __malloc char *name_of_newest_complete_snapshot(struct snapshot_list *sl);
105
106 /**
107  * Get the oldest snapshot in a snapshot list.
108  */
109 _static_inline_ struct snapshot *get_oldest_snapshot(struct snapshot_list *sl)
110 {
111         if (!sl->num_snapshots)
112                 return NULL;
113         return sl->snapshots[0];
114 }