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