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