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 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;
99 extern char *basis_dir[MAX_BASIS_DIRS+1];
100 extern struct file_list *cur_flist, *first_flist, *dir_flist;
101 extern filter_rule_list filter_list, daemon_filter_list;
103 int maybe_ATTRS_REPORT = 0;
105 static dev_t dev_zero;
106 static int deldelay_size = 0, deldelay_cnt = 0;
107 static char *deldelay_buf = NULL;
108 static int deldelay_fd = -1;
109 static int loopchk_limit;
110 static int dir_tweaking;
111 static int symlink_timeset_failed_flags;
112 static int need_retouch_dir_times;
113 static int need_retouch_dir_perms;
114 static const char *solo_file = NULL;
117 TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
120 /* Forward declarations. */
121 #ifdef SUPPORT_HARD_LINKS
122 static void handle_skipped_hlink(struct file_struct *file, int itemizing,
123 enum logcode code, int f_out);
126 #define EARLY_DELAY_DONE_MSG() (!delay_updates)
127 #define EARLY_DELETE_DONE_MSG() (!(delete_during == 2 || delete_after))
129 static int start_delete_delay_temp(void)
131 char fnametmp[MAXPATHLEN];
132 int save_dry_run = dry_run;
135 if (!get_tmpname(fnametmp, "deldelay", False)
136 || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
137 rprintf(FINFO, "NOTE: Unable to create delete-delay temp file%s.\n",
138 inc_recurse ? "" : " -- switching to --delete-after");
140 delete_after = !inc_recurse;
141 dry_run = save_dry_run;
145 dry_run = save_dry_run;
149 static int flush_delete_delay(void)
151 if (deldelay_fd < 0 && !start_delete_delay_temp())
153 if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
154 rsyserr(FERROR, errno, "flush of delete-delay buffer");
156 delete_after = !inc_recurse;
164 static int remember_delete(struct file_struct *file, const char *fname, int flags)
168 if (deldelay_cnt == deldelay_size && !flush_delete_delay())
171 if (flags & DEL_NO_UID_WRITE)
172 deldelay_buf[deldelay_cnt++] = '!';
175 len = snprintf(deldelay_buf + deldelay_cnt,
176 deldelay_size - deldelay_cnt,
178 (int)file->mode, fname, '\0');
179 if ((deldelay_cnt += len) <= deldelay_size)
182 if (!flush_delete_delay())
189 static int read_delay_line(char *buf, int *flags_p)
191 static int read_pos = 0;
193 char *bp, *past_space;
196 for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
197 if (j < deldelay_cnt)
199 if (deldelay_fd < 0) {
204 deldelay_cnt -= read_pos;
205 if (deldelay_cnt == deldelay_size)
207 if (deldelay_cnt && read_pos) {
208 memmove(deldelay_buf, deldelay_buf + read_pos,
211 len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
212 deldelay_size - deldelay_cnt);
216 "ERROR: unexpected EOF in delete-delay file.\n");
221 rsyserr(FERROR, errno,
222 "reading delete-delay file");
229 bp = deldelay_buf + read_pos;
232 *flags_p = DEL_NO_UID_WRITE;
236 if (sscanf(bp, "%x ", &mode) != 1) {
238 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
241 past_space = strchr(bp, ' ') + 1;
242 len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
245 if (len > MAXPATHLEN) {
246 rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
250 /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
251 * instead of returning a pointer to our buffer. */
252 memcpy(buf, past_space, len);
257 static void do_delayed_deletions(char *delbuf)
261 if (deldelay_fd >= 0) {
262 if (deldelay_cnt && !flush_delete_delay())
264 lseek(deldelay_fd, 0, 0);
266 while ((mode = read_delay_line(delbuf, &flags)) >= 0)
267 delete_item(delbuf, mode, flags | DEL_RECURSE);
268 if (deldelay_fd >= 0)
272 /* This function is used to implement per-directory deletion, and is used by
273 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
274 * MAXPATHLEN buffer with the name of the directory in it (the functions we
275 * call will append names onto the end, but the old dir value will be restored
277 static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
279 static int already_warned = 0;
280 struct file_list *dirlist;
281 char delbuf[MAXPATHLEN];
283 int save_uid_ndx = uid_ndx;
286 change_local_filter_dir(NULL, 0, 0);
290 if (DEBUG_GTE(DEL, 2))
291 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
294 maybe_send_keepalive();
296 if (io_error && !ignore_errors) {
300 "IO error encountered -- skipping file deletion\n");
306 change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
308 if (one_file_system) {
309 if (file->flags & FLAG_TOP_DIR)
310 filesystem_dev = *fs_dev;
311 else if (filesystem_dev != *fs_dev)
316 uid_ndx = ++file_extra_cnt;
318 dirlist = get_dirlist(fbuf, dlen, 0);
320 /* If an item in dirlist is not found in flist, delete it
321 * from the filesystem. */
322 for (i = dirlist->used; i--; ) {
323 struct file_struct *fp = dirlist->files[i];
324 if (!F_IS_ACTIVE(fp))
326 if (fp->flags & FLAG_MOUNT_DIR && S_ISDIR(fp->mode)) {
327 if (INFO_GTE(MOUNT, 1))
328 rprintf(FINFO, "cannot delete mount point: %s\n",
332 /* Here we want to match regardless of file type. Replacement
333 * of a file with one of another type is handled separately by
334 * a delete_item call with a DEL_MAKE_ROOM flag. */
335 if (flist_find_ignore_dirness(cur_flist, fp) < 0) {
336 int flags = DEL_RECURSE;
337 if (!(fp->mode & S_IWUSR) && !am_root && (uid_t)F_OWNER(fp) == our_uid)
338 flags |= DEL_NO_UID_WRITE;
340 if (delete_during == 2) {
341 if (!remember_delete(fp, delbuf, flags))
344 delete_item(delbuf, fp->mode, flags);
356 /* This deletes any files on the receiving side that are not present on the
357 * sending side. This is used by --delete-before and --delete-after. */
358 static void do_delete_pass(void)
360 char fbuf[MAXPATHLEN];
364 /* dry_run is incremented when the destination doesn't exist yet. */
365 if (dry_run > 1 || list_only)
368 for (j = 0; j < cur_flist->used; j++) {
369 struct file_struct *file = cur_flist->sorted[j];
373 if (!(file->flags & FLAG_CONTENT_DIR)) {
374 change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(file));
378 if (DEBUG_GTE(DEL, 1) && file->flags & FLAG_TOP_DIR)
379 rprintf(FINFO, "deleting in %s\n", fbuf);
381 if (link_stat(fbuf, &st, keep_dirlinks) < 0
382 || !S_ISDIR(st.st_mode))
385 delete_in_dir(fbuf, file, &st.st_dev);
387 delete_in_dir(NULL, NULL, &dev_zero);
389 if (INFO_GTE(FLIST, 2) && !am_server)
390 rprintf(FINFO, " \r");
393 int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
395 #if !defined HAVE_LUTIMES || !defined HAVE_UTIMES
396 if (S_ISLNK(file->mode)) {
400 if (preserve_times && cmp_time(sxp->st.st_mtime, file->modtime) != 0)
403 if (preserve_perms) {
404 if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
406 } else if (preserve_executability
407 && ((sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0)))
410 if (am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file))
413 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
417 if (preserve_acls && !S_ISLNK(file->mode)) {
418 if (!ACL_READY(*sxp))
420 if (set_acl(NULL, file, sxp) == 0)
424 #ifdef SUPPORT_XATTRS
425 if (preserve_xattrs) {
426 if (!XATTR_READY(*sxp))
427 get_xattr(fname, sxp);
428 if (xattr_diff(file, sxp, 0))
436 void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
437 stat_x *sxp, int32 iflags, uchar fnamecmp_type,
440 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
441 int keep_time = !preserve_times ? 0
442 : S_ISDIR(file->mode) ? preserve_times > 1 :
443 #if defined HAVE_LUTIMES && defined HAVE_UTIMES
446 !S_ISLNK(file->mode);
449 if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
450 iflags |= ITEM_REPORT_SIZE;
451 if (file->flags & FLAG_TIME_FAILED) { /* symlinks only */
452 if (iflags & ITEM_LOCAL_CHANGE)
453 iflags |= symlink_timeset_failed_flags;
455 ? cmp_time(file->modtime, sxp->st.st_mtime) != 0
456 : iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !(iflags & ITEM_MATCHED)
457 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
458 iflags |= ITEM_REPORT_TIME;
459 #if !defined HAVE_LCHMOD && !defined HAVE_SETATTRLIST
460 if (S_ISLNK(file->mode)) {
464 if (preserve_perms) {
465 if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
466 iflags |= ITEM_REPORT_PERMS;
467 } else if (preserve_executability
468 && ((sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0)))
469 iflags |= ITEM_REPORT_PERMS;
470 if (uid_ndx && am_root && (uid_t)F_OWNER(file) != sxp->st.st_uid)
471 iflags |= ITEM_REPORT_OWNER;
472 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
473 && sxp->st.st_gid != (gid_t)F_GROUP(file))
474 iflags |= ITEM_REPORT_GROUP;
476 if (preserve_acls && !S_ISLNK(file->mode)) {
477 if (!ACL_READY(*sxp))
478 get_acl(fnamecmp, sxp);
479 if (set_acl(NULL, file, sxp) == 0)
480 iflags |= ITEM_REPORT_ACL;
483 #ifdef SUPPORT_XATTRS
484 if (preserve_xattrs) {
485 if (!XATTR_READY(*sxp))
486 get_xattr(fnamecmp, sxp);
487 if (xattr_diff(file, sxp, 1))
488 iflags |= ITEM_REPORT_XATTR;
492 #ifdef SUPPORT_XATTRS
493 if (preserve_xattrs && xattr_diff(file, NULL, 1))
494 iflags |= ITEM_REPORT_XATTR;
496 iflags |= ITEM_IS_NEW;
500 if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || INFO_GTE(NAME, 2)
501 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
502 if (protocol_version >= 29) {
504 write_ndx(sock_f_out, ndx);
505 write_shortint(sock_f_out, iflags);
506 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
507 write_byte(sock_f_out, fnamecmp_type);
508 if (iflags & ITEM_XNAME_FOLLOWS)
509 write_vstring(sock_f_out, xname, strlen(xname));
510 #ifdef SUPPORT_XATTRS
511 if (preserve_xattrs && do_xfers
512 && iflags & (ITEM_REPORT_XATTR|ITEM_TRANSFER)) {
513 send_xattr_request(NULL, file,
514 iflags & ITEM_REPORT_XATTR ? sock_f_out : -1);
517 } else if (ndx >= 0) {
518 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
519 log_item(code, file, &stats, iflags, xname);
525 /* Perform our quick-check heuristic for determining if a file is unchanged. */
526 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
528 if (st->st_size != F_LENGTH(file))
531 /* if always checksum is set then we use the checksum instead
532 of the file time to determine whether to sync */
533 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
534 char sum[MAX_DIGEST_LEN];
535 file_checksum(fn, sum, st->st_size);
536 return memcmp(sum, F_SUM(file), checksum_len) == 0;
545 return cmp_time(st->st_mtime, file->modtime) == 0;
550 * set (initialize) the size entries in the per-file sum_struct
551 * calculating dynamic block and checksum sizes.
553 * This is only called from generate_and_send_sums() but is a separate
554 * function to encapsulate the logic.
556 * The block size is a rounded square root of file length.
558 * The checksum size is determined according to:
559 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
560 * provided by Donovan Baarda which gives a probability of rsync
561 * algorithm corrupting data and falling back using the whole md4
564 * This might be made one of several selectable heuristics.
566 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
573 blength = block_size;
574 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
575 blength = BLOCK_SIZE;
577 int32 max_blength = protocol_version < 30 ? OLD_MAX_BLOCK_SIZE : MAX_BLOCK_SIZE;
580 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
581 if (c < 0 || c >= max_blength)
582 blength = max_blength;
587 if (len < (int64)blength * blength)
590 } while (c >= 8); /* round to multiple of 8 */
591 blength = MAX(blength, BLOCK_SIZE);
595 if (protocol_version < 27) {
596 s2length = csum_length;
597 } else if (csum_length == SUM_LENGTH) {
598 s2length = SUM_LENGTH;
601 int b = BLOCKSUM_BIAS;
602 for (l = len; l >>= 1; b += 2) {}
603 for (c = blength; (c >>= 1) && b; b--) {}
604 /* add a bit, subtract rollsum, round up. */
605 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
606 s2length = MAX(s2length, csum_length);
607 s2length = MIN(s2length, SUM_LENGTH);
611 sum->blength = blength;
612 sum->s2length = s2length;
613 sum->remainder = (int32)(len % blength);
614 sum->count = (int32)(l = (len / blength) + (sum->remainder != 0));
616 if ((int64)sum->count != l)
619 if (sum->count && DEBUG_GTE(DELTASUM, 2)) {
621 "count=%s rem=%ld blength=%ld s2length=%d flength=%s\n",
622 big_num(sum->count), (long)sum->remainder, (long)sum->blength,
623 sum->s2length, big_num(sum->flength));
629 * Generate and send a stream of signatures/checksums that describe a buffer
631 * Generate approximately one checksum every block_len bytes.
633 static int generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
636 struct map_struct *mapbuf;
637 struct sum_struct sum;
640 sum_sizes_sqroot(&sum, len);
643 write_sum_head(f_out, &sum);
645 if (append_mode > 0 && f_copy < 0)
649 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
653 for (i = 0; i < sum.count; i++) {
654 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
655 char *map = map_ptr(mapbuf, offset, n1);
656 char sum2[SUM_LENGTH];
663 full_write(f_copy, map, n1);
668 sum1 = get_checksum1(map, n1);
669 get_checksum2(map, n1, sum2);
671 if (DEBUG_GTE(DELTASUM, 3)) {
673 "chunk[%s] offset=%s len=%ld sum1=%08lx\n",
674 big_num(i), big_num(offset - n1), (long)n1,
675 (unsigned long)sum1);
677 write_int(f_out, sum1);
678 write_buf(f_out, sum2, sum.s2length);
688 /* Try to find a filename in the same dir as "fname" with a similar name. */
689 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
691 int fname_len, fname_suf_len;
692 const char *fname_suf, *fname = file->basename;
693 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
694 int j, lowest_j = -1;
696 fname_len = strlen(fname);
697 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
699 for (j = 0; j < dirlist->used; j++) {
700 struct file_struct *fp = dirlist->files[j];
701 const char *suf, *name;
705 if (!S_ISREG(fp->mode) || !F_LENGTH(fp)
706 || fp->flags & FLAG_FILE_SENT)
711 if (F_LENGTH(fp) == F_LENGTH(file)
712 && cmp_time(fp->modtime, file->modtime) == 0) {
713 if (DEBUG_GTE(FUZZY, 2)) {
715 "fuzzy size/modtime match for %s\n",
722 suf = find_filename_suffix(name, len, &suf_len);
724 dist = fuzzy_distance(name, len, fname, fname_len);
725 /* Add some extra weight to how well the suffixes match. */
726 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
728 if (DEBUG_GTE(FUZZY, 2)) {
729 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
730 name, (int)(dist>>16), (int)(dist&0xFFFF));
732 if (dist <= lowest_dist) {
741 /* Copy a file found in our --copy-dest handling. */
742 static int copy_altdest_file(const char *src, const char *dest, struct file_struct *file)
744 char buf[MAXPATHLEN];
745 const char *copy_to, *partialptr;
746 int save_preserve_xattrs = preserve_xattrs;
750 /* Let copy_file open the destination in place. */
754 fd_w = open_tmpfile(buf, dest, file);
759 cleanup_set(copy_to, NULL, NULL, -1, -1);
760 if (copy_file(src, copy_to, fd_w, file->mode) < 0) {
761 if (INFO_GTE(COPY, 1)) {
762 rsyserr(FINFO, errno, "copy_file %s => %s",
763 full_fname(src), copy_to);
765 /* Try to clean up. */
770 partialptr = partial_dir ? partial_dir_fname(dest) : NULL;
771 preserve_xattrs = 0; /* xattrs were copied with file */
772 ok = finish_transfer(dest, copy_to, src, partialptr, file, 1, 0);
773 preserve_xattrs = save_preserve_xattrs;
778 /* This is only called for regular files. We return -2 if we've finished
779 * handling the file, -1 if no dest-linking occurred, or a non-negative
780 * value if we found an alternate basis file. */
781 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
782 char *cmpbuf, stat_x *sxp, int itemizing,
790 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
791 if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
793 switch (match_level) {
799 if (!unchanged_file(cmpbuf, file, &sxp->st))
805 if (!unchanged_attrs(cmpbuf, file, sxp))
812 } while (basis_dir[++j] != NULL);
817 if (j != best_match) {
819 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
820 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
824 if (match_level == 3 && !copy_dest) {
825 #ifdef SUPPORT_HARD_LINKS
827 if (!hard_link_one(file, fname, cmpbuf, 1))
829 if (preserve_hard_links && F_IS_HLINKED(file))
830 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, j);
831 if (!maybe_ATTRS_REPORT && (INFO_GTE(NAME, 2) || stdout_format_has_i > 1)) {
832 itemize(cmpbuf, file, ndx, 1, sxp,
833 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
839 itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
840 if (INFO_GTE(NAME, 2) && maybe_ATTRS_REPORT)
841 rprintf(FCLIENT, "%s is uptodate\n", fname);
845 if (match_level >= 2) {
846 #ifdef SUPPORT_HARD_LINKS
847 try_a_copy: /* Copy the file locally. */
849 if (!dry_run && copy_altdest_file(cmpbuf, fname, file) < 0)
852 itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
853 if (maybe_ATTRS_REPORT
854 && ((!itemizing && INFO_GTE(NAME, 1) && match_level == 2)
855 || (INFO_GTE(NAME, 2) && match_level == 3))) {
856 code = match_level == 3 ? FCLIENT : FINFO;
857 rprintf(code, "%s%s\n", fname,
858 match_level == 3 ? " is uptodate" : "");
860 #ifdef SUPPORT_HARD_LINKS
861 if (preserve_hard_links && F_IS_HLINKED(file))
862 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, -1);
867 return FNAMECMP_BASIS_DIR_LOW + j;
870 /* This is only called for non-regular files. We return -2 if we've finished
871 * handling the file, or -1 if no dest-linking occurred, or a non-negative
872 * value if we found an alternate basis file. */
873 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
874 char *cmpbuf, stat_x *sxp, int itemizing,
879 enum nonregtype type;
882 char lnk[MAXPATHLEN];
887 #ifndef SUPPORT_LINKS
888 if (S_ISLNK(file->mode))
891 if (S_ISDIR(file->mode)) {
893 } else if (IS_SPECIAL(file->mode))
895 else if (IS_DEVICE(file->mode))
898 else if (S_ISLNK(file->mode))
903 "internal: try_dests_non() called with invalid mode (%o)\n",
905 exit_cleanup(RERR_UNSUPPORTED);
909 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
910 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
914 if (!S_ISDIR(sxp->st.st_mode))
918 if (!IS_SPECIAL(sxp->st.st_mode))
922 if (!IS_DEVICE(sxp->st.st_mode))
927 if (!S_ISLNK(sxp->st.st_mode))
934 if (match_level < 1) {
943 devp = F_RDEV_P(file);
944 if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
949 if ((len = do_readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
952 if (strcmp(lnk, F_SYMLINK(file)) != 0)
959 if (match_level < 2) {
963 if (unchanged_attrs(cmpbuf, file, sxp)) {
968 } while (basis_dir[++j] != NULL);
973 if (j != best_match) {
975 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
976 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
980 if (match_level == 3) {
981 #ifdef SUPPORT_HARD_LINKS
983 #ifndef CAN_HARDLINK_SYMLINK
984 && !S_ISLNK(file->mode)
986 #ifndef CAN_HARDLINK_SPECIAL
987 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
989 && !S_ISDIR(file->mode)) {
990 if (do_link(cmpbuf, fname) < 0) {
991 rsyserr(FERROR_XFER, errno,
992 "failed to hard-link %s with %s",
996 if (preserve_hard_links && F_IS_HLINKED(file))
997 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1001 if (itemizing && stdout_format_has_i
1002 && (INFO_GTE(NAME, 2) || stdout_format_has_i > 1)) {
1003 int chg = compare_dest && type != TYPE_DIR ? 0
1004 : ITEM_LOCAL_CHANGE + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1005 char *lp = match_level == 3 ? "" : NULL;
1006 itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
1008 if (INFO_GTE(NAME, 2) && maybe_ATTRS_REPORT) {
1009 rprintf(FCLIENT, "%s%s is uptodate\n",
1010 fname, type == TYPE_DIR ? "/" : "");
1018 static void list_file_entry(struct file_struct *f)
1020 char permbuf[PERMSTRING_SIZE];
1022 int colwidth = human_readable ? 14 : 11;
1024 if (!F_IS_ACTIVE(f)) {
1025 /* this can happen if duplicate names were removed */
1029 permstring(permbuf, f->mode);
1032 /* TODO: indicate '+' if the entry has an ACL. */
1034 #ifdef SUPPORT_LINKS
1035 if (preserve_links && S_ISLNK(f->mode)) {
1036 rprintf(FINFO, "%s %*s %s %s -> %s\n",
1037 permbuf, colwidth, comma_num(len),
1038 timestring(f->modtime), f_name(f, NULL),
1042 if (missing_args == 2 && f->mode == 0) {
1043 rprintf(FINFO, "%-*s %s\n",
1044 colwidth + 31, "*missing",
1047 rprintf(FINFO, "%s %*s %s %s\n",
1048 permbuf, colwidth, comma_num(len),
1049 timestring(f->modtime), f_name(f, NULL));
1053 static int phase = 0;
1054 static int dflt_perms;
1056 static int implied_dirs_are_missing;
1057 /* Helper for recv_generator's skip_dir and dry_missing_dir tests. */
1058 static BOOL is_below(struct file_struct *file, struct file_struct *subtree)
1060 return F_DEPTH(file) > F_DEPTH(subtree)
1061 && (!implied_dirs_are_missing || f_name_has_prefix(file, subtree));
1064 /* Acts on the indicated item in cur_flist whose name is fname. If a dir,
1065 * make sure it exists, and has the right permissions/timestamp info. For
1066 * all other non-regular files (symlinks, etc.) we create them here. For
1067 * regular files that have changed, we try to find a basis file and then
1068 * start sending checksums. The ndx is the file's unique index value.
1070 * The fname parameter must point to a MAXPATHLEN buffer! (e.g it gets
1071 * passed to delete_item(), which can use it during a recursive delete.)
1073 * Note that f_out is set to -1 when doing final directory-permission and
1074 * modification-time repair. */
1075 static void recv_generator(char *fname, struct file_struct *file, int ndx,
1076 int itemizing, enum logcode code, int f_out)
1078 static const char *parent_dirname = "";
1079 /* Missing dir not created due to --dry-run; will still be scanned. */
1080 static struct file_struct *dry_missing_dir = NULL;
1081 /* Missing dir whose contents are skipped altogether due to
1082 * --ignore-non-existing, daemon exclude, or mkdir failure. */
1083 static struct file_struct *skip_dir = NULL;
1084 static struct file_list *fuzzy_dirlist = NULL;
1085 static int need_fuzzy_dirlist = 0;
1086 struct file_struct *fuzzy_file = NULL;
1087 int fd = -1, f_copy = -1;
1089 STRUCT_STAT partial_st;
1090 struct file_struct *back_file = NULL;
1091 int statret, real_ret, stat_errno;
1092 char *fnamecmp, *partialptr, *backupptr = NULL;
1093 char fnamecmpbuf[MAXPATHLEN];
1094 uchar fnamecmp_type;
1095 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1096 int is_dir = !S_ISDIR(file->mode) ? 0
1097 : inc_recurse && ndx != cur_flist->ndx_start - 1 ? -1
1100 if (DEBUG_GTE(GENR, 1))
1101 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
1105 || (is_dir && !implied_dirs && file->flags & FLAG_IMPLIED_DIR))
1107 list_file_entry(file);
1112 if (is_below(file, skip_dir)) {
1114 file->flags |= FLAG_MISSING_DIR;
1115 #ifdef SUPPORT_HARD_LINKS
1116 else if (F_IS_HLINKED(file))
1117 handle_skipped_hlink(file, itemizing, code, f_out);
1124 if (daemon_filter_list.head && (*fname != '.' || fname[1])) {
1125 if (check_filter(&daemon_filter_list, FLOG, fname, is_dir) < 0) {
1128 #ifdef SUPPORT_HARD_LINKS
1129 if (F_IS_HLINKED(file))
1130 handle_skipped_hlink(file, itemizing, code, f_out);
1132 rprintf(FERROR_XFER,
1133 "skipping daemon-excluded %s \"%s\"\n",
1134 is_dir ? "directory" : "file", fname);
1136 goto skipping_dir_contents;
1142 if (dry_run > 1 || (dry_missing_dir && is_below(file, dry_missing_dir))) {
1143 parent_is_dry_missing:
1144 if (fuzzy_dirlist) {
1145 flist_free(fuzzy_dirlist);
1146 fuzzy_dirlist = NULL;
1148 parent_dirname = "";
1150 stat_errno = ENOENT;
1152 const char *dn = file->dirname ? file->dirname : ".";
1153 dry_missing_dir = NULL;
1154 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1155 if (relative_paths && !implied_dirs
1156 && do_stat(dn, &sx.st) < 0) {
1158 goto parent_is_dry_missing;
1159 if (make_path(fname, MKP_DROP_NAME | MKP_SKIP_SLASH) < 0) {
1160 rsyserr(FERROR_XFER, errno,
1161 "recv_generator: mkdir %s failed",
1165 if (fuzzy_dirlist) {
1166 flist_free(fuzzy_dirlist);
1167 fuzzy_dirlist = NULL;
1170 need_fuzzy_dirlist = 1;
1172 if (!preserve_perms)
1173 dflt_perms = default_perms_for_dir(dn);
1176 parent_dirname = dn;
1178 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
1179 strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1180 fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
1181 need_fuzzy_dirlist = 0;
1184 statret = link_stat(fname, &sx.st, keep_dirlinks && is_dir);
1188 if (missing_args == 2 && file->mode == 0) {
1189 if (filter_list.head && check_filter(&filter_list, FINFO, fname, is_dir) < 0)
1192 delete_item(fname, sx.st.st_mode, del_opts);
1196 if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
1201 file->flags |= FLAG_MISSING_DIR;
1203 #ifdef SUPPORT_HARD_LINKS
1204 else if (F_IS_HLINKED(file))
1205 handle_skipped_hlink(file, itemizing, code, f_out);
1207 if (INFO_GTE(SKIP, 1)) {
1208 rprintf(FINFO, "not creating new %s \"%s\"\n",
1209 is_dir ? "directory" : "file", fname);
1214 if (statret == 0 && !(sx.st.st_mode & S_IWUSR)
1215 && !am_root && sx.st.st_uid == our_uid)
1216 del_opts |= DEL_NO_UID_WRITE;
1218 if (ignore_existing > 0 && statret == 0
1219 && (!is_dir || !S_ISDIR(sx.st.st_mode))) {
1220 if (INFO_GTE(SKIP, 1) && is_dir >= 0)
1221 rprintf(FINFO, "%s exists\n", fname);
1222 #ifdef SUPPORT_HARD_LINKS
1223 if (F_IS_HLINKED(file))
1224 handle_skipped_hlink(file, itemizing, code, f_out);
1230 if (!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
1233 /* In inc_recurse mode we want to make sure any missing
1234 * directories get created while we're still processing
1235 * the parent dir (which allows us to touch the parent
1236 * dir's mtime right away). We will handle the dir in
1237 * full later (right before we handle its contents). */
1239 && (S_ISDIR(sx.st.st_mode)
1240 || delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0))
1241 goto cleanup; /* Any errors get reported later. */
1242 if (do_mkdir(fname, file->mode & 0700) == 0)
1243 file->flags |= FLAG_DIR_CREATED;
1246 /* The file to be received is a directory, so we need
1247 * to prepare appropriately. If there is already a
1248 * file of that name and it is *not* a directory, then
1249 * we need to delete it. If it doesn't exist, then
1250 * (perhaps recursively) create it. */
1251 if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1252 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0)
1253 goto skipping_dir_contents;
1256 if (dry_run && statret != 0) {
1257 if (!dry_missing_dir)
1258 dry_missing_dir = file;
1259 file->flags |= FLAG_MISSING_DIR;
1263 if (file->flags & FLAG_DIR_CREATED)
1265 if (!preserve_perms) { /* See comment in non-dir code below. */
1266 file->mode = dest_mode(file->mode, sx.st.st_mode,
1267 dflt_perms, statret == 0);
1269 if (statret != 0 && basis_dir[0] != NULL) {
1270 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1279 if (itemizing && f_out != -1) {
1280 itemize(fname, file, ndx, statret, &sx,
1281 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1283 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1284 if (!relative_paths || errno != ENOENT
1285 || make_path(fname, MKP_DROP_NAME | MKP_SKIP_SLASH) < 0
1286 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
1287 rsyserr(FERROR_XFER, errno,
1288 "recv_generator: mkdir %s failed",
1290 skipping_dir_contents:
1292 "*** Skipping any contents from this failed directory ***\n");
1294 file->flags |= FLAG_MISSING_DIR;
1298 #ifdef SUPPORT_XATTRS
1299 if (preserve_xattrs && statret == 1)
1300 copy_xattrs(fnamecmpbuf, fname);
1302 if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
1303 && INFO_GTE(NAME, 1) && code != FNONE && f_out != -1)
1304 rprintf(code, "%s/\n", fname);
1306 /* We need to ensure that the dirs in the transfer have writable
1307 * permissions during the time we are putting files within them.
1308 * This is then fixed after the transfer is done. */
1310 if (!am_root && !(file->mode & S_IWUSR) && dir_tweaking) {
1311 mode_t mode = file->mode | S_IWUSR;
1312 if (do_chmod(fname, mode) < 0) {
1313 rsyserr(FERROR_XFER, errno,
1314 "failed to modify permissions on %s",
1317 need_retouch_dir_perms = 1;
1321 if (real_ret != 0 && one_file_system)
1322 real_sx.st.st_dev = filesystem_dev;
1324 if (one_file_system) {
1325 uint32 *devp = F_DIR_DEV_P(file);
1326 DEV_MAJOR(devp) = major(real_sx.st.st_dev);
1327 DEV_MINOR(devp) = minor(real_sx.st.st_dev);
1330 else if (delete_during && f_out != -1 && !phase
1331 && !(file->flags & FLAG_MISSING_DIR)) {
1332 if (file->flags & FLAG_CONTENT_DIR)
1333 delete_in_dir(fname, file, &real_sx.st.st_dev);
1335 change_local_filter_dir(fname, strlen(fname), F_DEPTH(file));
1340 /* If we're not preserving permissions, change the file-list's
1341 * mode based on the local permissions and some heuristics. */
1342 if (!preserve_perms) {
1343 int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
1344 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
1348 #ifdef SUPPORT_HARD_LINKS
1349 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1350 && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
1354 if (preserve_links && S_ISLNK(file->mode)) {
1355 #ifdef SUPPORT_LINKS
1356 const char *sl = F_SYMLINK(file);
1357 if (safe_symlinks && unsafe_symlink(sl, fname)) {
1358 if (INFO_GTE(NAME, 1)) {
1360 fname = f_name(file, NULL);
1362 "ignoring unsafe symlink %s -> \"%s\"\n",
1363 full_fname(fname), sl);
1368 char lnk[MAXPATHLEN];
1371 if (S_ISLNK(sx.st.st_mode)
1372 && (len = do_readlink(fname, lnk, MAXPATHLEN-1)) > 0
1373 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1374 /* The link is pointing to the right place. */
1375 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1377 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1378 #if defined SUPPORT_HARD_LINKS && defined CAN_HARDLINK_SYMLINK
1379 if (preserve_hard_links && F_IS_HLINKED(file))
1380 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1382 if (remove_source_files == 1)
1383 goto return_with_success;
1386 } else if (basis_dir[0] != NULL) {
1387 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1390 #ifndef CAN_HARDLINK_SYMLINK
1392 /* Resort to --copy-dest behavior. */
1402 if (atomic_create(file, fname, sl, MAKEDEV(0, 0), &sx, statret == 0 ? DEL_FOR_SYMLINK : 0)) {
1403 set_file_attrs(fname, file, NULL, NULL, 0);
1405 if (statret == 0 && !S_ISLNK(sx.st.st_mode))
1407 itemize(fname, file, ndx, statret, &sx,
1408 ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
1410 if (code != FNONE && INFO_GTE(NAME, 1))
1411 rprintf(code, "%s -> %s\n", fname, sl);
1412 #ifdef SUPPORT_HARD_LINKS
1413 if (preserve_hard_links && F_IS_HLINKED(file))
1414 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1416 /* This does not check remove_source_files == 1
1417 * because this is one of the items that the old
1418 * --remove-sent-files option would remove. */
1419 if (remove_source_files)
1420 goto return_with_success;
1426 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1427 || (preserve_specials && IS_SPECIAL(file->mode))) {
1429 int del_for_flag = 0;
1430 if (IS_DEVICE(file->mode)) {
1431 uint32 *devp = F_RDEV_P(file);
1432 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1436 if (IS_DEVICE(file->mode)) {
1437 if (!IS_DEVICE(sx.st.st_mode))
1439 del_for_flag = DEL_FOR_DEVICE;
1441 if (!IS_SPECIAL(sx.st.st_mode))
1443 del_for_flag = DEL_FOR_SPECIAL;
1446 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
1447 && (IS_SPECIAL(sx.st.st_mode) || sx.st.st_rdev == rdev)) {
1448 /* The device or special file is identical. */
1449 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1451 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1452 #ifdef SUPPORT_HARD_LINKS
1453 if (preserve_hard_links && F_IS_HLINKED(file))
1454 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1456 if (remove_source_files == 1)
1457 goto return_with_success;
1460 } else if (basis_dir[0] != NULL) {
1461 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1464 #ifndef CAN_HARDLINK_SPECIAL
1466 /* Resort to --copy-dest behavior. */
1476 if (DEBUG_GTE(GENR, 1)) {
1477 rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1478 fname, (int)file->mode,
1479 (long)major(rdev), (long)minor(rdev));
1481 if (atomic_create(file, fname, NULL, rdev, &sx, del_for_flag)) {
1482 set_file_attrs(fname, file, NULL, NULL, 0);
1484 itemize(fname, file, ndx, statret, &sx,
1485 ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
1487 if (code != FNONE && INFO_GTE(NAME, 1))
1488 rprintf(code, "%s\n", fname);
1489 #ifdef SUPPORT_HARD_LINKS
1490 if (preserve_hard_links && F_IS_HLINKED(file))
1491 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1493 if (remove_source_files == 1)
1494 goto return_with_success;
1499 if (!S_ISREG(file->mode)) {
1501 fname = f_name(file, NULL);
1502 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1506 if (max_size > 0 && F_LENGTH(file) > max_size) {
1507 if (INFO_GTE(SKIP, 1)) {
1509 fname = f_name(file, NULL);
1510 rprintf(FINFO, "%s is over max-size\n", fname);
1514 if (min_size > 0 && F_LENGTH(file) < min_size) {
1515 if (INFO_GTE(SKIP, 1)) {
1517 fname = f_name(file, NULL);
1518 rprintf(FINFO, "%s is under min-size\n", fname);
1523 if (update_only > 0 && statret == 0
1524 && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
1525 if (INFO_GTE(SKIP, 1))
1526 rprintf(FINFO, "%s is newer\n", fname);
1527 #ifdef SUPPORT_HARD_LINKS
1528 if (F_IS_HLINKED(file))
1529 handle_skipped_hlink(file, itemizing, code, f_out);
1535 fnamecmp_type = FNAMECMP_FNAME;
1537 if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
1538 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE) != 0)
1541 stat_errno = ENOENT;
1544 if (statret != 0 && basis_dir[0] != NULL) {
1545 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
1548 if (remove_source_files == 1)
1549 goto return_with_success;
1553 fnamecmp = fnamecmpbuf;
1562 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1563 && link_stat(partialptr, &partial_st, 0) == 0
1564 && S_ISREG(partial_st.st_mode)) {
1566 goto prepare_to_open;
1570 if (statret != 0 && fuzzy_dirlist) {
1571 int j = find_fuzzy(file, fuzzy_dirlist);
1573 fuzzy_file = fuzzy_dirlist->files[j];
1574 f_name(fuzzy_file, fnamecmpbuf);
1575 if (DEBUG_GTE(FUZZY, 1)) {
1576 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1577 fname, fnamecmpbuf);
1579 sx.st.st_size = F_LENGTH(fuzzy_file);
1581 fnamecmp = fnamecmpbuf;
1582 fnamecmp_type = FNAMECMP_FUZZY;
1587 #ifdef SUPPORT_HARD_LINKS
1588 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1589 cur_flist->in_progress++;
1593 if (stat_errno == ENOENT)
1595 rsyserr(FERROR_XFER, stat_errno, "recv_generator: failed to stat %s",
1600 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1602 else if (fnamecmp_type == FNAMECMP_FUZZY)
1604 else if (unchanged_file(fnamecmp, file, &sx.st)) {
1606 do_unlink(partialptr);
1607 handle_partial_dir(partialptr, PDIR_DELETE);
1609 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1611 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
1612 #ifdef SUPPORT_HARD_LINKS
1613 if (preserve_hard_links && F_IS_HLINKED(file))
1614 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1616 if (remove_source_files != 1)
1618 return_with_success:
1620 send_msg_int(MSG_SUCCESS, ndx);
1624 if (append_mode > 0 && sx.st.st_size >= F_LENGTH(file)) {
1625 #ifdef SUPPORT_HARD_LINKS
1626 if (F_IS_HLINKED(file))
1627 handle_skipped_hlink(file, itemizing, code, f_out);
1635 fnamecmp = partialptr;
1636 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1643 if (read_batch || whole_file) {
1644 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1645 if (!(backupptr = get_backup_name(fname)))
1647 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
1648 goto pretend_missing;
1649 if (copy_file(fname, backupptr, -1, back_file->mode) < 0) {
1650 unmake_file(back_file);
1658 if (fuzzy_dirlist) {
1659 int j = flist_find(fuzzy_dirlist, file);
1660 if (j >= 0) /* don't use changing file as future fuzzy basis */
1661 fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
1665 if ((fd = do_open(fnamecmp, O_RDONLY, 0)) < 0) {
1666 rsyserr(FERROR, errno, "failed to open %s, continuing",
1667 full_fname(fnamecmp));
1669 /* pretend the file didn't exist */
1670 #ifdef SUPPORT_HARD_LINKS
1671 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1672 cur_flist->in_progress++;
1676 statret = real_ret = -1;
1680 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1681 if (!(backupptr = get_backup_name(fname))) {
1685 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1687 goto pretend_missing;
1689 if (robust_unlink(backupptr) && errno != ENOENT) {
1690 rsyserr(FERROR_XFER, errno, "unlink %s",
1691 full_fname(backupptr));
1692 unmake_file(back_file);
1697 if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1698 rsyserr(FERROR_XFER, errno, "open %s", full_fname(backupptr));
1699 unmake_file(back_file);
1704 fnamecmp_type = FNAMECMP_BACKUP;
1707 if (DEBUG_GTE(DELTASUM, 3)) {
1708 rprintf(FINFO, "gen mapped %s of size %s\n",
1709 fnamecmp, big_num(sx.st.st_size));
1712 if (DEBUG_GTE(DELTASUM, 2))
1713 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1716 if (remove_source_files && !delay_updates && !phase && !dry_run)
1717 increment_active_files(ndx, itemizing, code);
1718 if (inc_recurse && !dry_run)
1719 cur_flist->in_progress++;
1720 #ifdef SUPPORT_HARD_LINKS
1721 if (preserve_hard_links && F_IS_HLINKED(file))
1722 file->flags |= FLAG_FILE_SENT;
1724 write_ndx(f_out, ndx);
1726 int iflags = ITEM_TRANSFER;
1727 if (always_checksum > 0)
1728 iflags |= ITEM_REPORT_CHANGE;
1729 if (fnamecmp_type != FNAMECMP_FNAME)
1730 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1731 if (fnamecmp_type == FNAMECMP_FUZZY)
1732 iflags |= ITEM_XNAME_FOLLOWS;
1733 itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
1734 fuzzy_file ? fuzzy_file->basename : NULL);
1739 #ifdef SUPPORT_XATTRS
1740 if (preserve_xattrs)
1741 free_xattr(&real_sx);
1746 #ifdef SUPPORT_HARD_LINKS
1747 if (preserve_hard_links && F_IS_HLINKED(file))
1748 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1755 if (statret != 0 || whole_file)
1756 write_sum_head(f_out, NULL);
1757 else if (sx.st.st_size <= 0) {
1758 write_sum_head(f_out, NULL);
1761 if (generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy) < 0) {
1763 "WARNING: file is too large for checksum sending: %s\n",
1765 write_sum_head(f_out, NULL);
1772 int save_preserve_xattrs = preserve_xattrs;
1775 #ifdef SUPPORT_XATTRS
1776 if (preserve_xattrs) {
1777 copy_xattrs(fname, backupptr);
1778 preserve_xattrs = 0;
1781 set_file_attrs(backupptr, back_file, NULL, NULL, 0);
1782 preserve_xattrs = save_preserve_xattrs;
1783 if (INFO_GTE(BACKUP, 1)) {
1784 rprintf(FINFO, "backed up %s to %s\n",
1787 unmake_file(back_file);
1794 #ifdef SUPPORT_XATTRS
1795 if (preserve_xattrs)
1801 /* If we are replacing an existing hard link, symlink, device, or special file,
1802 * create a temp-name item and rename it into place. Only a symlink or hard
1803 * link puts a non-NULL value into the lnk arg. Only a device puts a non-0
1804 * value into the rdev arg. Specify 0 for the del_for_flag if there is not a
1805 * file to replace. This returns 1 on success and 0 on failure. */
1806 int atomic_create(struct file_struct *file, char *fname, const char *lnk,
1807 dev_t rdev, stat_x *sxp, int del_for_flag)
1809 char tmpname[MAXPATHLEN];
1810 const char *create_name;
1811 int skip_atomic, dir_in_the_way = del_for_flag && S_ISDIR(sxp->st.st_mode);
1813 if (!del_for_flag || dir_in_the_way || tmpdir || !get_tmpname(tmpname, fname, True))
1819 if (make_backups > 0 && !dir_in_the_way) {
1820 if (!make_backup(fname, skip_atomic))
1822 } else if (skip_atomic) {
1823 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1824 if (delete_item(fname, sxp->st.st_mode, del_opts | del_for_flag) != 0)
1829 create_name = skip_atomic ? fname : tmpname;
1832 #ifdef SUPPORT_LINKS
1833 if (S_ISLNK(file->mode)
1834 #ifdef SUPPORT_HARD_LINKS /* The first symlink in a hard-linked cluster is always created. */
1835 && (!F_IS_HLINKED(file) || file->flags & FLAG_HLINK_FIRST)
1838 if (do_symlink(lnk, create_name) < 0) {
1839 rsyserr(FERROR_XFER, errno, "symlink %s -> \"%s\" failed",
1840 full_fname(create_name), lnk);
1845 #ifdef SUPPORT_HARD_LINKS
1846 if (!hard_link_one(file, create_name, lnk, 0))
1850 if (do_mknod(create_name, file->mode, rdev) < 0) {
1851 rsyserr(FERROR_XFER, errno, "mknod %s failed",
1852 full_fname(create_name));
1858 if (do_rename(tmpname, fname) < 0) {
1859 rsyserr(FERROR_XFER, errno, "rename %s -> \"%s\" failed",
1860 full_fname(tmpname), full_fname(fname));
1869 #ifdef SUPPORT_HARD_LINKS
1870 static void handle_skipped_hlink(struct file_struct *file, int itemizing,
1871 enum logcode code, int f_out)
1873 char fbuf[MAXPATHLEN];
1875 struct file_list *save_flist = cur_flist;
1877 /* If we skip the last item in a chain of links and there was a
1878 * prior non-skipped hard-link waiting to finish, finish it now. */
1879 if ((new_last_ndx = skip_hard_link(file, &cur_flist)) < 0)
1882 file = cur_flist->files[new_last_ndx - cur_flist->ndx_start];
1883 cur_flist->in_progress--; /* undo prior increment */
1885 recv_generator(fbuf, file, new_last_ndx, itemizing, code, f_out);
1887 cur_flist = save_flist;
1891 static void touch_up_dirs(struct file_list *flist, int ndx)
1893 static int counter = 0;
1894 struct file_struct *file;
1900 end = flist->used - 1;
1904 /* Fix any directory permissions that were modified during the
1905 * transfer and/or re-set any tweaked modified-time values. */
1906 for (i = start; i <= end; i++, counter++) {
1907 file = flist->files[i];
1908 if (!S_ISDIR(file->mode)
1909 || (!implied_dirs && file->flags & FLAG_IMPLIED_DIR))
1911 if (DEBUG_GTE(TIME, 2)) {
1912 fname = f_name(file, NULL);
1913 rprintf(FINFO, "touch_up_dirs: %s (%d)\n",
1916 if (!F_IS_ACTIVE(file) || file->flags & FLAG_MISSING_DIR
1917 || (!need_retouch_dir_times && file->mode & S_IWUSR))
1919 fname = f_name(file, NULL);
1920 if (!(file->mode & S_IWUSR))
1921 do_chmod(fname, file->mode);
1922 if (need_retouch_dir_times) {
1924 if (link_stat(fname, &st, 0) == 0
1925 && cmp_time(st.st_mtime, file->modtime) != 0)
1926 set_modtime(fname, file->modtime, file->mode);
1928 if (counter >= loopchk_limit) {
1930 maybe_send_keepalive();
1932 maybe_flush_socket(0);
1938 void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
1940 struct file_struct *file;
1941 struct file_list *flist;
1942 char fbuf[MAXPATHLEN];
1946 #ifdef SUPPORT_HARD_LINKS
1947 if (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
1948 flist = flist_for_ndx(ndx, "check_for_finished_files.1");
1949 file = flist->files[ndx - flist->ndx_start];
1950 assert(file->flags & FLAG_HLINKED);
1951 finish_hard_link(file, f_name(file, fbuf), ndx, NULL, itemizing, code, -1);
1952 flist->in_progress--;
1957 if (check_redo && (ndx = get_redo_num()) != -1) {
1958 csum_length = SUM_LENGTH;
1959 max_size = -max_size;
1960 min_size = -min_size;
1961 ignore_existing = -ignore_existing;
1962 ignore_non_existing = -ignore_non_existing;
1963 update_only = -update_only;
1964 always_checksum = -always_checksum;
1965 size_only = -size_only;
1966 append_mode = -append_mode;
1967 make_backups = -make_backups; /* avoid dup backup w/inplace */
1971 cur_flist = flist_for_ndx(ndx, "check_for_finished_files.2");
1973 file = cur_flist->files[ndx - cur_flist->ndx_start];
1975 strlcpy(fbuf, solo_file, sizeof fbuf);
1978 recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
1979 cur_flist->to_redo--;
1983 csum_length = SHORT_SUM_LENGTH;
1984 max_size = -max_size;
1985 min_size = -min_size;
1986 ignore_existing = -ignore_existing;
1987 ignore_non_existing = -ignore_non_existing;
1988 update_only = -update_only;
1989 always_checksum = -always_checksum;
1990 size_only = -size_only;
1991 append_mode = -append_mode;
1992 make_backups = -make_backups;
1997 if (cur_flist == first_flist)
2000 /* We only get here if inc_recurse is enabled. */
2001 if (first_flist->in_progress || first_flist->to_redo)
2004 write_ndx(sock_f_out, NDX_DONE);
2005 if (!read_batch && !flist_eof) {
2007 for (flist = first_flist; flist != cur_flist; flist = flist->next)
2008 old_total += flist->used;
2009 maybe_flush_socket(!flist_eof && file_total - old_total < MIN_FILECNT_LOOKAHEAD/2);
2012 if (delete_during == 2 || !dir_tweaking) {
2013 /* Skip directory touch-up. */
2014 } else if (first_flist->parent_ndx >= 0)
2015 touch_up_dirs(dir_flist, first_flist->parent_ndx);
2017 flist_free(first_flist); /* updates first_flist */
2021 void generate_files(int f_out, const char *local_name)
2023 int i, ndx, next_loopchk = 0;
2024 char fbuf[MAXPATHLEN];
2027 int save_info_flist = info_levels[INFO_FLIST];
2028 int save_info_progress = info_levels[INFO_PROGRESS];
2030 if (protocol_version >= 29) {
2032 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
2033 code = logfile_format_has_i ? FNONE : FLOG;
2034 } else if (am_daemon) {
2035 itemizing = logfile_format_has_i && do_xfers;
2036 maybe_ATTRS_REPORT = ATTRS_REPORT;
2037 code = itemizing || !do_xfers ? FCLIENT : FINFO;
2038 } else if (!am_server) {
2039 itemizing = stdout_format_has_i;
2040 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
2041 code = itemizing ? FNONE : FINFO;
2044 maybe_ATTRS_REPORT = ATTRS_REPORT;
2047 solo_file = local_name;
2048 dir_tweaking = !(list_only || solo_file || dry_run);
2049 need_retouch_dir_times = preserve_times > 1;
2050 loopchk_limit = allowed_lull ? allowed_lull * 5 : 200;
2051 symlink_timeset_failed_flags = ITEM_REPORT_TIME
2052 | (protocol_version >= 30 || !am_server ? ITEM_REPORT_TIMEFAIL : 0);
2053 implied_dirs_are_missing = relative_paths && !implied_dirs && protocol_version < 30;
2055 if (DEBUG_GTE(GENR, 1))
2056 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
2058 if (delete_before && !solo_file && cur_flist->used > 0)
2060 if (delete_during == 2) {
2061 deldelay_size = BIGPATHBUFLEN * 4;
2062 deldelay_buf = new_array(char, deldelay_size);
2064 out_of_memory("delete-delay");
2066 info_levels[INFO_FLIST] = info_levels[INFO_PROGRESS] = 0;
2068 if (append_mode > 0 || whole_file < 0)
2070 if (DEBUG_GTE(FLIST, 1)) {
2071 rprintf(FINFO, "delta-transmission %s\n",
2073 ? "disabled for local transfer or --whole-file"
2077 /* Since we often fill up the outgoing socket and then just sit around
2078 * waiting for the other 2 processes to do their thing, we don't want
2079 * to exit on a timeout. If the data stops flowing, the receiver will
2080 * notice that and let us know via the redo pipe (or its closing). */
2083 dflt_perms = (ACCESSPERMS & ~orig_umask);
2086 #ifdef SUPPORT_HARD_LINKS
2087 if (preserve_hard_links && inc_recurse) {
2088 while (!flist_eof && file_total < MIN_FILECNT_LOOKAHEAD/2)
2089 wait_for_receiver();
2093 if (inc_recurse && cur_flist->parent_ndx >= 0) {
2094 struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
2096 strlcpy(fbuf, solo_file, sizeof fbuf);
2099 ndx = cur_flist->ndx_start - 1;
2100 recv_generator(fbuf, fp, ndx, itemizing, code, f_out);
2101 if (delete_during && dry_run < 2 && !list_only
2102 && !(fp->flags & FLAG_MISSING_DIR)) {
2103 if (fp->flags & FLAG_CONTENT_DIR) {
2105 if (one_file_system) {
2106 uint32 *devp = F_DIR_DEV_P(fp);
2107 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
2109 dirdev = MAKEDEV(0, 0);
2110 delete_in_dir(fbuf, fp, &dirdev);
2112 change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(fp));
2115 for (i = cur_flist->low; i <= cur_flist->high; i++) {
2116 struct file_struct *file = cur_flist->sorted[i];
2118 if (!F_IS_ACTIVE(file))
2124 ndx = i + cur_flist->ndx_start;
2127 strlcpy(fbuf, solo_file, sizeof fbuf);
2130 recv_generator(fbuf, file, ndx, itemizing, code, f_out);
2132 check_for_finished_files(itemizing, code, 0);
2134 if (i + cur_flist->ndx_start >= next_loopchk) {
2136 maybe_send_keepalive();
2138 maybe_flush_socket(0);
2139 next_loopchk += loopchk_limit;
2144 write_ndx(f_out, NDX_DONE);
2149 check_for_finished_files(itemizing, code, 1);
2150 if (cur_flist->next || flist_eof)
2152 wait_for_receiver();
2154 } while ((cur_flist = cur_flist->next) != NULL);
2157 delete_in_dir(NULL, NULL, &dev_zero);
2159 if (DEBUG_GTE(GENR, 1))
2160 rprintf(FINFO, "generate_files phase=%d\n", phase);
2163 check_for_finished_files(itemizing, code, 1);
2166 wait_for_receiver();
2170 if (DEBUG_GTE(GENR, 1))
2171 rprintf(FINFO, "generate_files phase=%d\n", phase);
2173 write_ndx(f_out, NDX_DONE);
2175 /* Reduce round-trip lag-time for a useless delay-updates phase. */
2176 if (protocol_version >= 29 && EARLY_DELAY_DONE_MSG())
2177 write_ndx(f_out, NDX_DONE);
2179 if (protocol_version >= 31 && EARLY_DELETE_DONE_MSG()) {
2180 if ((INFO_GTE(STATS, 2) && (delete_mode || force_delete)) || read_batch)
2181 write_del_stats(f_out);
2182 if (EARLY_DELAY_DONE_MSG()) /* Can't send this before delay */
2183 write_ndx(f_out, NDX_DONE);
2186 /* Read MSG_DONE for the redo phase (and any prior messages). */
2188 check_for_finished_files(itemizing, code, 0);
2189 if (msgdone_cnt > 1)
2191 wait_for_receiver();
2194 if (protocol_version >= 29) {
2196 if (DEBUG_GTE(GENR, 1))
2197 rprintf(FINFO, "generate_files phase=%d\n", phase);
2198 if (!EARLY_DELAY_DONE_MSG()) {
2199 write_ndx(f_out, NDX_DONE);
2200 if (protocol_version >= 31 && EARLY_DELETE_DONE_MSG())
2201 write_ndx(f_out, NDX_DONE);
2203 /* Read MSG_DONE for delay-updates phase & prior messages. */
2204 while (msgdone_cnt == 2)
2205 wait_for_receiver();
2208 info_levels[INFO_FLIST] = save_info_flist;
2209 info_levels[INFO_PROGRESS] = save_info_progress;
2211 if (delete_during == 2)
2212 do_delayed_deletions(fbuf);
2213 if (delete_after && !solo_file && file_total > 0)
2216 if (max_delete >= 0 && skipped_deletes) {
2218 "Deletions stopped due to --max-delete limit (%d skipped)\n",
2220 io_error |= IOERR_DEL_LIMIT;
2223 if (protocol_version >= 31) {
2224 if (!EARLY_DELETE_DONE_MSG()) {
2225 if (INFO_GTE(STATS, 2) || read_batch)
2226 write_del_stats(f_out);
2227 write_ndx(f_out, NDX_DONE);
2230 /* Read MSG_DONE for late-delete phase & prior messages. */
2231 while (msgdone_cnt == 3)
2232 wait_for_receiver();
2235 if ((need_retouch_dir_perms || need_retouch_dir_times)
2236 && dir_tweaking && (!inc_recurse || delete_during == 2))
2237 touch_up_dirs(dir_flist, -1);
2239 if (DEBUG_GTE(GENR, 1))
2240 rprintf(FINFO, "generate_files finished\n");