Moved the flist_ndx_{push,pop}() routines from io.c into util.c.
[rsync/rsync.git] / receiver.c
... / ...
CommitLineData
1/*
2 * Routines only used by the receiving process.
3 *
4 * Copyright (C) 1996-2000 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-2008 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
20 */
21
22#include "rsync.h"
23#include "inums.h"
24
25extern int dry_run;
26extern int do_xfers;
27extern int am_server;
28extern int inc_recurse;
29extern int log_before_transfer;
30extern int stdout_format_has_i;
31extern int logfile_format_has_i;
32extern int csum_length;
33extern int read_batch;
34extern int write_batch;
35extern int batch_gen_fd;
36extern int protocol_version;
37extern int relative_paths;
38extern int preserve_hard_links;
39extern int preserve_perms;
40extern int preserve_xattrs;
41extern int basis_dir_cnt;
42extern int make_backups;
43extern int cleanup_got_literal;
44extern int remove_source_files;
45extern int append_mode;
46extern int sparse_files;
47extern int keep_partial;
48extern int checksum_len;
49extern int checksum_seed;
50extern int inplace;
51extern int delay_updates;
52extern mode_t orig_umask;
53extern struct stats stats;
54extern char *tmpdir;
55extern char *partial_dir;
56extern char *basis_dir[MAX_BASIS_DIRS+1];
57extern char sender_file_sum[MAX_DIGEST_LEN];
58extern struct file_list *cur_flist, *first_flist, *dir_flist;
59extern struct filter_list_struct daemon_filter_list;
60
61static struct bitbag *delayed_bits = NULL;
62static int phase = 0, redoing = 0;
63/* We're either updating the basis file or an identical copy: */
64static int updating_basis_or_equiv;
65
66/*
67 * get_tmpname() - create a tmp filename for a given filename
68 *
69 * If a tmpdir is defined, use that as the directory to
70 * put it in. Otherwise, the tmp filename is in the same
71 * directory as the given name. Note that there may be no
72 * directory at all in the given name!
73 *
74 * The tmp filename is basically the given filename with a
75 * dot prepended, and .XXXXXX appended (for mkstemp() to
76 * put its unique gunk in). Take care to not exceed
77 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
78 * the basename basically becomes 8 chars longer. In that
79 * case, the original name is shortened sufficiently to
80 * make it all fit.
81 *
82 * Of course, there's no real reason for the tmp name to
83 * look like the original, except to satisfy us humans.
84 * As long as it's unique, rsync will work.
85 */
86
87int get_tmpname(char *fnametmp, const char *fname)
88{
89 int maxname, added, length = 0;
90 const char *f;
91
92 if (tmpdir) {
93 /* Note: this can't overflow, so the return value is safe */
94 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
95 fnametmp[length++] = '/';
96 }
97
98 if ((f = strrchr(fname, '/')) != NULL) {
99 ++f;
100 if (!tmpdir) {
101 length = f - fname;
102 /* copy up to and including the slash */
103 strlcpy(fnametmp, fname, length + 1);
104 }
105 } else
106 f = fname;
107 fnametmp[length++] = '.';
108
109 /* The maxname value is bufsize, and includes space for the '\0'.
110 * (Note that NAME_MAX get -8 for the leading '.' above.) */
111 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
112
113 if (maxname < 1) {
114 rprintf(FERROR_XFER, "temporary filename too long: %s\n", fname);
115 fnametmp[0] = '\0';
116 return 0;
117 }
118
119 added = strlcpy(fnametmp + length, f, maxname);
120 if (added >= maxname)
121 added = maxname - 1;
122 memcpy(fnametmp + length + added, ".XXXXXX", 8);
123
124 return 1;
125}
126
127/* Opens a temporary file for writing.
128 * Success: Writes name into fnametmp, returns fd.
129 * Failure: Clobbers fnametmp, returns -1.
130 * Calling cleanup_set() is the caller's job. */
131int open_tmpfile(char *fnametmp, const char *fname, struct file_struct *file)
132{
133 int fd;
134
135 if (!get_tmpname(fnametmp, fname))
136 return -1;
137
138 /* We initially set the perms without the setuid/setgid bits or group
139 * access to ensure that there is no race condition. They will be
140 * correctly updated after the right owner and group info is set.
141 * (Thanks to snabb@epipe.fi for pointing this out.) */
142 fd = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
143
144#if 0
145 /* In most cases parent directories will already exist because their
146 * information should have been previously transferred, but that may
147 * not be the case with -R */
148 if (fd == -1 && relative_paths && errno == ENOENT
149 && create_directory_path(fnametmp) == 0) {
150 /* Get back to name with XXXXXX in it. */
151 get_tmpname(fnametmp, fname);
152 fd = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
153 }
154#endif
155
156 if (fd == -1) {
157 rsyserr(FERROR_XFER, errno, "mkstemp %s failed",
158 full_fname(fnametmp));
159 return -1;
160 }
161
162 return fd;
163}
164
165static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
166 const char *fname, int fd, OFF_T total_size)
167{
168 static char file_sum1[MAX_DIGEST_LEN];
169 struct map_struct *mapbuf;
170 struct sum_struct sum;
171 int32 len;
172 OFF_T offset = 0;
173 OFF_T offset2;
174 char *data;
175 int32 i;
176 char *map = NULL;
177
178 read_sum_head(f_in, &sum);
179
180 if (fd_r >= 0 && size_r > 0) {
181 int32 read_size = MAX(sum.blength * 2, 16*1024);
182 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
183 if (DEBUG_GTE(DELTASUM, 2)) {
184 rprintf(FINFO, "recv mapped %s of size %s\n",
185 fname_r, big_num(size_r));
186 }
187 } else
188 mapbuf = NULL;
189
190 sum_init(checksum_seed);
191
192 if (append_mode > 0) {
193 OFF_T j;
194 sum.flength = (OFF_T)sum.count * sum.blength;
195 if (sum.remainder)
196 sum.flength -= sum.blength - sum.remainder;
197 if (append_mode == 2) {
198 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
199 if (INFO_GTE(PROGRESS, 1))
200 show_progress(offset, total_size);
201 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
202 CHUNK_SIZE);
203 offset = j;
204 }
205 if (offset < sum.flength) {
206 int32 len = (int32)(sum.flength - offset);
207 if (INFO_GTE(PROGRESS, 1))
208 show_progress(offset, total_size);
209 sum_update(map_ptr(mapbuf, offset, len), len);
210 }
211 }
212 offset = sum.flength;
213 if (fd != -1 && (j = do_lseek(fd, offset, SEEK_SET)) != offset) {
214 rsyserr(FERROR_XFER, errno, "lseek of %s returned %s, not %s",
215 full_fname(fname), big_num(j), big_num(offset));
216 exit_cleanup(RERR_FILEIO);
217 }
218 }
219
220 while ((i = recv_token(f_in, &data)) != 0) {
221 if (INFO_GTE(PROGRESS, 1))
222 show_progress(offset, total_size);
223
224 if (i > 0) {
225 if (DEBUG_GTE(DELTASUM, 3)) {
226 rprintf(FINFO,"data recv %d at %s\n",
227 i, big_num(offset));
228 }
229
230 stats.literal_data += i;
231 cleanup_got_literal = 1;
232
233 sum_update(data, i);
234
235 if (fd != -1 && write_file(fd,data,i) != i)
236 goto report_write_error;
237 offset += i;
238 continue;
239 }
240
241 i = -(i+1);
242 offset2 = i * (OFF_T)sum.blength;
243 len = sum.blength;
244 if (i == (int)sum.count-1 && sum.remainder != 0)
245 len = sum.remainder;
246
247 stats.matched_data += len;
248
249 if (DEBUG_GTE(DELTASUM, 3)) {
250 rprintf(FINFO,
251 "chunk[%d] of size %ld at %s offset=%s\n",
252 i, (long)len, big_num(offset2), big_num(offset));
253 }
254
255 if (mapbuf) {
256 map = map_ptr(mapbuf,offset2,len);
257
258 see_token(map, len);
259 sum_update(map, len);
260 }
261
262 if (updating_basis_or_equiv) {
263 if (offset == offset2 && fd != -1) {
264 OFF_T pos;
265 if (flush_write_file(fd) < 0)
266 goto report_write_error;
267 offset += len;
268 if ((pos = do_lseek(fd, len, SEEK_CUR)) != offset) {
269 rsyserr(FERROR_XFER, errno,
270 "lseek of %s returned %s, not %s",
271 full_fname(fname),
272 big_num(pos), big_num(offset));
273 exit_cleanup(RERR_FILEIO);
274 }
275 continue;
276 }
277 }
278 if (fd != -1 && map && write_file(fd, map, len) != (int)len)
279 goto report_write_error;
280 offset += len;
281 }
282
283 if (flush_write_file(fd) < 0)
284 goto report_write_error;
285
286#ifdef HAVE_FTRUNCATE
287 if (inplace && fd != -1)
288 ftruncate(fd, offset);
289#endif
290
291 if (INFO_GTE(PROGRESS, 1))
292 end_progress(total_size);
293
294 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
295 report_write_error:
296 rsyserr(FERROR_XFER, errno, "write failed on %s",
297 full_fname(fname));
298 exit_cleanup(RERR_FILEIO);
299 }
300
301 if (sum_end(file_sum1) != checksum_len)
302 overflow_exit("checksum_len"); /* Impossible... */
303
304 if (mapbuf)
305 unmap_file(mapbuf);
306
307 read_buf(f_in, sender_file_sum, checksum_len);
308 if (DEBUG_GTE(DELTASUM, 2))
309 rprintf(FINFO,"got file_sum\n");
310 if (fd != -1 && memcmp(file_sum1, sender_file_sum, checksum_len) != 0)
311 return 0;
312 return 1;
313}
314
315
316static void discard_receive_data(int f_in, OFF_T length)
317{
318 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
319}
320
321static void handle_delayed_updates(char *local_name)
322{
323 char *fname, *partialptr;
324 int ndx;
325
326 for (ndx = -1; (ndx = bitbag_next_bit(delayed_bits, ndx)) >= 0; ) {
327 struct file_struct *file = cur_flist->files[ndx];
328 fname = local_name ? local_name : f_name(file, NULL);
329 if ((partialptr = partial_dir_fname(fname)) != NULL) {
330 if (make_backups > 0 && !make_backup(fname))
331 continue;
332 if (DEBUG_GTE(RECV, 1)) {
333 rprintf(FINFO, "renaming %s to %s\n",
334 partialptr, fname);
335 }
336 /* We don't use robust_rename() here because the
337 * partial-dir must be on the same drive. */
338 if (do_rename(partialptr, fname) < 0) {
339 rsyserr(FERROR_XFER, errno,
340 "rename failed for %s (from %s)",
341 full_fname(fname), partialptr);
342 } else {
343 if (remove_source_files
344 || (preserve_hard_links && F_IS_HLINKED(file)))
345 send_msg_int(MSG_SUCCESS, ndx);
346 handle_partial_dir(partialptr, PDIR_DELETE);
347 }
348 }
349 }
350}
351
352static int get_next_gen_ndx(int fd, int next_gen_ndx, int desired_ndx)
353{
354 static int batch_eof = 0;
355
356 while (next_gen_ndx < desired_ndx) {
357 if (next_gen_ndx >= 0) {
358 struct file_struct *file = cur_flist->files[next_gen_ndx];
359 rprintf(FERROR_XFER,
360 "(No batched update for%s \"%s\")\n",
361 file->flags & FLAG_FILE_SENT ? " resend of" : "",
362 f_name(file, NULL));
363 }
364 next_gen_ndx = batch_eof ? -1 : read_int(fd);
365 if (next_gen_ndx == -1) {
366 if (inc_recurse)
367 next_gen_ndx = first_flist->prev->used + first_flist->prev->ndx_start;
368 else
369 next_gen_ndx = cur_flist->used;
370 batch_eof = 1;
371 }
372 }
373
374 return next_gen_ndx;
375}
376
377/**
378 * main routine for receiver process.
379 *
380 * Receiver process runs on the same host as the generator process. */
381int recv_files(int f_in, char *local_name)
382{
383 int next_gen_ndx = -1;
384 int fd1,fd2;
385 STRUCT_STAT st;
386 int iflags, xlen;
387 char *fname, fbuf[MAXPATHLEN];
388 char xname[MAXPATHLEN];
389 char fnametmp[MAXPATHLEN];
390 char *fnamecmp, *partialptr;
391 char fnamecmpbuf[MAXPATHLEN];
392 uchar fnamecmp_type;
393 struct file_struct *file;
394 struct stats initial_stats;
395 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
396 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
397 int max_phase = protocol_version >= 29 ? 2 : 1;
398 int dflt_perms = (ACCESSPERMS & ~orig_umask);
399#ifdef SUPPORT_ACLS
400 const char *parent_dirname = "";
401#endif
402 int ndx, recv_ok;
403
404 if (DEBUG_GTE(RECV, 1))
405 rprintf(FINFO, "recv_files(%d) starting\n", cur_flist->used);
406
407 if (delay_updates)
408 delayed_bits = bitbag_create(cur_flist->used + 1);
409
410 while (1) {
411 cleanup_disable();
412
413 /* This call also sets cur_flist. */
414 ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
415 xname, &xlen);
416 if (ndx == NDX_DONE) {
417 if (!am_server && INFO_GTE(PROGRESS, 2) && cur_flist) {
418 set_current_file_index(NULL, 0);
419 end_progress(0);
420 }
421 if (inc_recurse && first_flist) {
422 flist_free(first_flist);
423 if (first_flist)
424 continue;
425 }
426 if (read_batch && cur_flist) {
427 int high = inc_recurse
428 ? first_flist->prev->used + first_flist->prev->ndx_start
429 : cur_flist->used;
430 get_next_gen_ndx(batch_gen_fd, next_gen_ndx, high);
431 next_gen_ndx = -1;
432 }
433 if (++phase > max_phase)
434 break;
435 if (DEBUG_GTE(RECV, 1))
436 rprintf(FINFO, "recv_files phase=%d\n", phase);
437 if (phase == 2 && delay_updates)
438 handle_delayed_updates(local_name);
439 send_msg(MSG_DONE, "", 0, 0);
440 continue;
441 }
442
443 if (ndx - cur_flist->ndx_start >= 0)
444 file = cur_flist->files[ndx - cur_flist->ndx_start];
445 else
446 file = dir_flist->files[cur_flist->parent_ndx];
447 fname = local_name ? local_name : f_name(file, fbuf);
448
449 if (DEBUG_GTE(RECV, 1))
450 rprintf(FINFO, "recv_files(%s)\n", fname);
451
452#ifdef SUPPORT_XATTRS
453 if (iflags & ITEM_REPORT_XATTR && !dry_run)
454 recv_xattr_request(file, f_in);
455#endif
456
457 if (!(iflags & ITEM_TRANSFER)) {
458 maybe_log_item(file, iflags, itemizing, xname);
459#ifdef SUPPORT_XATTRS
460 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
461 set_file_attrs(fname, file, NULL, fname, 0);
462#endif
463 continue;
464 }
465 if (phase == 2) {
466 rprintf(FERROR,
467 "got transfer request in phase 2 [%s]\n",
468 who_am_i());
469 exit_cleanup(RERR_PROTOCOL);
470 }
471
472 if (file->flags & FLAG_FILE_SENT) {
473 if (csum_length == SHORT_SUM_LENGTH) {
474 if (keep_partial && !partial_dir)
475 make_backups = -make_backups; /* prevents double backup */
476 if (append_mode)
477 sparse_files = -sparse_files;
478 append_mode = -append_mode;
479 csum_length = SUM_LENGTH;
480 redoing = 1;
481 }
482 } else {
483 if (csum_length != SHORT_SUM_LENGTH) {
484 if (keep_partial && !partial_dir)
485 make_backups = -make_backups;
486 if (append_mode)
487 sparse_files = -sparse_files;
488 append_mode = -append_mode;
489 csum_length = SHORT_SUM_LENGTH;
490 redoing = 0;
491 }
492 }
493
494 if (!am_server && INFO_GTE(PROGRESS, 1))
495 set_current_file_index(file, ndx);
496 stats.num_transferred_files++;
497 stats.total_transferred_size += F_LENGTH(file);
498
499 cleanup_got_literal = 0;
500
501 if (daemon_filter_list.head
502 && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0) {
503 rprintf(FERROR, "attempt to hack rsync failed.\n");
504 exit_cleanup(RERR_PROTOCOL);
505 }
506
507 if (!do_xfers) { /* log the transfer */
508 log_item(FCLIENT, file, &stats, iflags, NULL);
509 if (read_batch)
510 discard_receive_data(f_in, F_LENGTH(file));
511 continue;
512 }
513 if (write_batch < 0) {
514 log_item(FCLIENT, file, &stats, iflags, NULL);
515 if (!am_server)
516 discard_receive_data(f_in, F_LENGTH(file));
517 continue;
518 }
519
520 if (read_batch) {
521 next_gen_ndx = get_next_gen_ndx(batch_gen_fd, next_gen_ndx, ndx);
522 if (ndx < next_gen_ndx) {
523 rprintf(FINFO,
524 "(Skipping batched update for \"%s\")\n",
525 fname);
526 discard_receive_data(f_in, F_LENGTH(file));
527 continue;
528 }
529 next_gen_ndx = -1;
530 }
531
532 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
533
534 if (protocol_version >= 29) {
535 switch (fnamecmp_type) {
536 case FNAMECMP_FNAME:
537 fnamecmp = fname;
538 break;
539 case FNAMECMP_PARTIAL_DIR:
540 fnamecmp = partialptr;
541 break;
542 case FNAMECMP_BACKUP:
543 fnamecmp = get_backup_name(fname);
544 break;
545 case FNAMECMP_FUZZY:
546 if (file->dirname) {
547 pathjoin(fnamecmpbuf, MAXPATHLEN,
548 file->dirname, xname);
549 fnamecmp = fnamecmpbuf;
550 } else
551 fnamecmp = xname;
552 break;
553 default:
554 if (fnamecmp_type >= basis_dir_cnt) {
555 rprintf(FERROR,
556 "invalid basis_dir index: %d.\n",
557 fnamecmp_type);
558 exit_cleanup(RERR_PROTOCOL);
559 }
560 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
561 basis_dir[fnamecmp_type], fname);
562 fnamecmp = fnamecmpbuf;
563 break;
564 }
565 if (!fnamecmp || (daemon_filter_list.head
566 && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0)) {
567 fnamecmp = fname;
568 fnamecmp_type = FNAMECMP_FNAME;
569 }
570 } else {
571 /* Reminder: --inplace && --partial-dir are never
572 * enabled at the same time. */
573 if (inplace && make_backups > 0) {
574 if (!(fnamecmp = get_backup_name(fname)))
575 fnamecmp = fname;
576 else
577 fnamecmp_type = FNAMECMP_BACKUP;
578 } else if (partial_dir && partialptr)
579 fnamecmp = partialptr;
580 else
581 fnamecmp = fname;
582 }
583
584 initial_stats = stats;
585
586 /* open the file */
587 fd1 = do_open(fnamecmp, O_RDONLY, 0);
588
589 if (fd1 == -1 && protocol_version < 29) {
590 if (fnamecmp != fname) {
591 fnamecmp = fname;
592 fd1 = do_open(fnamecmp, O_RDONLY, 0);
593 }
594
595 if (fd1 == -1 && basis_dir[0]) {
596 /* pre-29 allowed only one alternate basis */
597 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
598 basis_dir[0], fname);
599 fnamecmp = fnamecmpbuf;
600 fd1 = do_open(fnamecmp, O_RDONLY, 0);
601 }
602 }
603
604 updating_basis_or_equiv = inplace
605 && (fnamecmp == fname || fnamecmp_type == FNAMECMP_BACKUP);
606
607 if (fd1 == -1) {
608 st.st_mode = 0;
609 st.st_size = 0;
610 } else if (do_fstat(fd1,&st) != 0) {
611 rsyserr(FERROR_XFER, errno, "fstat %s failed",
612 full_fname(fnamecmp));
613 discard_receive_data(f_in, F_LENGTH(file));
614 close(fd1);
615 if (inc_recurse)
616 send_msg_int(MSG_NO_SEND, ndx);
617 continue;
618 }
619
620 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
621 /* this special handling for directories
622 * wouldn't be necessary if robust_rename()
623 * and the underlying robust_unlink could cope
624 * with directories
625 */
626 rprintf(FERROR_XFER, "recv_files: %s is a directory\n",
627 full_fname(fnamecmp));
628 discard_receive_data(f_in, F_LENGTH(file));
629 close(fd1);
630 if (inc_recurse)
631 send_msg_int(MSG_NO_SEND, ndx);
632 continue;
633 }
634
635 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
636 close(fd1);
637 fd1 = -1;
638 }
639
640 /* If we're not preserving permissions, change the file-list's
641 * mode based on the local permissions and some heuristics. */
642 if (!preserve_perms) {
643 int exists = fd1 != -1;
644#ifdef SUPPORT_ACLS
645 const char *dn = file->dirname ? file->dirname : ".";
646 if (parent_dirname != dn
647 && strcmp(parent_dirname, dn) != 0) {
648 dflt_perms = default_perms_for_dir(dn);
649 parent_dirname = dn;
650 }
651#endif
652 file->mode = dest_mode(file->mode, st.st_mode,
653 dflt_perms, exists);
654 }
655
656 /* We now check to see if we are writing the file "inplace" */
657 if (inplace) {
658 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600);
659 if (fd2 == -1) {
660 rsyserr(FERROR_XFER, errno, "open %s failed",
661 full_fname(fname));
662 }
663 } else {
664 fd2 = open_tmpfile(fnametmp, fname, file);
665 if (fd2 != -1)
666 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
667 }
668
669 if (fd2 == -1) {
670 discard_receive_data(f_in, F_LENGTH(file));
671 if (fd1 != -1)
672 close(fd1);
673 if (inc_recurse)
674 send_msg_int(MSG_NO_SEND, ndx);
675 continue;
676 }
677
678 /* log the transfer */
679 if (log_before_transfer)
680 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
681 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
682 rprintf(FINFO, "%s\n", fname);
683
684 /* recv file data */
685 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
686 fname, fd2, F_LENGTH(file));
687
688 log_item(log_code, file, &initial_stats, iflags, NULL);
689
690 if (fd1 != -1)
691 close(fd1);
692 if (close(fd2) < 0) {
693 rsyserr(FERROR, errno, "close failed on %s",
694 full_fname(fnametmp));
695 exit_cleanup(RERR_FILEIO);
696 }
697
698 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
699 if (partialptr == fname)
700 partialptr = NULL;
701 if (!finish_transfer(fname, fnametmp, fnamecmp,
702 partialptr, file, recv_ok, 1))
703 recv_ok = -1;
704 else if (fnamecmp == partialptr) {
705 do_unlink(partialptr);
706 handle_partial_dir(partialptr, PDIR_DELETE);
707 }
708 } else if (keep_partial && partialptr) {
709 if (!handle_partial_dir(partialptr, PDIR_CREATE)) {
710 rprintf(FERROR,
711 "Unable to create partial-dir for %s -- discarding %s.\n",
712 local_name ? local_name : f_name(file, NULL),
713 recv_ok ? "completed file" : "partial file");
714 do_unlink(fnametmp);
715 recv_ok = -1;
716 } else if (!finish_transfer(partialptr, fnametmp, fnamecmp, NULL,
717 file, recv_ok, !partial_dir))
718 recv_ok = -1;
719 else if (delay_updates && recv_ok) {
720 bitbag_set_bit(delayed_bits, ndx);
721 recv_ok = 2;
722 } else
723 partialptr = NULL;
724 } else
725 do_unlink(fnametmp);
726
727 cleanup_disable();
728
729 switch (recv_ok) {
730 case 2:
731 break;
732 case 1:
733 if (remove_source_files || inc_recurse
734 || (preserve_hard_links && F_IS_HLINKED(file)))
735 send_msg_int(MSG_SUCCESS, ndx);
736 break;
737 case 0: {
738 enum logcode msgtype = redoing ? FERROR_XFER : FWARNING;
739 if (msgtype == FERROR_XFER || INFO_GTE(NAME, 1)) {
740 char *errstr, *redostr, *keptstr;
741 if (!(keep_partial && partialptr) && !inplace)
742 keptstr = "discarded";
743 else if (partial_dir)
744 keptstr = "put into partial-dir";
745 else
746 keptstr = "retained";
747 if (msgtype == FERROR_XFER) {
748 errstr = "ERROR";
749 redostr = "";
750 } else {
751 errstr = "WARNING";
752 redostr = read_batch ? " (may try again)"
753 : " (will try again)";
754 }
755 rprintf(msgtype,
756 "%s: %s failed verification -- update %s%s.\n",
757 errstr, local_name ? f_name(file, NULL) : fname,
758 keptstr, redostr);
759 }
760 if (!redoing) {
761 send_msg_int(MSG_REDO, ndx);
762 file->flags |= FLAG_FILE_SENT;
763 } else if (inc_recurse)
764 send_msg_int(MSG_NO_SEND, ndx);
765 break;
766 }
767 case -1:
768 if (inc_recurse)
769 send_msg_int(MSG_NO_SEND, ndx);
770 break;
771 }
772 }
773 if (make_backups < 0)
774 make_backups = -make_backups;
775
776 if (phase == 2 && delay_updates) /* for protocol_version < 29 */
777 handle_delayed_updates(local_name);
778
779 if (DEBUG_GTE(RECV, 1))
780 rprintf(FINFO,"recv_files finished\n");
781
782 return 0;
783}