Line data Source code
1 : #include "../../burp.h"
2 : #include "../../alloc.h"
3 : #include "../../asfd.h"
4 : #include "../../async.h"
5 : #include "../../attribs.h"
6 : #include "../../cmd.h"
7 : #include "../../cntr.h"
8 : #include "../../conf.h"
9 : #include "../../conffile.h"
10 : #include "../../cstat.h"
11 : #include "../../fsops.h"
12 : #include "../../handy.h"
13 : #include "../../iobuf.h"
14 : #include "../../log.h"
15 : #include "../../sbuf.h"
16 : #include "../child.h"
17 : #include "../compress.h"
18 : #include "../resume.h"
19 : #include "blocklen.h"
20 : #include "dpth.h"
21 : #include "backup_phase2.h"
22 : #include "link.h"
23 :
24 : static size_t treepathlen=0;
25 :
26 0 : static int path_length_warn(struct iobuf *path, struct conf **cconfs)
27 : {
28 0 : if(get_int(cconfs[OPT_PATH_LENGTH_WARN]))
29 0 : logw(NULL, get_cntr(cconfs), "Path too long for tree - will save in data structure instead: %s\n", path->buf);
30 0 : return 1;
31 : }
32 :
33 4 : static int path_too_long(struct iobuf *path, struct conf **cconfs)
34 : {
35 : const char *cp;
36 : const char *cp1;
37 : size_t len;
38 :
39 4 : if(treepathlen+path->len+1>fs_full_path_max)
40 : {
41 : // FIX THIS:
42 : // Cannot warn down the asfd to the client, because it can
43 : // arrive after the client has disconnected, which causes
44 : // an error on the server side.
45 : // Would need to change the way that "backupphase2end" works
46 : // to be able to fix it.
47 0 : return path_length_warn(path, cconfs);
48 : }
49 : // We have to check every part in the path to ensure it less then fs_name_max
50 : // minimum windows case is c:/a, nix case is /
51 : // usual: c:/users, /home
52 4 : cp = strchr(path->buf, '/');
53 4 : if( !cp ) // very strange
54 0 : return strlen(path->buf) > fs_name_max ? path_length_warn(path, cconfs):0;
55 14 : while(cp && *cp)
56 : {
57 10 : cp++;
58 10 : cp1 = strchr(cp, '/');
59 10 : len = cp1? cp1-cp : strlen(cp);
60 10 : if( len > fs_name_max )
61 0 : return path_length_warn(path, cconfs);
62 : cp = cp1;
63 : }
64 : return 0;
65 : }
66 :
67 4 : static int treedata(struct sbuf *sb, struct conf **cconfs)
68 : {
69 : // Windows is sending directory data as if it is file data - this
70 : // cannot be saved in a tree structure.
71 4 : if(S_ISDIR(sb->statp.st_mode)) return 0;
72 :
73 8 : if(sb->path.cmd!=CMD_FILE
74 4 : && sb->path.cmd!=CMD_ENC_FILE
75 0 : && sb->path.cmd!=CMD_EFS_FILE)
76 : return 0;
77 :
78 4 : return !path_too_long(&sb->path, cconfs);
79 : }
80 :
81 4 : static char *set_new_datapth(struct sdirs *sdirs, struct conf **cconfs,
82 : struct sbuf *sb, struct dpth *dpth, int *istreedata)
83 : {
84 4 : char *tmp=NULL;
85 4 : char *rpath=NULL;
86 4 : if(get_int(cconfs[OPT_DIRECTORY_TREE]))
87 4 : *istreedata=treedata(sb, cconfs);
88 :
89 4 : if(*istreedata)
90 : {
91 : // We want to place this file in a directory structure like
92 : // the directory structure on the original client.
93 4 : if(!(tmp=prepend_s(TREE_DIR, sb->path.buf)))
94 : {
95 0 : log_out_of_memory(__func__);
96 : return NULL;
97 : }
98 : }
99 : else
100 : {
101 0 : if(!(tmp=strdup_w(dpth_protocol1_mk(dpth, sb->compression,
102 : sb->path.cmd), __func__))) return NULL;
103 : }
104 4 : iobuf_from_str(&sb->protocol1->datapth, CMD_DATAPTH, tmp);
105 4 : if(build_path(sdirs->datadirtmp,
106 4 : sb->protocol1->datapth.buf, &rpath, sdirs->datadirtmp))
107 : {
108 0 : logp("build path failed");
109 : return NULL;
110 : }
111 4 : return rpath;
112 : }
113 :
114 4 : static int start_to_receive_new_file(struct asfd *asfd,
115 : struct sdirs *sdirs, struct conf **cconfs,
116 : struct sbuf *sb, struct dpth *dpth)
117 : {
118 4 : int ret=-1;
119 4 : char *rpath=NULL;
120 4 : int istreedata=0;
121 :
122 : //logp("start to receive: %s\n", sb->path.buf);
123 :
124 4 : if(!(rpath=set_new_datapth(sdirs, cconfs, sb, dpth, &istreedata)))
125 : goto end;
126 :
127 4 : if(!(sb->protocol1->fzp=fzp_open(rpath, "wb")))
128 : {
129 0 : log_and_send(asfd, "make file failed");
130 0 : goto end;
131 : }
132 4 : if(!istreedata) dpth_incr(dpth);
133 : ret=0;
134 : end:
135 4 : free_w(&rpath);
136 4 : return ret;
137 : }
138 :
139 : enum processed_e
140 : {
141 : P_ERROR=-1,
142 : P_DONE_NEW=0,
143 : P_DONE_CHANGED=1,
144 : P_DONE_UNCHANGED=2,
145 : P_DONE_DELETED=3,
146 : P_NEW=4,
147 : P_CHANGED=5
148 : };
149 :
150 : #include <librsync.h>
151 :
152 2 : static enum processed_e process_changed_file(struct asfd *asfd,
153 : struct conf **cconfs,
154 : struct sbuf *cb, struct sbuf *p1b,
155 : const char *adir)
156 : {
157 2 : int ret=P_ERROR;
158 2 : size_t blocklen=0;
159 2 : char *curpath=NULL;
160 : //logp("need to process changed file: %s (%s)\n",
161 : // cb->path, cb->datapth);
162 :
163 : // Move datapth onto p1b.
164 2 : iobuf_move(&p1b->protocol1->datapth, &cb->protocol1->datapth);
165 :
166 2 : if(!(curpath=prepend_s(adir, p1b->protocol1->datapth.buf)))
167 : {
168 0 : log_out_of_memory(__func__);
169 : goto end;
170 : }
171 2 : if(dpth_protocol1_is_compressed(cb->compression, curpath))
172 0 : p1b->protocol1->sigfzp=fzp_gzopen(curpath, "rb");
173 : else
174 2 : p1b->protocol1->sigfzp=fzp_open(curpath, "rb");
175 2 : if(!p1b->protocol1->sigfzp)
176 : {
177 0 : logp("could not open %s: %s\n", curpath, strerror(errno));
178 : goto end;
179 : }
180 :
181 2 : blocklen=get_librsync_block_len(cb->endfile.buf);
182 4 : if(!(p1b->protocol1->sigjob=
183 : #ifdef RS_DEFAULT_STRONG_LEN
184 2 : rs_sig_begin(blocklen, RS_DEFAULT_STRONG_LEN)
185 : #else
186 : // This is for librsync-1.0.0. RS_DEFAULT_STRONG_LEN was 8 in
187 : // librsync-0.9.7.
188 : rs_sig_begin(blocklen, 8,
189 : rshash_to_magic_number(get_e_rshash(cconfs[OPT_RSHASH])))
190 : #endif
191 : ))
192 : {
193 0 : logp("could not start signature job.\n");
194 : goto end;
195 : }
196 : //logp("sig begin: %s\n", p1b->protocol1->datapth.buf);
197 2 : if(!(p1b->protocol1->infb=rs_filebuf_new(NULL,
198 2 : p1b->protocol1->sigfzp,
199 : NULL, blocklen, -1)))
200 : {
201 0 : logp("could not rs_filebuf_new for infb.\n");
202 : goto end;
203 : }
204 2 : if(!(p1b->protocol1->outfb=rs_filebuf_new(NULL, NULL,
205 : asfd, ASYNC_BUF_LEN, -1)))
206 : {
207 0 : logp("could not rs_filebuf_new for in_outfb.\n");
208 : goto end;
209 : }
210 :
211 : // Flag the things that need to be sent (to the client)
212 2 : p1b->flags |= SBUF_SEND_DATAPTH;
213 2 : p1b->flags |= SBUF_SEND_STAT;
214 2 : p1b->flags |= SBUF_SEND_PATH;
215 :
216 : //logp("sending sig for %s\n", p1b->path);
217 : //logp("(%s)\n", p1b->datapth);
218 :
219 2 : ret=P_CHANGED;
220 : end:
221 2 : free_w(&curpath);
222 2 : return ret;
223 : }
224 :
225 68 : static enum processed_e new_non_file(struct sbuf *p1b,
226 : struct manio *ucmanio, struct conf **cconfs)
227 : {
228 : // Is something that does not need more data backed up.
229 : // Like a directory or a link or something like that.
230 : // Goes into the unchanged file, so that it does not end up out of
231 : // order with normal files, which has to wait around for their data
232 : // to turn up.
233 68 : if(manio_write_sbuf(ucmanio, p1b))
234 : return P_ERROR;
235 68 : cntr_add(get_cntr(cconfs), p1b->path.cmd, 0);
236 68 : return P_DONE_NEW;
237 : }
238 :
239 0 : static enum processed_e changed_non_file(struct sbuf *p1b,
240 : struct manio *ucmanio, enum cmd cmd, struct conf **cconfs)
241 : {
242 : // As new_non_file.
243 0 : if(manio_write_sbuf(ucmanio, p1b))
244 : return P_ERROR;
245 0 : cntr_add_changed(get_cntr(cconfs), cmd);
246 0 : return P_DONE_CHANGED;
247 : }
248 :
249 122 : static enum processed_e process_new(struct conf **cconfs,
250 : struct sbuf *p1b, struct manio *ucmanio)
251 : {
252 122 : if(sbuf_is_filedata(p1b)
253 68 : || sbuf_is_vssdata(p1b))
254 : {
255 : //logp("need to process new file: %s\n", p1b->path);
256 : // Flag the things that need to be sent (to the client)
257 54 : p1b->flags |= SBUF_SEND_STAT;
258 54 : p1b->flags |= SBUF_SEND_PATH;
259 54 : return P_NEW;
260 : }
261 68 : return new_non_file(p1b, ucmanio, cconfs);
262 : }
263 :
264 6 : static enum processed_e process_unchanged_file(struct sbuf *p1b, struct sbuf *cb,
265 : struct manio *ucmanio, struct conf **cconfs)
266 : {
267 : // Need to re-encode the p1b attribs to include compression and
268 : // other bits and pieces that are recorded on cb.
269 6 : iobuf_move(&p1b->protocol1->datapth, &cb->protocol1->datapth);
270 6 : iobuf_move(&p1b->endfile, &cb->endfile);
271 6 : p1b->compression=cb->compression;
272 : // Why is winattr not getting sent in phase1?
273 6 : p1b->winattr=cb->winattr;
274 : // Need to free attr so that it is reallocated, because it may get
275 : // longer than what the client told us in phase1.
276 6 : iobuf_free_content(&p1b->attr);
277 6 : if(attribs_encode(p1b))
278 : return P_ERROR;
279 6 : if(manio_write_sbuf(ucmanio, p1b))
280 : return P_ERROR;
281 6 : cntr_add_same(get_cntr(cconfs), p1b->path.cmd);
282 6 : if(p1b->endfile.buf) cntr_add_bytes(get_cntr(cconfs),
283 0 : strtoull(p1b->endfile.buf, NULL, 10));
284 : return P_DONE_UNCHANGED;
285 : }
286 :
287 10 : static int open_previous_manifest(struct manio **manio, struct sdirs *sdirs)
288 : {
289 : struct stat statp;
290 20 : if(!lstat(sdirs->cmanifest, &statp)
291 2 : && !(*manio=manio_open(sdirs->cmanifest, "rb", PROTO_1)))
292 : {
293 0 : logp("could not open old manifest %s\n", sdirs->cmanifest);
294 : return -1;
295 : }
296 : return 0;
297 : }
298 :
299 0 : static int get_hardlink_master_from_hmanio(
300 : struct manio **hmanio,
301 : struct sdirs *sdirs,
302 : struct sbuf *cb,
303 : struct sbuf *hb
304 : ) {
305 : int passes=0;
306 : while(1)
307 : {
308 0 : sbuf_free_content(hb);
309 0 : switch(manio_read(*hmanio, hb))
310 : {
311 : case 0: // Keep going.
312 : break;
313 : case 1: // Finished OK.
314 0 : manio_close(hmanio);
315 : // Might have already been part way down the
316 : // manifest when we started. If we get to the
317 : // end, go back to the beginning once.
318 0 : if(!passes)
319 : {
320 0 : if(open_previous_manifest(hmanio,
321 : sdirs)) return -1;
322 0 : passes++;
323 0 : continue;
324 : }
325 : return 0;
326 : default: // Error;
327 : return -1;
328 : }
329 0 : if(hb->path.cmd!=CMD_FILE
330 0 : && hb->path.cmd!=CMD_ENC_FILE)
331 0 : continue;
332 0 : if(!strcmp(hb->path.buf, cb->link.buf)
333 0 : && hb->protocol1->datapth.buf)
334 : return 1; // Found it.
335 : }
336 : return -1;
337 : }
338 :
339 0 : static int relink_deleted_hardlink_master(
340 : struct dpth *dpth,
341 : struct manio **hmanio,
342 : struct sdirs *sdirs,
343 : struct sbuf *p1b,
344 : struct sbuf *cb,
345 : struct conf **cconfs
346 : ) {
347 0 : int ret=-1;
348 0 : char *newdatapth_full=NULL;
349 0 : char *olddatapth_full=NULL;
350 0 : char *newdatapth=NULL;
351 0 : char *relinkpath_full=NULL;
352 : struct stat statp;
353 0 : struct sbuf *hb=NULL;
354 0 : int istreedata=0;
355 :
356 0 : if(!(hb=sbuf_alloc(PROTO_1)))
357 : goto end;
358 :
359 0 : switch(get_hardlink_master_from_hmanio(hmanio, sdirs, cb, hb))
360 : {
361 : case 0: // Did not find it.
362 : return 0;
363 : case 1: // Found it.
364 : break;
365 : default: // Error.
366 : goto end;
367 : }
368 :
369 : // Protect against the old file being encrypted, and the new file not
370 : // being encrypted - and vice versa.
371 0 : if(p1b->path.cmd!=hb->path.cmd)
372 : return 0;
373 :
374 0 : if(!(newdatapth_full=set_new_datapth(sdirs,
375 : cconfs, p1b, dpth, &istreedata)))
376 : goto end;
377 0 : if(!istreedata) dpth_incr(dpth);
378 0 : if(!(relinkpath_full=prepend_s(sdirs->relink,
379 0 : p1b->protocol1->datapth.buf)))
380 : goto end;
381 0 : if(!(newdatapth=strdup_w(p1b->protocol1->datapth.buf, __func__)))
382 : goto end;
383 : /*
384 : printf("newdatapth_full: %s\n", newdatapth_full);
385 : printf("relinkpath_full: %s\n", relinkpath_full);
386 : printf("newdatapth: %s\n", newdatapth);
387 : printf("hbdatapth: %s\n", hb->protocol1->datapth.buf);
388 : printf("sdirs->currentdata: %s\n", sdirs->currentdata);
389 : */
390 :
391 0 : if(!(olddatapth_full=prepend_s(sdirs->currentdata,
392 0 : hb->protocol1->datapth.buf)))
393 : goto end;
394 0 : if(lstat(olddatapth_full, &statp) || !S_ISREG(statp.st_mode))
395 : {
396 0 : logw(NULL, get_cntr(cconfs),
397 : "Did not find path for relink: %s\n", olddatapth_full);
398 0 : ret=0;
399 0 : goto end;
400 : }
401 0 : if(build_path_w(relinkpath_full))
402 : goto end;
403 0 : if(do_link(olddatapth_full, relinkpath_full,
404 : &statp, cconfs, /*overwrite*/0))
405 : goto end;
406 :
407 0 : iobuf_free_content(&cb->protocol1->datapth);
408 :
409 0 : iobuf_from_str(&hb->protocol1->datapth, CMD_DATAPTH,
410 0 : p1b->protocol1->datapth.buf);
411 0 : p1b->protocol1->datapth.buf=NULL;
412 0 : iobuf_from_str(&cb->protocol1->datapth, CMD_DATAPTH, newdatapth);
413 0 : newdatapth=NULL;
414 0 : iobuf_move(&cb->endfile, &hb->endfile);
415 :
416 0 : ret=1;
417 : end:
418 0 : free_w(&newdatapth_full);
419 0 : free_w(&olddatapth_full);
420 0 : free_w(&newdatapth);
421 0 : free_w(&relinkpath_full);
422 0 : sbuf_free(&hb);
423 0 : return ret;
424 : }
425 :
426 10 : static enum processed_e maybe_do_delta_stuff(struct asfd *asfd,
427 : struct dpth *dpth,
428 : struct sdirs *sdirs, struct sbuf *cb, struct sbuf *p1b,
429 : struct manio *ucmanio, struct manio **hmanio, struct conf **cconfs)
430 : {
431 10 : int oldcompressed=0;
432 10 : int compression=p1b->compression;
433 :
434 : // If the file type changed, I think it is time to back it up again
435 : // (for example, EFS changing to normal file, or back again).
436 10 : if(cb->path.cmd!=p1b->path.cmd)
437 : {
438 0 : if(hmanio && *hmanio
439 0 : && cb->path.cmd==CMD_HARD_LINK
440 0 : && (p1b->path.cmd==CMD_FILE
441 0 : || p1b->path.cmd==CMD_ENC_FILE))
442 : {
443 0 : struct stat *p1statp=&p1b->statp;
444 0 : struct stat *cstatp=&cb->statp;
445 : // A hardlink changed into a file. It is possible that
446 : // The file that the hardlink was pointing to got
447 : // deleted. Maybe we can reuse the previous file.
448 0 : if(p1statp->st_dev==cstatp->st_dev
449 0 : && p1statp->st_ino==cstatp->st_ino
450 0 : && p1statp->st_mtime==cstatp->st_mtime)
451 : {
452 0 : switch(relink_deleted_hardlink_master(
453 : dpth, hmanio, sdirs,
454 : p1b, cb, cconfs))
455 : {
456 : case 0:
457 : break;
458 : case 1:
459 0 : return process_unchanged_file(
460 : p1b, cb,
461 : ucmanio, cconfs);
462 : default:
463 : return P_ERROR;
464 : }
465 : }
466 : }
467 0 : return process_new(cconfs, p1b, ucmanio);
468 : }
469 :
470 : // mtime is the actual file data.
471 : // ctime is the attributes or meta data.
472 10 : if(cb->statp.st_mtime==p1b->statp.st_mtime
473 6 : && cb->statp.st_ctime==p1b->statp.st_ctime)
474 : {
475 : // got an unchanged file
476 : //logp("got unchanged file: %s %c %c\n",
477 : // cb->path.buf, cb->path.cmd, p1b->path.cmd);
478 6 : return process_unchanged_file(p1b, cb, ucmanio, cconfs);
479 : }
480 :
481 4 : if(cb->statp.st_mtime==p1b->statp.st_mtime
482 0 : && cb->statp.st_ctime!=p1b->statp.st_ctime)
483 : {
484 : // File data stayed the same, but attributes or meta data
485 : // changed. We already have the attributes, but may need to get
486 : // extra meta data.
487 : // FIX THIS horrible mess.
488 0 : if(cb->path.cmd==CMD_ENC_METADATA
489 0 : || p1b->path.cmd==CMD_ENC_METADATA
490 0 : || cb->path.cmd==CMD_EFS_FILE
491 0 : || p1b->path.cmd==CMD_EFS_FILE
492 : // FIX THIS: make unencrypted metadata use the librsync
493 0 : || cb->path.cmd==CMD_METADATA
494 0 : || p1b->path.cmd==CMD_METADATA
495 0 : || sbuf_is_vssdata(cb)
496 0 : || sbuf_is_vssdata(p1b))
497 0 : return process_new(cconfs, p1b, ucmanio);
498 : // On Windows, we have to back up the whole file if ctime
499 : // changed, otherwise things like permission changes do not get
500 : // noticed. So, in that case, fall through to the changed stuff
501 : // below.
502 : // Non-Windows clients finish here.
503 0 : else if(!get_int(cconfs[OPT_CLIENT_IS_WINDOWS]))
504 0 : return process_unchanged_file(p1b,
505 : cb, ucmanio, cconfs);
506 : }
507 :
508 : // Got a changed file.
509 : //logp("got changed file: %s\n", p1b->path.buf);
510 :
511 : // If either old or new is encrypted, or librsync is off, we need to
512 : // get a new file.
513 : // FIX THIS horrible mess.
514 4 : if(!get_int(cconfs[OPT_LIBRSYNC])
515 : // FIX THIS: make unencrypted metadata use the librsync
516 4 : || cb->path.cmd==CMD_METADATA
517 4 : || p1b->path.cmd==CMD_METADATA
518 4 : || sbuf_is_encrypted(cb)
519 2 : || sbuf_is_encrypted(p1b)
520 2 : || sbuf_is_vssdata(cb)
521 2 : || sbuf_is_vssdata(p1b))
522 2 : return process_new(cconfs, p1b, ucmanio);
523 :
524 : // Get new files if they have switched between compression on or off.
525 2 : if(cb->protocol1->datapth.buf
526 2 : && dpth_protocol1_is_compressed(cb->compression,
527 : cb->protocol1->datapth.buf))
528 0 : oldcompressed=1;
529 2 : if( ( oldcompressed && !compression)
530 2 : || (!oldcompressed && compression))
531 0 : return process_new(cconfs, p1b, ucmanio);
532 :
533 : // Otherwise, do the delta stuff (if possible).
534 2 : if(sbuf_is_filedata(p1b)
535 0 : || sbuf_is_vssdata(p1b))
536 2 : return process_changed_file(asfd, cconfs, cb, p1b,
537 2 : sdirs->currentdata);
538 0 : return changed_non_file(p1b, ucmanio, p1b->path.cmd, cconfs);
539 : }
540 :
541 : static enum processed_e process_deleted_file(struct sbuf *cb,
542 : struct conf **cconfs)
543 : {
544 2 : cntr_add_deleted(get_cntr(cconfs), cb->path.cmd);
545 : return P_DONE_DELETED;
546 : }
547 :
548 : // return 1 to say that a file was processed
549 12 : static enum processed_e maybe_process_file(struct asfd *asfd,
550 : struct dpth *dpth,
551 2 : struct sdirs *sdirs, struct sbuf *cb, struct sbuf *p1b,
552 : struct manio *ucmanio, struct manio **hmanio, struct conf **cconfs)
553 : {
554 : int pcmp;
555 12 : if(p1b)
556 : {
557 11 : if(!(pcmp=sbuf_pathcmp(cb, p1b)))
558 10 : return maybe_do_delta_stuff(asfd, dpth,sdirs, cb, p1b,
559 : ucmanio, hmanio, cconfs);
560 1 : else if(pcmp>0)
561 : {
562 : //logp("ahead: %s\n", p1b->path.buf);
563 : // ahead - need to get the whole file
564 0 : return process_new(cconfs, p1b, ucmanio);
565 : }
566 : }
567 : //logp("behind: %s\n", p1b->path.buf);
568 : // Behind - need to read more from the old manifest.
569 : // Count a deleted file - it was in the old manifest
570 : // but not the new.
571 4 : return process_deleted_file(cb, cconfs);
572 : }
573 :
574 : enum sts_e
575 : {
576 : STS_ERROR=-1,
577 : STS_OK=0,
578 : STS_BLOCKED=1
579 : };
580 :
581 : // Return 1 if there is still stuff needing to be sent.
582 : // FIX THIS: lots of repeated code.
583 63 : static enum sts_e do_stuff_to_send(struct asfd *asfd,
584 : struct sbuf *p1b, char **last_requested)
585 : {
586 : static struct iobuf wbuf;
587 63 : if(p1b->flags & SBUF_SEND_DATAPTH)
588 : {
589 2 : iobuf_copy(&wbuf, &p1b->protocol1->datapth);
590 2 : switch(asfd->append_all_to_write_buffer(asfd, &wbuf))
591 : {
592 : case APPEND_OK: break;
593 : case APPEND_BLOCKED: return STS_BLOCKED;
594 0 : default: return STS_ERROR;
595 : }
596 2 : p1b->flags &= ~SBUF_SEND_DATAPTH;
597 : }
598 63 : if(p1b->flags & SBUF_SEND_STAT)
599 : {
600 56 : iobuf_copy(&wbuf, &p1b->attr);
601 56 : switch(asfd->append_all_to_write_buffer(asfd, &wbuf))
602 : {
603 : case APPEND_OK: break;
604 : case APPEND_BLOCKED: return STS_BLOCKED;
605 0 : default: return STS_ERROR;
606 : }
607 56 : p1b->flags &= ~SBUF_SEND_STAT;
608 : }
609 63 : if(p1b->flags & SBUF_SEND_PATH)
610 : {
611 56 : iobuf_copy(&wbuf, &p1b->path);
612 56 : switch(asfd->append_all_to_write_buffer(asfd, &wbuf))
613 : {
614 : case APPEND_OK: break;
615 : case APPEND_BLOCKED: return STS_BLOCKED;
616 0 : default: return STS_ERROR;
617 : }
618 56 : p1b->flags &= ~SBUF_SEND_PATH;
619 56 : free_w(last_requested);
620 56 : if(!(*last_requested=strdup_w(p1b->path.buf, __func__)))
621 : return STS_ERROR;
622 : }
623 63 : if(p1b->protocol1->sigjob && !(p1b->flags & SBUF_SEND_ENDOFSIG))
624 : {
625 : rs_result sigresult;
626 :
627 4 : switch((sigresult=rs_async(p1b->protocol1->sigjob,
628 : &(p1b->protocol1->rsbuf),
629 : p1b->protocol1->infb, p1b->protocol1->outfb)))
630 : {
631 : case RS_DONE:
632 2 : p1b->flags |= SBUF_SEND_ENDOFSIG;
633 2 : break;
634 : case RS_BLOCKED:
635 : case RS_RUNNING:
636 : // keep going round the loop.
637 : return STS_BLOCKED;
638 : default:
639 0 : logp("error in rs_async: %d\n", sigresult);
640 0 : return STS_ERROR;
641 : }
642 : }
643 61 : if(p1b->flags & SBUF_SEND_ENDOFSIG)
644 : {
645 2 : iobuf_from_str(&wbuf, CMD_END_FILE, (char *)"endfile");
646 2 : switch(asfd->append_all_to_write_buffer(asfd, &wbuf))
647 : {
648 : case APPEND_OK: break;
649 : case APPEND_BLOCKED: return STS_BLOCKED;
650 0 : default: return STS_ERROR;
651 : }
652 2 : p1b->flags &= ~SBUF_SEND_ENDOFSIG;
653 : }
654 : return STS_OK;
655 : }
656 :
657 4 : static int start_to_receive_delta(struct sdirs *sdirs, struct sbuf *rb)
658 : {
659 4 : if(rb->compression)
660 : {
661 0 : if(!(rb->protocol1->fzp=fzp_gzopen(sdirs->deltmppath,
662 0 : comp_level(rb->compression))))
663 : return -1;
664 : }
665 : else
666 : {
667 4 : if(!(rb->protocol1->fzp=fzp_open(sdirs->deltmppath, "wb")))
668 : return -1;
669 : }
670 4 : rb->flags |= SBUF_RECV_DELTA;
671 :
672 : return 0;
673 : }
674 :
675 4 : static int finish_delta(struct sdirs *sdirs, struct sbuf *rb)
676 : {
677 4 : int ret=0;
678 4 : char *deltmp=NULL;
679 4 : char *delpath=NULL;
680 4 : if(!(deltmp=prepend_s("deltas.forward", rb->protocol1->datapth.buf))
681 4 : || !(delpath=prepend_s(sdirs->working, deltmp))
682 4 : || mkpath(&delpath, sdirs->working)
683 : // Rename race condition is of no consequence here, as delpath will
684 : // just get recreated.
685 4 : || do_rename(sdirs->deltmppath, delpath))
686 : ret=-1;
687 4 : free_w(&delpath);
688 4 : free_w(&deltmp);
689 4 : return ret;
690 : }
691 :
692 8 : static int deal_with_receive_end_file(struct asfd *asfd, struct sdirs *sdirs,
693 4 : struct sbuf *rb, struct manio *chmanio, struct conf **cconfs,
694 : char **last_requested)
695 : {
696 8 : int ret=-1;
697 : static char *cp=NULL;
698 : static struct iobuf *rbuf;
699 8 : struct cntr *cntr=get_cntr(cconfs);
700 8 : rbuf=asfd->rbuf;
701 : // Finished the file.
702 : // Write it to the phase2 file, and free the buffers.
703 :
704 8 : if(fzp_close(&(rb->protocol1->fzp)))
705 : {
706 0 : logp("error closing delta for %s in receive\n", rb->path.buf);
707 : goto end;
708 : }
709 8 : iobuf_move(&rb->endfile, rbuf);
710 12 : if(rb->flags & SBUF_RECV_DELTA && finish_delta(sdirs, rb))
711 : goto end;
712 :
713 8 : if(manio_write_sbuf(chmanio, rb))
714 : goto end;
715 :
716 8 : if(rb->flags & SBUF_RECV_DELTA)
717 4 : cntr_add_changed(cntr, rb->path.cmd);
718 : else
719 4 : cntr_add(cntr, rb->path.cmd, 0);
720 :
721 8 : if(*last_requested && !strcmp(rb->path.buf, *last_requested))
722 2 : free_w(last_requested);
723 :
724 8 : cp=strchr(rb->endfile.buf, ':');
725 8 : if(rb->endfile.buf)
726 8 : cntr_add_bytes(cntr, strtoull(rb->endfile.buf, NULL, 10));
727 : if(cp)
728 : {
729 : // checksum stuff goes here
730 : }
731 :
732 : ret=0;
733 : end:
734 8 : sbuf_free_content(rb);
735 8 : return ret;
736 : }
737 :
738 8 : static int deal_with_receive_append(struct asfd *asfd, struct sbuf *rb)
739 : {
740 8 : int app=0;
741 : static struct iobuf *rbuf;
742 8 : rbuf=asfd->rbuf;
743 : //logp("rbuf->len: %d\n", rbuf->len);
744 :
745 8 : if(rb->protocol1->fzp)
746 8 : app=fzp_write(rb->protocol1->fzp, rbuf->buf, rbuf->len);
747 :
748 8 : if(app>0) return 0;
749 0 : logp("error when appending: %d\n", app);
750 0 : asfd->write_str(asfd, CMD_ERROR, "write failed");
751 : return -1;
752 : }
753 :
754 8 : static int deal_with_filedata(struct asfd *asfd,
755 : struct sdirs *sdirs, struct sbuf *rb,
756 : struct iobuf *rbuf, struct dpth *dpth, struct conf **cconfs)
757 : {
758 8 : iobuf_move(&rb->path, rbuf);
759 :
760 8 : if(rb->protocol1->datapth.buf)
761 : {
762 : // Receiving a delta.
763 4 : if(start_to_receive_delta(sdirs, rb))
764 : {
765 0 : logp("error in start_to_receive_delta\n");
766 0 : return -1;
767 : }
768 : return 0;
769 : }
770 :
771 : // Receiving a whole new file.
772 4 : if(start_to_receive_new_file(asfd, sdirs, cconfs, rb, dpth))
773 : {
774 0 : logp("error in start_to_receive_new_file\n");
775 0 : return -1;
776 : }
777 : return 0;
778 : }
779 :
780 : enum str_e
781 : {
782 : STR_ERROR=-1,
783 : STR_OK=0,
784 : STR_FINISHED=1
785 : };
786 :
787 : // returns 1 for finished ok.
788 85 : static enum str_e do_stuff_to_receive(struct asfd *asfd,
789 : struct sdirs *sdirs, struct conf **cconfs,
790 : struct sbuf *rb, struct manio *chmanio,
791 : struct dpth *dpth, char **last_requested)
792 : {
793 85 : struct iobuf *rbuf=asfd->rbuf;
794 :
795 170 : if(rbuf->cmd==CMD_MESSAGE
796 85 : || rbuf->cmd==CMD_WARNING)
797 : {
798 0 : struct cntr *cntr=NULL;
799 0 : if(cconfs) cntr=get_cntr(cconfs);
800 0 : log_recvd(rbuf, cntr, 0);
801 0 : return STR_OK;
802 : }
803 :
804 85 : if(rb->protocol1->fzp)
805 : {
806 : // Currently writing a file (or meta data)
807 16 : switch(rbuf->cmd)
808 : {
809 : case CMD_APPEND:
810 8 : if(deal_with_receive_append(asfd, rb))
811 : goto error;
812 : return STR_OK;
813 : case CMD_END_FILE:
814 8 : if(deal_with_receive_end_file(asfd, sdirs, rb,
815 : chmanio, cconfs, last_requested))
816 : goto error;
817 : return STR_OK;
818 : default:
819 0 : iobuf_log_unexpected(rbuf, __func__);
820 0 : goto error;
821 : }
822 : }
823 :
824 : // Otherwise, expecting to be told of a file to save.
825 69 : switch(rbuf->cmd)
826 : {
827 : case CMD_DATAPTH:
828 4 : iobuf_move(&rb->protocol1->datapth, rbuf);
829 4 : return STR_OK;
830 : case CMD_ATTRIBS:
831 8 : iobuf_move(&rb->attr, rbuf);
832 8 : attribs_decode(rb);
833 8 : return STR_OK;
834 : case CMD_GEN:
835 5 : if(!strcmp(rbuf->buf, "okbackupphase2end"))
836 : goto end_phase2;
837 0 : iobuf_log_unexpected(rbuf, __func__);
838 0 : goto error;
839 : case CMD_INTERRUPT:
840 : // Interrupt - forget about the last requested
841 : // file if it matches. Otherwise, we can get
842 : // stuck on the select in the async stuff,
843 : // waiting for something that will never arrive.
844 44 : if(*last_requested
845 44 : && !strcmp(rbuf->buf, *last_requested))
846 1 : free_w(last_requested);
847 : return STR_OK;
848 : default:
849 : break;
850 : }
851 8 : if(iobuf_is_filedata(rbuf)
852 0 : || iobuf_is_vssdata(rbuf))
853 : {
854 8 : if(deal_with_filedata(asfd, sdirs, rb, rbuf, dpth, cconfs))
855 : goto error;
856 : return STR_OK;
857 : }
858 0 : iobuf_log_unexpected(rbuf, __func__);
859 :
860 : error:
861 : return STR_ERROR;
862 : end_phase2:
863 : return STR_FINISHED;
864 : }
865 :
866 5 : static int vss_opts_changed(struct sdirs *sdirs, struct conf **cconfs,
867 : const char *incexc)
868 : {
869 5 : int ret=-1;
870 : struct conf **oldconfs;
871 : struct conf **newconfs;
872 5 : if(!(oldconfs=confs_alloc())
873 5 : || !(newconfs=confs_alloc()))
874 : goto end;
875 5 : confs_init(oldconfs);
876 5 : confs_init(newconfs);
877 :
878 : // Figure out the old config, which is in the incexc file left
879 : // in the current backup directory on the server.
880 5 : if(conf_parse_incexcs_path(oldconfs, sdirs->cincexc))
881 : {
882 : // Assume that the file did not exist, and therefore
883 : // the old split_vss setting is 0.
884 4 : set_int(oldconfs[OPT_SPLIT_VSS], 0);
885 4 : set_int(oldconfs[OPT_STRIP_VSS], 0);
886 : }
887 :
888 : // Figure out the new config, which is either in the incexc file from
889 : // the client, or in the cconf on the server.
890 5 : if(incexc)
891 : {
892 0 : if(conf_parse_incexcs_buf(newconfs, incexc))
893 : {
894 : // Should probably not got here.
895 0 : set_int(newconfs[OPT_SPLIT_VSS], 0);
896 0 : set_int(newconfs[OPT_STRIP_VSS], 0);
897 : }
898 : }
899 : else
900 : {
901 5 : set_int(newconfs[OPT_SPLIT_VSS],
902 5 : get_int(cconfs[OPT_SPLIT_VSS]));
903 5 : set_int(newconfs[OPT_STRIP_VSS],
904 5 : get_int(cconfs[OPT_STRIP_VSS]));
905 : }
906 :
907 5 : if(get_int(newconfs[OPT_SPLIT_VSS])!=get_int(oldconfs[OPT_SPLIT_VSS]))
908 : {
909 0 : logp("split_vss=%d (changed since last backup)\n",
910 0 : get_int(newconfs[OPT_SPLIT_VSS]));
911 0 : ret=1; goto end;
912 : }
913 5 : if(get_int(newconfs[OPT_STRIP_VSS])!=get_int(oldconfs[OPT_STRIP_VSS]))
914 : {
915 0 : logp("strip_vss=%d (changed since last backup)\n",
916 0 : get_int(newconfs[OPT_STRIP_VSS]));
917 0 : ret=1; goto end;
918 : }
919 : ret=0;
920 : end:
921 5 : if(ret==1) logp("All files will be treated as new\n");
922 5 : confs_free(&oldconfs);
923 5 : confs_free(&newconfs);
924 5 : return ret;
925 : }
926 :
927 : // Maybe open the previous (current) manifest.
928 : // If the split_vss setting changed between the previous backup and the new
929 : // backup, do not open the previous manifest. This will have the effect of
930 : // making the client back up everything fresh. Need to do this, otherwise
931 : // toggling split_vss on and off will result in backups that do not work.
932 5 : static int maybe_open_previous_manifest(struct manio **manio,
933 : struct sdirs *sdirs, const char *incexc, struct conf **cconfs)
934 : {
935 5 : if(vss_opts_changed(sdirs, cconfs, incexc))
936 : return 0;
937 5 : return open_previous_manifest(manio, sdirs);
938 : }
939 :
940 135 : static int process_next_file_from_manios(struct asfd *asfd,
941 : struct dpth *dpth,
942 : struct sdirs *sdirs,
943 : struct manio **p1manio,
944 : struct manio **cmanio,
945 : struct manio *ucmanio,
946 : struct manio **hmanio,
947 : struct sbuf **p1b,
948 : struct sbuf *cb,
949 : struct conf **cconfs)
950 : {
951 135 : if(!(*p1b=sbuf_alloc(PROTO_1)))
952 : goto error;
953 135 : switch(manio_read(*p1manio, *p1b))
954 : {
955 : case 0:
956 : break;
957 : case 1:
958 5 : manio_close(p1manio);
959 5 : if(asfd->write_str(asfd, CMD_GEN, "backupphase2end"))
960 : goto error;
961 5 : sbuf_free(p1b);
962 5 : break;
963 : case -1:
964 : goto error;
965 : }
966 :
967 135 : if(!*cmanio)
968 : {
969 124 : if(!*p1b) return 0;
970 : // No old manifest, need to ask for a new file.
971 120 : switch(process_new(cconfs, *p1b, ucmanio))
972 : {
973 : case P_NEW:
974 : return 0;
975 : case P_DONE_NEW:
976 : goto p1b_done;
977 : default:
978 : goto error;
979 : }
980 : }
981 :
982 : // Have an old manifest, look for it there.
983 :
984 : // Might already have it, or be ahead in the old
985 : // manifest.
986 11 : if(cb->path.buf)
987 : {
988 2 : switch(maybe_process_file(asfd, dpth,
989 : sdirs, cb, *p1b, ucmanio, hmanio, cconfs))
990 : {
991 : case P_NEW:
992 : return 0;
993 : case P_CHANGED:
994 : // Free cb content for things like encrypted
995 : // files, which we only pretend are new.
996 0 : sbuf_free_content(cb);
997 0 : return 0;
998 : case P_DONE_NEW:
999 : goto p1b_done;
1000 : case P_DONE_CHANGED:
1001 : case P_DONE_UNCHANGED:
1002 0 : sbuf_free_content(cb);
1003 0 : goto p1b_done;
1004 : case P_DONE_DELETED:
1005 2 : sbuf_free_content(cb);
1006 2 : break;
1007 : case P_ERROR:
1008 : goto error;
1009 : }
1010 : }
1011 :
1012 : while(1)
1013 : {
1014 11 : sbuf_free_content(cb);
1015 11 : switch(manio_read(*cmanio, cb))
1016 : {
1017 : case 0: break;
1018 1 : case 1: manio_close(cmanio);
1019 1 : if(*p1b) switch(process_new(cconfs,
1020 : *p1b, ucmanio))
1021 : {
1022 : case P_NEW:
1023 : return 0;
1024 : case P_DONE_NEW:
1025 : goto p1b_done;
1026 : default:
1027 : goto error;
1028 : }
1029 : return 0;
1030 : case -1: goto error;
1031 : }
1032 10 : switch(maybe_process_file(asfd, dpth, sdirs,
1033 : cb, *p1b, ucmanio, hmanio, cconfs))
1034 : {
1035 : case P_NEW:
1036 : return 0;
1037 : case P_CHANGED:
1038 : // Free cb content for things like encrypted
1039 : // files, which we only pretend are new.
1040 2 : sbuf_free_content(cb);
1041 2 : return 0;
1042 : case P_DONE_NEW:
1043 : goto p1b_done;
1044 : case P_DONE_CHANGED:
1045 : case P_DONE_UNCHANGED:
1046 6 : sbuf_free_content(cb);
1047 6 : goto p1b_done;
1048 : case P_DONE_DELETED:
1049 0 : sbuf_free_content(cb);
1050 0 : continue;
1051 : case P_ERROR:
1052 : goto error;
1053 : }
1054 : }
1055 : error:
1056 : return -1;
1057 : p1b_done:
1058 74 : sbuf_free(p1b);
1059 74 : return 0;
1060 : }
1061 :
1062 10 : int backup_phase2_server_protocol1(struct async *as, struct sdirs *sdirs,
1063 : const char *incexc, int resume, struct conf **cconfs)
1064 : {
1065 10 : int ret=0;
1066 10 : man_off_t *p1pos=NULL;
1067 10 : struct manio *p1manio=NULL;
1068 10 : struct dpth *dpth=NULL;
1069 10 : char *deltmppath=NULL;
1070 10 : char *last_requested=NULL;
1071 10 : struct manio *chmanio=NULL; // changed data
1072 10 : struct manio *ucmanio=NULL; // unchanged data
1073 10 : struct manio *cmanio=NULL; // previous (current) manifest
1074 10 : struct manio *hmanio=NULL; // to look up deleted hardlinks
1075 10 : struct sbuf *cb=NULL; // file list in current manifest
1076 10 : struct sbuf *p1b=NULL; // file list from client
1077 10 : struct sbuf *rb=NULL; // receiving file from client
1078 10 : struct asfd *asfd=NULL;
1079 10 : int breaking=0;
1080 10 : int breakcount=0;
1081 10 : struct cntr *cntr=NULL;
1082 :
1083 10 : if(!as)
1084 : {
1085 1 : logp("async not provided to %s()\n", __func__);
1086 1 : goto error;
1087 : }
1088 9 : if(!sdirs)
1089 : {
1090 2 : logp("sdirs not provided to %s()\n", __func__);
1091 2 : goto error;
1092 : }
1093 7 : if(!cconfs)
1094 : {
1095 1 : logp("cconfs not provided to %s()\n", __func__);
1096 1 : goto error;
1097 : }
1098 6 : asfd=as->asfd;
1099 6 : if(!asfd)
1100 : {
1101 1 : logp("asfd not provided to %s()\n", __func__);
1102 1 : goto error;
1103 : }
1104 5 : cntr=get_cntr(cconfs);
1105 :
1106 5 : if(get_int(cconfs[OPT_BREAKPOINT])>=2000
1107 0 : && get_int(cconfs[OPT_BREAKPOINT])<3000)
1108 : {
1109 0 : breaking=get_int(cconfs[OPT_BREAKPOINT]);
1110 0 : breakcount=breaking-2000;
1111 : }
1112 :
1113 5 : logp("Begin phase2 (receive file data)\n");
1114 :
1115 5 : if(!(dpth=dpth_alloc())
1116 5 : || dpth_protocol1_init(dpth, sdirs->currentdata,
1117 : get_int(cconfs[OPT_MAX_STORAGE_SUBDIRS])))
1118 : goto error;
1119 :
1120 5 : if(maybe_open_previous_manifest(&cmanio, sdirs, incexc, cconfs))
1121 : goto error;
1122 : // If the first file that others hardlink to has been deleted, we will
1123 : // need to look through the previous manifest to find the information
1124 : // for that original file, in order to not have to copy all the data
1125 : // across again.
1126 5 : if(open_previous_manifest(&hmanio, sdirs))
1127 : goto error;
1128 :
1129 5 : if(get_int(cconfs[OPT_DIRECTORY_TREE]))
1130 : {
1131 : // Need to make sure we do not try to create a path that is
1132 : // too long.
1133 5 : if(build_path_w(sdirs->treepath)) goto error;
1134 5 : mkdir(sdirs->treepath, 0777);
1135 5 : treepathlen=strlen(sdirs->treepath);
1136 5 : if(init_fs_max(sdirs->treepath))
1137 : goto error;
1138 : }
1139 :
1140 5 : if(resume)
1141 : {
1142 0 : if(!(p1pos=do_resume(sdirs, dpth, cconfs)))
1143 : goto error;
1144 0 : if(cntr_send_sdirs(asfd, sdirs, cconfs, CNTR_STATUS_BACKUP))
1145 : goto error;
1146 : }
1147 :
1148 5 : if(!(p1manio=manio_open_phase1(sdirs->phase1data, "rb", PROTO_1))
1149 5 : || (resume && manio_seek(p1manio, p1pos)))
1150 : goto error;
1151 5 : if(!(cb=sbuf_alloc(PROTO_1))
1152 5 : || !(p1b=sbuf_alloc(PROTO_1))
1153 5 : || !(rb=sbuf_alloc(PROTO_1)))
1154 : goto error;
1155 :
1156 : // Unchanged and changed should now be truncated correctly, we just
1157 : // have to open them for appending.
1158 : // Data is not getting written to a compressed file.
1159 : // This is important for recovery if the power goes.
1160 5 : if(!(ucmanio=manio_open_phase2(sdirs->unchanged, "ab", PROTO_1))
1161 5 : || !(chmanio=manio_open_phase2(sdirs->changed, "ab", PROTO_1)))
1162 : goto error;
1163 :
1164 : while(1)
1165 : {
1166 921 : if(breaking && breakcount--==0)
1167 0 : return breakpoint(breaking, __func__);
1168 :
1169 921 : if(write_status(CNTR_STATUS_BACKUP,
1170 921 : rb->path.buf?rb->path.buf:"", cntr))
1171 : goto error;
1172 1842 : if(last_requested
1173 921 : || !p1manio
1174 16 : || asfd->writebuflen)
1175 : {
1176 905 : iobuf_free_content(asfd->rbuf);
1177 905 : if(asfd->as->read_write(asfd->as))
1178 : {
1179 0 : logp("error in %s\n", __func__);
1180 0 : goto error;
1181 : }
1182 :
1183 905 : if(asfd->rbuf->buf)
1184 85 : switch(do_stuff_to_receive(asfd, sdirs,
1185 : cconfs, rb, chmanio, dpth, &last_requested))
1186 : {
1187 : case STR_OK:
1188 : break;
1189 : case STR_FINISHED:
1190 : goto end;
1191 : case STR_ERROR:
1192 : goto error;
1193 : }
1194 : }
1195 :
1196 916 : if(p1b) switch(do_stuff_to_send(asfd, p1b, &last_requested))
1197 : {
1198 : case STS_OK:
1199 61 : sbuf_free(&p1b);
1200 61 : break;
1201 : case STS_BLOCKED:
1202 2 : continue;
1203 : case STS_ERROR:
1204 : goto error;
1205 : }
1206 :
1207 914 : if(p1manio
1208 135 : && process_next_file_from_manios(asfd, dpth, sdirs,
1209 : &p1manio, &cmanio, ucmanio, &hmanio, &p1b, cb, cconfs))
1210 : goto error;
1211 : }
1212 :
1213 : error:
1214 : ret=-1;
1215 : end:
1216 10 : if(manio_close(&chmanio))
1217 : {
1218 0 : logp("error closing %s in %s\n", sdirs->changed, __func__);
1219 0 : ret=-1;
1220 : }
1221 10 : if(manio_close(&ucmanio))
1222 : {
1223 0 : logp("error closing %s in %s\n", sdirs->unchanged, __func__);
1224 0 : ret=-1;
1225 : }
1226 10 : free_w(&deltmppath);
1227 10 : free_w(&last_requested);
1228 10 : sbuf_free(&cb);
1229 10 : sbuf_free(&p1b);
1230 10 : sbuf_free(&rb);
1231 10 : manio_close(&p1manio);
1232 10 : manio_close(&cmanio);
1233 10 : manio_close(&hmanio);
1234 10 : dpth_free(&dpth);
1235 10 : man_off_t_free(&p1pos);
1236 10 : if(!ret && sdirs)
1237 5 : unlink(sdirs->phase1data);
1238 :
1239 10 : logp("End phase2 (receive file data)\n");
1240 :
1241 10 : return ret;
1242 : }
|