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, 2004, 2005, 2006 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 2 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, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
29 extern int stdout_format_has_i;
30 extern int logfile_format_has_i;
34 extern int do_progress;
35 extern int relative_paths;
36 extern int implied_dirs;
37 extern int keep_dirlinks;
38 extern int preserve_links;
39 extern int preserve_devices;
40 extern int preserve_specials;
41 extern int preserve_hard_links;
42 extern int preserve_perms;
43 extern int preserve_uid;
44 extern int preserve_gid;
45 extern int preserve_times;
46 extern int omit_dir_times;
47 extern int delete_mode;
48 extern int delete_before;
49 extern int delete_during;
50 extern int delete_after;
52 extern int ignore_errors;
53 extern int remove_source_files;
54 extern int delay_updates;
55 extern int update_only;
56 extern int ignore_existing;
57 extern int ignore_non_existing;
59 extern int append_mode;
60 extern int make_backups;
61 extern int csum_length;
62 extern int ignore_times;
64 extern OFF_T max_size;
65 extern OFF_T min_size;
67 extern int allowed_lull;
68 extern int sock_f_out;
69 extern int ignore_timeout;
70 extern int protocol_version;
71 extern int fuzzy_basis;
72 extern int always_checksum;
73 extern int checksum_len;
74 extern char *partial_dir;
75 extern char *basis_dir[];
76 extern int compare_dest;
79 extern int whole_file;
81 extern int new_root_dir;
82 extern int read_batch;
83 extern int safe_symlinks;
84 extern long block_size; /* "long" because popt can't set an int32. */
85 extern int max_delete;
86 extern int force_delete;
87 extern int one_file_system;
88 extern struct stats stats;
89 extern dev_t filesystem_dev;
90 extern char *backup_dir;
91 extern char *backup_suffix;
92 extern int backup_suffix_len;
93 extern struct file_list *the_file_list;
94 extern struct filter_list_struct server_filter_list;
96 int ignore_perishable = 0;
97 int non_perishable_cnt = 0;
99 static int deletion_count = 0; /* used to implement --max-delete */
100 static int deldelay_size = 0, deldelay_cnt = 0;
101 static char *deldelay_buf = NULL;
102 static int deldelay_fd = -1;
103 static BOOL solo_file = 0;
105 /* For calling delete_item() and delete_dir_contents(). */
106 #define DEL_RECURSE (1<<1) /* recurse */
107 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
110 TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
114 DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_NOT_EMPTY
117 /* Forward declaration for delete_item(). */
118 static enum delret delete_dir_contents(char *fname, int flags);
121 static int is_backup_file(char *fn)
123 int k = strlen(fn) - backup_suffix_len;
124 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
127 /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
128 * delete recursively.
130 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
131 * a directory! (The buffer is used for recursion, but returned unchanged.)
133 static enum delret delete_item(char *fbuf, int mode, char *replace, int flags)
140 rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
144 if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
145 ignore_perishable = 1;
146 /* If DEL_RECURSE is not set, this just reports emptiness. */
147 ret = delete_dir_contents(fbuf, flags);
148 ignore_perishable = 0;
149 if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT)
151 /* OK: try to delete the directory. */
154 if (!replace && max_delete >= 0 && ++deletion_count > max_delete)
159 ok = do_rmdir(fbuf) == 0;
160 } else if (make_backups && (backup_dir || !is_backup_file(fbuf))) {
161 what = "make_backup";
162 ok = make_backup(fbuf);
165 ok = robust_unlink(fbuf) == 0;
170 log_delete(fbuf, mode);
173 if (S_ISDIR(mode) && errno == ENOTEMPTY) {
174 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
177 } else if (errno != ENOENT) {
178 rsyserr(FERROR, errno, "delete_file: %s(%s) failed",
188 if (replace && ret != DR_SUCCESS) {
189 rprintf(FERROR, "could not make way for new %s: %s\n",
195 /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
196 * its contents, otherwise just checks for content. Returns DR_SUCCESS or
197 * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
198 * buffer is used for recursion, but returned unchanged.)
200 static enum delret delete_dir_contents(char *fname, int flags)
202 struct file_list *dirlist;
210 rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n",
214 dlen = strlen(fname);
215 save_filters = push_local_filters(fname, dlen);
217 non_perishable_cnt = 0;
218 dirlist = get_dirlist(fname, dlen, 0);
219 ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
224 if (!(flags & DEL_RECURSE)) {
230 if (dlen != 1 || *fname != '/')
232 remainder = MAXPATHLEN - (p - fname);
234 /* We do our own recursion, so make delete_item() non-recursive. */
235 flags = (flags & ~DEL_RECURSE) | DEL_DIR_IS_EMPTY;
237 for (j = dirlist->count; j--; ) {
238 struct file_struct *fp = dirlist->files[j];
240 if (fp->flags & FLAG_MOUNT_DIR) {
243 "mount point, %s, pins parent directory\n",
250 strlcpy(p, F_BASENAME(fp), remainder);
251 /* Save stack by recursing to ourself directly. */
252 if (S_ISDIR(fp->mode)
253 && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
255 if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
263 pop_local_filters(save_filters);
265 if (ret == DR_NOT_EMPTY) {
266 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
272 static int start_delete_delay_temp(void)
274 char fnametmp[MAXPATHLEN];
275 int save_dry_run = dry_run;
278 if (!get_tmpname(fnametmp, "deldelay")
279 || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
280 rprintf(FINFO, "NOTE: Unable to create delete-delay temp file--"
281 "switching to --delete-after.\n");
284 dry_run = save_dry_run;
287 dry_run = save_dry_run;
292 static int flush_delete_delay(void)
294 if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
295 rsyserr(FERROR, errno, "flush of delete-delay buffer");
305 static int remember_delete(struct file_struct *file, const char *fname)
310 len = snprintf(deldelay_buf + deldelay_cnt,
311 deldelay_size - deldelay_cnt,
312 "%x %s%c", (int)file->mode, fname, '\0');
313 if ((deldelay_cnt += len) <= deldelay_size)
315 if (deldelay_fd < 0 && !start_delete_delay_temp())
318 if (!flush_delete_delay())
325 static int read_delay_line(char *buf)
327 static int read_pos = 0;
329 char *bp, *past_space;
332 for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
333 if (j < deldelay_cnt)
335 if (deldelay_fd < 0) {
340 deldelay_cnt -= read_pos;
341 if (deldelay_cnt == deldelay_size)
343 if (deldelay_cnt && read_pos) {
344 memmove(deldelay_buf, deldelay_buf + read_pos,
347 len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
348 deldelay_size - deldelay_cnt);
352 "ERROR: unexpected EOF in delete-delay file.\n");
357 rsyserr(FERROR, errno,
358 "reading delete-delay file");
365 bp = deldelay_buf + read_pos;
367 if (sscanf(bp, "%x ", &mode) != 1) {
369 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
372 past_space = strchr(bp, ' ') + 1;
373 len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
376 if (len > MAXPATHLEN) {
377 rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
381 /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
382 * instead of returning a pointer to our buffer. */
383 memcpy(buf, past_space, len);
388 static void do_delayed_deletions(char *delbuf)
392 if (deldelay_fd >= 0) {
393 if (deldelay_cnt && !flush_delete_delay())
395 lseek(deldelay_fd, 0, 0);
397 while ((mode = read_delay_line(delbuf)) >= 0)
398 delete_item(delbuf, mode, NULL, DEL_RECURSE);
399 if (deldelay_fd >= 0)
403 /* This function is used to implement per-directory deletion, and is used by
404 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
405 * MAXPATHLEN buffer with the name of the directory in it (the functions we
406 * call will append names onto the end, but the old dir value will be restored
408 static void delete_in_dir(struct file_list *flist, char *fbuf,
409 struct file_struct *file, STRUCT_STAT *stp)
411 static int min_depth = MAXPATHLEN, cur_depth = -1;
412 static void *filt_array[MAXPATHLEN/2+1];
413 static int already_warned = 0;
414 struct file_list *dirlist;
415 char delbuf[MAXPATHLEN];
419 while (cur_depth >= min_depth)
420 pop_local_filters(filt_array[cur_depth--]);
421 min_depth = MAXPATHLEN;
427 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
430 maybe_send_keepalive();
432 if (file->dir.depth >= MAXPATHLEN/2+1)
433 return; /* Impossible... */
435 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
439 "IO error encountered -- skipping file deletion\n");
444 while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
445 pop_local_filters(filt_array[cur_depth--]);
446 cur_depth = file->dir.depth;
447 if (min_depth > cur_depth)
448 min_depth = cur_depth;
450 filt_array[cur_depth] = push_local_filters(fbuf, dlen);
452 if (one_file_system) {
453 if (file->flags & FLAG_TOP_DIR)
454 filesystem_dev = stp->st_dev;
455 else if (filesystem_dev != stp->st_dev)
459 dirlist = get_dirlist(fbuf, dlen, 0);
461 /* If an item in dirlist is not found in flist, delete it
462 * from the filesystem. */
463 for (i = dirlist->count; i--; ) {
464 struct file_struct *fp = dirlist->files[i];
465 if (!F_IS_ACTIVE(fp))
467 if (fp->flags & FLAG_MOUNT_DIR) {
469 rprintf(FINFO, "cannot delete mount point: %s\n",
473 if (flist_find(flist, fp) < 0) {
475 if (delete_during == 2) {
476 if (!remember_delete(fp, delbuf))
479 delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
486 /* This deletes any files on the receiving side that are not present on the
487 * sending side. This is used by --delete-before and --delete-after. */
488 static void do_delete_pass(struct file_list *flist)
490 char fbuf[MAXPATHLEN];
494 /* dry_run is incremented when the destination doesn't exist yet. */
495 if (dry_run > 1 || list_only)
498 for (j = 0; j < flist->count; j++) {
499 struct file_struct *file = flist->files[j];
501 if (!(file->flags & FLAG_XFER_DIR))
505 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
506 rprintf(FINFO, "deleting in %s\n", fbuf);
508 if (link_stat(fbuf, &st, keep_dirlinks) < 0
509 || !S_ISDIR(st.st_mode))
512 delete_in_dir(flist, fbuf, file, &st);
514 delete_in_dir(NULL, NULL, NULL, NULL);
516 if (do_progress && !am_server)
517 rprintf(FINFO, " \r");
520 int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
523 && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
526 if (am_root && preserve_uid && st->st_uid != F_UID(file))
529 if (preserve_gid && F_GID(file) != GID_NONE && st->st_gid != F_GID(file))
535 void itemize(struct file_struct *file, int ndx, int statret, STRUCT_STAT *st,
536 int32 iflags, uchar fnamecmp_type, const char *xname)
538 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
539 int keep_time = !preserve_times ? 0
540 : S_ISDIR(file->mode) ? !omit_dir_times
541 : !S_ISLNK(file->mode);
543 if (S_ISREG(file->mode) && F_LENGTH(file) != st->st_size)
544 iflags |= ITEM_REPORT_SIZE;
545 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
546 && !(iflags & ITEM_MATCHED)
547 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
548 || (keep_time && cmp_time(file->modtime, st->st_mtime) != 0))
549 iflags |= ITEM_REPORT_TIME;
550 if ((file->mode & CHMOD_BITS) != (st->st_mode & CHMOD_BITS))
551 iflags |= ITEM_REPORT_PERMS;
552 if (preserve_uid && am_root && F_UID(file) != st->st_uid)
553 iflags |= ITEM_REPORT_OWNER;
554 if (preserve_gid && F_GID(file) != GID_NONE
555 && st->st_gid != F_GID(file))
556 iflags |= ITEM_REPORT_GROUP;
558 iflags |= ITEM_IS_NEW;
561 if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
562 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
563 if (protocol_version >= 29) {
565 write_int(sock_f_out, ndx);
566 write_shortint(sock_f_out, iflags);
567 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
568 write_byte(sock_f_out, fnamecmp_type);
569 if (iflags & ITEM_XNAME_FOLLOWS)
570 write_vstring(sock_f_out, xname, strlen(xname));
571 } else if (ndx >= 0) {
572 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
573 log_item(code, file, &stats, iflags, xname);
579 /* Perform our quick-check heuristic for determining if a file is unchanged. */
580 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
582 if (st->st_size != F_LENGTH(file))
585 /* if always checksum is set then we use the checksum instead
586 of the file time to determine whether to sync */
587 if (always_checksum && S_ISREG(st->st_mode)) {
588 char sum[MD4_SUM_LENGTH];
589 file_checksum(fn, sum, st->st_size);
590 return memcmp(sum, F_SUM(file), checksum_len) == 0;
599 return cmp_time(st->st_mtime, file->modtime) == 0;
604 * set (initialize) the size entries in the per-file sum_struct
605 * calculating dynamic block and checksum sizes.
607 * This is only called from generate_and_send_sums() but is a separate
608 * function to encapsulate the logic.
610 * The block size is a rounded square root of file length.
612 * The checksum size is determined according to:
613 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
614 * provided by Donovan Baarda which gives a probability of rsync
615 * algorithm corrupting data and falling back using the whole md4
618 * This might be made one of several selectable heuristics.
620 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
626 blength = block_size;
627 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
628 blength = BLOCK_SIZE;
633 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
634 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
635 blength = MAX_BLOCK_SIZE;
640 if (len < (int64)blength * blength)
643 } while (c >= 8); /* round to multiple of 8 */
644 blength = MAX(blength, BLOCK_SIZE);
648 if (protocol_version < 27) {
649 s2length = csum_length;
650 } else if (csum_length == SUM_LENGTH) {
651 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)(len / blength) + (sum->remainder != 0);
670 if (sum->count && verbose > 2) {
672 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
673 (double)sum->count, (long)sum->remainder, (long)sum->blength,
674 sum->s2length, (double)sum->flength);
680 * Generate and send a stream of signatures/checksums that describe a buffer
682 * Generate approximately one checksum every block_len bytes.
684 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
687 struct map_struct *mapbuf;
688 struct sum_struct sum;
691 sum_sizes_sqroot(&sum, len);
692 write_sum_head(f_out, &sum);
694 if (append_mode > 0 && f_copy < 0)
698 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
702 for (i = 0; i < sum.count; i++) {
703 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
704 char *map = map_ptr(mapbuf, offset, n1);
705 char sum2[SUM_LENGTH];
712 full_write(f_copy, map, n1);
717 sum1 = get_checksum1(map, n1);
718 get_checksum2(map, n1, sum2);
722 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
723 (double)i, (double)offset - n1, (long)n1,
724 (unsigned long)sum1);
726 write_int(f_out, sum1);
727 write_buf(f_out, sum2, sum.s2length);
735 /* Try to find a filename in the same dir as "fname" with a similar name. */
736 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
738 int fname_len, fname_suf_len;
739 const char *fname_suf, *fname = F_BASENAME(file);
740 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
741 int j, lowest_j = -1;
743 fname_len = strlen(fname);
744 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
746 for (j = 0; j < dirlist->count; j++) {
747 struct file_struct *fp = dirlist->files[j];
748 const char *suf, *name;
752 if (!S_ISREG(fp->mode) || !F_LENGTH(fp) || fp->flags & FLAG_SENT)
755 name = F_BASENAME(fp);
757 if (F_LENGTH(fp) == F_LENGTH(file)
758 && cmp_time(fp->modtime, file->modtime) == 0) {
761 "fuzzy size/modtime match for %s\n",
768 suf = find_filename_suffix(name, len, &suf_len);
770 dist = fuzzy_distance(name, len, fname, fname_len);
771 /* Add some extra weight to how well the suffixes match. */
772 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
775 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
776 name, (int)(dist>>16), (int)(dist&0xFFFF));
778 if (dist <= lowest_dist) {
787 #ifdef SUPPORT_HARD_LINKS
788 void check_for_finished_hlinks(int itemizing, enum logcode code)
790 struct file_struct *file;
793 while ((ndx = get_hlink_num()) != -1) {
794 if (ndx < 0 || ndx >= the_file_list->count)
797 file = the_file_list->files[ndx];
798 if (!F_IS_HLINKED(file))
801 hard_link_cluster(file, ndx, itemizing, code, -1);
806 /* This is only called for regular files. We return -2 if we've finished
807 * handling the file, -1 if no dest-linking occurred, or a non-negative
808 * value if we found an alternate basis file. */
809 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
810 char *cmpbuf, STRUCT_STAT *stp, int itemizing,
811 int maybe_ATTRS_REPORT, enum logcode code)
818 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
819 if (link_stat(cmpbuf, stp, 0) < 0 || !S_ISREG(stp->st_mode))
821 switch (match_level) {
827 if (!unchanged_file(cmpbuf, file, stp))
833 if (!unchanged_attrs(file, stp))
835 if (always_checksum && preserve_times
836 && cmp_time(stp->st_mtime, file->modtime))
843 } while (basis_dir[++j] != NULL);
848 if (j != best_match) {
850 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
851 if (link_stat(cmpbuf, stp, 0) < 0)
855 if (match_level == 3 && !copy_dest) {
856 #ifdef SUPPORT_HARD_LINKS
858 int i = itemizing && (verbose > 1 || stdout_format_has_i > 1);
859 if (hard_link_one(file, ndx, fname, 0, stp,
860 cmpbuf, 1, i, code) < 0)
862 if (preserve_hard_links && F_IS_HLINKED(file))
863 hard_link_cluster(file, ndx, itemizing, code, j);
867 itemize(file, ndx, 0, stp, 0, 0, NULL);
868 if (verbose > 1 && maybe_ATTRS_REPORT) {
869 rprintf(FCLIENT, "%s is uptodate\n", fname);
874 if (match_level >= 2) {
875 try_a_copy: /* Copy the file locally. */
876 if (copy_file(cmpbuf, fname, file->mode) < 0) {
878 rsyserr(FINFO, errno, "copy_file %s => %s",
879 full_fname(cmpbuf), fname);
884 itemize(file, ndx, 0, stp, ITEM_LOCAL_CHANGE, 0, NULL);
885 set_file_attrs(fname, file, NULL, 0);
886 if (maybe_ATTRS_REPORT
887 && ((!itemizing && verbose && match_level == 2)
888 || (verbose > 1 && match_level == 3))) {
889 code = match_level == 3 ? FCLIENT : FINFO;
890 rprintf(code, "%s%s\n", fname,
891 match_level == 3 ? " is uptodate" : "");
893 #ifdef SUPPORT_HARD_LINKS
894 if (preserve_hard_links && F_IS_HLINKED(file))
895 hard_link_cluster(file, ndx, itemizing, code, j);
900 return FNAMECMP_BASIS_DIR_LOW + j;
903 /* This is only called for non-regular files. We return -2 if we've finished
904 * handling the file, or -1 if no dest-linking occurred, or a non-negative
905 * value if we found an alternate basis file. */
906 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
907 char *cmpbuf, STRUCT_STAT *stp, int itemizing,
908 int maybe_ATTRS_REPORT, enum logcode code)
910 char lnk[MAXPATHLEN];
914 enum nonregtype type;
917 #ifndef SUPPORT_LINKS
918 if (S_ISLNK(file->mode))
921 if (S_ISDIR(file->mode)) {
923 } else if (IS_SPECIAL(file->mode))
925 else if (IS_DEVICE(file->mode))
928 else if (S_ISLNK(file->mode))
933 "internal: try_dests_non() called with invalid mode (%o)\n",
935 exit_cleanup(RERR_UNSUPPORTED);
939 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
940 if (link_stat(cmpbuf, stp, 0) < 0)
944 if (!S_ISDIR(stp->st_mode))
948 if (!IS_SPECIAL(stp->st_mode))
952 if (!IS_DEVICE(stp->st_mode))
957 if (!S_ISLNK(stp->st_mode))
962 if (match_level < 1) {
971 devp = F_RDEV_P(file);
972 if (stp->st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
977 if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
980 if (strcmp(lnk, F_SYMLINK(file)) != 0)
985 if (match_level < 2) {
989 if (unchanged_attrs(file, stp)) {
994 } while (basis_dir[++j] != NULL);
999 if (j != best_match) {
1001 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1002 if (link_stat(cmpbuf, stp, 0) < 0)
1006 if (match_level == 3) {
1007 #ifdef SUPPORT_HARD_LINKS
1009 #ifndef CAN_HARDLINK_SYMLINK
1010 && !S_ISLNK(file->mode)
1012 #ifndef CAN_HARDLINK_SPECIAL
1013 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
1015 && !S_ISDIR(file->mode)) {
1016 if (do_link(cmpbuf, fname) < 0) {
1017 rsyserr(FERROR, errno,
1018 "failed to hard-link %s with %s",
1022 if (preserve_hard_links && F_IS_HLINKED(file))
1023 hard_link_cluster(file, ndx, itemizing, code, -1);
1027 if (itemizing && stdout_format_has_i
1028 && (verbose > 1 || stdout_format_has_i > 1)) {
1029 int chg = compare_dest && type != TYPE_DIR ? 0
1031 + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1032 char *lp = match_level == 3 ? "" : NULL;
1033 itemize(file, ndx, 0, stp, chg + ITEM_MATCHED, 0, lp);
1035 if (verbose > 1 && maybe_ATTRS_REPORT) {
1036 rprintf(FCLIENT, "%s%s is uptodate\n",
1037 fname, type == TYPE_DIR ? "/" : "");
1045 static int phase = 0;
1047 /* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir,
1048 * make sure it exists, and has the right permissions/timestamp info. For
1049 * all other non-regular files (symlinks, etc.) we create them here. For
1050 * regular files that have changed, we try to find a basis file and then
1051 * start sending checksums.
1053 * When fname is non-null, it must point to a MAXPATHLEN buffer!
1055 * Note that f_out is set to -1 when doing final directory-permission and
1056 * modification-time repair. */
1057 static void recv_generator(char *fname, struct file_struct *file, int ndx,
1058 int itemizing, int maybe_ATTRS_REPORT,
1059 enum logcode code, int f_out)
1061 static int missing_below = -1, excluded_below = -1;
1062 static const char *parent_dirname = "";
1063 static struct file_list *fuzzy_dirlist = NULL;
1064 static int need_fuzzy_dirlist = 0;
1065 struct file_struct *fuzzy_file = NULL;
1066 int fd = -1, f_copy = -1;
1067 STRUCT_STAT st, real_st, partial_st;
1068 struct file_struct *back_file = NULL;
1069 int statret, real_ret, stat_errno;
1070 char *fnamecmp, *partialptr, *backupptr = NULL;
1071 char fnamecmpbuf[MAXPATHLEN];
1072 uchar fnamecmp_type;
1073 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1079 if (fuzzy_dirlist) {
1080 flist_free(fuzzy_dirlist);
1081 fuzzy_dirlist = NULL;
1083 if (missing_below >= 0) {
1088 parent_dirname = "";
1093 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
1095 if (server_filter_list.head) {
1096 if (excluded_below >= 0) {
1097 if (file->dir.depth > excluded_below)
1099 excluded_below = -1;
1101 if (check_filter(&server_filter_list, fname,
1102 S_ISDIR(file->mode)) < 0) {
1103 if (S_ISDIR(file->mode))
1104 excluded_below = file->dir.depth;
1108 "skipping server-excluded file \"%s\"\n",
1115 if (missing_below >= 0) {
1116 if (file->dir.depth <= missing_below) {
1120 } else if (!dry_run)
1125 stat_errno = ENOENT;
1127 const char *dn = file->dirname ? file->dirname : ".";
1128 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1129 if (relative_paths && !implied_dirs
1130 && do_stat(dn, &st) < 0
1131 && create_directory_path(fname) < 0) {
1132 rsyserr(FERROR, errno,
1133 "recv_generator: mkdir %s failed",
1136 if (fuzzy_dirlist) {
1137 flist_free(fuzzy_dirlist);
1138 fuzzy_dirlist = NULL;
1141 need_fuzzy_dirlist = 1;
1143 parent_dirname = dn;
1145 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
1146 strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1147 fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
1148 need_fuzzy_dirlist = 0;
1151 statret = link_stat(fname, &st,
1152 keep_dirlinks && S_ISDIR(file->mode));
1156 if (ignore_non_existing && statret == -1 && stat_errno == ENOENT) {
1158 rprintf(FINFO, "not creating new %s \"%s\"\n",
1159 S_ISDIR(file->mode) ? "directory" : "file",
1165 /* If we're not preserving permissions, change the file-list's
1166 * mode based on the local permissions and some heuristics. */
1167 if (!preserve_perms) {
1168 int exists = statret == 0
1169 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode);
1170 file->mode = dest_mode(file->mode, st.st_mode, exists);
1173 if (S_ISDIR(file->mode)) {
1174 /* The file to be received is a directory, so we need
1175 * to prepare appropriately. If there is already a
1176 * file of that name and it is *not* a directory, then
1177 * we need to delete it. If it doesn't exist, then
1178 * (perhaps recursively) create it. */
1179 if (statret == 0 && !S_ISDIR(st.st_mode)) {
1180 if (delete_item(fname, st.st_mode, "directory", del_opts) != 0)
1184 if (dry_run && statret != 0 && missing_below < 0) {
1185 missing_below = file->dir.depth;
1191 if (*fname == '.' && fname[1] == '\0')
1195 if (statret != 0 && basis_dir[0] != NULL) {
1196 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1197 itemizing, maybe_ATTRS_REPORT, code);
1204 if (itemizing && f_out != -1) {
1205 itemize(file, ndx, statret, &st,
1206 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1208 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1209 if (!relative_paths || errno != ENOENT
1210 || create_directory_path(fname) < 0
1211 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
1212 rsyserr(FERROR, errno,
1213 "recv_generator: mkdir %s failed",
1215 file->flags |= FLAG_MISSING_DIR;
1216 if (ndx+1 < the_file_list->count
1217 && the_file_list->files[ndx+1]->dir.depth > file->dir.depth) {
1219 "*** Skipping everything below this failed directory ***\n");
1220 missing_below = file->dir.depth;
1225 if (set_file_attrs(fname, file, real_ret ? NULL : &real_st, 0)
1226 && verbose && code != FNONE && f_out != -1)
1227 rprintf(code, "%s/\n", fname);
1228 if (real_ret != 0 && one_file_system)
1229 real_st.st_dev = filesystem_dev;
1230 if (delete_during && f_out != -1 && !phase && dry_run < 2
1231 && (file->flags & FLAG_XFER_DIR))
1232 delete_in_dir(the_file_list, fname, file, &real_st);
1236 #ifdef SUPPORT_HARD_LINKS
1237 if (preserve_hard_links && F_IS_HLINKED(file)
1238 && hard_link_check(file, ndx, fname, statret, &st,
1239 itemizing, code, HL_CHECK_MASTER))
1243 if (preserve_links && S_ISLNK(file->mode)) {
1244 #ifdef SUPPORT_LINKS
1245 const char *sl = F_SYMLINK(file);
1246 if (safe_symlinks && unsafe_symlink(sl, fname)) {
1249 fname = f_name(file, NULL);
1251 "ignoring unsafe symlink %s -> \"%s\"\n",
1252 full_fname(fname), sl);
1257 char lnk[MAXPATHLEN];
1260 if (!S_ISLNK(st.st_mode))
1262 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1263 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1264 /* The link is pointing to the right place. */
1266 itemize(file, ndx, 0, &st, 0, 0, NULL);
1267 set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1268 #ifdef SUPPORT_HARD_LINKS
1269 if (preserve_hard_links && F_IS_HLINKED(file))
1270 hard_link_cluster(file, ndx, itemizing, code, -1);
1272 if (remove_source_files == 1)
1273 goto return_with_success;
1276 /* Not the right symlink (or not a symlink), so
1278 if (delete_item(fname, st.st_mode, "symlink", del_opts) != 0)
1280 } else if (basis_dir[0] != NULL) {
1281 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1282 itemizing, maybe_ATTRS_REPORT, code);
1284 #ifndef CAN_HARDLINK_SYMLINK
1286 /* Resort to --copy-dest behavior. */
1296 #ifdef SUPPORT_HARD_LINKS
1297 if (preserve_hard_links && F_IS_HLINKED(file)
1298 && hard_link_check(file, ndx, fname, -1, &st,
1299 itemizing, code, HL_SKIP))
1302 if (do_symlink(sl, fname) != 0) {
1303 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
1304 full_fname(fname), sl);
1306 set_file_attrs(fname, file, NULL, 0);
1308 itemize(file, ndx, statret, &st,
1309 ITEM_LOCAL_CHANGE, 0, NULL);
1311 if (code != FNONE && verbose)
1312 rprintf(code, "%s -> %s\n", fname, sl);
1313 #ifdef SUPPORT_HARD_LINKS
1314 if (preserve_hard_links && F_IS_HLINKED(file))
1315 hard_link_cluster(file, ndx, itemizing, code, -1);
1317 /* This does not check remove_source_files == 1
1318 * because this is one of the items that the old
1319 * --remove-sent-files option would remove. */
1320 if (remove_source_files)
1321 goto return_with_success;
1327 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1328 || (preserve_specials && IS_SPECIAL(file->mode))) {
1329 uint32 *devp = F_RDEV_P(file);
1330 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1333 if (IS_DEVICE(file->mode)) {
1334 if (!IS_DEVICE(st.st_mode))
1338 if (!IS_SPECIAL(st.st_mode))
1343 && (st.st_mode & ~CHMOD_BITS) == (file->mode & ~CHMOD_BITS)
1344 && st.st_rdev == rdev) {
1345 /* The device or special file is identical. */
1347 itemize(file, ndx, 0, &st, 0, 0, NULL);
1348 set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1349 #ifdef SUPPORT_HARD_LINKS
1350 if (preserve_hard_links && F_IS_HLINKED(file))
1351 hard_link_cluster(file, ndx, itemizing, code, -1);
1353 if (remove_source_files == 1)
1354 goto return_with_success;
1357 if (delete_item(fname, st.st_mode, t, del_opts) != 0)
1359 } else if (basis_dir[0] != NULL) {
1360 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1361 itemizing, maybe_ATTRS_REPORT, code);
1363 #ifndef CAN_HARDLINK_SPECIAL
1365 /* Resort to --copy-dest behavior. */
1375 #ifdef SUPPORT_HARD_LINKS
1376 if (preserve_hard_links && F_IS_HLINKED(file)
1377 && hard_link_check(file, ndx, fname, -1, &st,
1378 itemizing, code, HL_SKIP))
1382 rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1383 fname, (int)file->mode,
1384 (long)major(rdev), (long)minor(rdev));
1386 if (do_mknod(fname, file->mode, rdev) < 0) {
1387 rsyserr(FERROR, errno, "mknod %s failed",
1390 set_file_attrs(fname, file, NULL, 0);
1392 itemize(file, ndx, statret, &st,
1393 ITEM_LOCAL_CHANGE, 0, NULL);
1395 if (code != FNONE && verbose)
1396 rprintf(code, "%s\n", fname);
1397 #ifdef SUPPORT_HARD_LINKS
1398 if (preserve_hard_links && F_IS_HLINKED(file))
1399 hard_link_cluster(file, ndx, itemizing, code, -1);
1401 if (remove_source_files == 1)
1402 goto return_with_success;
1407 if (!S_ISREG(file->mode)) {
1409 fname = f_name(file, NULL);
1410 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1414 if (max_size && F_LENGTH(file) > max_size) {
1417 fname = f_name(file, NULL);
1418 rprintf(FINFO, "%s is over max-size\n", fname);
1422 if (min_size && F_LENGTH(file) < min_size) {
1425 fname = f_name(file, NULL);
1426 rprintf(FINFO, "%s is under min-size\n", fname);
1431 if (ignore_existing && statret == 0) {
1433 rprintf(FINFO, "%s exists\n", fname);
1437 if (update_only && statret == 0
1438 && cmp_time(st.st_mtime, file->modtime) > 0) {
1440 rprintf(FINFO, "%s is newer\n", fname);
1445 fnamecmp_type = FNAMECMP_FNAME;
1447 if (statret == 0 && !S_ISREG(st.st_mode)) {
1448 if (delete_item(fname, st.st_mode, "regular file", del_opts) != 0)
1451 stat_errno = ENOENT;
1454 if (statret != 0 && basis_dir[0] != NULL) {
1455 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &st,
1456 itemizing, maybe_ATTRS_REPORT, code);
1458 if (remove_source_files == 1)
1459 goto return_with_success;
1463 fnamecmp = fnamecmpbuf;
1472 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1473 && link_stat(partialptr, &partial_st, 0) == 0
1474 && S_ISREG(partial_st.st_mode)) {
1476 goto prepare_to_open;
1480 if (statret != 0 && fuzzy_dirlist && dry_run <= 1) {
1481 int j = find_fuzzy(file, fuzzy_dirlist);
1483 fuzzy_file = fuzzy_dirlist->files[j];
1484 f_name(fuzzy_file, fnamecmpbuf);
1486 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1487 fname, fnamecmpbuf);
1489 st.st_size = F_LENGTH(fuzzy_file);
1491 fnamecmp = fnamecmpbuf;
1492 fnamecmp_type = FNAMECMP_FUZZY;
1497 #ifdef SUPPORT_HARD_LINKS
1498 if (preserve_hard_links && F_IS_HLINKED(file)
1499 && hard_link_check(file, ndx, fname, statret, &st,
1500 itemizing, code, HL_SKIP))
1503 if (stat_errno == ENOENT)
1505 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
1510 if (append_mode && st.st_size > F_LENGTH(file))
1513 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1515 else if (fnamecmp_type == FNAMECMP_FUZZY)
1517 else if (unchanged_file(fnamecmp, file, &st)) {
1519 do_unlink(partialptr);
1520 handle_partial_dir(partialptr, PDIR_DELETE);
1523 itemize(file, ndx, real_ret, &real_st,
1526 set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1527 #ifdef SUPPORT_HARD_LINKS
1528 if (preserve_hard_links && F_IS_HLINKED(file))
1529 hard_link_cluster(file, ndx, itemizing, code, -1);
1531 if (remove_source_files != 1)
1533 return_with_success:
1535 send_msg_int(MSG_SUCCESS, ndx);
1542 fnamecmp = partialptr;
1543 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1547 if (!do_xfers || read_batch || whole_file)
1550 if (fuzzy_dirlist) {
1551 int j = flist_find(fuzzy_dirlist, file);
1552 if (j >= 0) /* don't use changing file as future fuzzy basis */
1553 fuzzy_dirlist->files[j]->flags |= FLAG_SENT;
1557 fd = do_open(fnamecmp, O_RDONLY, 0);
1560 rsyserr(FERROR, errno, "failed to open %s, continuing",
1561 full_fname(fnamecmp));
1563 /* pretend the file didn't exist */
1564 #ifdef SUPPORT_HARD_LINKS
1565 if (preserve_hard_links && F_IS_HLINKED(file)
1566 && hard_link_check(file, ndx, fname, statret, &st,
1567 itemizing, code, HL_SKIP))
1570 statret = real_ret = -1;
1574 if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
1575 if (!(backupptr = get_backup_name(fname))) {
1579 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1581 goto pretend_missing;
1583 if (robust_unlink(backupptr) && errno != ENOENT) {
1584 rsyserr(FERROR, errno, "unlink %s",
1585 full_fname(backupptr));
1586 unmake_file(back_file);
1590 if ((f_copy = do_open(backupptr,
1591 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1592 rsyserr(FERROR, errno, "open %s",
1593 full_fname(backupptr));
1594 unmake_file(back_file);
1598 fnamecmp_type = FNAMECMP_BACKUP;
1602 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1603 fnamecmp, (double)st.st_size);
1607 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1610 if (remove_source_files && !delay_updates && !phase)
1611 increment_active_files(ndx, itemizing, code);
1612 write_int(f_out, ndx);
1614 int iflags = ITEM_TRANSFER;
1615 if (always_checksum)
1616 iflags |= ITEM_REPORT_CHECKSUM;
1617 if (fnamecmp_type != FNAMECMP_FNAME)
1618 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1619 if (fnamecmp_type == FNAMECMP_FUZZY)
1620 iflags |= ITEM_XNAME_FOLLOWS;
1621 itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type,
1622 fuzzy_file ? F_BASENAME(fuzzy_file) : NULL);
1626 #ifdef SUPPORT_HARD_LINKS
1627 if (preserve_hard_links && F_IS_HLINKED(file))
1628 hard_link_cluster(file, ndx, itemizing, code, -1);
1635 if (statret != 0 || whole_file) {
1636 write_sum_head(f_out, NULL);
1640 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
1644 set_file_attrs(backupptr, back_file, NULL, 0);
1646 rprintf(FINFO, "backed up %s to %s\n",
1649 unmake_file(back_file);
1655 void generate_files(int f_out, struct file_list *flist, char *local_name)
1658 char fbuf[MAXPATHLEN];
1659 int itemizing, maybe_ATTRS_REPORT;
1661 int lull_mod = allowed_lull * 5;
1662 int need_retouch_dir_times = preserve_times && !omit_dir_times;
1663 int need_retouch_dir_perms = 0;
1664 int save_ignore_existing = ignore_existing;
1665 int save_ignore_non_existing = ignore_non_existing;
1666 int save_do_progress = do_progress;
1667 int save_make_backups = make_backups;
1668 int dir_tweaking = !(list_only || local_name || dry_run);
1670 if (protocol_version >= 29) {
1672 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1673 code = logfile_format_has_i ? FNONE : FLOG;
1674 } else if (am_daemon) {
1675 itemizing = logfile_format_has_i && do_xfers;
1676 maybe_ATTRS_REPORT = ATTRS_REPORT;
1677 code = itemizing || !do_xfers ? FCLIENT : FINFO;
1678 } else if (!am_server) {
1679 itemizing = stdout_format_has_i;
1680 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1681 code = itemizing ? FNONE : FINFO;
1684 maybe_ATTRS_REPORT = ATTRS_REPORT;
1687 solo_file = local_name != NULL;
1690 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
1691 (long)getpid(), flist->count);
1694 if (delete_before && !local_name && flist->count > 0)
1695 do_delete_pass(flist);
1696 if (delete_during == 2) {
1697 deldelay_size = BIGPATHBUFLEN * 4;
1698 deldelay_buf = new_array(char, deldelay_size);
1700 out_of_memory("delete-delay");
1704 if (append_mode || whole_file < 0)
1707 rprintf(FINFO, "delta-transmission %s\n",
1709 ? "disabled for local transfer or --whole-file"
1713 /* Since we often fill up the outgoing socket and then just sit around
1714 * waiting for the other 2 processes to do their thing, we don't want
1715 * to exit on a timeout. If the data stops flowing, the receiver will
1716 * notice that and let us know via the redo pipe (or its closing). */
1719 for (i = 0; i < flist->count; i++) {
1720 struct file_struct *file = flist->files[i];
1722 if (!F_IS_ACTIVE(file))
1726 strlcpy(fbuf, local_name, sizeof fbuf);
1729 recv_generator(fbuf, file, i, itemizing, maybe_ATTRS_REPORT,
1732 /* We need to ensure that any dirs we create have writeable
1733 * permissions during the time we are putting files within
1734 * them. This is then fixed after the transfer is done. */
1736 if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)
1738 mode_t mode = file->mode | S_IWUSR; /* user write */
1739 char *fname = local_name ? local_name : fbuf;
1740 if (do_chmod(fname, mode) < 0) {
1741 rsyserr(FERROR, errno,
1742 "failed to modify permissions on %s",
1745 need_retouch_dir_perms = 1;
1749 #ifdef SUPPORT_HARD_LINKS
1750 if (preserve_hard_links)
1751 check_for_finished_hlinks(itemizing, code);
1754 if (allowed_lull && !(i % lull_mod))
1755 maybe_send_keepalive();
1756 else if (!(i % 200))
1757 maybe_flush_socket();
1759 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
1761 delete_in_dir(NULL, NULL, NULL, NULL);
1764 csum_length = SUM_LENGTH;
1765 max_size = min_size = ignore_existing = ignore_non_existing = 0;
1766 update_only = always_checksum = size_only = 0;
1768 if (append_mode) /* resend w/o append mode */
1769 append_mode = -1; /* ... but only longer files */
1770 make_backups = 0; /* avoid a duplicate backup for inplace processing */
1773 rprintf(FINFO, "generate_files phase=%d\n", phase);
1775 write_int(f_out, NDX_DONE);
1777 /* files can cycle through the system more than once
1778 * to catch initial checksum errors */
1779 while ((i = get_redo_num(itemizing, code)) != -1) {
1780 struct file_struct *file = flist->files[i];
1782 strlcpy(fbuf, local_name, sizeof fbuf);
1785 recv_generator(fbuf, file, i, itemizing, maybe_ATTRS_REPORT,
1790 ignore_non_existing = save_ignore_non_existing;
1791 ignore_existing = save_ignore_existing;
1792 make_backups = save_make_backups;
1795 rprintf(FINFO, "generate_files phase=%d\n", phase);
1797 write_int(f_out, NDX_DONE);
1798 /* Reduce round-trip lag-time for a useless delay-updates phase. */
1799 if (protocol_version >= 29 && !delay_updates)
1800 write_int(f_out, NDX_DONE);
1802 /* Read MSG_DONE for the redo phase (and any prior messages). */
1803 get_redo_num(itemizing, code);
1805 if (protocol_version >= 29) {
1808 rprintf(FINFO, "generate_files phase=%d\n", phase);
1810 write_int(f_out, NDX_DONE);
1811 /* Read MSG_DONE for delay-updates phase & prior messages. */
1812 get_redo_num(itemizing, code);
1815 do_progress = save_do_progress;
1816 if (delete_during == 2)
1817 do_delayed_deletions(fbuf);
1818 if (delete_after && !local_name && flist->count > 0)
1819 do_delete_pass(flist);
1821 if ((need_retouch_dir_perms || need_retouch_dir_times) && dir_tweaking) {
1823 /* Now we need to fix any directory permissions that were
1824 * modified during the transfer and/or re-set any tweaked
1825 * modified-time values. */
1826 for (i = 0; i < flist->count; i++) {
1827 struct file_struct *file = flist->files[i];
1828 if (!F_IS_ACTIVE(file) || !S_ISDIR(file->mode))
1830 if (!need_retouch_dir_times && file->mode & S_IWUSR)
1832 if (file->flags & FLAG_MISSING_DIR) {
1833 int missing = file->dir.depth;
1834 while (++i < flist->count) {
1835 file = flist->files[i];
1836 if (file->dir.depth <= missing)
1842 recv_generator(f_name(file, NULL), file, i, itemizing,
1843 maybe_ATTRS_REPORT, code, -1);
1844 if (allowed_lull && !(++j % lull_mod))
1845 maybe_send_keepalive();
1846 else if (!(j % 200))
1847 maybe_flush_socket();
1850 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
1852 if (max_delete >= 0 && deletion_count > max_delete) {
1854 "Deletions stopped due to --max-delete limit (%d skipped)\n",
1855 deletion_count - max_delete);
1856 io_error |= IOERR_DEL_LIMIT;
1860 rprintf(FINFO, "generate_files finished\n");