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-2009 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 delete_missing_args;
54 extern int msgdone_cnt;
55 extern int ignore_errors;
56 extern int remove_source_files;
57 extern int delay_updates;
58 extern int update_only;
59 extern int human_readable;
60 extern int ignore_existing;
61 extern int ignore_non_existing;
63 extern int append_mode;
64 extern int make_backups;
65 extern int csum_length;
66 extern int ignore_times;
68 extern OFF_T max_size;
69 extern OFF_T min_size;
72 extern int allowed_lull;
73 extern int sock_f_out;
74 extern int ignore_timeout;
75 extern int protocol_version;
76 extern int file_total;
77 extern int fuzzy_basis;
78 extern int always_checksum;
79 extern int checksum_len;
80 extern char *partial_dir;
81 extern int compare_dest;
84 extern int whole_file;
86 extern int read_batch;
87 extern int safe_symlinks;
88 extern long block_size; /* "long" because popt can't set an int32. */
89 extern int unsort_ndx;
90 extern int max_delete;
91 extern int force_delete;
92 extern int one_file_system;
93 extern int skipped_deletes;
94 extern struct stats stats;
95 extern dev_t filesystem_dev;
96 extern mode_t orig_umask;
98 extern char *basis_dir[MAX_BASIS_DIRS+1];
99 extern struct file_list *cur_flist, *first_flist, *dir_flist;
100 extern struct filter_list_struct daemon_filter_list;
102 int maybe_ATTRS_REPORT = 0;
104 static dev_t dev_zero;
105 static int deldelay_size = 0, deldelay_cnt = 0;
106 static char *deldelay_buf = NULL;
107 static int deldelay_fd = -1;
108 static int loopchk_limit;
109 static int dir_tweaking;
110 static int symlink_timeset_failed_flags;
111 static int need_retouch_dir_times;
112 static int need_retouch_dir_perms;
113 static const char *solo_file = NULL;
116 TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
119 /* Forward declarations. */
120 #ifdef SUPPORT_HARD_LINKS
121 static void handle_skipped_hlink(struct file_struct *file, int itemizing,
122 enum logcode code, int f_out);
125 #define EARLY_DELAY_DONE_MSG() (!delay_updates)
126 #define EARLY_DELETE_DONE_MSG() (!(delete_during == 2 || delete_after))
128 static int start_delete_delay_temp(void)
130 char fnametmp[MAXPATHLEN];
131 int save_dry_run = dry_run;
134 if (!get_tmpname(fnametmp, "deldelay")
135 || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
136 rprintf(FINFO, "NOTE: Unable to create delete-delay temp file%s.\n",
137 inc_recurse ? "" : " -- switching to --delete-after");
139 delete_after = !inc_recurse;
140 dry_run = save_dry_run;
144 dry_run = save_dry_run;
148 static int flush_delete_delay(void)
150 if (deldelay_fd < 0 && !start_delete_delay_temp())
152 if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
153 rsyserr(FERROR, errno, "flush of delete-delay buffer");
155 delete_after = !inc_recurse;
163 static int remember_delete(struct file_struct *file, const char *fname, int flags)
167 if (deldelay_cnt == deldelay_size && !flush_delete_delay())
170 if (flags & DEL_NO_UID_WRITE)
171 deldelay_buf[deldelay_cnt++] = '!';
174 len = snprintf(deldelay_buf + deldelay_cnt,
175 deldelay_size - deldelay_cnt,
177 (int)file->mode, fname, '\0');
178 if ((deldelay_cnt += len) <= deldelay_size)
181 if (!flush_delete_delay())
188 static int read_delay_line(char *buf, int *flags_p)
190 static int read_pos = 0;
192 char *bp, *past_space;
195 for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
196 if (j < deldelay_cnt)
198 if (deldelay_fd < 0) {
203 deldelay_cnt -= read_pos;
204 if (deldelay_cnt == deldelay_size)
206 if (deldelay_cnt && read_pos) {
207 memmove(deldelay_buf, deldelay_buf + read_pos,
210 len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
211 deldelay_size - deldelay_cnt);
215 "ERROR: unexpected EOF in delete-delay file.\n");
220 rsyserr(FERROR, errno,
221 "reading delete-delay file");
228 bp = deldelay_buf + read_pos;
231 *flags_p = DEL_NO_UID_WRITE;
235 if (sscanf(bp, "%x ", &mode) != 1) {
237 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
240 past_space = strchr(bp, ' ') + 1;
241 len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
244 if (len > MAXPATHLEN) {
245 rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
249 /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
250 * instead of returning a pointer to our buffer. */
251 memcpy(buf, past_space, len);
256 static void do_delayed_deletions(char *delbuf)
260 if (deldelay_fd >= 0) {
261 if (deldelay_cnt && !flush_delete_delay())
263 lseek(deldelay_fd, 0, 0);
265 while ((mode = read_delay_line(delbuf, &flags)) >= 0)
266 delete_item(delbuf, mode, flags | DEL_RECURSE);
267 if (deldelay_fd >= 0)
271 /* This function is used to implement per-directory deletion, and is used by
272 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
273 * MAXPATHLEN buffer with the name of the directory in it (the functions we
274 * call will append names onto the end, but the old dir value will be restored
276 static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
278 static int already_warned = 0;
279 struct file_list *dirlist;
280 char delbuf[MAXPATHLEN];
282 int save_uid_ndx = uid_ndx;
285 change_local_filter_dir(NULL, 0, 0);
289 if (DEBUG_GTE(DEL, 2))
290 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
293 maybe_send_keepalive();
295 if (io_error && !ignore_errors) {
299 "IO error encountered -- skipping file deletion\n");
305 change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
307 if (one_file_system) {
308 if (file->flags & FLAG_TOP_DIR)
309 filesystem_dev = *fs_dev;
310 else if (filesystem_dev != *fs_dev)
315 uid_ndx = ++file_extra_cnt;
317 dirlist = get_dirlist(fbuf, dlen, 0);
319 /* If an item in dirlist is not found in flist, delete it
320 * from the filesystem. */
321 for (i = dirlist->used; i--; ) {
322 struct file_struct *fp = dirlist->files[i];
323 if (!F_IS_ACTIVE(fp))
325 if (fp->flags & FLAG_MOUNT_DIR && S_ISDIR(fp->mode)) {
326 if (INFO_GTE(MOUNT, 1))
327 rprintf(FINFO, "cannot delete mount point: %s\n",
331 /* Here we want to match regardless of file type. Replacement
332 * of a file with one of another type is handled separately by
333 * a delete_item call with a DEL_MAKE_ROOM flag. */
334 if (flist_find_ignore_dirness(cur_flist, fp) < 0) {
335 int flags = DEL_RECURSE;
336 if (!(fp->mode & S_IWUSR) && !am_root && (uid_t)F_OWNER(fp) == our_uid)
337 flags |= DEL_NO_UID_WRITE;
339 if (delete_during == 2) {
340 if (!remember_delete(fp, delbuf, flags))
343 delete_item(delbuf, fp->mode, flags);
355 /* This deletes any files on the receiving side that are not present on the
356 * sending side. This is used by --delete-before and --delete-after. */
357 static void do_delete_pass(void)
359 char fbuf[MAXPATHLEN];
363 /* dry_run is incremented when the destination doesn't exist yet. */
364 if (dry_run > 1 || list_only)
367 for (j = 0; j < cur_flist->used; j++) {
368 struct file_struct *file = cur_flist->sorted[j];
372 if (!(file->flags & FLAG_CONTENT_DIR)) {
373 change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(file));
377 if (DEBUG_GTE(DEL, 1) && file->flags & FLAG_TOP_DIR)
378 rprintf(FINFO, "deleting in %s\n", fbuf);
380 if (link_stat(fbuf, &st, keep_dirlinks) < 0
381 || !S_ISDIR(st.st_mode))
384 delete_in_dir(fbuf, file, &st.st_dev);
386 delete_in_dir(NULL, NULL, &dev_zero);
388 if (INFO_GTE(FLIST, 2) && !am_server)
389 rprintf(FINFO, " \r");
392 int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
394 #if !defined HAVE_LUTIMES || !defined HAVE_UTIMES
395 if (S_ISLNK(file->mode)) {
399 if (preserve_times && cmp_time(sxp->st.st_mtime, file->modtime) != 0)
402 if (preserve_perms) {
403 if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
405 } else if (preserve_executability
406 && ((sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0)))
409 if (am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file))
412 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
416 if (preserve_acls && !S_ISLNK(file->mode)) {
417 if (!ACL_READY(*sxp))
419 if (set_acl(NULL, file, sxp) == 0)
423 #ifdef SUPPORT_XATTRS
424 if (preserve_xattrs) {
425 if (!XATTR_READY(*sxp))
426 get_xattr(fname, sxp);
427 if (xattr_diff(file, sxp, 0))
435 void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
436 stat_x *sxp, int32 iflags, uchar fnamecmp_type,
439 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
440 int keep_time = !preserve_times ? 0
441 : S_ISDIR(file->mode) ? preserve_times > 1 :
442 #if defined HAVE_LUTIMES && defined HAVE_UTIMES
445 !S_ISLNK(file->mode);
448 if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
449 iflags |= ITEM_REPORT_SIZE;
450 if (file->flags & FLAG_TIME_FAILED) { /* symlinks only */
451 if (iflags & ITEM_LOCAL_CHANGE)
452 iflags |= symlink_timeset_failed_flags;
454 ? cmp_time(file->modtime, sxp->st.st_mtime) != 0
455 : iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !(iflags & ITEM_MATCHED)
456 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
457 iflags |= ITEM_REPORT_TIME;
458 #if !defined HAVE_LCHMOD && !defined HAVE_SETATTRLIST
459 if (S_ISLNK(file->mode)) {
463 if (preserve_perms) {
464 if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
465 iflags |= ITEM_REPORT_PERMS;
466 } else if (preserve_executability
467 && ((sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0)))
468 iflags |= ITEM_REPORT_PERMS;
469 if (uid_ndx && am_root && (uid_t)F_OWNER(file) != sxp->st.st_uid)
470 iflags |= ITEM_REPORT_OWNER;
471 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
472 && sxp->st.st_gid != (gid_t)F_GROUP(file))
473 iflags |= ITEM_REPORT_GROUP;
475 if (preserve_acls && !S_ISLNK(file->mode)) {
476 if (!ACL_READY(*sxp))
477 get_acl(fnamecmp, sxp);
478 if (set_acl(NULL, file, sxp) == 0)
479 iflags |= ITEM_REPORT_ACL;
482 #ifdef SUPPORT_XATTRS
483 if (preserve_xattrs) {
484 if (!XATTR_READY(*sxp))
485 get_xattr(fnamecmp, sxp);
486 if (xattr_diff(file, sxp, 1))
487 iflags |= ITEM_REPORT_XATTR;
491 #ifdef SUPPORT_XATTRS
492 if (preserve_xattrs && xattr_diff(file, NULL, 1))
493 iflags |= ITEM_REPORT_XATTR;
495 iflags |= ITEM_IS_NEW;
499 if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || INFO_GTE(NAME, 2)
500 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
501 if (protocol_version >= 29) {
503 write_ndx(sock_f_out, ndx);
504 write_shortint(sock_f_out, iflags);
505 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
506 write_byte(sock_f_out, fnamecmp_type);
507 if (iflags & ITEM_XNAME_FOLLOWS)
508 write_vstring(sock_f_out, xname, strlen(xname));
509 #ifdef SUPPORT_XATTRS
510 if (preserve_xattrs && !dry_run
511 && iflags & (ITEM_REPORT_XATTR|ITEM_TRANSFER)) {
512 send_xattr_request(NULL, file,
513 iflags & ITEM_REPORT_XATTR ? sock_f_out : -1);
516 } else if (ndx >= 0) {
517 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
518 log_item(code, file, &stats, iflags, xname);
524 /* Perform our quick-check heuristic for determining if a file is unchanged. */
525 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
527 if (st->st_size != F_LENGTH(file))
530 /* if always checksum is set then we use the checksum instead
531 of the file time to determine whether to sync */
532 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
533 char sum[MAX_DIGEST_LEN];
534 file_checksum(fn, sum, st->st_size);
535 return memcmp(sum, F_SUM(file), checksum_len) == 0;
544 return cmp_time(st->st_mtime, file->modtime) == 0;
549 * set (initialize) the size entries in the per-file sum_struct
550 * calculating dynamic block and checksum sizes.
552 * This is only called from generate_and_send_sums() but is a separate
553 * function to encapsulate the logic.
555 * The block size is a rounded square root of file length.
557 * The checksum size is determined according to:
558 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
559 * provided by Donovan Baarda which gives a probability of rsync
560 * algorithm corrupting data and falling back using the whole md4
563 * This might be made one of several selectable heuristics.
565 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
572 blength = block_size;
573 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
574 blength = BLOCK_SIZE;
576 int32 max_blength = protocol_version < 30 ? OLD_MAX_BLOCK_SIZE : MAX_BLOCK_SIZE;
579 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
580 if (c < 0 || c >= max_blength)
581 blength = max_blength;
586 if (len < (int64)blength * blength)
589 } while (c >= 8); /* round to multiple of 8 */
590 blength = MAX(blength, BLOCK_SIZE);
594 if (protocol_version < 27) {
595 s2length = csum_length;
596 } else if (csum_length == SUM_LENGTH) {
597 s2length = SUM_LENGTH;
600 int b = BLOCKSUM_BIAS;
601 for (l = len; l >>= 1; b += 2) {}
602 for (c = blength; (c >>= 1) && b; b--) {}
603 /* add a bit, subtract rollsum, round up. */
604 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
605 s2length = MAX(s2length, csum_length);
606 s2length = MIN(s2length, SUM_LENGTH);
610 sum->blength = blength;
611 sum->s2length = s2length;
612 sum->remainder = (int32)(len % blength);
613 sum->count = (int32)(l = (len / blength) + (sum->remainder != 0));
615 if ((int64)sum->count != l)
618 if (sum->count && DEBUG_GTE(DELTASUM, 2)) {
620 "count=%s rem=%ld blength=%ld s2length=%d flength=%s\n",
621 big_num(sum->count), (long)sum->remainder, (long)sum->blength,
622 sum->s2length, big_num(sum->flength));
628 * Generate and send a stream of signatures/checksums that describe a buffer
630 * Generate approximately one checksum every block_len bytes.
632 static int generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
635 struct map_struct *mapbuf;
636 struct sum_struct sum;
639 sum_sizes_sqroot(&sum, len);
642 write_sum_head(f_out, &sum);
644 if (append_mode > 0 && f_copy < 0)
648 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
652 for (i = 0; i < sum.count; i++) {
653 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
654 char *map = map_ptr(mapbuf, offset, n1);
655 char sum2[SUM_LENGTH];
662 full_write(f_copy, map, n1);
667 sum1 = get_checksum1(map, n1);
668 get_checksum2(map, n1, sum2);
670 if (DEBUG_GTE(DELTASUM, 3)) {
672 "chunk[%s] offset=%s len=%ld sum1=%08lx\n",
673 big_num(i), big_num(offset - n1), (long)n1,
674 (unsigned long)sum1);
676 write_int(f_out, sum1);
677 write_buf(f_out, sum2, sum.s2length);
687 /* Try to find a filename in the same dir as "fname" with a similar name. */
688 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
690 int fname_len, fname_suf_len;
691 const char *fname_suf, *fname = file->basename;
692 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
693 int j, lowest_j = -1;
695 fname_len = strlen(fname);
696 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
698 for (j = 0; j < dirlist->used; j++) {
699 struct file_struct *fp = dirlist->files[j];
700 const char *suf, *name;
704 if (!S_ISREG(fp->mode) || !F_LENGTH(fp)
705 || fp->flags & FLAG_FILE_SENT)
710 if (F_LENGTH(fp) == F_LENGTH(file)
711 && cmp_time(fp->modtime, file->modtime) == 0) {
712 if (DEBUG_GTE(FUZZY, 2)) {
714 "fuzzy size/modtime match for %s\n",
721 suf = find_filename_suffix(name, len, &suf_len);
723 dist = fuzzy_distance(name, len, fname, fname_len);
724 /* Add some extra weight to how well the suffixes match. */
725 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
727 if (DEBUG_GTE(FUZZY, 2)) {
728 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
729 name, (int)(dist>>16), (int)(dist&0xFFFF));
731 if (dist <= lowest_dist) {
740 /* Copy a file found in our --copy-dest handling. */
741 static int copy_altdest_file(const char *src, const char *dest, struct file_struct *file)
743 char buf[MAXPATHLEN];
744 const char *copy_to, *partialptr;
745 int save_preserve_xattrs = preserve_xattrs;
749 /* Let copy_file open the destination in place. */
753 fd_w = open_tmpfile(buf, dest, file);
758 cleanup_set(copy_to, NULL, NULL, -1, -1);
759 if (copy_file(src, copy_to, fd_w, file->mode, 0) < 0) {
760 if (INFO_GTE(COPY, 1)) {
761 rsyserr(FINFO, errno, "copy_file %s => %s",
762 full_fname(src), copy_to);
764 /* Try to clean up. */
769 partialptr = partial_dir ? partial_dir_fname(dest) : NULL;
770 preserve_xattrs = 0; /* xattrs were copied with file */
771 ok = finish_transfer(dest, copy_to, src, partialptr, file, 1, 0);
772 preserve_xattrs = save_preserve_xattrs;
777 /* This is only called for regular files. We return -2 if we've finished
778 * handling the file, -1 if no dest-linking occurred, or a non-negative
779 * value if we found an alternate basis file. */
780 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
781 char *cmpbuf, stat_x *sxp, int itemizing,
789 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
790 if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
792 switch (match_level) {
798 if (!unchanged_file(cmpbuf, file, &sxp->st))
804 if (!unchanged_attrs(cmpbuf, file, sxp))
811 } while (basis_dir[++j] != NULL);
816 if (j != best_match) {
818 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
819 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
823 if (match_level == 3 && !copy_dest) {
824 #ifdef SUPPORT_HARD_LINKS
826 if (!hard_link_one(file, fname, cmpbuf, 1))
828 if (preserve_hard_links && F_IS_HLINKED(file))
829 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, j);
830 if (!maybe_ATTRS_REPORT && (INFO_GTE(NAME, 2) || stdout_format_has_i > 1)) {
831 itemize(cmpbuf, file, ndx, 1, sxp,
832 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
838 itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
839 if (INFO_GTE(NAME, 2) && maybe_ATTRS_REPORT)
840 rprintf(FCLIENT, "%s is uptodate\n", fname);
844 if (match_level >= 2) {
845 #ifdef SUPPORT_HARD_LINKS
846 try_a_copy: /* Copy the file locally. */
848 if (!dry_run && copy_altdest_file(cmpbuf, fname, file) < 0)
851 itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
852 if (maybe_ATTRS_REPORT
853 && ((!itemizing && INFO_GTE(NAME, 1) && match_level == 2)
854 || (INFO_GTE(NAME, 2) && match_level == 3))) {
855 code = match_level == 3 ? FCLIENT : FINFO;
856 rprintf(code, "%s%s\n", fname,
857 match_level == 3 ? " is uptodate" : "");
859 #ifdef SUPPORT_HARD_LINKS
860 if (preserve_hard_links && F_IS_HLINKED(file))
861 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, -1);
866 return FNAMECMP_BASIS_DIR_LOW + j;
869 /* This is only called for non-regular files. We return -2 if we've finished
870 * handling the file, or -1 if no dest-linking occurred, or a non-negative
871 * value if we found an alternate basis file. */
872 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
873 char *cmpbuf, stat_x *sxp, int itemizing,
876 char lnk[MAXPATHLEN];
879 enum nonregtype type;
883 #ifndef SUPPORT_LINKS
884 if (S_ISLNK(file->mode))
887 if (S_ISDIR(file->mode)) {
889 } else if (IS_SPECIAL(file->mode))
891 else if (IS_DEVICE(file->mode))
894 else if (S_ISLNK(file->mode))
899 "internal: try_dests_non() called with invalid mode (%o)\n",
901 exit_cleanup(RERR_UNSUPPORTED);
905 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
906 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
910 if (!S_ISDIR(sxp->st.st_mode))
914 if (!IS_SPECIAL(sxp->st.st_mode))
918 if (!IS_DEVICE(sxp->st.st_mode))
923 if (!S_ISLNK(sxp->st.st_mode))
928 if (match_level < 1) {
937 devp = F_RDEV_P(file);
938 if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
943 if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
946 if (strcmp(lnk, F_SYMLINK(file)) != 0)
951 if (match_level < 2) {
955 if (unchanged_attrs(cmpbuf, file, sxp)) {
960 } while (basis_dir[++j] != NULL);
965 if (j != best_match) {
967 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
968 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
972 if (match_level == 3) {
973 #ifdef SUPPORT_HARD_LINKS
975 #ifndef CAN_HARDLINK_SYMLINK
976 && !S_ISLNK(file->mode)
978 #ifndef CAN_HARDLINK_SPECIAL
979 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
981 && !S_ISDIR(file->mode)) {
982 if (do_link(cmpbuf, fname) < 0) {
983 rsyserr(FERROR_XFER, errno,
984 "failed to hard-link %s with %s",
988 if (preserve_hard_links && F_IS_HLINKED(file))
989 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
993 if (itemizing && stdout_format_has_i
994 && (INFO_GTE(NAME, 2) || stdout_format_has_i > 1)) {
995 int chg = compare_dest && type != TYPE_DIR ? 0
996 : ITEM_LOCAL_CHANGE + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
997 char *lp = match_level == 3 ? "" : NULL;
998 itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
1000 if (INFO_GTE(NAME, 2) && maybe_ATTRS_REPORT) {
1001 rprintf(FCLIENT, "%s%s is uptodate\n",
1002 fname, type == TYPE_DIR ? "/" : "");
1010 static void list_file_entry(struct file_struct *f)
1012 char permbuf[PERMSTRING_SIZE];
1014 int colwidth = human_readable ? 14 : 11;
1016 if (!F_IS_ACTIVE(f)) {
1017 /* this can happen if duplicate names were removed */
1021 permstring(permbuf, f->mode);
1024 /* TODO: indicate '+' if the entry has an ACL. */
1026 #ifdef SUPPORT_LINKS
1027 if (preserve_links && S_ISLNK(f->mode)) {
1028 rprintf(FINFO, "%s %*s %s %s -> %s\n",
1029 permbuf, colwidth, comma_num(len),
1030 timestring(f->modtime), f_name(f, NULL),
1035 rprintf(FINFO, "%s %*s %s %s\n",
1036 permbuf, colwidth, comma_num(len),
1037 timestring(f->modtime), f_name(f, NULL));
1041 static int phase = 0;
1042 static int dflt_perms;
1044 static int implied_dirs_are_missing;
1045 /* Helper for recv_generator's skip_dir and dry_missing_dir tests. */
1046 static BOOL is_below(struct file_struct *file, struct file_struct *subtree)
1048 return F_DEPTH(file) > F_DEPTH(subtree)
1049 && (!implied_dirs_are_missing || f_name_has_prefix(file, subtree));
1052 /* Acts on the indicated item in cur_flist whose name is fname. If a dir,
1053 * make sure it exists, and has the right permissions/timestamp info. For
1054 * all other non-regular files (symlinks, etc.) we create them here. For
1055 * regular files that have changed, we try to find a basis file and then
1056 * start sending checksums. The ndx is the file's unique index value.
1058 * The fname parameter must point to a MAXPATHLEN buffer! (e.g it gets
1059 * passed to delete_item(), which can use it during a recursive delete.)
1061 * Note that f_out is set to -1 when doing final directory-permission and
1062 * modification-time repair. */
1063 static void recv_generator(char *fname, struct file_struct *file, int ndx,
1064 int itemizing, enum logcode code, int f_out)
1066 static const char *parent_dirname = "";
1067 /* Missing dir not created due to --dry-run; will still be scanned. */
1068 static struct file_struct *dry_missing_dir = NULL;
1069 /* Missing dir whose contents are skipped altogether due to
1070 * --ignore-non-existing, daemon exclude, or mkdir failure. */
1071 static struct file_struct *skip_dir = NULL;
1072 static struct file_list *fuzzy_dirlist = NULL;
1073 static int need_fuzzy_dirlist = 0;
1074 struct file_struct *fuzzy_file = NULL;
1075 int fd = -1, f_copy = -1;
1077 STRUCT_STAT partial_st;
1078 struct file_struct *back_file = NULL;
1079 int statret, real_ret, stat_errno;
1080 char *fnamecmp, *partialptr, *backupptr = NULL;
1081 char fnamecmpbuf[MAXPATHLEN];
1082 uchar fnamecmp_type;
1083 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1084 int is_dir = !S_ISDIR(file->mode) ? 0
1085 : inc_recurse && ndx != cur_flist->ndx_start - 1 ? -1
1088 if (DEBUG_GTE(GENR, 1))
1089 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
1093 || (is_dir && !implied_dirs && file->flags & FLAG_IMPLIED_DIR))
1095 list_file_entry(file);
1100 if (is_below(file, skip_dir)) {
1102 file->flags |= FLAG_MISSING_DIR;
1103 #ifdef SUPPORT_HARD_LINKS
1104 else if (F_IS_HLINKED(file))
1105 handle_skipped_hlink(file, itemizing, code, f_out);
1112 if (daemon_filter_list.head && (*fname != '.' || fname[1])) {
1113 if (check_filter(&daemon_filter_list, FLOG, fname, is_dir) < 0) {
1116 #ifdef SUPPORT_HARD_LINKS
1117 if (F_IS_HLINKED(file))
1118 handle_skipped_hlink(file, itemizing, code, f_out);
1120 rprintf(FERROR_XFER,
1121 "skipping daemon-excluded %s \"%s\"\n",
1122 is_dir ? "directory" : "file", fname);
1124 goto skipping_dir_contents;
1130 if (dry_run > 1 || (dry_missing_dir && is_below(file, dry_missing_dir))) {
1131 parent_is_dry_missing:
1132 if (fuzzy_dirlist) {
1133 flist_free(fuzzy_dirlist);
1134 fuzzy_dirlist = NULL;
1136 parent_dirname = "";
1138 stat_errno = ENOENT;
1140 const char *dn = file->dirname ? file->dirname : ".";
1141 dry_missing_dir = NULL;
1142 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1143 if (relative_paths && !implied_dirs
1144 && do_stat(dn, &sx.st) < 0) {
1146 goto parent_is_dry_missing;
1147 if (create_directory_path(fname) < 0) {
1148 rsyserr(FERROR_XFER, errno,
1149 "recv_generator: mkdir %s failed",
1153 if (fuzzy_dirlist) {
1154 flist_free(fuzzy_dirlist);
1155 fuzzy_dirlist = NULL;
1158 need_fuzzy_dirlist = 1;
1160 if (!preserve_perms)
1161 dflt_perms = default_perms_for_dir(dn);
1164 parent_dirname = dn;
1166 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
1167 strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1168 fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
1169 need_fuzzy_dirlist = 0;
1172 statret = link_stat(fname, &sx.st, keep_dirlinks && is_dir);
1176 if (delete_missing_args && file->mode == 0) {
1178 delete_item(fname, sx.st.st_mode, del_opts);
1182 if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
1187 file->flags |= FLAG_MISSING_DIR;
1189 #ifdef SUPPORT_HARD_LINKS
1190 else if (F_IS_HLINKED(file))
1191 handle_skipped_hlink(file, itemizing, code, f_out);
1193 if (INFO_GTE(SKIP, 1)) {
1194 rprintf(FINFO, "not creating new %s \"%s\"\n",
1195 is_dir ? "directory" : "file", fname);
1200 if (statret == 0 && !(sx.st.st_mode & S_IWUSR)
1201 && !am_root && sx.st.st_uid == our_uid)
1202 del_opts |= DEL_NO_UID_WRITE;
1204 if (ignore_existing > 0 && statret == 0
1205 && (!is_dir || !S_ISDIR(sx.st.st_mode))) {
1206 if (INFO_GTE(SKIP, 1) && is_dir >= 0)
1207 rprintf(FINFO, "%s exists\n", fname);
1208 #ifdef SUPPORT_HARD_LINKS
1209 if (F_IS_HLINKED(file))
1210 handle_skipped_hlink(file, itemizing, code, f_out);
1216 if (!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
1219 /* In inc_recurse mode we want to make sure any missing
1220 * directories get created while we're still processing
1221 * the parent dir (which allows us to touch the parent
1222 * dir's mtime right away). We will handle the dir in
1223 * full later (right before we handle its contents). */
1225 && (S_ISDIR(sx.st.st_mode)
1226 || delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0))
1227 goto cleanup; /* Any errors get reported later. */
1228 if (do_mkdir(fname, file->mode & 0700) == 0)
1229 file->flags |= FLAG_DIR_CREATED;
1232 /* The file to be received is a directory, so we need
1233 * to prepare appropriately. If there is already a
1234 * file of that name and it is *not* a directory, then
1235 * we need to delete it. If it doesn't exist, then
1236 * (perhaps recursively) create it. */
1237 if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1238 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0)
1239 goto skipping_dir_contents;
1242 if (dry_run && statret != 0) {
1243 if (!dry_missing_dir)
1244 dry_missing_dir = file;
1245 file->flags |= FLAG_MISSING_DIR;
1249 if (file->flags & FLAG_DIR_CREATED)
1251 if (!preserve_perms) { /* See comment in non-dir code below. */
1252 file->mode = dest_mode(file->mode, sx.st.st_mode,
1253 dflt_perms, statret == 0);
1255 if (statret != 0 && basis_dir[0] != NULL) {
1256 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1265 if (itemizing && f_out != -1) {
1266 itemize(fname, file, ndx, statret, &sx,
1267 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1269 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1270 if (!relative_paths || errno != ENOENT
1271 || create_directory_path(fname) < 0
1272 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
1273 rsyserr(FERROR_XFER, errno,
1274 "recv_generator: mkdir %s failed",
1276 skipping_dir_contents:
1278 "*** Skipping any contents from this failed directory ***\n");
1280 file->flags |= FLAG_MISSING_DIR;
1284 #ifdef SUPPORT_XATTRS
1285 if (preserve_xattrs && statret == 1)
1286 copy_xattrs(fnamecmpbuf, fname);
1288 if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
1289 && INFO_GTE(NAME, 1) && code != FNONE && f_out != -1)
1290 rprintf(code, "%s/\n", fname);
1292 /* We need to ensure that the dirs in the transfer have writable
1293 * permissions during the time we are putting files within them.
1294 * This is then fixed after the transfer is done. */
1296 if (!am_root && !(file->mode & S_IWUSR) && dir_tweaking) {
1297 mode_t mode = file->mode | S_IWUSR;
1298 if (do_chmod(fname, mode) < 0) {
1299 rsyserr(FERROR_XFER, errno,
1300 "failed to modify permissions on %s",
1303 need_retouch_dir_perms = 1;
1307 if (real_ret != 0 && one_file_system)
1308 real_sx.st.st_dev = filesystem_dev;
1310 if (one_file_system) {
1311 uint32 *devp = F_DIR_DEV_P(file);
1312 DEV_MAJOR(devp) = major(real_sx.st.st_dev);
1313 DEV_MINOR(devp) = minor(real_sx.st.st_dev);
1316 else if (delete_during && f_out != -1 && !phase
1317 && !(file->flags & FLAG_MISSING_DIR)) {
1318 if (file->flags & FLAG_CONTENT_DIR)
1319 delete_in_dir(fname, file, &real_sx.st.st_dev);
1321 change_local_filter_dir(fname, strlen(fname), F_DEPTH(file));
1326 /* If we're not preserving permissions, change the file-list's
1327 * mode based on the local permissions and some heuristics. */
1328 if (!preserve_perms) {
1329 int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
1330 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
1334 #ifdef SUPPORT_HARD_LINKS
1335 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1336 && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
1340 if (preserve_links && S_ISLNK(file->mode)) {
1341 #ifdef SUPPORT_LINKS
1342 const char *sl = F_SYMLINK(file);
1343 if (safe_symlinks && unsafe_symlink(sl, fname)) {
1344 if (INFO_GTE(NAME, 1)) {
1346 fname = f_name(file, NULL);
1348 "ignoring unsafe symlink %s -> \"%s\"\n",
1349 full_fname(fname), sl);
1354 char lnk[MAXPATHLEN];
1357 if (!S_ISLNK(sx.st.st_mode))
1359 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1360 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1361 /* The link is pointing to the right place. */
1362 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1364 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1365 #if defined SUPPORT_HARD_LINKS && defined CAN_HARDLINK_SYMLINK
1366 if (preserve_hard_links && F_IS_HLINKED(file))
1367 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1369 if (remove_source_files == 1)
1370 goto return_with_success;
1373 /* Not the right symlink (or not a symlink), so
1375 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_SYMLINK) != 0)
1377 } else if (basis_dir[0] != NULL) {
1378 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1381 #ifndef CAN_HARDLINK_SYMLINK
1383 /* Resort to --copy-dest behavior. */
1393 #ifdef SUPPORT_HARD_LINKS
1394 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1395 cur_flist->in_progress++;
1399 if (do_symlink(sl, fname) != 0) {
1400 rsyserr(FERROR_XFER, errno, "symlink %s -> \"%s\" failed",
1401 full_fname(fname), sl);
1403 set_file_attrs(fname, file, NULL, NULL, 0);
1405 itemize(fname, file, ndx, statret, &sx,
1406 ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
1408 if (code != FNONE && INFO_GTE(NAME, 1))
1409 rprintf(code, "%s -> %s\n", fname, sl);
1410 #ifdef SUPPORT_HARD_LINKS
1411 if (preserve_hard_links && F_IS_HLINKED(file))
1412 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1414 /* This does not check remove_source_files == 1
1415 * because this is one of the items that the old
1416 * --remove-sent-files option would remove. */
1417 if (remove_source_files)
1418 goto return_with_success;
1424 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1425 || (preserve_specials && IS_SPECIAL(file->mode))) {
1426 uint32 *devp = F_RDEV_P(file);
1427 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1430 if (IS_DEVICE(file->mode)) {
1431 if (!IS_DEVICE(sx.st.st_mode))
1433 del_for_flag = DEL_FOR_DEVICE;
1435 if (!IS_SPECIAL(sx.st.st_mode))
1437 del_for_flag = DEL_FOR_SPECIAL;
1440 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
1441 && sx.st.st_rdev == rdev) {
1442 /* The device or special file is identical. */
1443 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1445 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1446 #ifdef SUPPORT_HARD_LINKS
1447 if (preserve_hard_links && F_IS_HLINKED(file))
1448 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1450 if (remove_source_files == 1)
1451 goto return_with_success;
1454 if (delete_item(fname, sx.st.st_mode, del_opts | del_for_flag) != 0)
1456 } else if (basis_dir[0] != NULL) {
1457 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1460 #ifndef CAN_HARDLINK_SPECIAL
1462 /* Resort to --copy-dest behavior. */
1472 #ifdef SUPPORT_HARD_LINKS
1473 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1474 cur_flist->in_progress++;
1478 if (DEBUG_GTE(GENR, 1)) {
1479 rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1480 fname, (int)file->mode,
1481 (long)major(rdev), (long)minor(rdev));
1483 if (do_mknod(fname, file->mode, rdev) < 0) {
1484 rsyserr(FERROR_XFER, errno, "mknod %s failed",
1487 set_file_attrs(fname, file, NULL, NULL, 0);
1489 itemize(fname, file, ndx, statret, &sx,
1490 ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
1492 if (code != FNONE && INFO_GTE(NAME, 1))
1493 rprintf(code, "%s\n", fname);
1494 #ifdef SUPPORT_HARD_LINKS
1495 if (preserve_hard_links && F_IS_HLINKED(file))
1496 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1498 if (remove_source_files == 1)
1499 goto return_with_success;
1504 if (!S_ISREG(file->mode)) {
1506 fname = f_name(file, NULL);
1507 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1511 if (max_size > 0 && F_LENGTH(file) > max_size) {
1512 if (INFO_GTE(SKIP, 1)) {
1514 fname = f_name(file, NULL);
1515 rprintf(FINFO, "%s is over max-size\n", fname);
1519 if (min_size > 0 && F_LENGTH(file) < min_size) {
1520 if (INFO_GTE(SKIP, 1)) {
1522 fname = f_name(file, NULL);
1523 rprintf(FINFO, "%s is under min-size\n", fname);
1528 if (update_only > 0 && statret == 0
1529 && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
1530 if (INFO_GTE(SKIP, 1))
1531 rprintf(FINFO, "%s is newer\n", fname);
1532 #ifdef SUPPORT_HARD_LINKS
1533 if (F_IS_HLINKED(file))
1534 handle_skipped_hlink(file, itemizing, code, f_out);
1540 fnamecmp_type = FNAMECMP_FNAME;
1542 if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
1543 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE) != 0)
1546 stat_errno = ENOENT;
1549 if (statret != 0 && basis_dir[0] != NULL) {
1550 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
1553 if (remove_source_files == 1)
1554 goto return_with_success;
1558 fnamecmp = fnamecmpbuf;
1567 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1568 && link_stat(partialptr, &partial_st, 0) == 0
1569 && S_ISREG(partial_st.st_mode)) {
1571 goto prepare_to_open;
1575 if (statret != 0 && fuzzy_dirlist) {
1576 int j = find_fuzzy(file, fuzzy_dirlist);
1578 fuzzy_file = fuzzy_dirlist->files[j];
1579 f_name(fuzzy_file, fnamecmpbuf);
1580 if (DEBUG_GTE(FUZZY, 1)) {
1581 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1582 fname, fnamecmpbuf);
1584 sx.st.st_size = F_LENGTH(fuzzy_file);
1586 fnamecmp = fnamecmpbuf;
1587 fnamecmp_type = FNAMECMP_FUZZY;
1592 #ifdef SUPPORT_HARD_LINKS
1593 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1594 cur_flist->in_progress++;
1598 if (stat_errno == ENOENT)
1600 rsyserr(FERROR_XFER, stat_errno, "recv_generator: failed to stat %s",
1605 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1607 else if (fnamecmp_type == FNAMECMP_FUZZY)
1609 else if (unchanged_file(fnamecmp, file, &sx.st)) {
1611 do_unlink(partialptr);
1612 handle_partial_dir(partialptr, PDIR_DELETE);
1614 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1616 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
1617 #ifdef SUPPORT_HARD_LINKS
1618 if (preserve_hard_links && F_IS_HLINKED(file))
1619 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1621 if (remove_source_files != 1)
1623 return_with_success:
1625 send_msg_int(MSG_SUCCESS, ndx);
1629 if (append_mode > 0 && sx.st.st_size >= F_LENGTH(file)) {
1630 #ifdef SUPPORT_HARD_LINKS
1631 if (F_IS_HLINKED(file))
1632 handle_skipped_hlink(file, itemizing, code, f_out);
1640 fnamecmp = partialptr;
1641 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1648 if (read_batch || whole_file) {
1649 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1650 if (!(backupptr = get_backup_name(fname)))
1652 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
1653 goto pretend_missing;
1654 if (copy_file(fname, backupptr, -1, back_file->mode, 1) < 0) {
1655 unmake_file(back_file);
1663 if (fuzzy_dirlist) {
1664 int j = flist_find(fuzzy_dirlist, file);
1665 if (j >= 0) /* don't use changing file as future fuzzy basis */
1666 fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
1670 if ((fd = do_open(fnamecmp, O_RDONLY, 0)) < 0) {
1671 rsyserr(FERROR, errno, "failed to open %s, continuing",
1672 full_fname(fnamecmp));
1674 /* pretend the file didn't exist */
1675 #ifdef SUPPORT_HARD_LINKS
1676 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1677 cur_flist->in_progress++;
1681 statret = real_ret = -1;
1685 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1686 if (!(backupptr = get_backup_name(fname))) {
1690 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1692 goto pretend_missing;
1694 if (robust_unlink(backupptr) && errno != ENOENT) {
1695 rsyserr(FERROR_XFER, errno, "unlink %s",
1696 full_fname(backupptr));
1697 unmake_file(back_file);
1702 if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1703 int save_errno = errno ? errno : EINVAL; /* 0 paranoia */
1704 if (errno == ENOENT && make_bak_dir(backupptr) == 0) {
1705 if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0)
1706 save_errno = errno ? errno : save_errno;
1711 rsyserr(FERROR_XFER, save_errno, "open %s", full_fname(backupptr));
1712 unmake_file(back_file);
1718 fnamecmp_type = FNAMECMP_BACKUP;
1721 if (DEBUG_GTE(DELTASUM, 3)) {
1722 rprintf(FINFO, "gen mapped %s of size %s\n",
1723 fnamecmp, big_num(sx.st.st_size));
1726 if (DEBUG_GTE(DELTASUM, 2))
1727 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1730 if (remove_source_files && !delay_updates && !phase && !dry_run)
1731 increment_active_files(ndx, itemizing, code);
1732 if (inc_recurse && !dry_run)
1733 cur_flist->in_progress++;
1734 #ifdef SUPPORT_HARD_LINKS
1735 if (preserve_hard_links && F_IS_HLINKED(file))
1736 file->flags |= FLAG_FILE_SENT;
1738 write_ndx(f_out, ndx);
1740 int iflags = ITEM_TRANSFER;
1741 if (always_checksum > 0)
1742 iflags |= ITEM_REPORT_CHANGE;
1743 if (fnamecmp_type != FNAMECMP_FNAME)
1744 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1745 if (fnamecmp_type == FNAMECMP_FUZZY)
1746 iflags |= ITEM_XNAME_FOLLOWS;
1747 itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
1748 fuzzy_file ? fuzzy_file->basename : NULL);
1753 #ifdef SUPPORT_XATTRS
1754 if (preserve_xattrs)
1755 free_xattr(&real_sx);
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);
1769 if (statret != 0 || whole_file)
1770 write_sum_head(f_out, NULL);
1771 else if (sx.st.st_size <= 0) {
1772 write_sum_head(f_out, NULL);
1775 if (generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy) < 0) {
1777 "WARNING: file is too large for checksum sending: %s\n",
1779 write_sum_head(f_out, NULL);
1786 int save_preserve_xattrs = preserve_xattrs;
1789 #ifdef SUPPORT_XATTRS
1790 if (preserve_xattrs) {
1791 copy_xattrs(fname, backupptr);
1792 preserve_xattrs = 0;
1795 set_file_attrs(backupptr, back_file, NULL, NULL, 0);
1796 preserve_xattrs = save_preserve_xattrs;
1797 if (INFO_GTE(BACKUP, 1)) {
1798 rprintf(FINFO, "backed up %s to %s\n",
1801 unmake_file(back_file);
1808 #ifdef SUPPORT_XATTRS
1809 if (preserve_xattrs)
1815 #ifdef SUPPORT_HARD_LINKS
1816 static void handle_skipped_hlink(struct file_struct *file, int itemizing,
1817 enum logcode code, int f_out)
1819 char fbuf[MAXPATHLEN];
1821 struct file_list *save_flist = cur_flist;
1823 /* If we skip the last item in a chain of links and there was a
1824 * prior non-skipped hard-link waiting to finish, finish it now. */
1825 if ((new_last_ndx = skip_hard_link(file, &cur_flist)) < 0)
1828 file = cur_flist->files[new_last_ndx - cur_flist->ndx_start];
1829 cur_flist->in_progress--; /* undo prior increment */
1831 recv_generator(fbuf, file, new_last_ndx, itemizing, code, f_out);
1833 cur_flist = save_flist;
1837 static void touch_up_dirs(struct file_list *flist, int ndx)
1839 static int counter = 0;
1840 struct file_struct *file;
1846 end = flist->used - 1;
1850 /* Fix any directory permissions that were modified during the
1851 * transfer and/or re-set any tweaked modified-time values. */
1852 for (i = start; i <= end; i++, counter++) {
1853 file = flist->files[i];
1854 if (!S_ISDIR(file->mode)
1855 || (!implied_dirs && file->flags & FLAG_IMPLIED_DIR))
1857 if (DEBUG_GTE(TIME, 2)) {
1858 fname = f_name(file, NULL);
1859 rprintf(FINFO, "touch_up_dirs: %s (%d)\n",
1862 if (!F_IS_ACTIVE(file) || file->flags & FLAG_MISSING_DIR
1863 || (!need_retouch_dir_times && file->mode & S_IWUSR))
1865 fname = f_name(file, NULL);
1866 if (!(file->mode & S_IWUSR))
1867 do_chmod(fname, file->mode);
1868 if (need_retouch_dir_times) {
1870 if (link_stat(fname, &st, 0) == 0
1871 && cmp_time(st.st_mtime, file->modtime) != 0)
1872 set_modtime(fname, file->modtime, file->mode);
1874 if (counter >= loopchk_limit) {
1876 maybe_send_keepalive();
1878 maybe_flush_socket(0);
1884 void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
1886 struct file_struct *file;
1887 struct file_list *flist;
1888 char fbuf[MAXPATHLEN];
1892 #ifdef SUPPORT_HARD_LINKS
1893 if (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
1894 flist = flist_for_ndx(ndx, "check_for_finished_files.1");
1895 file = flist->files[ndx - flist->ndx_start];
1896 assert(file->flags & FLAG_HLINKED);
1897 finish_hard_link(file, f_name(file, fbuf), ndx, NULL, itemizing, code, -1);
1898 flist->in_progress--;
1903 if (check_redo && (ndx = get_redo_num()) != -1) {
1904 csum_length = SUM_LENGTH;
1905 max_size = -max_size;
1906 min_size = -min_size;
1907 ignore_existing = -ignore_existing;
1908 ignore_non_existing = -ignore_non_existing;
1909 update_only = -update_only;
1910 always_checksum = -always_checksum;
1911 size_only = -size_only;
1912 append_mode = -append_mode;
1913 make_backups = -make_backups; /* avoid dup backup w/inplace */
1917 cur_flist = flist_for_ndx(ndx, "check_for_finished_files.2");
1919 file = cur_flist->files[ndx - cur_flist->ndx_start];
1921 strlcpy(fbuf, solo_file, sizeof fbuf);
1924 recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
1925 cur_flist->to_redo--;
1929 csum_length = SHORT_SUM_LENGTH;
1930 max_size = -max_size;
1931 min_size = -min_size;
1932 ignore_existing = -ignore_existing;
1933 ignore_non_existing = -ignore_non_existing;
1934 update_only = -update_only;
1935 always_checksum = -always_checksum;
1936 size_only = -size_only;
1937 append_mode = -append_mode;
1938 make_backups = -make_backups;
1943 if (cur_flist == first_flist)
1946 /* We only get here if inc_recurse is enabled. */
1947 if (first_flist->in_progress || first_flist->to_redo)
1950 write_ndx(sock_f_out, NDX_DONE);
1952 maybe_flush_socket(1);
1954 if (delete_during == 2 || !dir_tweaking) {
1955 /* Skip directory touch-up. */
1956 } else if (first_flist->parent_ndx >= 0)
1957 touch_up_dirs(dir_flist, first_flist->parent_ndx);
1959 flist_free(first_flist); /* updates first_flist */
1963 void generate_files(int f_out, const char *local_name)
1965 int i, ndx, next_loopchk = 0;
1966 char fbuf[MAXPATHLEN];
1969 int save_info_flist = info_levels[INFO_FLIST];
1970 int save_info_progress = info_levels[INFO_PROGRESS];
1972 if (protocol_version >= 29) {
1974 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1975 code = logfile_format_has_i ? FNONE : FLOG;
1976 } else if (am_daemon) {
1977 itemizing = logfile_format_has_i && do_xfers;
1978 maybe_ATTRS_REPORT = ATTRS_REPORT;
1979 code = itemizing || !do_xfers ? FCLIENT : FINFO;
1980 } else if (!am_server) {
1981 itemizing = stdout_format_has_i;
1982 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1983 code = itemizing ? FNONE : FINFO;
1986 maybe_ATTRS_REPORT = ATTRS_REPORT;
1989 solo_file = local_name;
1990 dir_tweaking = !(list_only || solo_file || dry_run);
1991 need_retouch_dir_times = preserve_times > 1;
1992 loopchk_limit = allowed_lull ? allowed_lull * 5 : 200;
1993 symlink_timeset_failed_flags = ITEM_REPORT_TIME
1994 | (protocol_version >= 30 || !am_server ? ITEM_REPORT_TIMEFAIL : 0);
1995 implied_dirs_are_missing = relative_paths && !implied_dirs && protocol_version < 30;
1997 if (DEBUG_GTE(GENR, 1))
1998 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
2000 if (delete_before && !solo_file && cur_flist->used > 0)
2002 if (delete_during == 2) {
2003 deldelay_size = BIGPATHBUFLEN * 4;
2004 deldelay_buf = new_array(char, deldelay_size);
2006 out_of_memory("delete-delay");
2008 info_levels[INFO_FLIST] = info_levels[INFO_PROGRESS] = 0;
2010 if (append_mode > 0 || whole_file < 0)
2012 if (DEBUG_GTE(FLIST, 1)) {
2013 rprintf(FINFO, "delta-transmission %s\n",
2015 ? "disabled for local transfer or --whole-file"
2019 /* Since we often fill up the outgoing socket and then just sit around
2020 * waiting for the other 2 processes to do their thing, we don't want
2021 * to exit on a timeout. If the data stops flowing, the receiver will
2022 * notice that and let us know via the redo pipe (or its closing). */
2025 dflt_perms = (ACCESSPERMS & ~orig_umask);
2028 #ifdef SUPPORT_HARD_LINKS
2029 if (preserve_hard_links && inc_recurse) {
2030 while (!flist_eof && file_total < FILECNT_LOOKAHEAD/2)
2031 wait_for_receiver();
2035 if (inc_recurse && cur_flist->parent_ndx >= 0) {
2036 struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
2038 strlcpy(fbuf, solo_file, sizeof fbuf);
2041 ndx = cur_flist->ndx_start - 1;
2042 recv_generator(fbuf, fp, ndx, itemizing, code, f_out);
2043 if (delete_during && dry_run < 2 && !list_only
2044 && !(fp->flags & FLAG_MISSING_DIR)) {
2045 if (fp->flags & FLAG_CONTENT_DIR) {
2047 if (one_file_system) {
2048 uint32 *devp = F_DIR_DEV_P(fp);
2049 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
2051 dirdev = MAKEDEV(0, 0);
2052 delete_in_dir(fbuf, fp, &dirdev);
2054 change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(fp));
2057 for (i = cur_flist->low; i <= cur_flist->high; i++) {
2058 struct file_struct *file = cur_flist->sorted[i];
2060 if (!F_IS_ACTIVE(file))
2066 ndx = i + cur_flist->ndx_start;
2069 strlcpy(fbuf, solo_file, sizeof fbuf);
2072 recv_generator(fbuf, file, ndx, itemizing, code, f_out);
2074 check_for_finished_files(itemizing, code, 0);
2076 if (i + cur_flist->ndx_start >= next_loopchk) {
2078 maybe_send_keepalive();
2080 maybe_flush_socket(0);
2081 next_loopchk += loopchk_limit;
2086 write_ndx(f_out, NDX_DONE);
2091 check_for_finished_files(itemizing, code, 1);
2092 if (cur_flist->next || flist_eof)
2094 wait_for_receiver();
2096 } while ((cur_flist = cur_flist->next) != NULL);
2099 delete_in_dir(NULL, NULL, &dev_zero);
2101 if (DEBUG_GTE(GENR, 1))
2102 rprintf(FINFO, "generate_files phase=%d\n", phase);
2105 check_for_finished_files(itemizing, code, 1);
2108 wait_for_receiver();
2112 if (DEBUG_GTE(GENR, 1))
2113 rprintf(FINFO, "generate_files phase=%d\n", phase);
2115 write_ndx(f_out, NDX_DONE);
2117 /* Reduce round-trip lag-time for a useless delay-updates phase. */
2118 if (protocol_version >= 29 && EARLY_DELAY_DONE_MSG())
2119 write_ndx(f_out, NDX_DONE);
2121 if (protocol_version >= 31 && EARLY_DELETE_DONE_MSG()) {
2122 if ((INFO_GTE(STATS, 2) && (delete_mode || force_delete)) || read_batch)
2123 write_del_stats(f_out);
2124 if (EARLY_DELAY_DONE_MSG()) /* Can't send this before delay */
2125 write_ndx(f_out, NDX_DONE);
2128 /* Read MSG_DONE for the redo phase (and any prior messages). */
2130 check_for_finished_files(itemizing, code, 0);
2131 if (msgdone_cnt > 1)
2133 wait_for_receiver();
2136 if (protocol_version >= 29) {
2138 if (DEBUG_GTE(GENR, 1))
2139 rprintf(FINFO, "generate_files phase=%d\n", phase);
2140 if (!EARLY_DELAY_DONE_MSG()) {
2141 write_ndx(f_out, NDX_DONE);
2142 if (protocol_version >= 31 && EARLY_DELETE_DONE_MSG())
2143 write_ndx(f_out, NDX_DONE);
2145 /* Read MSG_DONE for delay-updates phase & prior messages. */
2146 while (msgdone_cnt == 2)
2147 wait_for_receiver();
2150 info_levels[INFO_FLIST] = save_info_flist;
2151 info_levels[INFO_PROGRESS] = save_info_progress;
2153 if (delete_during == 2)
2154 do_delayed_deletions(fbuf);
2155 if (delete_after && !solo_file && file_total > 0)
2158 if (max_delete >= 0 && skipped_deletes) {
2160 "Deletions stopped due to --max-delete limit (%d skipped)\n",
2162 io_error |= IOERR_DEL_LIMIT;
2165 if (protocol_version >= 31) {
2166 if (!EARLY_DELETE_DONE_MSG()) {
2167 if (INFO_GTE(STATS, 2) || read_batch)
2168 write_del_stats(f_out);
2169 write_ndx(f_out, NDX_DONE);
2172 /* Read MSG_DONE for late-delete phase & prior messages. */
2173 while (msgdone_cnt == 3)
2174 wait_for_receiver();
2177 if ((need_retouch_dir_perms || need_retouch_dir_times)
2178 && dir_tweaking && (!inc_recurse || delete_during == 2))
2179 touch_up_dirs(dir_flist, -1);
2181 if (DEBUG_GTE(GENR, 1))
2182 rprintf(FINFO, "generate_files finished\n");