Merge branch 'refs/heads/t/kill-w'
[dss.git] / snap.h
1 /*
2 * Copyright (C) 2008-2010 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7
8 /** The possible states for snapshot creation/removal. */
9 #define HOOK_STATUS_ARRAY \
10 HSA_ITEM(HS_READY, "ready for action"), \
11 HSA_ITEM(HS_PRE_RUNNING, "pre-hook running"), \
12 HSA_ITEM(HS_PRE_SUCCESS, "pre-hook completed successfully"), \
13 HSA_ITEM(HS_RUNNING, "in progress"), \
14 HSA_ITEM(HS_SUCCESS, "process terminated successfully"), \
15 HSA_ITEM(HS_NEEDS_RESTART, "restart needed"), \
16 HSA_ITEM(HS_POST_RUNNING, "post-hook running")
17
18
19 #define HSA_ITEM(x, y) x
20 enum hook_status {HOOK_STATUS_ARRAY};
21 #undef HSA_ITEM
22 #define HSA_ITEM(x, y) y
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 int num_complete_snapshots(struct snapshot_list *sl);
92
93 /**
94 * Get the newest snapshot in a snapshot list.
95 */
96 static inline struct snapshot *get_newest_snapshot(struct snapshot_list *sl)
97 {
98 if (!sl->num_snapshots)
99 return NULL;
100 return sl->snapshots[sl->num_snapshots - 1];
101 }