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 max_delete;
88 extern int force_delete;
89 extern int one_file_system;
90 extern struct stats stats;
91 extern dev_t filesystem_dev;
92 extern mode_t orig_umask;
93 extern char *backup_dir;
94 extern char *backup_suffix;
95 extern int backup_suffix_len;
96 extern struct file_list *cur_flist, *first_flist, *dir_flist;
97 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_RECURSE (1<<1) /* recurse */
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 (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
157 ignore_perishable = 1;
158 /* If DEL_RECURSE is not set, this just reports emptiness. */
159 ret = delete_dir_contents(fbuf, flags);
160 ignore_perishable = 0;
161 if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT)
163 /* OK: try to delete the directory. */
166 if (!replace && max_delete >= 0 && ++deletion_count > max_delete)
171 ok = do_rmdir(fbuf) == 0;
172 } else if (make_backups > 0 && (backup_dir || !is_backup_file(fbuf))) {
173 what = "make_backup";
174 ok = make_backup(fbuf);
177 ok = robust_unlink(fbuf) == 0;
182 log_delete(fbuf, mode);
185 if (S_ISDIR(mode) && errno == ENOTEMPTY) {
186 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
189 } else if (errno != ENOENT) {
190 rsyserr(FERROR, errno, "delete_file: %s(%s) failed",
200 if (replace && ret != DR_SUCCESS) {
201 rprintf(FERROR, "could not make way for new %s: %s\n",
207 /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
208 * its contents, otherwise just checks for content. Returns DR_SUCCESS or
209 * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
210 * buffer is used for recursion, but returned unchanged.)
212 static enum delret delete_dir_contents(char *fname, int flags)
214 struct file_list *dirlist;
222 rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n",
226 dlen = strlen(fname);
227 save_filters = push_local_filters(fname, dlen);
229 non_perishable_cnt = 0;
230 dirlist = get_dirlist(fname, dlen, 0);
231 ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
236 if (!(flags & DEL_RECURSE)) {
242 if (dlen != 1 || *fname != '/')
244 remainder = MAXPATHLEN - (p - fname);
246 /* We do our own recursion, so make delete_item() non-recursive. */
247 flags = (flags & ~DEL_RECURSE) | DEL_DIR_IS_EMPTY;
249 for (j = dirlist->used; j--; ) {
250 struct file_struct *fp = dirlist->files[j];
252 if (fp->flags & FLAG_MOUNT_DIR) {
255 "mount point, %s, pins parent directory\n",
262 strlcpy(p, fp->basename, remainder);
263 /* Save stack by recursing to ourself directly. */
264 if (S_ISDIR(fp->mode)
265 && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
267 if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
275 pop_local_filters(save_filters);
277 if (ret == DR_NOT_EMPTY) {
278 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
284 static int start_delete_delay_temp(void)
286 char fnametmp[MAXPATHLEN];
287 int save_dry_run = dry_run;
290 if (!get_tmpname(fnametmp, "deldelay")
291 || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
292 rprintf(FINFO, "NOTE: Unable to create delete-delay temp file%s.\n",
293 inc_recurse ? "" : " -- switching to --delete-after");
295 delete_after = !inc_recurse;
296 dry_run = save_dry_run;
300 dry_run = save_dry_run;
304 static int flush_delete_delay(void)
306 if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
307 rsyserr(FERROR, errno, "flush of delete-delay buffer");
317 static int remember_delete(struct file_struct *file, const char *fname)
322 len = snprintf(deldelay_buf + deldelay_cnt,
323 deldelay_size - deldelay_cnt,
324 "%x %s%c", (int)file->mode, fname, '\0');
325 if ((deldelay_cnt += len) <= deldelay_size)
327 if (deldelay_fd < 0 && !start_delete_delay_temp())
330 if (!flush_delete_delay())
337 static int read_delay_line(char *buf)
339 static int read_pos = 0;
341 char *bp, *past_space;
344 for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
345 if (j < deldelay_cnt)
347 if (deldelay_fd < 0) {
352 deldelay_cnt -= read_pos;
353 if (deldelay_cnt == deldelay_size)
355 if (deldelay_cnt && read_pos) {
356 memmove(deldelay_buf, deldelay_buf + read_pos,
359 len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
360 deldelay_size - deldelay_cnt);
364 "ERROR: unexpected EOF in delete-delay file.\n");
369 rsyserr(FERROR, errno,
370 "reading delete-delay file");
377 bp = deldelay_buf + read_pos;
379 if (sscanf(bp, "%x ", &mode) != 1) {
381 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
384 past_space = strchr(bp, ' ') + 1;
385 len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
388 if (len > MAXPATHLEN) {
389 rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
393 /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
394 * instead of returning a pointer to our buffer. */
395 memcpy(buf, past_space, len);
400 static void do_delayed_deletions(char *delbuf)
404 if (deldelay_fd >= 0) {
405 if (deldelay_cnt && !flush_delete_delay())
407 lseek(deldelay_fd, 0, 0);
409 while ((mode = read_delay_line(delbuf)) >= 0)
410 delete_item(delbuf, mode, NULL, DEL_RECURSE);
411 if (deldelay_fd >= 0)
415 /* This function is used to implement per-directory deletion, and is used by
416 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
417 * MAXPATHLEN buffer with the name of the directory in it (the functions we
418 * call will append names onto the end, but the old dir value will be restored
420 static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
422 static int already_warned = 0;
423 struct file_list *dirlist;
424 char delbuf[MAXPATHLEN];
428 change_local_filter_dir(NULL, 0, 0);
433 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
436 maybe_send_keepalive();
438 if (io_error && !ignore_errors) {
442 "IO error encountered -- skipping file deletion\n");
448 change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
450 if (one_file_system) {
451 if (file->flags & FLAG_TOP_DIR)
452 filesystem_dev = *fs_dev;
453 else if (filesystem_dev != *fs_dev)
457 dirlist = get_dirlist(fbuf, dlen, 0);
459 /* If an item in dirlist is not found in flist, delete it
460 * from the filesystem. */
461 for (i = dirlist->used; i--; ) {
462 struct file_struct *fp = dirlist->files[i];
463 if (!F_IS_ACTIVE(fp))
465 if (fp->flags & FLAG_MOUNT_DIR) {
467 rprintf(FINFO, "cannot delete mount point: %s\n",
471 if (flist_find(cur_flist, fp) < 0) {
473 if (delete_during == 2) {
474 if (!remember_delete(fp, delbuf))
477 delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
484 /* This deletes any files on the receiving side that are not present on the
485 * sending side. This is used by --delete-before and --delete-after. */
486 static void do_delete_pass(void)
488 char fbuf[MAXPATHLEN];
492 /* dry_run is incremented when the destination doesn't exist yet. */
493 if (dry_run > 1 || list_only)
496 for (j = 0; j < cur_flist->used; j++) {
497 struct file_struct *file = cur_flist->sorted[j];
499 if (!(file->flags & FLAG_CONTENT_DIR))
503 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
504 rprintf(FINFO, "deleting in %s\n", fbuf);
506 if (link_stat(fbuf, &st, keep_dirlinks) < 0
507 || !S_ISDIR(st.st_mode))
510 delete_in_dir(fbuf, file, &st.st_dev);
512 delete_in_dir(NULL, NULL, &dev_zero);
514 if (do_progress && !am_server)
515 rprintf(FINFO, " \r");
518 int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
520 #if !defined HAVE_LCHMOD && !defined HAVE_SETATTRLIST
521 if (S_ISLNK(file->mode)) {
525 if (preserve_times && cmp_time(sxp->st.st_mtime, file->modtime) != 0)
528 if (preserve_perms && !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
531 if (am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file))
534 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
538 if (preserve_acls && !S_ISLNK(file->mode)) {
539 if (!ACL_READY(*sxp))
541 if (set_acl(NULL, file, sxp) == 0)
545 #ifdef SUPPORT_XATTRS
546 if (preserve_xattrs) {
547 if (!XATTR_READY(*sxp))
548 get_xattr(fname, sxp);
549 if (xattr_diff(file, sxp, 0))
557 void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
558 stat_x *sxp, int32 iflags, uchar fnamecmp_type,
561 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
562 int keep_time = !preserve_times ? 0
563 : S_ISDIR(file->mode) ? preserve_times > 1
564 : !S_ISLNK(file->mode);
566 if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
567 iflags |= ITEM_REPORT_SIZE;
568 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
569 && !(iflags & ITEM_MATCHED)
570 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
571 || (keep_time && cmp_time(file->modtime, sxp->st.st_mtime) != 0))
572 iflags |= ITEM_REPORT_TIME;
573 #if !defined HAVE_LCHMOD && !defined HAVE_SETATTRLIST
574 if (S_ISLNK(file->mode)) {
578 if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
579 iflags |= ITEM_REPORT_PERMS;
580 if (uid_ndx && am_root && (uid_t)F_OWNER(file) != sxp->st.st_uid)
581 iflags |= ITEM_REPORT_OWNER;
582 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
583 && sxp->st.st_gid != (gid_t)F_GROUP(file))
584 iflags |= ITEM_REPORT_GROUP;
586 if (preserve_acls && !S_ISLNK(file->mode)) {
587 if (!ACL_READY(*sxp))
588 get_acl(fnamecmp, sxp);
589 if (set_acl(NULL, file, sxp) == 0)
590 iflags |= ITEM_REPORT_ACL;
593 #ifdef SUPPORT_XATTRS
594 if (preserve_xattrs) {
595 if (!XATTR_READY(*sxp))
596 get_xattr(fnamecmp, sxp);
597 if (xattr_diff(file, sxp, 1))
598 iflags |= ITEM_REPORT_XATTR;
602 #ifdef SUPPORT_XATTRS
603 if (preserve_xattrs && xattr_diff(file, NULL, 1))
604 iflags |= ITEM_REPORT_XATTR;
606 iflags |= ITEM_IS_NEW;
610 if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || verbose > 1
611 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
612 if (protocol_version >= 29) {
614 write_ndx(sock_f_out, ndx);
615 write_shortint(sock_f_out, iflags);
616 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
617 write_byte(sock_f_out, fnamecmp_type);
618 if (iflags & ITEM_XNAME_FOLLOWS)
619 write_vstring(sock_f_out, xname, strlen(xname));
620 #ifdef SUPPORT_XATTRS
621 if (iflags & ITEM_REPORT_XATTR && !dry_run)
622 send_xattr_request(NULL, file, sock_f_out);
624 } else if (ndx >= 0) {
625 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
626 log_item(code, file, &stats, iflags, xname);
632 /* Perform our quick-check heuristic for determining if a file is unchanged. */
633 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
635 if (st->st_size != F_LENGTH(file))
638 /* if always checksum is set then we use the checksum instead
639 of the file time to determine whether to sync */
640 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
641 char sum[MAX_DIGEST_LEN];
642 file_checksum(fn, sum, st->st_size);
643 return memcmp(sum, F_SUM(file), checksum_len) == 0;
652 return cmp_time(st->st_mtime, file->modtime) == 0;
657 * set (initialize) the size entries in the per-file sum_struct
658 * calculating dynamic block and checksum sizes.
660 * This is only called from generate_and_send_sums() but is a separate
661 * function to encapsulate the logic.
663 * The block size is a rounded square root of file length.
665 * The checksum size is determined according to:
666 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
667 * provided by Donovan Baarda which gives a probability of rsync
668 * algorithm corrupting data and falling back using the whole md4
671 * This might be made one of several selectable heuristics.
673 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
679 blength = block_size;
680 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
681 blength = BLOCK_SIZE;
686 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
687 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
688 blength = MAX_BLOCK_SIZE;
693 if (len < (int64)blength * blength)
696 } while (c >= 8); /* round to multiple of 8 */
697 blength = MAX(blength, BLOCK_SIZE);
701 if (protocol_version < 27) {
702 s2length = csum_length;
703 } else if (csum_length == SUM_LENGTH) {
704 s2length = SUM_LENGTH;
708 int b = BLOCKSUM_BIAS;
709 for (l = len; l >>= 1; b += 2) {}
710 for (c = blength; (c >>= 1) && b; b--) {}
711 /* add a bit, subtract rollsum, round up. */
712 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
713 s2length = MAX(s2length, csum_length);
714 s2length = MIN(s2length, SUM_LENGTH);
718 sum->blength = blength;
719 sum->s2length = s2length;
720 sum->remainder = (int32)(len % blength);
721 sum->count = (int32)(len / blength) + (sum->remainder != 0);
723 if (sum->count && verbose > 2) {
725 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
726 (double)sum->count, (long)sum->remainder, (long)sum->blength,
727 sum->s2length, (double)sum->flength);
733 * Generate and send a stream of signatures/checksums that describe a buffer
735 * Generate approximately one checksum every block_len bytes.
737 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
740 struct map_struct *mapbuf;
741 struct sum_struct sum;
744 sum_sizes_sqroot(&sum, len);
745 write_sum_head(f_out, &sum);
747 if (append_mode > 0 && f_copy < 0)
751 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
755 for (i = 0; i < sum.count; i++) {
756 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
757 char *map = map_ptr(mapbuf, offset, n1);
758 char sum2[SUM_LENGTH];
765 full_write(f_copy, map, n1);
770 sum1 = get_checksum1(map, n1);
771 get_checksum2(map, n1, sum2);
775 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
776 (double)i, (double)offset - n1, (long)n1,
777 (unsigned long)sum1);
779 write_int(f_out, sum1);
780 write_buf(f_out, sum2, sum.s2length);
788 /* Try to find a filename in the same dir as "fname" with a similar name. */
789 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
791 int fname_len, fname_suf_len;
792 const char *fname_suf, *fname = file->basename;
793 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
794 int j, lowest_j = -1;
796 fname_len = strlen(fname);
797 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
799 for (j = 0; j < dirlist->used; j++) {
800 struct file_struct *fp = dirlist->files[j];
801 const char *suf, *name;
805 if (!S_ISREG(fp->mode) || !F_LENGTH(fp)
806 || fp->flags & FLAG_FILE_SENT)
811 if (F_LENGTH(fp) == F_LENGTH(file)
812 && cmp_time(fp->modtime, file->modtime) == 0) {
815 "fuzzy size/modtime match for %s\n",
822 suf = find_filename_suffix(name, len, &suf_len);
824 dist = fuzzy_distance(name, len, fname, fname_len);
825 /* Add some extra weight to how well the suffixes match. */
826 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
829 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
830 name, (int)(dist>>16), (int)(dist&0xFFFF));
832 if (dist <= lowest_dist) {
841 /* Copy a file found in our --copy-dest handling. */
842 static int copy_altdest_file(const char *src, const char *dest, struct file_struct *file)
844 char buf[MAXPATHLEN];
845 const char *copy_to, *partialptr;
849 /* Let copy_file open the destination in place. */
853 fd_w = open_tmpfile(buf, dest, file);
858 cleanup_set(copy_to, NULL, NULL, -1, -1);
859 if (copy_file(src, copy_to, fd_w, file->mode, 0) < 0) {
861 rsyserr(FINFO, errno, "copy_file %s => %s",
862 full_fname(src), copy_to);
864 /* Try to clean up. */
869 partialptr = partial_dir ? partial_dir_fname(dest) : NULL;
870 if (partialptr && *partialptr == '/')
872 finish_transfer(dest, copy_to, src, partialptr, file, 1, 0);
877 /* This is only called for regular files. We return -2 if we've finished
878 * handling the file, -1 if no dest-linking occurred, or a non-negative
879 * value if we found an alternate basis file. */
880 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
881 char *cmpbuf, stat_x *sxp, int itemizing,
889 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
890 if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
892 switch (match_level) {
898 if (!unchanged_file(cmpbuf, file, &sxp->st))
904 if (!unchanged_attrs(cmpbuf, file, sxp))
906 if (always_checksum > 0 && preserve_times
907 && cmp_time(sxp->st.st_mtime, file->modtime))
914 } while (basis_dir[++j] != NULL);
919 if (j != best_match) {
921 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
922 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
926 if (match_level == 3 && !copy_dest) {
927 #ifdef SUPPORT_HARD_LINKS
929 if (!hard_link_one(file, fname, cmpbuf, 1))
931 if (preserve_hard_links && F_IS_HLINKED(file))
932 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, j);
933 if (itemizing && (verbose > 1 || stdout_format_has_i > 1)) {
934 itemize(cmpbuf, file, ndx, 1, sxp,
935 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
941 itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
942 if (verbose > 1 && maybe_ATTRS_REPORT)
943 rprintf(FCLIENT, "%s is uptodate\n", fname);
947 if (match_level >= 2) {
948 #ifdef SUPPORT_HARD_LINKS
949 try_a_copy: /* Copy the file locally. */
951 if (!dry_run && copy_altdest_file(cmpbuf, fname, file) < 0)
954 itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
955 #ifdef SUPPORT_XATTRS
957 xattr_clear_locals(file);
959 if (maybe_ATTRS_REPORT
960 && ((!itemizing && verbose && match_level == 2)
961 || (verbose > 1 && match_level == 3))) {
962 code = match_level == 3 ? FCLIENT : FINFO;
963 rprintf(code, "%s%s\n", fname,
964 match_level == 3 ? " is uptodate" : "");
966 #ifdef SUPPORT_HARD_LINKS
967 if (preserve_hard_links && F_IS_HLINKED(file))
968 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, -1);
973 return FNAMECMP_BASIS_DIR_LOW + j;
976 /* This is only called for non-regular files. We return -2 if we've finished
977 * handling the file, or -1 if no dest-linking occurred, or a non-negative
978 * value if we found an alternate basis file. */
979 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
980 char *cmpbuf, stat_x *sxp, int itemizing,
983 char lnk[MAXPATHLEN];
986 enum nonregtype type;
990 #ifndef SUPPORT_LINKS
991 if (S_ISLNK(file->mode))
994 if (S_ISDIR(file->mode)) {
996 } else if (IS_SPECIAL(file->mode))
998 else if (IS_DEVICE(file->mode))
1000 #ifdef SUPPORT_LINKS
1001 else if (S_ISLNK(file->mode))
1002 type = TYPE_SYMLINK;
1006 "internal: try_dests_non() called with invalid mode (%o)\n",
1008 exit_cleanup(RERR_UNSUPPORTED);
1012 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1013 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1017 if (!S_ISDIR(sxp->st.st_mode))
1021 if (!IS_SPECIAL(sxp->st.st_mode))
1025 if (!IS_DEVICE(sxp->st.st_mode))
1028 #ifdef SUPPORT_LINKS
1030 if (!S_ISLNK(sxp->st.st_mode))
1035 if (match_level < 1) {
1044 devp = F_RDEV_P(file);
1045 if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
1048 #ifdef SUPPORT_LINKS
1050 if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
1053 if (strcmp(lnk, F_SYMLINK(file)) != 0)
1058 if (match_level < 2) {
1062 if (unchanged_attrs(cmpbuf, file, sxp)) {
1067 } while (basis_dir[++j] != NULL);
1072 if (j != best_match) {
1074 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1075 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1079 if (match_level == 3) {
1080 #ifdef SUPPORT_HARD_LINKS
1082 #ifndef CAN_HARDLINK_SYMLINK
1083 && !S_ISLNK(file->mode)
1085 #ifndef CAN_HARDLINK_SPECIAL
1086 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
1088 && !S_ISDIR(file->mode)) {
1089 if (do_link(cmpbuf, fname) < 0) {
1090 rsyserr(FERROR, errno,
1091 "failed to hard-link %s with %s",
1095 if (preserve_hard_links && F_IS_HLINKED(file))
1096 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1100 if (itemizing && stdout_format_has_i
1101 && (verbose > 1 || stdout_format_has_i > 1)) {
1102 int chg = compare_dest && type != TYPE_DIR ? 0
1104 + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1105 char *lp = match_level == 3 ? "" : NULL;
1106 itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
1108 if (verbose > 1 && maybe_ATTRS_REPORT) {
1109 rprintf(FCLIENT, "%s%s is uptodate\n",
1110 fname, type == TYPE_DIR ? "/" : "");
1118 static void list_file_entry(struct file_struct *f)
1120 char permbuf[PERMSTRING_SIZE];
1123 if (!F_IS_ACTIVE(f)) {
1124 /* this can happen if duplicate names were removed */
1128 permstring(permbuf, f->mode);
1131 /* TODO: indicate '+' if the entry has an ACL. */
1133 #ifdef SUPPORT_LINKS
1134 if (preserve_links && S_ISLNK(f->mode)) {
1135 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
1136 permbuf, len, timestring(f->modtime),
1137 f_name(f, NULL), F_SYMLINK(f));
1141 rprintf(FINFO, "%s %11.0f %s %s\n",
1142 permbuf, len, timestring(f->modtime),
1147 static int phase = 0;
1148 static int dflt_perms;
1150 /* Acts on the indicated item in cur_flist whose name is fname. If a dir,
1151 * make sure it exists, and has the right permissions/timestamp info. For
1152 * all other non-regular files (symlinks, etc.) we create them here. For
1153 * regular files that have changed, we try to find a basis file and then
1154 * start sending checksums. The ndx is the file's unique index value.
1156 * When fname is non-null, it must point to a MAXPATHLEN buffer!
1158 * Note that f_out is set to -1 when doing final directory-permission and
1159 * modification-time repair. */
1160 static void recv_generator(char *fname, struct file_struct *file, int ndx,
1161 int itemizing, enum logcode code, int f_out)
1163 static int missing_below = -1, excluded_below = -1;
1164 static const char *parent_dirname = "";
1165 static struct file_struct *missing_dir = NULL, *excluded_dir = NULL;
1166 static struct file_list *fuzzy_dirlist = NULL;
1167 static int need_fuzzy_dirlist = 0;
1168 struct file_struct *fuzzy_file = NULL;
1169 int fd = -1, f_copy = -1;
1171 STRUCT_STAT partial_st;
1172 struct file_struct *back_file = NULL;
1173 int statret, real_ret, stat_errno;
1174 char *fnamecmp, *partialptr, *backupptr = NULL;
1175 char fnamecmpbuf[MAXPATHLEN];
1176 uchar fnamecmp_type;
1177 int implied_dirs_are_missing = relative_paths && !implied_dirs && protocol_version < 30;
1178 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1181 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
1184 if (S_ISDIR(file->mode)
1185 && ((!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
1186 || (inc_recurse && ndx != cur_flist->ndx_start - 1)))
1188 list_file_entry(file);
1192 if (server_filter_list.head) {
1193 if (excluded_below >= 0) {
1194 if (F_DEPTH(file) > excluded_below
1195 && (!implied_dirs_are_missing || f_name_has_prefix(file, excluded_dir)))
1197 excluded_below = -1;
1199 if (check_filter(&server_filter_list, fname,
1200 S_ISDIR(file->mode)) < 0) {
1201 if (S_ISDIR(file->mode)) {
1202 excluded_below = F_DEPTH(file);
1203 excluded_dir = file;
1208 "skipping server-excluded file \"%s\"\n",
1215 if (missing_below >= 0) {
1216 if (F_DEPTH(file) <= missing_below
1217 || (implied_dirs_are_missing && !f_name_has_prefix(file, missing_dir))) {
1221 } else if (!dry_run) {
1222 if (S_ISDIR(file->mode))
1223 file->flags |= FLAG_MISSING_DIR;
1228 sx.acc_acl = sx.def_acl = NULL;
1230 #ifdef SUPPORT_XATTRS
1234 if (fuzzy_dirlist) {
1235 flist_free(fuzzy_dirlist);
1236 fuzzy_dirlist = NULL;
1238 parent_dirname = "";
1240 stat_errno = ENOENT;
1242 const char *dn = file->dirname ? file->dirname : ".";
1243 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1244 if (relative_paths && !implied_dirs
1245 && do_stat(dn, &sx.st) < 0
1246 && create_directory_path(fname) < 0) {
1247 rsyserr(FERROR, errno,
1248 "recv_generator: mkdir %s failed",
1251 if (fuzzy_dirlist) {
1252 flist_free(fuzzy_dirlist);
1253 fuzzy_dirlist = NULL;
1256 need_fuzzy_dirlist = 1;
1258 if (!preserve_perms)
1259 dflt_perms = default_perms_for_dir(dn);
1262 parent_dirname = dn;
1264 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
1265 strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1266 fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
1267 need_fuzzy_dirlist = 0;
1270 statret = link_stat(fname, &sx.st,
1271 keep_dirlinks && S_ISDIR(file->mode));
1275 if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
1277 rprintf(FINFO, "not creating new %s \"%s\"\n",
1278 S_ISDIR(file->mode) ? "directory" : "file",
1281 if (S_ISDIR(file->mode)) {
1282 if (missing_below < 0) {
1285 missing_below = F_DEPTH(file);
1288 file->flags |= FLAG_MISSING_DIR;
1293 if (S_ISDIR(file->mode)) {
1294 if (!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
1296 if (inc_recurse && ndx != cur_flist->ndx_start - 1) {
1297 /* In inc_recurse mode we want to make sure any missing
1298 * directories get created while we're still processing
1299 * the parent dir (which allows us to touch the parent
1300 * dir's mtime right away). We will handle the dir in
1301 * full later (right before we handle its contents). */
1303 && (S_ISDIR(sx.st.st_mode)
1304 || delete_item(fname, sx.st.st_mode, "directory", del_opts) != 0))
1305 goto cleanup; /* Any errors get reported later. */
1306 if (do_mkdir(fname, file->mode & 0700) == 0)
1307 file->flags |= FLAG_DIR_CREATED;
1310 /* The file to be received is a directory, so we need
1311 * to prepare appropriately. If there is already a
1312 * file of that name and it is *not* a directory, then
1313 * we need to delete it. If it doesn't exist, then
1314 * (perhaps recursively) create it. */
1315 if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1316 if (delete_item(fname, sx.st.st_mode, "directory", del_opts) != 0)
1317 goto skipping_dir_contents;
1320 if (dry_run && statret != 0 && missing_below < 0) {
1321 missing_below = F_DEPTH(file);
1327 if (file->flags & FLAG_DIR_CREATED)
1329 if (!preserve_perms) { /* See comment in non-dir code below. */
1330 file->mode = dest_mode(file->mode, sx.st.st_mode,
1331 dflt_perms, statret == 0);
1333 if (statret != 0 && basis_dir[0] != NULL) {
1334 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1342 if (itemizing && f_out != -1) {
1343 itemize(fname, file, ndx, statret, &sx,
1344 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1346 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1347 if (!relative_paths || errno != ENOENT
1348 || create_directory_path(fname) < 0
1349 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
1350 rsyserr(FERROR, errno,
1351 "recv_generator: mkdir %s failed",
1353 skipping_dir_contents:
1355 "*** Skipping any contents from this failed directory ***\n");
1356 missing_below = F_DEPTH(file);
1358 file->flags |= FLAG_MISSING_DIR;
1362 if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
1363 && verbose && code != FNONE && f_out != -1)
1364 rprintf(code, "%s/\n", fname);
1366 /* We need to ensure that the dirs in the transfer have writable
1367 * permissions during the time we are putting files within them.
1368 * This is then fixed after the transfer is done. */
1370 if (!am_root && !(file->mode & S_IWUSR) && dir_tweaking) {
1371 mode_t mode = file->mode | S_IWUSR;
1372 if (do_chmod(fname, mode) < 0) {
1373 rsyserr(FERROR, errno,
1374 "failed to modify permissions on %s",
1377 need_retouch_dir_perms = 1;
1381 if (real_ret != 0 && one_file_system)
1382 real_sx.st.st_dev = filesystem_dev;
1384 if (one_file_system) {
1385 uint32 *devp = F_DIR_DEV_P(file);
1386 DEV_MAJOR(devp) = major(real_sx.st.st_dev);
1387 DEV_MINOR(devp) = minor(real_sx.st.st_dev);
1390 else if (delete_during && f_out != -1 && !phase && dry_run < 2
1391 && (file->flags & FLAG_CONTENT_DIR))
1392 delete_in_dir(fname, file, &real_sx.st.st_dev);
1396 /* If we're not preserving permissions, change the file-list's
1397 * mode based on the local permissions and some heuristics. */
1398 if (!preserve_perms) {
1399 int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
1400 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
1404 #ifdef SUPPORT_HARD_LINKS
1405 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1406 && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
1410 if (preserve_links && S_ISLNK(file->mode)) {
1411 #ifdef SUPPORT_LINKS
1412 const char *sl = F_SYMLINK(file);
1413 if (safe_symlinks && unsafe_symlink(sl, fname)) {
1416 fname = f_name(file, NULL);
1418 "ignoring unsafe symlink %s -> \"%s\"\n",
1419 full_fname(fname), sl);
1424 char lnk[MAXPATHLEN];
1427 if (!S_ISLNK(sx.st.st_mode))
1429 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1430 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1431 /* The link is pointing to the right place. */
1432 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1434 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1435 #ifdef SUPPORT_HARD_LINKS
1436 if (preserve_hard_links && F_IS_HLINKED(file))
1437 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1439 if (remove_source_files == 1)
1440 goto return_with_success;
1443 /* Not the right symlink (or not a symlink), so
1445 if (delete_item(fname, sx.st.st_mode, "symlink", del_opts) != 0)
1447 } else if (basis_dir[0] != NULL) {
1448 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1451 #ifndef CAN_HARDLINK_SYMLINK
1453 /* Resort to --copy-dest behavior. */
1463 #ifdef SUPPORT_HARD_LINKS
1464 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1465 cur_flist->in_progress++;
1469 if (do_symlink(sl, fname) != 0) {
1470 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
1471 full_fname(fname), sl);
1473 set_file_attrs(fname, file, NULL, NULL, 0);
1475 itemize(fname, file, ndx, statret, &sx,
1476 ITEM_LOCAL_CHANGE, 0, NULL);
1478 if (code != FNONE && verbose)
1479 rprintf(code, "%s -> %s\n", fname, sl);
1480 #ifdef SUPPORT_HARD_LINKS
1481 if (preserve_hard_links && F_IS_HLINKED(file))
1482 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1484 /* This does not check remove_source_files == 1
1485 * because this is one of the items that the old
1486 * --remove-sent-files option would remove. */
1487 if (remove_source_files)
1488 goto return_with_success;
1494 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1495 || (preserve_specials && IS_SPECIAL(file->mode))) {
1496 uint32 *devp = F_RDEV_P(file);
1497 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1500 if (IS_DEVICE(file->mode)) {
1501 if (!IS_DEVICE(sx.st.st_mode))
1505 if (!IS_SPECIAL(sx.st.st_mode))
1510 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
1511 && sx.st.st_rdev == rdev) {
1512 /* The device or special file is identical. */
1513 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1515 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1516 #ifdef SUPPORT_HARD_LINKS
1517 if (preserve_hard_links && F_IS_HLINKED(file))
1518 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1520 if (remove_source_files == 1)
1521 goto return_with_success;
1524 if (delete_item(fname, sx.st.st_mode, t, del_opts) != 0)
1526 } else if (basis_dir[0] != NULL) {
1527 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1530 #ifndef CAN_HARDLINK_SPECIAL
1532 /* Resort to --copy-dest behavior. */
1542 #ifdef SUPPORT_HARD_LINKS
1543 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1544 cur_flist->in_progress++;
1549 rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1550 fname, (int)file->mode,
1551 (long)major(rdev), (long)minor(rdev));
1553 if (do_mknod(fname, file->mode, rdev) < 0) {
1554 rsyserr(FERROR, errno, "mknod %s failed",
1557 set_file_attrs(fname, file, NULL, NULL, 0);
1559 itemize(fname, file, ndx, statret, &sx,
1560 ITEM_LOCAL_CHANGE, 0, NULL);
1562 if (code != FNONE && verbose)
1563 rprintf(code, "%s\n", fname);
1564 #ifdef SUPPORT_HARD_LINKS
1565 if (preserve_hard_links && F_IS_HLINKED(file))
1566 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1568 if (remove_source_files == 1)
1569 goto return_with_success;
1574 if (!S_ISREG(file->mode)) {
1576 fname = f_name(file, NULL);
1577 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1581 if (max_size > 0 && F_LENGTH(file) > max_size) {
1584 fname = f_name(file, NULL);
1585 rprintf(FINFO, "%s is over max-size\n", fname);
1589 if (min_size > 0 && F_LENGTH(file) < min_size) {
1592 fname = f_name(file, NULL);
1593 rprintf(FINFO, "%s is under min-size\n", fname);
1598 if (ignore_existing > 0 && statret == 0) {
1600 rprintf(FINFO, "%s exists\n", fname);
1604 if (update_only > 0 && statret == 0
1605 && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
1607 rprintf(FINFO, "%s is newer\n", fname);
1612 fnamecmp_type = FNAMECMP_FNAME;
1614 if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
1615 if (delete_item(fname, sx.st.st_mode, "regular file", del_opts) != 0)
1618 stat_errno = ENOENT;
1621 if (statret != 0 && basis_dir[0] != NULL) {
1622 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
1625 if (remove_source_files == 1)
1626 goto return_with_success;
1630 fnamecmp = fnamecmpbuf;
1639 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1640 && link_stat(partialptr, &partial_st, 0) == 0
1641 && S_ISREG(partial_st.st_mode)) {
1643 goto prepare_to_open;
1647 if (statret != 0 && fuzzy_dirlist && dry_run <= 1) {
1648 int j = find_fuzzy(file, fuzzy_dirlist);
1650 fuzzy_file = fuzzy_dirlist->files[j];
1651 f_name(fuzzy_file, fnamecmpbuf);
1653 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1654 fname, fnamecmpbuf);
1656 sx.st.st_size = F_LENGTH(fuzzy_file);
1658 fnamecmp = fnamecmpbuf;
1659 fnamecmp_type = FNAMECMP_FUZZY;
1664 #ifdef SUPPORT_HARD_LINKS
1665 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1666 cur_flist->in_progress++;
1670 if (stat_errno == ENOENT)
1672 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
1677 if (append_mode > 0 && sx.st.st_size >= F_LENGTH(file))
1680 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1682 else if (fnamecmp_type == FNAMECMP_FUZZY)
1684 else if (unchanged_file(fnamecmp, file, &sx.st)) {
1686 do_unlink(partialptr);
1687 handle_partial_dir(partialptr, PDIR_DELETE);
1689 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1691 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
1692 #ifdef SUPPORT_HARD_LINKS
1693 if (preserve_hard_links && F_IS_HLINKED(file))
1694 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1696 if (remove_source_files != 1)
1698 return_with_success:
1700 send_msg_int(MSG_SUCCESS, ndx);
1707 fnamecmp = partialptr;
1708 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1715 if (read_batch || whole_file) {
1716 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1717 if (!(backupptr = get_backup_name(fname)))
1719 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
1720 goto pretend_missing;
1721 if (copy_file(fname, backupptr, -1, back_file->mode, 1) < 0) {
1722 unmake_file(back_file);
1730 if (fuzzy_dirlist) {
1731 int j = flist_find(fuzzy_dirlist, file);
1732 if (j >= 0) /* don't use changing file as future fuzzy basis */
1733 fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
1737 if ((fd = do_open(fnamecmp, O_RDONLY, 0)) < 0) {
1738 rsyserr(FERROR, errno, "failed to open %s, continuing",
1739 full_fname(fnamecmp));
1741 /* pretend the file didn't exist */
1742 #ifdef SUPPORT_HARD_LINKS
1743 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1744 cur_flist->in_progress++;
1748 statret = real_ret = -1;
1752 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1753 if (!(backupptr = get_backup_name(fname))) {
1757 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1759 goto pretend_missing;
1761 if (robust_unlink(backupptr) && errno != ENOENT) {
1762 rsyserr(FERROR, errno, "unlink %s",
1763 full_fname(backupptr));
1764 unmake_file(back_file);
1769 if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0
1770 && (errno != ENOENT || make_bak_dir(backupptr) < 0
1771 || (f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0)) {
1772 rsyserr(FERROR, errno, "open %s",
1773 full_fname(backupptr));
1774 unmake_file(back_file);
1779 fnamecmp_type = FNAMECMP_BACKUP;
1783 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1784 fnamecmp, (double)sx.st.st_size);
1788 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1791 if (remove_source_files && !delay_updates && !phase)
1792 increment_active_files(ndx, itemizing, code);
1793 if (inc_recurse && !dry_run)
1794 cur_flist->in_progress++;
1795 #ifdef SUPPORT_HARD_LINKS
1796 if (preserve_hard_links && F_IS_HLINKED(file))
1797 file->flags |= FLAG_FILE_SENT;
1799 write_ndx(f_out, ndx);
1801 int iflags = ITEM_TRANSFER;
1802 if (always_checksum > 0)
1803 iflags |= ITEM_REPORT_CHECKSUM;
1804 if (fnamecmp_type != FNAMECMP_FNAME)
1805 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1806 if (fnamecmp_type == FNAMECMP_FUZZY)
1807 iflags |= ITEM_XNAME_FOLLOWS;
1808 itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
1809 fuzzy_file ? fuzzy_file->basename : NULL);
1814 #ifdef SUPPORT_XATTRS
1815 if (preserve_xattrs)
1816 free_xattr(&real_sx);
1821 #ifdef SUPPORT_HARD_LINKS
1822 if (preserve_hard_links && F_IS_HLINKED(file))
1823 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1830 if (statret != 0 || whole_file)
1831 write_sum_head(f_out, NULL);
1833 generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy);
1841 set_file_attrs(backupptr, back_file, NULL, NULL, 0);
1843 rprintf(FINFO, "backed up %s to %s\n",
1846 unmake_file(back_file);
1853 #ifdef SUPPORT_XATTRS
1854 if (preserve_xattrs)
1860 static void touch_up_dirs(struct file_list *flist, int ndx)
1862 static int counter = 0;
1863 struct file_struct *file;
1869 end = flist->used - 1;
1873 /* Fix any directory permissions that were modified during the
1874 * transfer and/or re-set any tweaked modified-time values. */
1875 for (i = start; i <= end; i++, counter++) {
1876 file = flist->files[i];
1877 if (!S_ISDIR(file->mode)
1878 || (!implied_dirs && file->flags & FLAG_IMPLIED_DIR))
1881 fname = f_name(file, NULL);
1882 rprintf(FINFO, "touch_up_dirs: %s (%d)\n",
1885 if (!F_IS_ACTIVE(file) || file->flags & FLAG_MISSING_DIR
1886 || (!need_retouch_dir_times && file->mode & S_IWUSR))
1888 fname = f_name(file, NULL);
1889 if (!(file->mode & S_IWUSR))
1890 do_chmod(fname, file->mode);
1891 if (need_retouch_dir_times)
1892 set_modtime(fname, file->modtime, file->mode);
1893 if (allowed_lull && !(counter % lull_mod))
1894 maybe_send_keepalive();
1895 else if (!(counter & 0xFF))
1896 maybe_flush_socket(0);
1900 void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
1902 struct file_struct *file;
1903 struct file_list *flist;
1904 char fbuf[MAXPATHLEN];
1907 #ifdef SUPPORT_HARD_LINKS
1908 while (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
1909 flist = flist_for_ndx(ndx);
1910 assert(flist != NULL);
1911 file = flist->files[ndx - flist->ndx_start];
1912 assert(file->flags & FLAG_HLINKED);
1913 finish_hard_link(file, f_name(file, fbuf), ndx, NULL, itemizing, code, -1);
1914 flist->in_progress--;
1918 while (check_redo && (ndx = get_redo_num()) != -1) {
1919 csum_length = SUM_LENGTH;
1920 max_size = -max_size;
1921 min_size = -min_size;
1922 ignore_existing = -ignore_existing;
1923 ignore_non_existing = -ignore_non_existing;
1924 update_only = -update_only;
1925 always_checksum = -always_checksum;
1926 size_only = -size_only;
1927 append_mode = -append_mode;
1928 make_backups = -make_backups; /* avoid dup backup w/inplace */
1932 cur_flist = flist_for_ndx(ndx);
1934 file = cur_flist->files[ndx - cur_flist->ndx_start];
1936 strlcpy(fbuf, solo_file, sizeof fbuf);
1939 recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
1940 cur_flist->to_redo--;
1944 csum_length = SHORT_SUM_LENGTH;
1945 max_size = -max_size;
1946 min_size = -min_size;
1947 ignore_existing = -ignore_existing;
1948 ignore_non_existing = -ignore_non_existing;
1949 update_only = -update_only;
1950 always_checksum = -always_checksum;
1951 size_only = -size_only;
1952 append_mode = -append_mode;
1953 make_backups = -make_backups;
1957 while (cur_flist != first_flist) { /* only possible with inc_recurse */
1958 if (first_flist->in_progress || first_flist->to_redo)
1962 write_ndx(sock_f_out, NDX_DONE);
1963 maybe_flush_socket(1);
1966 if (delete_during == 2 || !dir_tweaking) {
1967 /* Skip directory touch-up. */
1968 } else if (first_flist->parent_ndx >= 0)
1969 touch_up_dirs(dir_flist, first_flist->parent_ndx);
1971 flist_free(first_flist); /* updates first_flist */
1975 void generate_files(int f_out, const char *local_name)
1978 char fbuf[MAXPATHLEN];
1981 int save_do_progress = do_progress;
1983 if (protocol_version >= 29) {
1985 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1986 code = logfile_format_has_i ? FNONE : FLOG;
1987 } else if (am_daemon) {
1988 itemizing = logfile_format_has_i && do_xfers;
1989 maybe_ATTRS_REPORT = ATTRS_REPORT;
1990 code = itemizing || !do_xfers ? FCLIENT : FINFO;
1991 } else if (!am_server) {
1992 itemizing = stdout_format_has_i;
1993 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1994 code = itemizing ? FNONE : FINFO;
1997 maybe_ATTRS_REPORT = ATTRS_REPORT;
2000 solo_file = local_name;
2001 dir_tweaking = !(list_only || solo_file || dry_run);
2002 need_retouch_dir_times = preserve_times > 1;
2003 lull_mod = allowed_lull * 5;
2006 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
2008 if (delete_before && !solo_file && cur_flist->used > 0)
2010 if (delete_during == 2) {
2011 deldelay_size = BIGPATHBUFLEN * 4;
2012 deldelay_buf = new_array(char, deldelay_size);
2014 out_of_memory("delete-delay");
2018 if (append_mode > 0 || whole_file < 0)
2021 rprintf(FINFO, "delta-transmission %s\n",
2023 ? "disabled for local transfer or --whole-file"
2027 /* Since we often fill up the outgoing socket and then just sit around
2028 * waiting for the other 2 processes to do their thing, we don't want
2029 * to exit on a timeout. If the data stops flowing, the receiver will
2030 * notice that and let us know via the redo pipe (or its closing). */
2033 dflt_perms = (ACCESSPERMS & ~orig_umask);
2036 #ifdef SUPPORT_HARD_LINKS
2037 if (preserve_hard_links && inc_recurse) {
2038 while (!flist_eof && file_total < FILECNT_LOOKAHEAD/2)
2039 wait_for_receiver();
2043 if (inc_recurse && cur_flist->parent_ndx >= 0) {
2044 struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
2046 ndx = cur_flist->ndx_start - 1;
2047 recv_generator(fbuf, fp, ndx, itemizing, code, f_out);
2048 if (delete_during && dry_run < 2) {
2049 if (BITS_SETnUNSET(fp->flags, FLAG_CONTENT_DIR, FLAG_MISSING_DIR)) {
2051 if (one_file_system) {
2052 uint32 *devp = F_DIR_DEV_P(fp);
2053 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
2055 dirdev = MAKEDEV(0, 0);
2056 delete_in_dir(f_name(fp, fbuf), fp, &dirdev);
2060 for (i = cur_flist->low; i <= cur_flist->high; i++) {
2061 struct file_struct *file = cur_flist->sorted[i];
2063 if (!F_IS_ACTIVE(file))
2071 ndx = i + cur_flist->ndx_start;
2074 strlcpy(fbuf, solo_file, sizeof fbuf);
2077 recv_generator(fbuf, file, ndx, itemizing, code, f_out);
2079 check_for_finished_files(itemizing, code, 0);
2081 if (allowed_lull && !(i % lull_mod))
2082 maybe_send_keepalive();
2083 else if (!(i & 0xFF))
2084 maybe_flush_socket(0);
2088 write_ndx(f_out, NDX_DONE);
2093 check_for_finished_files(itemizing, code, 1);
2094 if (cur_flist->next || flist_eof)
2096 wait_for_receiver();
2098 } while ((cur_flist = cur_flist->next) != NULL);
2101 delete_in_dir(NULL, NULL, &dev_zero);
2104 rprintf(FINFO, "generate_files phase=%d\n", phase);
2107 check_for_finished_files(itemizing, code, 1);
2110 wait_for_receiver();
2115 rprintf(FINFO, "generate_files phase=%d\n", phase);
2117 write_ndx(f_out, NDX_DONE);
2118 /* Reduce round-trip lag-time for a useless delay-updates phase. */
2119 if (protocol_version >= 29 && !delay_updates)
2120 write_ndx(f_out, NDX_DONE);
2122 /* Read MSG_DONE for the redo phase (and any prior messages). */
2124 check_for_finished_files(itemizing, code, 0);
2125 if (msgdone_cnt > 1)
2127 wait_for_receiver();
2130 if (protocol_version >= 29) {
2133 rprintf(FINFO, "generate_files phase=%d\n", phase);
2135 write_ndx(f_out, NDX_DONE);
2136 /* Read MSG_DONE for delay-updates phase & prior messages. */
2137 while (msgdone_cnt == 2)
2138 wait_for_receiver();
2141 do_progress = save_do_progress;
2142 if (delete_during == 2)
2143 do_delayed_deletions(fbuf);
2144 if (delete_after && !solo_file && file_total > 0)
2147 if ((need_retouch_dir_perms || need_retouch_dir_times)
2148 && dir_tweaking && (!inc_recurse || delete_during == 2))
2149 touch_up_dirs(dir_flist, -1);
2151 if (max_delete >= 0 && deletion_count > max_delete) {
2153 "Deletions stopped due to --max-delete limit (%d skipped)\n",
2154 deletion_count - max_delete);
2155 io_error |= IOERR_DEL_LIMIT;
2159 rprintf(FINFO, "generate_files finished\n");