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_executability;
45 extern int preserve_perms;
46 extern int preserve_times;
49 extern int delete_mode;
50 extern int delete_before;
51 extern int delete_during;
52 extern int delete_after;
53 extern int msgdone_cnt;
54 extern int ignore_errors;
55 extern int remove_source_files;
56 extern int delay_updates;
57 extern int update_only;
58 extern int ignore_existing;
59 extern int ignore_non_existing;
61 extern int append_mode;
62 extern int make_backups;
63 extern int csum_length;
64 extern int ignore_times;
66 extern OFF_T max_size;
67 extern OFF_T min_size;
70 extern int allowed_lull;
71 extern int sock_f_out;
72 extern int ignore_timeout;
73 extern int protocol_version;
74 extern int file_total;
75 extern int fuzzy_basis;
76 extern int always_checksum;
77 extern int checksum_len;
78 extern char *partial_dir;
79 extern char *basis_dir[];
80 extern int compare_dest;
83 extern int whole_file;
85 extern int read_batch;
86 extern int safe_symlinks;
87 extern long block_size; /* "long" because popt can't set an int32. */
88 extern int unsort_ndx;
89 extern int max_delete;
90 extern int force_delete;
91 extern int one_file_system;
92 extern struct stats stats;
93 extern dev_t filesystem_dev;
94 extern mode_t orig_umask;
96 extern char *backup_dir;
97 extern char *backup_suffix;
98 extern int backup_suffix_len;
99 extern struct file_list *cur_flist, *first_flist, *dir_flist;
100 extern struct filter_list_struct server_filter_list;
102 int ignore_perishable = 0;
103 int non_perishable_cnt = 0;
104 int maybe_ATTRS_REPORT = 0;
106 static dev_t dev_zero;
107 static int deletion_count = 0; /* used to implement --max-delete */
108 static int deldelay_size = 0, deldelay_cnt = 0;
109 static char *deldelay_buf = NULL;
110 static int deldelay_fd = -1;
112 static int dir_tweaking;
113 static int need_retouch_dir_times;
114 static int need_retouch_dir_perms;
115 static const char *solo_file = NULL;
117 /* For calling delete_item() and delete_dir_contents(). */
118 #define DEL_OWNED_BY_US (1<<0) /* file/dir has our uid */
119 #define DEL_RECURSE (1<<1) /* if dir, delete all contents */
120 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
121 #define DEL_FOR_FILE (1<<3) /* making room for a replacement file */
122 #define DEL_FOR_DIR (1<<4) /* making room for a replacement dir */
123 #define DEL_FOR_SYMLINK (1<<5) /* making room for a replacement symlink */
124 #define DEL_FOR_DEVICE (1<<6) /* making room for a replacement device */
125 #define DEL_FOR_SPECIAL (1<<7) /* making room for a replacement special */
127 #define DEL_MAKE_ROOM (DEL_FOR_FILE|DEL_FOR_DIR|DEL_FOR_SYMLINK|DEL_FOR_DEVICE|DEL_FOR_SPECIAL)
130 TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
134 DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_NOT_EMPTY
137 /* Forward declaration for delete_item(). */
138 static enum delret delete_dir_contents(char *fname, int flags);
140 static int is_backup_file(char *fn)
142 int k = strlen(fn) - backup_suffix_len;
143 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
146 /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
147 * delete recursively.
149 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
150 * a directory! (The buffer is used for recursion, but returned unchanged.)
152 static enum delret delete_item(char *fbuf, int mode, int flags)
159 rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
163 if (!am_root && !(mode & S_IWUSR) && flags & DEL_OWNED_BY_US)
164 do_chmod(fbuf, mode |= S_IWUSR);
166 if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
167 ignore_perishable = 1;
168 /* If DEL_RECURSE is not set, this just reports emptiness. */
169 ret = delete_dir_contents(fbuf, flags);
170 ignore_perishable = 0;
171 if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT)
173 /* OK: try to delete the directory. */
176 if (!(flags & DEL_MAKE_ROOM) && max_delete >= 0 && ++deletion_count > max_delete)
181 ok = do_rmdir(fbuf) == 0;
182 } else if (make_backups > 0 && (backup_dir || !is_backup_file(fbuf))) {
183 what = "make_backup";
184 ok = make_backup(fbuf);
187 ok = robust_unlink(fbuf) == 0;
191 if (!(flags & DEL_MAKE_ROOM))
192 log_delete(fbuf, mode);
195 if (S_ISDIR(mode) && errno == ENOTEMPTY) {
196 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
199 } else if (errno != ENOENT) {
200 rsyserr(FERROR, errno, "delete_file: %s(%s) failed",
210 if (ret != DR_SUCCESS && flags & DEL_MAKE_ROOM) {
212 switch (flags & DEL_MAKE_ROOM) {
213 case DEL_FOR_FILE: desc = "regular file"; break;
214 case DEL_FOR_DIR: desc = "directory"; break;
215 case DEL_FOR_SYMLINK: desc = "symlink"; break;
216 case DEL_FOR_DEVICE: desc = "device file"; break;
217 case DEL_FOR_SPECIAL: desc = "special file"; break;
218 default: exit_cleanup(RERR_UNSUPPORTED); /* IMPOSSIBLE */
220 rprintf(FERROR_XFER, "could not make way for new %s: %s\n",
226 /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
227 * its contents, otherwise just checks for content. Returns DR_SUCCESS or
228 * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
229 * buffer is used for recursion, but returned unchanged.)
231 static enum delret delete_dir_contents(char *fname, int flags)
233 struct file_list *dirlist;
241 rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n",
245 dlen = strlen(fname);
246 save_filters = push_local_filters(fname, dlen);
248 non_perishable_cnt = 0;
249 dirlist = get_dirlist(fname, dlen, 0);
250 ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
255 if (!(flags & DEL_RECURSE)) {
261 if (dlen != 1 || *fname != '/')
263 remainder = MAXPATHLEN - (p - fname);
265 /* We do our own recursion, so make delete_item() non-recursive. */
266 flags = (flags & ~(DEL_RECURSE|DEL_MAKE_ROOM)) | DEL_DIR_IS_EMPTY;
268 for (j = dirlist->used; j--; ) {
269 struct file_struct *fp = dirlist->files[j];
271 if (fp->flags & FLAG_MOUNT_DIR) {
274 "mount point, %s, pins parent directory\n",
281 strlcpy(p, fp->basename, remainder);
282 if (!uid_ndx || (uid_t)F_OWNER(fp) == our_uid)
283 flags |= DEL_OWNED_BY_US;
285 flags &= ~DEL_OWNED_BY_US;
286 /* Save stack by recursing to ourself directly. */
287 if (S_ISDIR(fp->mode)) {
288 if (!am_root && !(fp->mode & S_IWUSR) && flags & DEL_OWNED_BY_US)
289 do_chmod(fname, fp->mode |= S_IWUSR);
290 if (delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
293 if (delete_item(fname, fp->mode, flags) != DR_SUCCESS)
301 pop_local_filters(save_filters);
303 if (ret == DR_NOT_EMPTY) {
304 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
310 static int start_delete_delay_temp(void)
312 char fnametmp[MAXPATHLEN];
313 int save_dry_run = dry_run;
316 if (!get_tmpname(fnametmp, "deldelay")
317 || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
318 rprintf(FINFO, "NOTE: Unable to create delete-delay temp file%s.\n",
319 inc_recurse ? "" : " -- switching to --delete-after");
321 delete_after = !inc_recurse;
322 dry_run = save_dry_run;
326 dry_run = save_dry_run;
330 static int flush_delete_delay(void)
332 if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
333 rsyserr(FERROR, errno, "flush of delete-delay buffer");
343 static int remember_delete(struct file_struct *file, const char *fname, int flags)
345 const char *plus = (!am_root && !(file->mode & S_IWUSR) && flags & DEL_OWNED_BY_US)
350 len = snprintf(deldelay_buf + deldelay_cnt,
351 deldelay_size - deldelay_cnt,
353 plus, (int)file->mode, fname, '\0');
354 if ((deldelay_cnt += len) <= deldelay_size)
356 if (deldelay_fd < 0 && !start_delete_delay_temp())
359 if (!flush_delete_delay())
366 static int read_delay_line(char *buf, int *own_flag_p)
368 static int read_pos = 0;
370 char *bp, *past_space;
373 for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
374 if (j < deldelay_cnt)
376 if (deldelay_fd < 0) {
381 deldelay_cnt -= read_pos;
382 if (deldelay_cnt == deldelay_size)
384 if (deldelay_cnt && read_pos) {
385 memmove(deldelay_buf, deldelay_buf + read_pos,
388 len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
389 deldelay_size - deldelay_cnt);
393 "ERROR: unexpected EOF in delete-delay file.\n");
398 rsyserr(FERROR, errno,
399 "reading delete-delay file");
406 bp = deldelay_buf + read_pos;
409 *own_flag_p = DEL_OWNED_BY_US;
413 if (sscanf(bp, "%x ", &mode) != 1) {
415 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
418 past_space = strchr(bp, ' ') + 1;
419 len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
422 if (len > MAXPATHLEN) {
423 rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
427 /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
428 * instead of returning a pointer to our buffer. */
429 memcpy(buf, past_space, len);
434 static void do_delayed_deletions(char *delbuf)
438 if (deldelay_fd >= 0) {
439 if (deldelay_cnt && !flush_delete_delay())
441 lseek(deldelay_fd, 0, 0);
443 while ((mode = read_delay_line(delbuf, &own_flag)) >= 0)
444 delete_item(delbuf, mode, own_flag | DEL_RECURSE);
445 if (deldelay_fd >= 0)
449 /* This function is used to implement per-directory deletion, and is used by
450 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
451 * MAXPATHLEN buffer with the name of the directory in it (the functions we
452 * call will append names onto the end, but the old dir value will be restored
454 static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
456 static int already_warned = 0;
457 struct file_list *dirlist;
458 char delbuf[MAXPATHLEN];
462 change_local_filter_dir(NULL, 0, 0);
467 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
470 maybe_send_keepalive();
472 if (io_error && !ignore_errors) {
476 "IO error encountered -- skipping file deletion\n");
482 change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
484 if (one_file_system) {
485 if (file->flags & FLAG_TOP_DIR)
486 filesystem_dev = *fs_dev;
487 else if (filesystem_dev != *fs_dev)
491 dirlist = get_dirlist(fbuf, dlen, 0);
493 /* If an item in dirlist is not found in flist, delete it
494 * from the filesystem. */
495 for (i = dirlist->used; i--; ) {
496 struct file_struct *fp = dirlist->files[i];
497 if (!F_IS_ACTIVE(fp))
499 if (fp->flags & FLAG_MOUNT_DIR) {
501 rprintf(FINFO, "cannot delete mount point: %s\n",
505 if (flist_find(cur_flist, fp) < 0) {
506 int flags = DEL_RECURSE
507 | (!uid_ndx || (uid_t)F_OWNER(fp) == our_uid ? DEL_OWNED_BY_US : 0);
509 if (delete_during == 2) {
510 if (!remember_delete(fp, delbuf, flags))
513 delete_item(delbuf, fp->mode, flags);
520 /* This deletes any files on the receiving side that are not present on the
521 * sending side. This is used by --delete-before and --delete-after. */
522 static void do_delete_pass(void)
524 char fbuf[MAXPATHLEN];
528 /* dry_run is incremented when the destination doesn't exist yet. */
529 if (dry_run > 1 || list_only)
532 for (j = 0; j < cur_flist->used; j++) {
533 struct file_struct *file = cur_flist->sorted[j];
535 if (!(file->flags & FLAG_CONTENT_DIR))
539 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
540 rprintf(FINFO, "deleting in %s\n", fbuf);
542 if (link_stat(fbuf, &st, keep_dirlinks) < 0
543 || !S_ISDIR(st.st_mode))
546 delete_in_dir(fbuf, file, &st.st_dev);
548 delete_in_dir(NULL, NULL, &dev_zero);
550 if (do_progress && !am_server)
551 rprintf(FINFO, " \r");
554 int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
557 if (S_ISLNK(file->mode)) {
561 if (preserve_times && cmp_time(sxp->st.st_mtime, file->modtime) != 0)
564 if (preserve_perms && !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
567 if (preserve_executability && ((sxp->st.st_mode & 0111) != 0) ^ ((file->mode & 0111) != 0))
570 if (am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file))
573 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
577 if (preserve_acls && !S_ISLNK(file->mode)) {
578 if (!ACL_READY(*sxp))
580 if (set_acl(NULL, file, sxp) == 0)
584 #ifdef SUPPORT_XATTRS
585 if (preserve_xattrs) {
586 if (!XATTR_READY(*sxp))
587 get_xattr(fname, sxp);
588 if (xattr_diff(file, sxp, 0))
596 void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
597 stat_x *sxp, int32 iflags, uchar fnamecmp_type,
600 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
601 int keep_time = !preserve_times ? 0
602 : S_ISDIR(file->mode) ? preserve_times > 1
603 : !S_ISLNK(file->mode);
605 if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
606 iflags |= ITEM_REPORT_SIZE;
607 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
608 && !(iflags & ITEM_MATCHED)
609 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
610 || (keep_time && cmp_time(file->modtime, sxp->st.st_mtime) != 0))
611 iflags |= ITEM_REPORT_TIME;
612 #if !defined HAVE_LCHMOD && !defined HAVE_SETATTRLIST
613 if (S_ISLNK(file->mode)) {
617 if ((preserve_perms || preserve_executability)
618 && !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
619 iflags |= ITEM_REPORT_PERMS;
620 if (uid_ndx && am_root && (uid_t)F_OWNER(file) != sxp->st.st_uid)
621 iflags |= ITEM_REPORT_OWNER;
622 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
623 && sxp->st.st_gid != (gid_t)F_GROUP(file))
624 iflags |= ITEM_REPORT_GROUP;
626 if (preserve_acls && !S_ISLNK(file->mode)) {
627 if (!ACL_READY(*sxp))
628 get_acl(fnamecmp, sxp);
629 if (set_acl(NULL, file, sxp) == 0)
630 iflags |= ITEM_REPORT_ACL;
633 #ifdef SUPPORT_XATTRS
634 if (preserve_xattrs) {
635 if (!XATTR_READY(*sxp))
636 get_xattr(fnamecmp, sxp);
637 if (xattr_diff(file, sxp, 1))
638 iflags |= ITEM_REPORT_XATTR;
642 #ifdef SUPPORT_XATTRS
643 if (preserve_xattrs && xattr_diff(file, NULL, 1))
644 iflags |= ITEM_REPORT_XATTR;
646 iflags |= ITEM_IS_NEW;
650 if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || verbose > 1
651 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
652 if (protocol_version >= 29) {
654 write_ndx(sock_f_out, ndx);
655 write_shortint(sock_f_out, iflags);
656 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
657 write_byte(sock_f_out, fnamecmp_type);
658 if (iflags & ITEM_XNAME_FOLLOWS)
659 write_vstring(sock_f_out, xname, strlen(xname));
660 #ifdef SUPPORT_XATTRS
661 if (iflags & ITEM_REPORT_XATTR && !dry_run)
662 send_xattr_request(NULL, file, sock_f_out);
664 } else if (ndx >= 0) {
665 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
666 log_item(code, file, &stats, iflags, xname);
672 /* Perform our quick-check heuristic for determining if a file is unchanged. */
673 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
675 if (st->st_size != F_LENGTH(file))
678 /* if always checksum is set then we use the checksum instead
679 of the file time to determine whether to sync */
680 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
681 char sum[MAX_DIGEST_LEN];
682 file_checksum(fn, sum, st->st_size);
683 return memcmp(sum, F_SUM(file), checksum_len) == 0;
692 return cmp_time(st->st_mtime, file->modtime) == 0;
697 * set (initialize) the size entries in the per-file sum_struct
698 * calculating dynamic block and checksum sizes.
700 * This is only called from generate_and_send_sums() but is a separate
701 * function to encapsulate the logic.
703 * The block size is a rounded square root of file length.
705 * The checksum size is determined according to:
706 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
707 * provided by Donovan Baarda which gives a probability of rsync
708 * algorithm corrupting data and falling back using the whole md4
711 * This might be made one of several selectable heuristics.
713 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
719 blength = block_size;
720 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
721 blength = BLOCK_SIZE;
726 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
727 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
728 blength = MAX_BLOCK_SIZE;
733 if (len < (int64)blength * blength)
736 } while (c >= 8); /* round to multiple of 8 */
737 blength = MAX(blength, BLOCK_SIZE);
741 if (protocol_version < 27) {
742 s2length = csum_length;
743 } else if (csum_length == SUM_LENGTH) {
744 s2length = SUM_LENGTH;
748 int b = BLOCKSUM_BIAS;
749 for (l = len; l >>= 1; b += 2) {}
750 for (c = blength; (c >>= 1) && b; b--) {}
751 /* add a bit, subtract rollsum, round up. */
752 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
753 s2length = MAX(s2length, csum_length);
754 s2length = MIN(s2length, SUM_LENGTH);
758 sum->blength = blength;
759 sum->s2length = s2length;
760 sum->remainder = (int32)(len % blength);
761 sum->count = (int32)(len / blength) + (sum->remainder != 0);
763 if (sum->count && verbose > 2) {
765 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
766 (double)sum->count, (long)sum->remainder, (long)sum->blength,
767 sum->s2length, (double)sum->flength);
773 * Generate and send a stream of signatures/checksums that describe a buffer
775 * Generate approximately one checksum every block_len bytes.
777 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
780 struct map_struct *mapbuf;
781 struct sum_struct sum;
784 sum_sizes_sqroot(&sum, len);
785 write_sum_head(f_out, &sum);
787 if (append_mode > 0 && f_copy < 0)
791 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
795 for (i = 0; i < sum.count; i++) {
796 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
797 char *map = map_ptr(mapbuf, offset, n1);
798 char sum2[SUM_LENGTH];
805 full_write(f_copy, map, n1);
810 sum1 = get_checksum1(map, n1);
811 get_checksum2(map, n1, sum2);
815 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
816 (double)i, (double)offset - n1, (long)n1,
817 (unsigned long)sum1);
819 write_int(f_out, sum1);
820 write_buf(f_out, sum2, sum.s2length);
828 /* Try to find a filename in the same dir as "fname" with a similar name. */
829 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
831 int fname_len, fname_suf_len;
832 const char *fname_suf, *fname = file->basename;
833 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
834 int j, lowest_j = -1;
836 fname_len = strlen(fname);
837 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
839 for (j = 0; j < dirlist->used; j++) {
840 struct file_struct *fp = dirlist->files[j];
841 const char *suf, *name;
845 if (!S_ISREG(fp->mode) || !F_LENGTH(fp)
846 || fp->flags & FLAG_FILE_SENT)
851 if (F_LENGTH(fp) == F_LENGTH(file)
852 && cmp_time(fp->modtime, file->modtime) == 0) {
855 "fuzzy size/modtime match for %s\n",
862 suf = find_filename_suffix(name, len, &suf_len);
864 dist = fuzzy_distance(name, len, fname, fname_len);
865 /* Add some extra weight to how well the suffixes match. */
866 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
869 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
870 name, (int)(dist>>16), (int)(dist&0xFFFF));
872 if (dist <= lowest_dist) {
881 /* Copy a file found in our --copy-dest handling. */
882 static int copy_altdest_file(const char *src, const char *dest, struct file_struct *file)
884 char buf[MAXPATHLEN];
885 const char *copy_to, *partialptr;
889 /* Let copy_file open the destination in place. */
893 fd_w = open_tmpfile(buf, dest, file);
898 cleanup_set(copy_to, NULL, NULL, -1, -1);
899 if (copy_file(src, copy_to, fd_w, file->mode, 0) < 0) {
901 rsyserr(FINFO, errno, "copy_file %s => %s",
902 full_fname(src), copy_to);
904 /* Try to clean up. */
909 partialptr = partial_dir ? partial_dir_fname(dest) : NULL;
910 ok = finish_transfer(dest, copy_to, src, partialptr, file, 1, 0);
915 /* This is only called for regular files. We return -2 if we've finished
916 * handling the file, -1 if no dest-linking occurred, or a non-negative
917 * value if we found an alternate basis file. */
918 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
919 char *cmpbuf, stat_x *sxp, int itemizing,
927 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
928 if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
930 switch (match_level) {
936 if (!unchanged_file(cmpbuf, file, &sxp->st))
942 if (!unchanged_attrs(cmpbuf, file, sxp))
949 } while (basis_dir[++j] != NULL);
954 if (j != best_match) {
956 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
957 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
961 if (match_level == 3 && !copy_dest) {
962 #ifdef SUPPORT_HARD_LINKS
964 if (!hard_link_one(file, fname, cmpbuf, 1))
966 if (preserve_hard_links && F_IS_HLINKED(file))
967 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, j);
968 if (!maybe_ATTRS_REPORT && (verbose > 1 || stdout_format_has_i > 1)) {
969 itemize(cmpbuf, file, ndx, 1, sxp,
970 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
976 itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
977 if (verbose > 1 && maybe_ATTRS_REPORT)
978 rprintf(FCLIENT, "%s is uptodate\n", fname);
982 if (match_level >= 2) {
983 #ifdef SUPPORT_HARD_LINKS
984 try_a_copy: /* Copy the file locally. */
986 if (!dry_run && copy_altdest_file(cmpbuf, fname, file) < 0)
989 itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
990 if (maybe_ATTRS_REPORT
991 && ((!itemizing && verbose && match_level == 2)
992 || (verbose > 1 && match_level == 3))) {
993 code = match_level == 3 ? FCLIENT : FINFO;
994 rprintf(code, "%s%s\n", fname,
995 match_level == 3 ? " is uptodate" : "");
997 #ifdef SUPPORT_HARD_LINKS
998 if (preserve_hard_links && F_IS_HLINKED(file))
999 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, -1);
1004 return FNAMECMP_BASIS_DIR_LOW + j;
1007 /* This is only called for non-regular files. We return -2 if we've finished
1008 * handling the file, or -1 if no dest-linking occurred, or a non-negative
1009 * value if we found an alternate basis file. */
1010 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
1011 char *cmpbuf, stat_x *sxp, int itemizing,
1014 char lnk[MAXPATHLEN];
1015 int best_match = -1;
1016 int match_level = 0;
1017 enum nonregtype type;
1021 #ifndef SUPPORT_LINKS
1022 if (S_ISLNK(file->mode))
1025 if (S_ISDIR(file->mode)) {
1027 } else if (IS_SPECIAL(file->mode))
1028 type = TYPE_SPECIAL;
1029 else if (IS_DEVICE(file->mode))
1031 #ifdef SUPPORT_LINKS
1032 else if (S_ISLNK(file->mode))
1033 type = TYPE_SYMLINK;
1037 "internal: try_dests_non() called with invalid mode (%o)\n",
1039 exit_cleanup(RERR_UNSUPPORTED);
1043 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1044 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1048 if (!S_ISDIR(sxp->st.st_mode))
1052 if (!IS_SPECIAL(sxp->st.st_mode))
1056 if (!IS_DEVICE(sxp->st.st_mode))
1059 #ifdef SUPPORT_LINKS
1061 if (!S_ISLNK(sxp->st.st_mode))
1066 if (match_level < 1) {
1075 devp = F_RDEV_P(file);
1076 if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
1079 #ifdef SUPPORT_LINKS
1081 if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
1084 if (strcmp(lnk, F_SYMLINK(file)) != 0)
1089 if (match_level < 2) {
1093 if (unchanged_attrs(cmpbuf, file, sxp)) {
1098 } while (basis_dir[++j] != NULL);
1103 if (j != best_match) {
1105 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1106 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1110 if (match_level == 3) {
1111 #ifdef SUPPORT_HARD_LINKS
1113 #ifndef CAN_HARDLINK_SYMLINK
1114 && !S_ISLNK(file->mode)
1116 #ifndef CAN_HARDLINK_SPECIAL
1117 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
1119 && !S_ISDIR(file->mode)) {
1120 if (do_link(cmpbuf, fname) < 0) {
1121 rsyserr(FERROR_XFER, errno,
1122 "failed to hard-link %s with %s",
1126 if (preserve_hard_links && F_IS_HLINKED(file))
1127 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1131 if (itemizing && stdout_format_has_i
1132 && (verbose > 1 || stdout_format_has_i > 1)) {
1133 int chg = compare_dest && type != TYPE_DIR ? 0
1135 + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1136 char *lp = match_level == 3 ? "" : NULL;
1137 itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
1139 if (verbose > 1 && maybe_ATTRS_REPORT) {
1140 rprintf(FCLIENT, "%s%s is uptodate\n",
1141 fname, type == TYPE_DIR ? "/" : "");
1149 static void list_file_entry(struct file_struct *f)
1151 char permbuf[PERMSTRING_SIZE];
1154 if (!F_IS_ACTIVE(f)) {
1155 /* this can happen if duplicate names were removed */
1159 permstring(permbuf, f->mode);
1162 /* TODO: indicate '+' if the entry has an ACL. */
1164 #ifdef SUPPORT_LINKS
1165 if (preserve_links && S_ISLNK(f->mode)) {
1166 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
1167 permbuf, len, timestring(f->modtime),
1168 f_name(f, NULL), F_SYMLINK(f));
1172 rprintf(FINFO, "%s %11.0f %s %s\n",
1173 permbuf, len, timestring(f->modtime),
1178 static int phase = 0;
1179 static int dflt_perms;
1181 /* Acts on the indicated item in cur_flist whose name is fname. If a dir,
1182 * make sure it exists, and has the right permissions/timestamp info. For
1183 * all other non-regular files (symlinks, etc.) we create them here. For
1184 * regular files that have changed, we try to find a basis file and then
1185 * start sending checksums. The ndx is the file's unique index value.
1187 * When fname is non-null, it must point to a MAXPATHLEN buffer!
1189 * Note that f_out is set to -1 when doing final directory-permission and
1190 * modification-time repair. */
1191 static void recv_generator(char *fname, struct file_struct *file, int ndx,
1192 int itemizing, enum logcode code, int f_out)
1194 static int missing_below = -1, excluded_below = -1;
1195 static const char *parent_dirname = "";
1196 static struct file_struct *missing_dir = NULL, *excluded_dir = NULL;
1197 static struct file_list *fuzzy_dirlist = NULL;
1198 static int need_fuzzy_dirlist = 0;
1199 struct file_struct *fuzzy_file = NULL;
1200 int fd = -1, f_copy = -1;
1202 STRUCT_STAT partial_st;
1203 struct file_struct *back_file = NULL;
1204 int statret, real_ret, stat_errno;
1205 char *fnamecmp, *partialptr, *backupptr = NULL;
1206 char fnamecmpbuf[MAXPATHLEN];
1207 uchar fnamecmp_type;
1208 int implied_dirs_are_missing = relative_paths && !implied_dirs && protocol_version < 30;
1209 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1210 int is_dir = !S_ISDIR(file->mode) ? 0
1211 : inc_recurse && ndx != cur_flist->ndx_start - 1 ? -1
1215 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
1219 || (is_dir && !implied_dirs && file->flags & FLAG_IMPLIED_DIR))
1221 list_file_entry(file);
1225 if (server_filter_list.head) {
1226 int filtered = check_filter(&server_filter_list, fname, is_dir) < 0;
1227 if (is_dir < 0 && filtered)
1229 if (excluded_below >= 0) {
1230 if (F_DEPTH(file) > excluded_below
1231 && (!implied_dirs_are_missing || f_name_has_prefix(file, excluded_dir)))
1233 excluded_below = -1;
1237 excluded_below = F_DEPTH(file);
1238 excluded_dir = file;
1241 rprintf(FERROR_XFER,
1242 "skipping daemon-excluded file \"%s\"\n",
1248 if (missing_below >= 0) {
1249 if (F_DEPTH(file) <= missing_below
1250 || (implied_dirs_are_missing && !f_name_has_prefix(file, missing_dir))) {
1254 } else if (!dry_run) {
1256 file->flags |= FLAG_MISSING_DIR;
1261 sx.acc_acl = sx.def_acl = NULL;
1263 #ifdef SUPPORT_XATTRS
1267 if (fuzzy_dirlist) {
1268 flist_free(fuzzy_dirlist);
1269 fuzzy_dirlist = NULL;
1271 parent_dirname = "";
1273 stat_errno = ENOENT;
1275 const char *dn = file->dirname ? file->dirname : ".";
1276 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1277 if (relative_paths && !implied_dirs
1278 && do_stat(dn, &sx.st) < 0
1279 && create_directory_path(fname) < 0) {
1280 rsyserr(FERROR_XFER, errno,
1281 "recv_generator: mkdir %s failed",
1284 if (fuzzy_dirlist) {
1285 flist_free(fuzzy_dirlist);
1286 fuzzy_dirlist = NULL;
1289 need_fuzzy_dirlist = 1;
1291 if (!preserve_perms)
1292 dflt_perms = default_perms_for_dir(dn);
1295 parent_dirname = dn;
1297 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
1298 strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1299 fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
1300 need_fuzzy_dirlist = 0;
1303 statret = link_stat(fname, &sx.st, keep_dirlinks && is_dir);
1307 if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
1311 if (missing_below < 0) {
1314 missing_below = F_DEPTH(file);
1317 file->flags |= FLAG_MISSING_DIR;
1320 rprintf(FINFO, "not creating new %s \"%s\"\n",
1321 is_dir ? "directory" : "file", fname);
1326 if (statret == 0 && sx.st.st_uid == our_uid)
1327 del_opts |= DEL_OWNED_BY_US;
1330 if (!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
1333 /* In inc_recurse mode we want to make sure any missing
1334 * directories get created while we're still processing
1335 * the parent dir (which allows us to touch the parent
1336 * dir's mtime right away). We will handle the dir in
1337 * full later (right before we handle its contents). */
1339 && (S_ISDIR(sx.st.st_mode)
1340 || delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0))
1341 goto cleanup; /* Any errors get reported later. */
1342 if (do_mkdir(fname, file->mode & 0700) == 0)
1343 file->flags |= FLAG_DIR_CREATED;
1346 /* The file to be received is a directory, so we need
1347 * to prepare appropriately. If there is already a
1348 * file of that name and it is *not* a directory, then
1349 * we need to delete it. If it doesn't exist, then
1350 * (perhaps recursively) create it. */
1351 if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1352 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0)
1353 goto skipping_dir_contents;
1356 if (dry_run && statret != 0 && missing_below < 0) {
1357 missing_below = F_DEPTH(file);
1363 if (file->flags & FLAG_DIR_CREATED)
1365 if (!preserve_perms) { /* See comment in non-dir code below. */
1366 file->mode = dest_mode(file->mode, sx.st.st_mode,
1367 dflt_perms, statret == 0);
1369 if (statret != 0 && basis_dir[0] != NULL) {
1370 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1378 if (itemizing && f_out != -1) {
1379 itemize(fname, file, ndx, statret, &sx,
1380 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1382 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1383 if (!relative_paths || errno != ENOENT
1384 || create_directory_path(fname) < 0
1385 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
1386 rsyserr(FERROR_XFER, errno,
1387 "recv_generator: mkdir %s failed",
1389 skipping_dir_contents:
1391 "*** Skipping any contents from this failed directory ***\n");
1392 missing_below = F_DEPTH(file);
1394 file->flags |= FLAG_MISSING_DIR;
1398 if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
1399 && verbose && code != FNONE && f_out != -1)
1400 rprintf(code, "%s/\n", fname);
1402 /* We need to ensure that the dirs in the transfer have writable
1403 * permissions during the time we are putting files within them.
1404 * This is then fixed after the transfer is done. */
1406 if (!am_root && !(file->mode & S_IWUSR) && dir_tweaking) {
1407 mode_t mode = file->mode | S_IWUSR;
1408 if (do_chmod(fname, mode) < 0) {
1409 rsyserr(FERROR_XFER, errno,
1410 "failed to modify permissions on %s",
1413 need_retouch_dir_perms = 1;
1417 if (real_ret != 0 && one_file_system)
1418 real_sx.st.st_dev = filesystem_dev;
1420 if (one_file_system) {
1421 uint32 *devp = F_DIR_DEV_P(file);
1422 DEV_MAJOR(devp) = major(real_sx.st.st_dev);
1423 DEV_MINOR(devp) = minor(real_sx.st.st_dev);
1426 else if (delete_during && f_out != -1 && !phase && dry_run < 2
1427 && (file->flags & FLAG_CONTENT_DIR))
1428 delete_in_dir(fname, file, &real_sx.st.st_dev);
1432 /* If we're not preserving permissions, change the file-list's
1433 * mode based on the local permissions and some heuristics. */
1434 if (!preserve_perms) {
1435 int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
1436 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
1440 #ifdef SUPPORT_HARD_LINKS
1441 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1442 && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
1446 if (preserve_links && S_ISLNK(file->mode)) {
1447 #ifdef SUPPORT_LINKS
1448 const char *sl = F_SYMLINK(file);
1449 if (safe_symlinks && unsafe_symlink(sl, fname)) {
1452 fname = f_name(file, NULL);
1454 "ignoring unsafe symlink %s -> \"%s\"\n",
1455 full_fname(fname), sl);
1460 char lnk[MAXPATHLEN];
1463 if (!S_ISLNK(sx.st.st_mode))
1465 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1466 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1467 /* The link is pointing to the right place. */
1468 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1470 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1471 #if defined SUPPORT_HARD_LINKS && defined CAN_HARDLINK_SYMLINK
1472 if (preserve_hard_links && F_IS_HLINKED(file))
1473 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1475 if (remove_source_files == 1)
1476 goto return_with_success;
1479 /* Not the right symlink (or not a symlink), so
1481 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_SYMLINK) != 0)
1483 } else if (basis_dir[0] != NULL) {
1484 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1487 #ifndef CAN_HARDLINK_SYMLINK
1489 /* Resort to --copy-dest behavior. */
1499 #ifdef SUPPORT_HARD_LINKS
1500 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1501 cur_flist->in_progress++;
1505 if (do_symlink(sl, fname) != 0) {
1506 rsyserr(FERROR_XFER, errno, "symlink %s -> \"%s\" failed",
1507 full_fname(fname), sl);
1509 set_file_attrs(fname, file, NULL, NULL, 0);
1511 itemize(fname, file, ndx, statret, &sx,
1512 ITEM_LOCAL_CHANGE, 0, NULL);
1514 if (code != FNONE && verbose)
1515 rprintf(code, "%s -> %s\n", fname, sl);
1516 #ifdef SUPPORT_HARD_LINKS
1517 if (preserve_hard_links && F_IS_HLINKED(file))
1518 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1520 /* This does not check remove_source_files == 1
1521 * because this is one of the items that the old
1522 * --remove-sent-files option would remove. */
1523 if (remove_source_files)
1524 goto return_with_success;
1530 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1531 || (preserve_specials && IS_SPECIAL(file->mode))) {
1532 uint32 *devp = F_RDEV_P(file);
1533 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1536 if (IS_DEVICE(file->mode)) {
1537 if (!IS_DEVICE(sx.st.st_mode))
1539 del_for_flag = DEL_FOR_DEVICE;
1541 if (!IS_SPECIAL(sx.st.st_mode))
1543 del_for_flag = DEL_FOR_SPECIAL;
1546 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
1547 && sx.st.st_rdev == rdev) {
1548 /* The device or special file is identical. */
1549 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1551 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1552 #ifdef SUPPORT_HARD_LINKS
1553 if (preserve_hard_links && F_IS_HLINKED(file))
1554 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1556 if (remove_source_files == 1)
1557 goto return_with_success;
1560 if (delete_item(fname, sx.st.st_mode, del_opts | del_for_flag) != 0)
1562 } else if (basis_dir[0] != NULL) {
1563 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1566 #ifndef CAN_HARDLINK_SPECIAL
1568 /* Resort to --copy-dest behavior. */
1578 #ifdef SUPPORT_HARD_LINKS
1579 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1580 cur_flist->in_progress++;
1585 rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1586 fname, (int)file->mode,
1587 (long)major(rdev), (long)minor(rdev));
1589 if (do_mknod(fname, file->mode, rdev) < 0) {
1590 rsyserr(FERROR_XFER, errno, "mknod %s failed",
1593 set_file_attrs(fname, file, NULL, NULL, 0);
1595 itemize(fname, file, ndx, statret, &sx,
1596 ITEM_LOCAL_CHANGE, 0, NULL);
1598 if (code != FNONE && verbose)
1599 rprintf(code, "%s\n", fname);
1600 #ifdef SUPPORT_HARD_LINKS
1601 if (preserve_hard_links && F_IS_HLINKED(file))
1602 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1604 if (remove_source_files == 1)
1605 goto return_with_success;
1610 if (!S_ISREG(file->mode)) {
1612 fname = f_name(file, NULL);
1613 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1617 if (max_size > 0 && F_LENGTH(file) > max_size) {
1620 fname = f_name(file, NULL);
1621 rprintf(FINFO, "%s is over max-size\n", fname);
1625 if (min_size > 0 && F_LENGTH(file) < min_size) {
1628 fname = f_name(file, NULL);
1629 rprintf(FINFO, "%s is under min-size\n", fname);
1634 if (ignore_existing > 0 && statret == 0) {
1636 rprintf(FINFO, "%s exists\n", fname);
1640 if (update_only > 0 && statret == 0
1641 && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
1643 rprintf(FINFO, "%s is newer\n", fname);
1648 fnamecmp_type = FNAMECMP_FNAME;
1650 if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
1651 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE) != 0)
1654 stat_errno = ENOENT;
1657 if (statret != 0 && basis_dir[0] != NULL) {
1658 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
1661 if (remove_source_files == 1)
1662 goto return_with_success;
1666 fnamecmp = fnamecmpbuf;
1675 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1676 && link_stat(partialptr, &partial_st, 0) == 0
1677 && S_ISREG(partial_st.st_mode)) {
1679 goto prepare_to_open;
1683 if (statret != 0 && fuzzy_dirlist && dry_run <= 1) {
1684 int j = find_fuzzy(file, fuzzy_dirlist);
1686 fuzzy_file = fuzzy_dirlist->files[j];
1687 f_name(fuzzy_file, fnamecmpbuf);
1689 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1690 fname, fnamecmpbuf);
1692 sx.st.st_size = F_LENGTH(fuzzy_file);
1694 fnamecmp = fnamecmpbuf;
1695 fnamecmp_type = FNAMECMP_FUZZY;
1700 #ifdef SUPPORT_HARD_LINKS
1701 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1702 cur_flist->in_progress++;
1706 if (stat_errno == ENOENT)
1708 rsyserr(FERROR_XFER, stat_errno, "recv_generator: failed to stat %s",
1713 if (append_mode > 0 && sx.st.st_size >= F_LENGTH(file))
1716 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1718 else if (fnamecmp_type == FNAMECMP_FUZZY)
1720 else if (unchanged_file(fnamecmp, file, &sx.st)) {
1722 do_unlink(partialptr);
1723 handle_partial_dir(partialptr, PDIR_DELETE);
1725 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1727 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
1728 #ifdef SUPPORT_HARD_LINKS
1729 if (preserve_hard_links && F_IS_HLINKED(file))
1730 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1732 if (remove_source_files != 1)
1734 return_with_success:
1736 send_msg_int(MSG_SUCCESS, ndx);
1743 fnamecmp = partialptr;
1744 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1751 if (read_batch || whole_file) {
1752 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1753 if (!(backupptr = get_backup_name(fname)))
1755 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
1756 goto pretend_missing;
1757 if (copy_file(fname, backupptr, -1, back_file->mode, 1) < 0) {
1758 unmake_file(back_file);
1766 if (fuzzy_dirlist) {
1767 int j = flist_find(fuzzy_dirlist, file);
1768 if (j >= 0) /* don't use changing file as future fuzzy basis */
1769 fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
1773 if ((fd = do_open(fnamecmp, O_RDONLY, 0)) < 0) {
1774 rsyserr(FERROR, errno, "failed to open %s, continuing",
1775 full_fname(fnamecmp));
1777 /* pretend the file didn't exist */
1778 #ifdef SUPPORT_HARD_LINKS
1779 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1780 cur_flist->in_progress++;
1784 statret = real_ret = -1;
1788 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1789 if (!(backupptr = get_backup_name(fname))) {
1793 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1795 goto pretend_missing;
1797 if (robust_unlink(backupptr) && errno != ENOENT) {
1798 rsyserr(FERROR_XFER, errno, "unlink %s",
1799 full_fname(backupptr));
1800 unmake_file(back_file);
1805 if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0
1806 && (errno != ENOENT || make_bak_dir(backupptr) < 0
1807 || (f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0)) {
1808 rsyserr(FERROR_XFER, errno, "open %s",
1809 full_fname(backupptr));
1810 unmake_file(back_file);
1815 fnamecmp_type = FNAMECMP_BACKUP;
1819 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1820 fnamecmp, (double)sx.st.st_size);
1824 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1827 if (remove_source_files && !delay_updates && !phase)
1828 increment_active_files(ndx, itemizing, code);
1829 if (inc_recurse && !dry_run)
1830 cur_flist->in_progress++;
1831 #ifdef SUPPORT_HARD_LINKS
1832 if (preserve_hard_links && F_IS_HLINKED(file))
1833 file->flags |= FLAG_FILE_SENT;
1835 write_ndx(f_out, ndx);
1837 int iflags = ITEM_TRANSFER;
1838 if (always_checksum > 0)
1839 iflags |= ITEM_REPORT_CHECKSUM;
1840 if (fnamecmp_type != FNAMECMP_FNAME)
1841 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1842 if (fnamecmp_type == FNAMECMP_FUZZY)
1843 iflags |= ITEM_XNAME_FOLLOWS;
1844 itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
1845 fuzzy_file ? fuzzy_file->basename : NULL);
1850 #ifdef SUPPORT_XATTRS
1851 if (preserve_xattrs)
1852 free_xattr(&real_sx);
1857 #ifdef SUPPORT_HARD_LINKS
1858 if (preserve_hard_links && F_IS_HLINKED(file))
1859 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1866 if (statret != 0 || whole_file)
1867 write_sum_head(f_out, NULL);
1869 generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy);
1877 set_file_attrs(backupptr, back_file, NULL, NULL, 0);
1879 rprintf(FINFO, "backed up %s to %s\n",
1882 unmake_file(back_file);
1889 #ifdef SUPPORT_XATTRS
1890 if (preserve_xattrs)
1896 static void touch_up_dirs(struct file_list *flist, int ndx)
1898 static int counter = 0;
1899 struct file_struct *file;
1905 end = flist->used - 1;
1909 /* Fix any directory permissions that were modified during the
1910 * transfer and/or re-set any tweaked modified-time values. */
1911 for (i = start; i <= end; i++, counter++) {
1912 file = flist->files[i];
1913 if (!S_ISDIR(file->mode)
1914 || (!implied_dirs && file->flags & FLAG_IMPLIED_DIR))
1917 fname = f_name(file, NULL);
1918 rprintf(FINFO, "touch_up_dirs: %s (%d)\n",
1921 if (!F_IS_ACTIVE(file) || file->flags & FLAG_MISSING_DIR
1922 || (!need_retouch_dir_times && file->mode & S_IWUSR))
1924 fname = f_name(file, NULL);
1925 if (!(file->mode & S_IWUSR))
1926 do_chmod(fname, file->mode);
1927 if (need_retouch_dir_times)
1928 set_modtime(fname, file->modtime, file->mode);
1929 if (allowed_lull && !(counter % lull_mod))
1930 maybe_send_keepalive();
1931 else if (!(counter & 0xFF))
1932 maybe_flush_socket(0);
1936 void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
1938 struct file_struct *file;
1939 struct file_list *flist;
1940 char fbuf[MAXPATHLEN];
1944 #ifdef SUPPORT_HARD_LINKS
1945 if (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
1946 flist = flist_for_ndx(ndx);
1947 assert(flist != NULL);
1948 file = flist->files[ndx - flist->ndx_start];
1949 assert(file->flags & FLAG_HLINKED);
1950 finish_hard_link(file, f_name(file, fbuf), ndx, NULL, itemizing, code, -1);
1951 flist->in_progress--;
1956 if (check_redo && (ndx = get_redo_num()) != -1) {
1957 csum_length = SUM_LENGTH;
1958 max_size = -max_size;
1959 min_size = -min_size;
1960 ignore_existing = -ignore_existing;
1961 ignore_non_existing = -ignore_non_existing;
1962 update_only = -update_only;
1963 always_checksum = -always_checksum;
1964 size_only = -size_only;
1965 append_mode = -append_mode;
1966 make_backups = -make_backups; /* avoid dup backup w/inplace */
1970 cur_flist = flist_for_ndx(ndx);
1972 file = cur_flist->files[ndx - cur_flist->ndx_start];
1974 strlcpy(fbuf, solo_file, sizeof fbuf);
1977 recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
1978 cur_flist->to_redo--;
1982 csum_length = SHORT_SUM_LENGTH;
1983 max_size = -max_size;
1984 min_size = -min_size;
1985 ignore_existing = -ignore_existing;
1986 ignore_non_existing = -ignore_non_existing;
1987 update_only = -update_only;
1988 always_checksum = -always_checksum;
1989 size_only = -size_only;
1990 append_mode = -append_mode;
1991 make_backups = -make_backups;
1996 if (cur_flist == first_flist)
1999 /* We only get here if inc_recurse is enabled. */
2000 if (first_flist->in_progress || first_flist->to_redo)
2004 write_ndx(sock_f_out, NDX_DONE);
2005 maybe_flush_socket(1);
2008 if (delete_during == 2 || !dir_tweaking) {
2009 /* Skip directory touch-up. */
2010 } else if (first_flist->parent_ndx >= 0)
2011 touch_up_dirs(dir_flist, first_flist->parent_ndx);
2013 flist_free(first_flist); /* updates first_flist */
2017 void generate_files(int f_out, const char *local_name)
2020 char fbuf[MAXPATHLEN];
2023 int save_do_progress = do_progress;
2025 if (protocol_version >= 29) {
2027 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
2028 code = logfile_format_has_i ? FNONE : FLOG;
2029 } else if (am_daemon) {
2030 itemizing = logfile_format_has_i && do_xfers;
2031 maybe_ATTRS_REPORT = ATTRS_REPORT;
2032 code = itemizing || !do_xfers ? FCLIENT : FINFO;
2033 } else if (!am_server) {
2034 itemizing = stdout_format_has_i;
2035 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
2036 code = itemizing ? FNONE : FINFO;
2039 maybe_ATTRS_REPORT = ATTRS_REPORT;
2042 solo_file = local_name;
2043 dir_tweaking = !(list_only || solo_file || dry_run);
2044 need_retouch_dir_times = preserve_times > 1;
2045 lull_mod = allowed_lull * 5;
2048 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
2050 if (delete_before && !solo_file && cur_flist->used > 0)
2052 if (delete_during == 2) {
2053 deldelay_size = BIGPATHBUFLEN * 4;
2054 deldelay_buf = new_array(char, deldelay_size);
2056 out_of_memory("delete-delay");
2060 if (append_mode > 0 || whole_file < 0)
2063 rprintf(FINFO, "delta-transmission %s\n",
2065 ? "disabled for local transfer or --whole-file"
2069 /* Since we often fill up the outgoing socket and then just sit around
2070 * waiting for the other 2 processes to do their thing, we don't want
2071 * to exit on a timeout. If the data stops flowing, the receiver will
2072 * notice that and let us know via the redo pipe (or its closing). */
2075 dflt_perms = (ACCESSPERMS & ~orig_umask);
2078 #ifdef SUPPORT_HARD_LINKS
2079 if (preserve_hard_links && inc_recurse) {
2080 while (!flist_eof && file_total < FILECNT_LOOKAHEAD/2)
2081 wait_for_receiver();
2085 if (inc_recurse && cur_flist->parent_ndx >= 0) {
2086 struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
2088 ndx = cur_flist->ndx_start - 1;
2089 recv_generator(fbuf, fp, ndx, itemizing, code, f_out);
2090 if (delete_during && dry_run < 2 && !list_only) {
2091 if (BITS_SETnUNSET(fp->flags, FLAG_CONTENT_DIR, FLAG_MISSING_DIR)) {
2093 if (one_file_system) {
2094 uint32 *devp = F_DIR_DEV_P(fp);
2095 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
2097 dirdev = MAKEDEV(0, 0);
2098 delete_in_dir(f_name(fp, fbuf), fp, &dirdev);
2102 for (i = cur_flist->low; i <= cur_flist->high; i++) {
2103 struct file_struct *file = cur_flist->sorted[i];
2105 if (!F_IS_ACTIVE(file))
2111 ndx = i + cur_flist->ndx_start;
2114 strlcpy(fbuf, solo_file, sizeof fbuf);
2117 recv_generator(fbuf, file, ndx, itemizing, code, f_out);
2119 check_for_finished_files(itemizing, code, 0);
2121 if (allowed_lull && !(i % lull_mod))
2122 maybe_send_keepalive();
2123 else if (!(i & 0xFF))
2124 maybe_flush_socket(0);
2128 write_ndx(f_out, NDX_DONE);
2133 check_for_finished_files(itemizing, code, 1);
2134 if (cur_flist->next || flist_eof)
2136 wait_for_receiver();
2138 } while ((cur_flist = cur_flist->next) != NULL);
2141 delete_in_dir(NULL, NULL, &dev_zero);
2144 rprintf(FINFO, "generate_files phase=%d\n", phase);
2147 check_for_finished_files(itemizing, code, 1);
2150 wait_for_receiver();
2155 rprintf(FINFO, "generate_files phase=%d\n", phase);
2157 write_ndx(f_out, NDX_DONE);
2158 /* Reduce round-trip lag-time for a useless delay-updates phase. */
2159 if (protocol_version >= 29 && !delay_updates)
2160 write_ndx(f_out, NDX_DONE);
2162 /* Read MSG_DONE for the redo phase (and any prior messages). */
2164 check_for_finished_files(itemizing, code, 0);
2165 if (msgdone_cnt > 1)
2167 wait_for_receiver();
2170 if (protocol_version >= 29) {
2173 rprintf(FINFO, "generate_files phase=%d\n", phase);
2175 write_ndx(f_out, NDX_DONE);
2176 /* Read MSG_DONE for delay-updates phase & prior messages. */
2177 while (msgdone_cnt == 2)
2178 wait_for_receiver();
2181 do_progress = save_do_progress;
2182 if (delete_during == 2)
2183 do_delayed_deletions(fbuf);
2184 if (delete_after && !solo_file && file_total > 0)
2187 if ((need_retouch_dir_perms || need_retouch_dir_times)
2188 && dir_tweaking && (!inc_recurse || delete_during == 2))
2189 touch_up_dirs(dir_flist, -1);
2191 if (max_delete >= 0 && deletion_count > max_delete) {
2193 "Deletions stopped due to --max-delete limit (%d skipped)\n",
2194 deletion_count - max_delete);
2195 io_error |= IOERR_DEL_LIMIT;
2199 rprintf(FINFO, "generate_files finished\n");