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_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;
52 extern int msgdone_cnt;
53 extern int ignore_errors;
54 extern int remove_source_files;
55 extern int delay_updates;
56 extern int update_only;
57 extern int 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 mode_t orig_umask;
94 extern char *backup_dir;
95 extern char *backup_suffix;
96 extern int backup_suffix_len;
97 extern struct file_list *cur_flist, *first_flist, *dir_flist;
98 extern struct filter_list_struct server_filter_list;
100 int ignore_perishable = 0;
101 int non_perishable_cnt = 0;
102 int maybe_ATTRS_REPORT = 0;
104 static dev_t dev_zero;
105 static int deletion_count = 0; /* used to implement --max-delete */
106 static int deldelay_size = 0, deldelay_cnt = 0;
107 static char *deldelay_buf = NULL;
108 static int deldelay_fd = -1;
110 static int dir_tweaking;
111 static int need_retouch_dir_times;
112 static const char *solo_file = NULL;
114 /* For calling delete_item() and delete_dir_contents(). */
115 #define DEL_RECURSE (1<<1) /* recurse */
116 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
119 TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
123 DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_NOT_EMPTY
126 /* Forward declaration for delete_item(). */
127 static enum delret delete_dir_contents(char *fname, int flags);
130 static int is_backup_file(char *fn)
132 int k = strlen(fn) - backup_suffix_len;
133 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
136 /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
137 * delete recursively.
139 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
140 * a directory! (The buffer is used for recursion, but returned unchanged.)
142 static enum delret delete_item(char *fbuf, int mode, char *replace, int flags)
149 rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
153 if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
154 ignore_perishable = 1;
155 /* If DEL_RECURSE is not set, this just reports emptiness. */
156 ret = delete_dir_contents(fbuf, flags);
157 ignore_perishable = 0;
158 if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT)
160 /* OK: try to delete the directory. */
163 if (!replace && max_delete >= 0 && ++deletion_count > max_delete)
168 ok = do_rmdir(fbuf) == 0;
169 } else if (make_backups > 0 && (backup_dir || !is_backup_file(fbuf))) {
170 what = "make_backup";
171 ok = make_backup(fbuf);
174 ok = robust_unlink(fbuf) == 0;
179 log_delete(fbuf, mode);
182 if (S_ISDIR(mode) && errno == ENOTEMPTY) {
183 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
186 } else if (errno != ENOENT) {
187 rsyserr(FERROR, errno, "delete_file: %s(%s) failed",
197 if (replace && ret != DR_SUCCESS) {
198 rprintf(FERROR, "could not make way for new %s: %s\n",
204 /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
205 * its contents, otherwise just checks for content. Returns DR_SUCCESS or
206 * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
207 * buffer is used for recursion, but returned unchanged.)
209 static enum delret delete_dir_contents(char *fname, int flags)
211 struct file_list *dirlist;
219 rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n",
223 dlen = strlen(fname);
224 save_filters = push_local_filters(fname, dlen);
226 non_perishable_cnt = 0;
227 dirlist = get_dirlist(fname, dlen, 0);
228 ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
233 if (!(flags & DEL_RECURSE)) {
239 if (dlen != 1 || *fname != '/')
241 remainder = MAXPATHLEN - (p - fname);
243 /* We do our own recursion, so make delete_item() non-recursive. */
244 flags = (flags & ~DEL_RECURSE) | DEL_DIR_IS_EMPTY;
246 for (j = dirlist->count; j--; ) {
247 struct file_struct *fp = dirlist->files[j];
249 if (fp->flags & FLAG_MOUNT_DIR) {
252 "mount point, %s, pins parent directory\n",
259 strlcpy(p, fp->basename, remainder);
260 /* Save stack by recursing to ourself directly. */
261 if (S_ISDIR(fp->mode)
262 && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
264 if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
272 pop_local_filters(save_filters);
274 if (ret == DR_NOT_EMPTY) {
275 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
281 static int start_delete_delay_temp(void)
283 char fnametmp[MAXPATHLEN];
284 int save_dry_run = dry_run;
287 if (!get_tmpname(fnametmp, "deldelay")
288 || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
289 rprintf(FINFO, "NOTE: Unable to create delete-delay temp file%s.\n",
290 inc_recurse ? "" : " -- switching to --delete-after");
292 delete_after = !inc_recurse;
293 dry_run = save_dry_run;
297 dry_run = save_dry_run;
301 static int flush_delete_delay(void)
303 if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
304 rsyserr(FERROR, errno, "flush of delete-delay buffer");
314 static int remember_delete(struct file_struct *file, const char *fname)
319 len = snprintf(deldelay_buf + deldelay_cnt,
320 deldelay_size - deldelay_cnt,
321 "%x %s%c", (int)file->mode, fname, '\0');
322 if ((deldelay_cnt += len) <= deldelay_size)
324 if (deldelay_fd < 0 && !start_delete_delay_temp())
327 if (!flush_delete_delay())
334 static int read_delay_line(char *buf)
336 static int read_pos = 0;
338 char *bp, *past_space;
341 for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
342 if (j < deldelay_cnt)
344 if (deldelay_fd < 0) {
349 deldelay_cnt -= read_pos;
350 if (deldelay_cnt == deldelay_size)
352 if (deldelay_cnt && read_pos) {
353 memmove(deldelay_buf, deldelay_buf + read_pos,
356 len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
357 deldelay_size - deldelay_cnt);
361 "ERROR: unexpected EOF in delete-delay file.\n");
366 rsyserr(FERROR, errno,
367 "reading delete-delay file");
374 bp = deldelay_buf + read_pos;
376 if (sscanf(bp, "%x ", &mode) != 1) {
378 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
381 past_space = strchr(bp, ' ') + 1;
382 len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
385 if (len > MAXPATHLEN) {
386 rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
390 /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
391 * instead of returning a pointer to our buffer. */
392 memcpy(buf, past_space, len);
397 static void do_delayed_deletions(char *delbuf)
401 if (deldelay_fd >= 0) {
402 if (deldelay_cnt && !flush_delete_delay())
404 lseek(deldelay_fd, 0, 0);
406 while ((mode = read_delay_line(delbuf)) >= 0)
407 delete_item(delbuf, mode, NULL, DEL_RECURSE);
408 if (deldelay_fd >= 0)
412 /* This function is used to implement per-directory deletion, and is used by
413 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
414 * MAXPATHLEN buffer with the name of the directory in it (the functions we
415 * call will append names onto the end, but the old dir value will be restored
417 static void delete_in_dir(struct file_list *flist, char *fbuf,
418 struct file_struct *file, dev_t *fs_dev)
420 static int already_warned = 0;
421 struct file_list *dirlist;
422 char delbuf[MAXPATHLEN];
426 change_local_filter_dir(NULL, 0, 0);
431 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
434 maybe_send_keepalive();
436 if (io_error && !ignore_errors) {
440 "IO error encountered -- skipping file deletion\n");
446 change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
448 if (one_file_system) {
449 if (file->flags & FLAG_TOP_DIR)
450 filesystem_dev = *fs_dev;
451 else if (filesystem_dev != *fs_dev)
455 dirlist = get_dirlist(fbuf, dlen, 0);
457 /* If an item in dirlist is not found in flist, delete it
458 * from the filesystem. */
459 for (i = dirlist->count; i--; ) {
460 struct file_struct *fp = dirlist->files[i];
461 if (!F_IS_ACTIVE(fp))
463 if (fp->flags & FLAG_MOUNT_DIR) {
465 rprintf(FINFO, "cannot delete mount point: %s\n",
469 if (flist_find(flist, fp) < 0) {
471 if (delete_during == 2) {
472 if (!remember_delete(fp, delbuf))
475 delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
482 /* This deletes any files on the receiving side that are not present on the
483 * sending side. This is used by --delete-before and --delete-after. */
484 static void do_delete_pass(struct file_list *flist)
486 char fbuf[MAXPATHLEN];
490 /* dry_run is incremented when the destination doesn't exist yet. */
491 if (dry_run > 1 || list_only)
494 for (j = 0; j < flist->count; j++) {
495 struct file_struct *file = flist->files[j];
497 if (!(file->flags & FLAG_XFER_DIR))
501 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
502 rprintf(FINFO, "deleting in %s\n", fbuf);
504 if (link_stat(fbuf, &st, keep_dirlinks) < 0
505 || !S_ISDIR(st.st_mode))
508 delete_in_dir(flist, fbuf, file, &st.st_dev);
510 delete_in_dir(NULL, NULL, NULL, &dev_zero);
512 if (do_progress && !am_server)
513 rprintf(FINFO, " \r");
516 int unchanged_attrs(const char *fname, struct file_struct *file, statx *sxp)
518 if (preserve_perms && !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
521 if (am_root && preserve_uid && sxp->st.st_uid != F_UID(file))
524 if (preserve_gid && F_GID(file) != GID_NONE && sxp->st.st_gid != F_GID(file))
528 if (preserve_acls && !S_ISLNK(file->mode)) {
529 if (!ACL_READY(*sxp))
531 if (set_acl(NULL, file, sxp) == 0)
539 void itemize(const char *fname, struct file_struct *file, int ndx, int statret,
540 statx *sxp, int32 iflags, uchar fnamecmp_type,
543 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
544 int keep_time = !preserve_times ? 0
545 : S_ISDIR(file->mode) ? !omit_dir_times
546 : !S_ISLNK(file->mode);
548 if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
549 iflags |= ITEM_REPORT_SIZE;
550 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
551 && !(iflags & ITEM_MATCHED)
552 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
553 || (keep_time && cmp_time(file->modtime, sxp->st.st_mtime) != 0))
554 iflags |= ITEM_REPORT_TIME;
555 if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
556 iflags |= ITEM_REPORT_PERMS;
557 if (preserve_uid && am_root && F_UID(file) != sxp->st.st_uid)
558 iflags |= ITEM_REPORT_OWNER;
559 if (preserve_gid && F_GID(file) != GID_NONE
560 && sxp->st.st_gid != F_GID(file))
561 iflags |= ITEM_REPORT_GROUP;
563 if (preserve_acls && !S_ISLNK(file->mode)) {
564 if (!ACL_READY(*sxp))
566 if (set_acl(NULL, file, sxp) == 0)
567 iflags |= ITEM_REPORT_ACL;
571 iflags |= ITEM_IS_NEW;
574 if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
575 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
576 if (protocol_version >= 29) {
578 write_ndx(sock_f_out, ndx);
579 write_shortint(sock_f_out, iflags);
580 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
581 write_byte(sock_f_out, fnamecmp_type);
582 if (iflags & ITEM_XNAME_FOLLOWS)
583 write_vstring(sock_f_out, xname, strlen(xname));
584 } else if (ndx >= 0) {
585 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
586 log_item(code, file, &stats, iflags, xname);
592 /* Perform our quick-check heuristic for determining if a file is unchanged. */
593 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
595 if (st->st_size != F_LENGTH(file))
598 /* if always checksum is set then we use the checksum instead
599 of the file time to determine whether to sync */
600 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
601 char sum[MAX_DIGEST_LEN];
602 file_checksum(fn, sum, st->st_size);
603 return memcmp(sum, F_SUM(file), checksum_len) == 0;
612 return cmp_time(st->st_mtime, file->modtime) == 0;
617 * set (initialize) the size entries in the per-file sum_struct
618 * calculating dynamic block and checksum sizes.
620 * This is only called from generate_and_send_sums() but is a separate
621 * function to encapsulate the logic.
623 * The block size is a rounded square root of file length.
625 * The checksum size is determined according to:
626 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
627 * provided by Donovan Baarda which gives a probability of rsync
628 * algorithm corrupting data and falling back using the whole md4
631 * This might be made one of several selectable heuristics.
633 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
639 blength = block_size;
640 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
641 blength = BLOCK_SIZE;
646 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
647 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
648 blength = MAX_BLOCK_SIZE;
653 if (len < (int64)blength * blength)
656 } while (c >= 8); /* round to multiple of 8 */
657 blength = MAX(blength, BLOCK_SIZE);
661 if (protocol_version < 27) {
662 s2length = csum_length;
663 } else if (csum_length == SUM_LENGTH) {
664 s2length = SUM_LENGTH;
668 int b = BLOCKSUM_BIAS;
669 for (l = len; l >>= 1; b += 2) {}
670 for (c = blength; (c >>= 1) && b; b--) {}
671 /* add a bit, subtract rollsum, round up. */
672 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
673 s2length = MAX(s2length, csum_length);
674 s2length = MIN(s2length, SUM_LENGTH);
678 sum->blength = blength;
679 sum->s2length = s2length;
680 sum->remainder = (int32)(len % blength);
681 sum->count = (int32)(len / blength) + (sum->remainder != 0);
683 if (sum->count && verbose > 2) {
685 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
686 (double)sum->count, (long)sum->remainder, (long)sum->blength,
687 sum->s2length, (double)sum->flength);
693 * Generate and send a stream of signatures/checksums that describe a buffer
695 * Generate approximately one checksum every block_len bytes.
697 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
700 struct map_struct *mapbuf;
701 struct sum_struct sum;
704 sum_sizes_sqroot(&sum, len);
705 write_sum_head(f_out, &sum);
707 if (append_mode > 0 && f_copy < 0)
711 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
715 for (i = 0; i < sum.count; i++) {
716 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
717 char *map = map_ptr(mapbuf, offset, n1);
718 char sum2[SUM_LENGTH];
725 full_write(f_copy, map, n1);
730 sum1 = get_checksum1(map, n1);
731 get_checksum2(map, n1, sum2);
735 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
736 (double)i, (double)offset - n1, (long)n1,
737 (unsigned long)sum1);
739 write_int(f_out, sum1);
740 write_buf(f_out, sum2, sum.s2length);
748 /* Try to find a filename in the same dir as "fname" with a similar name. */
749 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
751 int fname_len, fname_suf_len;
752 const char *fname_suf, *fname = file->basename;
753 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
754 int j, lowest_j = -1;
756 fname_len = strlen(fname);
757 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
759 for (j = 0; j < dirlist->count; j++) {
760 struct file_struct *fp = dirlist->files[j];
761 const char *suf, *name;
765 if (!S_ISREG(fp->mode) || !F_LENGTH(fp)
766 || fp->flags & FLAG_FILE_SENT)
771 if (F_LENGTH(fp) == F_LENGTH(file)
772 && cmp_time(fp->modtime, file->modtime) == 0) {
775 "fuzzy size/modtime match for %s\n",
782 suf = find_filename_suffix(name, len, &suf_len);
784 dist = fuzzy_distance(name, len, fname, fname_len);
785 /* Add some extra weight to how well the suffixes match. */
786 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
789 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
790 name, (int)(dist>>16), (int)(dist&0xFFFF));
792 if (dist <= lowest_dist) {
801 /* This is only called for regular files. We return -2 if we've finished
802 * handling the file, -1 if no dest-linking occurred, or a non-negative
803 * value if we found an alternate basis file. */
804 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
805 char *cmpbuf, statx *sxp, int itemizing,
813 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
814 if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
816 switch (match_level) {
822 if (!unchanged_file(cmpbuf, file, &sxp->st))
828 if (!unchanged_attrs(cmpbuf, file, sxp))
830 if (always_checksum > 0 && preserve_times
831 && cmp_time(sxp->st.st_mtime, file->modtime))
838 } while (basis_dir[++j] != NULL);
843 if (j != best_match) {
845 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
846 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
850 if (match_level == 3 && !copy_dest) {
851 #ifdef SUPPORT_HARD_LINKS
853 if (!hard_link_one(file, fname, cmpbuf, 1))
855 if (preserve_hard_links && F_IS_HLINKED(file))
856 finish_hard_link(file, fname, &sxp->st, itemizing, code, j);
857 if (itemizing && (verbose > 1 || stdout_format_has_i > 1)) {
858 itemize(fname, file, ndx, 1, sxp,
859 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
865 itemize(fname, file, ndx, 0, sxp, 0, 0, NULL);
866 if (verbose > 1 && maybe_ATTRS_REPORT)
867 rprintf(FCLIENT, "%s is uptodate\n", fname);
871 if (match_level >= 2) {
872 #ifdef SUPPORT_HARD_LINKS
873 try_a_copy: /* Copy the file locally. */
875 if (copy_file(cmpbuf, fname, file->mode) < 0) {
877 rsyserr(FINFO, errno, "copy_file %s => %s",
878 full_fname(cmpbuf), fname);
883 itemize(fname, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
884 set_file_attrs(fname, file, NULL, 0);
885 if (maybe_ATTRS_REPORT
886 && ((!itemizing && verbose && match_level == 2)
887 || (verbose > 1 && match_level == 3))) {
888 code = match_level == 3 ? FCLIENT : FINFO;
889 rprintf(code, "%s%s\n", fname,
890 match_level == 3 ? " is uptodate" : "");
892 #ifdef SUPPORT_HARD_LINKS
893 if (preserve_hard_links && F_IS_HLINKED(file))
894 finish_hard_link(file, fname, &sxp->st, itemizing, code, -1);
899 return FNAMECMP_BASIS_DIR_LOW + j;
902 /* This is only called for non-regular files. We return -2 if we've finished
903 * handling the file, or -1 if no dest-linking occurred, or a non-negative
904 * value if we found an alternate basis file. */
905 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
906 char *cmpbuf, statx *sxp, int itemizing,
909 char lnk[MAXPATHLEN];
912 enum nonregtype type;
916 #ifndef SUPPORT_LINKS
917 if (S_ISLNK(file->mode))
920 if (S_ISDIR(file->mode)) {
922 } else if (IS_SPECIAL(file->mode))
924 else if (IS_DEVICE(file->mode))
927 else if (S_ISLNK(file->mode))
932 "internal: try_dests_non() called with invalid mode (%o)\n",
934 exit_cleanup(RERR_UNSUPPORTED);
938 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
939 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
943 if (!S_ISDIR(sxp->st.st_mode))
947 if (!IS_SPECIAL(sxp->st.st_mode))
951 if (!IS_DEVICE(sxp->st.st_mode))
956 if (!S_ISLNK(sxp->st.st_mode))
961 if (match_level < 1) {
970 devp = F_RDEV_P(file);
971 if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
976 if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
979 if (strcmp(lnk, F_SYMLINK(file)) != 0)
984 if (match_level < 2) {
988 if (unchanged_attrs(cmpbuf, file, sxp)) {
993 } while (basis_dir[++j] != NULL);
998 if (j != best_match) {
1000 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1001 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1005 if (match_level == 3) {
1006 #ifdef SUPPORT_HARD_LINKS
1008 #ifndef CAN_HARDLINK_SYMLINK
1009 && !S_ISLNK(file->mode)
1011 #ifndef CAN_HARDLINK_SPECIAL
1012 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
1014 && !S_ISDIR(file->mode)) {
1015 if (do_link(cmpbuf, fname) < 0) {
1016 rsyserr(FERROR, errno,
1017 "failed to hard-link %s with %s",
1021 if (preserve_hard_links && F_IS_HLINKED(file))
1022 finish_hard_link(file, fname, NULL, itemizing, code, -1);
1026 if (itemizing && stdout_format_has_i
1027 && (verbose > 1 || stdout_format_has_i > 1)) {
1028 int chg = compare_dest && type != TYPE_DIR ? 0
1030 + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1031 char *lp = match_level == 3 ? "" : NULL;
1032 itemize(fname, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
1034 if (verbose > 1 && maybe_ATTRS_REPORT) {
1035 rprintf(FCLIENT, "%s%s is uptodate\n",
1036 fname, type == TYPE_DIR ? "/" : "");
1044 static int phase = 0;
1045 static int dflt_perms;
1047 /* Acts on the indicated item in cur_flist 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. The ndx is the file's unique index value.
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, enum logcode code, int f_out)
1060 static int missing_below = -1, excluded_below = -1;
1061 static const char *parent_dirname = "";
1062 static struct file_list *fuzzy_dirlist = NULL;
1063 static int need_fuzzy_dirlist = 0;
1064 struct file_struct *fuzzy_file = NULL;
1065 int fd = -1, f_copy = -1;
1067 STRUCT_STAT 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 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
1081 if (server_filter_list.head) {
1082 if (excluded_below >= 0) {
1083 if (F_DEPTH(file) > excluded_below)
1085 excluded_below = -1;
1087 if (check_filter(&server_filter_list, fname,
1088 S_ISDIR(file->mode)) < 0) {
1089 if (S_ISDIR(file->mode))
1090 excluded_below = F_DEPTH(file);
1094 "skipping server-excluded file \"%s\"\n",
1101 if (missing_below >= 0) {
1102 if (F_DEPTH(file) <= missing_below) {
1106 } else if (!dry_run) {
1107 if (S_ISDIR(file->mode))
1108 file->flags |= FLAG_MISSING_DIR;
1113 sx.acc_acl = sx.def_acl = NULL;
1116 if (fuzzy_dirlist) {
1117 flist_free(fuzzy_dirlist);
1118 fuzzy_dirlist = NULL;
1120 parent_dirname = "";
1122 stat_errno = ENOENT;
1124 const char *dn = file->dirname ? file->dirname : ".";
1125 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1126 if (relative_paths && !implied_dirs
1127 && do_stat(dn, &sx.st) < 0
1128 && create_directory_path(fname) < 0) {
1129 rsyserr(FERROR, errno,
1130 "recv_generator: mkdir %s failed",
1133 if (fuzzy_dirlist) {
1134 flist_free(fuzzy_dirlist);
1135 fuzzy_dirlist = NULL;
1138 need_fuzzy_dirlist = 1;
1140 if (!preserve_perms)
1141 dflt_perms = default_perms_for_dir(dn);
1144 parent_dirname = dn;
1146 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
1147 strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1148 fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
1149 need_fuzzy_dirlist = 0;
1152 statret = link_stat(fname, &sx.st,
1153 keep_dirlinks && S_ISDIR(file->mode));
1157 if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
1159 rprintf(FINFO, "not creating new %s \"%s\"\n",
1160 S_ISDIR(file->mode) ? "directory" : "file",
1163 if (S_ISDIR(file->mode)) {
1164 if (missing_below < 0) {
1167 missing_below = F_DEPTH(file);
1169 file->flags |= FLAG_MISSING_DIR;
1174 if (S_ISDIR(file->mode)) {
1175 /* The file to be received is a directory, so we need
1176 * to prepare appropriately. If there is already a
1177 * file of that name and it is *not* a directory, then
1178 * we need to delete it. If it doesn't exist, then
1179 * (perhaps recursively) create it. */
1180 if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1181 if (delete_item(fname, sx.st.st_mode, "directory", del_opts) != 0)
1185 if (dry_run && statret != 0 && missing_below < 0) {
1186 missing_below = F_DEPTH(file);
1192 if (*fname == '.' && fname[1] == '\0')
1196 if (!preserve_perms) { /* See comment in non-dir code below. */
1197 file->mode = dest_mode(file->mode, sx.st.st_mode,
1198 dflt_perms, statret == 0);
1200 if (statret != 0 && basis_dir[0] != NULL) {
1201 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1209 if (itemizing && f_out != -1) {
1210 itemize(fname, file, ndx, statret, &sx,
1211 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1213 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1214 if (!relative_paths || errno != ENOENT
1215 || create_directory_path(fname) < 0
1216 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
1217 rsyserr(FERROR, errno,
1218 "recv_generator: mkdir %s failed",
1221 "*** Skipping any contents from this failed directory ***\n");
1222 missing_below = F_DEPTH(file);
1223 file->flags |= FLAG_MISSING_DIR;
1227 if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, 0)
1228 && verbose && code != FNONE && f_out != -1)
1229 rprintf(code, "%s/\n", fname);
1230 if (real_ret != 0 && one_file_system)
1231 real_sx.st.st_dev = filesystem_dev;
1233 if (one_file_system) {
1234 uint32 *devp = F_DIRDEV_P(file);
1235 DEV_MAJOR(devp) = major(real_sx.st.st_dev);
1236 DEV_MINOR(devp) = minor(real_sx.st.st_dev);
1239 else if (delete_during && f_out != -1 && !phase && dry_run < 2
1240 && (file->flags & FLAG_XFER_DIR))
1241 delete_in_dir(cur_flist, fname, file, &real_sx.st.st_dev);
1245 /* If we're not preserving permissions, change the file-list's
1246 * mode based on the local permissions and some heuristics. */
1247 if (!preserve_perms) {
1248 int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
1249 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
1253 #ifdef SUPPORT_HARD_LINKS
1254 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1255 && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
1259 if (preserve_links && S_ISLNK(file->mode)) {
1260 #ifdef SUPPORT_LINKS
1261 const char *sl = F_SYMLINK(file);
1262 if (safe_symlinks && unsafe_symlink(sl, fname)) {
1265 fname = f_name(file, NULL);
1267 "ignoring unsafe symlink %s -> \"%s\"\n",
1268 full_fname(fname), sl);
1273 char lnk[MAXPATHLEN];
1276 if (!S_ISLNK(sx.st.st_mode))
1278 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1279 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1280 /* The link is pointing to the right place. */
1282 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1283 set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
1284 #ifdef SUPPORT_HARD_LINKS
1285 if (preserve_hard_links && F_IS_HLINKED(file))
1286 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1288 if (remove_source_files == 1)
1289 goto return_with_success;
1292 /* Not the right symlink (or not a symlink), so
1294 if (delete_item(fname, sx.st.st_mode, "symlink", del_opts) != 0)
1296 } else if (basis_dir[0] != NULL) {
1297 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1300 #ifndef CAN_HARDLINK_SYMLINK
1302 /* Resort to --copy-dest behavior. */
1312 #ifdef SUPPORT_HARD_LINKS
1313 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1316 if (do_symlink(sl, fname) != 0) {
1317 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
1318 full_fname(fname), sl);
1320 set_file_attrs(fname, file, NULL, 0);
1322 itemize(fname, file, ndx, statret, &sx,
1323 ITEM_LOCAL_CHANGE, 0, NULL);
1325 if (code != FNONE && verbose)
1326 rprintf(code, "%s -> %s\n", fname, sl);
1327 #ifdef SUPPORT_HARD_LINKS
1328 if (preserve_hard_links && F_IS_HLINKED(file))
1329 finish_hard_link(file, fname, NULL, itemizing, code, -1);
1331 /* This does not check remove_source_files == 1
1332 * because this is one of the items that the old
1333 * --remove-sent-files option would remove. */
1334 if (remove_source_files)
1335 goto return_with_success;
1341 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1342 || (preserve_specials && IS_SPECIAL(file->mode))) {
1343 uint32 *devp = F_RDEV_P(file);
1344 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1347 if (IS_DEVICE(file->mode)) {
1348 if (!IS_DEVICE(sx.st.st_mode))
1352 if (!IS_SPECIAL(sx.st.st_mode))
1357 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
1358 && sx.st.st_rdev == rdev) {
1359 /* The device or special file is identical. */
1361 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1362 set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
1363 #ifdef SUPPORT_HARD_LINKS
1364 if (preserve_hard_links && F_IS_HLINKED(file))
1365 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1367 if (remove_source_files == 1)
1368 goto return_with_success;
1371 if (delete_item(fname, sx.st.st_mode, t, del_opts) != 0)
1373 } else if (basis_dir[0] != NULL) {
1374 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1377 #ifndef CAN_HARDLINK_SPECIAL
1379 /* Resort to --copy-dest behavior. */
1389 #ifdef SUPPORT_HARD_LINKS
1390 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1394 rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1395 fname, (int)file->mode,
1396 (long)major(rdev), (long)minor(rdev));
1398 if (do_mknod(fname, file->mode, rdev) < 0) {
1399 rsyserr(FERROR, errno, "mknod %s failed",
1402 set_file_attrs(fname, file, NULL, 0);
1404 itemize(fname, file, ndx, statret, &sx,
1405 ITEM_LOCAL_CHANGE, 0, NULL);
1407 if (code != FNONE && verbose)
1408 rprintf(code, "%s\n", fname);
1409 #ifdef SUPPORT_HARD_LINKS
1410 if (preserve_hard_links && F_IS_HLINKED(file))
1411 finish_hard_link(file, fname, NULL, itemizing, code, -1);
1413 if (remove_source_files == 1)
1414 goto return_with_success;
1419 if (!S_ISREG(file->mode)) {
1421 fname = f_name(file, NULL);
1422 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1426 if (max_size > 0 && F_LENGTH(file) > max_size) {
1429 fname = f_name(file, NULL);
1430 rprintf(FINFO, "%s is over max-size\n", fname);
1434 if (min_size > 0 && F_LENGTH(file) < min_size) {
1437 fname = f_name(file, NULL);
1438 rprintf(FINFO, "%s is under min-size\n", fname);
1443 if (ignore_existing > 0 && statret == 0) {
1445 rprintf(FINFO, "%s exists\n", fname);
1449 if (update_only > 0 && statret == 0
1450 && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
1452 rprintf(FINFO, "%s is newer\n", fname);
1457 fnamecmp_type = FNAMECMP_FNAME;
1459 if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
1460 if (delete_item(fname, sx.st.st_mode, "regular file", del_opts) != 0)
1463 stat_errno = ENOENT;
1466 if (statret != 0 && basis_dir[0] != NULL) {
1467 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
1470 if (remove_source_files == 1)
1471 goto return_with_success;
1475 fnamecmp = fnamecmpbuf;
1484 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1485 && link_stat(partialptr, &partial_st, 0) == 0
1486 && S_ISREG(partial_st.st_mode)) {
1488 goto prepare_to_open;
1492 if (statret != 0 && fuzzy_dirlist && dry_run <= 1) {
1493 int j = find_fuzzy(file, fuzzy_dirlist);
1495 fuzzy_file = fuzzy_dirlist->files[j];
1496 f_name(fuzzy_file, fnamecmpbuf);
1498 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1499 fname, fnamecmpbuf);
1501 sx.st.st_size = F_LENGTH(fuzzy_file);
1503 fnamecmp = fnamecmpbuf;
1504 fnamecmp_type = FNAMECMP_FUZZY;
1509 #ifdef SUPPORT_HARD_LINKS
1510 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1513 if (stat_errno == ENOENT)
1515 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
1520 if (append_mode > 0 && sx.st.st_size > F_LENGTH(file))
1523 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1525 else if (fnamecmp_type == FNAMECMP_FUZZY)
1527 else if (unchanged_file(fnamecmp, file, &sx.st)) {
1529 do_unlink(partialptr);
1530 handle_partial_dir(partialptr, PDIR_DELETE);
1533 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
1534 set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
1535 #ifdef SUPPORT_HARD_LINKS
1536 if (preserve_hard_links && F_IS_HLINKED(file))
1537 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1539 if (remove_source_files != 1)
1541 return_with_success:
1543 send_msg_int(MSG_SUCCESS, ndx);
1550 fnamecmp = partialptr;
1551 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1555 if (!do_xfers || read_batch || whole_file)
1558 if (fuzzy_dirlist) {
1559 int j = flist_find(fuzzy_dirlist, file);
1560 if (j >= 0) /* don't use changing file as future fuzzy basis */
1561 fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
1565 fd = do_open(fnamecmp, O_RDONLY, 0);
1568 rsyserr(FERROR, errno, "failed to open %s, continuing",
1569 full_fname(fnamecmp));
1571 /* pretend the file didn't exist */
1572 #ifdef SUPPORT_HARD_LINKS
1573 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1576 statret = real_ret = -1;
1580 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1581 if (!(backupptr = get_backup_name(fname))) {
1585 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1587 goto pretend_missing;
1589 if (robust_unlink(backupptr) && errno != ENOENT) {
1590 rsyserr(FERROR, errno, "unlink %s",
1591 full_fname(backupptr));
1592 unmake_file(back_file);
1596 if ((f_copy = do_open(backupptr,
1597 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1598 rsyserr(FERROR, errno, "open %s",
1599 full_fname(backupptr));
1600 unmake_file(back_file);
1604 fnamecmp_type = FNAMECMP_BACKUP;
1608 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1609 fnamecmp, (double)sx.st.st_size);
1613 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1616 if (remove_source_files && !delay_updates && !phase)
1617 increment_active_files(ndx, itemizing, code);
1618 if (inc_recurse && !dry_run)
1619 cur_flist->in_progress++;
1620 #ifdef SUPPORT_HARD_LINKS
1621 if (preserve_hard_links && F_IS_HLINKED(file))
1622 file->flags |= FLAG_FILE_SENT;
1624 write_ndx(f_out, ndx);
1626 int iflags = ITEM_TRANSFER;
1627 if (always_checksum > 0)
1628 iflags |= ITEM_REPORT_CHECKSUM;
1629 if (fnamecmp_type != FNAMECMP_FNAME)
1630 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1631 if (fnamecmp_type == FNAMECMP_FUZZY)
1632 iflags |= ITEM_XNAME_FOLLOWS;
1633 itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
1634 fuzzy_file ? fuzzy_file->basename : NULL);
1642 #ifdef SUPPORT_HARD_LINKS
1643 if (preserve_hard_links && F_IS_HLINKED(file))
1644 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1651 if (statret != 0 || whole_file) {
1652 write_sum_head(f_out, NULL);
1656 generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy);
1660 set_file_attrs(backupptr, back_file, NULL, 0);
1662 rprintf(FINFO, "backed up %s to %s\n",
1665 unmake_file(back_file);
1678 static void touch_up_dirs(struct file_list *flist, int ndx)
1680 struct file_struct *file;
1682 int i, j, start, end;
1686 end = flist->count - 1;
1690 /* Fix any directory permissions that were modified during the
1691 * transfer and/or re-set any tweaked modified-time values. */
1692 for (i = start, j = 0; i <= end; i++) {
1693 file = flist->files[i];
1694 if (!F_IS_ACTIVE(file) || !S_ISDIR(file->mode)
1695 || file->flags & FLAG_MISSING_DIR)
1697 if (!need_retouch_dir_times && file->mode & S_IWUSR)
1699 fname = f_name(file, NULL);
1700 if (!(file->mode & S_IWUSR))
1701 do_chmod(fname, file->mode);
1702 if (need_retouch_dir_times)
1703 set_modtime(fname, file->modtime, file->mode);
1704 if (allowed_lull && !(++j % lull_mod))
1705 maybe_send_keepalive();
1706 else if (!(j % 200))
1707 maybe_flush_socket(0);
1711 void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
1713 struct file_struct *file;
1714 struct file_list *flist;
1715 char fbuf[MAXPATHLEN];
1718 #ifdef SUPPORT_HARD_LINKS
1719 while (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
1720 flist = flist_for_ndx(ndx);
1721 assert(flist != NULL);
1722 file = flist->files[ndx];
1723 assert(file->flags & FLAG_HLINKED);
1724 finish_hard_link(file, f_name(file, fbuf), NULL, itemizing, code, -1);
1728 while (check_redo && (ndx = get_redo_num()) != -1) {
1729 csum_length = SUM_LENGTH;
1730 max_size = -max_size;
1731 min_size = -min_size;
1732 ignore_existing = -ignore_existing;
1733 ignore_non_existing = -ignore_non_existing;
1734 update_only = -update_only;
1735 always_checksum = -always_checksum;
1736 size_only = -size_only;
1737 append_mode = -append_mode;
1738 make_backups = -make_backups; /* avoid dup backup w/inplace */
1742 cur_flist = flist_for_ndx(ndx);
1744 file = cur_flist->files[ndx - cur_flist->ndx_start];
1746 strlcpy(fbuf, solo_file, sizeof fbuf);
1749 recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
1750 cur_flist->to_redo--;
1754 csum_length = SHORT_SUM_LENGTH;
1755 max_size = -max_size;
1756 min_size = -min_size;
1757 ignore_existing = -ignore_existing;
1758 ignore_non_existing = -ignore_non_existing;
1759 update_only = -update_only;
1760 always_checksum = -always_checksum;
1761 size_only = -size_only;
1762 append_mode = -append_mode;
1763 make_backups = -make_backups;
1767 while (cur_flist != first_flist) { /* only possible with inc_recurse */
1768 if (first_flist->in_progress || first_flist->to_redo)
1772 write_ndx(sock_f_out, NDX_DONE);
1773 maybe_flush_socket(1);
1776 if (delete_during == 2 || !dir_tweaking) {
1777 /* Skip directory touch-up. */
1778 } else if (first_flist->ndx_start != 0)
1779 touch_up_dirs(dir_flist, first_flist->parent_ndx);
1780 else if (relative_paths && implied_dirs)
1781 touch_up_dirs(first_flist, -1);
1783 flist_free(first_flist); /* updates first_flist */
1787 void generate_files(int f_out, const char *local_name)
1790 char fbuf[MAXPATHLEN];
1793 int need_retouch_dir_perms = 0;
1794 int save_do_progress = do_progress;
1796 if (protocol_version >= 29) {
1798 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1799 code = logfile_format_has_i ? FNONE : FLOG;
1800 } else if (am_daemon) {
1801 itemizing = logfile_format_has_i && do_xfers;
1802 maybe_ATTRS_REPORT = ATTRS_REPORT;
1803 code = itemizing || !do_xfers ? FCLIENT : FINFO;
1804 } else if (!am_server) {
1805 itemizing = stdout_format_has_i;
1806 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1807 code = itemizing ? FNONE : FINFO;
1810 maybe_ATTRS_REPORT = ATTRS_REPORT;
1813 solo_file = local_name;
1814 dir_tweaking = !(list_only || solo_file || dry_run);
1815 need_retouch_dir_times = preserve_times && !omit_dir_times;
1816 lull_mod = allowed_lull * 5;
1819 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
1821 if (delete_before && !solo_file && cur_flist->count > 0)
1822 do_delete_pass(cur_flist);
1823 if (delete_during == 2) {
1824 deldelay_size = BIGPATHBUFLEN * 4;
1825 deldelay_buf = new_array(char, deldelay_size);
1827 out_of_memory("delete-delay");
1831 if (append_mode > 0 || whole_file < 0)
1834 rprintf(FINFO, "delta-transmission %s\n",
1836 ? "disabled for local transfer or --whole-file"
1840 /* Since we often fill up the outgoing socket and then just sit around
1841 * waiting for the other 2 processes to do their thing, we don't want
1842 * to exit on a timeout. If the data stops flowing, the receiver will
1843 * notice that and let us know via the redo pipe (or its closing). */
1846 dflt_perms = (ACCESSPERMS & ~orig_umask);
1849 if (inc_recurse && delete_during && cur_flist->ndx_start) {
1850 struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
1851 if (BITS_SETnUNSET(fp->flags, FLAG_XFER_DIR, FLAG_MISSING_DIR)) {
1853 if (one_file_system) {
1854 uint32 *devp = F_DIRDEV_P(fp);
1855 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1857 dirdev = MAKEDEV(0, 0);
1858 delete_in_dir(cur_flist, f_name(fp, fbuf), fp, &dirdev);
1861 for (i = cur_flist->low; i <= cur_flist->high; i++) {
1862 struct file_struct *file = cur_flist->files[i];
1864 if (!F_IS_ACTIVE(file))
1868 strlcpy(fbuf, solo_file, sizeof fbuf);
1871 recv_generator(fbuf, file, i + cur_flist->ndx_start,
1872 itemizing, code, f_out);
1874 /* We need to ensure that any dirs we create have
1875 * writeable permissions during the time we are putting
1876 * files within them. This is then fixed after the
1877 * transfer is done. */
1879 if (!am_root && S_ISDIR(file->mode)
1880 && !(file->mode & S_IWUSR) && dir_tweaking) {
1881 mode_t mode = file->mode | S_IWUSR;
1882 const char *fname = solo_file ? solo_file : fbuf;
1883 if (do_chmod(fname, mode) < 0) {
1884 rsyserr(FERROR, errno,
1885 "failed to modify permissions on %s",
1888 need_retouch_dir_perms = 1;
1892 check_for_finished_files(itemizing, code, 0);
1894 if (allowed_lull && !(i % lull_mod))
1895 maybe_send_keepalive();
1896 else if (!(i % 200))
1897 maybe_flush_socket(0);
1901 write_ndx(f_out, NDX_DONE);
1906 check_for_finished_files(itemizing, code, 1);
1907 if (cur_flist->next || flist_eof)
1909 wait_for_receiver();
1911 } while ((cur_flist = cur_flist->next) != NULL);
1914 delete_in_dir(NULL, NULL, NULL, &dev_zero);
1917 rprintf(FINFO, "generate_files phase=%d\n", phase);
1920 check_for_finished_files(itemizing, code, 1);
1923 wait_for_receiver();
1928 rprintf(FINFO, "generate_files phase=%d\n", phase);
1930 write_ndx(f_out, NDX_DONE);
1931 /* Reduce round-trip lag-time for a useless delay-updates phase. */
1932 if (protocol_version >= 29 && !delay_updates)
1933 write_ndx(f_out, NDX_DONE);
1935 /* Read MSG_DONE for the redo phase (and any prior messages). */
1937 check_for_finished_files(itemizing, code, 0);
1938 if (msgdone_cnt > 1)
1940 wait_for_receiver();
1943 if (protocol_version >= 29) {
1946 rprintf(FINFO, "generate_files phase=%d\n", phase);
1948 write_ndx(f_out, NDX_DONE);
1949 /* Read MSG_DONE for delay-updates phase & prior messages. */
1950 while (msgdone_cnt == 2)
1951 wait_for_receiver();
1954 do_progress = save_do_progress;
1955 if (delete_during == 2)
1956 do_delayed_deletions(fbuf);
1957 if (delete_after && !solo_file && file_total > 0)
1958 do_delete_pass(cur_flist);
1960 if ((need_retouch_dir_perms || need_retouch_dir_times)
1961 && dir_tweaking && (!inc_recurse || delete_during == 2))
1962 touch_up_dirs(inc_recurse ? dir_flist : cur_flist, -1);
1964 if (max_delete >= 0 && deletion_count > max_delete) {
1966 "Deletions stopped due to --max-delete limit (%d skipped)\n",
1967 deletion_count - max_delete);
1968 io_error |= IOERR_DEL_LIMIT;
1972 rprintf(FINFO, "generate_files finished\n");