2 * Routines that are exclusive to the generator process.
4 * Copyright (C) 1996-2000 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2003-2009 Wayne Davison
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, visit the http://fsf.org website.
29 extern int stdout_format_has_i;
30 extern int logfile_format_has_i;
34 extern int inc_recurse;
35 extern int relative_paths;
36 extern int implied_dirs;
37 extern int keep_dirlinks;
38 extern int preserve_acls;
39 extern int preserve_xattrs;
40 extern int preserve_links;
41 extern int preserve_devices;
42 extern int preserve_specials;
43 extern int preserve_hard_links;
44 extern int preserve_executability;
45 extern int preserve_perms;
46 extern int preserve_times;
47 extern int delete_mode;
48 extern int delete_before;
49 extern int delete_during;
50 extern int delete_after;
51 extern int missing_args;
52 extern int msgdone_cnt;
53 extern int ignore_errors;
54 extern int remove_source_files;
55 extern int delay_updates;
56 extern int update_only;
57 extern int human_readable;
58 extern int ignore_existing;
59 extern int ignore_non_existing;
61 extern int append_mode;
62 extern int make_backups;
63 extern int csum_length;
64 extern int ignore_times;
66 extern OFF_T max_size;
67 extern OFF_T min_size;
70 extern int allowed_lull;
71 extern int sock_f_out;
72 extern int protocol_version;
73 extern int file_total;
74 extern int fuzzy_basis;
75 extern int always_checksum;
76 extern int checksum_len;
77 extern char *partial_dir;
78 extern int compare_dest;
81 extern int whole_file;
83 extern int read_batch;
84 extern int safe_symlinks;
85 extern long block_size; /* "long" because popt can't set an int32. */
86 extern int unsort_ndx;
87 extern int max_delete;
88 extern int force_delete;
89 extern int one_file_system;
90 extern int skipped_deletes;
91 extern dev_t filesystem_dev;
92 extern mode_t orig_umask;
95 extern char *basis_dir[MAX_BASIS_DIRS+1];
96 extern struct file_list *cur_flist, *first_flist, *dir_flist;
97 extern filter_rule_list filter_list, daemon_filter_list;
99 int maybe_ATTRS_REPORT = 0;
101 static dev_t dev_zero;
102 static int deldelay_size = 0, deldelay_cnt = 0;
103 static char *deldelay_buf = NULL;
104 static int deldelay_fd = -1;
105 static int loopchk_limit;
106 static int dir_tweaking;
107 static int symlink_timeset_failed_flags;
108 static int need_retouch_dir_times;
109 static int need_retouch_dir_perms;
110 static const char *solo_file = NULL;
113 TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
116 /* Forward declarations. */
117 #ifdef SUPPORT_HARD_LINKS
118 static void handle_skipped_hlink(struct file_struct *file, int itemizing,
119 enum logcode code, int f_out);
122 #define EARLY_DELAY_DONE_MSG() (!delay_updates)
123 #define EARLY_DELETE_DONE_MSG() (!(delete_during == 2 || delete_after))
125 static int start_delete_delay_temp(void)
127 char fnametmp[MAXPATHLEN];
128 int save_dry_run = dry_run;
131 if (!get_tmpname(fnametmp, "deldelay", False)
132 || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
133 rprintf(FINFO, "NOTE: Unable to create delete-delay temp file%s.\n",
134 inc_recurse ? "" : " -- switching to --delete-after");
136 delete_after = !inc_recurse;
137 dry_run = save_dry_run;
141 dry_run = save_dry_run;
145 static int flush_delete_delay(void)
147 if (deldelay_fd < 0 && !start_delete_delay_temp())
149 if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
150 rsyserr(FERROR, errno, "flush of delete-delay buffer");
152 delete_after = !inc_recurse;
160 static int remember_delete(struct file_struct *file, const char *fname, int flags)
164 if (deldelay_cnt == deldelay_size && !flush_delete_delay())
167 if (flags & DEL_NO_UID_WRITE)
168 deldelay_buf[deldelay_cnt++] = '!';
171 len = snprintf(deldelay_buf + deldelay_cnt,
172 deldelay_size - deldelay_cnt,
174 (int)file->mode, fname, '\0');
175 if ((deldelay_cnt += len) <= deldelay_size)
178 if (!flush_delete_delay())
185 static int read_delay_line(char *buf, int *flags_p)
187 static int read_pos = 0;
189 char *bp, *past_space;
192 for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
193 if (j < deldelay_cnt)
195 if (deldelay_fd < 0) {
200 deldelay_cnt -= read_pos;
201 if (deldelay_cnt == deldelay_size)
203 if (deldelay_cnt && read_pos) {
204 memmove(deldelay_buf, deldelay_buf + read_pos,
207 len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
208 deldelay_size - deldelay_cnt);
212 "ERROR: unexpected EOF in delete-delay file.\n");
217 rsyserr(FERROR, errno,
218 "reading delete-delay file");
225 bp = deldelay_buf + read_pos;
228 *flags_p = DEL_NO_UID_WRITE;
232 if (sscanf(bp, "%x ", &mode) != 1) {
234 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
237 past_space = strchr(bp, ' ') + 1;
238 len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
241 if (len > MAXPATHLEN) {
242 rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
246 /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
247 * instead of returning a pointer to our buffer. */
248 memcpy(buf, past_space, len);
253 static void do_delayed_deletions(char *delbuf)
257 if (deldelay_fd >= 0) {
258 if (deldelay_cnt && !flush_delete_delay())
260 lseek(deldelay_fd, 0, 0);
262 while ((mode = read_delay_line(delbuf, &flags)) >= 0)
263 delete_item(delbuf, mode, flags | DEL_RECURSE);
264 if (deldelay_fd >= 0)
268 /* This function is used to implement per-directory deletion, and is used by
269 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
270 * MAXPATHLEN buffer with the name of the directory in it (the functions we
271 * call will append names onto the end, but the old dir value will be restored
273 static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
275 static int already_warned = 0;
276 struct file_list *dirlist;
277 char delbuf[MAXPATHLEN];
279 int save_uid_ndx = uid_ndx;
282 change_local_filter_dir(NULL, 0, 0);
286 if (DEBUG_GTE(DEL, 2))
287 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
290 maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
292 if (io_error && !ignore_errors) {
296 "IO error encountered -- skipping file deletion\n");
302 change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
304 if (one_file_system) {
305 if (file->flags & FLAG_TOP_DIR)
306 filesystem_dev = *fs_dev;
307 else if (filesystem_dev != *fs_dev)
312 uid_ndx = ++file_extra_cnt;
314 dirlist = get_dirlist(fbuf, dlen, 0);
316 /* If an item in dirlist is not found in flist, delete it
317 * from the filesystem. */
318 for (i = dirlist->used; i--; ) {
319 struct file_struct *fp = dirlist->files[i];
320 if (!F_IS_ACTIVE(fp))
322 if (fp->flags & FLAG_MOUNT_DIR && S_ISDIR(fp->mode)) {
323 if (INFO_GTE(MOUNT, 1))
324 rprintf(FINFO, "cannot delete mount point: %s\n",
328 /* Here we want to match regardless of file type. Replacement
329 * of a file with one of another type is handled separately by
330 * a delete_item call with a DEL_MAKE_ROOM flag. */
331 if (flist_find_ignore_dirness(cur_flist, fp) < 0) {
332 int flags = DEL_RECURSE;
333 if (!(fp->mode & S_IWUSR) && !am_root && (uid_t)F_OWNER(fp) == our_uid)
334 flags |= DEL_NO_UID_WRITE;
336 if (delete_during == 2) {
337 if (!remember_delete(fp, delbuf, flags))
340 delete_item(delbuf, fp->mode, flags);
352 /* This deletes any files on the receiving side that are not present on the
353 * sending side. This is used by --delete-before and --delete-after. */
354 static void do_delete_pass(void)
356 char fbuf[MAXPATHLEN];
360 /* dry_run is incremented when the destination doesn't exist yet. */
361 if (dry_run > 1 || list_only)
364 for (j = 0; j < cur_flist->used; j++) {
365 struct file_struct *file = cur_flist->sorted[j];
369 if (!(file->flags & FLAG_CONTENT_DIR)) {
370 change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(file));
374 if (DEBUG_GTE(DEL, 1) && file->flags & FLAG_TOP_DIR)
375 rprintf(FINFO, "deleting in %s\n", fbuf);
377 if (link_stat(fbuf, &st, keep_dirlinks) < 0
378 || !S_ISDIR(st.st_mode))
381 delete_in_dir(fbuf, file, &st.st_dev);
383 delete_in_dir(NULL, NULL, &dev_zero);
385 if (INFO_GTE(FLIST, 2) && !am_server)
386 rprintf(FINFO, " \r");
389 static inline int time_differs(struct file_struct *file, stat_x *sxp)
391 return cmp_time(sxp->st.st_mtime, file->modtime);
394 static inline int perms_differ(struct file_struct *file, stat_x *sxp)
397 return !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS);
399 if (preserve_executability)
400 return (sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0);
405 static inline int ownership_differs(struct file_struct *file, stat_x *sxp)
407 if (am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file))
410 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
417 static inline int acls_differ(const char *fname, struct file_struct *file, stat_x *sxp)
420 if (!ACL_READY(*sxp))
422 if (set_acl(NULL, file, sxp, file->mode))
430 #ifdef SUPPORT_XATTRS
431 static inline int xattrs_differ(const char *fname, struct file_struct *file, stat_x *sxp)
433 if (preserve_xattrs) {
434 if (!XATTR_READY(*sxp))
435 get_xattr(fname, sxp);
436 if (xattr_diff(file, sxp, 0))
444 int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
446 if (S_ISLNK(file->mode)) {
447 #ifdef CAN_SET_SYMLINK_TIMES
448 if (preserve_times & PRESERVE_LINK_TIMES && time_differs(file, sxp))
451 #ifdef CAN_CHMOD_SYMLINK
452 if (perms_differ(file, sxp))
455 #ifndef CAN_CHOWN_SYMLINK
456 if (ownership_differs(file, sxp))
459 #if defined SUPPORT_ACLS && 0 /* no current symlink-ACL support */
460 if (acls_differ(fname, file, sxp))
463 #if defined SUPPORT_XATTRS && !defined NO_SYMLINK_XATTRS
464 if (xattrs_differ(fname, file, sxp))
468 if (preserve_times && time_differs(file, sxp))
470 if (perms_differ(file, sxp))
472 if (ownership_differs(file, sxp))
475 if (acls_differ(fname, file, sxp))
478 #ifdef SUPPORT_XATTRS
479 if (xattrs_differ(fname, file, sxp))
487 void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
488 stat_x *sxp, int32 iflags, uchar fnamecmp_type,
491 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
492 int keep_time = !preserve_times ? 0
493 : S_ISDIR(file->mode) ? preserve_times & PRESERVE_DIR_TIMES
494 : S_ISLNK(file->mode) ? preserve_times & PRESERVE_LINK_TIMES
497 if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
498 iflags |= ITEM_REPORT_SIZE;
499 if (file->flags & FLAG_TIME_FAILED) { /* symlinks only */
500 if (iflags & ITEM_LOCAL_CHANGE)
501 iflags |= symlink_timeset_failed_flags;
503 ? cmp_time(file->modtime, sxp->st.st_mtime) != 0
504 : iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !(iflags & ITEM_MATCHED)
505 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
506 iflags |= ITEM_REPORT_TIME;
507 #if !defined HAVE_LCHMOD && !defined HAVE_SETATTRLIST
508 if (S_ISLNK(file->mode)) {
512 if (preserve_perms) {
513 if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
514 iflags |= ITEM_REPORT_PERMS;
515 } else if (preserve_executability
516 && ((sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0)))
517 iflags |= ITEM_REPORT_PERMS;
518 if (uid_ndx && am_root && (uid_t)F_OWNER(file) != sxp->st.st_uid)
519 iflags |= ITEM_REPORT_OWNER;
520 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
521 && sxp->st.st_gid != (gid_t)F_GROUP(file))
522 iflags |= ITEM_REPORT_GROUP;
524 if (preserve_acls && !S_ISLNK(file->mode)) {
525 if (!ACL_READY(*sxp))
526 get_acl(fnamecmp, sxp);
527 if (set_acl(NULL, file, sxp, file->mode))
528 iflags |= ITEM_REPORT_ACL;
531 #ifdef SUPPORT_XATTRS
532 if (preserve_xattrs) {
533 if (!XATTR_READY(*sxp))
534 get_xattr(fnamecmp, sxp);
535 if (xattr_diff(file, sxp, 1))
536 iflags |= ITEM_REPORT_XATTR;
540 #ifdef SUPPORT_XATTRS
541 if (preserve_xattrs && xattr_diff(file, NULL, 1))
542 iflags |= ITEM_REPORT_XATTR;
544 iflags |= ITEM_IS_NEW;
548 if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || INFO_GTE(NAME, 2)
549 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
550 if (protocol_version >= 29) {
552 write_ndx(sock_f_out, ndx);
553 write_shortint(sock_f_out, iflags);
554 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
555 write_byte(sock_f_out, fnamecmp_type);
556 if (iflags & ITEM_XNAME_FOLLOWS)
557 write_vstring(sock_f_out, xname, strlen(xname));
558 #ifdef SUPPORT_XATTRS
559 if (preserve_xattrs && do_xfers
560 && iflags & (ITEM_REPORT_XATTR|ITEM_TRANSFER)) {
561 send_xattr_request(NULL, file,
562 iflags & ITEM_REPORT_XATTR ? sock_f_out : -1);
565 } else if (ndx >= 0) {
566 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
567 log_item(code, file, iflags, xname);
573 /* Perform our quick-check heuristic for determining if a file is unchanged. */
574 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
576 if (st->st_size != F_LENGTH(file))
579 /* if always checksum is set then we use the checksum instead
580 of the file time to determine whether to sync */
581 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
582 char sum[MAX_DIGEST_LEN];
583 file_checksum(fn, sum, st->st_size);
584 return memcmp(sum, F_SUM(file), checksum_len) == 0;
593 return cmp_time(st->st_mtime, file->modtime) == 0;
598 * set (initialize) the size entries in the per-file sum_struct
599 * calculating dynamic block and checksum sizes.
601 * This is only called from generate_and_send_sums() but is a separate
602 * function to encapsulate the logic.
604 * The block size is a rounded square root of file length.
606 * The checksum size is determined according to:
607 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
608 * provided by Donovan Baarda which gives a probability of rsync
609 * algorithm corrupting data and falling back using the whole md4
612 * This might be made one of several selectable heuristics.
614 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
621 /* The file length overflowed our int64 var, so we can't process this file. */
622 sum->count = -1; /* indicate overflow error */
627 blength = block_size;
628 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
629 blength = BLOCK_SIZE;
631 int32 max_blength = protocol_version < 30 ? OLD_MAX_BLOCK_SIZE : MAX_BLOCK_SIZE;
634 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
635 if (c < 0 || c >= max_blength)
636 blength = max_blength;
641 if (len < (int64)blength * blength)
644 } while (c >= 8); /* round to multiple of 8 */
645 blength = MAX(blength, BLOCK_SIZE);
649 if (protocol_version < 27) {
650 s2length = csum_length;
651 } else if (csum_length == SUM_LENGTH) {
652 s2length = SUM_LENGTH;
655 int b = BLOCKSUM_BIAS;
656 for (l = len; l >>= 1; b += 2) {}
657 for (c = blength; (c >>= 1) && b; b--) {}
658 /* add a bit, subtract rollsum, round up. */
659 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
660 s2length = MAX(s2length, csum_length);
661 s2length = MIN(s2length, SUM_LENGTH);
665 sum->blength = blength;
666 sum->s2length = s2length;
667 sum->remainder = (int32)(len % blength);
668 sum->count = (int32)(l = (len / blength) + (sum->remainder != 0));
670 if ((int64)sum->count != l)
673 if (sum->count && DEBUG_GTE(DELTASUM, 2)) {
675 "count=%s rem=%ld blength=%ld s2length=%d flength=%s\n",
676 big_num(sum->count), (long)sum->remainder, (long)sum->blength,
677 sum->s2length, big_num(sum->flength));
683 * Generate and send a stream of signatures/checksums that describe a buffer
685 * Generate approximately one checksum every block_len bytes.
687 static int generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
690 struct map_struct *mapbuf;
691 struct sum_struct sum;
694 sum_sizes_sqroot(&sum, len);
697 write_sum_head(f_out, &sum);
699 if (append_mode > 0 && f_copy < 0)
703 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
707 for (i = 0; i < sum.count; i++) {
708 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
709 char *map = map_ptr(mapbuf, offset, n1);
710 char sum2[SUM_LENGTH];
717 full_write(f_copy, map, n1);
722 sum1 = get_checksum1(map, n1);
723 get_checksum2(map, n1, sum2);
725 if (DEBUG_GTE(DELTASUM, 3)) {
727 "chunk[%s] offset=%s len=%ld sum1=%08lx\n",
728 big_num(i), big_num(offset - n1), (long)n1,
729 (unsigned long)sum1);
731 write_int(f_out, sum1);
732 write_buf(f_out, sum2, sum.s2length);
742 /* Try to find a filename in the same dir as "fname" with a similar name. */
743 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
745 int fname_len, fname_suf_len;
746 const char *fname_suf, *fname = file->basename;
747 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
748 int j, lowest_j = -1;
750 fname_len = strlen(fname);
751 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
753 for (j = 0; j < dirlist->used; j++) {
754 struct file_struct *fp = dirlist->files[j];
755 const char *suf, *name;
759 if (!S_ISREG(fp->mode) || !F_LENGTH(fp)
760 || fp->flags & FLAG_FILE_SENT)
765 if (F_LENGTH(fp) == F_LENGTH(file)
766 && cmp_time(fp->modtime, file->modtime) == 0) {
767 if (DEBUG_GTE(FUZZY, 2)) {
769 "fuzzy size/modtime match for %s\n",
776 suf = find_filename_suffix(name, len, &suf_len);
778 dist = fuzzy_distance(name, len, fname, fname_len);
779 /* Add some extra weight to how well the suffixes match. */
780 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
782 if (DEBUG_GTE(FUZZY, 2)) {
783 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
784 name, (int)(dist>>16), (int)(dist&0xFFFF));
786 if (dist <= lowest_dist) {
795 /* Copy a file found in our --copy-dest handling. */
796 static int copy_altdest_file(const char *src, const char *dest, struct file_struct *file)
798 char buf[MAXPATHLEN];
799 const char *copy_to, *partialptr;
800 int save_preserve_xattrs = preserve_xattrs;
804 /* Let copy_file open the destination in place. */
808 fd_w = open_tmpfile(buf, dest, file);
813 cleanup_set(copy_to, NULL, NULL, -1, -1);
814 if (copy_file(src, copy_to, fd_w, file->mode) < 0) {
815 if (INFO_GTE(COPY, 1)) {
816 rsyserr(FINFO, errno, "copy_file %s => %s",
817 full_fname(src), copy_to);
819 /* Try to clean up. */
824 partialptr = partial_dir ? partial_dir_fname(dest) : NULL;
825 preserve_xattrs = 0; /* xattrs were copied with file */
826 ok = finish_transfer(dest, copy_to, src, partialptr, file, 1, 0);
827 preserve_xattrs = save_preserve_xattrs;
832 /* This is only called for regular files. We return -2 if we've finished
833 * handling the file, -1 if no dest-linking occurred, or a non-negative
834 * value if we found an alternate basis file. */
835 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
836 char *cmpbuf, stat_x *sxp, int itemizing,
844 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
845 if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
847 switch (match_level) {
853 if (!unchanged_file(cmpbuf, file, &sxp->st))
859 if (!unchanged_attrs(cmpbuf, file, sxp))
866 } while (basis_dir[++j] != NULL);
871 if (j != best_match) {
873 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
874 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
878 if (match_level == 3 && !copy_dest) {
879 #ifdef SUPPORT_HARD_LINKS
881 if (!hard_link_one(file, fname, cmpbuf, 1))
883 if (preserve_hard_links && F_IS_HLINKED(file))
884 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, j);
885 if (!maybe_ATTRS_REPORT && (INFO_GTE(NAME, 2) || stdout_format_has_i > 1)) {
886 itemize(cmpbuf, file, ndx, 1, sxp,
887 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
893 itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
894 if (INFO_GTE(NAME, 2) && maybe_ATTRS_REPORT)
895 rprintf(FCLIENT, "%s is uptodate\n", fname);
899 if (match_level >= 2) {
900 #ifdef SUPPORT_HARD_LINKS
901 try_a_copy: /* Copy the file locally. */
903 if (!dry_run && copy_altdest_file(cmpbuf, fname, file) < 0)
906 itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
907 if (maybe_ATTRS_REPORT
908 && ((!itemizing && INFO_GTE(NAME, 1) && match_level == 2)
909 || (INFO_GTE(NAME, 2) && match_level == 3))) {
910 code = match_level == 3 ? FCLIENT : FINFO;
911 rprintf(code, "%s%s\n", fname,
912 match_level == 3 ? " is uptodate" : "");
914 #ifdef SUPPORT_HARD_LINKS
915 if (preserve_hard_links && F_IS_HLINKED(file))
916 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, -1);
921 return FNAMECMP_BASIS_DIR_LOW + j;
924 /* This is only called for non-regular files. We return -2 if we've finished
925 * handling the file, or -1 if no dest-linking occurred, or a non-negative
926 * value if we found an alternate basis file. */
927 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
928 char *cmpbuf, stat_x *sxp, int itemizing,
933 enum nonregtype type;
936 char lnk[MAXPATHLEN];
941 #ifndef SUPPORT_LINKS
942 if (S_ISLNK(file->mode))
945 if (S_ISDIR(file->mode)) {
947 } else if (IS_SPECIAL(file->mode))
949 else if (IS_DEVICE(file->mode))
952 else if (S_ISLNK(file->mode))
957 "internal: try_dests_non() called with invalid mode (%o)\n",
959 exit_cleanup(RERR_UNSUPPORTED);
963 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
964 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
968 if (!S_ISDIR(sxp->st.st_mode))
972 if (!IS_SPECIAL(sxp->st.st_mode))
976 if (!IS_DEVICE(sxp->st.st_mode))
981 if (!S_ISLNK(sxp->st.st_mode))
988 if (match_level < 1) {
997 devp = F_RDEV_P(file);
998 if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
1002 #ifdef SUPPORT_LINKS
1003 if ((len = do_readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
1006 if (strcmp(lnk, F_SYMLINK(file)) != 0)
1013 if (match_level < 2) {
1017 if (unchanged_attrs(cmpbuf, file, sxp)) {
1022 } while (basis_dir[++j] != NULL);
1027 if (j != best_match) {
1029 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1030 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1034 if (match_level == 3) {
1035 #ifdef SUPPORT_HARD_LINKS
1037 #ifndef CAN_HARDLINK_SYMLINK
1038 && !S_ISLNK(file->mode)
1040 #ifndef CAN_HARDLINK_SPECIAL
1041 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
1043 && !S_ISDIR(file->mode)) {
1044 if (do_link(cmpbuf, fname) < 0) {
1045 rsyserr(FERROR_XFER, errno,
1046 "failed to hard-link %s with %s",
1050 if (preserve_hard_links && F_IS_HLINKED(file))
1051 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1055 if (itemizing && stdout_format_has_i
1056 && (INFO_GTE(NAME, 2) || stdout_format_has_i > 1)) {
1057 int chg = compare_dest && type != TYPE_DIR ? 0
1058 : ITEM_LOCAL_CHANGE + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1059 char *lp = match_level == 3 ? "" : NULL;
1060 itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
1062 if (INFO_GTE(NAME, 2) && maybe_ATTRS_REPORT) {
1063 rprintf(FCLIENT, "%s%s is uptodate\n",
1064 fname, type == TYPE_DIR ? "/" : "");
1072 static void list_file_entry(struct file_struct *f)
1074 char permbuf[PERMSTRING_SIZE];
1076 int colwidth = human_readable ? 14 : 11;
1078 if (!F_IS_ACTIVE(f)) {
1079 /* this can happen if duplicate names were removed */
1083 permstring(permbuf, f->mode);
1086 /* TODO: indicate '+' if the entry has an ACL. */
1088 #ifdef SUPPORT_LINKS
1089 if (preserve_links && S_ISLNK(f->mode)) {
1090 rprintf(FINFO, "%s %*s %s %s -> %s\n",
1091 permbuf, colwidth, human_num(len),
1092 timestring(f->modtime), f_name(f, NULL),
1096 if (missing_args == 2 && f->mode == 0) {
1097 rprintf(FINFO, "%-*s %s\n",
1098 colwidth + 31, "*missing",
1101 rprintf(FINFO, "%s %*s %s %s\n",
1102 permbuf, colwidth, human_num(len),
1103 timestring(f->modtime), f_name(f, NULL));
1107 static int phase = 0;
1108 static int dflt_perms;
1110 static int implied_dirs_are_missing;
1111 /* Helper for recv_generator's skip_dir and dry_missing_dir tests. */
1112 static BOOL is_below(struct file_struct *file, struct file_struct *subtree)
1114 return F_DEPTH(file) > F_DEPTH(subtree)
1115 && (!implied_dirs_are_missing || f_name_has_prefix(file, subtree));
1118 /* Acts on the indicated item in cur_flist whose name is fname. If a dir,
1119 * make sure it exists, and has the right permissions/timestamp info. For
1120 * all other non-regular files (symlinks, etc.) we create them here. For
1121 * regular files that have changed, we try to find a basis file and then
1122 * start sending checksums. The ndx is the file's unique index value.
1124 * The fname parameter must point to a MAXPATHLEN buffer! (e.g it gets
1125 * passed to delete_item(), which can use it during a recursive delete.)
1127 * Note that f_out is set to -1 when doing final directory-permission and
1128 * modification-time repair. */
1129 static void recv_generator(char *fname, struct file_struct *file, int ndx,
1130 int itemizing, enum logcode code, int f_out)
1132 static const char *parent_dirname = "";
1133 /* Missing dir not created due to --dry-run; will still be scanned. */
1134 static struct file_struct *dry_missing_dir = NULL;
1135 /* Missing dir whose contents are skipped altogether due to
1136 * --ignore-non-existing, daemon exclude, or mkdir failure. */
1137 static struct file_struct *skip_dir = NULL;
1138 static struct file_list *fuzzy_dirlist = NULL;
1139 static int need_fuzzy_dirlist = 0;
1140 struct file_struct *fuzzy_file = NULL;
1141 int fd = -1, f_copy = -1;
1143 STRUCT_STAT partial_st;
1144 struct file_struct *back_file = NULL;
1145 int statret, real_ret, stat_errno;
1146 char *fnamecmp, *partialptr, *backupptr = NULL;
1147 char fnamecmpbuf[MAXPATHLEN];
1148 uchar fnamecmp_type;
1149 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1150 int is_dir = !S_ISDIR(file->mode) ? 0
1151 : inc_recurse && ndx != cur_flist->ndx_start - 1 ? -1
1154 if (DEBUG_GTE(GENR, 1))
1155 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
1159 || (is_dir && !implied_dirs && file->flags & FLAG_IMPLIED_DIR))
1161 list_file_entry(file);
1166 if (is_below(file, skip_dir)) {
1168 file->flags |= FLAG_MISSING_DIR;
1169 #ifdef SUPPORT_HARD_LINKS
1170 else if (F_IS_HLINKED(file))
1171 handle_skipped_hlink(file, itemizing, code, f_out);
1179 if (daemon_filter_list.head && (*fname != '.' || fname[1])) {
1180 if (check_filter(&daemon_filter_list, FLOG, fname, is_dir) < 0) {
1183 #ifdef SUPPORT_HARD_LINKS
1184 if (F_IS_HLINKED(file))
1185 handle_skipped_hlink(file, itemizing, code, f_out);
1187 rprintf(FERROR_XFER,
1188 "skipping daemon-excluded %s \"%s\"\n",
1189 is_dir ? "directory" : "file", fname);
1191 goto skipping_dir_contents;
1196 if (dry_run > 1 || (dry_missing_dir && is_below(file, dry_missing_dir))) {
1197 parent_is_dry_missing:
1198 if (fuzzy_dirlist) {
1199 flist_free(fuzzy_dirlist);
1200 fuzzy_dirlist = NULL;
1202 parent_dirname = "";
1204 stat_errno = ENOENT;
1206 const char *dn = file->dirname ? file->dirname : ".";
1207 dry_missing_dir = NULL;
1208 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1209 if (relative_paths && !implied_dirs
1210 && do_stat(dn, &sx.st) < 0) {
1212 goto parent_is_dry_missing;
1213 if (make_path(fname, MKP_DROP_NAME | MKP_SKIP_SLASH) < 0) {
1214 rsyserr(FERROR_XFER, errno,
1215 "recv_generator: mkdir %s failed",
1219 if (fuzzy_dirlist) {
1220 flist_free(fuzzy_dirlist);
1221 fuzzy_dirlist = NULL;
1224 need_fuzzy_dirlist = 1;
1226 if (!preserve_perms)
1227 dflt_perms = default_perms_for_dir(dn);
1230 parent_dirname = dn;
1232 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
1233 strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1234 fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, GDL_IGNORE_FILTER_RULES);
1235 need_fuzzy_dirlist = 0;
1238 statret = link_stat(fname, &sx.st, keep_dirlinks && is_dir);
1242 if (missing_args == 2 && file->mode == 0) {
1243 if (filter_list.head && check_filter(&filter_list, FINFO, fname, is_dir) < 0)
1246 delete_item(fname, sx.st.st_mode, del_opts);
1250 if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
1255 file->flags |= FLAG_MISSING_DIR;
1257 #ifdef SUPPORT_HARD_LINKS
1258 else if (F_IS_HLINKED(file))
1259 handle_skipped_hlink(file, itemizing, code, f_out);
1261 if (INFO_GTE(SKIP, 1)) {
1262 rprintf(FINFO, "not creating new %s \"%s\"\n",
1263 is_dir ? "directory" : "file", fname);
1268 if (statret == 0 && !(sx.st.st_mode & S_IWUSR)
1269 && !am_root && sx.st.st_uid == our_uid)
1270 del_opts |= DEL_NO_UID_WRITE;
1272 if (ignore_existing > 0 && statret == 0
1273 && (!is_dir || !S_ISDIR(sx.st.st_mode))) {
1274 if (INFO_GTE(SKIP, 1) && is_dir >= 0)
1275 rprintf(FINFO, "%s exists\n", fname);
1276 #ifdef SUPPORT_HARD_LINKS
1277 if (F_IS_HLINKED(file))
1278 handle_skipped_hlink(file, itemizing, code, f_out);
1287 if (!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
1290 /* For --fake-super, the dir must be useable by the copying
1291 * user, just like it would be for root. */
1292 added_perms = S_IRUSR|S_IWUSR|S_IXUSR;
1296 /* In inc_recurse mode we want to make sure any missing
1297 * directories get created while we're still processing
1298 * the parent dir (which allows us to touch the parent
1299 * dir's mtime right away). We will handle the dir in
1300 * full later (right before we handle its contents). */
1302 && (S_ISDIR(sx.st.st_mode)
1303 || delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0))
1304 goto cleanup; /* Any errors get reported later. */
1305 if (do_mkdir(fname, (file->mode|added_perms) & 0700) == 0)
1306 file->flags |= FLAG_DIR_CREATED;
1309 /* The file to be received is a directory, so we need
1310 * to prepare appropriately. If there is already a
1311 * file of that name and it is *not* a directory, then
1312 * we need to delete it. If it doesn't exist, then
1313 * (perhaps recursively) create it. */
1314 if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1315 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0)
1316 goto skipping_dir_contents;
1319 if (dry_run && statret != 0) {
1320 if (!dry_missing_dir)
1321 dry_missing_dir = file;
1322 file->flags |= FLAG_MISSING_DIR;
1326 if (file->flags & FLAG_DIR_CREATED)
1328 if (!preserve_perms) { /* See comment in non-dir code below. */
1329 file->mode = dest_mode(file->mode, sx.st.st_mode,
1330 dflt_perms, statret == 0);
1332 if (statret != 0 && basis_dir[0] != NULL) {
1333 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1339 } else if (j >= 0) {
1341 fnamecmp = fnamecmpbuf;
1344 if (itemizing && f_out != -1) {
1345 itemize(fnamecmp, file, ndx, statret, &sx,
1346 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1348 if (real_ret != 0 && do_mkdir(fname,file->mode|added_perms) < 0 && errno != EEXIST) {
1349 if (!relative_paths || errno != ENOENT
1350 || make_path(fname, MKP_DROP_NAME | MKP_SKIP_SLASH) < 0
1351 || (do_mkdir(fname, file->mode|added_perms) < 0 && errno != EEXIST)) {
1352 rsyserr(FERROR_XFER, errno,
1353 "recv_generator: mkdir %s failed",
1355 skipping_dir_contents:
1357 "*** Skipping any contents from this failed directory ***\n");
1359 file->flags |= FLAG_MISSING_DIR;
1363 #ifdef SUPPORT_XATTRS
1364 if (preserve_xattrs && statret == 1)
1365 copy_xattrs(fnamecmpbuf, fname);
1367 if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
1368 && INFO_GTE(NAME, 1) && code != FNONE && f_out != -1)
1369 rprintf(code, "%s/\n", fname);
1371 /* We need to ensure that the dirs in the transfer have writable
1372 * permissions during the time we are putting files within them.
1373 * This is then fixed after the transfer is done. */
1375 if (!am_root && !(file->mode & S_IWUSR) && dir_tweaking) {
1376 mode_t mode = file->mode | S_IWUSR;
1377 if (do_chmod(fname, mode) < 0) {
1378 rsyserr(FERROR_XFER, errno,
1379 "failed to modify permissions on %s",
1382 need_retouch_dir_perms = 1;
1386 if (real_ret != 0 && one_file_system)
1387 real_sx.st.st_dev = filesystem_dev;
1389 if (one_file_system) {
1390 uint32 *devp = F_DIR_DEV_P(file);
1391 DEV_MAJOR(devp) = major(real_sx.st.st_dev);
1392 DEV_MINOR(devp) = minor(real_sx.st.st_dev);
1395 else if (delete_during && f_out != -1 && !phase
1396 && !(file->flags & FLAG_MISSING_DIR)) {
1397 if (file->flags & FLAG_CONTENT_DIR)
1398 delete_in_dir(fname, file, &real_sx.st.st_dev);
1400 change_local_filter_dir(fname, strlen(fname), F_DEPTH(file));
1405 /* If we're not preserving permissions, change the file-list's
1406 * mode based on the local permissions and some heuristics. */
1407 if (!preserve_perms) {
1408 int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
1409 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
1413 #ifdef SUPPORT_HARD_LINKS
1414 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1415 && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
1419 if (preserve_links && S_ISLNK(file->mode)) {
1420 #ifdef SUPPORT_LINKS
1421 const char *sl = F_SYMLINK(file);
1422 if (safe_symlinks && unsafe_symlink(sl, fname)) {
1423 if (INFO_GTE(NAME, 1)) {
1425 /* fname contains the destination path, but we
1426 * want to report the source path. */
1427 fname = f_name(file, NULL);
1430 "ignoring unsafe symlink \"%s\" -> \"%s\"\n",
1436 char lnk[MAXPATHLEN];
1439 if (S_ISLNK(sx.st.st_mode)
1440 && (len = do_readlink(fname, lnk, MAXPATHLEN-1)) > 0
1441 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1442 /* The link is pointing to the right place. */
1443 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1445 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1446 #if defined SUPPORT_HARD_LINKS && defined CAN_HARDLINK_SYMLINK
1447 if (preserve_hard_links && F_IS_HLINKED(file))
1448 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1450 if (remove_source_files == 1)
1451 goto return_with_success;
1454 } else if (basis_dir[0] != NULL) {
1455 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1458 #ifndef CAN_HARDLINK_SYMLINK
1460 /* Resort to --copy-dest behavior. */
1470 if (atomic_create(file, fname, sl, MAKEDEV(0, 0), &sx, statret == 0 ? DEL_FOR_SYMLINK : 0)) {
1471 set_file_attrs(fname, file, NULL, NULL, 0);
1473 if (statret == 0 && !S_ISLNK(sx.st.st_mode))
1475 itemize(fname, file, ndx, statret, &sx,
1476 ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
1478 if (code != FNONE && INFO_GTE(NAME, 1))
1479 rprintf(code, "%s -> %s\n", fname, sl);
1480 #ifdef SUPPORT_HARD_LINKS
1481 if (preserve_hard_links && F_IS_HLINKED(file))
1482 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1484 /* This does not check remove_source_files == 1
1485 * because this is one of the items that the old
1486 * --remove-sent-files option would remove. */
1487 if (remove_source_files)
1488 goto return_with_success;
1494 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1495 || (preserve_specials && IS_SPECIAL(file->mode))) {
1497 int del_for_flag = 0;
1498 if (IS_DEVICE(file->mode)) {
1499 uint32 *devp = F_RDEV_P(file);
1500 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1504 if (IS_DEVICE(file->mode)) {
1505 if (!IS_DEVICE(sx.st.st_mode))
1507 del_for_flag = DEL_FOR_DEVICE;
1509 if (!IS_SPECIAL(sx.st.st_mode))
1511 del_for_flag = DEL_FOR_SPECIAL;
1514 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
1515 && (IS_SPECIAL(sx.st.st_mode) || sx.st.st_rdev == rdev)) {
1516 /* The device or special file is identical. */
1517 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1519 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1520 #ifdef SUPPORT_HARD_LINKS
1521 if (preserve_hard_links && F_IS_HLINKED(file))
1522 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1524 if (remove_source_files == 1)
1525 goto return_with_success;
1528 } else if (basis_dir[0] != NULL) {
1529 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1532 #ifndef CAN_HARDLINK_SPECIAL
1534 /* Resort to --copy-dest behavior. */
1544 if (DEBUG_GTE(GENR, 1)) {
1545 rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1546 fname, (int)file->mode,
1547 (long)major(rdev), (long)minor(rdev));
1549 if (atomic_create(file, fname, NULL, rdev, &sx, del_for_flag)) {
1550 set_file_attrs(fname, file, NULL, NULL, 0);
1552 itemize(fname, file, ndx, statret, &sx,
1553 ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
1555 if (code != FNONE && INFO_GTE(NAME, 1))
1556 rprintf(code, "%s\n", fname);
1557 #ifdef SUPPORT_HARD_LINKS
1558 if (preserve_hard_links && F_IS_HLINKED(file))
1559 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1561 if (remove_source_files == 1)
1562 goto return_with_success;
1567 if (!S_ISREG(file->mode)) {
1569 fname = f_name(file, NULL);
1570 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1574 if (max_size > 0 && F_LENGTH(file) > max_size) {
1575 if (INFO_GTE(SKIP, 1)) {
1577 fname = f_name(file, NULL);
1578 rprintf(FINFO, "%s is over max-size\n", fname);
1582 if (min_size > 0 && F_LENGTH(file) < min_size) {
1583 if (INFO_GTE(SKIP, 1)) {
1585 fname = f_name(file, NULL);
1586 rprintf(FINFO, "%s is under min-size\n", fname);
1591 if (update_only > 0 && statret == 0
1592 && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
1593 if (INFO_GTE(SKIP, 1))
1594 rprintf(FINFO, "%s is newer\n", fname);
1595 #ifdef SUPPORT_HARD_LINKS
1596 if (F_IS_HLINKED(file))
1597 handle_skipped_hlink(file, itemizing, code, f_out);
1602 fnamecmp_type = FNAMECMP_FNAME;
1604 if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
1605 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE) != 0)
1608 stat_errno = ENOENT;
1611 if (statret != 0 && basis_dir[0] != NULL) {
1612 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
1615 if (remove_source_files == 1)
1616 goto return_with_success;
1620 fnamecmp = fnamecmpbuf;
1629 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1630 && link_stat(partialptr, &partial_st, 0) == 0
1631 && S_ISREG(partial_st.st_mode)) {
1633 goto prepare_to_open;
1637 if (statret != 0 && fuzzy_dirlist) {
1638 int j = find_fuzzy(file, fuzzy_dirlist);
1640 fuzzy_file = fuzzy_dirlist->files[j];
1641 f_name(fuzzy_file, fnamecmpbuf);
1642 if (DEBUG_GTE(FUZZY, 1)) {
1643 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1644 fname, fnamecmpbuf);
1646 sx.st.st_size = F_LENGTH(fuzzy_file);
1648 fnamecmp = fnamecmpbuf;
1649 fnamecmp_type = FNAMECMP_FUZZY;
1654 #ifdef SUPPORT_HARD_LINKS
1655 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1656 cur_flist->in_progress++;
1660 if (stat_errno == ENOENT)
1662 rsyserr(FERROR_XFER, stat_errno, "recv_generator: failed to stat %s",
1667 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1669 else if (fnamecmp_type == FNAMECMP_FUZZY)
1671 else if (unchanged_file(fnamecmp, file, &sx.st)) {
1673 do_unlink(partialptr);
1674 handle_partial_dir(partialptr, PDIR_DELETE);
1676 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1678 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
1679 #ifdef SUPPORT_HARD_LINKS
1680 if (preserve_hard_links && F_IS_HLINKED(file))
1681 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1683 if (remove_source_files != 1)
1685 return_with_success:
1687 send_msg_int(MSG_SUCCESS, ndx);
1691 if (append_mode > 0 && sx.st.st_size >= F_LENGTH(file)) {
1692 #ifdef SUPPORT_HARD_LINKS
1693 if (F_IS_HLINKED(file))
1694 handle_skipped_hlink(file, itemizing, code, f_out);
1702 fnamecmp = partialptr;
1703 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1710 if (read_batch || whole_file) {
1711 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1712 if (!(backupptr = get_backup_name(fname)))
1714 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
1715 goto pretend_missing;
1716 if (copy_file(fname, backupptr, -1, back_file->mode) < 0) {
1717 unmake_file(back_file);
1725 if (fuzzy_dirlist) {
1726 int j = flist_find(fuzzy_dirlist, file);
1727 if (j >= 0) /* don't use changing file as future fuzzy basis */
1728 fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
1732 if ((fd = do_open(fnamecmp, O_RDONLY, 0)) < 0) {
1733 rsyserr(FERROR, errno, "failed to open %s, continuing",
1734 full_fname(fnamecmp));
1736 /* pretend the file didn't exist */
1737 #ifdef SUPPORT_HARD_LINKS
1738 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1739 cur_flist->in_progress++;
1743 statret = real_ret = -1;
1747 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1748 if (!(backupptr = get_backup_name(fname))) {
1752 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1754 goto pretend_missing;
1756 if (robust_unlink(backupptr) && errno != ENOENT) {
1757 rsyserr(FERROR_XFER, errno, "unlink %s",
1758 full_fname(backupptr));
1759 unmake_file(back_file);
1764 if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1765 rsyserr(FERROR_XFER, errno, "open %s", full_fname(backupptr));
1766 unmake_file(back_file);
1771 fnamecmp_type = FNAMECMP_BACKUP;
1774 if (DEBUG_GTE(DELTASUM, 3)) {
1775 rprintf(FINFO, "gen mapped %s of size %s\n",
1776 fnamecmp, big_num(sx.st.st_size));
1779 if (DEBUG_GTE(DELTASUM, 2))
1780 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1783 if (remove_source_files && !delay_updates && !phase && !dry_run)
1784 increment_active_files(ndx, itemizing, code);
1785 if (inc_recurse && !dry_run)
1786 cur_flist->in_progress++;
1787 #ifdef SUPPORT_HARD_LINKS
1788 if (preserve_hard_links && F_IS_HLINKED(file))
1789 file->flags |= FLAG_FILE_SENT;
1791 write_ndx(f_out, ndx);
1793 int iflags = ITEM_TRANSFER;
1794 if (always_checksum > 0)
1795 iflags |= ITEM_REPORT_CHANGE;
1796 if (fnamecmp_type != FNAMECMP_FNAME)
1797 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1798 if (fnamecmp_type == FNAMECMP_FUZZY)
1799 iflags |= ITEM_XNAME_FOLLOWS;
1800 itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
1801 fuzzy_file ? fuzzy_file->basename : NULL);
1806 #ifdef SUPPORT_XATTRS
1807 if (preserve_xattrs)
1808 free_xattr(&real_sx);
1813 #ifdef SUPPORT_HARD_LINKS
1814 if (preserve_hard_links && F_IS_HLINKED(file))
1815 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1822 if (statret != 0 || whole_file)
1823 write_sum_head(f_out, NULL);
1824 else if (sx.st.st_size <= 0) {
1825 write_sum_head(f_out, NULL);
1828 if (generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy) < 0) {
1830 "WARNING: file is too large for checksum sending: %s\n",
1832 write_sum_head(f_out, NULL);
1839 int save_preserve_xattrs = preserve_xattrs;
1842 #ifdef SUPPORT_XATTRS
1843 if (preserve_xattrs) {
1844 copy_xattrs(fname, backupptr);
1845 preserve_xattrs = 0;
1848 set_file_attrs(backupptr, back_file, NULL, NULL, 0);
1849 preserve_xattrs = save_preserve_xattrs;
1850 if (INFO_GTE(BACKUP, 1)) {
1851 rprintf(FINFO, "backed up %s to %s\n",
1854 unmake_file(back_file);
1861 #ifdef SUPPORT_XATTRS
1862 if (preserve_xattrs)
1868 /* If we are replacing an existing hard link, symlink, device, or special file,
1869 * create a temp-name item and rename it into place. Only a symlink or hard
1870 * link puts a non-NULL value into the lnk arg. Only a device puts a non-0
1871 * value into the rdev arg. Specify 0 for the del_for_flag if there is not a
1872 * file to replace. This returns 1 on success and 0 on failure. */
1873 int atomic_create(struct file_struct *file, char *fname, const char *lnk,
1874 dev_t rdev, stat_x *sxp, int del_for_flag)
1876 char tmpname[MAXPATHLEN];
1877 const char *create_name;
1878 int skip_atomic, dir_in_the_way = del_for_flag && S_ISDIR(sxp->st.st_mode);
1880 if (!del_for_flag || dir_in_the_way || tmpdir || !get_tmpname(tmpname, fname, True))
1886 if (make_backups > 0 && !dir_in_the_way) {
1887 if (!make_backup(fname, skip_atomic))
1889 } else if (skip_atomic) {
1890 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1891 if (delete_item(fname, sxp->st.st_mode, del_opts | del_for_flag) != 0)
1896 create_name = skip_atomic ? fname : tmpname;
1899 #ifdef SUPPORT_LINKS
1900 if (S_ISLNK(file->mode)
1901 #ifdef SUPPORT_HARD_LINKS /* The first symlink in a hard-linked cluster is always created. */
1902 && (!F_IS_HLINKED(file) || file->flags & FLAG_HLINK_FIRST)
1905 if (do_symlink(lnk, create_name) < 0) {
1906 rsyserr(FERROR_XFER, errno, "symlink %s -> \"%s\" failed",
1907 full_fname(create_name), lnk);
1912 #ifdef SUPPORT_HARD_LINKS
1913 if (!hard_link_one(file, create_name, lnk, 0))
1917 if (do_mknod(create_name, file->mode, rdev) < 0) {
1918 rsyserr(FERROR_XFER, errno, "mknod %s failed",
1919 full_fname(create_name));
1925 if (do_rename(tmpname, fname) < 0) {
1926 rsyserr(FERROR_XFER, errno, "rename %s -> \"%s\" failed",
1927 full_fname(tmpname), full_fname(fname));
1936 #ifdef SUPPORT_HARD_LINKS
1937 static void handle_skipped_hlink(struct file_struct *file, int itemizing,
1938 enum logcode code, int f_out)
1940 char fbuf[MAXPATHLEN];
1942 struct file_list *save_flist = cur_flist;
1944 /* If we skip the last item in a chain of links and there was a
1945 * prior non-skipped hard-link waiting to finish, finish it now. */
1946 if ((new_last_ndx = skip_hard_link(file, &cur_flist)) < 0)
1949 file = cur_flist->files[new_last_ndx - cur_flist->ndx_start];
1950 cur_flist->in_progress--; /* undo prior increment */
1952 recv_generator(fbuf, file, new_last_ndx, itemizing, code, f_out);
1954 cur_flist = save_flist;
1958 static void touch_up_dirs(struct file_list *flist, int ndx)
1960 static int counter = 0;
1961 struct file_struct *file;
1968 end = flist->used - 1;
1972 /* Fix any directory permissions that were modified during the
1973 * transfer and/or re-set any tweaked modified-time values. */
1974 for (i = start; i <= end; i++, counter++) {
1975 file = flist->files[i];
1976 if (!S_ISDIR(file->mode)
1977 || (!implied_dirs && file->flags & FLAG_IMPLIED_DIR))
1979 if (DEBUG_GTE(TIME, 2)) {
1980 fname = f_name(file, NULL);
1981 rprintf(FINFO, "touch_up_dirs: %s (%d)\n",
1984 /* Be sure not to retouch permissions with --fake-super. */
1985 fix_dir_perms = !am_root && !(file->mode & S_IWUSR);
1986 if (!F_IS_ACTIVE(file) || file->flags & FLAG_MISSING_DIR
1987 || !(need_retouch_dir_times || fix_dir_perms))
1989 fname = f_name(file, NULL);
1991 do_chmod(fname, file->mode);
1992 if (need_retouch_dir_times) {
1994 if (link_stat(fname, &st, 0) == 0
1995 && cmp_time(st.st_mtime, file->modtime) != 0)
1996 set_modtime(fname, file->modtime, F_MOD_NSEC(file), file->mode);
1998 if (counter >= loopchk_limit) {
2000 maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
2002 maybe_flush_socket(0);
2008 void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
2010 struct file_struct *file;
2011 struct file_list *flist;
2012 char fbuf[MAXPATHLEN];
2016 #ifdef SUPPORT_HARD_LINKS
2017 if (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
2018 flist = flist_for_ndx(ndx, "check_for_finished_files.1");
2019 file = flist->files[ndx - flist->ndx_start];
2020 assert(file->flags & FLAG_HLINKED);
2021 finish_hard_link(file, f_name(file, fbuf), ndx, NULL, itemizing, code, -1);
2022 flist->in_progress--;
2027 if (check_redo && (ndx = get_redo_num()) != -1) {
2028 csum_length = SUM_LENGTH;
2029 max_size = -max_size;
2030 min_size = -min_size;
2031 ignore_existing = -ignore_existing;
2032 ignore_non_existing = -ignore_non_existing;
2033 update_only = -update_only;
2034 always_checksum = -always_checksum;
2035 size_only = -size_only;
2036 append_mode = -append_mode;
2037 make_backups = -make_backups; /* avoid dup backup w/inplace */
2041 cur_flist = flist_for_ndx(ndx, "check_for_finished_files.2");
2043 file = cur_flist->files[ndx - cur_flist->ndx_start];
2045 strlcpy(fbuf, solo_file, sizeof fbuf);
2048 recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
2049 cur_flist->to_redo--;
2053 csum_length = SHORT_SUM_LENGTH;
2054 max_size = -max_size;
2055 min_size = -min_size;
2056 ignore_existing = -ignore_existing;
2057 ignore_non_existing = -ignore_non_existing;
2058 update_only = -update_only;
2059 always_checksum = -always_checksum;
2060 size_only = -size_only;
2061 append_mode = -append_mode;
2062 make_backups = -make_backups;
2067 if (cur_flist == first_flist)
2070 /* We only get here if inc_recurse is enabled. */
2071 if (first_flist->in_progress || first_flist->to_redo)
2074 write_ndx(sock_f_out, NDX_DONE);
2075 if (!read_batch && !flist_eof) {
2077 for (flist = first_flist; flist != cur_flist; flist = flist->next)
2078 old_total += flist->used;
2079 maybe_flush_socket(!flist_eof && file_total - old_total < MIN_FILECNT_LOOKAHEAD/2);
2082 if (delete_during == 2 || !dir_tweaking) {
2083 /* Skip directory touch-up. */
2084 } else if (first_flist->parent_ndx >= 0)
2085 touch_up_dirs(dir_flist, first_flist->parent_ndx);
2087 flist_free(first_flist); /* updates first_flist */
2091 void generate_files(int f_out, const char *local_name)
2093 int i, ndx, next_loopchk = 0;
2094 char fbuf[MAXPATHLEN];
2097 int save_info_flist = info_levels[INFO_FLIST];
2098 int save_info_progress = info_levels[INFO_PROGRESS];
2100 if (protocol_version >= 29) {
2102 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
2103 code = logfile_format_has_i ? FNONE : FLOG;
2104 } else if (am_daemon) {
2105 itemizing = logfile_format_has_i && do_xfers;
2106 maybe_ATTRS_REPORT = ATTRS_REPORT;
2107 code = itemizing || !do_xfers ? FCLIENT : FINFO;
2108 } else if (!am_server) {
2109 itemizing = stdout_format_has_i;
2110 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
2111 code = itemizing ? FNONE : FINFO;
2114 maybe_ATTRS_REPORT = ATTRS_REPORT;
2117 solo_file = local_name;
2118 dir_tweaking = !(list_only || solo_file || dry_run);
2119 need_retouch_dir_times = preserve_times & PRESERVE_DIR_TIMES;
2120 loopchk_limit = allowed_lull ? allowed_lull * 5 : 200;
2121 symlink_timeset_failed_flags = ITEM_REPORT_TIME
2122 | (protocol_version >= 30 || !am_server ? ITEM_REPORT_TIMEFAIL : 0);
2123 implied_dirs_are_missing = relative_paths && !implied_dirs && protocol_version < 30;
2125 if (DEBUG_GTE(GENR, 1))
2126 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
2128 if (delete_before && !solo_file && cur_flist->used > 0)
2130 if (delete_during == 2) {
2131 deldelay_size = BIGPATHBUFLEN * 4;
2132 deldelay_buf = new_array(char, deldelay_size);
2134 out_of_memory("delete-delay");
2136 info_levels[INFO_FLIST] = info_levels[INFO_PROGRESS] = 0;
2138 if (append_mode > 0 || whole_file < 0)
2140 if (DEBUG_GTE(FLIST, 1)) {
2141 rprintf(FINFO, "delta-transmission %s\n",
2143 ? "disabled for local transfer or --whole-file"
2147 dflt_perms = (ACCESSPERMS & ~orig_umask);
2150 #ifdef SUPPORT_HARD_LINKS
2151 if (preserve_hard_links && inc_recurse) {
2152 while (!flist_eof && file_total < MIN_FILECNT_LOOKAHEAD/2)
2153 wait_for_receiver();
2157 if (inc_recurse && cur_flist->parent_ndx >= 0) {
2158 struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
2160 strlcpy(fbuf, solo_file, sizeof fbuf);
2163 ndx = cur_flist->ndx_start - 1;
2164 recv_generator(fbuf, fp, ndx, itemizing, code, f_out);
2165 if (delete_during && dry_run < 2 && !list_only
2166 && !(fp->flags & FLAG_MISSING_DIR)) {
2167 if (fp->flags & FLAG_CONTENT_DIR) {
2169 if (one_file_system) {
2170 uint32 *devp = F_DIR_DEV_P(fp);
2171 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
2173 dirdev = MAKEDEV(0, 0);
2174 delete_in_dir(fbuf, fp, &dirdev);
2176 change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(fp));
2179 for (i = cur_flist->low; i <= cur_flist->high; i++) {
2180 struct file_struct *file = cur_flist->sorted[i];
2182 if (!F_IS_ACTIVE(file))
2188 ndx = i + cur_flist->ndx_start;
2191 strlcpy(fbuf, solo_file, sizeof fbuf);
2194 recv_generator(fbuf, file, ndx, itemizing, code, f_out);
2196 check_for_finished_files(itemizing, code, 0);
2198 if (i + cur_flist->ndx_start >= next_loopchk) {
2200 maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
2202 maybe_flush_socket(0);
2203 next_loopchk += loopchk_limit;
2208 write_ndx(f_out, NDX_DONE);
2213 check_for_finished_files(itemizing, code, 1);
2214 if (cur_flist->next || flist_eof)
2216 wait_for_receiver();
2218 } while ((cur_flist = cur_flist->next) != NULL);
2221 delete_in_dir(NULL, NULL, &dev_zero);
2223 if (DEBUG_GTE(GENR, 1))
2224 rprintf(FINFO, "generate_files phase=%d\n", phase);
2227 check_for_finished_files(itemizing, code, 1);
2230 wait_for_receiver();
2234 if (DEBUG_GTE(GENR, 1))
2235 rprintf(FINFO, "generate_files phase=%d\n", phase);
2237 write_ndx(f_out, NDX_DONE);
2239 /* Reduce round-trip lag-time for a useless delay-updates phase. */
2240 if (protocol_version >= 29 && EARLY_DELAY_DONE_MSG())
2241 write_ndx(f_out, NDX_DONE);
2243 if (protocol_version >= 31 && EARLY_DELETE_DONE_MSG()) {
2244 if ((INFO_GTE(STATS, 2) && (delete_mode || force_delete)) || read_batch)
2245 write_del_stats(f_out);
2246 if (EARLY_DELAY_DONE_MSG()) /* Can't send this before delay */
2247 write_ndx(f_out, NDX_DONE);
2250 /* Read MSG_DONE for the redo phase (and any prior messages). */
2252 check_for_finished_files(itemizing, code, 0);
2253 if (msgdone_cnt > 1)
2255 wait_for_receiver();
2258 if (protocol_version >= 29) {
2260 if (DEBUG_GTE(GENR, 1))
2261 rprintf(FINFO, "generate_files phase=%d\n", phase);
2262 if (!EARLY_DELAY_DONE_MSG()) {
2263 write_ndx(f_out, NDX_DONE);
2264 if (protocol_version >= 31 && EARLY_DELETE_DONE_MSG())
2265 write_ndx(f_out, NDX_DONE);
2267 /* Read MSG_DONE for delay-updates phase & prior messages. */
2268 while (msgdone_cnt == 2)
2269 wait_for_receiver();
2272 info_levels[INFO_FLIST] = save_info_flist;
2273 info_levels[INFO_PROGRESS] = save_info_progress;
2275 if (delete_during == 2)
2276 do_delayed_deletions(fbuf);
2277 if (delete_after && !solo_file && file_total > 0)
2280 if (max_delete >= 0 && skipped_deletes) {
2282 "Deletions stopped due to --max-delete limit (%d skipped)\n",
2284 io_error |= IOERR_DEL_LIMIT;
2287 if (protocol_version >= 31) {
2288 if (!EARLY_DELETE_DONE_MSG()) {
2289 if (INFO_GTE(STATS, 2) || read_batch)
2290 write_del_stats(f_out);
2291 write_ndx(f_out, NDX_DONE);
2294 /* Read MSG_DONE for late-delete phase & prior messages. */
2295 while (msgdone_cnt == 3)
2296 wait_for_receiver();
2299 if ((need_retouch_dir_perms || need_retouch_dir_times)
2300 && dir_tweaking && (!inc_recurse || delete_during == 2))
2301 touch_up_dirs(dir_flist, -1);
2303 if (DEBUG_GTE(GENR, 1))
2304 rprintf(FINFO, "generate_files finished\n");