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-2007 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 version 2 as
11 * published by the Free Software Foundation.
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, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
28 extern int stdout_format_has_i;
29 extern int logfile_format_has_i;
33 extern int inc_recurse;
34 extern int do_progress;
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_perms;
45 extern int preserve_uid;
46 extern int preserve_gid;
47 extern int preserve_times;
48 extern int omit_dir_times;
49 extern int delete_mode;
50 extern int delete_before;
51 extern int delete_during;
52 extern int delete_after;
53 extern int msgdone_cnt;
54 extern int ignore_errors;
55 extern int remove_source_files;
56 extern int delay_updates;
57 extern int update_only;
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 ignore_timeout;
73 extern int protocol_version;
74 extern int file_total;
75 extern int fuzzy_basis;
76 extern int always_checksum;
77 extern int checksum_len;
78 extern char *partial_dir;
79 extern char *basis_dir[];
80 extern int compare_dest;
83 extern int whole_file;
85 extern int new_root_dir;
86 extern int read_batch;
87 extern int safe_symlinks;
88 extern long block_size; /* "long" because popt can't set an int32. */
89 extern int max_delete;
90 extern int force_delete;
91 extern int one_file_system;
92 extern struct stats stats;
93 extern dev_t filesystem_dev;
94 extern mode_t orig_umask;
95 extern char *backup_dir;
96 extern char *backup_suffix;
97 extern int backup_suffix_len;
98 extern struct file_list *cur_flist, *first_flist, *dir_flist;
99 extern struct filter_list_struct server_filter_list;
101 int ignore_perishable = 0;
102 int non_perishable_cnt = 0;
103 int maybe_ATTRS_REPORT = 0;
105 static dev_t dev_zero;
106 static int deletion_count = 0; /* used to implement --max-delete */
107 static int deldelay_size = 0, deldelay_cnt = 0;
108 static char *deldelay_buf = NULL;
109 static int deldelay_fd = -1;
111 static int dir_tweaking;
112 static int need_retouch_dir_times;
113 static const char *solo_file = NULL;
115 /* For calling delete_item() and delete_dir_contents(). */
116 #define DEL_RECURSE (1<<1) /* recurse */
117 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
120 TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
124 DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_NOT_EMPTY
127 /* Forward declaration for delete_item(). */
128 static enum delret delete_dir_contents(char *fname, int flags);
131 static int is_backup_file(char *fn)
133 int k = strlen(fn) - backup_suffix_len;
134 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
137 /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
138 * delete recursively.
140 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
141 * a directory! (The buffer is used for recursion, but returned unchanged.)
143 static enum delret delete_item(char *fbuf, int mode, char *replace, int flags)
150 rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
154 if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
155 ignore_perishable = 1;
156 /* If DEL_RECURSE is not set, this just reports emptiness. */
157 ret = delete_dir_contents(fbuf, flags);
158 ignore_perishable = 0;
159 if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT)
161 /* OK: try to delete the directory. */
164 if (!replace && max_delete >= 0 && ++deletion_count > max_delete)
169 ok = do_rmdir(fbuf) == 0;
170 } else if (make_backups > 0 && (backup_dir || !is_backup_file(fbuf))) {
171 what = "make_backup";
172 ok = make_backup(fbuf);
175 ok = robust_unlink(fbuf) == 0;
180 log_delete(fbuf, mode);
183 if (S_ISDIR(mode) && errno == ENOTEMPTY) {
184 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
187 } else if (errno != ENOENT) {
188 rsyserr(FERROR, errno, "delete_file: %s(%s) failed",
198 if (replace && ret != DR_SUCCESS) {
199 rprintf(FERROR, "could not make way for new %s: %s\n",
205 /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
206 * its contents, otherwise just checks for content. Returns DR_SUCCESS or
207 * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
208 * buffer is used for recursion, but returned unchanged.)
210 static enum delret delete_dir_contents(char *fname, int flags)
212 struct file_list *dirlist;
220 rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n",
224 dlen = strlen(fname);
225 save_filters = push_local_filters(fname, dlen);
227 non_perishable_cnt = 0;
228 dirlist = get_dirlist(fname, dlen, 0);
229 ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
234 if (!(flags & DEL_RECURSE)) {
240 if (dlen != 1 || *fname != '/')
242 remainder = MAXPATHLEN - (p - fname);
244 /* We do our own recursion, so make delete_item() non-recursive. */
245 flags = (flags & ~DEL_RECURSE) | DEL_DIR_IS_EMPTY;
247 for (j = dirlist->count; j--; ) {
248 struct file_struct *fp = dirlist->files[j];
250 if (fp->flags & FLAG_MOUNT_DIR) {
253 "mount point, %s, pins parent directory\n",
260 strlcpy(p, fp->basename, remainder);
261 /* Save stack by recursing to ourself directly. */
262 if (S_ISDIR(fp->mode)
263 && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
265 if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
273 pop_local_filters(save_filters);
275 if (ret == DR_NOT_EMPTY) {
276 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
282 static int start_delete_delay_temp(void)
284 char fnametmp[MAXPATHLEN];
285 int save_dry_run = dry_run;
288 if (!get_tmpname(fnametmp, "deldelay")
289 || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
290 rprintf(FINFO, "NOTE: Unable to create delete-delay temp file%s.\n",
291 inc_recurse ? "" : " -- switching to --delete-after");
293 delete_after = !inc_recurse;
294 dry_run = save_dry_run;
298 dry_run = save_dry_run;
302 static int flush_delete_delay(void)
304 if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
305 rsyserr(FERROR, errno, "flush of delete-delay buffer");
315 static int remember_delete(struct file_struct *file, const char *fname)
320 len = snprintf(deldelay_buf + deldelay_cnt,
321 deldelay_size - deldelay_cnt,
322 "%x %s%c", (int)file->mode, fname, '\0');
323 if ((deldelay_cnt += len) <= deldelay_size)
325 if (deldelay_fd < 0 && !start_delete_delay_temp())
328 if (!flush_delete_delay())
335 static int read_delay_line(char *buf)
337 static int read_pos = 0;
339 char *bp, *past_space;
342 for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
343 if (j < deldelay_cnt)
345 if (deldelay_fd < 0) {
350 deldelay_cnt -= read_pos;
351 if (deldelay_cnt == deldelay_size)
353 if (deldelay_cnt && read_pos) {
354 memmove(deldelay_buf, deldelay_buf + read_pos,
357 len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
358 deldelay_size - deldelay_cnt);
362 "ERROR: unexpected EOF in delete-delay file.\n");
367 rsyserr(FERROR, errno,
368 "reading delete-delay file");
375 bp = deldelay_buf + read_pos;
377 if (sscanf(bp, "%x ", &mode) != 1) {
379 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
382 past_space = strchr(bp, ' ') + 1;
383 len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
386 if (len > MAXPATHLEN) {
387 rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
391 /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
392 * instead of returning a pointer to our buffer. */
393 memcpy(buf, past_space, len);
398 static void do_delayed_deletions(char *delbuf)
402 if (deldelay_fd >= 0) {
403 if (deldelay_cnt && !flush_delete_delay())
405 lseek(deldelay_fd, 0, 0);
407 while ((mode = read_delay_line(delbuf)) >= 0)
408 delete_item(delbuf, mode, NULL, DEL_RECURSE);
409 if (deldelay_fd >= 0)
413 /* This function is used to implement per-directory deletion, and is used by
414 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
415 * MAXPATHLEN buffer with the name of the directory in it (the functions we
416 * call will append names onto the end, but the old dir value will be restored
418 static void delete_in_dir(struct file_list *flist, char *fbuf,
419 struct file_struct *file, dev_t *fs_dev)
421 static int already_warned = 0;
422 struct file_list *dirlist;
423 char delbuf[MAXPATHLEN];
427 change_local_filter_dir(NULL, 0, 0);
432 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
435 maybe_send_keepalive();
437 if (io_error && !ignore_errors) {
441 "IO error encountered -- skipping file deletion\n");
447 change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
449 if (one_file_system) {
450 if (file->flags & FLAG_TOP_DIR)
451 filesystem_dev = *fs_dev;
452 else if (filesystem_dev != *fs_dev)
456 dirlist = get_dirlist(fbuf, dlen, 0);
458 /* If an item in dirlist is not found in flist, delete it
459 * from the filesystem. */
460 for (i = dirlist->count; i--; ) {
461 struct file_struct *fp = dirlist->files[i];
462 if (!F_IS_ACTIVE(fp))
464 if (fp->flags & FLAG_MOUNT_DIR) {
466 rprintf(FINFO, "cannot delete mount point: %s\n",
470 if (flist_find(flist, fp) < 0) {
472 if (delete_during == 2) {
473 if (!remember_delete(fp, delbuf))
476 delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
483 /* This deletes any files on the receiving side that are not present on the
484 * sending side. This is used by --delete-before and --delete-after. */
485 static void do_delete_pass(struct file_list *flist)
487 char fbuf[MAXPATHLEN];
491 /* dry_run is incremented when the destination doesn't exist yet. */
492 if (dry_run > 1 || list_only)
495 for (j = 0; j < flist->count; j++) {
496 struct file_struct *file = flist->files[j];
498 if (!(file->flags & FLAG_XFER_DIR))
502 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
503 rprintf(FINFO, "deleting in %s\n", fbuf);
505 if (link_stat(fbuf, &st, keep_dirlinks) < 0
506 || !S_ISDIR(st.st_mode))
509 delete_in_dir(flist, fbuf, file, &st.st_dev);
511 delete_in_dir(NULL, NULL, NULL, &dev_zero);
513 if (do_progress && !am_server)
514 rprintf(FINFO, " \r");
517 int unchanged_attrs(const char *fname, struct file_struct *file, statx *sxp)
519 if (preserve_perms && !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
522 if (am_root && preserve_uid && sxp->st.st_uid != F_UID(file))
525 if (preserve_gid && F_GID(file) != GID_NONE && sxp->st.st_gid != F_GID(file))
529 if (preserve_acls && !S_ISLNK(file->mode)) {
530 if (!ACL_READY(*sxp))
532 if (set_acl(NULL, file, sxp) == 0)
536 #ifdef SUPPORT_XATTRS
537 if (preserve_xattrs) {
538 if (!XATTR_READY(*sxp))
539 get_xattr(fname, sxp);
540 if (xattr_diff(file, sxp, 0))
548 void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
549 statx *sxp, int32 iflags, uchar fnamecmp_type,
552 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
553 int keep_time = !preserve_times ? 0
554 : S_ISDIR(file->mode) ? !omit_dir_times
555 : !S_ISLNK(file->mode);
557 if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
558 iflags |= ITEM_REPORT_SIZE;
559 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
560 && !(iflags & ITEM_MATCHED)
561 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
562 || (keep_time && cmp_time(file->modtime, sxp->st.st_mtime) != 0))
563 iflags |= ITEM_REPORT_TIME;
564 if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
565 iflags |= ITEM_REPORT_PERMS;
566 if (preserve_uid && am_root && F_UID(file) != sxp->st.st_uid)
567 iflags |= ITEM_REPORT_OWNER;
568 if (preserve_gid && F_GID(file) != GID_NONE
569 && sxp->st.st_gid != F_GID(file))
570 iflags |= ITEM_REPORT_GROUP;
572 if (preserve_acls && !S_ISLNK(file->mode)) {
573 if (!ACL_READY(*sxp))
574 get_acl(fnamecmp, sxp);
575 if (set_acl(NULL, file, sxp) == 0)
576 iflags |= ITEM_REPORT_ACL;
579 #ifdef SUPPORT_XATTRS
580 if (preserve_xattrs) {
581 if (!XATTR_READY(*sxp))
582 get_xattr(fnamecmp, sxp);
583 if (xattr_diff(file, sxp, 1))
584 iflags |= ITEM_REPORT_XATTR;
588 #ifdef SUPPORT_XATTRS
589 if (preserve_xattrs && xattr_diff(file, NULL, 1))
590 iflags |= ITEM_REPORT_XATTR;
592 iflags |= ITEM_IS_NEW;
596 if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || verbose > 1
597 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
598 if (protocol_version >= 29) {
600 write_ndx(sock_f_out, ndx);
601 write_shortint(sock_f_out, iflags);
602 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
603 write_byte(sock_f_out, fnamecmp_type);
604 if (iflags & ITEM_XNAME_FOLLOWS)
605 write_vstring(sock_f_out, xname, strlen(xname));
606 #ifdef SUPPORT_XATTRS
607 if (iflags & ITEM_REPORT_XATTR && !dry_run)
608 send_xattr_request(NULL, file, sock_f_out);
610 } else if (ndx >= 0) {
611 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
612 log_item(code, file, &stats, iflags, xname);
618 /* Perform our quick-check heuristic for determining if a file is unchanged. */
619 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
621 if (st->st_size != F_LENGTH(file))
624 /* if always checksum is set then we use the checksum instead
625 of the file time to determine whether to sync */
626 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
627 char sum[MAX_DIGEST_LEN];
628 file_checksum(fn, sum, st->st_size);
629 return memcmp(sum, F_SUM(file), checksum_len) == 0;
638 return cmp_time(st->st_mtime, file->modtime) == 0;
643 * set (initialize) the size entries in the per-file sum_struct
644 * calculating dynamic block and checksum sizes.
646 * This is only called from generate_and_send_sums() but is a separate
647 * function to encapsulate the logic.
649 * The block size is a rounded square root of file length.
651 * The checksum size is determined according to:
652 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
653 * provided by Donovan Baarda which gives a probability of rsync
654 * algorithm corrupting data and falling back using the whole md4
657 * This might be made one of several selectable heuristics.
659 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
665 blength = block_size;
666 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
667 blength = BLOCK_SIZE;
672 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
673 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
674 blength = MAX_BLOCK_SIZE;
679 if (len < (int64)blength * blength)
682 } while (c >= 8); /* round to multiple of 8 */
683 blength = MAX(blength, BLOCK_SIZE);
687 if (protocol_version < 27) {
688 s2length = csum_length;
689 } else if (csum_length == SUM_LENGTH) {
690 s2length = SUM_LENGTH;
694 int b = BLOCKSUM_BIAS;
695 for (l = len; l >>= 1; b += 2) {}
696 for (c = blength; (c >>= 1) && b; b--) {}
697 /* add a bit, subtract rollsum, round up. */
698 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
699 s2length = MAX(s2length, csum_length);
700 s2length = MIN(s2length, SUM_LENGTH);
704 sum->blength = blength;
705 sum->s2length = s2length;
706 sum->remainder = (int32)(len % blength);
707 sum->count = (int32)(len / blength) + (sum->remainder != 0);
709 if (sum->count && verbose > 2) {
711 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
712 (double)sum->count, (long)sum->remainder, (long)sum->blength,
713 sum->s2length, (double)sum->flength);
719 * Generate and send a stream of signatures/checksums that describe a buffer
721 * Generate approximately one checksum every block_len bytes.
723 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
726 struct map_struct *mapbuf;
727 struct sum_struct sum;
730 sum_sizes_sqroot(&sum, len);
731 write_sum_head(f_out, &sum);
733 if (append_mode > 0 && f_copy < 0)
737 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
741 for (i = 0; i < sum.count; i++) {
742 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
743 char *map = map_ptr(mapbuf, offset, n1);
744 char sum2[SUM_LENGTH];
751 full_write(f_copy, map, n1);
756 sum1 = get_checksum1(map, n1);
757 get_checksum2(map, n1, sum2);
761 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
762 (double)i, (double)offset - n1, (long)n1,
763 (unsigned long)sum1);
765 write_int(f_out, sum1);
766 write_buf(f_out, sum2, sum.s2length);
774 /* Try to find a filename in the same dir as "fname" with a similar name. */
775 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
777 int fname_len, fname_suf_len;
778 const char *fname_suf, *fname = file->basename;
779 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
780 int j, lowest_j = -1;
782 fname_len = strlen(fname);
783 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
785 for (j = 0; j < dirlist->count; j++) {
786 struct file_struct *fp = dirlist->files[j];
787 const char *suf, *name;
791 if (!S_ISREG(fp->mode) || !F_LENGTH(fp)
792 || fp->flags & FLAG_FILE_SENT)
797 if (F_LENGTH(fp) == F_LENGTH(file)
798 && cmp_time(fp->modtime, file->modtime) == 0) {
801 "fuzzy size/modtime match for %s\n",
808 suf = find_filename_suffix(name, len, &suf_len);
810 dist = fuzzy_distance(name, len, fname, fname_len);
811 /* Add some extra weight to how well the suffixes match. */
812 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
815 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
816 name, (int)(dist>>16), (int)(dist&0xFFFF));
818 if (dist <= lowest_dist) {
827 /* This is only called for regular files. We return -2 if we've finished
828 * handling the file, -1 if no dest-linking occurred, or a non-negative
829 * value if we found an alternate basis file. */
830 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
831 char *cmpbuf, statx *sxp, int itemizing,
839 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
840 if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
842 switch (match_level) {
848 if (!unchanged_file(cmpbuf, file, &sxp->st))
854 if (!unchanged_attrs(cmpbuf, file, sxp))
856 if (always_checksum > 0 && preserve_times
857 && cmp_time(sxp->st.st_mtime, file->modtime))
864 } while (basis_dir[++j] != NULL);
869 if (j != best_match) {
871 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
872 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
876 if (match_level == 3 && !copy_dest) {
877 #ifdef SUPPORT_HARD_LINKS
879 if (!hard_link_one(file, fname, cmpbuf, 1))
881 if (preserve_hard_links && F_IS_HLINKED(file))
882 finish_hard_link(file, fname, &sxp->st, itemizing, code, j);
883 if (itemizing && (verbose > 1 || stdout_format_has_i > 1)) {
884 itemize(cmpbuf, file, ndx, 1, sxp,
885 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
891 itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
892 if (verbose > 1 && maybe_ATTRS_REPORT)
893 rprintf(FCLIENT, "%s is uptodate\n", fname);
897 if (match_level >= 2) {
898 #ifdef SUPPORT_HARD_LINKS
899 try_a_copy: /* Copy the file locally. */
901 if (copy_file(cmpbuf, fname, file->mode) < 0) {
903 rsyserr(FINFO, errno, "copy_file %s => %s",
904 full_fname(cmpbuf), fname);
908 set_file_attrs(fname, file, NULL, cmpbuf, 0);
910 itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
911 #ifdef SUPPORT_XATTRS
913 xattr_clear_locals(file);
915 if (maybe_ATTRS_REPORT
916 && ((!itemizing && verbose && match_level == 2)
917 || (verbose > 1 && match_level == 3))) {
918 code = match_level == 3 ? FCLIENT : FINFO;
919 rprintf(code, "%s%s\n", fname,
920 match_level == 3 ? " is uptodate" : "");
922 #ifdef SUPPORT_HARD_LINKS
923 if (preserve_hard_links && F_IS_HLINKED(file))
924 finish_hard_link(file, fname, &sxp->st, itemizing, code, -1);
929 return FNAMECMP_BASIS_DIR_LOW + j;
932 /* This is only called for non-regular files. We return -2 if we've finished
933 * handling the file, or -1 if no dest-linking occurred, or a non-negative
934 * value if we found an alternate basis file. */
935 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
936 char *cmpbuf, statx *sxp, int itemizing,
939 char lnk[MAXPATHLEN];
942 enum nonregtype type;
946 #ifndef SUPPORT_LINKS
947 if (S_ISLNK(file->mode))
950 if (S_ISDIR(file->mode)) {
952 } else if (IS_SPECIAL(file->mode))
954 else if (IS_DEVICE(file->mode))
957 else if (S_ISLNK(file->mode))
962 "internal: try_dests_non() called with invalid mode (%o)\n",
964 exit_cleanup(RERR_UNSUPPORTED);
968 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
969 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
973 if (!S_ISDIR(sxp->st.st_mode))
977 if (!IS_SPECIAL(sxp->st.st_mode))
981 if (!IS_DEVICE(sxp->st.st_mode))
986 if (!S_ISLNK(sxp->st.st_mode))
991 if (match_level < 1) {
1000 devp = F_RDEV_P(file);
1001 if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
1004 #ifdef SUPPORT_LINKS
1006 if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
1009 if (strcmp(lnk, F_SYMLINK(file)) != 0)
1014 if (match_level < 2) {
1018 if (unchanged_attrs(cmpbuf, file, sxp)) {
1023 } while (basis_dir[++j] != NULL);
1028 if (j != best_match) {
1030 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1031 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1035 if (match_level == 3) {
1036 #ifdef SUPPORT_HARD_LINKS
1038 #ifndef CAN_HARDLINK_SYMLINK
1039 && !S_ISLNK(file->mode)
1041 #ifndef CAN_HARDLINK_SPECIAL
1042 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
1044 && !S_ISDIR(file->mode)) {
1045 if (do_link(cmpbuf, fname) < 0) {
1046 rsyserr(FERROR, errno,
1047 "failed to hard-link %s with %s",
1051 if (preserve_hard_links && F_IS_HLINKED(file))
1052 finish_hard_link(file, fname, NULL, itemizing, code, -1);
1056 if (itemizing && stdout_format_has_i
1057 && (verbose > 1 || stdout_format_has_i > 1)) {
1058 int chg = compare_dest && type != TYPE_DIR ? 0
1060 + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1061 char *lp = match_level == 3 ? "" : NULL;
1062 itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
1064 if (verbose > 1 && maybe_ATTRS_REPORT) {
1065 rprintf(FCLIENT, "%s%s is uptodate\n",
1066 fname, type == TYPE_DIR ? "/" : "");
1074 static int phase = 0;
1075 static int dflt_perms;
1077 /* Acts on the indicated item in cur_flist whose name is fname. If a dir,
1078 * make sure it exists, and has the right permissions/timestamp info. For
1079 * all other non-regular files (symlinks, etc.) we create them here. For
1080 * regular files that have changed, we try to find a basis file and then
1081 * start sending checksums. The ndx is the file's unique index value.
1083 * When fname is non-null, it must point to a MAXPATHLEN buffer!
1085 * Note that f_out is set to -1 when doing final directory-permission and
1086 * modification-time repair. */
1087 static void recv_generator(char *fname, struct file_struct *file, int ndx,
1088 int itemizing, enum logcode code, int f_out)
1090 static int missing_below = -1, excluded_below = -1;
1091 static const char *parent_dirname = "";
1092 static struct file_list *fuzzy_dirlist = NULL;
1093 static int need_fuzzy_dirlist = 0;
1094 struct file_struct *fuzzy_file = NULL;
1095 int fd = -1, f_copy = -1;
1097 STRUCT_STAT partial_st;
1098 struct file_struct *back_file = NULL;
1099 int statret, real_ret, stat_errno;
1100 char *fnamecmp, *partialptr, *backupptr = NULL;
1101 char fnamecmpbuf[MAXPATHLEN];
1102 uchar fnamecmp_type;
1103 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1109 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
1111 if (server_filter_list.head) {
1112 if (excluded_below >= 0) {
1113 if (F_DEPTH(file) > excluded_below)
1115 excluded_below = -1;
1117 if (check_filter(&server_filter_list, fname,
1118 S_ISDIR(file->mode)) < 0) {
1119 if (S_ISDIR(file->mode))
1120 excluded_below = F_DEPTH(file);
1124 "skipping server-excluded file \"%s\"\n",
1131 if (missing_below >= 0) {
1132 if (F_DEPTH(file) <= missing_below) {
1136 } else if (!dry_run) {
1137 if (S_ISDIR(file->mode))
1138 file->flags |= FLAG_MISSING_DIR;
1143 sx.acc_acl = sx.def_acl = NULL;
1145 #ifdef SUPPORT_XATTRS
1149 if (fuzzy_dirlist) {
1150 flist_free(fuzzy_dirlist);
1151 fuzzy_dirlist = NULL;
1153 parent_dirname = "";
1155 stat_errno = ENOENT;
1157 const char *dn = file->dirname ? file->dirname : ".";
1158 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1159 if (relative_paths && !implied_dirs
1160 && do_stat(dn, &sx.st) < 0
1161 && create_directory_path(fname) < 0) {
1162 rsyserr(FERROR, errno,
1163 "recv_generator: mkdir %s failed",
1166 if (fuzzy_dirlist) {
1167 flist_free(fuzzy_dirlist);
1168 fuzzy_dirlist = NULL;
1171 need_fuzzy_dirlist = 1;
1173 if (!preserve_perms)
1174 dflt_perms = default_perms_for_dir(dn);
1177 parent_dirname = dn;
1179 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
1180 strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1181 fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
1182 need_fuzzy_dirlist = 0;
1185 statret = link_stat(fname, &sx.st,
1186 keep_dirlinks && S_ISDIR(file->mode));
1190 if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
1192 rprintf(FINFO, "not creating new %s \"%s\"\n",
1193 S_ISDIR(file->mode) ? "directory" : "file",
1196 if (S_ISDIR(file->mode)) {
1197 if (missing_below < 0) {
1200 missing_below = F_DEPTH(file);
1202 file->flags |= FLAG_MISSING_DIR;
1207 if (S_ISDIR(file->mode)) {
1208 /* The file to be received is a directory, so we need
1209 * to prepare appropriately. If there is already a
1210 * file of that name and it is *not* a directory, then
1211 * we need to delete it. If it doesn't exist, then
1212 * (perhaps recursively) create it. */
1213 if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1214 if (delete_item(fname, sx.st.st_mode, "directory", del_opts) != 0)
1218 if (dry_run && statret != 0 && missing_below < 0) {
1219 missing_below = F_DEPTH(file);
1225 if (*fname == '.' && fname[1] == '\0')
1229 if (!preserve_perms) { /* See comment in non-dir code below. */
1230 file->mode = dest_mode(file->mode, sx.st.st_mode,
1231 dflt_perms, statret == 0);
1233 if (statret != 0 && basis_dir[0] != NULL) {
1234 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1242 if (itemizing && f_out != -1) {
1243 itemize(fname, file, ndx, statret, &sx,
1244 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1246 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1247 if (!relative_paths || errno != ENOENT
1248 || create_directory_path(fname) < 0
1249 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
1250 rsyserr(FERROR, errno,
1251 "recv_generator: mkdir %s failed",
1254 "*** Skipping any contents from this failed directory ***\n");
1255 missing_below = F_DEPTH(file);
1256 file->flags |= FLAG_MISSING_DIR;
1260 if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
1261 && verbose && code != FNONE && f_out != -1)
1262 rprintf(code, "%s/\n", fname);
1263 if (real_ret != 0 && one_file_system)
1264 real_sx.st.st_dev = filesystem_dev;
1266 if (one_file_system) {
1267 uint32 *devp = F_DIRDEV_P(file);
1268 DEV_MAJOR(devp) = major(real_sx.st.st_dev);
1269 DEV_MINOR(devp) = minor(real_sx.st.st_dev);
1272 else if (delete_during && f_out != -1 && !phase && dry_run < 2
1273 && (file->flags & FLAG_XFER_DIR))
1274 delete_in_dir(cur_flist, fname, file, &real_sx.st.st_dev);
1278 /* If we're not preserving permissions, change the file-list's
1279 * mode based on the local permissions and some heuristics. */
1280 if (!preserve_perms) {
1281 int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
1282 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
1286 #ifdef SUPPORT_HARD_LINKS
1287 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1288 && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
1292 if (preserve_links && S_ISLNK(file->mode)) {
1293 #ifdef SUPPORT_LINKS
1294 const char *sl = F_SYMLINK(file);
1295 if (safe_symlinks && unsafe_symlink(sl, fname)) {
1298 fname = f_name(file, NULL);
1300 "ignoring unsafe symlink %s -> \"%s\"\n",
1301 full_fname(fname), sl);
1306 char lnk[MAXPATHLEN];
1309 if (!S_ISLNK(sx.st.st_mode))
1311 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1312 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1313 /* The link is pointing to the right place. */
1314 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1316 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1317 #ifdef SUPPORT_HARD_LINKS
1318 if (preserve_hard_links && F_IS_HLINKED(file))
1319 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1321 if (remove_source_files == 1)
1322 goto return_with_success;
1325 /* Not the right symlink (or not a symlink), so
1327 if (delete_item(fname, sx.st.st_mode, "symlink", del_opts) != 0)
1329 } else if (basis_dir[0] != NULL) {
1330 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1333 #ifndef CAN_HARDLINK_SYMLINK
1335 /* Resort to --copy-dest behavior. */
1345 #ifdef SUPPORT_HARD_LINKS
1346 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1349 if (do_symlink(sl, fname) != 0) {
1350 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
1351 full_fname(fname), sl);
1353 set_file_attrs(fname, file, NULL, NULL, 0);
1355 itemize(fname, file, ndx, statret, &sx,
1356 ITEM_LOCAL_CHANGE, 0, NULL);
1358 if (code != FNONE && verbose)
1359 rprintf(code, "%s -> %s\n", fname, sl);
1360 #ifdef SUPPORT_HARD_LINKS
1361 if (preserve_hard_links && F_IS_HLINKED(file))
1362 finish_hard_link(file, fname, NULL, itemizing, code, -1);
1364 /* This does not check remove_source_files == 1
1365 * because this is one of the items that the old
1366 * --remove-sent-files option would remove. */
1367 if (remove_source_files)
1368 goto return_with_success;
1374 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1375 || (preserve_specials && IS_SPECIAL(file->mode))) {
1376 uint32 *devp = F_RDEV_P(file);
1377 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1380 if (IS_DEVICE(file->mode)) {
1381 if (!IS_DEVICE(sx.st.st_mode))
1385 if (!IS_SPECIAL(sx.st.st_mode))
1390 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
1391 && sx.st.st_rdev == rdev) {
1392 /* The device or special file is identical. */
1393 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1395 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1396 #ifdef SUPPORT_HARD_LINKS
1397 if (preserve_hard_links && F_IS_HLINKED(file))
1398 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1400 if (remove_source_files == 1)
1401 goto return_with_success;
1404 if (delete_item(fname, sx.st.st_mode, t, del_opts) != 0)
1406 } else if (basis_dir[0] != NULL) {
1407 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1410 #ifndef CAN_HARDLINK_SPECIAL
1412 /* Resort to --copy-dest behavior. */
1422 #ifdef SUPPORT_HARD_LINKS
1423 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1427 rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1428 fname, (int)file->mode,
1429 (long)major(rdev), (long)minor(rdev));
1431 if (do_mknod(fname, file->mode, rdev) < 0) {
1432 rsyserr(FERROR, errno, "mknod %s failed",
1435 set_file_attrs(fname, file, NULL, NULL, 0);
1437 itemize(fname, file, ndx, statret, &sx,
1438 ITEM_LOCAL_CHANGE, 0, NULL);
1440 if (code != FNONE && verbose)
1441 rprintf(code, "%s\n", fname);
1442 #ifdef SUPPORT_HARD_LINKS
1443 if (preserve_hard_links && F_IS_HLINKED(file))
1444 finish_hard_link(file, fname, NULL, itemizing, code, -1);
1446 if (remove_source_files == 1)
1447 goto return_with_success;
1452 if (!S_ISREG(file->mode)) {
1454 fname = f_name(file, NULL);
1455 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1459 if (max_size > 0 && F_LENGTH(file) > max_size) {
1462 fname = f_name(file, NULL);
1463 rprintf(FINFO, "%s is over max-size\n", fname);
1467 if (min_size > 0 && F_LENGTH(file) < min_size) {
1470 fname = f_name(file, NULL);
1471 rprintf(FINFO, "%s is under min-size\n", fname);
1476 if (ignore_existing > 0 && statret == 0) {
1478 rprintf(FINFO, "%s exists\n", fname);
1482 if (update_only > 0 && statret == 0
1483 && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
1485 rprintf(FINFO, "%s is newer\n", fname);
1490 fnamecmp_type = FNAMECMP_FNAME;
1492 if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
1493 if (delete_item(fname, sx.st.st_mode, "regular file", del_opts) != 0)
1496 stat_errno = ENOENT;
1499 if (statret != 0 && basis_dir[0] != NULL) {
1500 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
1503 if (remove_source_files == 1)
1504 goto return_with_success;
1508 fnamecmp = fnamecmpbuf;
1517 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1518 && link_stat(partialptr, &partial_st, 0) == 0
1519 && S_ISREG(partial_st.st_mode)) {
1521 goto prepare_to_open;
1525 if (statret != 0 && fuzzy_dirlist && dry_run <= 1) {
1526 int j = find_fuzzy(file, fuzzy_dirlist);
1528 fuzzy_file = fuzzy_dirlist->files[j];
1529 f_name(fuzzy_file, fnamecmpbuf);
1531 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1532 fname, fnamecmpbuf);
1534 sx.st.st_size = F_LENGTH(fuzzy_file);
1536 fnamecmp = fnamecmpbuf;
1537 fnamecmp_type = FNAMECMP_FUZZY;
1542 #ifdef SUPPORT_HARD_LINKS
1543 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1546 if (stat_errno == ENOENT)
1548 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
1553 if (append_mode > 0 && sx.st.st_size > F_LENGTH(file))
1556 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1558 else if (fnamecmp_type == FNAMECMP_FUZZY)
1560 else if (unchanged_file(fnamecmp, file, &sx.st)) {
1562 do_unlink(partialptr);
1563 handle_partial_dir(partialptr, PDIR_DELETE);
1565 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1567 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
1568 #ifdef SUPPORT_HARD_LINKS
1569 if (preserve_hard_links && F_IS_HLINKED(file))
1570 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1572 if (remove_source_files != 1)
1574 return_with_success:
1576 send_msg_int(MSG_SUCCESS, ndx);
1583 fnamecmp = partialptr;
1584 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1588 if (!do_xfers || read_batch || whole_file)
1591 if (fuzzy_dirlist) {
1592 int j = flist_find(fuzzy_dirlist, file);
1593 if (j >= 0) /* don't use changing file as future fuzzy basis */
1594 fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
1598 fd = do_open(fnamecmp, O_RDONLY, 0);
1601 rsyserr(FERROR, errno, "failed to open %s, continuing",
1602 full_fname(fnamecmp));
1604 /* pretend the file didn't exist */
1605 #ifdef SUPPORT_HARD_LINKS
1606 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1609 statret = real_ret = -1;
1613 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1614 if (!(backupptr = get_backup_name(fname))) {
1618 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1620 goto pretend_missing;
1622 if (robust_unlink(backupptr) && errno != ENOENT) {
1623 rsyserr(FERROR, errno, "unlink %s",
1624 full_fname(backupptr));
1625 unmake_file(back_file);
1629 if ((f_copy = do_open(backupptr,
1630 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1631 rsyserr(FERROR, errno, "open %s",
1632 full_fname(backupptr));
1633 unmake_file(back_file);
1637 fnamecmp_type = FNAMECMP_BACKUP;
1641 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1642 fnamecmp, (double)sx.st.st_size);
1646 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1649 if (remove_source_files && !delay_updates && !phase)
1650 increment_active_files(ndx, itemizing, code);
1651 if (inc_recurse && !dry_run)
1652 cur_flist->in_progress++;
1653 #ifdef SUPPORT_HARD_LINKS
1654 if (preserve_hard_links && F_IS_HLINKED(file))
1655 file->flags |= FLAG_FILE_SENT;
1657 write_ndx(f_out, ndx);
1659 int iflags = ITEM_TRANSFER;
1660 if (always_checksum > 0)
1661 iflags |= ITEM_REPORT_CHECKSUM;
1662 if (fnamecmp_type != FNAMECMP_FNAME)
1663 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1664 if (fnamecmp_type == FNAMECMP_FUZZY)
1665 iflags |= ITEM_XNAME_FOLLOWS;
1666 itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
1667 fuzzy_file ? fuzzy_file->basename : NULL);
1672 #ifdef SUPPORT_XATTRS
1673 if (preserve_xattrs)
1674 free_xattr(&real_sx);
1679 #ifdef SUPPORT_HARD_LINKS
1680 if (preserve_hard_links && F_IS_HLINKED(file))
1681 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1688 if (statret != 0 || whole_file) {
1689 write_sum_head(f_out, NULL);
1693 generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy);
1697 set_file_attrs(backupptr, back_file, NULL, NULL, 0);
1699 rprintf(FINFO, "backed up %s to %s\n",
1702 unmake_file(back_file);
1712 #ifdef SUPPORT_XATTRS
1713 if (preserve_xattrs)
1719 static void touch_up_dirs(struct file_list *flist, int ndx)
1721 struct file_struct *file;
1723 int i, j, start, end;
1727 end = flist->count - 1;
1731 /* Fix any directory permissions that were modified during the
1732 * transfer and/or re-set any tweaked modified-time values. */
1733 for (i = start, j = 0; i <= end; i++) {
1734 file = flist->files[i];
1735 if (!F_IS_ACTIVE(file) || !S_ISDIR(file->mode)
1736 || file->flags & FLAG_MISSING_DIR)
1738 if (!need_retouch_dir_times && file->mode & S_IWUSR)
1740 fname = f_name(file, NULL);
1741 if (!(file->mode & S_IWUSR))
1742 do_chmod(fname, file->mode);
1743 if (need_retouch_dir_times)
1744 set_modtime(fname, file->modtime, file->mode);
1745 if (allowed_lull && !(++j % lull_mod))
1746 maybe_send_keepalive();
1747 else if (!(j % 200))
1748 maybe_flush_socket(0);
1752 void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
1754 struct file_struct *file;
1755 struct file_list *flist;
1756 char fbuf[MAXPATHLEN];
1759 #ifdef SUPPORT_HARD_LINKS
1760 while (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
1761 flist = flist_for_ndx(ndx);
1762 assert(flist != NULL);
1763 file = flist->files[ndx];
1764 assert(file->flags & FLAG_HLINKED);
1765 finish_hard_link(file, f_name(file, fbuf), NULL, itemizing, code, -1);
1769 while (check_redo && (ndx = get_redo_num()) != -1) {
1770 csum_length = SUM_LENGTH;
1771 max_size = -max_size;
1772 min_size = -min_size;
1773 ignore_existing = -ignore_existing;
1774 ignore_non_existing = -ignore_non_existing;
1775 update_only = -update_only;
1776 always_checksum = -always_checksum;
1777 size_only = -size_only;
1778 append_mode = -append_mode;
1779 make_backups = -make_backups; /* avoid dup backup w/inplace */
1783 cur_flist = flist_for_ndx(ndx);
1785 file = cur_flist->files[ndx - cur_flist->ndx_start];
1787 strlcpy(fbuf, solo_file, sizeof fbuf);
1790 recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
1791 cur_flist->to_redo--;
1795 csum_length = SHORT_SUM_LENGTH;
1796 max_size = -max_size;
1797 min_size = -min_size;
1798 ignore_existing = -ignore_existing;
1799 ignore_non_existing = -ignore_non_existing;
1800 update_only = -update_only;
1801 always_checksum = -always_checksum;
1802 size_only = -size_only;
1803 append_mode = -append_mode;
1804 make_backups = -make_backups;
1808 while (cur_flist != first_flist) { /* only possible with inc_recurse */
1809 if (first_flist->in_progress || first_flist->to_redo)
1813 write_ndx(sock_f_out, NDX_DONE);
1814 maybe_flush_socket(1);
1817 if (delete_during == 2 || !dir_tweaking) {
1818 /* Skip directory touch-up. */
1819 } else if (first_flist->ndx_start != 0)
1820 touch_up_dirs(dir_flist, first_flist->parent_ndx);
1821 else if (relative_paths && implied_dirs)
1822 touch_up_dirs(first_flist, -1);
1824 flist_free(first_flist); /* updates first_flist */
1828 void generate_files(int f_out, const char *local_name)
1831 char fbuf[MAXPATHLEN];
1834 int need_retouch_dir_perms = 0;
1835 int save_do_progress = do_progress;
1837 if (protocol_version >= 29) {
1839 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1840 code = logfile_format_has_i ? FNONE : FLOG;
1841 } else if (am_daemon) {
1842 itemizing = logfile_format_has_i && do_xfers;
1843 maybe_ATTRS_REPORT = ATTRS_REPORT;
1844 code = itemizing || !do_xfers ? FCLIENT : FINFO;
1845 } else if (!am_server) {
1846 itemizing = stdout_format_has_i;
1847 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1848 code = itemizing ? FNONE : FINFO;
1851 maybe_ATTRS_REPORT = ATTRS_REPORT;
1854 solo_file = local_name;
1855 dir_tweaking = !(list_only || solo_file || dry_run);
1856 need_retouch_dir_times = preserve_times && !omit_dir_times;
1857 lull_mod = allowed_lull * 5;
1860 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
1862 if (delete_before && !solo_file && cur_flist->count > 0)
1863 do_delete_pass(cur_flist);
1864 if (delete_during == 2) {
1865 deldelay_size = BIGPATHBUFLEN * 4;
1866 deldelay_buf = new_array(char, deldelay_size);
1868 out_of_memory("delete-delay");
1872 if (append_mode > 0 || whole_file < 0)
1875 rprintf(FINFO, "delta-transmission %s\n",
1877 ? "disabled for local transfer or --whole-file"
1881 /* Since we often fill up the outgoing socket and then just sit around
1882 * waiting for the other 2 processes to do their thing, we don't want
1883 * to exit on a timeout. If the data stops flowing, the receiver will
1884 * notice that and let us know via the redo pipe (or its closing). */
1887 dflt_perms = (ACCESSPERMS & ~orig_umask);
1890 if (inc_recurse && delete_during && cur_flist->ndx_start) {
1891 struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
1892 if (BITS_SETnUNSET(fp->flags, FLAG_XFER_DIR, FLAG_MISSING_DIR)) {
1894 if (one_file_system) {
1895 uint32 *devp = F_DIRDEV_P(fp);
1896 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1898 dirdev = MAKEDEV(0, 0);
1899 delete_in_dir(cur_flist, f_name(fp, fbuf), fp, &dirdev);
1902 for (i = cur_flist->low; i <= cur_flist->high; i++) {
1903 struct file_struct *file = cur_flist->files[i];
1905 if (!F_IS_ACTIVE(file))
1909 strlcpy(fbuf, solo_file, sizeof fbuf);
1912 recv_generator(fbuf, file, i + cur_flist->ndx_start,
1913 itemizing, code, f_out);
1915 /* We need to ensure that any dirs we create have
1916 * writeable permissions during the time we are putting
1917 * files within them. This is then fixed after the
1918 * transfer is done. */
1920 if (!am_root && S_ISDIR(file->mode)
1921 && !(file->mode & S_IWUSR) && dir_tweaking) {
1922 mode_t mode = file->mode | S_IWUSR;
1923 const char *fname = solo_file ? solo_file : fbuf;
1924 if (do_chmod(fname, mode) < 0) {
1925 rsyserr(FERROR, errno,
1926 "failed to modify permissions on %s",
1929 need_retouch_dir_perms = 1;
1933 check_for_finished_files(itemizing, code, 0);
1935 if (allowed_lull && !(i % lull_mod))
1936 maybe_send_keepalive();
1937 else if (!(i % 200))
1938 maybe_flush_socket(0);
1942 write_ndx(f_out, NDX_DONE);
1947 check_for_finished_files(itemizing, code, 1);
1948 if (cur_flist->next || flist_eof)
1950 wait_for_receiver();
1952 } while ((cur_flist = cur_flist->next) != NULL);
1955 delete_in_dir(NULL, NULL, NULL, &dev_zero);
1958 rprintf(FINFO, "generate_files phase=%d\n", phase);
1961 check_for_finished_files(itemizing, code, 1);
1964 wait_for_receiver();
1969 rprintf(FINFO, "generate_files phase=%d\n", phase);
1971 write_ndx(f_out, NDX_DONE);
1972 /* Reduce round-trip lag-time for a useless delay-updates phase. */
1973 if (protocol_version >= 29 && !delay_updates)
1974 write_ndx(f_out, NDX_DONE);
1976 /* Read MSG_DONE for the redo phase (and any prior messages). */
1978 check_for_finished_files(itemizing, code, 0);
1979 if (msgdone_cnt > 1)
1981 wait_for_receiver();
1984 if (protocol_version >= 29) {
1987 rprintf(FINFO, "generate_files phase=%d\n", phase);
1989 write_ndx(f_out, NDX_DONE);
1990 /* Read MSG_DONE for delay-updates phase & prior messages. */
1991 while (msgdone_cnt == 2)
1992 wait_for_receiver();
1995 do_progress = save_do_progress;
1996 if (delete_during == 2)
1997 do_delayed_deletions(fbuf);
1998 if (delete_after && !solo_file && file_total > 0)
1999 do_delete_pass(cur_flist);
2001 if ((need_retouch_dir_perms || need_retouch_dir_times)
2002 && dir_tweaking && (!inc_recurse || delete_during == 2))
2003 touch_up_dirs(inc_recurse ? dir_flist : cur_flist, -1);
2005 if (max_delete >= 0 && deletion_count > max_delete) {
2007 "Deletions stopped due to --max-delete limit (%d skipped)\n",
2008 deletion_count - max_delete);
2009 io_error |= IOERR_DEL_LIMIT;
2013 rprintf(FINFO, "generate_files finished\n");