More NEWS updates.
[rsync.git] / receiver.c
blobc9d7e01d7fe01f90ca6eeff58a1f511397b675f8
1 /*
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-2022 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.
22 #include "rsync.h"
23 #include "inums.h"
25 extern int dry_run;
26 extern int do_xfers;
27 extern int am_root;
28 extern int am_server;
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 write_devices;
43 extern int preserve_xattrs;
44 extern int do_fsync;
45 extern int basis_dir_cnt;
46 extern int make_backups;
47 extern int cleanup_got_literal;
48 extern int remove_source_files;
49 extern int append_mode;
50 extern int sparse_files;
51 extern int preallocate_files;
52 extern int keep_partial;
53 extern int checksum_seed;
54 extern int whole_file;
55 extern int inplace;
56 extern int inplace_partial;
57 extern int allowed_lull;
58 extern int delay_updates;
59 extern BOOL want_progress_now;
60 extern mode_t orig_umask;
61 extern struct stats stats;
62 extern char *tmpdir;
63 extern char *partial_dir;
64 extern char *basis_dir[MAX_BASIS_DIRS+1];
65 extern char sender_file_sum[MAX_DIGEST_LEN];
66 extern struct file_list *cur_flist, *first_flist, *dir_flist;
67 extern filter_rule_list daemon_filter_list;
68 extern OFF_T preallocated_len;
70 extern struct name_num_item *xfer_sum_nni;
71 extern int xfer_sum_len;
73 static struct bitbag *delayed_bits = NULL;
74 static int phase = 0, redoing = 0;
75 static flist_ndx_list batch_redo_list;
76 /* This is non-0 when we are updating the basis file or an identical copy: */
77 static int updating_basis_or_equiv;
79 #define TMPNAME_SUFFIX ".XXXXXX"
80 #define TMPNAME_SUFFIX_LEN ((int)sizeof TMPNAME_SUFFIX - 1)
81 #define MAX_UNIQUE_NUMBER 999999
82 #define MAX_UNIQUE_LOOP 100
84 /* get_tmpname() - create a tmp filename for a given filename
86 * If a tmpdir is defined, use that as the directory to put it in. Otherwise,
87 * the tmp filename is in the same directory as the given name. Note that
88 * there may be no directory at all in the given name!
90 * The tmp filename is basically the given filename with a dot prepended, and
91 * .XXXXXX appended (for mkstemp() to put its unique gunk in). We take care
92 * to not exceed either the MAXPATHLEN or NAME_MAX, especially the last, as
93 * the basename basically becomes 8 characters longer. In such a case, the
94 * original name is shortened sufficiently to make it all fit.
96 * If the make_unique arg is True, the XXXXXX string is replaced with a unique
97 * string that doesn't exist at the time of the check. This is intended to be
98 * used for creating hard links, symlinks, devices, and special files, since
99 * normal files should be handled by mkstemp() for safety.
101 * Of course, the only reason the file is based on the original name is to
102 * make it easier to figure out what purpose a temp file is serving when a
103 * transfer is in progress. */
104 int get_tmpname(char *fnametmp, const char *fname, BOOL make_unique)
106 int maxname, length = 0;
107 const char *f;
108 char *suf;
110 if (tmpdir) {
111 /* Note: this can't overflow, so the return value is safe */
112 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
113 fnametmp[length++] = '/';
116 if ((f = strrchr(fname, '/')) != NULL) {
117 ++f;
118 if (!tmpdir) {
119 length = f - fname;
120 /* copy up to and including the slash */
121 strlcpy(fnametmp, fname, length + 1);
123 } else
124 f = fname;
126 if (!tmpdir) { /* using a tmpdir avoids the leading dot on our temp names */
127 if (*f == '.') /* avoid an extra leading dot for OS X's sake */
128 f++;
129 fnametmp[length++] = '.';
132 /* The maxname value is bufsize, and includes space for the '\0'.
133 * NAME_MAX needs an extra -1 for the name's leading dot. */
134 maxname = MIN(MAXPATHLEN - length - TMPNAME_SUFFIX_LEN,
135 NAME_MAX - 1 - TMPNAME_SUFFIX_LEN);
137 if (maxname < 0) {
138 rprintf(FERROR_XFER, "temporary filename too long: %s\n", fname);
139 fnametmp[0] = '\0';
140 return 0;
143 if (maxname) {
144 int added = strlcpy(fnametmp + length, f, maxname);
145 if (added >= maxname)
146 added = maxname - 1;
147 suf = fnametmp + length + added;
149 /* Trim any dangling high-bit chars if the first-trimmed char (if any) is
150 * also a high-bit char, just in case we cut into a multi-byte sequence.
151 * We are guaranteed to stop because of the leading '.' we added. */
152 if ((int)f[added] & 0x80) {
153 while ((int)suf[-1] & 0x80)
154 suf--;
156 /* trim one trailing dot before our suffix's dot */
157 if (suf[-1] == '.')
158 suf--;
159 } else
160 suf = fnametmp + length - 1; /* overwrite the leading dot with suffix's dot */
162 if (make_unique) {
163 static unsigned counter_limit;
164 unsigned counter;
166 if (!counter_limit) {
167 counter_limit = (unsigned)getpid() + MAX_UNIQUE_LOOP;
168 if (counter_limit > MAX_UNIQUE_NUMBER || counter_limit < MAX_UNIQUE_LOOP)
169 counter_limit = MAX_UNIQUE_LOOP;
171 counter = counter_limit - MAX_UNIQUE_LOOP;
173 /* This doesn't have to be very good because we don't need
174 * to worry about someone trying to guess the values: all
175 * a conflict will do is cause a device, special file, hard
176 * link, or symlink to fail to be created. Also: avoid
177 * using mktemp() due to gcc's annoying warning. */
178 while (1) {
179 snprintf(suf, TMPNAME_SUFFIX_LEN+1, ".%d", counter);
180 if (access(fnametmp, 0) < 0)
181 break;
182 if (++counter >= counter_limit)
183 return 0;
185 } else
186 memcpy(suf, TMPNAME_SUFFIX, TMPNAME_SUFFIX_LEN+1);
188 return 1;
191 /* Opens a temporary file for writing.
192 * Success: Writes name into fnametmp, returns fd.
193 * Failure: Clobbers fnametmp, returns -1.
194 * Calling cleanup_set() is the caller's job. */
195 int open_tmpfile(char *fnametmp, const char *fname, struct file_struct *file)
197 int fd;
198 mode_t added_perms;
200 if (!get_tmpname(fnametmp, fname, False))
201 return -1;
203 if (am_root < 0) {
204 /* For --fake-super, the file must be useable by the copying
205 * user, just like it would be for root. */
206 added_perms = S_IRUSR|S_IWUSR;
207 } else {
208 /* For a normal copy, we need to be able to tweak things like xattrs. */
209 added_perms = S_IWUSR;
212 /* We initially set the perms without the setuid/setgid bits or group
213 * access to ensure that there is no race condition. They will be
214 * correctly updated after the right owner and group info is set.
215 * (Thanks to snabb@epipe.fi for pointing this out.) */
216 fd = do_mkstemp(fnametmp, (file->mode|added_perms) & INITACCESSPERMS);
218 #if 0
219 /* In most cases parent directories will already exist because their
220 * information should have been previously transferred, but that may
221 * not be the case with -R */
222 if (fd == -1 && relative_paths && errno == ENOENT
223 && make_path(fnametmp, MKP_SKIP_SLASH | MKP_DROP_NAME) == 0) {
224 /* Get back to name with XXXXXX in it. */
225 get_tmpname(fnametmp, fname, False);
226 fd = do_mkstemp(fnametmp, (file->mode|added_perms) & INITACCESSPERMS);
228 #endif
230 if (fd == -1) {
231 rsyserr(FERROR_XFER, errno, "mkstemp %s failed",
232 full_fname(fnametmp));
233 return -1;
236 return fd;
239 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
240 const char *fname, int fd, struct file_struct *file, int inplace_sizing)
242 static char file_sum1[MAX_DIGEST_LEN];
243 struct map_struct *mapbuf;
244 struct sum_struct sum;
245 int32 len;
246 OFF_T total_size = F_LENGTH(file);
247 OFF_T offset = 0;
248 OFF_T offset2;
249 char *data;
250 int32 i;
251 char *map = NULL;
253 #ifdef SUPPORT_PREALLOCATION
254 if (preallocate_files && fd != -1 && total_size > 0 && (!inplace_sizing || total_size > size_r)) {
255 /* Try to preallocate enough space for file's eventual length. Can
256 * reduce fragmentation on filesystems like ext4, xfs, and NTFS. */
257 if ((preallocated_len = do_fallocate(fd, 0, total_size)) < 0)
258 rsyserr(FWARNING, errno, "do_fallocate %s", full_fname(fname));
259 } else
260 #endif
261 if (inplace_sizing) {
262 #ifdef HAVE_FTRUNCATE
263 /* The most compatible way to create a sparse file is to start with no length. */
264 if (sparse_files > 0 && whole_file && fd >= 0 && do_ftruncate(fd, 0) == 0)
265 preallocated_len = 0;
266 else
267 #endif
268 preallocated_len = size_r;
269 } else
270 preallocated_len = 0;
272 read_sum_head(f_in, &sum);
274 if (fd_r >= 0 && size_r > 0) {
275 int32 read_size = MAX(sum.blength * 2, 16*1024);
276 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
277 if (DEBUG_GTE(DELTASUM, 2)) {
278 rprintf(FINFO, "recv mapped %s of size %s\n",
279 fname_r, big_num(size_r));
281 } else
282 mapbuf = NULL;
284 sum_init(xfer_sum_nni, checksum_seed);
286 if (append_mode > 0) {
287 OFF_T j;
288 sum.flength = (OFF_T)sum.count * sum.blength;
289 if (sum.remainder)
290 sum.flength -= sum.blength - sum.remainder;
291 if (append_mode == 2 && mapbuf) {
292 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
293 if (INFO_GTE(PROGRESS, 1))
294 show_progress(offset, total_size);
295 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
296 CHUNK_SIZE);
297 offset = j;
299 if (offset < sum.flength) {
300 int32 len = (int32)(sum.flength - offset);
301 if (INFO_GTE(PROGRESS, 1))
302 show_progress(offset, total_size);
303 sum_update(map_ptr(mapbuf, offset, len), len);
306 offset = sum.flength;
307 if (fd != -1 && (j = do_lseek(fd, offset, SEEK_SET)) != offset) {
308 rsyserr(FERROR_XFER, errno, "lseek of %s returned %s, not %s",
309 full_fname(fname), big_num(j), big_num(offset));
310 exit_cleanup(RERR_FILEIO);
314 while ((i = recv_token(f_in, &data)) != 0) {
315 if (INFO_GTE(PROGRESS, 1))
316 show_progress(offset, total_size);
318 if (allowed_lull)
319 maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH | MSK_ACTIVE_RECEIVER);
321 if (i > 0) {
322 if (DEBUG_GTE(DELTASUM, 3)) {
323 rprintf(FINFO,"data recv %d at %s\n",
324 i, big_num(offset));
327 stats.literal_data += i;
328 cleanup_got_literal = 1;
330 sum_update(data, i);
332 if (fd != -1 && write_file(fd, 0, offset, data, i) != i)
333 goto report_write_error;
334 offset += i;
335 continue;
338 i = -(i+1);
339 offset2 = i * (OFF_T)sum.blength;
340 len = sum.blength;
341 if (i == (int)sum.count-1 && sum.remainder != 0)
342 len = sum.remainder;
344 stats.matched_data += len;
346 if (DEBUG_GTE(DELTASUM, 3)) {
347 rprintf(FINFO,
348 "chunk[%d] of size %ld at %s offset=%s%s\n",
349 i, (long)len, big_num(offset2), big_num(offset),
350 updating_basis_or_equiv && offset == offset2 ? " (seek)" : "");
353 if (mapbuf) {
354 map = map_ptr(mapbuf,offset2,len);
356 see_token(map, len);
357 sum_update(map, len);
360 if (updating_basis_or_equiv) {
361 if (offset == offset2 && fd != -1) {
362 if (skip_matched(fd, offset, map, len) < 0)
363 goto report_write_error;
364 offset += len;
365 continue;
368 if (fd != -1 && map && write_file(fd, 0, offset, map, len) != (int)len)
369 goto report_write_error;
370 offset += len;
373 if (fd != -1 && offset > 0) {
374 if (sparse_files > 0) {
375 if (sparse_end(fd, offset) != 0)
376 goto report_write_error;
377 } else if (flush_write_file(fd) < 0) {
378 report_write_error:
379 rsyserr(FERROR_XFER, errno, "write failed on %s", full_fname(fname));
380 exit_cleanup(RERR_FILEIO);
384 #ifdef HAVE_FTRUNCATE
385 /* inplace: New data could be shorter than old data.
386 * preallocate_files: total_size could have been an overestimate.
387 * Cut off any extra preallocated zeros from dest file. */
388 if ((inplace_sizing || preallocated_len > offset) && fd != -1 && !IS_DEVICE(file->mode)) {
389 if (do_ftruncate(fd, offset) < 0)
390 rsyserr(FERROR_XFER, errno, "ftruncate failed on %s", full_fname(fname));
392 #endif
394 if (INFO_GTE(PROGRESS, 1))
395 end_progress(total_size);
397 sum_end(file_sum1);
399 if (do_fsync && fd != -1 && fsync(fd) != 0) {
400 rsyserr(FERROR, errno, "fsync failed on %s", full_fname(fname));
401 exit_cleanup(RERR_FILEIO);
404 if (mapbuf)
405 unmap_file(mapbuf);
407 read_buf(f_in, sender_file_sum, xfer_sum_len);
408 if (DEBUG_GTE(DELTASUM, 2))
409 rprintf(FINFO,"got file_sum\n");
410 if (fd != -1 && memcmp(file_sum1, sender_file_sum, xfer_sum_len) != 0)
411 return 0;
412 return 1;
416 static void discard_receive_data(int f_in, struct file_struct *file)
418 receive_data(f_in, NULL, -1, 0, NULL, -1, file, 0);
421 static void handle_delayed_updates(char *local_name)
423 char *fname, *partialptr;
424 int ndx;
426 for (ndx = -1; (ndx = bitbag_next_bit(delayed_bits, ndx)) >= 0; ) {
427 struct file_struct *file = cur_flist->files[ndx];
428 fname = local_name ? local_name : f_name(file, NULL);
429 if ((partialptr = partial_dir_fname(fname)) != NULL) {
430 if (make_backups > 0 && !make_backup(fname, False))
431 continue;
432 if (DEBUG_GTE(RECV, 1)) {
433 rprintf(FINFO, "renaming %s to %s\n",
434 partialptr, fname);
436 /* We don't use robust_rename() here because the
437 * partial-dir must be on the same drive. */
438 if (do_rename(partialptr, fname) < 0) {
439 rsyserr(FERROR_XFER, errno,
440 "rename failed for %s (from %s)",
441 full_fname(fname), partialptr);
442 } else {
443 if (remove_source_files || (preserve_hard_links && F_IS_HLINKED(file)))
444 send_msg_success(fname, ndx);
445 handle_partial_dir(partialptr, PDIR_DELETE);
451 static void no_batched_update(int ndx, BOOL is_redo)
453 struct file_list *flist = flist_for_ndx(ndx, "no_batched_update");
454 struct file_struct *file = flist->files[ndx - flist->ndx_start];
456 rprintf(FERROR_XFER, "(No batched update for%s \"%s\")\n",
457 is_redo ? " resend of" : "", f_name(file, NULL));
459 if (inc_recurse && !dry_run)
460 send_msg_int(MSG_NO_SEND, ndx);
463 static int we_want_redo(int desired_ndx)
465 static int redo_ndx = -1;
467 while (redo_ndx < desired_ndx) {
468 if (redo_ndx >= 0)
469 no_batched_update(redo_ndx, True);
470 if ((redo_ndx = flist_ndx_pop(&batch_redo_list)) < 0)
471 return 0;
474 if (redo_ndx == desired_ndx) {
475 redo_ndx = -1;
476 return 1;
479 return 0;
482 static int gen_wants_ndx(int desired_ndx, int flist_num)
484 static int next_ndx = -1;
485 static int done_cnt = 0;
486 static BOOL got_eof = False;
488 if (got_eof)
489 return 0;
491 /* TODO: integrate gen-reading I/O into perform_io() so this is not needed? */
492 io_flush(FULL_FLUSH);
494 while (next_ndx < desired_ndx) {
495 if (inc_recurse && flist_num <= done_cnt)
496 return 0;
497 if (next_ndx >= 0)
498 no_batched_update(next_ndx, False);
499 if ((next_ndx = read_int(batch_gen_fd)) < 0) {
500 if (inc_recurse) {
501 done_cnt++;
502 continue;
504 got_eof = True;
505 return 0;
509 if (next_ndx == desired_ndx) {
510 next_ndx = -1;
511 return 1;
514 return 0;
518 * main routine for receiver process.
520 * Receiver process runs on the same host as the generator process. */
521 int recv_files(int f_in, int f_out, char *local_name)
523 int fd1,fd2;
524 STRUCT_STAT st;
525 int iflags, xlen;
526 char *fname, fbuf[MAXPATHLEN];
527 char xname[MAXPATHLEN];
528 char *fnametmp, fnametmpbuf[MAXPATHLEN];
529 char *fnamecmp, *partialptr;
530 char fnamecmpbuf[MAXPATHLEN];
531 uchar fnamecmp_type;
532 struct file_struct *file;
533 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
534 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
535 int max_phase = protocol_version >= 29 ? 2 : 1;
536 int dflt_perms = (ACCESSPERMS & ~orig_umask);
537 #ifdef SUPPORT_ACLS
538 const char *parent_dirname = "";
539 #endif
540 int ndx, recv_ok, one_inplace;
542 if (DEBUG_GTE(RECV, 1))
543 rprintf(FINFO, "recv_files(%d) starting\n", cur_flist->used);
545 if (delay_updates)
546 delayed_bits = bitbag_create(cur_flist->used + 1);
548 if (whole_file < 0)
549 whole_file = 0;
551 progress_init();
553 while (1) {
554 cleanup_disable();
556 /* This call also sets cur_flist. */
557 ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
558 xname, &xlen);
559 if (ndx == NDX_DONE) {
560 if (!am_server && cur_flist) {
561 set_current_file_index(NULL, 0);
562 if (INFO_GTE(PROGRESS, 2))
563 end_progress(0);
565 if (inc_recurse && first_flist) {
566 if (read_batch) {
567 ndx = first_flist->used + first_flist->ndx_start;
568 gen_wants_ndx(ndx, first_flist->flist_num);
570 flist_free(first_flist);
571 if (first_flist)
572 continue;
573 } else if (read_batch && first_flist) {
574 ndx = first_flist->used;
575 gen_wants_ndx(ndx, first_flist->flist_num);
577 if (++phase > max_phase)
578 break;
579 if (DEBUG_GTE(RECV, 1))
580 rprintf(FINFO, "recv_files phase=%d\n", phase);
581 if (phase == 2 && delay_updates)
582 handle_delayed_updates(local_name);
583 write_int(f_out, NDX_DONE);
584 continue;
587 if (ndx - cur_flist->ndx_start >= 0)
588 file = cur_flist->files[ndx - cur_flist->ndx_start];
589 else
590 file = dir_flist->files[cur_flist->parent_ndx];
591 fname = local_name ? local_name : f_name(file, fbuf);
593 if (DEBUG_GTE(RECV, 1))
594 rprintf(FINFO, "recv_files(%s)\n", fname);
596 if (daemon_filter_list.head && (*fname != '.' || fname[1] != '\0')) {
597 int filt_flags = S_ISDIR(file->mode) ? NAME_IS_DIR : NAME_IS_FILE;
598 if (check_filter(&daemon_filter_list, FLOG, fname, filt_flags) < 0) {
599 rprintf(FERROR, "ERROR: rejecting file transfer request for daemon excluded file: %s\n",
600 fname);
601 exit_cleanup(RERR_PROTOCOL);
605 #ifdef SUPPORT_XATTRS
606 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
607 && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
608 recv_xattr_request(file, f_in);
609 #endif
611 if (!(iflags & ITEM_TRANSFER)) {
612 maybe_log_item(file, iflags, itemizing, xname);
613 #ifdef SUPPORT_XATTRS
614 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
615 && !BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE))
616 set_file_attrs(fname, file, NULL, fname, 0);
617 #endif
618 if (iflags & ITEM_IS_NEW) {
619 stats.created_files++;
620 if (S_ISREG(file->mode)) {
621 /* Nothing further to count. */
622 } else if (S_ISDIR(file->mode))
623 stats.created_dirs++;
624 #ifdef SUPPORT_LINKS
625 else if (S_ISLNK(file->mode))
626 stats.created_symlinks++;
627 #endif
628 else if (IS_DEVICE(file->mode))
629 stats.created_devices++;
630 else
631 stats.created_specials++;
633 continue;
635 if (phase == 2) {
636 rprintf(FERROR,
637 "got transfer request in phase 2 [%s]\n",
638 who_am_i());
639 exit_cleanup(RERR_PROTOCOL);
642 if (file->flags & FLAG_FILE_SENT) {
643 if (csum_length == SHORT_SUM_LENGTH) {
644 if (keep_partial && !partial_dir)
645 make_backups = -make_backups; /* prevents double backup */
646 if (append_mode)
647 sparse_files = -sparse_files;
648 append_mode = -append_mode;
649 csum_length = SUM_LENGTH;
650 redoing = 1;
652 } else {
653 if (csum_length != SHORT_SUM_LENGTH) {
654 if (keep_partial && !partial_dir)
655 make_backups = -make_backups;
656 if (append_mode)
657 sparse_files = -sparse_files;
658 append_mode = -append_mode;
659 csum_length = SHORT_SUM_LENGTH;
660 redoing = 0;
662 if (iflags & ITEM_IS_NEW)
663 stats.created_files++;
666 if (!am_server)
667 set_current_file_index(file, ndx);
668 stats.xferred_files++;
669 stats.total_transferred_size += F_LENGTH(file);
671 cleanup_got_literal = 0;
673 if (read_batch) {
674 int wanted = redoing
675 ? we_want_redo(ndx)
676 : gen_wants_ndx(ndx, cur_flist->flist_num);
677 if (!wanted) {
678 rprintf(FINFO,
679 "(Skipping batched update for%s \"%s\")\n",
680 redoing ? " resend of" : "",
681 fname);
682 discard_receive_data(f_in, file);
683 file->flags |= FLAG_FILE_SENT;
684 continue;
688 remember_initial_stats();
690 if (!do_xfers) { /* log the transfer */
691 log_item(FCLIENT, file, iflags, NULL);
692 if (read_batch)
693 discard_receive_data(f_in, file);
694 continue;
696 if (write_batch < 0) {
697 log_item(FCLIENT, file, iflags, NULL);
698 if (!am_server)
699 discard_receive_data(f_in, file);
700 if (inc_recurse)
701 send_msg_success(fname, ndx);
702 continue;
705 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
707 if (protocol_version >= 29) {
708 switch (fnamecmp_type) {
709 case FNAMECMP_FNAME:
710 fnamecmp = fname;
711 break;
712 case FNAMECMP_PARTIAL_DIR:
713 fnamecmp = partialptr;
714 break;
715 case FNAMECMP_BACKUP:
716 fnamecmp = get_backup_name(fname);
717 break;
718 case FNAMECMP_FUZZY:
719 if (file->dirname) {
720 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, file->dirname, xname);
721 fnamecmp = fnamecmpbuf;
722 } else
723 fnamecmp = xname;
724 break;
725 default:
726 if (fnamecmp_type > FNAMECMP_FUZZY && fnamecmp_type-FNAMECMP_FUZZY <= basis_dir_cnt) {
727 fnamecmp_type -= FNAMECMP_FUZZY + 1;
728 if (file->dirname) {
729 stringjoin(fnamecmpbuf, sizeof fnamecmpbuf,
730 basis_dir[fnamecmp_type], "/", file->dirname, "/", xname, NULL);
731 } else
732 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, basis_dir[fnamecmp_type], xname);
733 } else if (fnamecmp_type >= basis_dir_cnt) {
734 rprintf(FERROR,
735 "invalid basis_dir index: %d.\n",
736 fnamecmp_type);
737 exit_cleanup(RERR_PROTOCOL);
738 } else
739 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, basis_dir[fnamecmp_type], fname);
740 fnamecmp = fnamecmpbuf;
741 break;
743 if (!fnamecmp || (daemon_filter_list.head
744 && check_filter(&daemon_filter_list, FLOG, fnamecmp, 0) < 0)) {
745 fnamecmp = fname;
746 fnamecmp_type = FNAMECMP_FNAME;
748 } else {
749 /* Reminder: --inplace && --partial-dir are never
750 * enabled at the same time. */
751 if (inplace && make_backups > 0) {
752 if (!(fnamecmp = get_backup_name(fname)))
753 fnamecmp = fname;
754 else
755 fnamecmp_type = FNAMECMP_BACKUP;
756 } else if (partial_dir && partialptr)
757 fnamecmp = partialptr;
758 else
759 fnamecmp = fname;
762 /* open the file */
763 fd1 = do_open(fnamecmp, O_RDONLY, 0);
765 if (fd1 == -1 && protocol_version < 29) {
766 if (fnamecmp != fname) {
767 fnamecmp = fname;
768 fnamecmp_type = FNAMECMP_FNAME;
769 fd1 = do_open(fnamecmp, O_RDONLY, 0);
772 if (fd1 == -1 && basis_dir[0]) {
773 /* pre-29 allowed only one alternate basis */
774 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
775 basis_dir[0], fname);
776 fnamecmp = fnamecmpbuf;
777 fnamecmp_type = FNAMECMP_BASIS_DIR_LOW;
778 fd1 = do_open(fnamecmp, O_RDONLY, 0);
782 one_inplace = inplace_partial && fnamecmp_type == FNAMECMP_PARTIAL_DIR;
783 updating_basis_or_equiv = one_inplace
784 || (inplace && (fnamecmp == fname || fnamecmp_type == FNAMECMP_BACKUP));
786 if (fd1 == -1) {
787 st.st_mode = 0;
788 st.st_size = 0;
789 } else if (do_fstat(fd1,&st) != 0) {
790 rsyserr(FERROR_XFER, errno, "fstat %s failed",
791 full_fname(fnamecmp));
792 discard_receive_data(f_in, file);
793 close(fd1);
794 if (inc_recurse)
795 send_msg_int(MSG_NO_SEND, ndx);
796 continue;
799 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
800 /* this special handling for directories
801 * wouldn't be necessary if robust_rename()
802 * and the underlying robust_unlink could cope
803 * with directories
805 rprintf(FERROR_XFER, "recv_files: %s is a directory\n",
806 full_fname(fnamecmp));
807 discard_receive_data(f_in, file);
808 close(fd1);
809 if (inc_recurse)
810 send_msg_int(MSG_NO_SEND, ndx);
811 continue;
814 if (write_devices && IS_DEVICE(st.st_mode)) {
815 if (fd1 != -1 && st.st_size == 0)
816 st.st_size = get_device_size(fd1, fname);
817 /* Mark the file entry as a device so that we don't try to truncate it later on. */
818 file->mode = S_IFBLK | (file->mode & ACCESSPERMS);
819 } else if (fd1 != -1 && !(S_ISREG(st.st_mode))) {
820 close(fd1);
821 fd1 = -1;
824 /* If we're not preserving permissions, change the file-list's
825 * mode based on the local permissions and some heuristics. */
826 if (!preserve_perms) {
827 int exists = fd1 != -1;
828 #ifdef SUPPORT_ACLS
829 const char *dn = file->dirname ? file->dirname : ".";
830 if (parent_dirname != dn
831 && strcmp(parent_dirname, dn) != 0) {
832 dflt_perms = default_perms_for_dir(dn);
833 parent_dirname = dn;
835 #endif
836 file->mode = dest_mode(file->mode, st.st_mode, dflt_perms, exists);
839 /* We now check to see if we are writing the file "inplace" */
840 if (inplace || one_inplace) {
841 fnametmp = one_inplace ? partialptr : fname;
842 fd2 = do_open(fnametmp, O_WRONLY|O_CREAT, 0600);
843 #ifdef linux
844 if (fd2 == -1 && errno == EACCES) {
845 /* Maybe the error was due to protected_regular setting? */
846 fd2 = do_open(fname, O_WRONLY, 0600);
848 #endif
849 if (fd2 == -1) {
850 rsyserr(FERROR_XFER, errno, "open %s failed",
851 full_fname(fnametmp));
852 } else if (updating_basis_or_equiv)
853 cleanup_set(NULL, NULL, file, fd1, fd2);
854 } else {
855 fnametmp = fnametmpbuf;
856 fd2 = open_tmpfile(fnametmp, fname, file);
857 if (fd2 != -1)
858 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
861 if (fd2 == -1) {
862 discard_receive_data(f_in, file);
863 if (fd1 != -1)
864 close(fd1);
865 if (inc_recurse)
866 send_msg_int(MSG_NO_SEND, ndx);
867 continue;
870 /* log the transfer */
871 if (log_before_transfer)
872 log_item(FCLIENT, file, iflags, NULL);
873 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
874 rprintf(FINFO, "%s\n", fname);
876 /* recv file data */
877 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size, fname, fd2, file, inplace || one_inplace);
879 log_item(log_code, file, iflags, NULL);
880 if (want_progress_now)
881 instant_progress(fname);
883 if (fd1 != -1)
884 close(fd1);
885 if (close(fd2) < 0) {
886 rsyserr(FERROR, errno, "close failed on %s",
887 full_fname(fnametmp));
888 exit_cleanup(RERR_FILEIO);
891 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
892 if (partialptr == fname)
893 partialptr = NULL;
894 if (!finish_transfer(fname, fnametmp, fnamecmp, partialptr, file, recv_ok, 1))
895 recv_ok = -1;
896 else if (fnamecmp == partialptr) {
897 if (!one_inplace)
898 do_unlink(partialptr);
899 handle_partial_dir(partialptr, PDIR_DELETE);
901 } else if (keep_partial && partialptr && (!one_inplace || delay_updates)) {
902 if (!handle_partial_dir(partialptr, PDIR_CREATE)) {
903 rprintf(FERROR,
904 "Unable to create partial-dir for %s -- discarding %s.\n",
905 local_name ? local_name : f_name(file, NULL),
906 recv_ok ? "completed file" : "partial file");
907 do_unlink(fnametmp);
908 recv_ok = -1;
909 } else if (!finish_transfer(partialptr, fnametmp, fnamecmp, NULL,
910 file, recv_ok, !partial_dir))
911 recv_ok = -1;
912 else if (delay_updates && recv_ok) {
913 bitbag_set_bit(delayed_bits, ndx);
914 recv_ok = 2;
915 } else
916 partialptr = NULL;
917 } else if (!one_inplace)
918 do_unlink(fnametmp);
920 cleanup_disable();
922 if (read_batch)
923 file->flags |= FLAG_FILE_SENT;
925 switch (recv_ok) {
926 case 2:
927 break;
928 case 1:
929 if (remove_source_files || inc_recurse || (preserve_hard_links && F_IS_HLINKED(file)))
930 send_msg_success(fname, ndx);
931 break;
932 case 0: {
933 enum logcode msgtype = redoing ? FERROR_XFER : FWARNING;
934 if (msgtype == FERROR_XFER || INFO_GTE(NAME, 1) || stdout_format_has_i) {
935 char *errstr, *redostr, *keptstr;
936 if (!(keep_partial && partialptr) && !inplace)
937 keptstr = "discarded";
938 else if (partial_dir)
939 keptstr = "put into partial-dir";
940 else
941 keptstr = "retained";
942 if (msgtype == FERROR_XFER) {
943 errstr = "ERROR";
944 redostr = "";
945 } else {
946 errstr = "WARNING";
947 redostr = read_batch ? " (may try again)"
948 : " (will try again)";
950 rprintf(msgtype,
951 "%s: %s failed verification -- update %s%s.\n",
952 errstr, local_name ? f_name(file, NULL) : fname,
953 keptstr, redostr);
955 if (!redoing) {
956 if (read_batch)
957 flist_ndx_push(&batch_redo_list, ndx);
958 send_msg_int(MSG_REDO, ndx);
959 file->flags |= FLAG_FILE_SENT;
960 } else if (inc_recurse)
961 send_msg_int(MSG_NO_SEND, ndx);
962 break;
964 case -1:
965 if (inc_recurse)
966 send_msg_int(MSG_NO_SEND, ndx);
967 break;
970 if (make_backups < 0)
971 make_backups = -make_backups;
973 if (phase == 2 && delay_updates) /* for protocol_version < 29 */
974 handle_delayed_updates(local_name);
976 if (DEBUG_GTE(RECV, 1))
977 rprintf(FINFO,"recv_files finished\n");
979 return 0;