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-2008 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 3 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, visit the http://fsf.org website.
28 extern int stdout_format_has_i;
29 extern int logfile_format_has_i;
33 extern int inc_recurse;
34 extern int do_progress;
35 extern int relative_paths;
36 extern int implied_dirs;
37 extern int keep_dirlinks;
38 extern int preserve_acls;
39 extern int preserve_xattrs;
40 extern int preserve_links;
41 extern int preserve_devices;
42 extern int preserve_specials;
43 extern int preserve_hard_links;
44 extern int preserve_executability;
45 extern int preserve_perms;
46 extern int preserve_times;
49 extern int delete_mode;
50 extern int delete_before;
51 extern int delete_during;
52 extern int delete_after;
53 extern int msgdone_cnt;
54 extern int ignore_errors;
55 extern int remove_source_files;
56 extern int delay_updates;
57 extern int update_only;
58 extern int ignore_existing;
59 extern int ignore_non_existing;
61 extern int append_mode;
62 extern int make_backups;
63 extern int csum_length;
64 extern int ignore_times;
66 extern OFF_T max_size;
67 extern OFF_T min_size;
70 extern int allowed_lull;
71 extern int sock_f_out;
72 extern int ignore_timeout;
73 extern int protocol_version;
74 extern int file_total;
75 extern int fuzzy_basis;
76 extern int always_checksum;
77 extern int checksum_len;
78 extern char *partial_dir;
79 extern char *basis_dir[];
80 extern int compare_dest;
83 extern int whole_file;
85 extern int read_batch;
86 extern int safe_symlinks;
87 extern long block_size; /* "long" because popt can't set an int32. */
88 extern int unsort_ndx;
89 extern int max_delete;
90 extern int force_delete;
91 extern int one_file_system;
92 extern struct stats stats;
93 extern dev_t filesystem_dev;
94 extern mode_t orig_umask;
96 extern char *backup_dir;
97 extern char *backup_suffix;
98 extern int backup_suffix_len;
99 extern struct file_list *cur_flist, *first_flist, *dir_flist;
100 extern struct filter_list_struct server_filter_list;
102 int ignore_perishable = 0;
103 int non_perishable_cnt = 0;
104 int maybe_ATTRS_REPORT = 0;
106 static dev_t dev_zero;
107 static int deletion_count = 0; /* used to implement --max-delete */
108 static int deldelay_size = 0, deldelay_cnt = 0;
109 static char *deldelay_buf = NULL;
110 static int deldelay_fd = -1;
112 static int dir_tweaking;
113 static int symlink_timeset_failed_flags;
114 static int need_retouch_dir_times;
115 static int need_retouch_dir_perms;
116 static const char *solo_file = NULL;
118 /* For calling delete_item() and delete_dir_contents(). */
119 #define DEL_NO_UID_WRITE (1<<0) /* file/dir has our uid w/o write perm */
120 #define DEL_RECURSE (1<<1) /* if dir, delete all contents */
121 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
122 #define DEL_FOR_FILE (1<<3) /* making room for a replacement file */
123 #define DEL_FOR_DIR (1<<4) /* making room for a replacement dir */
124 #define DEL_FOR_SYMLINK (1<<5) /* making room for a replacement symlink */
125 #define DEL_FOR_DEVICE (1<<6) /* making room for a replacement device */
126 #define DEL_FOR_SPECIAL (1<<7) /* making room for a replacement special */
128 #define DEL_MAKE_ROOM (DEL_FOR_FILE|DEL_FOR_DIR|DEL_FOR_SYMLINK|DEL_FOR_DEVICE|DEL_FOR_SPECIAL)
131 TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
135 DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_NOT_EMPTY
138 /* Forward declaration for delete_item(). */
139 static enum delret delete_dir_contents(char *fname, uint16 flags);
141 static int is_backup_file(char *fn)
143 int k = strlen(fn) - backup_suffix_len;
144 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
147 /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
148 * delete recursively.
150 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
151 * a directory! (The buffer is used for recursion, but returned unchanged.)
153 static enum delret delete_item(char *fbuf, uint16 mode, uint16 flags)
160 rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
161 fbuf, (int)mode, (int)flags);
164 if (flags & DEL_NO_UID_WRITE)
165 do_chmod(fbuf, mode | S_IWUSR);
167 if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
168 int save_uid_ndx = uid_ndx;
169 /* This only happens on the first call to delete_item() since
170 * delete_dir_contents() always calls us w/DEL_DIR_IS_EMPTY. */
172 uid_ndx = ++file_extra_cnt;
173 ignore_perishable = 1;
174 /* If DEL_RECURSE is not set, this just reports emptiness. */
175 ret = delete_dir_contents(fbuf, flags);
176 ignore_perishable = 0;
181 if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT)
183 /* OK: try to delete the directory. */
186 if (!(flags & DEL_MAKE_ROOM) && max_delete >= 0 && ++deletion_count > max_delete)
191 ok = do_rmdir(fbuf) == 0;
192 } else if (make_backups > 0 && (backup_dir || !is_backup_file(fbuf))) {
193 what = "make_backup";
194 ok = make_backup(fbuf);
197 ok = robust_unlink(fbuf) == 0;
201 if (!(flags & DEL_MAKE_ROOM))
202 log_delete(fbuf, mode);
205 if (S_ISDIR(mode) && errno == ENOTEMPTY) {
206 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
209 } else if (errno != ENOENT) {
210 rsyserr(FERROR, errno, "delete_file: %s(%s) failed",
220 if (ret != DR_SUCCESS && flags & DEL_MAKE_ROOM) {
222 switch (flags & DEL_MAKE_ROOM) {
223 case DEL_FOR_FILE: desc = "regular file"; break;
224 case DEL_FOR_DIR: desc = "directory"; break;
225 case DEL_FOR_SYMLINK: desc = "symlink"; break;
226 case DEL_FOR_DEVICE: desc = "device file"; break;
227 case DEL_FOR_SPECIAL: desc = "special file"; break;
228 default: exit_cleanup(RERR_UNSUPPORTED); /* IMPOSSIBLE */
230 rprintf(FERROR_XFER, "could not make way for new %s: %s\n",
236 /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
237 * its contents, otherwise just checks for content. Returns DR_SUCCESS or
238 * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
239 * buffer is used for recursion, but returned unchanged.)
241 static enum delret delete_dir_contents(char *fname, uint16 flags)
243 struct file_list *dirlist;
251 rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n",
255 dlen = strlen(fname);
256 save_filters = push_local_filters(fname, dlen);
258 non_perishable_cnt = 0;
259 dirlist = get_dirlist(fname, dlen, 0);
260 ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
265 if (!(flags & DEL_RECURSE)) {
271 if (dlen != 1 || *fname != '/')
273 remainder = MAXPATHLEN - (p - fname);
275 /* We do our own recursion, so make delete_item() non-recursive. */
276 flags = (flags & ~(DEL_RECURSE|DEL_MAKE_ROOM|DEL_NO_UID_WRITE))
279 for (j = dirlist->used; j--; ) {
280 struct file_struct *fp = dirlist->files[j];
282 if (fp->flags & FLAG_MOUNT_DIR) {
285 "mount point, %s, pins parent directory\n",
292 strlcpy(p, fp->basename, remainder);
293 if (!(fp->mode & S_IWUSR) && !am_root && (uid_t)F_OWNER(fp) == our_uid)
294 do_chmod(fname, fp->mode | S_IWUSR);
295 /* Save stack by recursing to ourself directly. */
296 if (S_ISDIR(fp->mode)) {
297 if (delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
300 if (delete_item(fname, fp->mode, flags) != DR_SUCCESS)
308 pop_local_filters(save_filters);
310 if (ret == DR_NOT_EMPTY) {
311 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
317 static int start_delete_delay_temp(void)
319 char fnametmp[MAXPATHLEN];
320 int save_dry_run = dry_run;
323 if (!get_tmpname(fnametmp, "deldelay")
324 || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
325 rprintf(FINFO, "NOTE: Unable to create delete-delay temp file%s.\n",
326 inc_recurse ? "" : " -- switching to --delete-after");
328 delete_after = !inc_recurse;
329 dry_run = save_dry_run;
333 dry_run = save_dry_run;
337 static int flush_delete_delay(void)
339 if (deldelay_fd < 0 && !start_delete_delay_temp())
341 if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
342 rsyserr(FERROR, errno, "flush of delete-delay buffer");
344 delete_after = !inc_recurse;
352 static int remember_delete(struct file_struct *file, const char *fname, int flags)
356 if (deldelay_cnt == deldelay_size && !flush_delete_delay())
359 if (flags & DEL_NO_UID_WRITE)
360 deldelay_buf[deldelay_cnt++] = '!';
363 len = snprintf(deldelay_buf + deldelay_cnt,
364 deldelay_size - deldelay_cnt,
366 (int)file->mode, fname, '\0');
367 if ((deldelay_cnt += len) <= deldelay_size)
370 if (!flush_delete_delay())
377 static int read_delay_line(char *buf, int *flags_p)
379 static int read_pos = 0;
381 char *bp, *past_space;
384 for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
385 if (j < deldelay_cnt)
387 if (deldelay_fd < 0) {
392 deldelay_cnt -= read_pos;
393 if (deldelay_cnt == deldelay_size)
395 if (deldelay_cnt && read_pos) {
396 memmove(deldelay_buf, deldelay_buf + read_pos,
399 len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
400 deldelay_size - deldelay_cnt);
404 "ERROR: unexpected EOF in delete-delay file.\n");
409 rsyserr(FERROR, errno,
410 "reading delete-delay file");
417 bp = deldelay_buf + read_pos;
420 *flags_p = DEL_NO_UID_WRITE;
424 if (sscanf(bp, "%x ", &mode) != 1) {
426 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
429 past_space = strchr(bp, ' ') + 1;
430 len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
433 if (len > MAXPATHLEN) {
434 rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
438 /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
439 * instead of returning a pointer to our buffer. */
440 memcpy(buf, past_space, len);
445 static void do_delayed_deletions(char *delbuf)
449 if (deldelay_fd >= 0) {
450 if (deldelay_cnt && !flush_delete_delay())
452 lseek(deldelay_fd, 0, 0);
454 while ((mode = read_delay_line(delbuf, &flags)) >= 0)
455 delete_item(delbuf, mode, flags | DEL_RECURSE);
456 if (deldelay_fd >= 0)
460 /* This function is used to implement per-directory deletion, and is used by
461 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
462 * MAXPATHLEN buffer with the name of the directory in it (the functions we
463 * call will append names onto the end, but the old dir value will be restored
465 static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
467 static int already_warned = 0;
468 struct file_list *dirlist;
469 char delbuf[MAXPATHLEN];
471 int save_uid_ndx = uid_ndx;
474 change_local_filter_dir(NULL, 0, 0);
479 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
482 maybe_send_keepalive();
484 if (io_error && !ignore_errors) {
488 "IO error encountered -- skipping file deletion\n");
494 change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
496 if (one_file_system) {
497 if (file->flags & FLAG_TOP_DIR)
498 filesystem_dev = *fs_dev;
499 else if (filesystem_dev != *fs_dev)
504 uid_ndx = ++file_extra_cnt;
506 dirlist = get_dirlist(fbuf, dlen, 0);
508 /* If an item in dirlist is not found in flist, delete it
509 * from the filesystem. */
510 for (i = dirlist->used; i--; ) {
511 struct file_struct *fp = dirlist->files[i];
512 if (!F_IS_ACTIVE(fp))
514 if (fp->flags & FLAG_MOUNT_DIR) {
516 rprintf(FINFO, "cannot delete mount point: %s\n",
520 if (flist_find(cur_flist, fp) < 0) {
521 int flags = DEL_RECURSE;
522 if (!(fp->mode & S_IWUSR) && !am_root && (uid_t)F_OWNER(fp) == our_uid)
523 flags |= DEL_NO_UID_WRITE;
525 if (delete_during == 2) {
526 if (!remember_delete(fp, delbuf, flags))
529 delete_item(delbuf, fp->mode, flags);
541 /* This deletes any files on the receiving side that are not present on the
542 * sending side. This is used by --delete-before and --delete-after. */
543 static void do_delete_pass(void)
545 char fbuf[MAXPATHLEN];
549 /* dry_run is incremented when the destination doesn't exist yet. */
550 if (dry_run > 1 || list_only)
553 for (j = 0; j < cur_flist->used; j++) {
554 struct file_struct *file = cur_flist->sorted[j];
556 if (!(file->flags & FLAG_CONTENT_DIR))
560 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
561 rprintf(FINFO, "deleting in %s\n", fbuf);
563 if (link_stat(fbuf, &st, keep_dirlinks) < 0
564 || !S_ISDIR(st.st_mode))
567 delete_in_dir(fbuf, file, &st.st_dev);
569 delete_in_dir(NULL, NULL, &dev_zero);
571 if (do_progress && !am_server)
572 rprintf(FINFO, " \r");
575 int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
577 #if !defined HAVE_LUTIMES || !defined HAVE_UTIMES
578 if (S_ISLNK(file->mode)) {
582 if (preserve_times && cmp_time(sxp->st.st_mtime, file->modtime) != 0)
585 if (preserve_perms) {
586 if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
588 } else if (preserve_executability
589 && ((sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0)))
592 if (am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file))
595 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
599 if (preserve_acls && !S_ISLNK(file->mode)) {
600 if (!ACL_READY(*sxp))
602 if (set_acl(NULL, file, sxp) == 0)
606 #ifdef SUPPORT_XATTRS
607 if (preserve_xattrs) {
608 if (!XATTR_READY(*sxp))
609 get_xattr(fname, sxp);
610 if (xattr_diff(file, sxp, 0))
618 void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
619 stat_x *sxp, int32 iflags, uchar fnamecmp_type,
622 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
623 int keep_time = !preserve_times ? 0
624 : S_ISDIR(file->mode) ? preserve_times > 1 :
625 #if defined HAVE_LUTIMES && defined HAVE_UTIMES
628 !S_ISLNK(file->mode);
631 if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
632 iflags |= ITEM_REPORT_SIZE;
633 if (file->flags & FLAG_TIME_FAILED) { /* symlinks only */
634 if (iflags & ITEM_LOCAL_CHANGE)
635 iflags |= symlink_timeset_failed_flags;
637 ? cmp_time(file->modtime, sxp->st.st_mtime) != 0
638 : iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !(iflags & ITEM_MATCHED)
639 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
640 iflags |= ITEM_REPORT_TIME;
641 #if !defined HAVE_LCHMOD && !defined HAVE_SETATTRLIST
642 if (S_ISLNK(file->mode)) {
646 if (preserve_perms) {
647 if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
648 iflags |= ITEM_REPORT_PERMS;
649 } else if (preserve_executability
650 && ((sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0)))
651 iflags |= ITEM_REPORT_PERMS;
652 if (uid_ndx && am_root && (uid_t)F_OWNER(file) != sxp->st.st_uid)
653 iflags |= ITEM_REPORT_OWNER;
654 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
655 && sxp->st.st_gid != (gid_t)F_GROUP(file))
656 iflags |= ITEM_REPORT_GROUP;
658 if (preserve_acls && !S_ISLNK(file->mode)) {
659 if (!ACL_READY(*sxp))
660 get_acl(fnamecmp, sxp);
661 if (set_acl(NULL, file, sxp) == 0)
662 iflags |= ITEM_REPORT_ACL;
665 #ifdef SUPPORT_XATTRS
666 if (preserve_xattrs) {
667 if (!XATTR_READY(*sxp))
668 get_xattr(fnamecmp, sxp);
669 if (xattr_diff(file, sxp, 1))
670 iflags |= ITEM_REPORT_XATTR;
674 #ifdef SUPPORT_XATTRS
675 if (preserve_xattrs && xattr_diff(file, NULL, 1))
676 iflags |= ITEM_REPORT_XATTR;
678 iflags |= ITEM_IS_NEW;
682 if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || verbose > 1
683 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
684 if (protocol_version >= 29) {
686 write_ndx(sock_f_out, ndx);
687 write_shortint(sock_f_out, iflags);
688 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
689 write_byte(sock_f_out, fnamecmp_type);
690 if (iflags & ITEM_XNAME_FOLLOWS)
691 write_vstring(sock_f_out, xname, strlen(xname));
692 #ifdef SUPPORT_XATTRS
693 if (iflags & ITEM_REPORT_XATTR && !dry_run)
694 send_xattr_request(NULL, file, sock_f_out);
696 } else if (ndx >= 0) {
697 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
698 log_item(code, file, &stats, iflags, xname);
704 /* Perform our quick-check heuristic for determining if a file is unchanged. */
705 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
707 if (st->st_size != F_LENGTH(file))
710 /* if always checksum is set then we use the checksum instead
711 of the file time to determine whether to sync */
712 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
713 char sum[MAX_DIGEST_LEN];
714 file_checksum(fn, sum, st->st_size);
715 return memcmp(sum, F_SUM(file), checksum_len) == 0;
724 return cmp_time(st->st_mtime, file->modtime) == 0;
729 * set (initialize) the size entries in the per-file sum_struct
730 * calculating dynamic block and checksum sizes.
732 * This is only called from generate_and_send_sums() but is a separate
733 * function to encapsulate the logic.
735 * The block size is a rounded square root of file length.
737 * The checksum size is determined according to:
738 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
739 * provided by Donovan Baarda which gives a probability of rsync
740 * algorithm corrupting data and falling back using the whole md4
743 * This might be made one of several selectable heuristics.
745 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
751 blength = block_size;
752 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
753 blength = BLOCK_SIZE;
758 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
759 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
760 blength = MAX_BLOCK_SIZE;
765 if (len < (int64)blength * blength)
768 } while (c >= 8); /* round to multiple of 8 */
769 blength = MAX(blength, BLOCK_SIZE);
773 if (protocol_version < 27) {
774 s2length = csum_length;
775 } else if (csum_length == SUM_LENGTH) {
776 s2length = SUM_LENGTH;
780 int b = BLOCKSUM_BIAS;
781 for (l = len; l >>= 1; b += 2) {}
782 for (c = blength; (c >>= 1) && b; b--) {}
783 /* add a bit, subtract rollsum, round up. */
784 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
785 s2length = MAX(s2length, csum_length);
786 s2length = MIN(s2length, SUM_LENGTH);
790 sum->blength = blength;
791 sum->s2length = s2length;
792 sum->remainder = (int32)(len % blength);
793 sum->count = (int32)(len / blength) + (sum->remainder != 0);
795 if (sum->count && verbose > 2) {
797 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
798 (double)sum->count, (long)sum->remainder, (long)sum->blength,
799 sum->s2length, (double)sum->flength);
805 * Generate and send a stream of signatures/checksums that describe a buffer
807 * Generate approximately one checksum every block_len bytes.
809 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
812 struct map_struct *mapbuf;
813 struct sum_struct sum;
816 sum_sizes_sqroot(&sum, len);
817 write_sum_head(f_out, &sum);
819 if (append_mode > 0 && f_copy < 0)
823 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
827 for (i = 0; i < sum.count; i++) {
828 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
829 char *map = map_ptr(mapbuf, offset, n1);
830 char sum2[SUM_LENGTH];
837 full_write(f_copy, map, n1);
842 sum1 = get_checksum1(map, n1);
843 get_checksum2(map, n1, sum2);
847 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
848 (double)i, (double)offset - n1, (long)n1,
849 (unsigned long)sum1);
851 write_int(f_out, sum1);
852 write_buf(f_out, sum2, sum.s2length);
860 /* Try to find a filename in the same dir as "fname" with a similar name. */
861 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
863 int fname_len, fname_suf_len;
864 const char *fname_suf, *fname = file->basename;
865 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
866 int j, lowest_j = -1;
868 fname_len = strlen(fname);
869 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
871 for (j = 0; j < dirlist->used; j++) {
872 struct file_struct *fp = dirlist->files[j];
873 const char *suf, *name;
877 if (!S_ISREG(fp->mode) || !F_LENGTH(fp)
878 || fp->flags & FLAG_FILE_SENT)
883 if (F_LENGTH(fp) == F_LENGTH(file)
884 && cmp_time(fp->modtime, file->modtime) == 0) {
887 "fuzzy size/modtime match for %s\n",
894 suf = find_filename_suffix(name, len, &suf_len);
896 dist = fuzzy_distance(name, len, fname, fname_len);
897 /* Add some extra weight to how well the suffixes match. */
898 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
901 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
902 name, (int)(dist>>16), (int)(dist&0xFFFF));
904 if (dist <= lowest_dist) {
913 /* Copy a file found in our --copy-dest handling. */
914 static int copy_altdest_file(const char *src, const char *dest, struct file_struct *file)
916 char buf[MAXPATHLEN];
917 const char *copy_to, *partialptr;
921 /* Let copy_file open the destination in place. */
925 fd_w = open_tmpfile(buf, dest, file);
930 cleanup_set(copy_to, NULL, NULL, -1, -1);
931 if (copy_file(src, copy_to, fd_w, file->mode, 0) < 0) {
933 rsyserr(FINFO, errno, "copy_file %s => %s",
934 full_fname(src), copy_to);
936 /* Try to clean up. */
941 partialptr = partial_dir ? partial_dir_fname(dest) : NULL;
942 ok = finish_transfer(dest, copy_to, src, partialptr, file, 1, 0);
947 /* This is only called for regular files. We return -2 if we've finished
948 * handling the file, -1 if no dest-linking occurred, or a non-negative
949 * value if we found an alternate basis file. */
950 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
951 char *cmpbuf, stat_x *sxp, int itemizing,
959 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
960 if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
962 switch (match_level) {
968 if (!unchanged_file(cmpbuf, file, &sxp->st))
974 if (!unchanged_attrs(cmpbuf, file, sxp))
981 } while (basis_dir[++j] != NULL);
986 if (j != best_match) {
988 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
989 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
993 if (match_level == 3 && !copy_dest) {
994 #ifdef SUPPORT_HARD_LINKS
996 if (!hard_link_one(file, fname, cmpbuf, 1))
998 if (preserve_hard_links && F_IS_HLINKED(file))
999 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, j);
1000 if (!maybe_ATTRS_REPORT && (verbose > 1 || stdout_format_has_i > 1)) {
1001 itemize(cmpbuf, file, ndx, 1, sxp,
1002 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
1008 itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
1009 if (verbose > 1 && maybe_ATTRS_REPORT)
1010 rprintf(FCLIENT, "%s is uptodate\n", fname);
1014 if (match_level >= 2) {
1015 #ifdef SUPPORT_HARD_LINKS
1016 try_a_copy: /* Copy the file locally. */
1018 if (!dry_run && copy_altdest_file(cmpbuf, fname, file) < 0)
1021 itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
1022 if (maybe_ATTRS_REPORT
1023 && ((!itemizing && verbose && match_level == 2)
1024 || (verbose > 1 && match_level == 3))) {
1025 code = match_level == 3 ? FCLIENT : FINFO;
1026 rprintf(code, "%s%s\n", fname,
1027 match_level == 3 ? " is uptodate" : "");
1029 #ifdef SUPPORT_HARD_LINKS
1030 if (preserve_hard_links && F_IS_HLINKED(file))
1031 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, -1);
1036 return FNAMECMP_BASIS_DIR_LOW + j;
1039 /* This is only called for non-regular files. We return -2 if we've finished
1040 * handling the file, or -1 if no dest-linking occurred, or a non-negative
1041 * value if we found an alternate basis file. */
1042 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
1043 char *cmpbuf, stat_x *sxp, int itemizing,
1046 char lnk[MAXPATHLEN];
1047 int best_match = -1;
1048 int match_level = 0;
1049 enum nonregtype type;
1053 #ifndef SUPPORT_LINKS
1054 if (S_ISLNK(file->mode))
1057 if (S_ISDIR(file->mode)) {
1059 } else if (IS_SPECIAL(file->mode))
1060 type = TYPE_SPECIAL;
1061 else if (IS_DEVICE(file->mode))
1063 #ifdef SUPPORT_LINKS
1064 else if (S_ISLNK(file->mode))
1065 type = TYPE_SYMLINK;
1069 "internal: try_dests_non() called with invalid mode (%o)\n",
1071 exit_cleanup(RERR_UNSUPPORTED);
1075 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1076 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1080 if (!S_ISDIR(sxp->st.st_mode))
1084 if (!IS_SPECIAL(sxp->st.st_mode))
1088 if (!IS_DEVICE(sxp->st.st_mode))
1091 #ifdef SUPPORT_LINKS
1093 if (!S_ISLNK(sxp->st.st_mode))
1098 if (match_level < 1) {
1107 devp = F_RDEV_P(file);
1108 if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
1111 #ifdef SUPPORT_LINKS
1113 if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
1116 if (strcmp(lnk, F_SYMLINK(file)) != 0)
1121 if (match_level < 2) {
1125 if (unchanged_attrs(cmpbuf, file, sxp)) {
1130 } while (basis_dir[++j] != NULL);
1135 if (j != best_match) {
1137 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1138 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1142 if (match_level == 3) {
1143 #ifdef SUPPORT_HARD_LINKS
1145 #ifndef CAN_HARDLINK_SYMLINK
1146 && !S_ISLNK(file->mode)
1148 #ifndef CAN_HARDLINK_SPECIAL
1149 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
1151 && !S_ISDIR(file->mode)) {
1152 if (do_link(cmpbuf, fname) < 0) {
1153 rsyserr(FERROR_XFER, errno,
1154 "failed to hard-link %s with %s",
1158 if (preserve_hard_links && F_IS_HLINKED(file))
1159 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1163 if (itemizing && stdout_format_has_i
1164 && (verbose > 1 || stdout_format_has_i > 1)) {
1165 int chg = compare_dest && type != TYPE_DIR ? 0
1166 : ITEM_LOCAL_CHANGE + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1167 char *lp = match_level == 3 ? "" : NULL;
1168 itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
1170 if (verbose > 1 && maybe_ATTRS_REPORT) {
1171 rprintf(FCLIENT, "%s%s is uptodate\n",
1172 fname, type == TYPE_DIR ? "/" : "");
1180 static void list_file_entry(struct file_struct *f)
1182 char permbuf[PERMSTRING_SIZE];
1185 if (!F_IS_ACTIVE(f)) {
1186 /* this can happen if duplicate names were removed */
1190 permstring(permbuf, f->mode);
1193 /* TODO: indicate '+' if the entry has an ACL. */
1195 #ifdef SUPPORT_LINKS
1196 if (preserve_links && S_ISLNK(f->mode)) {
1197 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
1198 permbuf, len, timestring(f->modtime),
1199 f_name(f, NULL), F_SYMLINK(f));
1203 rprintf(FINFO, "%s %11.0f %s %s\n",
1204 permbuf, len, timestring(f->modtime),
1209 static int phase = 0;
1210 static int dflt_perms;
1212 /* Acts on the indicated item in cur_flist whose name is fname. If a dir,
1213 * make sure it exists, and has the right permissions/timestamp info. For
1214 * all other non-regular files (symlinks, etc.) we create them here. For
1215 * regular files that have changed, we try to find a basis file and then
1216 * start sending checksums. The ndx is the file's unique index value.
1218 * When fname is non-null, it must point to a MAXPATHLEN buffer!
1220 * Note that f_out is set to -1 when doing final directory-permission and
1221 * modification-time repair. */
1222 static void recv_generator(char *fname, struct file_struct *file, int ndx,
1223 int itemizing, enum logcode code, int f_out)
1225 static int missing_below = -1, excluded_below = -1;
1226 static const char *parent_dirname = "";
1227 static struct file_struct *missing_dir = NULL, *excluded_dir = NULL;
1228 static struct file_list *fuzzy_dirlist = NULL;
1229 static int need_fuzzy_dirlist = 0;
1230 struct file_struct *fuzzy_file = NULL;
1231 int fd = -1, f_copy = -1;
1233 STRUCT_STAT partial_st;
1234 struct file_struct *back_file = NULL;
1235 int statret, real_ret, stat_errno;
1236 char *fnamecmp, *partialptr, *backupptr = NULL;
1237 char fnamecmpbuf[MAXPATHLEN];
1238 uchar fnamecmp_type;
1239 int implied_dirs_are_missing = relative_paths && !implied_dirs && protocol_version < 30;
1240 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1241 int is_dir = !S_ISDIR(file->mode) ? 0
1242 : inc_recurse && ndx != cur_flist->ndx_start - 1 ? -1
1246 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
1250 || (is_dir && !implied_dirs && file->flags & FLAG_IMPLIED_DIR))
1252 list_file_entry(file);
1256 if (server_filter_list.head) {
1257 int filtered = check_filter(&server_filter_list, fname, is_dir) < 0;
1258 if (is_dir < 0 && filtered)
1260 if (excluded_below >= 0) {
1261 if (F_DEPTH(file) > excluded_below
1262 && (!implied_dirs_are_missing || f_name_has_prefix(file, excluded_dir)))
1264 excluded_below = -1;
1268 excluded_below = F_DEPTH(file);
1269 excluded_dir = file;
1272 rprintf(FERROR_XFER,
1273 "skipping daemon-excluded file \"%s\"\n",
1279 if (missing_below >= 0) {
1280 if (F_DEPTH(file) <= missing_below
1281 || (implied_dirs_are_missing && !f_name_has_prefix(file, missing_dir))) {
1285 } else if (!dry_run) {
1287 file->flags |= FLAG_MISSING_DIR;
1292 sx.acc_acl = sx.def_acl = NULL;
1294 #ifdef SUPPORT_XATTRS
1298 if (fuzzy_dirlist) {
1299 flist_free(fuzzy_dirlist);
1300 fuzzy_dirlist = NULL;
1302 parent_dirname = "";
1304 stat_errno = ENOENT;
1306 const char *dn = file->dirname ? file->dirname : ".";
1307 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1308 if (relative_paths && !implied_dirs
1309 && do_stat(dn, &sx.st) < 0
1310 && create_directory_path(fname) < 0) {
1311 rsyserr(FERROR_XFER, errno,
1312 "recv_generator: mkdir %s failed",
1315 if (fuzzy_dirlist) {
1316 flist_free(fuzzy_dirlist);
1317 fuzzy_dirlist = NULL;
1320 need_fuzzy_dirlist = 1;
1322 if (!preserve_perms)
1323 dflt_perms = default_perms_for_dir(dn);
1326 parent_dirname = dn;
1328 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
1329 strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1330 fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
1331 need_fuzzy_dirlist = 0;
1334 statret = link_stat(fname, &sx.st, keep_dirlinks && is_dir);
1338 if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
1342 if (missing_below < 0) {
1345 missing_below = F_DEPTH(file);
1348 file->flags |= FLAG_MISSING_DIR;
1351 rprintf(FINFO, "not creating new %s \"%s\"\n",
1352 is_dir ? "directory" : "file", fname);
1357 if (statret == 0 && !(sx.st.st_mode & S_IWUSR)
1358 && !am_root && sx.st.st_uid == our_uid)
1359 del_opts |= DEL_NO_UID_WRITE;
1362 if (!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
1365 /* In inc_recurse mode we want to make sure any missing
1366 * directories get created while we're still processing
1367 * the parent dir (which allows us to touch the parent
1368 * dir's mtime right away). We will handle the dir in
1369 * full later (right before we handle its contents). */
1371 && (S_ISDIR(sx.st.st_mode)
1372 || delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0))
1373 goto cleanup; /* Any errors get reported later. */
1374 if (do_mkdir(fname, file->mode & 0700) == 0)
1375 file->flags |= FLAG_DIR_CREATED;
1378 /* The file to be received is a directory, so we need
1379 * to prepare appropriately. If there is already a
1380 * file of that name and it is *not* a directory, then
1381 * we need to delete it. If it doesn't exist, then
1382 * (perhaps recursively) create it. */
1383 if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1384 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0)
1385 goto skipping_dir_contents;
1388 if (dry_run && statret != 0 && missing_below < 0) {
1389 missing_below = F_DEPTH(file);
1395 if (file->flags & FLAG_DIR_CREATED)
1397 if (!preserve_perms) { /* See comment in non-dir code below. */
1398 file->mode = dest_mode(file->mode, sx.st.st_mode,
1399 dflt_perms, statret == 0);
1401 if (statret != 0 && basis_dir[0] != NULL) {
1402 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1410 if (itemizing && f_out != -1) {
1411 itemize(fname, file, ndx, statret, &sx,
1412 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1414 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1415 if (!relative_paths || errno != ENOENT
1416 || create_directory_path(fname) < 0
1417 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
1418 rsyserr(FERROR_XFER, errno,
1419 "recv_generator: mkdir %s failed",
1421 skipping_dir_contents:
1423 "*** Skipping any contents from this failed directory ***\n");
1424 missing_below = F_DEPTH(file);
1426 file->flags |= FLAG_MISSING_DIR;
1430 if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
1431 && verbose && code != FNONE && f_out != -1)
1432 rprintf(code, "%s/\n", fname);
1434 /* We need to ensure that the dirs in the transfer have writable
1435 * permissions during the time we are putting files within them.
1436 * This is then fixed after the transfer is done. */
1438 if (!am_root && !(file->mode & S_IWUSR) && dir_tweaking) {
1439 mode_t mode = file->mode | S_IWUSR;
1440 if (do_chmod(fname, mode) < 0) {
1441 rsyserr(FERROR_XFER, errno,
1442 "failed to modify permissions on %s",
1445 need_retouch_dir_perms = 1;
1449 if (real_ret != 0 && one_file_system)
1450 real_sx.st.st_dev = filesystem_dev;
1452 if (one_file_system) {
1453 uint32 *devp = F_DIR_DEV_P(file);
1454 DEV_MAJOR(devp) = major(real_sx.st.st_dev);
1455 DEV_MINOR(devp) = minor(real_sx.st.st_dev);
1458 else if (delete_during && f_out != -1 && !phase && dry_run < 2
1459 && (file->flags & FLAG_CONTENT_DIR))
1460 delete_in_dir(fname, file, &real_sx.st.st_dev);
1464 /* If we're not preserving permissions, change the file-list's
1465 * mode based on the local permissions and some heuristics. */
1466 if (!preserve_perms) {
1467 int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
1468 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
1472 #ifdef SUPPORT_HARD_LINKS
1473 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1474 && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
1478 if (preserve_links && S_ISLNK(file->mode)) {
1479 #ifdef SUPPORT_LINKS
1480 const char *sl = F_SYMLINK(file);
1481 if (safe_symlinks && unsafe_symlink(sl, fname)) {
1484 fname = f_name(file, NULL);
1486 "ignoring unsafe symlink %s -> \"%s\"\n",
1487 full_fname(fname), sl);
1492 char lnk[MAXPATHLEN];
1495 if (!S_ISLNK(sx.st.st_mode))
1497 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1498 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1499 /* The link is pointing to the right place. */
1500 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1502 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1503 #if defined SUPPORT_HARD_LINKS && defined CAN_HARDLINK_SYMLINK
1504 if (preserve_hard_links && F_IS_HLINKED(file))
1505 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1507 if (remove_source_files == 1)
1508 goto return_with_success;
1511 /* Not the right symlink (or not a symlink), so
1513 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_SYMLINK) != 0)
1515 } else if (basis_dir[0] != NULL) {
1516 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1519 #ifndef CAN_HARDLINK_SYMLINK
1521 /* Resort to --copy-dest behavior. */
1531 #ifdef SUPPORT_HARD_LINKS
1532 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1533 cur_flist->in_progress++;
1537 if (do_symlink(sl, fname) != 0) {
1538 rsyserr(FERROR_XFER, errno, "symlink %s -> \"%s\" failed",
1539 full_fname(fname), sl);
1541 set_file_attrs(fname, file, NULL, NULL, 0);
1543 itemize(fname, file, ndx, statret, &sx,
1544 ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
1546 if (code != FNONE && verbose)
1547 rprintf(code, "%s -> %s\n", fname, sl);
1548 #ifdef SUPPORT_HARD_LINKS
1549 if (preserve_hard_links && F_IS_HLINKED(file))
1550 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1552 /* This does not check remove_source_files == 1
1553 * because this is one of the items that the old
1554 * --remove-sent-files option would remove. */
1555 if (remove_source_files)
1556 goto return_with_success;
1562 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1563 || (preserve_specials && IS_SPECIAL(file->mode))) {
1564 uint32 *devp = F_RDEV_P(file);
1565 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1568 if (IS_DEVICE(file->mode)) {
1569 if (!IS_DEVICE(sx.st.st_mode))
1571 del_for_flag = DEL_FOR_DEVICE;
1573 if (!IS_SPECIAL(sx.st.st_mode))
1575 del_for_flag = DEL_FOR_SPECIAL;
1578 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
1579 && sx.st.st_rdev == rdev) {
1580 /* The device or special file is identical. */
1581 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1583 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1584 #ifdef SUPPORT_HARD_LINKS
1585 if (preserve_hard_links && F_IS_HLINKED(file))
1586 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1588 if (remove_source_files == 1)
1589 goto return_with_success;
1592 if (delete_item(fname, sx.st.st_mode, del_opts | del_for_flag) != 0)
1594 } else if (basis_dir[0] != NULL) {
1595 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1598 #ifndef CAN_HARDLINK_SPECIAL
1600 /* Resort to --copy-dest behavior. */
1610 #ifdef SUPPORT_HARD_LINKS
1611 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1612 cur_flist->in_progress++;
1617 rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1618 fname, (int)file->mode,
1619 (long)major(rdev), (long)minor(rdev));
1621 if (do_mknod(fname, file->mode, rdev) < 0) {
1622 rsyserr(FERROR_XFER, errno, "mknod %s failed",
1625 set_file_attrs(fname, file, NULL, NULL, 0);
1627 itemize(fname, file, ndx, statret, &sx,
1628 ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
1630 if (code != FNONE && verbose)
1631 rprintf(code, "%s\n", fname);
1632 #ifdef SUPPORT_HARD_LINKS
1633 if (preserve_hard_links && F_IS_HLINKED(file))
1634 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1636 if (remove_source_files == 1)
1637 goto return_with_success;
1642 if (!S_ISREG(file->mode)) {
1644 fname = f_name(file, NULL);
1645 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1649 if (max_size > 0 && F_LENGTH(file) > max_size) {
1652 fname = f_name(file, NULL);
1653 rprintf(FINFO, "%s is over max-size\n", fname);
1657 if (min_size > 0 && F_LENGTH(file) < min_size) {
1660 fname = f_name(file, NULL);
1661 rprintf(FINFO, "%s is under min-size\n", fname);
1666 if (ignore_existing > 0 && statret == 0) {
1668 rprintf(FINFO, "%s exists\n", fname);
1672 if (update_only > 0 && statret == 0
1673 && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
1675 rprintf(FINFO, "%s is newer\n", fname);
1680 fnamecmp_type = FNAMECMP_FNAME;
1682 if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
1683 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE) != 0)
1686 stat_errno = ENOENT;
1689 if (statret != 0 && basis_dir[0] != NULL) {
1690 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
1693 if (remove_source_files == 1)
1694 goto return_with_success;
1698 fnamecmp = fnamecmpbuf;
1707 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1708 && link_stat(partialptr, &partial_st, 0) == 0
1709 && S_ISREG(partial_st.st_mode)) {
1711 goto prepare_to_open;
1715 if (statret != 0 && fuzzy_dirlist && dry_run <= 1) {
1716 int j = find_fuzzy(file, fuzzy_dirlist);
1718 fuzzy_file = fuzzy_dirlist->files[j];
1719 f_name(fuzzy_file, fnamecmpbuf);
1721 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1722 fname, fnamecmpbuf);
1724 sx.st.st_size = F_LENGTH(fuzzy_file);
1726 fnamecmp = fnamecmpbuf;
1727 fnamecmp_type = FNAMECMP_FUZZY;
1732 #ifdef SUPPORT_HARD_LINKS
1733 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1734 cur_flist->in_progress++;
1738 if (stat_errno == ENOENT)
1740 rsyserr(FERROR_XFER, stat_errno, "recv_generator: failed to stat %s",
1745 if (append_mode > 0 && sx.st.st_size >= F_LENGTH(file))
1748 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1750 else if (fnamecmp_type == FNAMECMP_FUZZY)
1752 else if (unchanged_file(fnamecmp, file, &sx.st)) {
1754 do_unlink(partialptr);
1755 handle_partial_dir(partialptr, PDIR_DELETE);
1757 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1759 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
1760 #ifdef SUPPORT_HARD_LINKS
1761 if (preserve_hard_links && F_IS_HLINKED(file))
1762 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1764 if (remove_source_files != 1)
1766 return_with_success:
1768 send_msg_int(MSG_SUCCESS, ndx);
1775 fnamecmp = partialptr;
1776 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1783 if (read_batch || whole_file) {
1784 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1785 if (!(backupptr = get_backup_name(fname)))
1787 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
1788 goto pretend_missing;
1789 if (copy_file(fname, backupptr, -1, back_file->mode, 1) < 0) {
1790 unmake_file(back_file);
1798 if (fuzzy_dirlist) {
1799 int j = flist_find(fuzzy_dirlist, file);
1800 if (j >= 0) /* don't use changing file as future fuzzy basis */
1801 fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
1805 if ((fd = do_open(fnamecmp, O_RDONLY, 0)) < 0) {
1806 rsyserr(FERROR, errno, "failed to open %s, continuing",
1807 full_fname(fnamecmp));
1809 /* pretend the file didn't exist */
1810 #ifdef SUPPORT_HARD_LINKS
1811 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1812 cur_flist->in_progress++;
1816 statret = real_ret = -1;
1820 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1821 if (!(backupptr = get_backup_name(fname))) {
1825 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1827 goto pretend_missing;
1829 if (robust_unlink(backupptr) && errno != ENOENT) {
1830 rsyserr(FERROR_XFER, errno, "unlink %s",
1831 full_fname(backupptr));
1832 unmake_file(back_file);
1837 if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0
1838 && (errno != ENOENT || make_bak_dir(backupptr) < 0
1839 || (f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0)) {
1840 rsyserr(FERROR_XFER, errno, "open %s",
1841 full_fname(backupptr));
1842 unmake_file(back_file);
1847 fnamecmp_type = FNAMECMP_BACKUP;
1851 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1852 fnamecmp, (double)sx.st.st_size);
1856 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1859 if (remove_source_files && !delay_updates && !phase)
1860 increment_active_files(ndx, itemizing, code);
1861 if (inc_recurse && !dry_run)
1862 cur_flist->in_progress++;
1863 #ifdef SUPPORT_HARD_LINKS
1864 if (preserve_hard_links && F_IS_HLINKED(file))
1865 file->flags |= FLAG_FILE_SENT;
1867 write_ndx(f_out, ndx);
1869 int iflags = ITEM_TRANSFER;
1870 if (always_checksum > 0)
1871 iflags |= ITEM_REPORT_CHANGE;
1872 if (fnamecmp_type != FNAMECMP_FNAME)
1873 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1874 if (fnamecmp_type == FNAMECMP_FUZZY)
1875 iflags |= ITEM_XNAME_FOLLOWS;
1876 itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
1877 fuzzy_file ? fuzzy_file->basename : NULL);
1882 #ifdef SUPPORT_XATTRS
1883 if (preserve_xattrs)
1884 free_xattr(&real_sx);
1889 #ifdef SUPPORT_HARD_LINKS
1890 if (preserve_hard_links && F_IS_HLINKED(file))
1891 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1898 if (statret != 0 || whole_file)
1899 write_sum_head(f_out, NULL);
1901 generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy);
1909 set_file_attrs(backupptr, back_file, NULL, NULL, 0);
1911 rprintf(FINFO, "backed up %s to %s\n",
1914 unmake_file(back_file);
1921 #ifdef SUPPORT_XATTRS
1922 if (preserve_xattrs)
1928 static void touch_up_dirs(struct file_list *flist, int ndx)
1930 static int counter = 0;
1931 struct file_struct *file;
1937 end = flist->used - 1;
1941 /* Fix any directory permissions that were modified during the
1942 * transfer and/or re-set any tweaked modified-time values. */
1943 for (i = start; i <= end; i++, counter++) {
1944 file = flist->files[i];
1945 if (!S_ISDIR(file->mode)
1946 || (!implied_dirs && file->flags & FLAG_IMPLIED_DIR))
1949 fname = f_name(file, NULL);
1950 rprintf(FINFO, "touch_up_dirs: %s (%d)\n",
1953 if (!F_IS_ACTIVE(file) || file->flags & FLAG_MISSING_DIR
1954 || (!need_retouch_dir_times && file->mode & S_IWUSR))
1956 fname = f_name(file, NULL);
1957 if (!(file->mode & S_IWUSR))
1958 do_chmod(fname, file->mode);
1959 if (need_retouch_dir_times) {
1961 if (link_stat(fname, &st, 0) == 0
1962 && cmp_time(st.st_mtime, file->modtime) != 0)
1963 set_modtime(fname, file->modtime, file->mode);
1965 if (allowed_lull && !(counter % lull_mod))
1966 maybe_send_keepalive();
1967 else if (!(counter & 0xFF))
1968 maybe_flush_socket(0);
1972 void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
1974 struct file_struct *file;
1975 struct file_list *flist;
1976 char fbuf[MAXPATHLEN];
1980 #ifdef SUPPORT_HARD_LINKS
1981 if (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
1982 flist = flist_for_ndx(ndx);
1983 assert(flist != NULL);
1984 file = flist->files[ndx - flist->ndx_start];
1985 assert(file->flags & FLAG_HLINKED);
1986 finish_hard_link(file, f_name(file, fbuf), ndx, NULL, itemizing, code, -1);
1987 flist->in_progress--;
1992 if (check_redo && (ndx = get_redo_num()) != -1) {
1993 csum_length = SUM_LENGTH;
1994 max_size = -max_size;
1995 min_size = -min_size;
1996 ignore_existing = -ignore_existing;
1997 ignore_non_existing = -ignore_non_existing;
1998 update_only = -update_only;
1999 always_checksum = -always_checksum;
2000 size_only = -size_only;
2001 append_mode = -append_mode;
2002 make_backups = -make_backups; /* avoid dup backup w/inplace */
2006 cur_flist = flist_for_ndx(ndx);
2008 file = cur_flist->files[ndx - cur_flist->ndx_start];
2010 strlcpy(fbuf, solo_file, sizeof fbuf);
2013 recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
2014 cur_flist->to_redo--;
2018 csum_length = SHORT_SUM_LENGTH;
2019 max_size = -max_size;
2020 min_size = -min_size;
2021 ignore_existing = -ignore_existing;
2022 ignore_non_existing = -ignore_non_existing;
2023 update_only = -update_only;
2024 always_checksum = -always_checksum;
2025 size_only = -size_only;
2026 append_mode = -append_mode;
2027 make_backups = -make_backups;
2032 if (cur_flist == first_flist)
2035 /* We only get here if inc_recurse is enabled. */
2036 if (first_flist->in_progress || first_flist->to_redo)
2040 write_ndx(sock_f_out, NDX_DONE);
2041 maybe_flush_socket(1);
2044 if (delete_during == 2 || !dir_tweaking) {
2045 /* Skip directory touch-up. */
2046 } else if (first_flist->parent_ndx >= 0)
2047 touch_up_dirs(dir_flist, first_flist->parent_ndx);
2049 flist_free(first_flist); /* updates first_flist */
2053 void generate_files(int f_out, const char *local_name)
2056 char fbuf[MAXPATHLEN];
2059 int save_do_progress = do_progress;
2061 if (protocol_version >= 29) {
2063 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
2064 code = logfile_format_has_i ? FNONE : FLOG;
2065 } else if (am_daemon) {
2066 itemizing = logfile_format_has_i && do_xfers;
2067 maybe_ATTRS_REPORT = ATTRS_REPORT;
2068 code = itemizing || !do_xfers ? FCLIENT : FINFO;
2069 } else if (!am_server) {
2070 itemizing = stdout_format_has_i;
2071 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
2072 code = itemizing ? FNONE : FINFO;
2075 maybe_ATTRS_REPORT = ATTRS_REPORT;
2078 solo_file = local_name;
2079 dir_tweaking = !(list_only || solo_file || dry_run);
2080 need_retouch_dir_times = preserve_times > 1;
2081 lull_mod = allowed_lull * 5;
2082 symlink_timeset_failed_flags = ITEM_REPORT_TIME
2083 | (protocol_version >= 30 || !am_server ? ITEM_REPORT_TIMEFAIL : 0);
2086 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
2088 if (delete_before && !solo_file && cur_flist->used > 0)
2090 if (delete_during == 2) {
2091 deldelay_size = BIGPATHBUFLEN * 4;
2092 deldelay_buf = new_array(char, deldelay_size);
2094 out_of_memory("delete-delay");
2098 if (append_mode > 0 || whole_file < 0)
2101 rprintf(FINFO, "delta-transmission %s\n",
2103 ? "disabled for local transfer or --whole-file"
2107 /* Since we often fill up the outgoing socket and then just sit around
2108 * waiting for the other 2 processes to do their thing, we don't want
2109 * to exit on a timeout. If the data stops flowing, the receiver will
2110 * notice that and let us know via the redo pipe (or its closing). */
2113 dflt_perms = (ACCESSPERMS & ~orig_umask);
2116 #ifdef SUPPORT_HARD_LINKS
2117 if (preserve_hard_links && inc_recurse) {
2118 while (!flist_eof && file_total < FILECNT_LOOKAHEAD/2)
2119 wait_for_receiver();
2123 if (inc_recurse && cur_flist->parent_ndx >= 0) {
2124 struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
2126 ndx = cur_flist->ndx_start - 1;
2127 recv_generator(fbuf, fp, ndx, itemizing, code, f_out);
2128 if (delete_during && dry_run < 2 && !list_only) {
2129 if (BITS_SETnUNSET(fp->flags, FLAG_CONTENT_DIR, FLAG_MISSING_DIR)) {
2131 if (one_file_system) {
2132 uint32 *devp = F_DIR_DEV_P(fp);
2133 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
2135 dirdev = MAKEDEV(0, 0);
2136 delete_in_dir(f_name(fp, fbuf), fp, &dirdev);
2140 for (i = cur_flist->low; i <= cur_flist->high; i++) {
2141 struct file_struct *file = cur_flist->sorted[i];
2143 if (!F_IS_ACTIVE(file))
2149 ndx = i + cur_flist->ndx_start;
2152 strlcpy(fbuf, solo_file, sizeof fbuf);
2155 recv_generator(fbuf, file, ndx, itemizing, code, f_out);
2157 check_for_finished_files(itemizing, code, 0);
2159 if (allowed_lull && !(i % lull_mod))
2160 maybe_send_keepalive();
2161 else if (!(i & 0xFF))
2162 maybe_flush_socket(0);
2166 write_ndx(f_out, NDX_DONE);
2171 check_for_finished_files(itemizing, code, 1);
2172 if (cur_flist->next || flist_eof)
2174 wait_for_receiver();
2176 } while ((cur_flist = cur_flist->next) != NULL);
2179 delete_in_dir(NULL, NULL, &dev_zero);
2182 rprintf(FINFO, "generate_files phase=%d\n", phase);
2185 check_for_finished_files(itemizing, code, 1);
2188 wait_for_receiver();
2193 rprintf(FINFO, "generate_files phase=%d\n", phase);
2195 write_ndx(f_out, NDX_DONE);
2196 /* Reduce round-trip lag-time for a useless delay-updates phase. */
2197 if (protocol_version >= 29 && !delay_updates)
2198 write_ndx(f_out, NDX_DONE);
2200 /* Read MSG_DONE for the redo phase (and any prior messages). */
2202 check_for_finished_files(itemizing, code, 0);
2203 if (msgdone_cnt > 1)
2205 wait_for_receiver();
2208 if (protocol_version >= 29) {
2211 rprintf(FINFO, "generate_files phase=%d\n", phase);
2213 write_ndx(f_out, NDX_DONE);
2214 /* Read MSG_DONE for delay-updates phase & prior messages. */
2215 while (msgdone_cnt == 2)
2216 wait_for_receiver();
2219 do_progress = save_do_progress;
2220 if (delete_during == 2)
2221 do_delayed_deletions(fbuf);
2222 if (delete_after && !solo_file && file_total > 0)
2225 if ((need_retouch_dir_perms || need_retouch_dir_times)
2226 && dir_tweaking && (!inc_recurse || delete_during == 2))
2227 touch_up_dirs(dir_flist, -1);
2229 if (max_delete >= 0 && deletion_count > max_delete) {
2231 "Deletions stopped due to --max-delete limit (%d skipped)\n",
2232 deletion_count - max_delete);
2233 io_error |= IOERR_DEL_LIMIT;
2237 rprintf(FINFO, "generate_files finished\n");