2 * Routines that are exclusive to the generator process.
4 * Copyright (C) 1996-2000 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2003-2007 Wayne Davison
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, visit the http://fsf.org website.
28 extern int stdout_format_has_i;
29 extern int logfile_format_has_i;
33 extern int inc_recurse;
34 extern int do_progress;
35 extern int relative_paths;
36 extern int implied_dirs;
37 extern int keep_dirlinks;
38 extern int preserve_acls;
39 extern int preserve_xattrs;
40 extern int preserve_links;
41 extern int preserve_devices;
42 extern int preserve_specials;
43 extern int preserve_hard_links;
44 extern int preserve_perms;
45 extern int preserve_times;
48 extern int delete_mode;
49 extern int delete_before;
50 extern int delete_during;
51 extern int delete_after;
52 extern int msgdone_cnt;
53 extern int ignore_errors;
54 extern int remove_source_files;
55 extern int delay_updates;
56 extern int update_only;
57 extern int ignore_existing;
58 extern int ignore_non_existing;
60 extern int append_mode;
61 extern int make_backups;
62 extern int csum_length;
63 extern int ignore_times;
65 extern OFF_T max_size;
66 extern OFF_T min_size;
69 extern int allowed_lull;
70 extern int sock_f_out;
71 extern int ignore_timeout;
72 extern int protocol_version;
73 extern int file_total;
74 extern int fuzzy_basis;
75 extern int always_checksum;
76 extern int checksum_len;
77 extern char *partial_dir;
78 extern char *basis_dir[];
79 extern int compare_dest;
82 extern int whole_file;
84 extern int read_batch;
85 extern int safe_symlinks;
86 extern long block_size; /* "long" because popt can't set an int32. */
87 extern int unsort_ndx;
88 extern int max_delete;
89 extern int force_delete;
90 extern int one_file_system;
91 extern struct stats stats;
92 extern dev_t filesystem_dev;
93 extern mode_t orig_umask;
95 extern char *backup_dir;
96 extern char *backup_suffix;
97 extern int backup_suffix_len;
98 extern struct file_list *cur_flist, *first_flist, *dir_flist;
99 extern struct filter_list_struct server_filter_list;
101 int ignore_perishable = 0;
102 int non_perishable_cnt = 0;
103 int maybe_ATTRS_REPORT = 0;
105 static dev_t dev_zero;
106 static int deletion_count = 0; /* used to implement --max-delete */
107 static int deldelay_size = 0, deldelay_cnt = 0;
108 static char *deldelay_buf = NULL;
109 static int deldelay_fd = -1;
111 static int dir_tweaking;
112 static int need_retouch_dir_times;
113 static int need_retouch_dir_perms;
114 static const char *solo_file = NULL;
116 /* For calling delete_item() and delete_dir_contents(). */
117 #define DEL_OWNED_BY_US (1<<0) /* file/dir has our uid */
118 #define DEL_RECURSE (1<<1) /* if dir, delete all contents */
119 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
122 TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
126 DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_NOT_EMPTY
129 /* Forward declaration for delete_item(). */
130 static enum delret delete_dir_contents(char *fname, int flags);
133 static int is_backup_file(char *fn)
135 int k = strlen(fn) - backup_suffix_len;
136 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
139 /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
140 * delete recursively.
142 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
143 * a directory! (The buffer is used for recursion, but returned unchanged.)
145 static enum delret delete_item(char *fbuf, int mode, char *replace, int flags)
152 rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
156 if (!am_root && !(mode & S_IWUSR) && flags & DEL_OWNED_BY_US)
157 do_chmod(fbuf, mode |= S_IWUSR);
159 if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
160 ignore_perishable = 1;
161 /* If DEL_RECURSE is not set, this just reports emptiness. */
162 ret = delete_dir_contents(fbuf, flags);
163 ignore_perishable = 0;
164 if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT)
166 /* OK: try to delete the directory. */
169 if (!replace && max_delete >= 0 && ++deletion_count > max_delete)
174 ok = do_rmdir(fbuf) == 0;
175 } else if (make_backups > 0 && (backup_dir || !is_backup_file(fbuf))) {
176 what = "make_backup";
177 ok = make_backup(fbuf);
180 ok = robust_unlink(fbuf) == 0;
185 log_delete(fbuf, mode);
188 if (S_ISDIR(mode) && errno == ENOTEMPTY) {
189 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
192 } else if (errno != ENOENT) {
193 rsyserr(FERROR, errno, "delete_file: %s(%s) failed",
203 if (replace && ret != DR_SUCCESS) {
204 rprintf(FERROR_XFER, "could not make way for new %s: %s\n",
210 /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
211 * its contents, otherwise just checks for content. Returns DR_SUCCESS or
212 * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
213 * buffer is used for recursion, but returned unchanged.)
215 static enum delret delete_dir_contents(char *fname, int flags)
217 struct file_list *dirlist;
225 rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n",
229 dlen = strlen(fname);
230 save_filters = push_local_filters(fname, dlen);
232 non_perishable_cnt = 0;
233 dirlist = get_dirlist(fname, dlen, 0);
234 ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
239 if (!(flags & DEL_RECURSE)) {
245 if (dlen != 1 || *fname != '/')
247 remainder = MAXPATHLEN - (p - fname);
249 /* We do our own recursion, so make delete_item() non-recursive. */
250 flags = (flags & ~DEL_RECURSE) | DEL_DIR_IS_EMPTY;
252 for (j = dirlist->used; j--; ) {
253 struct file_struct *fp = dirlist->files[j];
255 if (fp->flags & FLAG_MOUNT_DIR) {
258 "mount point, %s, pins parent directory\n",
265 strlcpy(p, fp->basename, remainder);
266 if (F_OWNER(fp) == our_uid)
267 flags |= DEL_OWNED_BY_US;
269 flags &= DEL_OWNED_BY_US;
270 /* Save stack by recursing to ourself directly. */
271 if (S_ISDIR(fp->mode)) {
272 if (!am_root && !(fp->mode & S_IWUSR) && flags & DEL_OWNED_BY_US)
273 do_chmod(fname, fp->mode |= S_IWUSR);
274 if (delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
277 if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
285 pop_local_filters(save_filters);
287 if (ret == DR_NOT_EMPTY) {
288 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
294 static int start_delete_delay_temp(void)
296 char fnametmp[MAXPATHLEN];
297 int save_dry_run = dry_run;
300 if (!get_tmpname(fnametmp, "deldelay")
301 || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
302 rprintf(FINFO, "NOTE: Unable to create delete-delay temp file%s.\n",
303 inc_recurse ? "" : " -- switching to --delete-after");
305 delete_after = !inc_recurse;
306 dry_run = save_dry_run;
310 dry_run = save_dry_run;
314 static int flush_delete_delay(void)
316 if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
317 rsyserr(FERROR, errno, "flush of delete-delay buffer");
327 static int remember_delete(struct file_struct *file, const char *fname, int flags)
329 const char *plus = (!am_root && !(file->mode & S_IWUSR) && flags & DEL_OWNED_BY_US)
334 len = snprintf(deldelay_buf + deldelay_cnt,
335 deldelay_size - deldelay_cnt,
337 plus, (int)file->mode, fname, '\0');
338 if ((deldelay_cnt += len) <= deldelay_size)
340 if (deldelay_fd < 0 && !start_delete_delay_temp())
343 if (!flush_delete_delay())
350 static int read_delay_line(char *buf, int *own_flag_p)
352 static int read_pos = 0;
354 char *bp, *past_space;
357 for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
358 if (j < deldelay_cnt)
360 if (deldelay_fd < 0) {
365 deldelay_cnt -= read_pos;
366 if (deldelay_cnt == deldelay_size)
368 if (deldelay_cnt && read_pos) {
369 memmove(deldelay_buf, deldelay_buf + read_pos,
372 len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
373 deldelay_size - deldelay_cnt);
377 "ERROR: unexpected EOF in delete-delay file.\n");
382 rsyserr(FERROR, errno,
383 "reading delete-delay file");
390 bp = deldelay_buf + read_pos;
393 *own_flag_p = DEL_OWNED_BY_US;
397 if (sscanf(bp, "%x ", &mode) != 1) {
399 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
402 past_space = strchr(bp, ' ') + 1;
403 len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
406 if (len > MAXPATHLEN) {
407 rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
411 /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
412 * instead of returning a pointer to our buffer. */
413 memcpy(buf, past_space, len);
418 static void do_delayed_deletions(char *delbuf)
422 if (deldelay_fd >= 0) {
423 if (deldelay_cnt && !flush_delete_delay())
425 lseek(deldelay_fd, 0, 0);
427 while ((mode = read_delay_line(delbuf, &own_flag)) >= 0)
428 delete_item(delbuf, mode, NULL, own_flag | DEL_RECURSE);
429 if (deldelay_fd >= 0)
433 /* This function is used to implement per-directory deletion, and is used by
434 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
435 * MAXPATHLEN buffer with the name of the directory in it (the functions we
436 * call will append names onto the end, but the old dir value will be restored
438 static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
440 static int already_warned = 0;
441 struct file_list *dirlist;
442 char delbuf[MAXPATHLEN];
446 change_local_filter_dir(NULL, 0, 0);
451 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
454 maybe_send_keepalive();
456 if (io_error && !ignore_errors) {
460 "IO error encountered -- skipping file deletion\n");
466 change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
468 if (one_file_system) {
469 if (file->flags & FLAG_TOP_DIR)
470 filesystem_dev = *fs_dev;
471 else if (filesystem_dev != *fs_dev)
475 dirlist = get_dirlist(fbuf, dlen, 0);
477 /* If an item in dirlist is not found in flist, delete it
478 * from the filesystem. */
479 for (i = dirlist->used; i--; ) {
480 struct file_struct *fp = dirlist->files[i];
481 if (!F_IS_ACTIVE(fp))
483 if (fp->flags & FLAG_MOUNT_DIR) {
485 rprintf(FINFO, "cannot delete mount point: %s\n",
489 if (flist_find(cur_flist, fp) < 0) {
490 int flags = DEL_RECURSE
491 | (F_OWNER(fp) == our_uid ? DEL_OWNED_BY_US : 0);
493 if (delete_during == 2) {
494 if (!remember_delete(fp, delbuf, flags))
497 delete_item(delbuf, fp->mode, NULL, flags);
504 /* This deletes any files on the receiving side that are not present on the
505 * sending side. This is used by --delete-before and --delete-after. */
506 static void do_delete_pass(void)
508 char fbuf[MAXPATHLEN];
512 /* dry_run is incremented when the destination doesn't exist yet. */
513 if (dry_run > 1 || list_only)
516 for (j = 0; j < cur_flist->used; j++) {
517 struct file_struct *file = cur_flist->sorted[j];
519 if (!(file->flags & FLAG_CONTENT_DIR))
523 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
524 rprintf(FINFO, "deleting in %s\n", fbuf);
526 if (link_stat(fbuf, &st, keep_dirlinks) < 0
527 || !S_ISDIR(st.st_mode))
530 delete_in_dir(fbuf, file, &st.st_dev);
532 delete_in_dir(NULL, NULL, &dev_zero);
534 if (do_progress && !am_server)
535 rprintf(FINFO, " \r");
538 int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
541 if (S_ISLNK(file->mode)) {
545 if (preserve_times && cmp_time(sxp->st.st_mtime, file->modtime) != 0)
548 if (preserve_perms && !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
551 if (am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file))
554 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
558 if (preserve_acls && !S_ISLNK(file->mode)) {
559 if (!ACL_READY(*sxp))
561 if (set_acl(NULL, file, sxp) == 0)
565 #ifdef SUPPORT_XATTRS
566 if (preserve_xattrs) {
567 if (!XATTR_READY(*sxp))
568 get_xattr(fname, sxp);
569 if (xattr_diff(file, sxp, 0))
577 void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
578 stat_x *sxp, int32 iflags, uchar fnamecmp_type,
581 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
582 int keep_time = !preserve_times ? 0
583 : S_ISDIR(file->mode) ? preserve_times > 1
584 : !S_ISLNK(file->mode);
586 if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
587 iflags |= ITEM_REPORT_SIZE;
588 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
589 && !(iflags & ITEM_MATCHED)
590 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
591 || (keep_time && cmp_time(file->modtime, sxp->st.st_mtime) != 0))
592 iflags |= ITEM_REPORT_TIME;
593 #if !defined HAVE_LCHMOD && !defined HAVE_SETATTRLIST
594 if (S_ISLNK(file->mode)) {
598 if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
599 iflags |= ITEM_REPORT_PERMS;
600 if (uid_ndx && am_root && (uid_t)F_OWNER(file) != sxp->st.st_uid)
601 iflags |= ITEM_REPORT_OWNER;
602 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
603 && sxp->st.st_gid != (gid_t)F_GROUP(file))
604 iflags |= ITEM_REPORT_GROUP;
606 if (preserve_acls && !S_ISLNK(file->mode)) {
607 if (!ACL_READY(*sxp))
608 get_acl(fnamecmp, sxp);
609 if (set_acl(NULL, file, sxp) == 0)
610 iflags |= ITEM_REPORT_ACL;
613 #ifdef SUPPORT_XATTRS
614 if (preserve_xattrs) {
615 if (!XATTR_READY(*sxp))
616 get_xattr(fnamecmp, sxp);
617 if (xattr_diff(file, sxp, 1))
618 iflags |= ITEM_REPORT_XATTR;
622 #ifdef SUPPORT_XATTRS
623 if (preserve_xattrs && xattr_diff(file, NULL, 1))
624 iflags |= ITEM_REPORT_XATTR;
626 iflags |= ITEM_IS_NEW;
630 if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || verbose > 1
631 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
632 if (protocol_version >= 29) {
634 write_ndx(sock_f_out, ndx);
635 write_shortint(sock_f_out, iflags);
636 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
637 write_byte(sock_f_out, fnamecmp_type);
638 if (iflags & ITEM_XNAME_FOLLOWS)
639 write_vstring(sock_f_out, xname, strlen(xname));
640 #ifdef SUPPORT_XATTRS
641 if (iflags & ITEM_REPORT_XATTR && !dry_run)
642 send_xattr_request(NULL, file, sock_f_out);
644 } else if (ndx >= 0) {
645 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
646 log_item(code, file, &stats, iflags, xname);
652 /* Perform our quick-check heuristic for determining if a file is unchanged. */
653 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
655 if (st->st_size != F_LENGTH(file))
658 /* if always checksum is set then we use the checksum instead
659 of the file time to determine whether to sync */
660 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
661 char sum[MAX_DIGEST_LEN];
662 file_checksum(fn, sum, st->st_size);
663 return memcmp(sum, F_SUM(file), checksum_len) == 0;
672 return cmp_time(st->st_mtime, file->modtime) == 0;
677 * set (initialize) the size entries in the per-file sum_struct
678 * calculating dynamic block and checksum sizes.
680 * This is only called from generate_and_send_sums() but is a separate
681 * function to encapsulate the logic.
683 * The block size is a rounded square root of file length.
685 * The checksum size is determined according to:
686 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
687 * provided by Donovan Baarda which gives a probability of rsync
688 * algorithm corrupting data and falling back using the whole md4
691 * This might be made one of several selectable heuristics.
693 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
699 blength = block_size;
700 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
701 blength = BLOCK_SIZE;
706 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
707 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
708 blength = MAX_BLOCK_SIZE;
713 if (len < (int64)blength * blength)
716 } while (c >= 8); /* round to multiple of 8 */
717 blength = MAX(blength, BLOCK_SIZE);
721 if (protocol_version < 27) {
722 s2length = csum_length;
723 } else if (csum_length == SUM_LENGTH) {
724 s2length = SUM_LENGTH;
728 int b = BLOCKSUM_BIAS;
729 for (l = len; l >>= 1; b += 2) {}
730 for (c = blength; (c >>= 1) && b; b--) {}
731 /* add a bit, subtract rollsum, round up. */
732 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
733 s2length = MAX(s2length, csum_length);
734 s2length = MIN(s2length, SUM_LENGTH);
738 sum->blength = blength;
739 sum->s2length = s2length;
740 sum->remainder = (int32)(len % blength);
741 sum->count = (int32)(len / blength) + (sum->remainder != 0);
743 if (sum->count && verbose > 2) {
745 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
746 (double)sum->count, (long)sum->remainder, (long)sum->blength,
747 sum->s2length, (double)sum->flength);
753 * Generate and send a stream of signatures/checksums that describe a buffer
755 * Generate approximately one checksum every block_len bytes.
757 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
760 struct map_struct *mapbuf;
761 struct sum_struct sum;
764 sum_sizes_sqroot(&sum, len);
765 write_sum_head(f_out, &sum);
767 if (append_mode > 0 && f_copy < 0)
771 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
775 for (i = 0; i < sum.count; i++) {
776 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
777 char *map = map_ptr(mapbuf, offset, n1);
778 char sum2[SUM_LENGTH];
785 full_write(f_copy, map, n1);
790 sum1 = get_checksum1(map, n1);
791 get_checksum2(map, n1, sum2);
795 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
796 (double)i, (double)offset - n1, (long)n1,
797 (unsigned long)sum1);
799 write_int(f_out, sum1);
800 write_buf(f_out, sum2, sum.s2length);
808 /* Try to find a filename in the same dir as "fname" with a similar name. */
809 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
811 int fname_len, fname_suf_len;
812 const char *fname_suf, *fname = file->basename;
813 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
814 int j, lowest_j = -1;
816 fname_len = strlen(fname);
817 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
819 for (j = 0; j < dirlist->used; j++) {
820 struct file_struct *fp = dirlist->files[j];
821 const char *suf, *name;
825 if (!S_ISREG(fp->mode) || !F_LENGTH(fp)
826 || fp->flags & FLAG_FILE_SENT)
831 if (F_LENGTH(fp) == F_LENGTH(file)
832 && cmp_time(fp->modtime, file->modtime) == 0) {
835 "fuzzy size/modtime match for %s\n",
842 suf = find_filename_suffix(name, len, &suf_len);
844 dist = fuzzy_distance(name, len, fname, fname_len);
845 /* Add some extra weight to how well the suffixes match. */
846 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
849 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
850 name, (int)(dist>>16), (int)(dist&0xFFFF));
852 if (dist <= lowest_dist) {
861 /* Copy a file found in our --copy-dest handling. */
862 static int copy_altdest_file(const char *src, const char *dest, struct file_struct *file)
864 char buf[MAXPATHLEN];
865 const char *copy_to, *partialptr;
869 /* Let copy_file open the destination in place. */
873 fd_w = open_tmpfile(buf, dest, file);
878 cleanup_set(copy_to, NULL, NULL, -1, -1);
879 if (copy_file(src, copy_to, fd_w, file->mode, 0) < 0) {
881 rsyserr(FINFO, errno, "copy_file %s => %s",
882 full_fname(src), copy_to);
884 /* Try to clean up. */
889 partialptr = partial_dir ? partial_dir_fname(dest) : NULL;
890 ok = finish_transfer(dest, copy_to, src, partialptr, file, 1, 0);
895 /* This is only called for regular files. We return -2 if we've finished
896 * handling the file, -1 if no dest-linking occurred, or a non-negative
897 * value if we found an alternate basis file. */
898 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
899 char *cmpbuf, stat_x *sxp, int itemizing,
907 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
908 if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
910 switch (match_level) {
916 if (!unchanged_file(cmpbuf, file, &sxp->st))
922 if (!unchanged_attrs(cmpbuf, file, sxp))
929 } while (basis_dir[++j] != NULL);
934 if (j != best_match) {
936 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
937 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
941 if (match_level == 3 && !copy_dest) {
942 #ifdef SUPPORT_HARD_LINKS
944 if (!hard_link_one(file, fname, cmpbuf, 1))
946 if (preserve_hard_links && F_IS_HLINKED(file))
947 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, j);
948 if (itemizing && (verbose > 1 || stdout_format_has_i > 1)) {
949 itemize(cmpbuf, file, ndx, 1, sxp,
950 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
956 itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
957 if (verbose > 1 && maybe_ATTRS_REPORT)
958 rprintf(FCLIENT, "%s is uptodate\n", fname);
962 if (match_level >= 2) {
963 #ifdef SUPPORT_HARD_LINKS
964 try_a_copy: /* Copy the file locally. */
966 if (!dry_run && copy_altdest_file(cmpbuf, fname, file) < 0)
969 itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
970 if (maybe_ATTRS_REPORT
971 && ((!itemizing && verbose && match_level == 2)
972 || (verbose > 1 && match_level == 3))) {
973 code = match_level == 3 ? FCLIENT : FINFO;
974 rprintf(code, "%s%s\n", fname,
975 match_level == 3 ? " is uptodate" : "");
977 #ifdef SUPPORT_HARD_LINKS
978 if (preserve_hard_links && F_IS_HLINKED(file))
979 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, -1);
984 return FNAMECMP_BASIS_DIR_LOW + j;
987 /* This is only called for non-regular files. We return -2 if we've finished
988 * handling the file, or -1 if no dest-linking occurred, or a non-negative
989 * value if we found an alternate basis file. */
990 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
991 char *cmpbuf, stat_x *sxp, int itemizing,
994 char lnk[MAXPATHLEN];
997 enum nonregtype type;
1001 #ifndef SUPPORT_LINKS
1002 if (S_ISLNK(file->mode))
1005 if (S_ISDIR(file->mode)) {
1007 } else if (IS_SPECIAL(file->mode))
1008 type = TYPE_SPECIAL;
1009 else if (IS_DEVICE(file->mode))
1011 #ifdef SUPPORT_LINKS
1012 else if (S_ISLNK(file->mode))
1013 type = TYPE_SYMLINK;
1017 "internal: try_dests_non() called with invalid mode (%o)\n",
1019 exit_cleanup(RERR_UNSUPPORTED);
1023 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1024 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1028 if (!S_ISDIR(sxp->st.st_mode))
1032 if (!IS_SPECIAL(sxp->st.st_mode))
1036 if (!IS_DEVICE(sxp->st.st_mode))
1039 #ifdef SUPPORT_LINKS
1041 if (!S_ISLNK(sxp->st.st_mode))
1046 if (match_level < 1) {
1055 devp = F_RDEV_P(file);
1056 if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
1059 #ifdef SUPPORT_LINKS
1061 if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
1064 if (strcmp(lnk, F_SYMLINK(file)) != 0)
1069 if (match_level < 2) {
1073 if (unchanged_attrs(cmpbuf, file, sxp)) {
1078 } while (basis_dir[++j] != NULL);
1083 if (j != best_match) {
1085 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1086 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1090 if (match_level == 3) {
1091 #ifdef SUPPORT_HARD_LINKS
1093 #ifndef CAN_HARDLINK_SYMLINK
1094 && !S_ISLNK(file->mode)
1096 #ifndef CAN_HARDLINK_SPECIAL
1097 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
1099 && !S_ISDIR(file->mode)) {
1100 if (do_link(cmpbuf, fname) < 0) {
1101 rsyserr(FERROR_XFER, errno,
1102 "failed to hard-link %s with %s",
1106 if (preserve_hard_links && F_IS_HLINKED(file))
1107 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1111 if (itemizing && stdout_format_has_i
1112 && (verbose > 1 || stdout_format_has_i > 1)) {
1113 int chg = compare_dest && type != TYPE_DIR ? 0
1115 + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1116 char *lp = match_level == 3 ? "" : NULL;
1117 itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
1119 if (verbose > 1 && maybe_ATTRS_REPORT) {
1120 rprintf(FCLIENT, "%s%s is uptodate\n",
1121 fname, type == TYPE_DIR ? "/" : "");
1129 static void list_file_entry(struct file_struct *f)
1131 char permbuf[PERMSTRING_SIZE];
1134 if (!F_IS_ACTIVE(f)) {
1135 /* this can happen if duplicate names were removed */
1139 permstring(permbuf, f->mode);
1142 /* TODO: indicate '+' if the entry has an ACL. */
1144 #ifdef SUPPORT_LINKS
1145 if (preserve_links && S_ISLNK(f->mode)) {
1146 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
1147 permbuf, len, timestring(f->modtime),
1148 f_name(f, NULL), F_SYMLINK(f));
1152 rprintf(FINFO, "%s %11.0f %s %s\n",
1153 permbuf, len, timestring(f->modtime),
1158 static int phase = 0;
1159 static int dflt_perms;
1161 /* Acts on the indicated item in cur_flist whose name is fname. If a dir,
1162 * make sure it exists, and has the right permissions/timestamp info. For
1163 * all other non-regular files (symlinks, etc.) we create them here. For
1164 * regular files that have changed, we try to find a basis file and then
1165 * start sending checksums. The ndx is the file's unique index value.
1167 * When fname is non-null, it must point to a MAXPATHLEN buffer!
1169 * Note that f_out is set to -1 when doing final directory-permission and
1170 * modification-time repair. */
1171 static void recv_generator(char *fname, struct file_struct *file, int ndx,
1172 int itemizing, enum logcode code, int f_out)
1174 static int missing_below = -1, excluded_below = -1;
1175 static const char *parent_dirname = "";
1176 static struct file_struct *missing_dir = NULL, *excluded_dir = NULL;
1177 static struct file_list *fuzzy_dirlist = NULL;
1178 static int need_fuzzy_dirlist = 0;
1179 struct file_struct *fuzzy_file = NULL;
1180 int fd = -1, f_copy = -1;
1182 STRUCT_STAT partial_st;
1183 struct file_struct *back_file = NULL;
1184 int statret, real_ret, stat_errno;
1185 char *fnamecmp, *partialptr, *backupptr = NULL;
1186 char fnamecmpbuf[MAXPATHLEN];
1187 uchar fnamecmp_type;
1188 int implied_dirs_are_missing = relative_paths && !implied_dirs && protocol_version < 30;
1189 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1192 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
1195 if (S_ISDIR(file->mode)
1196 && ((!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
1197 || (inc_recurse && ndx != cur_flist->ndx_start - 1)))
1199 list_file_entry(file);
1203 if (server_filter_list.head) {
1204 if (excluded_below >= 0) {
1205 if (F_DEPTH(file) > excluded_below
1206 && (!implied_dirs_are_missing || f_name_has_prefix(file, excluded_dir)))
1208 excluded_below = -1;
1210 if (check_filter(&server_filter_list, fname,
1211 S_ISDIR(file->mode)) < 0) {
1212 if (S_ISDIR(file->mode)) {
1213 excluded_below = F_DEPTH(file);
1214 excluded_dir = file;
1219 "skipping server-excluded file \"%s\"\n",
1226 if (missing_below >= 0) {
1227 if (F_DEPTH(file) <= missing_below
1228 || (implied_dirs_are_missing && !f_name_has_prefix(file, missing_dir))) {
1232 } else if (!dry_run) {
1233 if (S_ISDIR(file->mode))
1234 file->flags |= FLAG_MISSING_DIR;
1239 sx.acc_acl = sx.def_acl = NULL;
1241 #ifdef SUPPORT_XATTRS
1245 if (fuzzy_dirlist) {
1246 flist_free(fuzzy_dirlist);
1247 fuzzy_dirlist = NULL;
1249 parent_dirname = "";
1251 stat_errno = ENOENT;
1253 const char *dn = file->dirname ? file->dirname : ".";
1254 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1255 if (relative_paths && !implied_dirs
1256 && do_stat(dn, &sx.st) < 0
1257 && create_directory_path(fname) < 0) {
1258 rsyserr(FERROR_XFER, errno,
1259 "recv_generator: mkdir %s failed",
1262 if (fuzzy_dirlist) {
1263 flist_free(fuzzy_dirlist);
1264 fuzzy_dirlist = NULL;
1267 need_fuzzy_dirlist = 1;
1269 if (!preserve_perms)
1270 dflt_perms = default_perms_for_dir(dn);
1273 parent_dirname = dn;
1275 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
1276 strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1277 fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
1278 need_fuzzy_dirlist = 0;
1281 statret = link_stat(fname, &sx.st,
1282 keep_dirlinks && S_ISDIR(file->mode));
1286 if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
1288 rprintf(FINFO, "not creating new %s \"%s\"\n",
1289 S_ISDIR(file->mode) ? "directory" : "file",
1292 if (S_ISDIR(file->mode)) {
1293 if (missing_below < 0) {
1296 missing_below = F_DEPTH(file);
1299 file->flags |= FLAG_MISSING_DIR;
1304 if (statret == 0 && F_OWNER(file) == our_uid)
1305 del_opts |= DEL_OWNED_BY_US;
1307 if (S_ISDIR(file->mode)) {
1308 if (!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
1310 if (inc_recurse && ndx != cur_flist->ndx_start - 1) {
1311 /* In inc_recurse mode we want to make sure any missing
1312 * directories get created while we're still processing
1313 * the parent dir (which allows us to touch the parent
1314 * dir's mtime right away). We will handle the dir in
1315 * full later (right before we handle its contents). */
1317 && (S_ISDIR(sx.st.st_mode)
1318 || delete_item(fname, sx.st.st_mode, "directory", del_opts) != 0))
1319 goto cleanup; /* Any errors get reported later. */
1320 if (do_mkdir(fname, file->mode & 0700) == 0)
1321 file->flags |= FLAG_DIR_CREATED;
1324 /* The file to be received is a directory, so we need
1325 * to prepare appropriately. If there is already a
1326 * file of that name and it is *not* a directory, then
1327 * we need to delete it. If it doesn't exist, then
1328 * (perhaps recursively) create it. */
1329 if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1330 if (delete_item(fname, sx.st.st_mode, "directory", del_opts) != 0)
1331 goto skipping_dir_contents;
1334 if (dry_run && statret != 0 && missing_below < 0) {
1335 missing_below = F_DEPTH(file);
1341 if (file->flags & FLAG_DIR_CREATED)
1343 if (!preserve_perms) { /* See comment in non-dir code below. */
1344 file->mode = dest_mode(file->mode, sx.st.st_mode,
1345 dflt_perms, statret == 0);
1347 if (statret != 0 && basis_dir[0] != NULL) {
1348 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1356 if (itemizing && f_out != -1) {
1357 itemize(fname, file, ndx, statret, &sx,
1358 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1360 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1361 if (!relative_paths || errno != ENOENT
1362 || create_directory_path(fname) < 0
1363 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
1364 rsyserr(FERROR_XFER, errno,
1365 "recv_generator: mkdir %s failed",
1367 skipping_dir_contents:
1369 "*** Skipping any contents from this failed directory ***\n");
1370 missing_below = F_DEPTH(file);
1372 file->flags |= FLAG_MISSING_DIR;
1376 if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
1377 && verbose && code != FNONE && f_out != -1)
1378 rprintf(code, "%s/\n", fname);
1380 /* We need to ensure that the dirs in the transfer have writable
1381 * permissions during the time we are putting files within them.
1382 * This is then fixed after the transfer is done. */
1384 if (!am_root && !(file->mode & S_IWUSR) && dir_tweaking) {
1385 mode_t mode = file->mode | S_IWUSR;
1386 if (do_chmod(fname, mode) < 0) {
1387 rsyserr(FERROR_XFER, errno,
1388 "failed to modify permissions on %s",
1391 need_retouch_dir_perms = 1;
1395 if (real_ret != 0 && one_file_system)
1396 real_sx.st.st_dev = filesystem_dev;
1398 if (one_file_system) {
1399 uint32 *devp = F_DIR_DEV_P(file);
1400 DEV_MAJOR(devp) = major(real_sx.st.st_dev);
1401 DEV_MINOR(devp) = minor(real_sx.st.st_dev);
1404 else if (delete_during && f_out != -1 && !phase && dry_run < 2
1405 && (file->flags & FLAG_CONTENT_DIR))
1406 delete_in_dir(fname, file, &real_sx.st.st_dev);
1410 /* If we're not preserving permissions, change the file-list's
1411 * mode based on the local permissions and some heuristics. */
1412 if (!preserve_perms) {
1413 int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
1414 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
1418 #ifdef SUPPORT_HARD_LINKS
1419 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1420 && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
1424 if (preserve_links && S_ISLNK(file->mode)) {
1425 #ifdef SUPPORT_LINKS
1426 const char *sl = F_SYMLINK(file);
1427 if (safe_symlinks && unsafe_symlink(sl, fname)) {
1430 fname = f_name(file, NULL);
1432 "ignoring unsafe symlink %s -> \"%s\"\n",
1433 full_fname(fname), sl);
1438 char lnk[MAXPATHLEN];
1441 if (!S_ISLNK(sx.st.st_mode))
1443 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1444 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1445 /* The link is pointing to the right place. */
1446 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1448 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1449 #if defined SUPPORT_HARD_LINKS && defined CAN_HARDLINK_SYMLINK
1450 if (preserve_hard_links && F_IS_HLINKED(file))
1451 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1453 if (remove_source_files == 1)
1454 goto return_with_success;
1457 /* Not the right symlink (or not a symlink), so
1459 if (delete_item(fname, sx.st.st_mode, "symlink", del_opts) != 0)
1461 } else if (basis_dir[0] != NULL) {
1462 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1465 #ifndef CAN_HARDLINK_SYMLINK
1467 /* Resort to --copy-dest behavior. */
1477 #ifdef SUPPORT_HARD_LINKS
1478 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1479 cur_flist->in_progress++;
1483 if (do_symlink(sl, fname) != 0) {
1484 rsyserr(FERROR_XFER, errno, "symlink %s -> \"%s\" failed",
1485 full_fname(fname), sl);
1487 set_file_attrs(fname, file, NULL, NULL, 0);
1489 itemize(fname, file, ndx, statret, &sx,
1490 ITEM_LOCAL_CHANGE, 0, NULL);
1492 if (code != FNONE && verbose)
1493 rprintf(code, "%s -> %s\n", fname, sl);
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 /* This does not check remove_source_files == 1
1499 * because this is one of the items that the old
1500 * --remove-sent-files option would remove. */
1501 if (remove_source_files)
1502 goto return_with_success;
1508 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1509 || (preserve_specials && IS_SPECIAL(file->mode))) {
1510 uint32 *devp = F_RDEV_P(file);
1511 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1514 if (IS_DEVICE(file->mode)) {
1515 if (!IS_DEVICE(sx.st.st_mode))
1519 if (!IS_SPECIAL(sx.st.st_mode))
1524 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
1525 && sx.st.st_rdev == rdev) {
1526 /* The device or special file is identical. */
1527 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1529 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1530 #ifdef SUPPORT_HARD_LINKS
1531 if (preserve_hard_links && F_IS_HLINKED(file))
1532 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1534 if (remove_source_files == 1)
1535 goto return_with_success;
1538 if (delete_item(fname, sx.st.st_mode, t, del_opts) != 0)
1540 } else if (basis_dir[0] != NULL) {
1541 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1544 #ifndef CAN_HARDLINK_SPECIAL
1546 /* Resort to --copy-dest behavior. */
1556 #ifdef SUPPORT_HARD_LINKS
1557 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1558 cur_flist->in_progress++;
1563 rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1564 fname, (int)file->mode,
1565 (long)major(rdev), (long)minor(rdev));
1567 if (do_mknod(fname, file->mode, rdev) < 0) {
1568 rsyserr(FERROR_XFER, errno, "mknod %s failed",
1571 set_file_attrs(fname, file, NULL, NULL, 0);
1573 itemize(fname, file, ndx, statret, &sx,
1574 ITEM_LOCAL_CHANGE, 0, NULL);
1576 if (code != FNONE && verbose)
1577 rprintf(code, "%s\n", fname);
1578 #ifdef SUPPORT_HARD_LINKS
1579 if (preserve_hard_links && F_IS_HLINKED(file))
1580 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1582 if (remove_source_files == 1)
1583 goto return_with_success;
1588 if (!S_ISREG(file->mode)) {
1590 fname = f_name(file, NULL);
1591 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1595 if (max_size > 0 && F_LENGTH(file) > max_size) {
1598 fname = f_name(file, NULL);
1599 rprintf(FINFO, "%s is over max-size\n", fname);
1603 if (min_size > 0 && F_LENGTH(file) < min_size) {
1606 fname = f_name(file, NULL);
1607 rprintf(FINFO, "%s is under min-size\n", fname);
1612 if (ignore_existing > 0 && statret == 0) {
1614 rprintf(FINFO, "%s exists\n", fname);
1618 if (update_only > 0 && statret == 0
1619 && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
1621 rprintf(FINFO, "%s is newer\n", fname);
1626 fnamecmp_type = FNAMECMP_FNAME;
1628 if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
1629 if (delete_item(fname, sx.st.st_mode, "regular file", del_opts) != 0)
1632 stat_errno = ENOENT;
1635 if (statret != 0 && basis_dir[0] != NULL) {
1636 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
1639 if (remove_source_files == 1)
1640 goto return_with_success;
1644 fnamecmp = fnamecmpbuf;
1653 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1654 && link_stat(partialptr, &partial_st, 0) == 0
1655 && S_ISREG(partial_st.st_mode)) {
1657 goto prepare_to_open;
1661 if (statret != 0 && fuzzy_dirlist && dry_run <= 1) {
1662 int j = find_fuzzy(file, fuzzy_dirlist);
1664 fuzzy_file = fuzzy_dirlist->files[j];
1665 f_name(fuzzy_file, fnamecmpbuf);
1667 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1668 fname, fnamecmpbuf);
1670 sx.st.st_size = F_LENGTH(fuzzy_file);
1672 fnamecmp = fnamecmpbuf;
1673 fnamecmp_type = FNAMECMP_FUZZY;
1678 #ifdef SUPPORT_HARD_LINKS
1679 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1680 cur_flist->in_progress++;
1684 if (stat_errno == ENOENT)
1686 rsyserr(FERROR_XFER, stat_errno, "recv_generator: failed to stat %s",
1691 if (append_mode > 0 && sx.st.st_size >= F_LENGTH(file))
1694 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1696 else if (fnamecmp_type == FNAMECMP_FUZZY)
1698 else if (unchanged_file(fnamecmp, file, &sx.st)) {
1700 do_unlink(partialptr);
1701 handle_partial_dir(partialptr, PDIR_DELETE);
1703 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1705 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
1706 #ifdef SUPPORT_HARD_LINKS
1707 if (preserve_hard_links && F_IS_HLINKED(file))
1708 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1710 if (remove_source_files != 1)
1712 return_with_success:
1714 send_msg_int(MSG_SUCCESS, ndx);
1721 fnamecmp = partialptr;
1722 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1729 if (read_batch || whole_file) {
1730 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1731 if (!(backupptr = get_backup_name(fname)))
1733 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
1734 goto pretend_missing;
1735 if (copy_file(fname, backupptr, -1, back_file->mode, 1) < 0) {
1736 unmake_file(back_file);
1744 if (fuzzy_dirlist) {
1745 int j = flist_find(fuzzy_dirlist, file);
1746 if (j >= 0) /* don't use changing file as future fuzzy basis */
1747 fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
1751 if ((fd = do_open(fnamecmp, O_RDONLY, 0)) < 0) {
1752 rsyserr(FERROR, errno, "failed to open %s, continuing",
1753 full_fname(fnamecmp));
1755 /* pretend the file didn't exist */
1756 #ifdef SUPPORT_HARD_LINKS
1757 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1758 cur_flist->in_progress++;
1762 statret = real_ret = -1;
1766 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1767 if (!(backupptr = get_backup_name(fname))) {
1771 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1773 goto pretend_missing;
1775 if (robust_unlink(backupptr) && errno != ENOENT) {
1776 rsyserr(FERROR_XFER, errno, "unlink %s",
1777 full_fname(backupptr));
1778 unmake_file(back_file);
1783 if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0
1784 && (errno != ENOENT || make_bak_dir(backupptr) < 0
1785 || (f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0)) {
1786 rsyserr(FERROR_XFER, errno, "open %s",
1787 full_fname(backupptr));
1788 unmake_file(back_file);
1793 fnamecmp_type = FNAMECMP_BACKUP;
1797 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1798 fnamecmp, (double)sx.st.st_size);
1802 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1805 if (remove_source_files && !delay_updates && !phase)
1806 increment_active_files(ndx, itemizing, code);
1807 if (inc_recurse && !dry_run)
1808 cur_flist->in_progress++;
1809 #ifdef SUPPORT_HARD_LINKS
1810 if (preserve_hard_links && F_IS_HLINKED(file))
1811 file->flags |= FLAG_FILE_SENT;
1813 write_ndx(f_out, ndx);
1815 int iflags = ITEM_TRANSFER;
1816 if (always_checksum > 0)
1817 iflags |= ITEM_REPORT_CHECKSUM;
1818 if (fnamecmp_type != FNAMECMP_FNAME)
1819 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1820 if (fnamecmp_type == FNAMECMP_FUZZY)
1821 iflags |= ITEM_XNAME_FOLLOWS;
1822 itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
1823 fuzzy_file ? fuzzy_file->basename : NULL);
1828 #ifdef SUPPORT_XATTRS
1829 if (preserve_xattrs)
1830 free_xattr(&real_sx);
1835 #ifdef SUPPORT_HARD_LINKS
1836 if (preserve_hard_links && F_IS_HLINKED(file))
1837 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1844 if (statret != 0 || whole_file)
1845 write_sum_head(f_out, NULL);
1847 generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy);
1855 set_file_attrs(backupptr, back_file, NULL, NULL, 0);
1857 rprintf(FINFO, "backed up %s to %s\n",
1860 unmake_file(back_file);
1867 #ifdef SUPPORT_XATTRS
1868 if (preserve_xattrs)
1874 static void touch_up_dirs(struct file_list *flist, int ndx)
1876 static int counter = 0;
1877 struct file_struct *file;
1883 end = flist->used - 1;
1887 /* Fix any directory permissions that were modified during the
1888 * transfer and/or re-set any tweaked modified-time values. */
1889 for (i = start; i <= end; i++, counter++) {
1890 file = flist->files[i];
1891 if (!S_ISDIR(file->mode)
1892 || (!implied_dirs && file->flags & FLAG_IMPLIED_DIR))
1895 fname = f_name(file, NULL);
1896 rprintf(FINFO, "touch_up_dirs: %s (%d)\n",
1899 if (!F_IS_ACTIVE(file) || file->flags & FLAG_MISSING_DIR
1900 || (!need_retouch_dir_times && file->mode & S_IWUSR))
1902 fname = f_name(file, NULL);
1903 if (!(file->mode & S_IWUSR))
1904 do_chmod(fname, file->mode);
1905 if (need_retouch_dir_times)
1906 set_modtime(fname, file->modtime, file->mode);
1907 if (allowed_lull && !(counter % lull_mod))
1908 maybe_send_keepalive();
1909 else if (!(counter & 0xFF))
1910 maybe_flush_socket(0);
1914 void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
1916 struct file_struct *file;
1917 struct file_list *flist;
1918 char fbuf[MAXPATHLEN];
1922 #ifdef SUPPORT_HARD_LINKS
1923 if (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
1924 flist = flist_for_ndx(ndx);
1925 assert(flist != NULL);
1926 file = flist->files[ndx - flist->ndx_start];
1927 assert(file->flags & FLAG_HLINKED);
1928 finish_hard_link(file, f_name(file, fbuf), ndx, NULL, itemizing, code, -1);
1929 flist->in_progress--;
1934 if (check_redo && (ndx = get_redo_num()) != -1) {
1935 csum_length = SUM_LENGTH;
1936 max_size = -max_size;
1937 min_size = -min_size;
1938 ignore_existing = -ignore_existing;
1939 ignore_non_existing = -ignore_non_existing;
1940 update_only = -update_only;
1941 always_checksum = -always_checksum;
1942 size_only = -size_only;
1943 append_mode = -append_mode;
1944 make_backups = -make_backups; /* avoid dup backup w/inplace */
1948 cur_flist = flist_for_ndx(ndx);
1950 file = cur_flist->files[ndx - cur_flist->ndx_start];
1952 strlcpy(fbuf, solo_file, sizeof fbuf);
1955 recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
1956 cur_flist->to_redo--;
1960 csum_length = SHORT_SUM_LENGTH;
1961 max_size = -max_size;
1962 min_size = -min_size;
1963 ignore_existing = -ignore_existing;
1964 ignore_non_existing = -ignore_non_existing;
1965 update_only = -update_only;
1966 always_checksum = -always_checksum;
1967 size_only = -size_only;
1968 append_mode = -append_mode;
1969 make_backups = -make_backups;
1974 if (cur_flist == first_flist)
1977 /* We only get here if inc_recurse is enabled. */
1978 if (first_flist->in_progress || first_flist->to_redo)
1982 write_ndx(sock_f_out, NDX_DONE);
1983 maybe_flush_socket(1);
1986 if (delete_during == 2 || !dir_tweaking) {
1987 /* Skip directory touch-up. */
1988 } else if (first_flist->parent_ndx >= 0)
1989 touch_up_dirs(dir_flist, first_flist->parent_ndx);
1991 flist_free(first_flist); /* updates first_flist */
1995 void generate_files(int f_out, const char *local_name)
1998 char fbuf[MAXPATHLEN];
2001 int save_do_progress = do_progress;
2003 if (protocol_version >= 29) {
2005 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
2006 code = logfile_format_has_i ? FNONE : FLOG;
2007 } else if (am_daemon) {
2008 itemizing = logfile_format_has_i && do_xfers;
2009 maybe_ATTRS_REPORT = ATTRS_REPORT;
2010 code = itemizing || !do_xfers ? FCLIENT : FINFO;
2011 } else if (!am_server) {
2012 itemizing = stdout_format_has_i;
2013 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
2014 code = itemizing ? FNONE : FINFO;
2017 maybe_ATTRS_REPORT = ATTRS_REPORT;
2020 solo_file = local_name;
2021 dir_tweaking = !(list_only || solo_file || dry_run);
2022 need_retouch_dir_times = preserve_times > 1;
2023 lull_mod = allowed_lull * 5;
2026 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
2028 if (delete_before && !solo_file && cur_flist->used > 0)
2030 if (delete_during == 2) {
2031 deldelay_size = BIGPATHBUFLEN * 4;
2032 deldelay_buf = new_array(char, deldelay_size);
2034 out_of_memory("delete-delay");
2038 if (append_mode > 0 || whole_file < 0)
2041 rprintf(FINFO, "delta-transmission %s\n",
2043 ? "disabled for local transfer or --whole-file"
2047 /* Since we often fill up the outgoing socket and then just sit around
2048 * waiting for the other 2 processes to do their thing, we don't want
2049 * to exit on a timeout. If the data stops flowing, the receiver will
2050 * notice that and let us know via the redo pipe (or its closing). */
2053 dflt_perms = (ACCESSPERMS & ~orig_umask);
2056 #ifdef SUPPORT_HARD_LINKS
2057 if (preserve_hard_links && inc_recurse) {
2058 while (!flist_eof && file_total < FILECNT_LOOKAHEAD/2)
2059 wait_for_receiver();
2063 if (inc_recurse && cur_flist->parent_ndx >= 0) {
2064 struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
2066 ndx = cur_flist->ndx_start - 1;
2067 recv_generator(fbuf, fp, ndx, itemizing, code, f_out);
2068 if (delete_during && dry_run < 2 && !list_only) {
2069 if (BITS_SETnUNSET(fp->flags, FLAG_CONTENT_DIR, FLAG_MISSING_DIR)) {
2071 if (one_file_system) {
2072 uint32 *devp = F_DIR_DEV_P(fp);
2073 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
2075 dirdev = MAKEDEV(0, 0);
2076 delete_in_dir(f_name(fp, fbuf), fp, &dirdev);
2080 for (i = cur_flist->low; i <= cur_flist->high; i++) {
2081 struct file_struct *file = cur_flist->sorted[i];
2083 if (!F_IS_ACTIVE(file))
2089 ndx = i + cur_flist->ndx_start;
2092 strlcpy(fbuf, solo_file, sizeof fbuf);
2095 recv_generator(fbuf, file, ndx, itemizing, code, f_out);
2097 check_for_finished_files(itemizing, code, 0);
2099 if (allowed_lull && !(i % lull_mod))
2100 maybe_send_keepalive();
2101 else if (!(i & 0xFF))
2102 maybe_flush_socket(0);
2106 write_ndx(f_out, NDX_DONE);
2111 check_for_finished_files(itemizing, code, 1);
2112 if (cur_flist->next || flist_eof)
2114 wait_for_receiver();
2116 } while ((cur_flist = cur_flist->next) != NULL);
2119 delete_in_dir(NULL, NULL, &dev_zero);
2122 rprintf(FINFO, "generate_files phase=%d\n", phase);
2125 check_for_finished_files(itemizing, code, 1);
2128 wait_for_receiver();
2133 rprintf(FINFO, "generate_files phase=%d\n", phase);
2135 write_ndx(f_out, NDX_DONE);
2136 /* Reduce round-trip lag-time for a useless delay-updates phase. */
2137 if (protocol_version >= 29 && !delay_updates)
2138 write_ndx(f_out, NDX_DONE);
2140 /* Read MSG_DONE for the redo phase (and any prior messages). */
2142 check_for_finished_files(itemizing, code, 0);
2143 if (msgdone_cnt > 1)
2145 wait_for_receiver();
2148 if (protocol_version >= 29) {
2151 rprintf(FINFO, "generate_files phase=%d\n", phase);
2153 write_ndx(f_out, NDX_DONE);
2154 /* Read MSG_DONE for delay-updates phase & prior messages. */
2155 while (msgdone_cnt == 2)
2156 wait_for_receiver();
2159 do_progress = save_do_progress;
2160 if (delete_during == 2)
2161 do_delayed_deletions(fbuf);
2162 if (delete_after && !solo_file && file_total > 0)
2165 if ((need_retouch_dir_perms || need_retouch_dir_times)
2166 && dir_tweaking && (!inc_recurse || delete_during == 2))
2167 touch_up_dirs(dir_flist, -1);
2169 if (max_delete >= 0 && deletion_count > max_delete) {
2171 "Deletions stopped due to --max-delete limit (%d skipped)\n",
2172 deletion_count - max_delete);
2173 io_error |= IOERR_DEL_LIMIT;
2177 rprintf(FINFO, "generate_files finished\n");