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.
29 extern int stdout_format_has_i;
30 extern int logfile_format_has_i;
34 extern int inc_recurse;
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 human_readable;
59 extern int ignore_existing;
60 extern int ignore_non_existing;
62 extern int append_mode;
63 extern int make_backups;
64 extern int csum_length;
65 extern int ignore_times;
67 extern OFF_T max_size;
68 extern OFF_T min_size;
71 extern int allowed_lull;
72 extern int sock_f_out;
73 extern int ignore_timeout;
74 extern int protocol_version;
75 extern int file_total;
76 extern int fuzzy_basis;
77 extern int always_checksum;
78 extern int checksum_len;
79 extern char *partial_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 char *basis_dir[MAX_BASIS_DIRS+1];
100 extern struct file_list *cur_flist, *first_flist, *dir_flist;
101 extern struct filter_list_struct daemon_filter_list;
103 int ignore_perishable = 0;
104 int non_perishable_cnt = 0;
105 int maybe_ATTRS_REPORT = 0;
107 static dev_t dev_zero;
108 static int skipped_deletes = 0;
109 static int deldelay_size = 0, deldelay_cnt = 0;
110 static char *deldelay_buf = NULL;
111 static int deldelay_fd = -1;
112 static int loopchk_limit;
113 static int dir_tweaking;
114 static int symlink_timeset_failed_flags;
115 static int need_retouch_dir_times;
116 static int need_retouch_dir_perms;
117 static const char *solo_file = NULL;
119 /* For calling delete_item() and delete_dir_contents(). */
120 #define DEL_NO_UID_WRITE (1<<0) /* file/dir has our uid w/o write perm */
121 #define DEL_RECURSE (1<<1) /* if dir, delete all contents */
122 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
123 #define DEL_FOR_FILE (1<<3) /* making room for a replacement file */
124 #define DEL_FOR_DIR (1<<4) /* making room for a replacement dir */
125 #define DEL_FOR_SYMLINK (1<<5) /* making room for a replacement symlink */
126 #define DEL_FOR_DEVICE (1<<6) /* making room for a replacement device */
127 #define DEL_FOR_SPECIAL (1<<7) /* making room for a replacement special */
129 #define DEL_MAKE_ROOM (DEL_FOR_FILE|DEL_FOR_DIR|DEL_FOR_SYMLINK|DEL_FOR_DEVICE|DEL_FOR_SPECIAL)
132 TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
136 DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_NOT_EMPTY
139 /* Forward declarations. */
140 static enum delret delete_dir_contents(char *fname, uint16 flags);
141 #ifdef SUPPORT_HARD_LINKS
142 static void handle_skipped_hlink(struct file_struct *file, int itemizing,
143 enum logcode code, int f_out);
146 static int is_backup_file(char *fn)
148 int k = strlen(fn) - backup_suffix_len;
149 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
152 /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
153 * delete recursively.
155 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
156 * a directory! (The buffer is used for recursion, but returned unchanged.)
158 static enum delret delete_item(char *fbuf, uint16 mode, uint16 flags)
164 if (DEBUG_GTE(DEL, 2)) {
165 rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
166 fbuf, (int)mode, (int)flags);
169 if (flags & DEL_NO_UID_WRITE)
170 do_chmod(fbuf, mode | S_IWUSR);
172 if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
173 int save_uid_ndx = uid_ndx;
174 /* This only happens on the first call to delete_item() since
175 * delete_dir_contents() always calls us w/DEL_DIR_IS_EMPTY. */
177 uid_ndx = ++file_extra_cnt;
178 ignore_perishable = 1;
179 /* If DEL_RECURSE is not set, this just reports emptiness. */
180 ret = delete_dir_contents(fbuf, flags);
181 ignore_perishable = 0;
186 if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT)
188 /* OK: try to delete the directory. */
191 if (!(flags & DEL_MAKE_ROOM) && max_delete >= 0 && stats.deleted_files >= max_delete) {
198 ok = do_rmdir(fbuf) == 0;
199 } else if (make_backups > 0 && (backup_dir || !is_backup_file(fbuf))) {
200 what = "make_backup";
201 ok = make_backup(fbuf);
204 ok = robust_unlink(fbuf) == 0;
208 if (!(flags & DEL_MAKE_ROOM)) {
209 log_delete(fbuf, mode);
210 stats.deleted_files++;
212 /* Nothing more to count */
213 } else if (S_ISDIR(mode))
214 stats.deleted_dirs++;
216 else if (S_ISLNK(mode))
217 stats.deleted_symlinks++;
219 else if (IS_DEVICE(mode))
220 stats.deleted_symlinks++;
222 stats.deleted_specials++;
226 if (S_ISDIR(mode) && errno == ENOTEMPTY) {
227 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
230 } else if (errno != ENOENT) {
231 rsyserr(FERROR, errno, "delete_file: %s(%s) failed",
239 if (ret != DR_SUCCESS && flags & DEL_MAKE_ROOM) {
241 switch (flags & DEL_MAKE_ROOM) {
242 case DEL_FOR_FILE: desc = "regular file"; break;
243 case DEL_FOR_DIR: desc = "directory"; break;
244 case DEL_FOR_SYMLINK: desc = "symlink"; break;
245 case DEL_FOR_DEVICE: desc = "device file"; break;
246 case DEL_FOR_SPECIAL: desc = "special file"; break;
247 default: exit_cleanup(RERR_UNSUPPORTED); /* IMPOSSIBLE */
249 rprintf(FERROR_XFER, "could not make way for new %s: %s\n",
255 /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
256 * its contents, otherwise just checks for content. Returns DR_SUCCESS or
257 * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
258 * buffer is used for recursion, but returned unchanged.)
260 static enum delret delete_dir_contents(char *fname, uint16 flags)
262 struct file_list *dirlist;
269 if (DEBUG_GTE(DEL, 3)) {
270 rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n",
274 dlen = strlen(fname);
275 save_filters = push_local_filters(fname, dlen);
277 non_perishable_cnt = 0;
278 dirlist = get_dirlist(fname, dlen, 0);
279 ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
284 if (!(flags & DEL_RECURSE)) {
290 if (dlen != 1 || *fname != '/')
292 remainder = MAXPATHLEN - (p - fname);
294 /* We do our own recursion, so make delete_item() non-recursive. */
295 flags = (flags & ~(DEL_RECURSE|DEL_MAKE_ROOM|DEL_NO_UID_WRITE))
298 for (j = dirlist->used; j--; ) {
299 struct file_struct *fp = dirlist->files[j];
301 if (fp->flags & FLAG_MOUNT_DIR && S_ISDIR(fp->mode)) {
302 if (DEBUG_GTE(DEL, 1)) {
304 "mount point, %s, pins parent directory\n",
311 strlcpy(p, fp->basename, remainder);
312 if (!(fp->mode & S_IWUSR) && !am_root && (uid_t)F_OWNER(fp) == our_uid)
313 do_chmod(fname, fp->mode | S_IWUSR);
314 /* Save stack by recursing to ourself directly. */
315 if (S_ISDIR(fp->mode)) {
316 if (delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
319 if (delete_item(fname, fp->mode, flags) != DR_SUCCESS)
327 pop_local_filters(save_filters);
329 if (ret == DR_NOT_EMPTY) {
330 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
336 static int start_delete_delay_temp(void)
338 char fnametmp[MAXPATHLEN];
339 int save_dry_run = dry_run;
342 if (!get_tmpname(fnametmp, "deldelay")
343 || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
344 rprintf(FINFO, "NOTE: Unable to create delete-delay temp file%s.\n",
345 inc_recurse ? "" : " -- switching to --delete-after");
347 delete_after = !inc_recurse;
348 dry_run = save_dry_run;
352 dry_run = save_dry_run;
356 static int flush_delete_delay(void)
358 if (deldelay_fd < 0 && !start_delete_delay_temp())
360 if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
361 rsyserr(FERROR, errno, "flush of delete-delay buffer");
363 delete_after = !inc_recurse;
371 static int remember_delete(struct file_struct *file, const char *fname, int flags)
375 if (deldelay_cnt == deldelay_size && !flush_delete_delay())
378 if (flags & DEL_NO_UID_WRITE)
379 deldelay_buf[deldelay_cnt++] = '!';
382 len = snprintf(deldelay_buf + deldelay_cnt,
383 deldelay_size - deldelay_cnt,
385 (int)file->mode, fname, '\0');
386 if ((deldelay_cnt += len) <= deldelay_size)
389 if (!flush_delete_delay())
396 static int read_delay_line(char *buf, int *flags_p)
398 static int read_pos = 0;
400 char *bp, *past_space;
403 for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
404 if (j < deldelay_cnt)
406 if (deldelay_fd < 0) {
411 deldelay_cnt -= read_pos;
412 if (deldelay_cnt == deldelay_size)
414 if (deldelay_cnt && read_pos) {
415 memmove(deldelay_buf, deldelay_buf + read_pos,
418 len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
419 deldelay_size - deldelay_cnt);
423 "ERROR: unexpected EOF in delete-delay file.\n");
428 rsyserr(FERROR, errno,
429 "reading delete-delay file");
436 bp = deldelay_buf + read_pos;
439 *flags_p = DEL_NO_UID_WRITE;
443 if (sscanf(bp, "%x ", &mode) != 1) {
445 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
448 past_space = strchr(bp, ' ') + 1;
449 len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
452 if (len > MAXPATHLEN) {
453 rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
457 /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
458 * instead of returning a pointer to our buffer. */
459 memcpy(buf, past_space, len);
464 static void do_delayed_deletions(char *delbuf)
468 if (deldelay_fd >= 0) {
469 if (deldelay_cnt && !flush_delete_delay())
471 lseek(deldelay_fd, 0, 0);
473 while ((mode = read_delay_line(delbuf, &flags)) >= 0)
474 delete_item(delbuf, mode, flags | DEL_RECURSE);
475 if (deldelay_fd >= 0)
479 /* This function is used to implement per-directory deletion, and is used by
480 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
481 * MAXPATHLEN buffer with the name of the directory in it (the functions we
482 * call will append names onto the end, but the old dir value will be restored
484 static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
486 static int already_warned = 0;
487 struct file_list *dirlist;
488 char delbuf[MAXPATHLEN];
490 int save_uid_ndx = uid_ndx;
493 change_local_filter_dir(NULL, 0, 0);
497 if (DEBUG_GTE(DEL, 2))
498 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
501 maybe_send_keepalive();
503 if (io_error && !ignore_errors) {
507 "IO error encountered -- skipping file deletion\n");
513 change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
515 if (one_file_system) {
516 if (file->flags & FLAG_TOP_DIR)
517 filesystem_dev = *fs_dev;
518 else if (filesystem_dev != *fs_dev)
523 uid_ndx = ++file_extra_cnt;
525 dirlist = get_dirlist(fbuf, dlen, 0);
527 /* If an item in dirlist is not found in flist, delete it
528 * from the filesystem. */
529 for (i = dirlist->used; i--; ) {
530 struct file_struct *fp = dirlist->files[i];
531 if (!F_IS_ACTIVE(fp))
533 if (fp->flags & FLAG_MOUNT_DIR && S_ISDIR(fp->mode)) {
534 if (INFO_GTE(MOUNT, 1))
535 rprintf(FINFO, "cannot delete mount point: %s\n",
539 /* Here we want to match regardless of file type. Replacement
540 * of a file with one of another type is handled separately by
541 * a delete_item call with a DEL_MAKE_ROOM flag. */
542 if (flist_find_ignore_dirness(cur_flist, fp) < 0) {
543 int flags = DEL_RECURSE;
544 if (!(fp->mode & S_IWUSR) && !am_root && (uid_t)F_OWNER(fp) == our_uid)
545 flags |= DEL_NO_UID_WRITE;
547 if (delete_during == 2) {
548 if (!remember_delete(fp, delbuf, flags))
551 delete_item(delbuf, fp->mode, flags);
563 /* This deletes any files on the receiving side that are not present on the
564 * sending side. This is used by --delete-before and --delete-after. */
565 static void do_delete_pass(void)
567 char fbuf[MAXPATHLEN];
571 /* dry_run is incremented when the destination doesn't exist yet. */
572 if (dry_run > 1 || list_only)
575 for (j = 0; j < cur_flist->used; j++) {
576 struct file_struct *file = cur_flist->sorted[j];
580 if (!(file->flags & FLAG_CONTENT_DIR)) {
581 change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(file));
585 if (DEBUG_GTE(DEL, 1) && file->flags & FLAG_TOP_DIR)
586 rprintf(FINFO, "deleting in %s\n", fbuf);
588 if (link_stat(fbuf, &st, keep_dirlinks) < 0
589 || !S_ISDIR(st.st_mode))
592 delete_in_dir(fbuf, file, &st.st_dev);
594 delete_in_dir(NULL, NULL, &dev_zero);
596 if (INFO_GTE(FLIST, 2) && !am_server)
597 rprintf(FINFO, " \r");
600 int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
602 #if !defined HAVE_LUTIMES || !defined HAVE_UTIMES
603 if (S_ISLNK(file->mode)) {
607 if (preserve_times && cmp_time(sxp->st.st_mtime, file->modtime) != 0)
610 if (preserve_perms) {
611 if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
613 } else if (preserve_executability
614 && ((sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0)))
617 if (am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file))
620 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
624 if (preserve_acls && !S_ISLNK(file->mode)) {
625 if (!ACL_READY(*sxp))
627 if (set_acl(NULL, file, sxp) == 0)
631 #ifdef SUPPORT_XATTRS
632 if (preserve_xattrs) {
633 if (!XATTR_READY(*sxp))
634 get_xattr(fname, sxp);
635 if (xattr_diff(file, sxp, 0))
643 void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
644 stat_x *sxp, int32 iflags, uchar fnamecmp_type,
647 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
648 int keep_time = !preserve_times ? 0
649 : S_ISDIR(file->mode) ? preserve_times > 1 :
650 #if defined HAVE_LUTIMES && defined HAVE_UTIMES
653 !S_ISLNK(file->mode);
656 if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
657 iflags |= ITEM_REPORT_SIZE;
658 if (file->flags & FLAG_TIME_FAILED) { /* symlinks only */
659 if (iflags & ITEM_LOCAL_CHANGE)
660 iflags |= symlink_timeset_failed_flags;
662 ? cmp_time(file->modtime, sxp->st.st_mtime) != 0
663 : iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !(iflags & ITEM_MATCHED)
664 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
665 iflags |= ITEM_REPORT_TIME;
666 #if !defined HAVE_LCHMOD && !defined HAVE_SETATTRLIST
667 if (S_ISLNK(file->mode)) {
671 if (preserve_perms) {
672 if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
673 iflags |= ITEM_REPORT_PERMS;
674 } else if (preserve_executability
675 && ((sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0)))
676 iflags |= ITEM_REPORT_PERMS;
677 if (uid_ndx && am_root && (uid_t)F_OWNER(file) != sxp->st.st_uid)
678 iflags |= ITEM_REPORT_OWNER;
679 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
680 && sxp->st.st_gid != (gid_t)F_GROUP(file))
681 iflags |= ITEM_REPORT_GROUP;
683 if (preserve_acls && !S_ISLNK(file->mode)) {
684 if (!ACL_READY(*sxp))
685 get_acl(fnamecmp, sxp);
686 if (set_acl(NULL, file, sxp) == 0)
687 iflags |= ITEM_REPORT_ACL;
690 #ifdef SUPPORT_XATTRS
691 if (preserve_xattrs) {
692 if (!XATTR_READY(*sxp))
693 get_xattr(fnamecmp, sxp);
694 if (xattr_diff(file, sxp, 1))
695 iflags |= ITEM_REPORT_XATTR;
699 #ifdef SUPPORT_XATTRS
700 if (preserve_xattrs && xattr_diff(file, NULL, 1))
701 iflags |= ITEM_REPORT_XATTR;
703 iflags |= ITEM_IS_NEW;
707 if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || INFO_GTE(NAME, 2)
708 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
709 if (protocol_version >= 29) {
711 write_ndx(sock_f_out, ndx);
712 write_shortint(sock_f_out, iflags);
713 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
714 write_byte(sock_f_out, fnamecmp_type);
715 if (iflags & ITEM_XNAME_FOLLOWS)
716 write_vstring(sock_f_out, xname, strlen(xname));
717 #ifdef SUPPORT_XATTRS
718 if (preserve_xattrs && !dry_run
719 && iflags & (ITEM_REPORT_XATTR|ITEM_TRANSFER)) {
720 send_xattr_request(NULL, file,
721 iflags & ITEM_REPORT_XATTR ? sock_f_out : -1);
724 } else if (ndx >= 0) {
725 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
726 log_item(code, file, &stats, iflags, xname);
732 /* Perform our quick-check heuristic for determining if a file is unchanged. */
733 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
735 if (st->st_size != F_LENGTH(file))
738 /* if always checksum is set then we use the checksum instead
739 of the file time to determine whether to sync */
740 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
741 char sum[MAX_DIGEST_LEN];
742 file_checksum(fn, sum, st->st_size);
743 return memcmp(sum, F_SUM(file), checksum_len) == 0;
752 return cmp_time(st->st_mtime, file->modtime) == 0;
757 * set (initialize) the size entries in the per-file sum_struct
758 * calculating dynamic block and checksum sizes.
760 * This is only called from generate_and_send_sums() but is a separate
761 * function to encapsulate the logic.
763 * The block size is a rounded square root of file length.
765 * The checksum size is determined according to:
766 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
767 * provided by Donovan Baarda which gives a probability of rsync
768 * algorithm corrupting data and falling back using the whole md4
771 * This might be made one of several selectable heuristics.
773 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
780 blength = block_size;
781 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
782 blength = BLOCK_SIZE;
784 int32 max_blength = protocol_version < 30 ? OLD_MAX_BLOCK_SIZE : MAX_BLOCK_SIZE;
787 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
788 if (c < 0 || c >= max_blength)
789 blength = max_blength;
794 if (len < (int64)blength * blength)
797 } while (c >= 8); /* round to multiple of 8 */
798 blength = MAX(blength, BLOCK_SIZE);
802 if (protocol_version < 27) {
803 s2length = csum_length;
804 } else if (csum_length == SUM_LENGTH) {
805 s2length = SUM_LENGTH;
808 int b = BLOCKSUM_BIAS;
809 for (l = len; l >>= 1; b += 2) {}
810 for (c = blength; (c >>= 1) && b; b--) {}
811 /* add a bit, subtract rollsum, round up. */
812 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
813 s2length = MAX(s2length, csum_length);
814 s2length = MIN(s2length, SUM_LENGTH);
818 sum->blength = blength;
819 sum->s2length = s2length;
820 sum->remainder = (int32)(len % blength);
821 sum->count = (int32)(l = (len / blength) + (sum->remainder != 0));
823 if ((int64)sum->count != l)
826 if (sum->count && DEBUG_GTE(DELTASUM, 2)) {
828 "count=%s rem=%ld blength=%ld s2length=%d flength=%s\n",
829 big_num(sum->count), (long)sum->remainder, (long)sum->blength,
830 sum->s2length, big_num(sum->flength));
836 * Generate and send a stream of signatures/checksums that describe a buffer
838 * Generate approximately one checksum every block_len bytes.
840 static int generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
843 struct map_struct *mapbuf;
844 struct sum_struct sum;
847 sum_sizes_sqroot(&sum, len);
850 write_sum_head(f_out, &sum);
852 if (append_mode > 0 && f_copy < 0)
856 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
860 for (i = 0; i < sum.count; i++) {
861 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
862 char *map = map_ptr(mapbuf, offset, n1);
863 char sum2[SUM_LENGTH];
870 full_write(f_copy, map, n1);
875 sum1 = get_checksum1(map, n1);
876 get_checksum2(map, n1, sum2);
878 if (DEBUG_GTE(DELTASUM, 3)) {
880 "chunk[%s] offset=%s len=%ld sum1=%08lx\n",
881 big_num(i), big_num(offset - n1), (long)n1,
882 (unsigned long)sum1);
884 write_int(f_out, sum1);
885 write_buf(f_out, sum2, sum.s2length);
895 /* Try to find a filename in the same dir as "fname" with a similar name. */
896 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
898 int fname_len, fname_suf_len;
899 const char *fname_suf, *fname = file->basename;
900 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
901 int j, lowest_j = -1;
903 fname_len = strlen(fname);
904 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
906 for (j = 0; j < dirlist->used; j++) {
907 struct file_struct *fp = dirlist->files[j];
908 const char *suf, *name;
912 if (!S_ISREG(fp->mode) || !F_LENGTH(fp)
913 || fp->flags & FLAG_FILE_SENT)
918 if (F_LENGTH(fp) == F_LENGTH(file)
919 && cmp_time(fp->modtime, file->modtime) == 0) {
920 if (DEBUG_GTE(FUZZY, 2)) {
922 "fuzzy size/modtime match for %s\n",
929 suf = find_filename_suffix(name, len, &suf_len);
931 dist = fuzzy_distance(name, len, fname, fname_len);
932 /* Add some extra weight to how well the suffixes match. */
933 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
935 if (DEBUG_GTE(FUZZY, 2)) {
936 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
937 name, (int)(dist>>16), (int)(dist&0xFFFF));
939 if (dist <= lowest_dist) {
948 /* Copy a file found in our --copy-dest handling. */
949 static int copy_altdest_file(const char *src, const char *dest, struct file_struct *file)
951 char buf[MAXPATHLEN];
952 const char *copy_to, *partialptr;
953 int save_preserve_xattrs = preserve_xattrs;
957 /* Let copy_file open the destination in place. */
961 fd_w = open_tmpfile(buf, dest, file);
966 cleanup_set(copy_to, NULL, NULL, -1, -1);
967 if (copy_file(src, copy_to, fd_w, file->mode, 0) < 0) {
968 if (INFO_GTE(COPY, 1)) {
969 rsyserr(FINFO, errno, "copy_file %s => %s",
970 full_fname(src), copy_to);
972 /* Try to clean up. */
977 partialptr = partial_dir ? partial_dir_fname(dest) : NULL;
978 preserve_xattrs = 0; /* xattrs were copied with file */
979 ok = finish_transfer(dest, copy_to, src, partialptr, file, 1, 0);
980 preserve_xattrs = save_preserve_xattrs;
985 /* This is only called for regular files. We return -2 if we've finished
986 * handling the file, -1 if no dest-linking occurred, or a non-negative
987 * value if we found an alternate basis file. */
988 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
989 char *cmpbuf, stat_x *sxp, int itemizing,
997 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
998 if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
1000 switch (match_level) {
1006 if (!unchanged_file(cmpbuf, file, &sxp->st))
1012 if (!unchanged_attrs(cmpbuf, file, sxp))
1019 } while (basis_dir[++j] != NULL);
1024 if (j != best_match) {
1026 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1027 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1031 if (match_level == 3 && !copy_dest) {
1032 #ifdef SUPPORT_HARD_LINKS
1034 if (!hard_link_one(file, fname, cmpbuf, 1))
1036 if (preserve_hard_links && F_IS_HLINKED(file))
1037 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, j);
1038 if (!maybe_ATTRS_REPORT && (INFO_GTE(NAME, 2) || stdout_format_has_i > 1)) {
1039 itemize(cmpbuf, file, ndx, 1, sxp,
1040 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
1046 itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
1047 if (INFO_GTE(NAME, 2) && maybe_ATTRS_REPORT)
1048 rprintf(FCLIENT, "%s is uptodate\n", fname);
1052 if (match_level >= 2) {
1053 #ifdef SUPPORT_HARD_LINKS
1054 try_a_copy: /* Copy the file locally. */
1056 if (!dry_run && copy_altdest_file(cmpbuf, fname, file) < 0)
1059 itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
1060 if (maybe_ATTRS_REPORT
1061 && ((!itemizing && INFO_GTE(NAME, 1) && match_level == 2)
1062 || (INFO_GTE(NAME, 2) && match_level == 3))) {
1063 code = match_level == 3 ? FCLIENT : FINFO;
1064 rprintf(code, "%s%s\n", fname,
1065 match_level == 3 ? " is uptodate" : "");
1067 #ifdef SUPPORT_HARD_LINKS
1068 if (preserve_hard_links && F_IS_HLINKED(file))
1069 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, -1);
1074 return FNAMECMP_BASIS_DIR_LOW + j;
1077 /* This is only called for non-regular files. We return -2 if we've finished
1078 * handling the file, or -1 if no dest-linking occurred, or a non-negative
1079 * value if we found an alternate basis file. */
1080 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
1081 char *cmpbuf, stat_x *sxp, int itemizing,
1084 char lnk[MAXPATHLEN];
1085 int best_match = -1;
1086 int match_level = 0;
1087 enum nonregtype type;
1091 #ifndef SUPPORT_LINKS
1092 if (S_ISLNK(file->mode))
1095 if (S_ISDIR(file->mode)) {
1097 } else if (IS_SPECIAL(file->mode))
1098 type = TYPE_SPECIAL;
1099 else if (IS_DEVICE(file->mode))
1101 #ifdef SUPPORT_LINKS
1102 else if (S_ISLNK(file->mode))
1103 type = TYPE_SYMLINK;
1107 "internal: try_dests_non() called with invalid mode (%o)\n",
1109 exit_cleanup(RERR_UNSUPPORTED);
1113 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1114 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1118 if (!S_ISDIR(sxp->st.st_mode))
1122 if (!IS_SPECIAL(sxp->st.st_mode))
1126 if (!IS_DEVICE(sxp->st.st_mode))
1129 #ifdef SUPPORT_LINKS
1131 if (!S_ISLNK(sxp->st.st_mode))
1136 if (match_level < 1) {
1145 devp = F_RDEV_P(file);
1146 if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
1149 #ifdef SUPPORT_LINKS
1151 if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
1154 if (strcmp(lnk, F_SYMLINK(file)) != 0)
1159 if (match_level < 2) {
1163 if (unchanged_attrs(cmpbuf, file, sxp)) {
1168 } while (basis_dir[++j] != NULL);
1173 if (j != best_match) {
1175 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1176 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1180 if (match_level == 3) {
1181 #ifdef SUPPORT_HARD_LINKS
1183 #ifndef CAN_HARDLINK_SYMLINK
1184 && !S_ISLNK(file->mode)
1186 #ifndef CAN_HARDLINK_SPECIAL
1187 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
1189 && !S_ISDIR(file->mode)) {
1190 if (do_link(cmpbuf, fname) < 0) {
1191 rsyserr(FERROR_XFER, errno,
1192 "failed to hard-link %s with %s",
1196 if (preserve_hard_links && F_IS_HLINKED(file))
1197 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1201 if (itemizing && stdout_format_has_i
1202 && (INFO_GTE(NAME, 2) || stdout_format_has_i > 1)) {
1203 int chg = compare_dest && type != TYPE_DIR ? 0
1204 : ITEM_LOCAL_CHANGE + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1205 char *lp = match_level == 3 ? "" : NULL;
1206 itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
1208 if (INFO_GTE(NAME, 2) && maybe_ATTRS_REPORT) {
1209 rprintf(FCLIENT, "%s%s is uptodate\n",
1210 fname, type == TYPE_DIR ? "/" : "");
1218 static void list_file_entry(struct file_struct *f)
1220 char permbuf[PERMSTRING_SIZE];
1222 int colwidth = human_readable ? 14 : 11;
1224 if (!F_IS_ACTIVE(f)) {
1225 /* this can happen if duplicate names were removed */
1229 permstring(permbuf, f->mode);
1232 /* TODO: indicate '+' if the entry has an ACL. */
1234 #ifdef SUPPORT_LINKS
1235 if (preserve_links && S_ISLNK(f->mode)) {
1236 rprintf(FINFO, "%s %*s %s %s -> %s\n",
1237 permbuf, colwidth, comma_num(len),
1238 timestring(f->modtime), f_name(f, NULL),
1243 rprintf(FINFO, "%s %*s %s %s\n",
1244 permbuf, colwidth, comma_num(len),
1245 timestring(f->modtime), f_name(f, NULL));
1249 static int phase = 0;
1250 static int dflt_perms;
1252 static int implied_dirs_are_missing;
1253 /* Helper for recv_generator's skip_dir and dry_missing_dir tests. */
1254 static BOOL is_below(struct file_struct *file, struct file_struct *subtree)
1256 return F_DEPTH(file) > F_DEPTH(subtree)
1257 && (!implied_dirs_are_missing || f_name_has_prefix(file, subtree));
1260 /* Acts on the indicated item in cur_flist whose name is fname. If a dir,
1261 * make sure it exists, and has the right permissions/timestamp info. For
1262 * all other non-regular files (symlinks, etc.) we create them here. For
1263 * regular files that have changed, we try to find a basis file and then
1264 * start sending checksums. The ndx is the file's unique index value.
1266 * The fname parameter must point to a MAXPATHLEN buffer! (e.g it gets
1267 * passed to delete_item(), which can use it during a recursive delete.)
1269 * Note that f_out is set to -1 when doing final directory-permission and
1270 * modification-time repair. */
1271 static void recv_generator(char *fname, struct file_struct *file, int ndx,
1272 int itemizing, enum logcode code, int f_out)
1274 static const char *parent_dirname = "";
1275 /* Missing dir not created due to --dry-run; will still be scanned. */
1276 static struct file_struct *dry_missing_dir = NULL;
1277 /* Missing dir whose contents are skipped altogether due to
1278 * --ignore-non-existing, daemon exclude, or mkdir failure. */
1279 static struct file_struct *skip_dir = NULL;
1280 static struct file_list *fuzzy_dirlist = NULL;
1281 static int need_fuzzy_dirlist = 0;
1282 struct file_struct *fuzzy_file = NULL;
1283 int fd = -1, f_copy = -1;
1285 STRUCT_STAT partial_st;
1286 struct file_struct *back_file = NULL;
1287 int statret, real_ret, stat_errno;
1288 char *fnamecmp, *partialptr, *backupptr = NULL;
1289 char fnamecmpbuf[MAXPATHLEN];
1290 uchar fnamecmp_type;
1291 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1292 int is_dir = !S_ISDIR(file->mode) ? 0
1293 : inc_recurse && ndx != cur_flist->ndx_start - 1 ? -1
1296 if (DEBUG_GTE(GENR, 1))
1297 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
1301 || (is_dir && !implied_dirs && file->flags & FLAG_IMPLIED_DIR))
1303 list_file_entry(file);
1308 if (is_below(file, skip_dir)) {
1310 file->flags |= FLAG_MISSING_DIR;
1311 #ifdef SUPPORT_HARD_LINKS
1312 else if (F_IS_HLINKED(file))
1313 handle_skipped_hlink(file, itemizing, code, f_out);
1320 if (daemon_filter_list.head && (*fname != '.' || fname[1])) {
1321 if (check_filter(&daemon_filter_list, FLOG, fname, is_dir) < 0) {
1324 #ifdef SUPPORT_HARD_LINKS
1325 if (F_IS_HLINKED(file))
1326 handle_skipped_hlink(file, itemizing, code, f_out);
1328 rprintf(FERROR_XFER,
1329 "skipping daemon-excluded %s \"%s\"\n",
1330 is_dir ? "directory" : "file", fname);
1332 goto skipping_dir_contents;
1338 if (dry_run > 1 || (dry_missing_dir && is_below(file, dry_missing_dir))) {
1339 parent_is_dry_missing:
1340 if (fuzzy_dirlist) {
1341 flist_free(fuzzy_dirlist);
1342 fuzzy_dirlist = NULL;
1344 parent_dirname = "";
1346 stat_errno = ENOENT;
1348 const char *dn = file->dirname ? file->dirname : ".";
1349 dry_missing_dir = NULL;
1350 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1351 if (relative_paths && !implied_dirs
1352 && do_stat(dn, &sx.st) < 0) {
1354 goto parent_is_dry_missing;
1355 if (create_directory_path(fname) < 0) {
1356 rsyserr(FERROR_XFER, errno,
1357 "recv_generator: mkdir %s failed",
1361 if (fuzzy_dirlist) {
1362 flist_free(fuzzy_dirlist);
1363 fuzzy_dirlist = NULL;
1366 need_fuzzy_dirlist = 1;
1368 if (!preserve_perms)
1369 dflt_perms = default_perms_for_dir(dn);
1372 parent_dirname = dn;
1374 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
1375 strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1376 fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
1377 need_fuzzy_dirlist = 0;
1380 statret = link_stat(fname, &sx.st, keep_dirlinks && is_dir);
1384 if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
1389 file->flags |= FLAG_MISSING_DIR;
1391 #ifdef SUPPORT_HARD_LINKS
1392 else if (F_IS_HLINKED(file))
1393 handle_skipped_hlink(file, itemizing, code, f_out);
1395 if (INFO_GTE(SKIP, 1)) {
1396 rprintf(FINFO, "not creating new %s \"%s\"\n",
1397 is_dir ? "directory" : "file", fname);
1402 if (statret == 0 && !(sx.st.st_mode & S_IWUSR)
1403 && !am_root && sx.st.st_uid == our_uid)
1404 del_opts |= DEL_NO_UID_WRITE;
1406 if (ignore_existing > 0 && statret == 0
1407 && (!is_dir || !S_ISDIR(sx.st.st_mode))) {
1408 if (INFO_GTE(SKIP, 1) && is_dir >= 0)
1409 rprintf(FINFO, "%s exists\n", fname);
1410 #ifdef SUPPORT_HARD_LINKS
1411 if (F_IS_HLINKED(file))
1412 handle_skipped_hlink(file, itemizing, code, f_out);
1418 if (!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
1421 /* In inc_recurse mode we want to make sure any missing
1422 * directories get created while we're still processing
1423 * the parent dir (which allows us to touch the parent
1424 * dir's mtime right away). We will handle the dir in
1425 * full later (right before we handle its contents). */
1427 && (S_ISDIR(sx.st.st_mode)
1428 || delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0))
1429 goto cleanup; /* Any errors get reported later. */
1430 if (do_mkdir(fname, file->mode & 0700) == 0)
1431 file->flags |= FLAG_DIR_CREATED;
1434 /* The file to be received is a directory, so we need
1435 * to prepare appropriately. If there is already a
1436 * file of that name and it is *not* a directory, then
1437 * we need to delete it. If it doesn't exist, then
1438 * (perhaps recursively) create it. */
1439 if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1440 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0)
1441 goto skipping_dir_contents;
1444 if (dry_run && statret != 0) {
1445 if (!dry_missing_dir)
1446 dry_missing_dir = file;
1447 file->flags |= FLAG_MISSING_DIR;
1451 if (file->flags & FLAG_DIR_CREATED)
1453 if (!preserve_perms) { /* See comment in non-dir code below. */
1454 file->mode = dest_mode(file->mode, sx.st.st_mode,
1455 dflt_perms, statret == 0);
1457 if (statret != 0 && basis_dir[0] != NULL) {
1458 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1467 if (itemizing && f_out != -1) {
1468 itemize(fname, file, ndx, statret, &sx,
1469 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1471 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1472 if (!relative_paths || errno != ENOENT
1473 || create_directory_path(fname) < 0
1474 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
1475 rsyserr(FERROR_XFER, errno,
1476 "recv_generator: mkdir %s failed",
1478 skipping_dir_contents:
1480 "*** Skipping any contents from this failed directory ***\n");
1482 file->flags |= FLAG_MISSING_DIR;
1486 #ifdef SUPPORT_XATTRS
1487 if (preserve_xattrs && statret == 1)
1488 copy_xattrs(fnamecmpbuf, fname);
1490 if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
1491 && INFO_GTE(NAME, 1) && code != FNONE && f_out != -1)
1492 rprintf(code, "%s/\n", fname);
1494 /* We need to ensure that the dirs in the transfer have writable
1495 * permissions during the time we are putting files within them.
1496 * This is then fixed after the transfer is done. */
1498 if (!am_root && !(file->mode & S_IWUSR) && dir_tweaking) {
1499 mode_t mode = file->mode | S_IWUSR;
1500 if (do_chmod(fname, mode) < 0) {
1501 rsyserr(FERROR_XFER, errno,
1502 "failed to modify permissions on %s",
1505 need_retouch_dir_perms = 1;
1509 if (real_ret != 0 && one_file_system)
1510 real_sx.st.st_dev = filesystem_dev;
1512 if (one_file_system) {
1513 uint32 *devp = F_DIR_DEV_P(file);
1514 DEV_MAJOR(devp) = major(real_sx.st.st_dev);
1515 DEV_MINOR(devp) = minor(real_sx.st.st_dev);
1518 else if (delete_during && f_out != -1 && !phase
1519 && !(file->flags & FLAG_MISSING_DIR)) {
1520 if (file->flags & FLAG_CONTENT_DIR)
1521 delete_in_dir(fname, file, &real_sx.st.st_dev);
1523 change_local_filter_dir(fname, strlen(fname), F_DEPTH(file));
1528 /* If we're not preserving permissions, change the file-list's
1529 * mode based on the local permissions and some heuristics. */
1530 if (!preserve_perms) {
1531 int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
1532 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
1536 #ifdef SUPPORT_HARD_LINKS
1537 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1538 && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
1542 if (preserve_links && S_ISLNK(file->mode)) {
1543 #ifdef SUPPORT_LINKS
1544 const char *sl = F_SYMLINK(file);
1545 if (safe_symlinks && unsafe_symlink(sl, fname)) {
1546 if (INFO_GTE(NAME, 1)) {
1548 fname = f_name(file, NULL);
1550 "ignoring unsafe symlink %s -> \"%s\"\n",
1551 full_fname(fname), sl);
1556 char lnk[MAXPATHLEN];
1559 if (!S_ISLNK(sx.st.st_mode))
1561 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1562 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1563 /* The link is pointing to the right place. */
1564 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1566 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1567 #if defined SUPPORT_HARD_LINKS && defined CAN_HARDLINK_SYMLINK
1568 if (preserve_hard_links && F_IS_HLINKED(file))
1569 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1571 if (remove_source_files == 1)
1572 goto return_with_success;
1575 /* Not the right symlink (or not a symlink), so
1577 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_SYMLINK) != 0)
1579 } else if (basis_dir[0] != NULL) {
1580 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1583 #ifndef CAN_HARDLINK_SYMLINK
1585 /* Resort to --copy-dest behavior. */
1595 #ifdef SUPPORT_HARD_LINKS
1596 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1597 cur_flist->in_progress++;
1601 if (do_symlink(sl, fname) != 0) {
1602 rsyserr(FERROR_XFER, errno, "symlink %s -> \"%s\" failed",
1603 full_fname(fname), sl);
1605 set_file_attrs(fname, file, NULL, NULL, 0);
1607 itemize(fname, file, ndx, statret, &sx,
1608 ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
1610 if (code != FNONE && INFO_GTE(NAME, 1))
1611 rprintf(code, "%s -> %s\n", fname, sl);
1612 #ifdef SUPPORT_HARD_LINKS
1613 if (preserve_hard_links && F_IS_HLINKED(file))
1614 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1616 /* This does not check remove_source_files == 1
1617 * because this is one of the items that the old
1618 * --remove-sent-files option would remove. */
1619 if (remove_source_files)
1620 goto return_with_success;
1626 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1627 || (preserve_specials && IS_SPECIAL(file->mode))) {
1628 uint32 *devp = F_RDEV_P(file);
1629 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1632 if (IS_DEVICE(file->mode)) {
1633 if (!IS_DEVICE(sx.st.st_mode))
1635 del_for_flag = DEL_FOR_DEVICE;
1637 if (!IS_SPECIAL(sx.st.st_mode))
1639 del_for_flag = DEL_FOR_SPECIAL;
1642 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
1643 && sx.st.st_rdev == rdev) {
1644 /* The device or special file is identical. */
1645 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1647 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1648 #ifdef SUPPORT_HARD_LINKS
1649 if (preserve_hard_links && F_IS_HLINKED(file))
1650 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1652 if (remove_source_files == 1)
1653 goto return_with_success;
1656 if (delete_item(fname, sx.st.st_mode, del_opts | del_for_flag) != 0)
1658 } else if (basis_dir[0] != NULL) {
1659 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1662 #ifndef CAN_HARDLINK_SPECIAL
1664 /* Resort to --copy-dest behavior. */
1674 #ifdef SUPPORT_HARD_LINKS
1675 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1676 cur_flist->in_progress++;
1680 if (DEBUG_GTE(GENR, 1)) {
1681 rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1682 fname, (int)file->mode,
1683 (long)major(rdev), (long)minor(rdev));
1685 if (do_mknod(fname, file->mode, rdev) < 0) {
1686 rsyserr(FERROR_XFER, errno, "mknod %s failed",
1689 set_file_attrs(fname, file, NULL, NULL, 0);
1691 itemize(fname, file, ndx, statret, &sx,
1692 ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
1694 if (code != FNONE && INFO_GTE(NAME, 1))
1695 rprintf(code, "%s\n", fname);
1696 #ifdef SUPPORT_HARD_LINKS
1697 if (preserve_hard_links && F_IS_HLINKED(file))
1698 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1700 if (remove_source_files == 1)
1701 goto return_with_success;
1706 if (!S_ISREG(file->mode)) {
1708 fname = f_name(file, NULL);
1709 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1713 if (max_size > 0 && F_LENGTH(file) > max_size) {
1714 if (INFO_GTE(SKIP, 1)) {
1716 fname = f_name(file, NULL);
1717 rprintf(FINFO, "%s is over max-size\n", fname);
1721 if (min_size > 0 && F_LENGTH(file) < min_size) {
1722 if (INFO_GTE(SKIP, 1)) {
1724 fname = f_name(file, NULL);
1725 rprintf(FINFO, "%s is under min-size\n", fname);
1730 if (update_only > 0 && statret == 0
1731 && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
1732 if (INFO_GTE(SKIP, 1))
1733 rprintf(FINFO, "%s is newer\n", fname);
1734 #ifdef SUPPORT_HARD_LINKS
1735 if (F_IS_HLINKED(file))
1736 handle_skipped_hlink(file, itemizing, code, f_out);
1742 fnamecmp_type = FNAMECMP_FNAME;
1744 if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
1745 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE) != 0)
1748 stat_errno = ENOENT;
1751 if (statret != 0 && basis_dir[0] != NULL) {
1752 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
1755 if (remove_source_files == 1)
1756 goto return_with_success;
1760 fnamecmp = fnamecmpbuf;
1769 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1770 && link_stat(partialptr, &partial_st, 0) == 0
1771 && S_ISREG(partial_st.st_mode)) {
1773 goto prepare_to_open;
1777 if (statret != 0 && fuzzy_dirlist) {
1778 int j = find_fuzzy(file, fuzzy_dirlist);
1780 fuzzy_file = fuzzy_dirlist->files[j];
1781 f_name(fuzzy_file, fnamecmpbuf);
1782 if (DEBUG_GTE(FUZZY, 1)) {
1783 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1784 fname, fnamecmpbuf);
1786 sx.st.st_size = F_LENGTH(fuzzy_file);
1788 fnamecmp = fnamecmpbuf;
1789 fnamecmp_type = FNAMECMP_FUZZY;
1794 #ifdef SUPPORT_HARD_LINKS
1795 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1796 cur_flist->in_progress++;
1800 if (stat_errno == ENOENT)
1802 rsyserr(FERROR_XFER, stat_errno, "recv_generator: failed to stat %s",
1807 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1809 else if (fnamecmp_type == FNAMECMP_FUZZY)
1811 else if (unchanged_file(fnamecmp, file, &sx.st)) {
1813 do_unlink(partialptr);
1814 handle_partial_dir(partialptr, PDIR_DELETE);
1816 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1818 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
1819 #ifdef SUPPORT_HARD_LINKS
1820 if (preserve_hard_links && F_IS_HLINKED(file))
1821 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1823 if (remove_source_files != 1)
1825 return_with_success:
1827 send_msg_int(MSG_SUCCESS, ndx);
1831 if (append_mode > 0 && sx.st.st_size >= F_LENGTH(file)) {
1832 #ifdef SUPPORT_HARD_LINKS
1833 if (F_IS_HLINKED(file))
1834 handle_skipped_hlink(file, itemizing, code, f_out);
1842 fnamecmp = partialptr;
1843 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1850 if (read_batch || whole_file) {
1851 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1852 if (!(backupptr = get_backup_name(fname)))
1854 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
1855 goto pretend_missing;
1856 if (copy_file(fname, backupptr, -1, back_file->mode, 1) < 0) {
1857 unmake_file(back_file);
1865 if (fuzzy_dirlist) {
1866 int j = flist_find(fuzzy_dirlist, file);
1867 if (j >= 0) /* don't use changing file as future fuzzy basis */
1868 fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
1872 if ((fd = do_open(fnamecmp, O_RDONLY, 0)) < 0) {
1873 rsyserr(FERROR, errno, "failed to open %s, continuing",
1874 full_fname(fnamecmp));
1876 /* pretend the file didn't exist */
1877 #ifdef SUPPORT_HARD_LINKS
1878 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1879 cur_flist->in_progress++;
1883 statret = real_ret = -1;
1887 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1888 if (!(backupptr = get_backup_name(fname))) {
1892 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1894 goto pretend_missing;
1896 if (robust_unlink(backupptr) && errno != ENOENT) {
1897 rsyserr(FERROR_XFER, errno, "unlink %s",
1898 full_fname(backupptr));
1899 unmake_file(back_file);
1904 if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1905 int save_errno = errno ? errno : EINVAL; /* 0 paranoia */
1906 if (errno == ENOENT && make_bak_dir(backupptr) == 0) {
1907 if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0)
1908 save_errno = errno ? errno : save_errno;
1913 rsyserr(FERROR_XFER, save_errno, "open %s", full_fname(backupptr));
1914 unmake_file(back_file);
1920 fnamecmp_type = FNAMECMP_BACKUP;
1923 if (DEBUG_GTE(DELTASUM, 3)) {
1924 rprintf(FINFO, "gen mapped %s of size %s\n",
1925 fnamecmp, big_num(sx.st.st_size));
1928 if (DEBUG_GTE(DELTASUM, 2))
1929 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1932 if (remove_source_files && !delay_updates && !phase && !dry_run)
1933 increment_active_files(ndx, itemizing, code);
1934 if (inc_recurse && !dry_run)
1935 cur_flist->in_progress++;
1936 #ifdef SUPPORT_HARD_LINKS
1937 if (preserve_hard_links && F_IS_HLINKED(file))
1938 file->flags |= FLAG_FILE_SENT;
1940 write_ndx(f_out, ndx);
1942 int iflags = ITEM_TRANSFER;
1943 if (always_checksum > 0)
1944 iflags |= ITEM_REPORT_CHANGE;
1945 if (fnamecmp_type != FNAMECMP_FNAME)
1946 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1947 if (fnamecmp_type == FNAMECMP_FUZZY)
1948 iflags |= ITEM_XNAME_FOLLOWS;
1949 itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
1950 fuzzy_file ? fuzzy_file->basename : NULL);
1955 #ifdef SUPPORT_XATTRS
1956 if (preserve_xattrs)
1957 free_xattr(&real_sx);
1962 #ifdef SUPPORT_HARD_LINKS
1963 if (preserve_hard_links && F_IS_HLINKED(file))
1964 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1971 if (statret != 0 || whole_file)
1972 write_sum_head(f_out, NULL);
1973 else if (sx.st.st_size <= 0) {
1974 write_sum_head(f_out, NULL);
1977 if (generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy) < 0) {
1979 "WARNING: file is too large for checksum sending: %s\n",
1981 write_sum_head(f_out, NULL);
1988 int save_preserve_xattrs = preserve_xattrs;
1991 #ifdef SUPPORT_XATTRS
1992 if (preserve_xattrs) {
1993 copy_xattrs(fname, backupptr);
1994 preserve_xattrs = 0;
1997 set_file_attrs(backupptr, back_file, NULL, NULL, 0);
1998 preserve_xattrs = save_preserve_xattrs;
1999 if (INFO_GTE(BACKUP, 1)) {
2000 rprintf(FINFO, "backed up %s to %s\n",
2003 unmake_file(back_file);
2010 #ifdef SUPPORT_XATTRS
2011 if (preserve_xattrs)
2017 #ifdef SUPPORT_HARD_LINKS
2018 static void handle_skipped_hlink(struct file_struct *file, int itemizing,
2019 enum logcode code, int f_out)
2021 char fbuf[MAXPATHLEN];
2023 struct file_list *save_flist = cur_flist;
2025 /* If we skip the last item in a chain of links and there was a
2026 * prior non-skipped hard-link waiting to finish, finish it now. */
2027 if ((new_last_ndx = skip_hard_link(file, &cur_flist)) < 0)
2030 file = cur_flist->files[new_last_ndx - cur_flist->ndx_start];
2031 cur_flist->in_progress--; /* undo prior increment */
2033 recv_generator(fbuf, file, new_last_ndx, itemizing, code, f_out);
2035 cur_flist = save_flist;
2039 static void touch_up_dirs(struct file_list *flist, int ndx)
2041 static int counter = 0;
2042 struct file_struct *file;
2048 end = flist->used - 1;
2052 /* Fix any directory permissions that were modified during the
2053 * transfer and/or re-set any tweaked modified-time values. */
2054 for (i = start; i <= end; i++, counter++) {
2055 file = flist->files[i];
2056 if (!S_ISDIR(file->mode)
2057 || (!implied_dirs && file->flags & FLAG_IMPLIED_DIR))
2059 if (DEBUG_GTE(TIME, 2)) {
2060 fname = f_name(file, NULL);
2061 rprintf(FINFO, "touch_up_dirs: %s (%d)\n",
2064 if (!F_IS_ACTIVE(file) || file->flags & FLAG_MISSING_DIR
2065 || (!need_retouch_dir_times && file->mode & S_IWUSR))
2067 fname = f_name(file, NULL);
2068 if (!(file->mode & S_IWUSR))
2069 do_chmod(fname, file->mode);
2070 if (need_retouch_dir_times) {
2072 if (link_stat(fname, &st, 0) == 0
2073 && cmp_time(st.st_mtime, file->modtime) != 0)
2074 set_modtime(fname, file->modtime, file->mode);
2076 if (counter >= loopchk_limit) {
2078 maybe_send_keepalive();
2080 maybe_flush_socket(0);
2086 void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
2088 struct file_struct *file;
2089 struct file_list *flist;
2090 char fbuf[MAXPATHLEN];
2094 #ifdef SUPPORT_HARD_LINKS
2095 if (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
2096 flist = flist_for_ndx(ndx, "check_for_finished_files.1");
2097 file = flist->files[ndx - flist->ndx_start];
2098 assert(file->flags & FLAG_HLINKED);
2099 finish_hard_link(file, f_name(file, fbuf), ndx, NULL, itemizing, code, -1);
2100 flist->in_progress--;
2105 if (check_redo && (ndx = get_redo_num()) != -1) {
2106 csum_length = SUM_LENGTH;
2107 max_size = -max_size;
2108 min_size = -min_size;
2109 ignore_existing = -ignore_existing;
2110 ignore_non_existing = -ignore_non_existing;
2111 update_only = -update_only;
2112 always_checksum = -always_checksum;
2113 size_only = -size_only;
2114 append_mode = -append_mode;
2115 make_backups = -make_backups; /* avoid dup backup w/inplace */
2119 cur_flist = flist_for_ndx(ndx, "check_for_finished_files.2");
2121 file = cur_flist->files[ndx - cur_flist->ndx_start];
2123 strlcpy(fbuf, solo_file, sizeof fbuf);
2126 recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
2127 cur_flist->to_redo--;
2131 csum_length = SHORT_SUM_LENGTH;
2132 max_size = -max_size;
2133 min_size = -min_size;
2134 ignore_existing = -ignore_existing;
2135 ignore_non_existing = -ignore_non_existing;
2136 update_only = -update_only;
2137 always_checksum = -always_checksum;
2138 size_only = -size_only;
2139 append_mode = -append_mode;
2140 make_backups = -make_backups;
2145 if (cur_flist == first_flist)
2148 /* We only get here if inc_recurse is enabled. */
2149 if (first_flist->in_progress || first_flist->to_redo)
2153 write_ndx(sock_f_out, NDX_DONE);
2154 maybe_flush_socket(1);
2157 if (delete_during == 2 || !dir_tweaking) {
2158 /* Skip directory touch-up. */
2159 } else if (first_flist->parent_ndx >= 0)
2160 touch_up_dirs(dir_flist, first_flist->parent_ndx);
2162 flist_free(first_flist); /* updates first_flist */
2166 void generate_files(int f_out, const char *local_name)
2168 int i, ndx, next_loopchk = 0;
2169 char fbuf[MAXPATHLEN];
2172 int save_info_flist = info_levels[INFO_FLIST];
2173 int save_info_progress = info_levels[INFO_PROGRESS];
2175 if (protocol_version >= 29) {
2177 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
2178 code = logfile_format_has_i ? FNONE : FLOG;
2179 } else if (am_daemon) {
2180 itemizing = logfile_format_has_i && do_xfers;
2181 maybe_ATTRS_REPORT = ATTRS_REPORT;
2182 code = itemizing || !do_xfers ? FCLIENT : FINFO;
2183 } else if (!am_server) {
2184 itemizing = stdout_format_has_i;
2185 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
2186 code = itemizing ? FNONE : FINFO;
2189 maybe_ATTRS_REPORT = ATTRS_REPORT;
2192 solo_file = local_name;
2193 dir_tweaking = !(list_only || solo_file || dry_run);
2194 need_retouch_dir_times = preserve_times > 1;
2195 loopchk_limit = allowed_lull ? allowed_lull * 5 : 200;
2196 symlink_timeset_failed_flags = ITEM_REPORT_TIME
2197 | (protocol_version >= 30 || !am_server ? ITEM_REPORT_TIMEFAIL : 0);
2198 implied_dirs_are_missing = relative_paths && !implied_dirs && protocol_version < 30;
2200 if (DEBUG_GTE(GENR, 1))
2201 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
2203 if (delete_before && !solo_file && cur_flist->used > 0)
2205 if (delete_during == 2) {
2206 deldelay_size = BIGPATHBUFLEN * 4;
2207 deldelay_buf = new_array(char, deldelay_size);
2209 out_of_memory("delete-delay");
2211 info_levels[INFO_FLIST] = info_levels[INFO_PROGRESS] = 0;
2213 if (append_mode > 0 || whole_file < 0)
2215 if (DEBUG_GTE(FLIST, 1)) {
2216 rprintf(FINFO, "delta-transmission %s\n",
2218 ? "disabled for local transfer or --whole-file"
2222 /* Since we often fill up the outgoing socket and then just sit around
2223 * waiting for the other 2 processes to do their thing, we don't want
2224 * to exit on a timeout. If the data stops flowing, the receiver will
2225 * notice that and let us know via the redo pipe (or its closing). */
2228 dflt_perms = (ACCESSPERMS & ~orig_umask);
2231 #ifdef SUPPORT_HARD_LINKS
2232 if (preserve_hard_links && inc_recurse) {
2233 while (!flist_eof && file_total < FILECNT_LOOKAHEAD/2)
2234 wait_for_receiver();
2238 if (inc_recurse && cur_flist->parent_ndx >= 0) {
2239 struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
2241 strlcpy(fbuf, solo_file, sizeof fbuf);
2244 ndx = cur_flist->ndx_start - 1;
2245 recv_generator(fbuf, fp, ndx, itemizing, code, f_out);
2246 if (delete_during && dry_run < 2 && !list_only
2247 && !(fp->flags & FLAG_MISSING_DIR)) {
2248 if (fp->flags & FLAG_CONTENT_DIR) {
2250 if (one_file_system) {
2251 uint32 *devp = F_DIR_DEV_P(fp);
2252 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
2254 dirdev = MAKEDEV(0, 0);
2255 delete_in_dir(fbuf, fp, &dirdev);
2257 change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(fp));
2260 for (i = cur_flist->low; i <= cur_flist->high; i++) {
2261 struct file_struct *file = cur_flist->sorted[i];
2263 if (!F_IS_ACTIVE(file))
2269 ndx = i + cur_flist->ndx_start;
2272 strlcpy(fbuf, solo_file, sizeof fbuf);
2275 recv_generator(fbuf, file, ndx, itemizing, code, f_out);
2277 check_for_finished_files(itemizing, code, 0);
2279 if (i + cur_flist->ndx_start >= next_loopchk) {
2281 maybe_send_keepalive();
2283 maybe_flush_socket(0);
2284 next_loopchk += loopchk_limit;
2289 write_ndx(f_out, NDX_DONE);
2294 check_for_finished_files(itemizing, code, 1);
2295 if (cur_flist->next || flist_eof)
2297 wait_for_receiver();
2299 } while ((cur_flist = cur_flist->next) != NULL);
2301 if (read_batch && inc_recurse)
2302 write_ndx(f_out, NDX_DONE);
2305 delete_in_dir(NULL, NULL, &dev_zero);
2307 if (DEBUG_GTE(GENR, 1))
2308 rprintf(FINFO, "generate_files phase=%d\n", phase);
2311 check_for_finished_files(itemizing, code, 1);
2314 wait_for_receiver();
2318 if (DEBUG_GTE(GENR, 1))
2319 rprintf(FINFO, "generate_files phase=%d\n", phase);
2321 write_ndx(f_out, NDX_DONE);
2322 write_del_stats(f_out);
2324 /* Reduce round-trip lag-time for a useless delay-updates phase. */
2325 if (protocol_version >= 29 && !delay_updates)
2326 write_ndx(f_out, NDX_DONE);
2328 /* Read MSG_DONE for the redo phase (and any prior messages). */
2330 check_for_finished_files(itemizing, code, 0);
2331 if (msgdone_cnt > 1)
2333 wait_for_receiver();
2336 if (protocol_version >= 29) {
2338 if (DEBUG_GTE(GENR, 1))
2339 rprintf(FINFO, "generate_files phase=%d\n", phase);
2341 write_ndx(f_out, NDX_DONE);
2342 /* Read MSG_DONE for delay-updates phase & prior messages. */
2343 while (msgdone_cnt == 2)
2344 wait_for_receiver();
2347 info_levels[INFO_FLIST] = save_info_flist;
2348 info_levels[INFO_PROGRESS] = save_info_progress;
2350 if (delete_during == 2)
2351 do_delayed_deletions(fbuf);
2352 if (delete_after && !solo_file && file_total > 0)
2355 if ((need_retouch_dir_perms || need_retouch_dir_times)
2356 && dir_tweaking && (!inc_recurse || delete_during == 2))
2357 touch_up_dirs(dir_flist, -1);
2359 if (max_delete >= 0 && skipped_deletes) {
2361 "Deletions stopped due to --max-delete limit (%d skipped)\n",
2363 io_error |= IOERR_DEL_LIMIT;
2366 if (DEBUG_GTE(GENR, 1))
2367 rprintf(FINFO, "generate_files finished\n");