2 * Routines only used by the receiving process.
4 * Copyright (C) 1996-2000 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-2009 Wayne Davison
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.
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.
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.
29 extern int inc_recurse;
30 extern int log_before_transfer;
31 extern int stdout_format_has_i;
32 extern int logfile_format_has_i;
33 extern int csum_length;
34 extern int read_batch;
35 extern int write_batch;
36 extern int batch_gen_fd;
37 extern int protocol_version;
38 extern int relative_paths;
39 extern int preserve_hard_links;
40 extern int preserve_perms;
41 extern int preserve_xattrs;
42 extern int basis_dir_cnt;
43 extern int make_backups;
44 extern int cleanup_got_literal;
45 extern int remove_source_files;
46 extern int append_mode;
47 extern int sparse_files;
48 extern int preallocate_files;
49 extern int keep_partial;
50 extern int checksum_len;
51 extern int checksum_seed;
53 extern int allowed_lull;
54 extern int delay_updates;
55 extern mode_t orig_umask;
56 extern struct stats stats;
58 extern char *partial_dir;
59 extern char *basis_dir[MAX_BASIS_DIRS+1];
60 extern char sender_file_sum[MAX_DIGEST_LEN];
61 extern struct file_list *cur_flist, *first_flist, *dir_flist;
62 extern filter_rule_list daemon_filter_list;
64 static struct bitbag *delayed_bits = NULL;
65 static int phase = 0, redoing = 0;
66 static flist_ndx_list batch_redo_list;
67 /* We're either updating the basis file or an identical copy: */
68 static int updating_basis_or_equiv;
70 #define TMPNAME_SUFFIX ".XXXXXX"
71 #define TMPNAME_SUFFIX_LEN ((int)sizeof TMPNAME_SUFFIX - 1)
72 #define MAX_UNIQUE_NUMBER 999999
73 #define MAX_UNIQUE_LOOP 100
75 /* get_tmpname() - create a tmp filename for a given filename
77 * If a tmpdir is defined, use that as the directory to put it in. Otherwise,
78 * the tmp filename is in the same directory as the given name. Note that
79 * there may be no directory at all in the given name!
81 * The tmp filename is basically the given filename with a dot prepended, and
82 * .XXXXXX appended (for mkstemp() to put its unique gunk in). We take care
83 * to not exceed either the MAXPATHLEN or NAME_MAX, especially the last, as
84 * the basename basically becomes 8 characters longer. In such a case, the
85 * original name is shortened sufficiently to make it all fit.
87 * If the make_unique arg is True, the XXXXXX string is replaced with a unique
88 * string that doesn't exist at the time of the check. This is intended to be
89 * used for creating hard links, symlinks, devices, and special files, since
90 * normal files should be handled by mkstemp() for safety.
92 * Of course, the only reason the file is based on the original name is to
93 * make it easier to figure out what purpose a temp file is serving when a
94 * transfer is in progress. */
95 int get_tmpname(char *fnametmp, const char *fname, BOOL make_unique)
97 int maxname, added, length = 0;
102 /* Note: this can't overflow, so the return value is safe */
103 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
104 fnametmp[length++] = '/';
107 if ((f = strrchr(fname, '/')) != NULL) {
111 /* copy up to and including the slash */
112 strlcpy(fnametmp, fname, length + 1);
116 fnametmp[length++] = '.';
118 /* The maxname value is bufsize, and includes space for the '\0'.
119 * NAME_MAX needs an extra -1 for the name's leading dot. */
120 maxname = MIN(MAXPATHLEN - length - TMPNAME_SUFFIX_LEN,
121 NAME_MAX - 1 - TMPNAME_SUFFIX_LEN);
124 rprintf(FERROR_XFER, "temporary filename too long: %s\n", fname);
129 added = strlcpy(fnametmp + length, f, maxname);
130 if (added >= maxname)
132 suf = fnametmp + length + added;
134 /* Trim any dangling high-bit chars if the first-trimmed char (if any) is
135 * also a high-bit char, just in case we cut into a multi-byte sequence.
136 * We are guaranteed to stop because of the leading '.' we added. */
137 if ((int)f[added] & 0x80) {
138 while ((int)suf[-1] & 0x80)
143 static unsigned counter_limit;
146 if (!counter_limit) {
147 counter_limit = (unsigned)getpid() + MAX_UNIQUE_LOOP;
148 if (counter_limit > MAX_UNIQUE_NUMBER || counter_limit < MAX_UNIQUE_LOOP)
149 counter_limit = MAX_UNIQUE_LOOP;
151 counter = counter_limit - MAX_UNIQUE_LOOP;
153 /* This doesn't have to be very good because we don't need
154 * to worry about someone trying to guess the values: all
155 * a conflict will do is cause a device, special file, hard
156 * link, or symlink to fail to be created. Also: avoid
157 * using mktemp() due to gcc's annoying warning. */
159 snprintf(suf, TMPNAME_SUFFIX_LEN+1, ".%d", counter);
160 if (access(fnametmp, 0) < 0)
162 if (++counter >= counter_limit)
166 memcpy(suf, TMPNAME_SUFFIX, TMPNAME_SUFFIX_LEN+1);
171 /* Opens a temporary file for writing.
172 * Success: Writes name into fnametmp, returns fd.
173 * Failure: Clobbers fnametmp, returns -1.
174 * Calling cleanup_set() is the caller's job. */
175 int open_tmpfile(char *fnametmp, const char *fname, struct file_struct *file)
180 if (!get_tmpname(fnametmp, fname, False))
184 /* For --fake-super, the file must be useable by the copying
185 * user, just like it would be for root. */
186 added_perms = S_IRUSR|S_IWUSR;
188 /* For a normal copy, we need to be able to tweak things like xattrs. */
189 added_perms = S_IWUSR;
192 /* We initially set the perms without the setuid/setgid bits or group
193 * access to ensure that there is no race condition. They will be
194 * correctly updated after the right owner and group info is set.
195 * (Thanks to snabb@epipe.fi for pointing this out.) */
196 fd = do_mkstemp(fnametmp, (file->mode|added_perms) & INITACCESSPERMS);
199 /* In most cases parent directories will already exist because their
200 * information should have been previously transferred, but that may
201 * not be the case with -R */
202 if (fd == -1 && relative_paths && errno == ENOENT
203 && make_path(fnametmp, MKP_SKIP_SLASH | MKP_DROP_NAME) == 0) {
204 /* Get back to name with XXXXXX in it. */
205 get_tmpname(fnametmp, fname, False);
206 fd = do_mkstemp(fnametmp, (file->mode|added_perms) & INITACCESSPERMS);
211 rsyserr(FERROR_XFER, errno, "mkstemp %s failed",
212 full_fname(fnametmp));
219 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
220 const char *fname, int fd, OFF_T total_size)
222 static char file_sum1[MAX_DIGEST_LEN];
223 struct map_struct *mapbuf;
224 struct sum_struct sum;
231 #ifdef SUPPORT_PREALLOCATION
232 #ifdef PREALLOCATE_NEEDS_TRUNCATE
233 OFF_T preallocated_len = 0;
236 if (preallocate_files && fd != -1 && total_size > 0) {
237 /* Try to preallocate enough space for file's eventual length. Can
238 * reduce fragmentation on filesystems like ext4, xfs, and NTFS. */
239 if (do_fallocate(fd, 0, total_size) == 0) {
240 #ifdef PREALLOCATE_NEEDS_TRUNCATE
241 preallocated_len = total_size;
244 rsyserr(FWARNING, errno, "do_fallocate %s", full_fname(fname));
248 read_sum_head(f_in, &sum);
250 if (fd_r >= 0 && size_r > 0) {
251 int32 read_size = MAX(sum.blength * 2, 16*1024);
252 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
253 if (DEBUG_GTE(DELTASUM, 2)) {
254 rprintf(FINFO, "recv mapped %s of size %s\n",
255 fname_r, big_num(size_r));
260 sum_init(checksum_seed);
262 if (append_mode > 0) {
264 sum.flength = (OFF_T)sum.count * sum.blength;
266 sum.flength -= sum.blength - sum.remainder;
267 if (append_mode == 2 && mapbuf) {
268 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
269 if (INFO_GTE(PROGRESS, 1))
270 show_progress(offset, total_size);
271 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
275 if (offset < sum.flength) {
276 int32 len = (int32)(sum.flength - offset);
277 if (INFO_GTE(PROGRESS, 1))
278 show_progress(offset, total_size);
279 sum_update(map_ptr(mapbuf, offset, len), len);
282 offset = sum.flength;
283 if (fd != -1 && (j = do_lseek(fd, offset, SEEK_SET)) != offset) {
284 rsyserr(FERROR_XFER, errno, "lseek of %s returned %s, not %s",
285 full_fname(fname), big_num(j), big_num(offset));
286 exit_cleanup(RERR_FILEIO);
290 while ((i = recv_token(f_in, &data)) != 0) {
291 if (INFO_GTE(PROGRESS, 1))
292 show_progress(offset, total_size);
295 maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH | MSK_ACTIVE_RECEIVER);
298 if (DEBUG_GTE(DELTASUM, 3)) {
299 rprintf(FINFO,"data recv %d at %s\n",
303 stats.literal_data += i;
304 cleanup_got_literal = 1;
308 if (fd != -1 && write_file(fd,data,i) != i)
309 goto report_write_error;
315 offset2 = i * (OFF_T)sum.blength;
317 if (i == (int)sum.count-1 && sum.remainder != 0)
320 stats.matched_data += len;
322 if (DEBUG_GTE(DELTASUM, 3)) {
324 "chunk[%d] of size %ld at %s offset=%s%s\n",
325 i, (long)len, big_num(offset2), big_num(offset),
326 updating_basis_or_equiv && offset == offset2 ? " (seek)" : "");
330 map = map_ptr(mapbuf,offset2,len);
333 sum_update(map, len);
336 if (updating_basis_or_equiv) {
337 if (offset == offset2 && fd != -1) {
339 if (flush_write_file(fd) < 0)
340 goto report_write_error;
342 if ((pos = do_lseek(fd, len, SEEK_CUR)) != offset) {
343 rsyserr(FERROR_XFER, errno,
344 "lseek of %s returned %s, not %s",
346 big_num(pos), big_num(offset));
347 exit_cleanup(RERR_FILEIO);
352 if (fd != -1 && map && write_file(fd, map, len) != (int)len)
353 goto report_write_error;
357 if (flush_write_file(fd) < 0)
358 goto report_write_error;
360 #ifdef HAVE_FTRUNCATE
361 /* inplace: New data could be shorter than old data.
362 * preallocate_files: total_size could have been an overestimate.
363 * Cut off any extra preallocated zeros from dest file. */
365 #ifdef PREALLOCATE_NEEDS_TRUNCATE
366 || preallocated_len > offset
368 ) && fd != -1 && do_ftruncate(fd, offset) < 0) {
369 rsyserr(FERROR_XFER, errno, "ftruncate failed on %s",
374 if (INFO_GTE(PROGRESS, 1))
375 end_progress(total_size);
377 if (fd != -1 && offset > 0 && sparse_end(fd, offset) != 0) {
379 rsyserr(FERROR_XFER, errno, "write failed on %s",
381 exit_cleanup(RERR_FILEIO);
384 if (sum_end(file_sum1) != checksum_len)
385 overflow_exit("checksum_len"); /* Impossible... */
390 read_buf(f_in, sender_file_sum, checksum_len);
391 if (DEBUG_GTE(DELTASUM, 2))
392 rprintf(FINFO,"got file_sum\n");
393 if (fd != -1 && memcmp(file_sum1, sender_file_sum, checksum_len) != 0)
399 static void discard_receive_data(int f_in, OFF_T length)
401 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
404 static void handle_delayed_updates(char *local_name)
406 char *fname, *partialptr;
409 for (ndx = -1; (ndx = bitbag_next_bit(delayed_bits, ndx)) >= 0; ) {
410 struct file_struct *file = cur_flist->files[ndx];
411 fname = local_name ? local_name : f_name(file, NULL);
412 if ((partialptr = partial_dir_fname(fname)) != NULL) {
413 if (make_backups > 0 && !make_backup(fname, False))
415 if (DEBUG_GTE(RECV, 1)) {
416 rprintf(FINFO, "renaming %s to %s\n",
419 /* We don't use robust_rename() here because the
420 * partial-dir must be on the same drive. */
421 if (do_rename(partialptr, fname) < 0) {
422 rsyserr(FERROR_XFER, errno,
423 "rename failed for %s (from %s)",
424 full_fname(fname), partialptr);
426 if (remove_source_files
427 || (preserve_hard_links && F_IS_HLINKED(file)))
428 send_msg_int(MSG_SUCCESS, ndx);
429 handle_partial_dir(partialptr, PDIR_DELETE);
435 static void no_batched_update(int ndx, BOOL is_redo)
437 struct file_list *flist = flist_for_ndx(ndx, "no_batched_update");
438 struct file_struct *file = flist->files[ndx - flist->ndx_start];
440 rprintf(FERROR_XFER, "(No batched update for%s \"%s\")\n",
441 is_redo ? " resend of" : "", f_name(file, NULL));
443 if (inc_recurse && !dry_run)
444 send_msg_int(MSG_NO_SEND, ndx);
447 static int we_want_redo(int desired_ndx)
449 static int redo_ndx = -1;
451 while (redo_ndx < desired_ndx) {
453 no_batched_update(redo_ndx, True);
454 if ((redo_ndx = flist_ndx_pop(&batch_redo_list)) < 0)
458 if (redo_ndx == desired_ndx) {
466 static int gen_wants_ndx(int desired_ndx, int flist_num)
468 static int next_ndx = -1;
469 static int done_cnt = 0;
470 static BOOL got_eof = False;
475 /* TODO: integrate gen-reading I/O into perform_io() so this is not needed? */
476 io_flush(FULL_FLUSH);
478 while (next_ndx < desired_ndx) {
479 if (inc_recurse && flist_num <= done_cnt)
482 no_batched_update(next_ndx, False);
483 if ((next_ndx = read_int(batch_gen_fd)) < 0) {
493 if (next_ndx == desired_ndx) {
502 * main routine for receiver process.
504 * Receiver process runs on the same host as the generator process. */
505 int recv_files(int f_in, int f_out, char *local_name)
510 char *fname, fbuf[MAXPATHLEN];
511 char xname[MAXPATHLEN];
512 char fnametmp[MAXPATHLEN];
513 char *fnamecmp, *partialptr;
514 char fnamecmpbuf[MAXPATHLEN];
516 struct file_struct *file;
517 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
518 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
519 int max_phase = protocol_version >= 29 ? 2 : 1;
520 int dflt_perms = (ACCESSPERMS & ~orig_umask);
522 const char *parent_dirname = "";
526 if (DEBUG_GTE(RECV, 1))
527 rprintf(FINFO, "recv_files(%d) starting\n", cur_flist->used);
530 delayed_bits = bitbag_create(cur_flist->used + 1);
535 /* This call also sets cur_flist. */
536 ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
538 if (ndx == NDX_DONE) {
539 if (!am_server && INFO_GTE(PROGRESS, 2) && cur_flist) {
540 set_current_file_index(NULL, 0);
543 if (inc_recurse && first_flist) {
545 ndx = first_flist->used + first_flist->ndx_start;
546 gen_wants_ndx(ndx, first_flist->flist_num);
548 flist_free(first_flist);
551 } else if (read_batch && first_flist) {
552 ndx = first_flist->used;
553 gen_wants_ndx(ndx, first_flist->flist_num);
555 if (++phase > max_phase)
557 if (DEBUG_GTE(RECV, 1))
558 rprintf(FINFO, "recv_files phase=%d\n", phase);
559 if (phase == 2 && delay_updates)
560 handle_delayed_updates(local_name);
561 write_int(f_out, NDX_DONE);
565 if (ndx - cur_flist->ndx_start >= 0)
566 file = cur_flist->files[ndx - cur_flist->ndx_start];
568 file = dir_flist->files[cur_flist->parent_ndx];
569 fname = local_name ? local_name : f_name(file, fbuf);
571 if (DEBUG_GTE(RECV, 1))
572 rprintf(FINFO, "recv_files(%s)\n", fname);
574 #ifdef SUPPORT_XATTRS
575 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
576 && (protocol_version < 31 || !BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
577 recv_xattr_request(file, f_in);
580 if (!(iflags & ITEM_TRANSFER)) {
581 maybe_log_item(file, iflags, itemizing, xname);
582 #ifdef SUPPORT_XATTRS
583 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
584 && !BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE))
585 set_file_attrs(fname, file, NULL, fname, 0);
587 if (iflags & ITEM_IS_NEW) {
588 stats.created_files++;
589 if (S_ISREG(file->mode)) {
590 /* Nothing further to count. */
591 } else if (S_ISDIR(file->mode))
592 stats.created_dirs++;
594 else if (S_ISLNK(file->mode))
595 stats.created_symlinks++;
597 else if (IS_DEVICE(file->mode))
598 stats.created_devices++;
600 stats.created_specials++;
606 "got transfer request in phase 2 [%s]\n",
608 exit_cleanup(RERR_PROTOCOL);
611 if (file->flags & FLAG_FILE_SENT) {
612 if (csum_length == SHORT_SUM_LENGTH) {
613 if (keep_partial && !partial_dir)
614 make_backups = -make_backups; /* prevents double backup */
616 sparse_files = -sparse_files;
617 append_mode = -append_mode;
618 csum_length = SUM_LENGTH;
622 if (csum_length != SHORT_SUM_LENGTH) {
623 if (keep_partial && !partial_dir)
624 make_backups = -make_backups;
626 sparse_files = -sparse_files;
627 append_mode = -append_mode;
628 csum_length = SHORT_SUM_LENGTH;
631 if (iflags & ITEM_IS_NEW)
632 stats.created_files++;
635 if (!am_server && INFO_GTE(PROGRESS, 1))
636 set_current_file_index(file, ndx);
637 stats.xferred_files++;
638 stats.total_transferred_size += F_LENGTH(file);
640 cleanup_got_literal = 0;
642 if (daemon_filter_list.head
643 && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0) {
644 rprintf(FERROR, "attempt to hack rsync failed.\n");
645 exit_cleanup(RERR_PROTOCOL);
651 : gen_wants_ndx(ndx, cur_flist->flist_num);
654 "(Skipping batched update for%s \"%s\")\n",
655 redoing ? " resend of" : "",
657 discard_receive_data(f_in, F_LENGTH(file));
658 file->flags |= FLAG_FILE_SENT;
663 if (!log_before_transfer)
664 remember_initial_stats();
666 if (!do_xfers) { /* log the transfer */
667 log_item(FCLIENT, file, iflags, NULL);
669 discard_receive_data(f_in, F_LENGTH(file));
672 if (write_batch < 0) {
673 log_item(FCLIENT, file, iflags, NULL);
675 discard_receive_data(f_in, F_LENGTH(file));
679 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
681 if (protocol_version >= 29) {
682 switch (fnamecmp_type) {
686 case FNAMECMP_PARTIAL_DIR:
687 fnamecmp = partialptr;
689 case FNAMECMP_BACKUP:
690 fnamecmp = get_backup_name(fname);
694 pathjoin(fnamecmpbuf, MAXPATHLEN,
695 file->dirname, xname);
696 fnamecmp = fnamecmpbuf;
701 if (fnamecmp_type >= basis_dir_cnt) {
703 "invalid basis_dir index: %d.\n",
705 exit_cleanup(RERR_PROTOCOL);
707 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
708 basis_dir[fnamecmp_type], fname);
709 fnamecmp = fnamecmpbuf;
712 if (!fnamecmp || (daemon_filter_list.head
713 && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0)) {
715 fnamecmp_type = FNAMECMP_FNAME;
718 /* Reminder: --inplace && --partial-dir are never
719 * enabled at the same time. */
720 if (inplace && make_backups > 0) {
721 if (!(fnamecmp = get_backup_name(fname)))
724 fnamecmp_type = FNAMECMP_BACKUP;
725 } else if (partial_dir && partialptr)
726 fnamecmp = partialptr;
732 fd1 = do_open(fnamecmp, O_RDONLY, 0);
734 if (fd1 == -1 && protocol_version < 29) {
735 if (fnamecmp != fname) {
737 fd1 = do_open(fnamecmp, O_RDONLY, 0);
740 if (fd1 == -1 && basis_dir[0]) {
741 /* pre-29 allowed only one alternate basis */
742 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
743 basis_dir[0], fname);
744 fnamecmp = fnamecmpbuf;
745 fd1 = do_open(fnamecmp, O_RDONLY, 0);
749 updating_basis_or_equiv = inplace
750 && (fnamecmp == fname || fnamecmp_type == FNAMECMP_BACKUP);
755 } else if (do_fstat(fd1,&st) != 0) {
756 rsyserr(FERROR_XFER, errno, "fstat %s failed",
757 full_fname(fnamecmp));
758 discard_receive_data(f_in, F_LENGTH(file));
761 send_msg_int(MSG_NO_SEND, ndx);
765 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
766 /* this special handling for directories
767 * wouldn't be necessary if robust_rename()
768 * and the underlying robust_unlink could cope
771 rprintf(FERROR_XFER, "recv_files: %s is a directory\n",
772 full_fname(fnamecmp));
773 discard_receive_data(f_in, F_LENGTH(file));
776 send_msg_int(MSG_NO_SEND, ndx);
780 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
785 /* If we're not preserving permissions, change the file-list's
786 * mode based on the local permissions and some heuristics. */
787 if (!preserve_perms) {
788 int exists = fd1 != -1;
790 const char *dn = file->dirname ? file->dirname : ".";
791 if (parent_dirname != dn
792 && strcmp(parent_dirname, dn) != 0) {
793 dflt_perms = default_perms_for_dir(dn);
797 file->mode = dest_mode(file->mode, st.st_mode,
801 /* We now check to see if we are writing the file "inplace" */
803 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600);
805 rsyserr(FERROR_XFER, errno, "open %s failed",
809 fd2 = open_tmpfile(fnametmp, fname, file);
811 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
815 discard_receive_data(f_in, F_LENGTH(file));
819 send_msg_int(MSG_NO_SEND, ndx);
823 /* log the transfer */
824 if (log_before_transfer)
825 log_item(FCLIENT, file, iflags, NULL);
826 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
827 rprintf(FINFO, "%s\n", fname);
830 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
831 fname, fd2, F_LENGTH(file));
833 log_item(log_code, file, iflags, NULL);
837 if (close(fd2) < 0) {
838 rsyserr(FERROR, errno, "close failed on %s",
839 full_fname(fnametmp));
840 exit_cleanup(RERR_FILEIO);
843 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
844 if (partialptr == fname)
846 if (!finish_transfer(fname, fnametmp, fnamecmp,
847 partialptr, file, recv_ok, 1))
849 else if (fnamecmp == partialptr) {
850 do_unlink(partialptr);
851 handle_partial_dir(partialptr, PDIR_DELETE);
853 } else if (keep_partial && partialptr) {
854 if (!handle_partial_dir(partialptr, PDIR_CREATE)) {
856 "Unable to create partial-dir for %s -- discarding %s.\n",
857 local_name ? local_name : f_name(file, NULL),
858 recv_ok ? "completed file" : "partial file");
861 } else if (!finish_transfer(partialptr, fnametmp, fnamecmp, NULL,
862 file, recv_ok, !partial_dir))
864 else if (delay_updates && recv_ok) {
865 bitbag_set_bit(delayed_bits, ndx);
875 file->flags |= FLAG_FILE_SENT;
881 if (remove_source_files || inc_recurse
882 || (preserve_hard_links && F_IS_HLINKED(file)))
883 send_msg_int(MSG_SUCCESS, ndx);
886 enum logcode msgtype = redoing ? FERROR_XFER : FWARNING;
887 if (msgtype == FERROR_XFER || INFO_GTE(NAME, 1)) {
888 char *errstr, *redostr, *keptstr;
889 if (!(keep_partial && partialptr) && !inplace)
890 keptstr = "discarded";
891 else if (partial_dir)
892 keptstr = "put into partial-dir";
894 keptstr = "retained";
895 if (msgtype == FERROR_XFER) {
900 redostr = read_batch ? " (may try again)"
901 : " (will try again)";
904 "%s: %s failed verification -- update %s%s.\n",
905 errstr, local_name ? f_name(file, NULL) : fname,
910 flist_ndx_push(&batch_redo_list, ndx);
911 send_msg_int(MSG_REDO, ndx);
912 file->flags |= FLAG_FILE_SENT;
913 } else if (inc_recurse)
914 send_msg_int(MSG_NO_SEND, ndx);
919 send_msg_int(MSG_NO_SEND, ndx);
923 if (make_backups < 0)
924 make_backups = -make_backups;
926 if (phase == 2 && delay_updates) /* for protocol_version < 29 */
927 handle_delayed_updates(local_name);
929 if (DEBUG_GTE(RECV, 1))
930 rprintf(FINFO,"recv_files finished\n");