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 incremental;
35 extern int do_progress;
36 extern int relative_paths;
37 extern int implied_dirs;
38 extern int keep_dirlinks;
39 extern int preserve_links;
40 extern int preserve_devices;
41 extern int preserve_specials;
42 extern int preserve_hard_links;
43 extern int preserve_perms;
44 extern int preserve_uid;
45 extern int preserve_gid;
46 extern int preserve_times;
47 extern int omit_dir_times;
48 extern int delete_mode;
49 extern int delete_before;
50 extern int delete_during;
51 extern int delete_after;
53 extern int ignore_errors;
54 extern int remove_source_files;
55 extern int delay_updates;
56 extern int update_only;
57 extern int ignore_existing;
58 extern int ignore_non_existing;
60 extern int append_mode;
61 extern int make_backups;
62 extern int csum_length;
63 extern int ignore_times;
65 extern OFF_T max_size;
66 extern OFF_T min_size;
69 extern int allowed_lull;
70 extern int sock_f_out;
71 extern int ignore_timeout;
72 extern int protocol_version;
73 extern int file_total;
74 extern int fuzzy_basis;
75 extern int always_checksum;
76 extern int checksum_len;
77 extern char *partial_dir;
78 extern char *basis_dir[];
79 extern int compare_dest;
82 extern int whole_file;
84 extern int new_root_dir;
85 extern int read_batch;
86 extern int safe_symlinks;
87 extern long block_size; /* "long" because popt can't set an int32. */
88 extern int max_delete;
89 extern int force_delete;
90 extern int one_file_system;
91 extern struct stats stats;
92 extern dev_t filesystem_dev;
93 extern char *backup_dir;
94 extern char *backup_suffix;
95 extern int backup_suffix_len;
96 extern struct file_list *cur_flist, *first_flist, *dir_flist;
97 extern struct filter_list_struct server_filter_list;
99 int ignore_perishable = 0;
100 int non_perishable_cnt = 0;
101 int maybe_ATTRS_REPORT = 0;
103 static dev_t dev_zero;
104 static int deletion_count = 0; /* used to implement --max-delete */
105 static int deldelay_size = 0, deldelay_cnt = 0;
106 static char *deldelay_buf = NULL;
107 static int deldelay_fd = -1;
108 static BOOL solo_file = 0;
110 /* For calling delete_item() and delete_dir_contents(). */
111 #define DEL_RECURSE (1<<1) /* recurse */
112 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
115 TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
119 DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_NOT_EMPTY
122 /* Forward declaration for delete_item(). */
123 static enum delret delete_dir_contents(char *fname, int flags);
126 static int is_backup_file(char *fn)
128 int k = strlen(fn) - backup_suffix_len;
129 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
132 /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
133 * delete recursively.
135 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
136 * a directory! (The buffer is used for recursion, but returned unchanged.)
138 static enum delret delete_item(char *fbuf, int mode, char *replace, int flags)
145 rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
149 if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
150 ignore_perishable = 1;
151 /* If DEL_RECURSE is not set, this just reports emptiness. */
152 ret = delete_dir_contents(fbuf, flags);
153 ignore_perishable = 0;
154 if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT)
156 /* OK: try to delete the directory. */
159 if (!replace && max_delete >= 0 && ++deletion_count > max_delete)
164 ok = do_rmdir(fbuf) == 0;
165 } else if (make_backups > 0 && (backup_dir || !is_backup_file(fbuf))) {
166 what = "make_backup";
167 ok = make_backup(fbuf);
170 ok = robust_unlink(fbuf) == 0;
175 log_delete(fbuf, mode);
178 if (S_ISDIR(mode) && errno == ENOTEMPTY) {
179 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
182 } else if (errno != ENOENT) {
183 rsyserr(FERROR, errno, "delete_file: %s(%s) failed",
193 if (replace && ret != DR_SUCCESS) {
194 rprintf(FERROR, "could not make way for new %s: %s\n",
200 /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
201 * its contents, otherwise just checks for content. Returns DR_SUCCESS or
202 * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
203 * buffer is used for recursion, but returned unchanged.)
205 static enum delret delete_dir_contents(char *fname, int flags)
207 struct file_list *dirlist;
215 rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n",
219 dlen = strlen(fname);
220 save_filters = push_local_filters(fname, dlen);
222 non_perishable_cnt = 0;
223 dirlist = get_dirlist(fname, dlen, 0);
224 ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
229 if (!(flags & DEL_RECURSE)) {
235 if (dlen != 1 || *fname != '/')
237 remainder = MAXPATHLEN - (p - fname);
239 /* We do our own recursion, so make delete_item() non-recursive. */
240 flags = (flags & ~DEL_RECURSE) | DEL_DIR_IS_EMPTY;
242 for (j = dirlist->count; j--; ) {
243 struct file_struct *fp = dirlist->files[j];
245 if (fp->flags & FLAG_MOUNT_DIR) {
248 "mount point, %s, pins parent directory\n",
255 strlcpy(p, fp->basename, remainder);
256 /* Save stack by recursing to ourself directly. */
257 if (S_ISDIR(fp->mode)
258 && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
260 if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
268 pop_local_filters(save_filters);
270 if (ret == DR_NOT_EMPTY) {
271 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
277 static int start_delete_delay_temp(void)
279 char fnametmp[MAXPATHLEN];
280 int save_dry_run = dry_run;
283 if (!get_tmpname(fnametmp, "deldelay")
284 || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
285 rprintf(FINFO, "NOTE: Unable to create delete-delay temp file%s.\n",
286 incremental ? "" : " -- switching to --delete-after");
288 delete_after = !incremental;
289 dry_run = save_dry_run;
293 dry_run = save_dry_run;
297 static int flush_delete_delay(void)
299 if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
300 rsyserr(FERROR, errno, "flush of delete-delay buffer");
310 static int remember_delete(struct file_struct *file, const char *fname)
315 len = snprintf(deldelay_buf + deldelay_cnt,
316 deldelay_size - deldelay_cnt,
317 "%x %s%c", (int)file->mode, fname, '\0');
318 if ((deldelay_cnt += len) <= deldelay_size)
320 if (deldelay_fd < 0 && !start_delete_delay_temp())
323 if (!flush_delete_delay())
330 static int read_delay_line(char *buf)
332 static int read_pos = 0;
334 char *bp, *past_space;
337 for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
338 if (j < deldelay_cnt)
340 if (deldelay_fd < 0) {
345 deldelay_cnt -= read_pos;
346 if (deldelay_cnt == deldelay_size)
348 if (deldelay_cnt && read_pos) {
349 memmove(deldelay_buf, deldelay_buf + read_pos,
352 len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
353 deldelay_size - deldelay_cnt);
357 "ERROR: unexpected EOF in delete-delay file.\n");
362 rsyserr(FERROR, errno,
363 "reading delete-delay file");
370 bp = deldelay_buf + read_pos;
372 if (sscanf(bp, "%x ", &mode) != 1) {
374 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
377 past_space = strchr(bp, ' ') + 1;
378 len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
381 if (len > MAXPATHLEN) {
382 rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
386 /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
387 * instead of returning a pointer to our buffer. */
388 memcpy(buf, past_space, len);
393 static void do_delayed_deletions(char *delbuf)
397 if (deldelay_fd >= 0) {
398 if (deldelay_cnt && !flush_delete_delay())
400 lseek(deldelay_fd, 0, 0);
402 while ((mode = read_delay_line(delbuf)) >= 0)
403 delete_item(delbuf, mode, NULL, DEL_RECURSE);
404 if (deldelay_fd >= 0)
408 /* This function is used to implement per-directory deletion, and is used by
409 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
410 * MAXPATHLEN buffer with the name of the directory in it (the functions we
411 * call will append names onto the end, but the old dir value will be restored
413 static void delete_in_dir(struct file_list *flist, char *fbuf,
414 struct file_struct *file, dev_t *fs_dev)
416 static int already_warned = 0;
417 struct file_list *dirlist;
418 char delbuf[MAXPATHLEN];
422 change_local_filter_dir(NULL, 0, 0);
427 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
430 maybe_send_keepalive();
432 if (io_error && !ignore_errors) {
436 "IO error encountered -- skipping file deletion\n");
442 change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
444 if (one_file_system) {
445 if (file->flags & FLAG_TOP_DIR)
446 filesystem_dev = *fs_dev;
447 else if (filesystem_dev != *fs_dev)
451 dirlist = get_dirlist(fbuf, dlen, 0);
453 /* If an item in dirlist is not found in flist, delete it
454 * from the filesystem. */
455 for (i = dirlist->count; i--; ) {
456 struct file_struct *fp = dirlist->files[i];
457 if (!F_IS_ACTIVE(fp))
459 if (fp->flags & FLAG_MOUNT_DIR) {
461 rprintf(FINFO, "cannot delete mount point: %s\n",
465 if (flist_find(flist, fp) < 0) {
467 if (delete_during == 2) {
468 if (!remember_delete(fp, delbuf))
471 delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
478 /* This deletes any files on the receiving side that are not present on the
479 * sending side. This is used by --delete-before and --delete-after. */
480 static void do_delete_pass(struct file_list *flist)
482 char fbuf[MAXPATHLEN];
486 /* dry_run is incremented when the destination doesn't exist yet. */
487 if (dry_run > 1 || list_only)
490 for (j = 0; j < flist->count; j++) {
491 struct file_struct *file = flist->files[j];
493 if (!(file->flags & FLAG_XFER_DIR))
497 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
498 rprintf(FINFO, "deleting in %s\n", fbuf);
500 if (link_stat(fbuf, &st, keep_dirlinks) < 0
501 || !S_ISDIR(st.st_mode))
504 delete_in_dir(flist, fbuf, file, &st.st_dev);
506 delete_in_dir(NULL, NULL, NULL, &dev_zero);
508 if (do_progress && !am_server)
509 rprintf(FINFO, " \r");
512 int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
514 if (preserve_perms && !BITS_EQUAL(st->st_mode, file->mode, CHMOD_BITS))
517 if (am_root && preserve_uid && st->st_uid != F_UID(file))
520 if (preserve_gid && F_GID(file) != GID_NONE && st->st_gid != F_GID(file))
526 void itemize(struct file_struct *file, int ndx, int statret,
527 STRUCT_STAT *st, int32 iflags, uchar fnamecmp_type,
530 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
531 int keep_time = !preserve_times ? 0
532 : S_ISDIR(file->mode) ? !omit_dir_times
533 : !S_ISLNK(file->mode);
535 if (S_ISREG(file->mode) && F_LENGTH(file) != st->st_size)
536 iflags |= ITEM_REPORT_SIZE;
537 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
538 && !(iflags & ITEM_MATCHED)
539 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
540 || (keep_time && cmp_time(file->modtime, st->st_mtime) != 0))
541 iflags |= ITEM_REPORT_TIME;
542 if (!BITS_EQUAL(st->st_mode, file->mode, CHMOD_BITS))
543 iflags |= ITEM_REPORT_PERMS;
544 if (preserve_uid && am_root && F_UID(file) != st->st_uid)
545 iflags |= ITEM_REPORT_OWNER;
546 if (preserve_gid && F_GID(file) != GID_NONE
547 && st->st_gid != F_GID(file))
548 iflags |= ITEM_REPORT_GROUP;
550 iflags |= ITEM_IS_NEW;
553 if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
554 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
555 if (protocol_version >= 29) {
557 write_ndx(sock_f_out, ndx + cur_flist->ndx_start);
558 write_shortint(sock_f_out, iflags);
559 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
560 write_byte(sock_f_out, fnamecmp_type);
561 if (iflags & ITEM_XNAME_FOLLOWS)
562 write_vstring(sock_f_out, xname, strlen(xname));
563 } else if (ndx >= 0) {
564 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
565 log_item(code, file, &stats, iflags, xname);
571 /* Perform our quick-check heuristic for determining if a file is unchanged. */
572 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
574 if (st->st_size != F_LENGTH(file))
577 /* if always checksum is set then we use the checksum instead
578 of the file time to determine whether to sync */
579 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
580 char sum[MD4_SUM_LENGTH];
581 file_checksum(fn, sum, st->st_size);
582 return memcmp(sum, F_SUM(file), checksum_len) == 0;
591 return cmp_time(st->st_mtime, file->modtime) == 0;
596 * set (initialize) the size entries in the per-file sum_struct
597 * calculating dynamic block and checksum sizes.
599 * This is only called from generate_and_send_sums() but is a separate
600 * function to encapsulate the logic.
602 * The block size is a rounded square root of file length.
604 * The checksum size is determined according to:
605 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
606 * provided by Donovan Baarda which gives a probability of rsync
607 * algorithm corrupting data and falling back using the whole md4
610 * This might be made one of several selectable heuristics.
612 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
618 blength = block_size;
619 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
620 blength = BLOCK_SIZE;
625 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
626 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
627 blength = MAX_BLOCK_SIZE;
632 if (len < (int64)blength * blength)
635 } while (c >= 8); /* round to multiple of 8 */
636 blength = MAX(blength, BLOCK_SIZE);
640 if (protocol_version < 27) {
641 s2length = csum_length;
642 } else if (csum_length == SUM_LENGTH) {
643 s2length = SUM_LENGTH;
647 int b = BLOCKSUM_BIAS;
648 for (l = len; l >>= 1; b += 2) {}
649 for (c = blength; (c >>= 1) && b; b--) {}
650 /* add a bit, subtract rollsum, round up. */
651 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
652 s2length = MAX(s2length, csum_length);
653 s2length = MIN(s2length, SUM_LENGTH);
657 sum->blength = blength;
658 sum->s2length = s2length;
659 sum->remainder = (int32)(len % blength);
660 sum->count = (int32)(len / blength) + (sum->remainder != 0);
662 if (sum->count && verbose > 2) {
664 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
665 (double)sum->count, (long)sum->remainder, (long)sum->blength,
666 sum->s2length, (double)sum->flength);
672 * Generate and send a stream of signatures/checksums that describe a buffer
674 * Generate approximately one checksum every block_len bytes.
676 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
679 struct map_struct *mapbuf;
680 struct sum_struct sum;
683 sum_sizes_sqroot(&sum, len);
684 write_sum_head(f_out, &sum);
686 if (append_mode > 0 && f_copy < 0)
690 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
694 for (i = 0; i < sum.count; i++) {
695 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
696 char *map = map_ptr(mapbuf, offset, n1);
697 char sum2[SUM_LENGTH];
704 full_write(f_copy, map, n1);
709 sum1 = get_checksum1(map, n1);
710 get_checksum2(map, n1, sum2);
714 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
715 (double)i, (double)offset - n1, (long)n1,
716 (unsigned long)sum1);
718 write_int(f_out, sum1);
719 write_buf(f_out, sum2, sum.s2length);
727 /* Try to find a filename in the same dir as "fname" with a similar name. */
728 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
730 int fname_len, fname_suf_len;
731 const char *fname_suf, *fname = file->basename;
732 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
733 int j, lowest_j = -1;
735 fname_len = strlen(fname);
736 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
738 for (j = 0; j < dirlist->count; j++) {
739 struct file_struct *fp = dirlist->files[j];
740 const char *suf, *name;
744 if (!S_ISREG(fp->mode) || !F_LENGTH(fp)
745 || fp->flags & FLAG_FILE_SENT)
750 if (F_LENGTH(fp) == F_LENGTH(file)
751 && cmp_time(fp->modtime, file->modtime) == 0) {
754 "fuzzy size/modtime match for %s\n",
761 suf = find_filename_suffix(name, len, &suf_len);
763 dist = fuzzy_distance(name, len, fname, fname_len);
764 /* Add some extra weight to how well the suffixes match. */
765 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
768 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
769 name, (int)(dist>>16), (int)(dist&0xFFFF));
771 if (dist <= lowest_dist) {
780 #ifdef SUPPORT_HARD_LINKS
781 void check_for_finished_hlinks(int itemizing, enum logcode code)
783 struct file_struct *file;
784 struct file_list *flist;
785 char fbuf[MAXPATHLEN];
788 while ((ndx = get_hlink_num()) != -1) {
789 flist = flist_for_ndx(ndx);
790 assert(flist != NULL);
791 file = flist->files[ndx];
792 assert(file->flags & FLAG_HLINKED);
793 finish_hard_link(file, f_name(file, fbuf), NULL, itemizing, code, -1);
798 /* This is only called for regular files. We return -2 if we've finished
799 * handling the file, -1 if no dest-linking occurred, or a non-negative
800 * value if we found an alternate basis file. */
801 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
802 char *cmpbuf, STRUCT_STAT *stp, int itemizing,
810 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
811 if (link_stat(cmpbuf, stp, 0) < 0 || !S_ISREG(stp->st_mode))
813 switch (match_level) {
819 if (!unchanged_file(cmpbuf, file, stp))
825 if (!unchanged_attrs(file, stp))
827 if (always_checksum > 0 && preserve_times
828 && cmp_time(stp->st_mtime, file->modtime))
835 } while (basis_dir[++j] != NULL);
840 if (j != best_match) {
842 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
843 if (link_stat(cmpbuf, stp, 0) < 0)
847 if (match_level == 3 && !copy_dest) {
848 #ifdef SUPPORT_HARD_LINKS
850 if (!hard_link_one(file, fname, cmpbuf, 1))
852 if (preserve_hard_links && F_IS_HLINKED(file))
853 finish_hard_link(file, fname, stp, itemizing, code, j);
854 if (itemizing && (verbose > 1 || stdout_format_has_i > 1)) {
855 itemize(file, ndx, 1, stp,
856 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
862 itemize(file, ndx, 0, stp, 0, 0, NULL);
863 if (verbose > 1 && maybe_ATTRS_REPORT)
864 rprintf(FCLIENT, "%s is uptodate\n", fname);
868 if (match_level >= 2) {
869 #ifdef SUPPORT_HARD_LINKS
870 try_a_copy: /* Copy the file locally. */
872 if (copy_file(cmpbuf, fname, file->mode) < 0) {
874 rsyserr(FINFO, errno, "copy_file %s => %s",
875 full_fname(cmpbuf), fname);
880 itemize(file, ndx, 0, stp, ITEM_LOCAL_CHANGE, 0, NULL);
881 set_file_attrs(fname, file, NULL, 0);
882 if (maybe_ATTRS_REPORT
883 && ((!itemizing && verbose && match_level == 2)
884 || (verbose > 1 && match_level == 3))) {
885 code = match_level == 3 ? FCLIENT : FINFO;
886 rprintf(code, "%s%s\n", fname,
887 match_level == 3 ? " is uptodate" : "");
889 #ifdef SUPPORT_HARD_LINKS
890 if (preserve_hard_links && F_IS_HLINKED(file))
891 finish_hard_link(file, fname, stp, itemizing, code, -1);
896 return FNAMECMP_BASIS_DIR_LOW + j;
899 /* This is only called for non-regular files. We return -2 if we've finished
900 * handling the file, or -1 if no dest-linking occurred, or a non-negative
901 * value if we found an alternate basis file. */
902 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
903 char *cmpbuf, STRUCT_STAT *stp, int itemizing,
906 char lnk[MAXPATHLEN];
909 enum nonregtype type;
913 #ifndef SUPPORT_LINKS
914 if (S_ISLNK(file->mode))
917 if (S_ISDIR(file->mode)) {
919 } else if (IS_SPECIAL(file->mode))
921 else if (IS_DEVICE(file->mode))
924 else if (S_ISLNK(file->mode))
929 "internal: try_dests_non() called with invalid mode (%o)\n",
931 exit_cleanup(RERR_UNSUPPORTED);
935 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
936 if (link_stat(cmpbuf, stp, 0) < 0)
940 if (!S_ISDIR(stp->st_mode))
944 if (!IS_SPECIAL(stp->st_mode))
948 if (!IS_DEVICE(stp->st_mode))
953 if (!S_ISLNK(stp->st_mode))
958 if (match_level < 1) {
967 devp = F_RDEV_P(file);
968 if (stp->st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
973 if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
976 if (strcmp(lnk, F_SYMLINK(file)) != 0)
981 if (match_level < 2) {
985 if (unchanged_attrs(file, stp)) {
990 } while (basis_dir[++j] != NULL);
995 if (j != best_match) {
997 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
998 if (link_stat(cmpbuf, stp, 0) < 0)
1002 if (match_level == 3) {
1003 #ifdef SUPPORT_HARD_LINKS
1005 #ifndef CAN_HARDLINK_SYMLINK
1006 && !S_ISLNK(file->mode)
1008 #ifndef CAN_HARDLINK_SPECIAL
1009 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
1011 && !S_ISDIR(file->mode)) {
1012 if (do_link(cmpbuf, fname) < 0) {
1013 rsyserr(FERROR, errno,
1014 "failed to hard-link %s with %s",
1018 if (preserve_hard_links && F_IS_HLINKED(file))
1019 finish_hard_link(file, fname, NULL, itemizing, code, -1);
1023 if (itemizing && stdout_format_has_i
1024 && (verbose > 1 || stdout_format_has_i > 1)) {
1025 int chg = compare_dest && type != TYPE_DIR ? 0
1027 + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1028 char *lp = match_level == 3 ? "" : NULL;
1029 itemize(file, ndx, 0, stp, chg + ITEM_MATCHED, 0, lp);
1031 if (verbose > 1 && maybe_ATTRS_REPORT) {
1032 rprintf(FCLIENT, "%s%s is uptodate\n",
1033 fname, type == TYPE_DIR ? "/" : "");
1041 static int phase = 0;
1043 /* Acts on cur_flist->file's ndx'th item, whose name is fname. If a dir,
1044 * make sure it exists, and has the right permissions/timestamp info. For
1045 * all other non-regular files (symlinks, etc.) we create them here. For
1046 * regular files that have changed, we try to find a basis file and then
1047 * start sending checksums.
1049 * When fname is non-null, it must point to a MAXPATHLEN buffer!
1051 * Note that f_out is set to -1 when doing final directory-permission and
1052 * modification-time repair. */
1053 static void recv_generator(char *fname, struct file_struct *file, int ndx,
1054 int itemizing, enum logcode code, int f_out)
1056 static int missing_below = -1, excluded_below = -1;
1057 static const char *parent_dirname = "";
1058 static struct file_list *fuzzy_dirlist = NULL;
1059 static int need_fuzzy_dirlist = 0;
1060 struct file_struct *fuzzy_file = NULL;
1061 int fd = -1, f_copy = -1;
1062 STRUCT_STAT st, real_st, partial_st;
1063 struct file_struct *back_file = NULL;
1064 int statret, real_ret, stat_errno;
1065 char *fnamecmp, *partialptr, *backupptr = NULL;
1066 char fnamecmpbuf[MAXPATHLEN];
1067 uchar fnamecmp_type;
1068 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1074 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
1076 if (server_filter_list.head) {
1077 if (excluded_below >= 0) {
1078 if (F_DEPTH(file) > excluded_below)
1080 excluded_below = -1;
1082 if (check_filter(&server_filter_list, fname,
1083 S_ISDIR(file->mode)) < 0) {
1084 if (S_ISDIR(file->mode))
1085 excluded_below = F_DEPTH(file);
1089 "skipping server-excluded file \"%s\"\n",
1096 if (missing_below >= 0) {
1097 if (F_DEPTH(file) <= missing_below) {
1101 } else if (!dry_run) {
1102 if (S_ISDIR(file->mode))
1103 file->flags |= FLAG_MISSING_DIR;
1108 if (fuzzy_dirlist) {
1109 flist_free(fuzzy_dirlist);
1110 fuzzy_dirlist = NULL;
1112 parent_dirname = "";
1114 stat_errno = ENOENT;
1116 const char *dn = file->dirname ? file->dirname : ".";
1117 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1118 if (relative_paths && !implied_dirs
1119 && do_stat(dn, &st) < 0
1120 && create_directory_path(fname) < 0) {
1121 rsyserr(FERROR, errno,
1122 "recv_generator: mkdir %s failed",
1125 if (fuzzy_dirlist) {
1126 flist_free(fuzzy_dirlist);
1127 fuzzy_dirlist = NULL;
1130 need_fuzzy_dirlist = 1;
1132 parent_dirname = dn;
1134 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
1135 strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1136 fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
1137 need_fuzzy_dirlist = 0;
1140 statret = link_stat(fname, &st,
1141 keep_dirlinks && S_ISDIR(file->mode));
1145 if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
1147 rprintf(FINFO, "not creating new %s \"%s\"\n",
1148 S_ISDIR(file->mode) ? "directory" : "file",
1151 if (S_ISDIR(file->mode)) {
1152 if (missing_below < 0) {
1155 missing_below = F_DEPTH(file);
1157 file->flags |= FLAG_MISSING_DIR;
1162 /* If we're not preserving permissions, change the file-list's
1163 * mode based on the local permissions and some heuristics. */
1164 if (!preserve_perms) {
1165 int exists = statret == 0
1166 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode);
1167 file->mode = dest_mode(file->mode, st.st_mode, exists);
1170 if (S_ISDIR(file->mode)) {
1171 /* The file to be received is a directory, so we need
1172 * to prepare appropriately. If there is already a
1173 * file of that name and it is *not* a directory, then
1174 * we need to delete it. If it doesn't exist, then
1175 * (perhaps recursively) create it. */
1176 if (statret == 0 && !S_ISDIR(st.st_mode)) {
1177 if (delete_item(fname, st.st_mode, "directory", del_opts) != 0)
1181 if (dry_run && statret != 0 && missing_below < 0) {
1182 missing_below = F_DEPTH(file);
1188 if (*fname == '.' && fname[1] == '\0')
1192 if (statret != 0 && basis_dir[0] != NULL) {
1193 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1201 if (itemizing && f_out != -1) {
1202 itemize(file, ndx, statret, &st,
1203 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1205 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1206 if (!relative_paths || errno != ENOENT
1207 || create_directory_path(fname) < 0
1208 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
1209 rsyserr(FERROR, errno,
1210 "recv_generator: mkdir %s failed",
1213 "*** Skipping any contents from this failed directory ***\n");
1214 missing_below = F_DEPTH(file);
1215 file->flags |= FLAG_MISSING_DIR;
1219 if (set_file_attrs(fname, file, real_ret ? NULL : &real_st, 0)
1220 && verbose && code != FNONE && f_out != -1)
1221 rprintf(code, "%s/\n", fname);
1222 if (real_ret != 0 && one_file_system)
1223 real_st.st_dev = filesystem_dev;
1225 if (one_file_system) {
1226 uint32 *devp = F_DIRDEV_P(file);
1227 DEV_MAJOR(devp) = major(real_st.st_dev);
1228 DEV_MINOR(devp) = minor(real_st.st_dev);
1231 else if (delete_during && f_out != -1 && !phase && dry_run < 2
1232 && (file->flags & FLAG_XFER_DIR))
1233 delete_in_dir(cur_flist, fname, file, &real_st.st_dev);
1237 #ifdef SUPPORT_HARD_LINKS
1238 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1239 && hard_link_check(file, ndx, fname, statret, &st, itemizing, code))
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 finish_hard_link(file, fname, &st, 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,
1284 #ifndef CAN_HARDLINK_SYMLINK
1286 /* Resort to --copy-dest behavior. */
1296 #ifdef SUPPORT_HARD_LINKS
1297 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1300 if (do_symlink(sl, fname) != 0) {
1301 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
1302 full_fname(fname), sl);
1304 set_file_attrs(fname, file, NULL, 0);
1306 itemize(file, ndx, statret, &st,
1307 ITEM_LOCAL_CHANGE, 0, NULL);
1309 if (code != FNONE && verbose)
1310 rprintf(code, "%s -> %s\n", fname, sl);
1311 #ifdef SUPPORT_HARD_LINKS
1312 if (preserve_hard_links && F_IS_HLINKED(file))
1313 finish_hard_link(file, fname, NULL, itemizing, code, -1);
1315 /* This does not check remove_source_files == 1
1316 * because this is one of the items that the old
1317 * --remove-sent-files option would remove. */
1318 if (remove_source_files)
1319 goto return_with_success;
1325 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1326 || (preserve_specials && IS_SPECIAL(file->mode))) {
1327 uint32 *devp = F_RDEV_P(file);
1328 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1331 if (IS_DEVICE(file->mode)) {
1332 if (!IS_DEVICE(st.st_mode))
1336 if (!IS_SPECIAL(st.st_mode))
1341 && BITS_EQUAL(st.st_mode, file->mode, _S_IFMT)
1342 && st.st_rdev == rdev) {
1343 /* The device or special file is identical. */
1345 itemize(file, ndx, 0, &st, 0, 0, NULL);
1346 set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1347 #ifdef SUPPORT_HARD_LINKS
1348 if (preserve_hard_links && F_IS_HLINKED(file))
1349 finish_hard_link(file, fname, &st, itemizing, code, -1);
1351 if (remove_source_files == 1)
1352 goto return_with_success;
1355 if (delete_item(fname, st.st_mode, t, del_opts) != 0)
1357 } else if (basis_dir[0] != NULL) {
1358 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1361 #ifndef CAN_HARDLINK_SPECIAL
1363 /* Resort to --copy-dest behavior. */
1373 #ifdef SUPPORT_HARD_LINKS
1374 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1378 rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1379 fname, (int)file->mode,
1380 (long)major(rdev), (long)minor(rdev));
1382 if (do_mknod(fname, file->mode, rdev) < 0) {
1383 rsyserr(FERROR, errno, "mknod %s failed",
1386 set_file_attrs(fname, file, NULL, 0);
1388 itemize(file, ndx, statret, &st,
1389 ITEM_LOCAL_CHANGE, 0, NULL);
1391 if (code != FNONE && verbose)
1392 rprintf(code, "%s\n", fname);
1393 #ifdef SUPPORT_HARD_LINKS
1394 if (preserve_hard_links && F_IS_HLINKED(file))
1395 finish_hard_link(file, fname, NULL, itemizing, code, -1);
1397 if (remove_source_files == 1)
1398 goto return_with_success;
1403 if (!S_ISREG(file->mode)) {
1405 fname = f_name(file, NULL);
1406 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1410 if (max_size > 0 && F_LENGTH(file) > max_size) {
1413 fname = f_name(file, NULL);
1414 rprintf(FINFO, "%s is over max-size\n", fname);
1418 if (min_size > 0 && F_LENGTH(file) < min_size) {
1421 fname = f_name(file, NULL);
1422 rprintf(FINFO, "%s is under min-size\n", fname);
1427 if (ignore_existing > 0 && statret == 0) {
1429 rprintf(FINFO, "%s exists\n", fname);
1433 if (update_only > 0 && statret == 0
1434 && cmp_time(st.st_mtime, file->modtime) > 0) {
1436 rprintf(FINFO, "%s is newer\n", fname);
1441 fnamecmp_type = FNAMECMP_FNAME;
1443 if (statret == 0 && !S_ISREG(st.st_mode)) {
1444 if (delete_item(fname, st.st_mode, "regular file", del_opts) != 0)
1447 stat_errno = ENOENT;
1450 if (statret != 0 && basis_dir[0] != NULL) {
1451 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &st,
1454 if (remove_source_files == 1)
1455 goto return_with_success;
1459 fnamecmp = fnamecmpbuf;
1468 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1469 && link_stat(partialptr, &partial_st, 0) == 0
1470 && S_ISREG(partial_st.st_mode)) {
1472 goto prepare_to_open;
1476 if (statret != 0 && fuzzy_dirlist && dry_run <= 1) {
1477 int j = find_fuzzy(file, fuzzy_dirlist);
1479 fuzzy_file = fuzzy_dirlist->files[j];
1480 f_name(fuzzy_file, fnamecmpbuf);
1482 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1483 fname, fnamecmpbuf);
1485 st.st_size = F_LENGTH(fuzzy_file);
1487 fnamecmp = fnamecmpbuf;
1488 fnamecmp_type = FNAMECMP_FUZZY;
1493 #ifdef SUPPORT_HARD_LINKS
1494 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1497 if (stat_errno == ENOENT)
1499 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
1504 if (append_mode > 0 && st.st_size > F_LENGTH(file))
1507 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1509 else if (fnamecmp_type == FNAMECMP_FUZZY)
1511 else if (unchanged_file(fnamecmp, file, &st)) {
1513 do_unlink(partialptr);
1514 handle_partial_dir(partialptr, PDIR_DELETE);
1517 itemize(file, ndx, statret, &st, 0, 0, NULL);
1518 set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1519 #ifdef SUPPORT_HARD_LINKS
1520 if (preserve_hard_links && F_IS_HLINKED(file))
1521 finish_hard_link(file, fname, &st, itemizing, code, -1);
1523 if (remove_source_files != 1)
1525 return_with_success:
1527 send_msg_int(MSG_SUCCESS, ndx + cur_flist->ndx_start);
1534 fnamecmp = partialptr;
1535 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1539 if (!do_xfers || read_batch || whole_file)
1542 if (fuzzy_dirlist) {
1543 int j = flist_find(fuzzy_dirlist, file);
1544 if (j >= 0) /* don't use changing file as future fuzzy basis */
1545 fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
1549 fd = do_open(fnamecmp, O_RDONLY, 0);
1552 rsyserr(FERROR, errno, "failed to open %s, continuing",
1553 full_fname(fnamecmp));
1555 /* pretend the file didn't exist */
1556 #ifdef SUPPORT_HARD_LINKS
1557 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1560 statret = real_ret = -1;
1564 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1565 if (!(backupptr = get_backup_name(fname))) {
1569 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1571 goto pretend_missing;
1573 if (robust_unlink(backupptr) && errno != ENOENT) {
1574 rsyserr(FERROR, errno, "unlink %s",
1575 full_fname(backupptr));
1576 unmake_file(back_file);
1580 if ((f_copy = do_open(backupptr,
1581 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1582 rsyserr(FERROR, errno, "open %s",
1583 full_fname(backupptr));
1584 unmake_file(back_file);
1588 fnamecmp_type = FNAMECMP_BACKUP;
1592 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1593 fnamecmp, (double)st.st_size);
1597 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1600 if (remove_source_files && !delay_updates && !phase)
1601 increment_active_files(ndx, itemizing, code);
1602 if (incremental && !dry_run)
1603 cur_flist->in_progress++;
1604 #ifdef SUPPORT_HARD_LINKS
1605 if (preserve_hard_links && F_IS_HLINKED(file))
1606 file->flags |= FLAG_FILE_SENT;
1608 write_ndx(f_out, ndx + cur_flist->ndx_start);
1610 int iflags = ITEM_TRANSFER;
1611 if (always_checksum > 0)
1612 iflags |= ITEM_REPORT_CHECKSUM;
1613 if (fnamecmp_type != FNAMECMP_FNAME)
1614 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1615 if (fnamecmp_type == FNAMECMP_FUZZY)
1616 iflags |= ITEM_XNAME_FOLLOWS;
1617 itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type,
1618 fuzzy_file ? fuzzy_file->basename : NULL);
1622 #ifdef SUPPORT_HARD_LINKS
1623 if (preserve_hard_links && F_IS_HLINKED(file))
1624 finish_hard_link(file, fname, &st, itemizing, code, -1);
1631 if (statret != 0 || whole_file) {
1632 write_sum_head(f_out, NULL);
1636 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
1640 set_file_attrs(backupptr, back_file, NULL, 0);
1642 rprintf(FINFO, "backed up %s to %s\n",
1645 unmake_file(back_file);
1651 static void touch_up_dirs(struct file_list *flist, int ndx,
1652 int need_retouch_dir_times, int lull_mod)
1654 struct file_struct *file;
1656 int i, j, start, end;
1660 end = flist->count - 1;
1664 /* Fix any directory permissions that were modified during the
1665 * transfer and/or re-set any tweaked modified-time values. */
1666 for (i = start, j = 0; i <= end; i++) {
1667 file = flist->files[i];
1668 if (!F_IS_ACTIVE(file) || !S_ISDIR(file->mode)
1669 || file->flags & FLAG_MISSING_DIR)
1671 if (!need_retouch_dir_times && file->mode & S_IWUSR)
1673 fname = f_name(file, NULL);
1674 if (!(file->mode & S_IWUSR))
1675 do_chmod(fname, file->mode);
1676 if (need_retouch_dir_times)
1677 set_modtime(fname, file->modtime, file->mode);
1678 if (allowed_lull && !(++j % lull_mod))
1679 maybe_send_keepalive();
1680 else if (!(j % 200))
1681 maybe_flush_socket();
1685 void generate_files(int f_out, char *local_name)
1688 char fbuf[MAXPATHLEN];
1691 struct file_list *next_flist;
1692 int lull_mod = allowed_lull * 5;
1693 int need_retouch_dir_times = preserve_times && !omit_dir_times;
1694 int need_retouch_dir_perms = 0;
1695 int save_do_progress = do_progress;
1696 int dir_tweaking = !(list_only || local_name || dry_run);
1698 if (protocol_version >= 29) {
1700 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1701 code = logfile_format_has_i ? FNONE : FLOG;
1702 } else if (am_daemon) {
1703 itemizing = logfile_format_has_i && do_xfers;
1704 maybe_ATTRS_REPORT = ATTRS_REPORT;
1705 code = itemizing || !do_xfers ? FCLIENT : FINFO;
1706 } else if (!am_server) {
1707 itemizing = stdout_format_has_i;
1708 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1709 code = itemizing ? FNONE : FINFO;
1712 maybe_ATTRS_REPORT = ATTRS_REPORT;
1715 solo_file = local_name != NULL;
1718 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
1720 if (delete_before && !local_name && cur_flist->count > 0)
1721 do_delete_pass(cur_flist);
1722 if (delete_during == 2) {
1723 deldelay_size = BIGPATHBUFLEN * 4;
1724 deldelay_buf = new_array(char, deldelay_size);
1726 out_of_memory("delete-delay");
1730 if (append_mode > 0 || whole_file < 0)
1733 rprintf(FINFO, "delta-transmission %s\n",
1735 ? "disabled for local transfer or --whole-file"
1739 /* Since we often fill up the outgoing socket and then just sit around
1740 * waiting for the other 2 processes to do their thing, we don't want
1741 * to exit on a timeout. If the data stops flowing, the receiver will
1742 * notice that and let us know via the redo pipe (or its closing). */
1746 if (incremental && delete_during && cur_flist->ndx_start) {
1747 struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
1748 if (BITS_SETnUNSET(fp->flags, FLAG_XFER_DIR, FLAG_MISSING_DIR)) {
1750 if (one_file_system) {
1751 uint32 *devp = F_DIRDEV_P(fp);
1752 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1754 dirdev = MAKEDEV(0, 0);
1755 delete_in_dir(cur_flist, f_name(fp, fbuf), fp, &dirdev);
1758 for (i = cur_flist->low; i <= cur_flist->high; i++) {
1759 struct file_struct *file = cur_flist->files[i];
1761 if (!F_IS_ACTIVE(file))
1765 strlcpy(fbuf, local_name, sizeof fbuf);
1768 recv_generator(fbuf, file, i, itemizing, code, f_out);
1770 /* We need to ensure that any dirs we create have
1771 * writeable permissions during the time we are putting
1772 * files within them. This is then fixed after the
1773 * transfer is done. */
1775 if (!am_root && S_ISDIR(file->mode)
1776 && !(file->mode & S_IWUSR) && dir_tweaking) {
1777 mode_t mode = file->mode | S_IWUSR;
1778 char *fname = local_name ? local_name : fbuf;
1779 if (do_chmod(fname, mode) < 0) {
1780 rsyserr(FERROR, errno,
1781 "failed to modify permissions on %s",
1784 need_retouch_dir_perms = 1;
1788 #ifdef SUPPORT_HARD_LINKS
1789 if (preserve_hard_links)
1790 check_for_finished_hlinks(itemizing, code);
1793 if (allowed_lull && !(i % lull_mod))
1794 maybe_send_keepalive();
1795 else if (!(i % 200))
1796 maybe_flush_socket();
1801 delete_in_dir(NULL, NULL, NULL, &dev_zero);
1804 rprintf(FINFO, "generate_files phase=%d\n",
1808 write_ndx(f_out, NDX_DONE);
1811 csum_length = SUM_LENGTH;
1812 max_size = -max_size;
1813 min_size = -min_size;
1814 ignore_existing = -ignore_existing;
1815 ignore_non_existing = -ignore_non_existing;
1816 update_only = -update_only;
1817 always_checksum = -always_checksum;
1818 size_only = -size_only;
1819 append_mode = -append_mode;
1820 make_backups = -make_backups; /* avoid dup backup w/inplace */
1823 /* Files can cycle through the system more than once
1824 * to catch initial checksum errors. */
1826 struct file_struct *file;
1827 struct file_list *save_flist;
1829 check_for_finished_hlinks(itemizing, code);
1831 if ((i = get_redo_num()) == -1) {
1834 wait_for_receiver();
1838 save_flist = cur_flist;
1839 cur_flist = flist_for_ndx(i);
1840 file = cur_flist->files[i];
1842 strlcpy(fbuf, local_name, sizeof fbuf);
1845 recv_generator(fbuf, file, i, itemizing, code, f_out);
1846 cur_flist->to_redo--;
1847 cur_flist = save_flist;
1850 csum_length = SHORT_SUM_LENGTH;
1851 max_size = -max_size;
1852 min_size = -min_size;
1853 ignore_existing = -ignore_existing;
1854 ignore_non_existing = -ignore_non_existing;
1855 update_only = -update_only;
1856 always_checksum = -always_checksum;
1857 size_only = -size_only;
1858 append_mode = -append_mode;
1859 make_backups = -make_backups;
1865 while (!cur_flist->next && !flist_eof)
1866 wait_for_receiver();
1867 next_flist = cur_flist->next;
1868 while (first_flist != next_flist) {
1869 if (first_flist->in_progress || first_flist->to_redo) {
1872 wait_for_receiver();
1876 cur_flist = first_flist;
1877 if (delete_during == 2 || !dir_tweaking) {
1878 /* Skip directory touch-up. */
1879 } else if (cur_flist->ndx_start != 0) {
1880 touch_up_dirs(dir_flist, cur_flist->parent_ndx,
1881 need_retouch_dir_times, lull_mod);
1882 } else if (relative_paths && implied_dirs) {
1883 touch_up_dirs(cur_flist, -1,
1884 need_retouch_dir_times, lull_mod);
1887 flist_free(first_flist); /* updates cur_flist & first_flist */
1890 write_ndx(f_out, NDX_DONE);
1892 } while ((cur_flist = next_flist) != NULL);
1896 rprintf(FINFO, "generate_files phase=%d\n", phase);
1898 write_ndx(f_out, NDX_DONE);
1899 /* Reduce round-trip lag-time for a useless delay-updates phase. */
1900 if (protocol_version >= 29 && !delay_updates)
1901 write_ndx(f_out, NDX_DONE);
1903 /* Read MSG_DONE for the redo phase (and any prior messages). */
1904 while (done_cnt <= 1) {
1905 check_for_finished_hlinks(itemizing, code);
1906 wait_for_receiver();
1909 if (protocol_version >= 29) {
1912 rprintf(FINFO, "generate_files phase=%d\n", phase);
1914 write_ndx(f_out, NDX_DONE);
1915 /* Read MSG_DONE for delay-updates phase & prior messages. */
1916 while (done_cnt == 2)
1917 wait_for_receiver();
1920 do_progress = save_do_progress;
1921 if (delete_during == 2)
1922 do_delayed_deletions(fbuf);
1923 if (delete_after && !local_name && file_total > 0)
1924 do_delete_pass(cur_flist);
1926 if ((need_retouch_dir_perms || need_retouch_dir_times)
1927 && dir_tweaking && (!incremental || delete_during == 2)) {
1928 touch_up_dirs(incremental ? dir_flist : cur_flist, -1,
1929 need_retouch_dir_times, lull_mod);
1932 if (max_delete >= 0 && deletion_count > max_delete) {
1934 "Deletions stopped due to --max-delete limit (%d skipped)\n",
1935 deletion_count - max_delete);
1936 io_error |= IOERR_DEL_LIMIT;
1940 rprintf(FINFO, "generate_files finished\n");