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 :
11 848 : static int get_link(const char *dir, const char *lnk, char real[], size_t r)
12 : {
13 848 : ssize_t len=0;
14 848 : char *tmp=NULL;
15 848 : if(!(tmp=prepend_s(dir, lnk)))
16 : return -1;
17 1696 : if((len=readlink(tmp, real, r-1))<0) len=0;
18 848 : real[len]='\0';
19 848 : free_w(&tmp);
20 848 : return 0;
21 : }
22 :
23 5407 : static void have_backup_file_name(struct bu *bu,
24 : const char *file, uint32_t bit)
25 : {
26 : struct stat statp;
27 : static char path[256]="";
28 5407 : snprintf(path, sizeof(path), "%s/%s", bu->path, file);
29 10689 : if(lstat(path, &statp)) return;
30 125 : bu->flags|=bit;
31 : }
32 :
33 6657 : static void have_backup_file_name_w(struct bu *bu,
34 : const char *file, uint32_t bit)
35 : {
36 : char compressed[32];
37 : snprintf(compressed, sizeof(compressed), "%s.gz", file);
38 2219 : have_backup_file_name(bu, file, bit);
39 2219 : have_backup_file_name(bu, compressed, bit);
40 2219 : }
41 :
42 1234 : static int maybe_add_ent(const char *dir, const char *d_name,
43 : struct bu **bu_list, uint16_t flags, struct cstat *include_working)
44 : {
45 1234 : int ret=-1;
46 1234 : char buf[32]="";
47 : struct stat statp;
48 1234 : char *fullpath=NULL;
49 1234 : char *timestamp=NULL;
50 1234 : char *timestampstr=NULL;
51 1234 : char *hlinkedpath=NULL;
52 1234 : char *basename=NULL;
53 2203 : struct bu *bu=NULL;
54 :
55 2468 : if(!(basename=prepend("", d_name))
56 1234 : || !(fullpath=prepend_s(dir, basename))
57 1234 : || !(timestamp=prepend_s(fullpath, "timestamp"))
58 2468 : || !(hlinkedpath=prepend_s(fullpath, "hardlinked")))
59 : goto error;
60 :
61 3702 : if((!lstat(fullpath, &statp) && !S_ISDIR(statp.st_mode))
62 2468 : || lstat(timestamp, &statp) || !S_ISREG(statp.st_mode)
63 1234 : || timestamp_read(timestamp, buf, sizeof(buf))
64 : // A bit of paranoia to protect against loading directories moved
65 : // aside as if they were real storage directories.
66 2468 : || strncmp(buf, d_name, 8))
67 : {
68 : ret=0; // For resilience.
69 : goto error;
70 : }
71 :
72 1232 : free_w(×tamp);
73 :
74 1232 : if(!(timestampstr=strdup_w(buf, __func__)))
75 : goto error;
76 :
77 2464 : if(!lstat(hlinkedpath, &statp)) flags|=BU_HARDLINKED;
78 :
79 1232 : if(!(bu=bu_alloc())
80 1232 : || bu_init(bu, fullpath, basename, timestampstr, flags))
81 : goto error;
82 :
83 1232 : if(*bu_list) bu->next=*bu_list;
84 1232 : *bu_list=bu;
85 1232 : have_backup_file_name_w(bu, "manifest", BU_MANIFEST);
86 1232 : if(include_working)
87 : {
88 329 : have_backup_file_name_w(bu, "log", BU_LOG_BACKUP);
89 329 : have_backup_file_name_w(bu, "restorelog", BU_LOG_RESTORE);
90 329 : have_backup_file_name_w(bu, "verifylog", BU_LOG_VERIFY);
91 : // Hack to include option for live counters.
92 329 : if(include_working->run_status==RUN_STATUS_RUNNING)
93 : {
94 24 : switch(include_working->cntr->cntr_status)
95 : {
96 : case CNTR_STATUS_SCANNING:
97 : case CNTR_STATUS_BACKUP:
98 : case CNTR_STATUS_MERGING:
99 : case CNTR_STATUS_SHUFFLING:
100 6 : bu->flags|=BU_STATS_BACKUP;
101 6 : break;
102 : case CNTR_STATUS_VERIFYING:
103 6 : bu->flags|=BU_STATS_VERIFY;
104 6 : break;
105 : case CNTR_STATUS_RESTORING:
106 6 : bu->flags|=BU_STATS_RESTORE;
107 6 : break;
108 : default:
109 : break;
110 : }
111 : }
112 329 : if(!(bu->flags & BU_STATS_BACKUP))
113 323 : have_backup_file_name(bu, "backup_stats", BU_STATS_BACKUP);
114 329 : if(!(bu->flags & BU_STATS_RESTORE))
115 323 : have_backup_file_name(bu, "restore_stats", BU_STATS_RESTORE);
116 329 : if(!(bu->flags & BU_STATS_VERIFY))
117 323 : have_backup_file_name(bu, "verify_stats", BU_STATS_VERIFY);
118 : }
119 :
120 1232 : free_w(&hlinkedpath);
121 1232 : return 0;
122 : error:
123 2 : free_w(&basename);
124 2 : free_w(&fullpath);
125 2 : free_w(×tamp);
126 2 : free_w(×tampstr);
127 2 : free_w(&hlinkedpath);
128 2 : return ret;
129 : }
130 :
131 280 : static void setup_indices(struct bu *bu_list)
132 : {
133 : int i;
134 280 : int tr=0;
135 280 : struct bu *bu=NULL;
136 280 : struct bu *last=NULL;
137 :
138 280 : i=1;
139 1504 : for(bu=bu_list; bu; bu=bu->next)
140 : {
141 : // Enumerate the position of each entry.
142 1224 : bu->index=i++;
143 :
144 : // Backups that come after hardlinked backups are deletable.
145 1224 : if((bu->flags & BU_HARDLINKED) && bu->next)
146 216 : bu->next->flags|=BU_DELETABLE;
147 :
148 : // Also set up reverse linkage.
149 1224 : bu->prev=last;
150 1224 : last=bu;
151 : }
152 :
153 : // The oldest backup is deletable.
154 280 : if(bu_list) bu_list->flags|=BU_DELETABLE;
155 :
156 280 : if(last)
157 : {
158 :
159 274 : if((tr=last->bno))
160 : {
161 : // Transpose bnos so that the oldest bno is set to 1.
162 1224 : for(bu=bu_list; bu; bu=bu->next)
163 1224 : bu->trbno=tr-bu->bno+1;
164 : }
165 : }
166 280 : }
167 :
168 281 : static int do_bu_get_list(struct sdirs *sdirs,
169 : struct bu **bu_list, struct cstat *include_working)
170 : {
171 281 : int i=0;
172 281 : int n=0;
173 281 : int ret=-1;
174 281 : char realwork[32]="";
175 281 : char realfinishing[32]="";
176 281 : char realcurrent[32]="";
177 281 : struct dirent **dp=NULL;
178 281 : const char *dir=NULL;
179 281 : uint16_t flags=0;
180 : struct stat statp;
181 :
182 281 : if(!sdirs)
183 : {
184 1 : logp("%s() called with NULL sdirs\n", __func__);
185 1 : goto end;
186 : }
187 :
188 280 : dir=sdirs->client;
189 :
190 560 : if(get_link(dir, "working", realwork, sizeof(realwork))
191 280 : || get_link(dir, "finishing", realfinishing, sizeof(realfinishing))
192 560 : || get_link(dir, "current", realcurrent, sizeof(realcurrent)))
193 : goto end;
194 :
195 280 : if(!stat(dir, &statp)
196 280 : && entries_in_directory_alphasort_rev(dir, &dp, &n, 1 /* atime */))
197 : {
198 0 : logp("scandir failed in %s: %s\n", __func__, strerror(errno));
199 0 : goto end;
200 : }
201 1526 : for(i=0; i<n; i++)
202 : {
203 : // Each storage directory starts with a digit. The 'deleteme'
204 : // directory does not. This check avoids loading 'deleteme'
205 : // as a storage directory.
206 1526 : if(!isdigit(dp[i]->d_name[0]))
207 : continue;
208 1230 : flags=0;
209 1230 : if(!strcmp(dp[i]->d_name, realcurrent))
210 : {
211 : flags|=BU_CURRENT;
212 : }
213 956 : else if(!strcmp(dp[i]->d_name, realwork))
214 : {
215 9 : if(!include_working) continue;
216 : flags|=BU_WORKING;
217 : }
218 947 : else if(!strcmp(dp[i]->d_name, realfinishing))
219 : {
220 9 : if(!include_working) continue;
221 : flags|=BU_FINISHING;
222 : }
223 1226 : if(maybe_add_ent(dir, dp[i]->d_name, bu_list, flags,
224 1226 : include_working)) goto end;
225 : }
226 :
227 280 : setup_indices(*bu_list);
228 :
229 280 : ret=0;
230 : end:
231 281 : if(dp)
232 : {
233 1526 : for(i=0; i<n; i++) free_v((void **)&dp[i]);
234 274 : free_v((void **)&dp);
235 : }
236 281 : return ret;
237 : }
238 :
239 197 : int bu_get_list(struct sdirs *sdirs, struct bu **bu_list)
240 : {
241 197 : return do_bu_get_list(sdirs, bu_list, NULL);
242 : }
243 :
244 84 : int bu_get_list_with_working(struct sdirs *sdirs, struct bu **bu_list,
245 : struct cstat *cstat)
246 : {
247 84 : return do_bu_get_list(sdirs, bu_list, cstat);
248 : }
249 :
250 8 : int bu_get_current(struct sdirs *sdirs, struct bu **bu_list)
251 : {
252 8 : char real[32]="";
253 : // FIX THIS: should not need to specify "current".
254 8 : if(get_link(sdirs->client, "current", real, sizeof(real)))
255 : return -1;
256 8 : return maybe_add_ent(sdirs->client, real, bu_list, BU_CURRENT, NULL);
257 : }
|