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;
104 int ignore_perishable = 0;
105 int non_perishable_cnt = 0;
106 int maybe_ATTRS_REPORT = 0;
108 static dev_t dev_zero;
109 static int deletion_count = 0; /* used to implement --max-delete */
110 static int deldelay_size = 0, deldelay_cnt = 0;
111 static char *deldelay_buf = NULL;
112 static int deldelay_fd = -1;
114 static int dir_tweaking;
115 static int need_retouch_dir_times;
116 static const char *solo_file = NULL;
118 /* For calling delete_item() and delete_dir_contents(). */
119 #define DEL_RECURSE (1<<1) /* recurse */
120 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
123 TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
127 DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_NOT_EMPTY
130 /* Forward declaration for delete_item(). */
131 static enum delret delete_dir_contents(char *fname, int flags);
134 static int is_backup_file(char *fn)
136 int k = strlen(fn) - backup_suffix_len;
137 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
140 /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
141 * delete recursively.
143 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
144 * a directory! (The buffer is used for recursion, but returned unchanged.)
146 static enum delret delete_item(char *fbuf, int mode, char *replace, int flags)
153 rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
157 if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
158 ignore_perishable = 1;
159 /* If DEL_RECURSE is not set, this just reports emptiness. */
160 ret = delete_dir_contents(fbuf, flags);
161 ignore_perishable = 0;
162 if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT)
164 /* OK: try to delete the directory. */
167 if (!replace && max_delete >= 0 && ++deletion_count > max_delete)
172 ok = do_rmdir(fbuf) == 0;
173 } else if (make_backups > 0 && (backup_dir || !is_backup_file(fbuf))) {
174 what = "make_backup";
175 ok = make_backup(fbuf);
178 ok = robust_unlink(fbuf) == 0;
183 log_delete(fbuf, mode);
186 if (S_ISDIR(mode) && errno == ENOTEMPTY) {
187 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
190 } else if (errno != ENOENT) {
191 rsyserr(FERROR, errno, "delete_file: %s(%s) failed",
201 if (replace && ret != DR_SUCCESS) {
202 rprintf(FERROR, "could not make way for new %s: %s\n",
208 /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
209 * its contents, otherwise just checks for content. Returns DR_SUCCESS or
210 * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
211 * buffer is used for recursion, but returned unchanged.)
213 static enum delret delete_dir_contents(char *fname, int flags)
215 struct file_list *dirlist;
223 rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n",
227 dlen = strlen(fname);
228 save_filters = push_local_filters(fname, dlen);
230 non_perishable_cnt = 0;
231 dirlist = get_dirlist(fname, dlen, 0);
232 ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
237 if (!(flags & DEL_RECURSE)) {
243 if (dlen != 1 || *fname != '/')
245 remainder = MAXPATHLEN - (p - fname);
247 /* We do our own recursion, so make delete_item() non-recursive. */
248 flags = (flags & ~DEL_RECURSE) | DEL_DIR_IS_EMPTY;
250 for (j = dirlist->count; j--; ) {
251 struct file_struct *fp = dirlist->files[j];
253 if (fp->flags & FLAG_MOUNT_DIR) {
256 "mount point, %s, pins parent directory\n",
263 strlcpy(p, fp->basename, remainder);
264 /* Save stack by recursing to ourself directly. */
265 if (S_ISDIR(fp->mode)
266 && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
268 if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
276 pop_local_filters(save_filters);
278 if (ret == DR_NOT_EMPTY) {
279 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
285 static int start_delete_delay_temp(void)
287 char fnametmp[MAXPATHLEN];
288 int save_dry_run = dry_run;
291 if (!get_tmpname(fnametmp, "deldelay")
292 || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
293 rprintf(FINFO, "NOTE: Unable to create delete-delay temp file%s.\n",
294 inc_recurse ? "" : " -- switching to --delete-after");
296 delete_after = !inc_recurse;
297 dry_run = save_dry_run;
301 dry_run = save_dry_run;
305 static int flush_delete_delay(void)
307 if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
308 rsyserr(FERROR, errno, "flush of delete-delay buffer");
318 static int remember_delete(struct file_struct *file, const char *fname)
323 len = snprintf(deldelay_buf + deldelay_cnt,
324 deldelay_size - deldelay_cnt,
325 "%x %s%c", (int)file->mode, fname, '\0');
326 if ((deldelay_cnt += len) <= deldelay_size)
328 if (deldelay_fd < 0 && !start_delete_delay_temp())
331 if (!flush_delete_delay())
338 static int read_delay_line(char *buf)
340 static int read_pos = 0;
342 char *bp, *past_space;
345 for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
346 if (j < deldelay_cnt)
348 if (deldelay_fd < 0) {
353 deldelay_cnt -= read_pos;
354 if (deldelay_cnt == deldelay_size)
356 if (deldelay_cnt && read_pos) {
357 memmove(deldelay_buf, deldelay_buf + read_pos,
360 len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
361 deldelay_size - deldelay_cnt);
365 "ERROR: unexpected EOF in delete-delay file.\n");
370 rsyserr(FERROR, errno,
371 "reading delete-delay file");
378 bp = deldelay_buf + read_pos;
380 if (sscanf(bp, "%x ", &mode) != 1) {
382 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
385 past_space = strchr(bp, ' ') + 1;
386 len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
389 if (len > MAXPATHLEN) {
390 rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
394 /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
395 * instead of returning a pointer to our buffer. */
396 memcpy(buf, past_space, len);
401 static void do_delayed_deletions(char *delbuf)
405 if (deldelay_fd >= 0) {
406 if (deldelay_cnt && !flush_delete_delay())
408 lseek(deldelay_fd, 0, 0);
410 while ((mode = read_delay_line(delbuf)) >= 0)
411 delete_item(delbuf, mode, NULL, DEL_RECURSE);
412 if (deldelay_fd >= 0)
416 /* This function is used to implement per-directory deletion, and is used by
417 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
418 * MAXPATHLEN buffer with the name of the directory in it (the functions we
419 * call will append names onto the end, but the old dir value will be restored
421 static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
423 static int already_warned = 0;
424 struct file_list *dirlist;
425 char delbuf[MAXPATHLEN];
429 change_local_filter_dir(NULL, 0, 0);
434 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
437 maybe_send_keepalive();
439 if (io_error && !ignore_errors) {
443 "IO error encountered -- skipping file deletion\n");
449 change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
451 if (one_file_system) {
452 if (file->flags & FLAG_TOP_DIR)
453 filesystem_dev = *fs_dev;
454 else if (filesystem_dev != *fs_dev)
458 dirlist = get_dirlist(fbuf, dlen, 0);
460 /* If an item in dirlist is not found in flist, delete it
461 * from the filesystem. */
462 for (i = dirlist->count; i--; ) {
463 struct file_struct *fp = dirlist->files[i];
464 if (!F_IS_ACTIVE(fp))
466 if (fp->flags & FLAG_MOUNT_DIR) {
468 rprintf(FINFO, "cannot delete mount point: %s\n",
472 if (flist_find(cur_flist, fp) < 0) {
474 if (delete_during == 2) {
475 if (!remember_delete(fp, delbuf))
478 delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
485 /* This deletes any files on the receiving side that are not present on the
486 * sending side. This is used by --delete-before and --delete-after. */
487 static void do_delete_pass(void)
489 char fbuf[MAXPATHLEN];
493 /* dry_run is incremented when the destination doesn't exist yet. */
494 if (dry_run > 1 || list_only)
497 for (j = 0; j < cur_flist->count; j++) {
498 struct file_struct *file = cur_flist->sorted[j];
500 if (!(file->flags & FLAG_XFER_DIR))
504 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
505 rprintf(FINFO, "deleting in %s\n", fbuf);
507 if (link_stat(fbuf, &st, keep_dirlinks) < 0
508 || !S_ISDIR(st.st_mode))
511 delete_in_dir(fbuf, file, &st.st_dev);
513 delete_in_dir(NULL, NULL, &dev_zero);
515 if (do_progress && !am_server)
516 rprintf(FINFO, " \r");
519 int unchanged_attrs(const char *fname, struct file_struct *file, statx *sxp)
521 if (preserve_perms && !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
524 if (am_root && preserve_uid && sxp->st.st_uid != (uid_t)F_OWNER(file))
527 if (preserve_gid && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
531 if (preserve_acls && !S_ISLNK(file->mode)) {
532 if (!ACL_READY(*sxp))
534 if (set_acl(NULL, file, sxp) == 0)
538 #ifdef SUPPORT_XATTRS
539 if (preserve_xattrs) {
540 if (!XATTR_READY(*sxp))
541 get_xattr(fname, sxp);
542 if (xattr_diff(file, sxp, 0))
550 void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
551 statx *sxp, int32 iflags, uchar fnamecmp_type,
554 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
555 int keep_time = !preserve_times ? 0
556 : S_ISDIR(file->mode) ? !omit_dir_times
557 : !S_ISLNK(file->mode);
559 if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
560 iflags |= ITEM_REPORT_SIZE;
561 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
562 && !(iflags & ITEM_MATCHED)
563 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
564 || (keep_time && cmp_time(file->modtime, sxp->st.st_mtime) != 0))
565 iflags |= ITEM_REPORT_TIME;
566 if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
567 iflags |= ITEM_REPORT_PERMS;
568 if (preserve_uid && am_root && (uid_t)F_OWNER(file) != sxp->st.st_uid)
569 iflags |= ITEM_REPORT_OWNER;
570 if (preserve_gid && !(file->flags & FLAG_SKIP_GROUP)
571 && sxp->st.st_gid != (gid_t)F_GROUP(file))
572 iflags |= ITEM_REPORT_GROUP;
574 if (preserve_acls && !S_ISLNK(file->mode)) {
575 if (!ACL_READY(*sxp))
576 get_acl(fnamecmp, sxp);
577 if (set_acl(NULL, file, sxp) == 0)
578 iflags |= ITEM_REPORT_ACL;
581 #ifdef SUPPORT_XATTRS
582 if (preserve_xattrs) {
583 if (!XATTR_READY(*sxp))
584 get_xattr(fnamecmp, sxp);
585 if (xattr_diff(file, sxp, 1))
586 iflags |= ITEM_REPORT_XATTR;
590 #ifdef SUPPORT_XATTRS
591 if (preserve_xattrs && xattr_diff(file, NULL, 1))
592 iflags |= ITEM_REPORT_XATTR;
594 iflags |= ITEM_IS_NEW;
598 if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || verbose > 1
599 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
600 if (protocol_version >= 29) {
602 write_ndx(sock_f_out, ndx);
603 write_shortint(sock_f_out, iflags);
604 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
605 write_byte(sock_f_out, fnamecmp_type);
606 if (iflags & ITEM_XNAME_FOLLOWS)
607 write_vstring(sock_f_out, xname, strlen(xname));
608 #ifdef SUPPORT_XATTRS
609 if (iflags & ITEM_REPORT_XATTR && !dry_run)
610 send_xattr_request(NULL, file, sock_f_out);
612 } else if (ndx >= 0) {
613 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
614 log_item(code, file, &stats, iflags, xname);
620 /* Perform our quick-check heuristic for determining if a file is unchanged. */
621 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
623 if (st->st_size != F_LENGTH(file))
626 /* if always checksum is set then we use the checksum instead
627 of the file time to determine whether to sync */
628 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
629 char sum[MAX_DIGEST_LEN];
630 file_checksum(fn, sum, st->st_size);
631 return memcmp(sum, F_SUM(file), checksum_len) == 0;
640 return cmp_time(st->st_mtime, file->modtime) == 0;
645 * set (initialize) the size entries in the per-file sum_struct
646 * calculating dynamic block and checksum sizes.
648 * This is only called from generate_and_send_sums() but is a separate
649 * function to encapsulate the logic.
651 * The block size is a rounded square root of file length.
653 * The checksum size is determined according to:
654 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
655 * provided by Donovan Baarda which gives a probability of rsync
656 * algorithm corrupting data and falling back using the whole md4
659 * This might be made one of several selectable heuristics.
661 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
667 blength = block_size;
668 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
669 blength = BLOCK_SIZE;
674 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
675 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
676 blength = MAX_BLOCK_SIZE;
681 if (len < (int64)blength * blength)
684 } while (c >= 8); /* round to multiple of 8 */
685 blength = MAX(blength, BLOCK_SIZE);
689 if (protocol_version < 27) {
690 s2length = csum_length;
691 } else if (csum_length == SUM_LENGTH) {
692 s2length = SUM_LENGTH;
696 int b = BLOCKSUM_BIAS;
697 for (l = len; l >>= 1; b += 2) {}
698 for (c = blength; (c >>= 1) && b; b--) {}
699 /* add a bit, subtract rollsum, round up. */
700 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
701 s2length = MAX(s2length, csum_length);
702 s2length = MIN(s2length, SUM_LENGTH);
706 sum->blength = blength;
707 sum->s2length = s2length;
708 sum->remainder = (int32)(len % blength);
709 sum->count = (int32)(len / blength) + (sum->remainder != 0);
711 if (sum->count && verbose > 2) {
713 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
714 (double)sum->count, (long)sum->remainder, (long)sum->blength,
715 sum->s2length, (double)sum->flength);
721 * Generate and send a stream of signatures/checksums that describe a buffer
723 * Generate approximately one checksum every block_len bytes.
725 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
728 struct map_struct *mapbuf;
729 struct sum_struct sum;
732 sum_sizes_sqroot(&sum, len);
733 write_sum_head(f_out, &sum);
735 if (append_mode > 0 && f_copy < 0)
739 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
743 for (i = 0; i < sum.count; i++) {
744 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
745 char *map = map_ptr(mapbuf, offset, n1);
746 char sum2[SUM_LENGTH];
753 full_write(f_copy, map, n1);
758 sum1 = get_checksum1(map, n1);
759 get_checksum2(map, n1, sum2);
763 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
764 (double)i, (double)offset - n1, (long)n1,
765 (unsigned long)sum1);
767 write_int(f_out, sum1);
768 write_buf(f_out, sum2, sum.s2length);
776 /* Try to find a filename in the same dir as "fname" with a similar name. */
777 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
779 int fname_len, fname_suf_len;
780 const char *fname_suf, *fname = file->basename;
781 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
782 int j, lowest_j = -1;
784 fname_len = strlen(fname);
785 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
787 for (j = 0; j < dirlist->count; j++) {
788 struct file_struct *fp = dirlist->files[j];
789 const char *suf, *name;
793 if (!S_ISREG(fp->mode) || !F_LENGTH(fp)
794 || fp->flags & FLAG_FILE_SENT)
799 if (F_LENGTH(fp) == F_LENGTH(file)
800 && cmp_time(fp->modtime, file->modtime) == 0) {
803 "fuzzy size/modtime match for %s\n",
810 suf = find_filename_suffix(name, len, &suf_len);
812 dist = fuzzy_distance(name, len, fname, fname_len);
813 /* Add some extra weight to how well the suffixes match. */
814 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
817 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
818 name, (int)(dist>>16), (int)(dist&0xFFFF));
820 if (dist <= lowest_dist) {
829 /* This is only called for regular files. We return -2 if we've finished
830 * handling the file, -1 if no dest-linking occurred, or a non-negative
831 * value if we found an alternate basis file. */
832 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
833 char *cmpbuf, statx *sxp, int itemizing,
841 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
842 if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
844 switch (match_level) {
850 if (!unchanged_file(cmpbuf, file, &sxp->st))
856 if (!unchanged_attrs(cmpbuf, file, sxp))
858 if (always_checksum > 0 && preserve_times
859 && cmp_time(sxp->st.st_mtime, file->modtime))
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, &sxp->st, itemizing, code, j);
885 if (itemizing && (verbose > 1 || 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 (verbose > 1 && 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 (copy_file(cmpbuf, fname, file->mode) < 0) {
905 rsyserr(FINFO, errno, "copy_file %s => %s",
906 full_fname(cmpbuf), fname);
910 set_file_attrs(fname, file, NULL, cmpbuf, 0);
912 itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
913 #ifdef SUPPORT_XATTRS
915 xattr_clear_locals(file);
917 if (maybe_ATTRS_REPORT
918 && ((!itemizing && verbose && match_level == 2)
919 || (verbose > 1 && match_level == 3))) {
920 code = match_level == 3 ? FCLIENT : FINFO;
921 rprintf(code, "%s%s\n", fname,
922 match_level == 3 ? " is uptodate" : "");
924 #ifdef SUPPORT_HARD_LINKS
925 if (preserve_hard_links && F_IS_HLINKED(file))
926 finish_hard_link(file, fname, &sxp->st, itemizing, code, -1);
931 return FNAMECMP_BASIS_DIR_LOW + j;
934 /* This is only called for non-regular files. We return -2 if we've finished
935 * handling the file, or -1 if no dest-linking occurred, or a non-negative
936 * value if we found an alternate basis file. */
937 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
938 char *cmpbuf, statx *sxp, int itemizing,
941 char lnk[MAXPATHLEN];
944 enum nonregtype type;
948 #ifndef SUPPORT_LINKS
949 if (S_ISLNK(file->mode))
952 if (S_ISDIR(file->mode)) {
954 } else if (IS_SPECIAL(file->mode))
956 else if (IS_DEVICE(file->mode))
959 else if (S_ISLNK(file->mode))
964 "internal: try_dests_non() called with invalid mode (%o)\n",
966 exit_cleanup(RERR_UNSUPPORTED);
970 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
971 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
975 if (!S_ISDIR(sxp->st.st_mode))
979 if (!IS_SPECIAL(sxp->st.st_mode))
983 if (!IS_DEVICE(sxp->st.st_mode))
988 if (!S_ISLNK(sxp->st.st_mode))
993 if (match_level < 1) {
1002 devp = F_RDEV_P(file);
1003 if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
1006 #ifdef SUPPORT_LINKS
1008 if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
1011 if (strcmp(lnk, F_SYMLINK(file)) != 0)
1016 if (match_level < 2) {
1020 if (unchanged_attrs(cmpbuf, file, sxp)) {
1025 } while (basis_dir[++j] != NULL);
1030 if (j != best_match) {
1032 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1033 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1037 if (match_level == 3) {
1038 #ifdef SUPPORT_HARD_LINKS
1040 #ifndef CAN_HARDLINK_SYMLINK
1041 && !S_ISLNK(file->mode)
1043 #ifndef CAN_HARDLINK_SPECIAL
1044 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
1046 && !S_ISDIR(file->mode)) {
1047 if (do_link(cmpbuf, fname) < 0) {
1048 rsyserr(FERROR, errno,
1049 "failed to hard-link %s with %s",
1053 if (preserve_hard_links && F_IS_HLINKED(file))
1054 finish_hard_link(file, fname, NULL, itemizing, code, -1);
1058 if (itemizing && stdout_format_has_i
1059 && (verbose > 1 || stdout_format_has_i > 1)) {
1060 int chg = compare_dest && type != TYPE_DIR ? 0
1062 + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1063 char *lp = match_level == 3 ? "" : NULL;
1064 itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
1066 if (verbose > 1 && maybe_ATTRS_REPORT) {
1067 rprintf(FCLIENT, "%s%s is uptodate\n",
1068 fname, type == TYPE_DIR ? "/" : "");
1076 static int phase = 0;
1077 static int dflt_perms;
1079 /* Acts on the indicated item in cur_flist whose name is fname. If a dir,
1080 * make sure it exists, and has the right permissions/timestamp info. For
1081 * all other non-regular files (symlinks, etc.) we create them here. For
1082 * regular files that have changed, we try to find a basis file and then
1083 * start sending checksums. The ndx is the file's unique index value.
1085 * When fname is non-null, it must point to a MAXPATHLEN buffer!
1087 * Note that f_out is set to -1 when doing final directory-permission and
1088 * modification-time repair. */
1089 static void recv_generator(char *fname, struct file_struct *file, int ndx,
1090 int itemizing, enum logcode code, int f_out)
1092 static int missing_below = -1, excluded_below = -1;
1093 static const char *parent_dirname = "";
1094 static struct file_list *fuzzy_dirlist = NULL;
1095 static int need_fuzzy_dirlist = 0;
1096 struct file_struct *fuzzy_file = NULL;
1097 int fd = -1, f_copy = -1;
1099 STRUCT_STAT partial_st;
1100 struct file_struct *back_file = NULL;
1101 int statret, real_ret, stat_errno;
1102 char *fnamecmp, *partialptr, *backupptr = NULL;
1103 char fnamecmpbuf[MAXPATHLEN];
1104 uchar fnamecmp_type;
1105 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1111 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
1113 if (server_filter_list.head) {
1114 if (excluded_below >= 0) {
1115 if (F_DEPTH(file) > excluded_below)
1117 excluded_below = -1;
1119 if (check_filter(&server_filter_list, fname,
1120 S_ISDIR(file->mode)) < 0) {
1121 if (S_ISDIR(file->mode))
1122 excluded_below = F_DEPTH(file);
1126 "skipping server-excluded file \"%s\"\n",
1133 if (missing_below >= 0) {
1134 if (F_DEPTH(file) <= missing_below) {
1138 } else if (!dry_run) {
1139 if (S_ISDIR(file->mode))
1140 file->flags |= FLAG_MISSING_DIR;
1145 sx.acc_acl = sx.def_acl = NULL;
1147 #ifdef SUPPORT_XATTRS
1151 if (fuzzy_dirlist) {
1152 flist_free(fuzzy_dirlist);
1153 fuzzy_dirlist = NULL;
1155 parent_dirname = "";
1157 stat_errno = ENOENT;
1159 const char *dn = file->dirname ? file->dirname : ".";
1160 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1161 if (relative_paths && !implied_dirs
1162 && do_stat(dn, &sx.st) < 0
1163 && create_directory_path(fname) < 0) {
1164 rsyserr(FERROR, errno,
1165 "recv_generator: mkdir %s failed",
1168 if (fuzzy_dirlist) {
1169 flist_free(fuzzy_dirlist);
1170 fuzzy_dirlist = NULL;
1173 need_fuzzy_dirlist = 1;
1175 if (!preserve_perms)
1176 dflt_perms = default_perms_for_dir(dn);
1179 parent_dirname = dn;
1181 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
1182 strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1183 fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
1184 need_fuzzy_dirlist = 0;
1187 statret = link_stat(fname, &sx.st,
1188 keep_dirlinks && S_ISDIR(file->mode));
1192 if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
1194 rprintf(FINFO, "not creating new %s \"%s\"\n",
1195 S_ISDIR(file->mode) ? "directory" : "file",
1198 if (S_ISDIR(file->mode)) {
1199 if (missing_below < 0) {
1202 missing_below = F_DEPTH(file);
1204 file->flags |= FLAG_MISSING_DIR;
1209 if (S_ISDIR(file->mode)) {
1210 /* The file to be received is a directory, so we need
1211 * to prepare appropriately. If there is already a
1212 * file of that name and it is *not* a directory, then
1213 * we need to delete it. If it doesn't exist, then
1214 * (perhaps recursively) create it. */
1215 if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1216 if (delete_item(fname, sx.st.st_mode, "directory", del_opts) != 0)
1220 if (dry_run && statret != 0 && missing_below < 0) {
1221 missing_below = F_DEPTH(file);
1227 if (*fname == '.' && fname[1] == '\0')
1231 if (!preserve_perms) { /* See comment in non-dir code below. */
1232 file->mode = dest_mode(file->mode, sx.st.st_mode,
1233 dflt_perms, statret == 0);
1235 if (statret != 0 && basis_dir[0] != NULL) {
1236 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1244 if (itemizing && f_out != -1) {
1245 itemize(fname, file, ndx, statret, &sx,
1246 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1248 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1249 if (!relative_paths || errno != ENOENT
1250 || create_directory_path(fname) < 0
1251 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
1252 rsyserr(FERROR, errno,
1253 "recv_generator: mkdir %s failed",
1256 "*** Skipping any contents from this failed directory ***\n");
1257 missing_below = F_DEPTH(file);
1258 file->flags |= FLAG_MISSING_DIR;
1262 if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
1263 && verbose && code != FNONE && f_out != -1)
1264 rprintf(code, "%s/\n", fname);
1265 if (real_ret != 0 && one_file_system)
1266 real_sx.st.st_dev = filesystem_dev;
1268 if (one_file_system) {
1269 uint32 *devp = F_DIRDEV_P(file);
1270 DEV_MAJOR(devp) = major(real_sx.st.st_dev);
1271 DEV_MINOR(devp) = minor(real_sx.st.st_dev);
1274 else if (delete_during && f_out != -1 && !phase && dry_run < 2
1275 && (file->flags & FLAG_XFER_DIR))
1276 delete_in_dir(fname, file, &real_sx.st.st_dev);
1280 /* If we're not preserving permissions, change the file-list's
1281 * mode based on the local permissions and some heuristics. */
1282 if (!preserve_perms) {
1283 int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
1284 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
1288 #ifdef SUPPORT_HARD_LINKS
1289 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1290 && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
1294 if (preserve_links && S_ISLNK(file->mode)) {
1295 #ifdef SUPPORT_LINKS
1296 const char *sl = F_SYMLINK(file);
1297 if (safe_symlinks && unsafe_symlink(sl, fname)) {
1300 fname = f_name(file, NULL);
1302 "ignoring unsafe symlink %s -> \"%s\"\n",
1303 full_fname(fname), sl);
1308 char lnk[MAXPATHLEN];
1311 if (!S_ISLNK(sx.st.st_mode))
1313 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1314 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1315 /* The link is pointing to the right place. */
1316 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1318 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1319 #ifdef SUPPORT_HARD_LINKS
1320 if (preserve_hard_links && F_IS_HLINKED(file))
1321 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1323 if (remove_source_files == 1)
1324 goto return_with_success;
1327 /* Not the right symlink (or not a symlink), so
1329 if (delete_item(fname, sx.st.st_mode, "symlink", del_opts) != 0)
1331 } else if (basis_dir[0] != NULL) {
1332 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1335 #ifndef CAN_HARDLINK_SYMLINK
1337 /* Resort to --copy-dest behavior. */
1347 #ifdef SUPPORT_HARD_LINKS
1348 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1351 if (do_symlink(sl, fname) != 0) {
1352 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
1353 full_fname(fname), sl);
1355 set_file_attrs(fname, file, NULL, NULL, 0);
1357 itemize(fname, file, ndx, statret, &sx,
1358 ITEM_LOCAL_CHANGE, 0, NULL);
1360 if (code != FNONE && verbose)
1361 rprintf(code, "%s -> %s\n", fname, sl);
1362 #ifdef SUPPORT_HARD_LINKS
1363 if (preserve_hard_links && F_IS_HLINKED(file))
1364 finish_hard_link(file, fname, NULL, itemizing, code, -1);
1366 /* This does not check remove_source_files == 1
1367 * because this is one of the items that the old
1368 * --remove-sent-files option would remove. */
1369 if (remove_source_files)
1370 goto return_with_success;
1376 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1377 || (preserve_specials && IS_SPECIAL(file->mode))) {
1378 uint32 *devp = F_RDEV_P(file);
1379 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1382 if (IS_DEVICE(file->mode)) {
1383 if (!IS_DEVICE(sx.st.st_mode))
1387 if (!IS_SPECIAL(sx.st.st_mode))
1392 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
1393 && sx.st.st_rdev == rdev) {
1394 /* The device or special file is identical. */
1395 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1397 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1398 #ifdef SUPPORT_HARD_LINKS
1399 if (preserve_hard_links && F_IS_HLINKED(file))
1400 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1402 if (remove_source_files == 1)
1403 goto return_with_success;
1406 if (delete_item(fname, sx.st.st_mode, t, del_opts) != 0)
1408 } else if (basis_dir[0] != NULL) {
1409 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1412 #ifndef CAN_HARDLINK_SPECIAL
1414 /* Resort to --copy-dest behavior. */
1424 #ifdef SUPPORT_HARD_LINKS
1425 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1429 rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1430 fname, (int)file->mode,
1431 (long)major(rdev), (long)minor(rdev));
1433 if (do_mknod(fname, file->mode, rdev) < 0) {
1434 rsyserr(FERROR, errno, "mknod %s failed",
1437 set_file_attrs(fname, file, NULL, NULL, 0);
1439 itemize(fname, file, ndx, statret, &sx,
1440 ITEM_LOCAL_CHANGE, 0, NULL);
1442 if (code != FNONE && verbose)
1443 rprintf(code, "%s\n", fname);
1444 #ifdef SUPPORT_HARD_LINKS
1445 if (preserve_hard_links && F_IS_HLINKED(file))
1446 finish_hard_link(file, fname, NULL, itemizing, code, -1);
1448 if (remove_source_files == 1)
1449 goto return_with_success;
1454 if (!S_ISREG(file->mode)) {
1456 fname = f_name(file, NULL);
1457 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1461 if (max_size > 0 && F_LENGTH(file) > max_size) {
1464 fname = f_name(file, NULL);
1465 rprintf(FINFO, "%s is over max-size\n", fname);
1469 if (min_size > 0 && F_LENGTH(file) < min_size) {
1472 fname = f_name(file, NULL);
1473 rprintf(FINFO, "%s is under min-size\n", fname);
1478 if (ignore_existing > 0 && statret == 0) {
1480 rprintf(FINFO, "%s exists\n", fname);
1484 if (update_only > 0 && statret == 0
1485 && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
1487 rprintf(FINFO, "%s is newer\n", fname);
1492 fnamecmp_type = FNAMECMP_FNAME;
1494 if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
1495 if (delete_item(fname, sx.st.st_mode, "regular file", del_opts) != 0)
1498 stat_errno = ENOENT;
1501 if (statret != 0 && basis_dir[0] != NULL) {
1502 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
1505 if (remove_source_files == 1)
1506 goto return_with_success;
1510 fnamecmp = fnamecmpbuf;
1519 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1520 && link_stat(partialptr, &partial_st, 0) == 0
1521 && S_ISREG(partial_st.st_mode)) {
1523 goto prepare_to_open;
1527 if (statret != 0 && fuzzy_dirlist && dry_run <= 1) {
1528 int j = find_fuzzy(file, fuzzy_dirlist);
1530 fuzzy_file = fuzzy_dirlist->files[j];
1531 f_name(fuzzy_file, fnamecmpbuf);
1533 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1534 fname, fnamecmpbuf);
1536 sx.st.st_size = F_LENGTH(fuzzy_file);
1538 fnamecmp = fnamecmpbuf;
1539 fnamecmp_type = FNAMECMP_FUZZY;
1544 #ifdef SUPPORT_HARD_LINKS
1545 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1548 if (stat_errno == ENOENT)
1550 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
1555 if (append_mode > 0 && sx.st.st_size > F_LENGTH(file))
1558 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1560 else if (fnamecmp_type == FNAMECMP_FUZZY)
1562 else if (unchanged_file(fnamecmp, file, &sx.st)) {
1564 do_unlink(partialptr);
1565 handle_partial_dir(partialptr, PDIR_DELETE);
1567 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1569 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
1570 #ifdef SUPPORT_HARD_LINKS
1571 if (preserve_hard_links && F_IS_HLINKED(file))
1572 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1574 if (remove_source_files != 1)
1576 return_with_success:
1578 send_msg_int(MSG_SUCCESS, ndx);
1585 fnamecmp = partialptr;
1586 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1590 if (!do_xfers || read_batch || whole_file)
1593 if (fuzzy_dirlist) {
1594 int j = flist_find(fuzzy_dirlist, file);
1595 if (j >= 0) /* don't use changing file as future fuzzy basis */
1596 fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
1600 fd = do_open(fnamecmp, O_RDONLY, 0);
1603 rsyserr(FERROR, errno, "failed to open %s, continuing",
1604 full_fname(fnamecmp));
1606 /* pretend the file didn't exist */
1607 #ifdef SUPPORT_HARD_LINKS
1608 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1611 statret = real_ret = -1;
1615 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1616 if (!(backupptr = get_backup_name(fname))) {
1620 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1622 goto pretend_missing;
1624 if (robust_unlink(backupptr) && errno != ENOENT) {
1625 rsyserr(FERROR, errno, "unlink %s",
1626 full_fname(backupptr));
1627 unmake_file(back_file);
1631 if ((f_copy = do_open(backupptr,
1632 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1633 rsyserr(FERROR, errno, "open %s",
1634 full_fname(backupptr));
1635 unmake_file(back_file);
1639 fnamecmp_type = FNAMECMP_BACKUP;
1643 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1644 fnamecmp, (double)sx.st.st_size);
1648 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1651 if (remove_source_files && !delay_updates && !phase)
1652 increment_active_files(ndx, itemizing, code);
1653 if (inc_recurse && !dry_run)
1654 cur_flist->in_progress++;
1655 #ifdef SUPPORT_HARD_LINKS
1656 if (preserve_hard_links && F_IS_HLINKED(file))
1657 file->flags |= FLAG_FILE_SENT;
1659 write_ndx(f_out, ndx);
1661 int iflags = ITEM_TRANSFER;
1662 if (always_checksum > 0)
1663 iflags |= ITEM_REPORT_CHECKSUM;
1664 if (fnamecmp_type != FNAMECMP_FNAME)
1665 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1666 if (fnamecmp_type == FNAMECMP_FUZZY)
1667 iflags |= ITEM_XNAME_FOLLOWS;
1668 itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
1669 fuzzy_file ? fuzzy_file->basename : NULL);
1674 #ifdef SUPPORT_XATTRS
1675 if (preserve_xattrs)
1676 free_xattr(&real_sx);
1681 #ifdef SUPPORT_HARD_LINKS
1682 if (preserve_hard_links && F_IS_HLINKED(file))
1683 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1690 if (statret != 0 || whole_file) {
1691 write_sum_head(f_out, NULL);
1695 generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy);
1699 set_file_attrs(backupptr, back_file, NULL, NULL, 0);
1701 rprintf(FINFO, "backed up %s to %s\n",
1704 unmake_file(back_file);
1714 #ifdef SUPPORT_XATTRS
1715 if (preserve_xattrs)
1721 static void touch_up_dirs(struct file_list *flist, int ndx)
1723 static int counter = 0;
1724 struct file_struct *file;
1730 end = flist->count - 1;
1734 /* Fix any directory permissions that were modified during the
1735 * transfer and/or re-set any tweaked modified-time values. */
1736 for (i = start; i <= end; i++, counter++) {
1737 file = flist->files[i];
1739 rprintf(FINFO, "touch_up_dirs: %s (%d)\n",
1740 f_name(file, NULL), i);
1742 if (!F_IS_ACTIVE(file) || !S_ISDIR(file->mode)
1743 || file->flags & FLAG_MISSING_DIR)
1745 if (!need_retouch_dir_times && file->mode & S_IWUSR)
1747 fname = f_name(file, NULL);
1748 if (!(file->mode & S_IWUSR))
1749 do_chmod(fname, file->mode);
1750 if (need_retouch_dir_times)
1751 set_modtime(fname, file->modtime, file->mode);
1752 if (allowed_lull && !(counter % lull_mod))
1753 maybe_send_keepalive();
1754 else if (!(counter & 0xFF))
1755 maybe_flush_socket(0);
1759 void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
1761 struct file_struct *file;
1762 struct file_list *flist;
1763 char fbuf[MAXPATHLEN];
1766 #ifdef SUPPORT_HARD_LINKS
1767 while (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
1768 flist = flist_for_ndx(ndx);
1769 assert(flist != NULL);
1770 file = flist->files[ndx];
1771 assert(file->flags & FLAG_HLINKED);
1772 finish_hard_link(file, f_name(file, fbuf), NULL, itemizing, code, -1);
1776 while (check_redo && (ndx = get_redo_num()) != -1) {
1777 csum_length = SUM_LENGTH;
1778 max_size = -max_size;
1779 min_size = -min_size;
1780 ignore_existing = -ignore_existing;
1781 ignore_non_existing = -ignore_non_existing;
1782 update_only = -update_only;
1783 always_checksum = -always_checksum;
1784 size_only = -size_only;
1785 append_mode = -append_mode;
1786 make_backups = -make_backups; /* avoid dup backup w/inplace */
1790 cur_flist = flist_for_ndx(ndx);
1792 file = cur_flist->files[ndx - cur_flist->ndx_start];
1794 strlcpy(fbuf, solo_file, sizeof fbuf);
1797 recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
1798 cur_flist->to_redo--;
1802 csum_length = SHORT_SUM_LENGTH;
1803 max_size = -max_size;
1804 min_size = -min_size;
1805 ignore_existing = -ignore_existing;
1806 ignore_non_existing = -ignore_non_existing;
1807 update_only = -update_only;
1808 always_checksum = -always_checksum;
1809 size_only = -size_only;
1810 append_mode = -append_mode;
1811 make_backups = -make_backups;
1815 while (cur_flist != first_flist) { /* only possible with inc_recurse */
1816 if (first_flist->in_progress || first_flist->to_redo)
1820 write_ndx(sock_f_out, NDX_DONE);
1821 maybe_flush_socket(1);
1824 if (delete_during == 2 || !dir_tweaking) {
1825 /* Skip directory touch-up. */
1826 } else if (first_flist->ndx_start != 0)
1827 touch_up_dirs(dir_flist, first_flist->parent_ndx);
1828 else if (relative_paths && implied_dirs)
1829 touch_up_dirs(first_flist, -1);
1831 flist_free(first_flist); /* updates first_flist */
1835 void generate_files(int f_out, const char *local_name)
1838 char fbuf[MAXPATHLEN];
1841 int need_retouch_dir_perms = 0;
1842 int save_do_progress = do_progress;
1844 if (protocol_version >= 29) {
1846 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1847 code = logfile_format_has_i ? FNONE : FLOG;
1848 } else if (am_daemon) {
1849 itemizing = logfile_format_has_i && do_xfers;
1850 maybe_ATTRS_REPORT = ATTRS_REPORT;
1851 code = itemizing || !do_xfers ? FCLIENT : FINFO;
1852 } else if (!am_server) {
1853 itemizing = stdout_format_has_i;
1854 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1855 code = itemizing ? FNONE : FINFO;
1858 maybe_ATTRS_REPORT = ATTRS_REPORT;
1861 solo_file = local_name;
1862 dir_tweaking = !(list_only || solo_file || dry_run);
1863 need_retouch_dir_times = preserve_times && !omit_dir_times;
1864 lull_mod = allowed_lull * 5;
1867 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
1869 if (delete_before && !solo_file && cur_flist->count > 0)
1871 if (delete_during == 2) {
1872 deldelay_size = BIGPATHBUFLEN * 4;
1873 deldelay_buf = new_array(char, deldelay_size);
1875 out_of_memory("delete-delay");
1879 if (append_mode > 0 || whole_file < 0)
1882 rprintf(FINFO, "delta-transmission %s\n",
1884 ? "disabled for local transfer or --whole-file"
1888 /* Since we often fill up the outgoing socket and then just sit around
1889 * waiting for the other 2 processes to do their thing, we don't want
1890 * to exit on a timeout. If the data stops flowing, the receiver will
1891 * notice that and let us know via the redo pipe (or its closing). */
1894 dflt_perms = (ACCESSPERMS & ~orig_umask);
1897 if (inc_recurse && delete_during && cur_flist->ndx_start) {
1898 struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
1899 if (BITS_SETnUNSET(fp->flags, FLAG_XFER_DIR, FLAG_MISSING_DIR)) {
1901 if (one_file_system) {
1902 uint32 *devp = F_DIRDEV_P(fp);
1903 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1905 dirdev = MAKEDEV(0, 0);
1906 delete_in_dir(f_name(fp, fbuf), fp, &dirdev);
1909 for (i = cur_flist->low; i <= cur_flist->high; i++) {
1910 struct file_struct *file = cur_flist->sorted[i];
1912 if (!F_IS_ACTIVE(file))
1920 ndx = i + cur_flist->ndx_start;
1923 strlcpy(fbuf, solo_file, sizeof fbuf);
1926 recv_generator(fbuf, file, ndx, itemizing, code, f_out);
1928 /* We need to ensure that any dirs we create have
1929 * writeable permissions during the time we are putting
1930 * files within them. This is then fixed after the
1931 * transfer is done. */
1933 if (!am_root && S_ISDIR(file->mode)
1934 && !(file->mode & S_IWUSR) && dir_tweaking) {
1935 mode_t mode = file->mode | S_IWUSR;
1936 const char *fname = solo_file ? solo_file : fbuf;
1937 if (do_chmod(fname, mode) < 0) {
1938 rsyserr(FERROR, errno,
1939 "failed to modify permissions on %s",
1942 need_retouch_dir_perms = 1;
1946 check_for_finished_files(itemizing, code, 0);
1948 if (allowed_lull && !(i % lull_mod))
1949 maybe_send_keepalive();
1950 else if (!(i & 0xFF))
1951 maybe_flush_socket(0);
1955 write_ndx(f_out, NDX_DONE);
1960 check_for_finished_files(itemizing, code, 1);
1961 if (cur_flist->next || flist_eof)
1963 wait_for_receiver();
1965 } while ((cur_flist = cur_flist->next) != NULL);
1968 delete_in_dir(NULL, NULL, &dev_zero);
1971 rprintf(FINFO, "generate_files phase=%d\n", phase);
1974 check_for_finished_files(itemizing, code, 1);
1977 wait_for_receiver();
1982 rprintf(FINFO, "generate_files phase=%d\n", phase);
1984 write_ndx(f_out, NDX_DONE);
1985 /* Reduce round-trip lag-time for a useless delay-updates phase. */
1986 if (protocol_version >= 29 && !delay_updates)
1987 write_ndx(f_out, NDX_DONE);
1989 /* Read MSG_DONE for the redo phase (and any prior messages). */
1991 check_for_finished_files(itemizing, code, 0);
1992 if (msgdone_cnt > 1)
1994 wait_for_receiver();
1997 if (protocol_version >= 29) {
2000 rprintf(FINFO, "generate_files phase=%d\n", phase);
2002 write_ndx(f_out, NDX_DONE);
2003 /* Read MSG_DONE for delay-updates phase & prior messages. */
2004 while (msgdone_cnt == 2)
2005 wait_for_receiver();
2008 do_progress = save_do_progress;
2009 if (delete_during == 2)
2010 do_delayed_deletions(fbuf);
2011 if (delete_after && !solo_file && file_total > 0)
2014 if ((need_retouch_dir_perms || need_retouch_dir_times)
2015 && dir_tweaking && (!inc_recurse || delete_during == 2))
2016 touch_up_dirs(dir_flist, -1);
2018 if (max_delete >= 0 && deletion_count > max_delete) {
2020 "Deletions stopped due to --max-delete limit (%d skipped)\n",
2021 deletion_count - max_delete);
2022 io_error |= IOERR_DEL_LIMIT;
2026 rprintf(FINFO, "generate_files finished\n");