Line data Source code
1 : #include "../../burp.h"
2 : #include "../../alloc.h"
3 : #include "../../asfd.h"
4 : #include "../../async.h"
5 : #include "../../bu.h"
6 : #include "../../cmd.h"
7 : #include "../../cntr.h"
8 : #include "../../handy.h"
9 : #include "../../hexmap.h"
10 : #include "../../log.h"
11 : #include "../../prepend.h"
12 : #include "../../protocol1/handy.h"
13 : #include "../../server/protocol1/backup_phase4.h"
14 : #include "../../server/protocol1/link.h"
15 : #include "../../server/protocol1/zlibio.h"
16 : #include "../../server/protocol2/restore.h"
17 : #include "../../sbuf.h"
18 : #include "../../slist.h"
19 : #include "../sdirs.h"
20 : #include "dpth.h"
21 : #include "restore.h"
22 :
23 : #include <librsync.h>
24 :
25 0 : static int create_zero_length_file(const char *path)
26 : {
27 0 : int ret=0;
28 : struct fzp *dest;
29 0 : if(!(dest=fzp_open(path, "wb")))
30 0 : ret=-1;
31 0 : ret|=fzp_close(&dest);
32 0 : return ret;
33 : }
34 :
35 0 : static int inflate_or_link_oldfile(struct asfd *asfd, const char *oldpath,
36 : const char *infpath, struct conf **cconfs, int compression)
37 : {
38 0 : int ret=0;
39 : struct stat statp;
40 :
41 0 : if(lstat(oldpath, &statp))
42 : {
43 0 : logp("could not lstat %s\n", oldpath);
44 0 : return -1;
45 : }
46 :
47 0 : if(dpth_protocol1_is_compressed(compression, oldpath))
48 : {
49 : //logp("inflating...\n");
50 :
51 0 : if(!statp.st_size)
52 : {
53 : // Empty file - cannot inflate.
54 0 : logp("asked to inflate zero length file: %s\n",
55 : oldpath);
56 0 : return create_zero_length_file(infpath);
57 : }
58 :
59 0 : if((ret=zlib_inflate(asfd, oldpath, infpath, get_cntr(cconfs))))
60 0 : logp("zlib_inflate returned: %d\n", ret);
61 : }
62 : else
63 : {
64 : // Not compressed - just hard link it.
65 0 : if(do_link(oldpath, infpath, &statp, cconfs,
66 : 1 /* allow overwrite of infpath */))
67 : return -1;
68 : }
69 0 : return ret;
70 : }
71 :
72 4 : static int burp_send_file(struct asfd *asfd, struct sbuf *sb,
73 : int patches, const char *best, struct cntr *cntr)
74 : {
75 4 : enum send_e ret=SEND_FATAL;
76 : struct BFILE bfd;
77 4 : uint64_t bytes=0; // Unused.
78 :
79 4 : bfile_init(&bfd, 0, cntr);
80 4 : if(bfd.open_for_send(&bfd, asfd, best, sb->winattr,
81 : 1 /* no O_NOATIME */, cntr, PROTO_1))
82 : return SEND_FATAL;
83 4 : if(asfd->write(asfd, &sb->path))
84 : ret=SEND_FATAL;
85 4 : else if(patches)
86 : {
87 : // If we did some patches, the resulting file
88 : // is not gzipped. Gzip it during the send.
89 0 : ret=send_whole_file_gzl(asfd, sb->protocol1->datapth.buf,
90 : 1, &bytes, NULL, cntr, 9, &bfd, NULL, 0);
91 : }
92 : else
93 : {
94 : // If it was encrypted, it may or may not have been compressed
95 : // before encryption. Send it as it as, and let the client
96 : // sort it out.
97 4 : if(sbuf_is_encrypted(sb))
98 : {
99 2 : ret=send_whole_filel(asfd,
100 : #ifdef HAVE_WIN32
101 : sb->path.cmd
102 : #endif
103 2 : sb->protocol1->datapth.buf,
104 : 1, &bytes, cntr, &bfd, NULL, 0);
105 : }
106 : // It might have been stored uncompressed. Gzip it during
107 : // the send. If the client knew what kind of file it would be
108 : // receiving, this step could disappear.
109 2 : else if(!dpth_protocol1_is_compressed(sb->compression,
110 2 : sb->protocol1->datapth.buf))
111 : {
112 2 : ret=send_whole_file_gzl(asfd,
113 2 : sb->protocol1->datapth.buf, 1, &bytes,
114 : NULL, cntr, 9, &bfd, NULL, 0);
115 : }
116 : else
117 : {
118 : // If we did not do some patches, the resulting
119 : // file might already be gzipped. Send it as it is.
120 0 : ret=send_whole_filel(asfd,
121 : #ifdef HAVE_WIN32
122 : sb->path.cmd
123 : #endif
124 0 : sb->protocol1->datapth.buf,
125 : 1, &bytes, cntr, &bfd, NULL, 0);
126 : }
127 : }
128 4 : bfd.close(&bfd, asfd);
129 :
130 4 : switch(ret)
131 : {
132 : case SEND_OK:
133 : case SEND_ERROR: // Carry on.
134 : return 0;
135 : case SEND_FATAL:
136 : default:
137 0 : return -1;
138 : }
139 : }
140 :
141 : #ifndef UTEST
142 : static
143 : #endif
144 5 : int verify_file(struct asfd *asfd, struct sbuf *sb,
145 : int patches, const char *best, struct cntr *cntr)
146 : {
147 : MD5_CTX md5;
148 5 : int b=0;
149 5 : const char *cp=NULL;
150 5 : const char *newsum=NULL;
151 : uint8_t in[ZCHUNK];
152 : uint8_t checksum[MD5_DIGEST_LENGTH];
153 5 : uint64_t cbytes=0;
154 5 : struct fzp *fzp=NULL;
155 :
156 5 : if(!sb->endfile.buf
157 5 : || !(cp=strrchr(sb->endfile.buf, ':')))
158 : {
159 1 : logw(asfd, cntr,
160 1 : "%s has no md5sum!\n", sb->protocol1->datapth.buf);
161 1 : return 0;
162 : }
163 4 : cp++;
164 4 : if(!MD5_Init(&md5))
165 : {
166 0 : logp("MD5_Init() failed\n");
167 0 : return -1;
168 : }
169 4 : if(patches
170 4 : || sb->path.cmd==CMD_ENC_FILE
171 4 : || sb->path.cmd==CMD_ENC_METADATA
172 4 : || sb->path.cmd==CMD_EFS_FILE
173 4 : || sb->path.cmd==CMD_ENC_VSS
174 4 : || (!patches && !dpth_protocol1_is_compressed(sb->compression, best)))
175 2 : fzp=fzp_open(best, "rb");
176 : else
177 2 : fzp=fzp_gzopen(best, "rb");
178 :
179 4 : if(!fzp)
180 : {
181 1 : logw(asfd, cntr, "could not open %s\n", best);
182 1 : return 0;
183 : }
184 5 : while((b=fzp_read(fzp, in, ZCHUNK))>0)
185 : {
186 2 : cbytes+=b;
187 2 : if(!MD5_Update(&md5, in, b))
188 : {
189 0 : logp("MD5_Update() failed\n");
190 0 : fzp_close(&fzp);
191 0 : return -1;
192 : }
193 : }
194 3 : if(!fzp_eof(fzp))
195 : {
196 1 : logw(asfd, cntr, "error while reading %s\n", best);
197 1 : fzp_close(&fzp);
198 1 : return 0;
199 : }
200 2 : fzp_close(&fzp);
201 2 : if(!MD5_Final(checksum, &md5))
202 : {
203 0 : logp("MD5_Final() failed\n");
204 0 : return -1;
205 : }
206 2 : newsum=bytes_to_md5str(checksum);
207 :
208 2 : if(strcmp(newsum, cp))
209 : {
210 1 : logp("%s %s\n", newsum, cp);
211 1 : logw(asfd, cntr, "md5sum for '%s (%s)' did not match!\n",
212 1 : sb->path.buf, sb->protocol1->datapth.buf);
213 1 : logp("md5sum for '%s (%s)' did not match!\n",
214 1 : sb->path.buf, sb->protocol1->datapth.buf);
215 1 : return 0;
216 : }
217 :
218 : // Just send the file name to the client, so that it can show cntr.
219 1 : if(asfd->write(asfd, &sb->path)) return -1;
220 1 : return 0;
221 : }
222 :
223 4 : static int process_data_dir_file(struct asfd *asfd,
224 : struct bu *bu, struct bu *b, const char *path,
225 : struct sbuf *sb, enum action act, struct sdirs *sdirs,
226 : struct conf **cconfs)
227 : {
228 4 : int ret=-1;
229 4 : int patches=0;
230 4 : char *dpath=NULL;
231 : struct stat dstatp;
232 4 : const char *tmp=NULL;
233 4 : const char *best=NULL;
234 : static char *tmppath1=NULL;
235 : static char *tmppath2=NULL;
236 4 : struct cntr *cntr=NULL;
237 4 : if(cconfs) cntr=get_cntr(cconfs);
238 :
239 4 : if((!tmppath1 && !(tmppath1=prepend_s(bu->path, "tmp1")))
240 4 : || (!tmppath2 && !(tmppath2=prepend_s(bu->path, "tmp2"))))
241 : goto end;
242 :
243 4 : best=path;
244 4 : tmp=tmppath1;
245 : // Now go down the list, applying any deltas.
246 4 : for(b=b->prev; b && b->next!=bu; b=b->prev)
247 : {
248 0 : free_w(&dpath);
249 0 : if(!(dpath=prepend_s(b->delta, sb->protocol1->datapth.buf)))
250 : goto end;
251 :
252 0 : if(lstat(dpath, &dstatp) || !S_ISREG(dstatp.st_mode))
253 0 : continue;
254 :
255 0 : if(!patches)
256 : {
257 : // Need to gunzip the first one.
258 0 : if(inflate_or_link_oldfile(asfd, best, tmp,
259 : cconfs, sb->compression))
260 : {
261 0 : logw(asfd, cntr,
262 : "problem when inflating %s\n", best);
263 0 : ret=0;
264 : goto end;
265 : }
266 0 : best=tmp;
267 0 : if(tmp==tmppath1) tmp=tmppath2;
268 : else tmp=tmppath1;
269 : }
270 :
271 0 : if(do_patch(best, dpath, tmp,
272 : 0 /* do not gzip the result */,
273 : sb->compression /* from the manifest */))
274 : {
275 0 : logw(asfd, cntr, "problem when patching %s with %s\n", path, b->timestamp);
276 0 : ret=0;
277 : goto end;
278 : }
279 :
280 0 : best=tmp;
281 0 : if(tmp==tmppath1) tmp=tmppath2;
282 : else tmp=tmppath1;
283 0 : unlink(tmp);
284 0 : patches++;
285 : }
286 :
287 4 : switch(act)
288 : {
289 : case ACTION_RESTORE:
290 4 : if(burp_send_file(asfd, sb, patches, best, cntr))
291 : goto end;
292 : break;
293 : case ACTION_VERIFY:
294 0 : if(verify_file(asfd, sb, patches, best, cntr))
295 : goto end;
296 : break;
297 : default:
298 0 : logp("Unknown action: %d\n", act);
299 : goto end;
300 : }
301 4 : cntr_add(cntr, sb->path.cmd, 0);
302 4 : cntr_add_bytes(cntr, strtoull(sb->endfile.buf, NULL, 10));
303 :
304 4 : ret=0;
305 : end:
306 4 : free_w(&dpath);
307 4 : if(tmppath1) unlink(tmppath1);
308 4 : if(tmppath2) unlink(tmppath2);
309 4 : free_w(&tmppath1);
310 4 : free_w(&tmppath2);
311 4 : return ret;
312 : }
313 :
314 : // a = length of struct bu array
315 : // i = position to restore from
316 : #ifndef UTEST
317 : static
318 : #endif
319 5 : int restore_file(struct asfd *asfd, struct bu *bu,
320 : struct sbuf *sb, enum action act,
321 : struct sdirs *sdirs, struct conf **cconfs)
322 : {
323 5 : int ret=-1;
324 5 : char *path=NULL;
325 : struct bu *b;
326 5 : struct bu *hlwarn=NULL;
327 : struct stat statp;
328 5 : struct cntr *cntr=NULL;
329 5 : if(cconfs) cntr=get_cntr(cconfs);
330 :
331 : // Go up the array until we find the file in the data directory.
332 10 : for(b=bu; b; b=b->next)
333 : {
334 4 : free_w(&path);
335 4 : if(!(path=prepend_s(b->data, sb->protocol1->datapth.buf)))
336 : goto end;
337 :
338 8 : if(lstat(path, &statp) || !S_ISREG(statp.st_mode))
339 0 : continue;
340 :
341 4 : if(b!=bu && (bu->flags & BU_HARDLINKED)) hlwarn=b;
342 :
343 4 : if(process_data_dir_file(asfd, bu, b,
344 : path, sb, act, sdirs, cconfs))
345 : goto end;
346 :
347 : // This warning must be done after everything else,
348 : // Because the client does not expect another cmd after
349 : // the warning.
350 4 : if(hlwarn) logw(asfd, cntr, "restore found %s in %s\n",
351 : sb->path.buf, hlwarn->basename);
352 : ret=0; // All OK.
353 : break;
354 : }
355 :
356 5 : if(!b)
357 : {
358 1 : logw(asfd, cntr, "restore could not find %s (%s)\n",
359 1 : sb->path.buf, sb->protocol1->datapth.buf);
360 1 : ret=0; // Carry on to subsequent files.
361 : }
362 : end:
363 5 : free_w(&path);
364 5 : return ret;
365 : }
366 :
367 10 : int restore_sbuf_protocol1(struct asfd *asfd, struct sbuf *sb, struct bu *bu,
368 : enum action act, struct sdirs *sdirs, struct conf **cconfs)
369 : {
370 10 : if((sb->protocol1->datapth.buf
371 4 : && asfd->write(asfd, &(sb->protocol1->datapth)))
372 10 : || asfd->write(asfd, &sb->attr))
373 : return -1;
374 10 : else if(sbuf_is_filedata(sb)
375 6 : || sbuf_is_vssdata(sb))
376 : {
377 4 : if(!sb->protocol1->datapth.buf)
378 : {
379 0 : logw(asfd, get_cntr(cconfs),
380 : "Got filedata entry with no datapth: %s\n",
381 : iobuf_to_printable(&sb->path));
382 0 : return 0;
383 : }
384 4 : return restore_file(asfd, bu, sb, act, sdirs, cconfs);
385 : }
386 : else
387 : {
388 6 : if(asfd->write(asfd, &sb->path))
389 : return -1;
390 : // If it is a link, send what
391 : // it points to.
392 6 : else if(sbuf_is_link(sb)
393 4 : && asfd->write(asfd, &sb->link)) return -1;
394 6 : cntr_add(get_cntr(cconfs), sb->path.cmd, 0);
395 : }
396 6 : return 0;
397 : }
|