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-2015 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 want_xattr_optim;
34 extern int csum_length;
35 extern int read_batch;
36 extern int write_batch;
37 extern int batch_gen_fd;
38 extern int protocol_version;
39 extern int relative_paths;
40 extern int preserve_hard_links;
41 extern int preserve_perms;
42 extern int preserve_xattrs;
43 extern int basis_dir_cnt;
44 extern int make_backups;
45 extern int cleanup_got_literal;
46 extern int remove_source_files;
47 extern int append_mode;
48 extern int sparse_files;
49 extern int preallocate_files;
50 extern int keep_partial;
51 extern int checksum_seed;
52 extern int whole_file;
54 extern int allowed_lull;
55 extern int delay_updates;
56 extern int xfersum_type;
57 extern mode_t orig_umask;
58 extern struct stats stats;
60 extern char *partial_dir;
61 extern char *basis_dir[MAX_BASIS_DIRS+1];
62 extern char sender_file_sum[MAX_DIGEST_LEN];
63 extern struct file_list *cur_flist, *first_flist, *dir_flist;
64 extern filter_rule_list daemon_filter_list;
65 extern OFF_T preallocated_len;
67 static struct bitbag *delayed_bits = NULL;
68 static int phase = 0, redoing = 0;
69 static flist_ndx_list batch_redo_list;
70 /* We're either updating the basis file or an identical copy: */
71 static int updating_basis_or_equiv;
73 #define TMPNAME_SUFFIX ".XXXXXX"
74 #define TMPNAME_SUFFIX_LEN ((int)sizeof TMPNAME_SUFFIX - 1)
75 #define MAX_UNIQUE_NUMBER 999999
76 #define MAX_UNIQUE_LOOP 100
78 /* get_tmpname() - create a tmp filename for a given filename
80 * If a tmpdir is defined, use that as the directory to put it in. Otherwise,
81 * the tmp filename is in the same directory as the given name. Note that
82 * there may be no directory at all in the given name!
84 * The tmp filename is basically the given filename with a dot prepended, and
85 * .XXXXXX appended (for mkstemp() to put its unique gunk in). We take care
86 * to not exceed either the MAXPATHLEN or NAME_MAX, especially the last, as
87 * the basename basically becomes 8 characters longer. In such a case, the
88 * original name is shortened sufficiently to make it all fit.
90 * If the make_unique arg is True, the XXXXXX string is replaced with a unique
91 * string that doesn't exist at the time of the check. This is intended to be
92 * used for creating hard links, symlinks, devices, and special files, since
93 * normal files should be handled by mkstemp() for safety.
95 * Of course, the only reason the file is based on the original name is to
96 * make it easier to figure out what purpose a temp file is serving when a
97 * transfer is in progress. */
98 int get_tmpname(char *fnametmp, const char *fname, BOOL make_unique)
100 int maxname, length = 0;
105 /* Note: this can't overflow, so the return value is safe */
106 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
107 fnametmp[length++] = '/';
110 if ((f = strrchr(fname, '/')) != NULL) {
114 /* copy up to and including the slash */
115 strlcpy(fnametmp, fname, length + 1);
120 if (!tmpdir) { /* using a tmpdir avoids the leading dot on our temp names */
121 if (*f == '.') /* avoid an extra leading dot for OS X's sake */
123 fnametmp[length++] = '.';
126 /* The maxname value is bufsize, and includes space for the '\0'.
127 * NAME_MAX needs an extra -1 for the name's leading dot. */
128 maxname = MIN(MAXPATHLEN - length - TMPNAME_SUFFIX_LEN,
129 NAME_MAX - 1 - TMPNAME_SUFFIX_LEN);
132 rprintf(FERROR_XFER, "temporary filename too long: %s\n", fname);
138 int added = strlcpy(fnametmp + length, f, maxname);
139 if (added >= maxname)
141 suf = fnametmp + length + added;
143 /* Trim any dangling high-bit chars if the first-trimmed char (if any) is
144 * also a high-bit char, just in case we cut into a multi-byte sequence.
145 * We are guaranteed to stop because of the leading '.' we added. */
146 if ((int)f[added] & 0x80) {
147 while ((int)suf[-1] & 0x80)
150 /* trim one trailing dot before our suffix's dot */
154 suf = fnametmp + length - 1; /* overwrite the leading dot with suffix's dot */
157 static unsigned counter_limit;
160 if (!counter_limit) {
161 counter_limit = (unsigned)getpid() + MAX_UNIQUE_LOOP;
162 if (counter_limit > MAX_UNIQUE_NUMBER || counter_limit < MAX_UNIQUE_LOOP)
163 counter_limit = MAX_UNIQUE_LOOP;
165 counter = counter_limit - MAX_UNIQUE_LOOP;
167 /* This doesn't have to be very good because we don't need
168 * to worry about someone trying to guess the values: all
169 * a conflict will do is cause a device, special file, hard
170 * link, or symlink to fail to be created. Also: avoid
171 * using mktemp() due to gcc's annoying warning. */
173 snprintf(suf, TMPNAME_SUFFIX_LEN+1, ".%d", counter);
174 if (access(fnametmp, 0) < 0)
176 if (++counter >= counter_limit)
180 memcpy(suf, TMPNAME_SUFFIX, TMPNAME_SUFFIX_LEN+1);
185 /* Opens a temporary file for writing.
186 * Success: Writes name into fnametmp, returns fd.
187 * Failure: Clobbers fnametmp, returns -1.
188 * Calling cleanup_set() is the caller's job. */
189 int open_tmpfile(char *fnametmp, const char *fname, struct file_struct *file)
194 if (!get_tmpname(fnametmp, fname, False))
198 /* For --fake-super, the file must be useable by the copying
199 * user, just like it would be for root. */
200 added_perms = S_IRUSR|S_IWUSR;
202 /* For a normal copy, we need to be able to tweak things like xattrs. */
203 added_perms = S_IWUSR;
206 /* We initially set the perms without the setuid/setgid bits or group
207 * access to ensure that there is no race condition. They will be
208 * correctly updated after the right owner and group info is set.
209 * (Thanks to snabb@epipe.fi for pointing this out.) */
210 fd = do_mkstemp(fnametmp, (file->mode|added_perms) & INITACCESSPERMS);
213 /* In most cases parent directories will already exist because their
214 * information should have been previously transferred, but that may
215 * not be the case with -R */
216 if (fd == -1 && relative_paths && errno == ENOENT
217 && make_path(fnametmp, MKP_SKIP_SLASH | MKP_DROP_NAME) == 0) {
218 /* Get back to name with XXXXXX in it. */
219 get_tmpname(fnametmp, fname, False);
220 fd = do_mkstemp(fnametmp, (file->mode|added_perms) & INITACCESSPERMS);
225 rsyserr(FERROR_XFER, errno, "mkstemp %s failed",
226 full_fname(fnametmp));
233 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
234 const char *fname, int fd, OFF_T total_size)
236 static char file_sum1[MAX_DIGEST_LEN];
237 struct map_struct *mapbuf;
238 struct sum_struct sum;
247 #ifdef SUPPORT_PREALLOCATION
248 if (preallocate_files && fd != -1 && total_size > 0 && (!inplace || total_size > size_r)) {
249 /* Try to preallocate enough space for file's eventual length. Can
250 * reduce fragmentation on filesystems like ext4, xfs, and NTFS. */
251 if ((preallocated_len = do_fallocate(fd, 0, total_size)) < 0)
252 rsyserr(FWARNING, errno, "do_fallocate %s", full_fname(fname));
256 #ifdef HAVE_FTRUNCATE
257 /* The most compatible way to create a sparse file is to start with no length. */
258 if (sparse_files > 0 && whole_file && fd >= 0 && do_ftruncate(fd, 0) == 0)
259 preallocated_len = 0;
262 preallocated_len = size_r;
264 preallocated_len = 0;
266 read_sum_head(f_in, &sum);
268 if (fd_r >= 0 && size_r > 0) {
269 int32 read_size = MAX(sum.blength * 2, 16*1024);
270 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
271 if (DEBUG_GTE(DELTASUM, 2)) {
272 rprintf(FINFO, "recv mapped %s of size %s\n",
273 fname_r, big_num(size_r));
278 sum_init(xfersum_type, checksum_seed);
280 if (append_mode > 0) {
282 sum.flength = (OFF_T)sum.count * sum.blength;
284 sum.flength -= sum.blength - sum.remainder;
285 if (append_mode == 2 && mapbuf) {
286 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
287 if (INFO_GTE(PROGRESS, 1))
288 show_progress(offset, total_size);
289 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
293 if (offset < sum.flength) {
294 int32 len = (int32)(sum.flength - offset);
295 if (INFO_GTE(PROGRESS, 1))
296 show_progress(offset, total_size);
297 sum_update(map_ptr(mapbuf, offset, len), len);
300 offset = sum.flength;
301 if (fd != -1 && (j = do_lseek(fd, offset, SEEK_SET)) != offset) {
302 rsyserr(FERROR_XFER, errno, "lseek of %s returned %s, not %s",
303 full_fname(fname), big_num(j), big_num(offset));
304 exit_cleanup(RERR_FILEIO);
308 while ((i = recv_token(f_in, &data)) != 0) {
309 if (INFO_GTE(PROGRESS, 1))
310 show_progress(offset, total_size);
313 maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH | MSK_ACTIVE_RECEIVER);
316 if (DEBUG_GTE(DELTASUM, 3)) {
317 rprintf(FINFO,"data recv %d at %s\n",
321 stats.literal_data += i;
322 cleanup_got_literal = 1;
326 if (fd != -1 && write_file(fd, 0, offset, data, i) != i)
327 goto report_write_error;
333 offset2 = i * (OFF_T)sum.blength;
335 if (i == (int)sum.count-1 && sum.remainder != 0)
338 stats.matched_data += len;
340 if (DEBUG_GTE(DELTASUM, 3)) {
342 "chunk[%d] of size %ld at %s offset=%s%s\n",
343 i, (long)len, big_num(offset2), big_num(offset),
344 updating_basis_or_equiv && offset == offset2 ? " (seek)" : "");
348 map = map_ptr(mapbuf,offset2,len);
351 sum_update(map, len);
354 if (updating_basis_or_equiv) {
355 if (offset == offset2 && fd != -1) {
356 if (skip_matched(fd, offset, map, len) < 0)
357 goto report_write_error;
362 if (fd != -1 && map && write_file(fd, 0, offset, map, len) != (int)len)
363 goto report_write_error;
367 if (fd != -1 && offset > 0) {
368 if (sparse_files > 0) {
369 if (sparse_end(fd, offset) != 0)
370 goto report_write_error;
371 } else if (flush_write_file(fd) < 0) {
373 rsyserr(FERROR_XFER, errno, "write failed on %s", full_fname(fname));
374 exit_cleanup(RERR_FILEIO);
378 #ifdef HAVE_FTRUNCATE
379 /* inplace: New data could be shorter than old data.
380 * preallocate_files: total_size could have been an overestimate.
381 * Cut off any extra preallocated zeros from dest file. */
382 if ((inplace || preallocated_len > offset) && fd != -1 && do_ftruncate(fd, offset) < 0) {
383 rsyserr(FERROR_XFER, errno, "ftruncate failed on %s",
388 if (INFO_GTE(PROGRESS, 1))
389 end_progress(total_size);
391 sum_len = sum_end(file_sum1);
396 read_buf(f_in, sender_file_sum, sum_len);
397 if (DEBUG_GTE(DELTASUM, 2))
398 rprintf(FINFO,"got file_sum\n");
399 if (fd != -1 && memcmp(file_sum1, sender_file_sum, sum_len) != 0)
405 static void discard_receive_data(int f_in, OFF_T length)
407 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
410 static void handle_delayed_updates(char *local_name)
412 char *fname, *partialptr;
415 for (ndx = -1; (ndx = bitbag_next_bit(delayed_bits, ndx)) >= 0; ) {
416 struct file_struct *file = cur_flist->files[ndx];
417 fname = local_name ? local_name : f_name(file, NULL);
418 if ((partialptr = partial_dir_fname(fname)) != NULL) {
419 if (make_backups > 0 && !make_backup(fname, False))
421 if (DEBUG_GTE(RECV, 1)) {
422 rprintf(FINFO, "renaming %s to %s\n",
425 /* We don't use robust_rename() here because the
426 * partial-dir must be on the same drive. */
427 if (do_rename(partialptr, fname) < 0) {
428 rsyserr(FERROR_XFER, errno,
429 "rename failed for %s (from %s)",
430 full_fname(fname), partialptr);
432 if (remove_source_files
433 || (preserve_hard_links && F_IS_HLINKED(file)))
434 send_msg_int(MSG_SUCCESS, ndx);
435 handle_partial_dir(partialptr, PDIR_DELETE);
441 static void no_batched_update(int ndx, BOOL is_redo)
443 struct file_list *flist = flist_for_ndx(ndx, "no_batched_update");
444 struct file_struct *file = flist->files[ndx - flist->ndx_start];
446 rprintf(FERROR_XFER, "(No batched update for%s \"%s\")\n",
447 is_redo ? " resend of" : "", f_name(file, NULL));
449 if (inc_recurse && !dry_run)
450 send_msg_int(MSG_NO_SEND, ndx);
453 static int we_want_redo(int desired_ndx)
455 static int redo_ndx = -1;
457 while (redo_ndx < desired_ndx) {
459 no_batched_update(redo_ndx, True);
460 if ((redo_ndx = flist_ndx_pop(&batch_redo_list)) < 0)
464 if (redo_ndx == desired_ndx) {
472 static int gen_wants_ndx(int desired_ndx, int flist_num)
474 static int next_ndx = -1;
475 static int done_cnt = 0;
476 static BOOL got_eof = False;
481 /* TODO: integrate gen-reading I/O into perform_io() so this is not needed? */
482 io_flush(FULL_FLUSH);
484 while (next_ndx < desired_ndx) {
485 if (inc_recurse && flist_num <= done_cnt)
488 no_batched_update(next_ndx, False);
489 if ((next_ndx = read_int(batch_gen_fd)) < 0) {
499 if (next_ndx == desired_ndx) {
508 * main routine for receiver process.
510 * Receiver process runs on the same host as the generator process. */
511 int recv_files(int f_in, int f_out, char *local_name)
516 char *fname, fbuf[MAXPATHLEN];
517 char xname[MAXPATHLEN];
518 char fnametmp[MAXPATHLEN];
519 char *fnamecmp, *partialptr;
520 char fnamecmpbuf[MAXPATHLEN];
522 struct file_struct *file;
523 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
524 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
525 int max_phase = protocol_version >= 29 ? 2 : 1;
526 int dflt_perms = (ACCESSPERMS & ~orig_umask);
528 const char *parent_dirname = "";
532 if (DEBUG_GTE(RECV, 1))
533 rprintf(FINFO, "recv_files(%d) starting\n", cur_flist->used);
536 delayed_bits = bitbag_create(cur_flist->used + 1);
541 /* This call also sets cur_flist. */
542 ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
544 if (ndx == NDX_DONE) {
545 if (!am_server && INFO_GTE(PROGRESS, 2) && cur_flist) {
546 set_current_file_index(NULL, 0);
549 if (inc_recurse && first_flist) {
551 ndx = first_flist->used + first_flist->ndx_start;
552 gen_wants_ndx(ndx, first_flist->flist_num);
554 flist_free(first_flist);
557 } else if (read_batch && first_flist) {
558 ndx = first_flist->used;
559 gen_wants_ndx(ndx, first_flist->flist_num);
561 if (++phase > max_phase)
563 if (DEBUG_GTE(RECV, 1))
564 rprintf(FINFO, "recv_files phase=%d\n", phase);
565 if (phase == 2 && delay_updates)
566 handle_delayed_updates(local_name);
567 write_int(f_out, NDX_DONE);
571 if (ndx - cur_flist->ndx_start >= 0)
572 file = cur_flist->files[ndx - cur_flist->ndx_start];
574 file = dir_flist->files[cur_flist->parent_ndx];
575 fname = local_name ? local_name : f_name(file, fbuf);
577 if (daemon_filter_list.head
578 && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0) {
579 rprintf(FERROR, "attempt to hack rsync failed.\n");
580 exit_cleanup(RERR_PROTOCOL);
583 if (DEBUG_GTE(RECV, 1))
584 rprintf(FINFO, "recv_files(%s)\n", fname);
586 #ifdef SUPPORT_XATTRS
587 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
588 && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
589 recv_xattr_request(file, f_in);
592 if (!(iflags & ITEM_TRANSFER)) {
593 maybe_log_item(file, iflags, itemizing, xname);
594 #ifdef SUPPORT_XATTRS
595 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
596 && !BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE))
597 set_file_attrs(fname, file, NULL, fname, 0);
599 if (iflags & ITEM_IS_NEW) {
600 stats.created_files++;
601 if (S_ISREG(file->mode)) {
602 /* Nothing further to count. */
603 } else if (S_ISDIR(file->mode))
604 stats.created_dirs++;
606 else if (S_ISLNK(file->mode))
607 stats.created_symlinks++;
609 else if (IS_DEVICE(file->mode))
610 stats.created_devices++;
612 stats.created_specials++;
618 "got transfer request in phase 2 [%s]\n",
620 exit_cleanup(RERR_PROTOCOL);
623 if (file->flags & FLAG_FILE_SENT) {
624 if (csum_length == SHORT_SUM_LENGTH) {
625 if (keep_partial && !partial_dir)
626 make_backups = -make_backups; /* prevents double backup */
628 sparse_files = -sparse_files;
629 append_mode = -append_mode;
630 csum_length = SUM_LENGTH;
634 if (csum_length != SHORT_SUM_LENGTH) {
635 if (keep_partial && !partial_dir)
636 make_backups = -make_backups;
638 sparse_files = -sparse_files;
639 append_mode = -append_mode;
640 csum_length = SHORT_SUM_LENGTH;
643 if (iflags & ITEM_IS_NEW)
644 stats.created_files++;
647 if (!am_server && INFO_GTE(PROGRESS, 1))
648 set_current_file_index(file, ndx);
649 stats.xferred_files++;
650 stats.total_transferred_size += F_LENGTH(file);
652 cleanup_got_literal = 0;
657 : gen_wants_ndx(ndx, cur_flist->flist_num);
660 "(Skipping batched update for%s \"%s\")\n",
661 redoing ? " resend of" : "",
663 discard_receive_data(f_in, F_LENGTH(file));
664 file->flags |= FLAG_FILE_SENT;
669 remember_initial_stats();
671 if (!do_xfers) { /* log the transfer */
672 log_item(FCLIENT, file, iflags, NULL);
674 discard_receive_data(f_in, F_LENGTH(file));
677 if (write_batch < 0) {
678 log_item(FCLIENT, file, iflags, NULL);
680 discard_receive_data(f_in, F_LENGTH(file));
682 send_msg_int(MSG_SUCCESS, ndx);
686 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
688 if (protocol_version >= 29) {
689 switch (fnamecmp_type) {
693 case FNAMECMP_PARTIAL_DIR:
694 fnamecmp = partialptr;
696 case FNAMECMP_BACKUP:
697 fnamecmp = get_backup_name(fname);
701 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, file->dirname, xname);
702 fnamecmp = fnamecmpbuf;
707 if (fnamecmp_type > FNAMECMP_FUZZY && fnamecmp_type-FNAMECMP_FUZZY <= basis_dir_cnt) {
708 fnamecmp_type -= FNAMECMP_FUZZY + 1;
710 stringjoin(fnamecmpbuf, sizeof fnamecmpbuf,
711 basis_dir[fnamecmp_type], "/", file->dirname, "/", xname, NULL);
713 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, basis_dir[fnamecmp_type], xname);
714 } else if (fnamecmp_type >= basis_dir_cnt) {
716 "invalid basis_dir index: %d.\n",
718 exit_cleanup(RERR_PROTOCOL);
720 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, basis_dir[fnamecmp_type], fname);
721 fnamecmp = fnamecmpbuf;
724 if (!fnamecmp || (daemon_filter_list.head
725 && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0)) {
727 fnamecmp_type = FNAMECMP_FNAME;
730 /* Reminder: --inplace && --partial-dir are never
731 * enabled at the same time. */
732 if (inplace && make_backups > 0) {
733 if (!(fnamecmp = get_backup_name(fname)))
736 fnamecmp_type = FNAMECMP_BACKUP;
737 } else if (partial_dir && partialptr)
738 fnamecmp = partialptr;
744 fd1 = do_open(fnamecmp, O_RDONLY, 0);
746 if (fd1 == -1 && protocol_version < 29) {
747 if (fnamecmp != fname) {
749 fd1 = do_open(fnamecmp, O_RDONLY, 0);
752 if (fd1 == -1 && basis_dir[0]) {
753 /* pre-29 allowed only one alternate basis */
754 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
755 basis_dir[0], fname);
756 fnamecmp = fnamecmpbuf;
757 fd1 = do_open(fnamecmp, O_RDONLY, 0);
761 updating_basis_or_equiv = inplace
762 && (fnamecmp == fname || fnamecmp_type == FNAMECMP_BACKUP);
767 } else if (do_fstat(fd1,&st) != 0) {
768 rsyserr(FERROR_XFER, errno, "fstat %s failed",
769 full_fname(fnamecmp));
770 discard_receive_data(f_in, F_LENGTH(file));
773 send_msg_int(MSG_NO_SEND, ndx);
777 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
778 /* this special handling for directories
779 * wouldn't be necessary if robust_rename()
780 * and the underlying robust_unlink could cope
783 rprintf(FERROR_XFER, "recv_files: %s is a directory\n",
784 full_fname(fnamecmp));
785 discard_receive_data(f_in, F_LENGTH(file));
788 send_msg_int(MSG_NO_SEND, ndx);
792 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
797 /* If we're not preserving permissions, change the file-list's
798 * mode based on the local permissions and some heuristics. */
799 if (!preserve_perms) {
800 int exists = fd1 != -1;
802 const char *dn = file->dirname ? file->dirname : ".";
803 if (parent_dirname != dn
804 && strcmp(parent_dirname, dn) != 0) {
805 dflt_perms = default_perms_for_dir(dn);
809 file->mode = dest_mode(file->mode, st.st_mode,
813 /* We now check to see if we are writing the file "inplace" */
815 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600);
817 rsyserr(FERROR_XFER, errno, "open %s failed",
819 } else if (updating_basis_or_equiv)
820 cleanup_set(NULL, NULL, file, fd1, fd2);
822 fd2 = open_tmpfile(fnametmp, fname, file);
824 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
828 discard_receive_data(f_in, F_LENGTH(file));
832 send_msg_int(MSG_NO_SEND, ndx);
836 /* log the transfer */
837 if (log_before_transfer)
838 log_item(FCLIENT, file, iflags, NULL);
839 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
840 rprintf(FINFO, "%s\n", fname);
843 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
844 fname, fd2, F_LENGTH(file));
846 log_item(log_code, file, iflags, NULL);
850 if (close(fd2) < 0) {
851 rsyserr(FERROR, errno, "close failed on %s",
852 full_fname(fnametmp));
853 exit_cleanup(RERR_FILEIO);
856 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
857 if (partialptr == fname)
859 if (!finish_transfer(fname, fnametmp, fnamecmp,
860 partialptr, file, recv_ok, 1))
862 else if (fnamecmp == partialptr) {
863 do_unlink(partialptr);
864 handle_partial_dir(partialptr, PDIR_DELETE);
866 } else if (keep_partial && partialptr) {
867 if (!handle_partial_dir(partialptr, PDIR_CREATE)) {
869 "Unable to create partial-dir for %s -- discarding %s.\n",
870 local_name ? local_name : f_name(file, NULL),
871 recv_ok ? "completed file" : "partial file");
874 } else if (!finish_transfer(partialptr, fnametmp, fnamecmp, NULL,
875 file, recv_ok, !partial_dir))
877 else if (delay_updates && recv_ok) {
878 bitbag_set_bit(delayed_bits, ndx);
888 file->flags |= FLAG_FILE_SENT;
894 if (remove_source_files || inc_recurse
895 || (preserve_hard_links && F_IS_HLINKED(file)))
896 send_msg_int(MSG_SUCCESS, ndx);
899 enum logcode msgtype = redoing ? FERROR_XFER : FWARNING;
900 if (msgtype == FERROR_XFER || INFO_GTE(NAME, 1)) {
901 char *errstr, *redostr, *keptstr;
902 if (!(keep_partial && partialptr) && !inplace)
903 keptstr = "discarded";
904 else if (partial_dir)
905 keptstr = "put into partial-dir";
907 keptstr = "retained";
908 if (msgtype == FERROR_XFER) {
913 redostr = read_batch ? " (may try again)"
914 : " (will try again)";
917 "%s: %s failed verification -- update %s%s.\n",
918 errstr, local_name ? f_name(file, NULL) : fname,
923 flist_ndx_push(&batch_redo_list, ndx);
924 send_msg_int(MSG_REDO, ndx);
925 file->flags |= FLAG_FILE_SENT;
926 } else if (inc_recurse)
927 send_msg_int(MSG_NO_SEND, ndx);
932 send_msg_int(MSG_NO_SEND, ndx);
936 if (make_backups < 0)
937 make_backups = -make_backups;
939 if (phase == 2 && delay_updates) /* for protocol_version < 29 */
940 handle_delayed_updates(local_name);
942 if (DEBUG_GTE(RECV, 1))
943 rprintf(FINFO,"recv_files finished\n");