Line data Source code
1 : #include "../burp.h"
2 : #include "../alloc.h"
3 : #include "../bu.h"
4 : #include "../cstat.h"
5 : #include "../fsops.h"
6 : #include "../log.h"
7 : #include "../prepend.h"
8 : #include "sdirs.h"
9 : #include "timestamp.h"
10 : #include "bu_get.h"
11 :
12 : static int get_link(const char *dir, const char *lnk, char real[], size_t r)
13 : {
14 872 : return readlink_w_in_dir(dir, lnk, real, r);
15 : }
16 :
17 5301 : static void have_backup_file_name(struct bu *bu,
18 : const char *file, uint32_t bit)
19 : {
20 : struct stat statp;
21 : static char path[256]="";
22 5301 : snprintf(path, sizeof(path), "%s/%s", bu->path, file);
23 10466 : if(lstat(path, &statp)) return;
24 136 : bu->flags|=bit;
25 : }
26 :
27 6498 : static void have_backup_file_name_w(struct bu *bu,
28 : const char *file, uint32_t bit)
29 : {
30 : char compressed[32];
31 : snprintf(compressed, sizeof(compressed), "%s.gz", file);
32 2166 : have_backup_file_name(bu, file, bit);
33 2166 : have_backup_file_name(bu, compressed, bit);
34 2166 : }
35 :
36 1199 : static int maybe_add_ent(const char *dir, const char *d_name,
37 : struct bu **bu_list, uint16_t flags, struct cstat *cstat,
38 : int include_working)
39 : {
40 1199 : int ret=-1;
41 1199 : char buf[38]="";
42 : struct stat statp;
43 1199 : char *fullpath=NULL;
44 1199 : char *timestamp=NULL;
45 1199 : char *timestampstr=NULL;
46 1199 : char *hlinkedpath=NULL;
47 1199 : char *basename=NULL;
48 2168 : struct bu *bu=NULL;
49 :
50 1199 : if(!(basename=prepend("", d_name))
51 1199 : || !(fullpath=prepend_s(dir, basename))
52 1199 : || !(timestamp=prepend_s(fullpath, "timestamp"))
53 1199 : || !(hlinkedpath=prepend_s(fullpath, "hardlinked")))
54 : goto error;
55 :
56 2398 : if((!lstat(fullpath, &statp) && !S_ISDIR(statp.st_mode))
57 2398 : || lstat(timestamp, &statp) || !S_ISREG(statp.st_mode)
58 1199 : || timestamp_read(timestamp, buf, sizeof(buf))
59 : // A bit of paranoia to protect against loading directories moved
60 : // aside as if they were real storage directories.
61 1199 : || strncmp(buf, d_name, 8))
62 : {
63 : ret=0; // For resilience.
64 : goto error;
65 : }
66 :
67 1197 : free_w(×tamp);
68 :
69 1197 : if(!(timestampstr=strdup_w(buf, __func__)))
70 : goto error;
71 :
72 2394 : if(!lstat(hlinkedpath, &statp)) flags|=BU_HARDLINKED;
73 :
74 1197 : if(!(bu=bu_alloc())
75 1197 : || bu_init(bu, fullpath, basename, timestampstr, flags))
76 : goto error;
77 :
78 1197 : if(*bu_list) bu->next=*bu_list;
79 1197 : *bu_list=bu;
80 1197 : have_backup_file_name_w(bu, "manifest", BU_MANIFEST);
81 1197 : if(include_working)
82 : {
83 323 : have_backup_file_name_w(bu, "log", BU_LOG_BACKUP);
84 323 : have_backup_file_name_w(bu, "restorelog", BU_LOG_RESTORE);
85 323 : have_backup_file_name_w(bu, "verifylog", BU_LOG_VERIFY);
86 323 : if(!(bu->flags & BU_STATS_BACKUP))
87 323 : have_backup_file_name(bu, "backup_stats", BU_STATS_BACKUP);
88 323 : if(!(bu->flags & BU_STATS_RESTORE))
89 323 : have_backup_file_name(bu, "restore_stats", BU_STATS_RESTORE);
90 323 : if(!(bu->flags & BU_STATS_VERIFY))
91 323 : have_backup_file_name(bu, "verify_stats", BU_STATS_VERIFY);
92 : }
93 :
94 1197 : free_w(&hlinkedpath);
95 : return 0;
96 : error:
97 2 : free_w(&basename);
98 2 : free_w(&fullpath);
99 2 : free_w(×tamp);
100 2 : free_w(×tampstr);
101 2 : free_w(&hlinkedpath);
102 : return ret;
103 : }
104 :
105 288 : static void setup_indices(struct bu *bu_list, enum protocol protocol)
106 : {
107 : int i;
108 288 : int tr=0;
109 288 : struct bu *bu=NULL;
110 288 : struct bu *last=NULL;
111 :
112 288 : i=1;
113 1477 : for(bu=bu_list; bu; bu=bu->next)
114 : {
115 : // Enumerate the position of each entry.
116 1189 : bu->index=i++;
117 :
118 1189 : if(protocol==PROTO_2)
119 : {
120 : // All PROTO_2 backups are deletable.
121 553 : bu->flags|=BU_DELETABLE;
122 : }
123 : else
124 : {
125 : // Backups that come after hardlinked backups are
126 : // deletable.
127 636 : if((bu->flags & BU_HARDLINKED) && bu->next)
128 111 : bu->next->flags|=BU_DELETABLE;
129 : }
130 :
131 : // Also set up reverse linkage.
132 1189 : bu->prev=last;
133 1189 : last=bu;
134 : }
135 :
136 : // The oldest backup is deletable.
137 288 : if(bu_list) bu_list->flags|=BU_DELETABLE;
138 :
139 288 : if(last)
140 : {
141 :
142 276 : if((tr=last->bno))
143 : {
144 : // Transpose bnos so that the oldest bno is set to 1.
145 1189 : for(bu=bu_list; bu; bu=bu->next)
146 1189 : bu->trbno=tr-bu->bno+1;
147 : }
148 : }
149 288 : }
150 :
151 289 : static int do_bu_get_list(struct sdirs *sdirs,
152 : struct bu **bu_list, struct cstat *cstat, int include_working)
153 : {
154 289 : int i=0;
155 289 : int n=0;
156 289 : int ret=-1;
157 289 : char realwork[38]="";
158 289 : char realfinishing[38]="";
159 289 : char realcurrent[38]="";
160 289 : struct dirent **dp=NULL;
161 289 : const char *dir=NULL;
162 289 : uint16_t flags=0;
163 : struct stat statp;
164 :
165 289 : if(!sdirs)
166 : {
167 1 : logp("%s() called with NULL sdirs\n", __func__);
168 : goto end;
169 : }
170 :
171 288 : dir=sdirs->client;
172 :
173 288 : if(get_link(dir, "working", realwork, sizeof(realwork))
174 288 : || get_link(dir, "finishing", realfinishing, sizeof(realfinishing))
175 288 : || get_link(dir, "current", realcurrent, sizeof(realcurrent)))
176 : goto end;
177 :
178 288 : if(!stat(dir, &statp)
179 288 : && (n=scandir(dir, &dp, filter_dot, alphasort))<0)
180 : {
181 0 : logp("scandir failed in %s: %s\n", __func__, strerror(errno));
182 : goto end;
183 : }
184 288 : i=n;
185 1790 : while(i--)
186 : {
187 : // Each storage directory starts with a digit. The 'deleteme'
188 : // directory does not. This check avoids loading 'deleteme'
189 : // as a storage directory.
190 1502 : if(!isdigit(dp[i]->d_name[0]))
191 307 : continue;
192 1195 : flags=0;
193 1195 : if(!strcmp(dp[i]->d_name, realcurrent))
194 : {
195 : flags|=BU_CURRENT;
196 : }
197 919 : else if(!strcmp(dp[i]->d_name, realwork))
198 : {
199 9 : if(!include_working) continue;
200 : flags|=BU_WORKING;
201 : }
202 910 : else if(!strcmp(dp[i]->d_name, realfinishing))
203 : {
204 9 : if(!include_working) continue;
205 : flags|=BU_FINISHING;
206 : }
207 1191 : if(maybe_add_ent(dir, dp[i]->d_name, bu_list, flags,
208 : cstat, include_working)) goto end;
209 : }
210 :
211 288 : setup_indices(*bu_list, sdirs->protocol);
212 :
213 288 : ret=0;
214 : end:
215 289 : if(dp)
216 : {
217 1502 : for(i=0; i<n; i++)
218 1502 : free(dp[i]);
219 282 : free(dp);
220 : }
221 289 : return ret;
222 : }
223 :
224 207 : int bu_get_list(struct sdirs *sdirs, struct bu **bu_list)
225 : {
226 207 : return do_bu_get_list(sdirs, bu_list, NULL, 0/*include_working*/);
227 : }
228 :
229 82 : int bu_get_list_with_working(struct sdirs *sdirs, struct bu **bu_list,
230 : struct cstat *cstat)
231 : {
232 82 : return do_bu_get_list(sdirs, bu_list, cstat, 1/*include_working*/);
233 : }
234 :
235 8 : int bu_get_current(struct sdirs *sdirs, struct bu **bu_list)
236 : {
237 8 : char real[38]="";
238 : // FIX THIS: should not need to specify "current".
239 16 : if(get_link(sdirs->client, "current", real, sizeof(real)))
240 : return -1;
241 8 : return maybe_add_ent(sdirs->client, real, bu_list, BU_CURRENT,
242 : NULL, 0/*include_working*/);
243 : }
|