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, 2004, 2005, 2006 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 2 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, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
29 extern int stdout_format_has_i;
30 extern int logfile_format_has_i;
34 extern int do_progress;
35 extern int relative_paths;
36 extern int implied_dirs;
37 extern int keep_dirlinks;
38 extern int preserve_links;
39 extern int preserve_devices;
40 extern int preserve_specials;
41 extern int preserve_hard_links;
42 extern int preserve_perms;
43 extern int preserve_uid;
44 extern int preserve_gid;
45 extern int preserve_times;
46 extern int omit_dir_times;
47 extern int delete_mode;
48 extern int delete_before;
49 extern int delete_during;
50 extern int delete_after;
52 extern int ignore_errors;
53 extern int remove_source_files;
54 extern int delay_updates;
55 extern int update_only;
56 extern int ignore_existing;
57 extern int ignore_non_existing;
59 extern int append_mode;
60 extern int make_backups;
61 extern int csum_length;
62 extern int ignore_times;
64 extern OFF_T max_size;
65 extern OFF_T min_size;
67 extern int allowed_lull;
68 extern int sock_f_out;
69 extern int ignore_timeout;
70 extern int protocol_version;
71 extern int fuzzy_basis;
72 extern int always_checksum;
73 extern int checksum_len;
74 extern char *partial_dir;
75 extern char *basis_dir[];
76 extern int compare_dest;
79 extern int whole_file;
81 extern int new_root_dir;
82 extern int read_batch;
83 extern int safe_symlinks;
84 extern long block_size; /* "long" because popt can't set an int32. */
85 extern int max_delete;
86 extern int force_delete;
87 extern int one_file_system;
88 extern struct stats stats;
89 extern dev_t filesystem_dev;
90 extern char *backup_dir;
91 extern char *backup_suffix;
92 extern int backup_suffix_len;
93 extern struct file_list *the_file_list;
94 extern struct filter_list_struct server_filter_list;
96 static int deletion_count = 0; /* used to implement --max-delete */
98 /* For calling delete_item() */
99 #define DEL_RECURSE (1<<1) /* recurse */
102 TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
106 DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_PINNED, DR_NOT_EMPTY
109 /* Forward declaration for delete_item(). */
110 static enum delret delete_dir_contents(char *fname, int flags);
113 static int is_backup_file(char *fn)
115 int k = strlen(fn) - backup_suffix_len;
116 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
119 /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
120 * delete recursively.
122 * Note that fname must point to a MAXPATHLEN buffer if the mode indicates it's
123 * a directory! (The buffer is used for recursion, but returned unchanged.)
125 static enum delret delete_item(char *fname, int mode, char *replace, int flags)
132 rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
136 if (S_ISDIR(mode) && flags & DEL_RECURSE) {
137 ret = delete_dir_contents(fname, flags);
138 if (ret == DR_PINNED || ret == DR_NOT_EMPTY
139 || ret == DR_AT_LIMIT)
141 /* OK: try to delete the directory. */
144 if (!replace && max_delete >= 0 && ++deletion_count > max_delete)
149 ok = do_rmdir(fname) == 0;
150 } else if (make_backups && (backup_dir || !is_backup_file(fname))) {
151 what = "make_backup";
152 ok = make_backup(fname);
155 ok = robust_unlink(fname) == 0;
160 log_delete(fname, mode);
163 if (S_ISDIR(mode) && errno == ENOTEMPTY) {
164 rprintf(FINFO, "non-empty directory, %s, not deleted\n",
167 } else if (errno != ENOENT) {
168 rsyserr(FERROR, errno, "delete_file: %s(%s) failed",
169 what, full_fname(fname));
178 if (replace && ret != DR_SUCCESS) {
179 rprintf(FERROR, "could not make way for new %s: %s\n",
185 /* Prep directory is to be deleted, so delete all its contents. Note
186 * that fname must point to a MAXPATHLEN buffer! (The buffer is used
187 * for recursion, but returned unchanged.)
189 static enum delret delete_dir_contents(char *fname, int flags)
191 struct file_list *dirlist;
192 enum delret ret, result;
198 dlen = strlen(fname);
199 save_filters = push_local_filters(fname, dlen);
201 dirlist = get_dirlist(fname, dlen, 0);
204 if (dlen != 1 || *fname != '/')
206 remainder = MAXPATHLEN - (p - fname);
210 for (j = dirlist->count; j--; ) {
211 struct file_struct *fp = dirlist->files[j];
213 if (fp->flags & FLAG_MOUNT_POINT) {
216 "mount point, %s, pins parent directory\n",
223 strlcpy(p, fp->basename, remainder);
224 result = delete_item(fname, fp->mode, NULL, flags);
225 if (result != DR_SUCCESS && ret == DR_SUCCESS)
226 ret = result == DR_PINNED ? result : DR_NOT_EMPTY;
231 pop_local_filters(save_filters);
238 /* This function is used to implement per-directory deletion, and is used by
239 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
240 * MAXPATHLEN buffer with the name of the directory in it (the functions we
241 * call will append names onto the end, but the old dir value will be restored
243 static void delete_in_dir(struct file_list *flist, char *fbuf,
244 struct file_struct *file, STRUCT_STAT *stp)
246 static int min_depth = MAXPATHLEN, cur_depth = -1;
247 static void *filt_array[MAXPATHLEN/2+1];
248 static int already_warned = 0;
249 struct file_list *dirlist;
250 char delbuf[MAXPATHLEN];
254 while (cur_depth >= min_depth)
255 pop_local_filters(filt_array[cur_depth--]);
256 min_depth = MAXPATHLEN;
262 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
265 maybe_send_keepalive();
267 if (file->dir.depth >= MAXPATHLEN/2+1)
268 return; /* Impossible... */
270 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
274 "IO error encountered -- skipping file deletion\n");
279 while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
280 pop_local_filters(filt_array[cur_depth--]);
281 cur_depth = file->dir.depth;
282 if (min_depth > cur_depth)
283 min_depth = cur_depth;
285 filt_array[cur_depth] = push_local_filters(fbuf, dlen);
287 if (one_file_system) {
288 if (file->flags & FLAG_TOP_DIR)
289 filesystem_dev = stp->st_dev;
290 else if (filesystem_dev != stp->st_dev)
294 dirlist = get_dirlist(fbuf, dlen, 0);
296 /* If an item in dirlist is not found in flist, delete it
297 * from the filesystem. */
298 for (i = dirlist->count; i--; ) {
299 struct file_struct *fp = dirlist->files[i];
302 if (fp->flags & FLAG_MOUNT_POINT) {
304 rprintf(FINFO, "mount point %s not deleted\n",
308 if (flist_find(flist, fp) < 0) {
310 delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
317 /* This deletes any files on the receiving side that are not present on the
318 * sending side. This is used by --delete-before and --delete-after. */
319 static void do_delete_pass(struct file_list *flist)
321 char fbuf[MAXPATHLEN];
325 /* dry_run is incremented when the destination doesn't exist yet. */
326 if (dry_run > 1 || list_only)
329 for (j = 0; j < flist->count; j++) {
330 struct file_struct *file = flist->files[j];
332 if (!(file->flags & FLAG_DEL_HERE))
336 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
337 rprintf(FINFO, "deleting in %s\n", fbuf);
339 if (link_stat(fbuf, &st, keep_dirlinks) < 0
340 || !S_ISDIR(st.st_mode))
343 delete_in_dir(flist, fbuf, file, &st);
345 delete_in_dir(NULL, NULL, NULL, NULL);
347 if (do_progress && !am_server)
348 rprintf(FINFO, " \r");
351 int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
354 && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
357 if (am_root && preserve_uid && st->st_uid != file->uid)
360 if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid)
366 void itemize(struct file_struct *file, int ndx, int statret, STRUCT_STAT *st,
367 int32 iflags, uchar fnamecmp_type, char *xname)
369 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
370 int keep_time = !preserve_times ? 0
371 : S_ISDIR(file->mode) ? !omit_dir_times
372 : !S_ISLNK(file->mode);
374 if (S_ISREG(file->mode) && file->length != st->st_size)
375 iflags |= ITEM_REPORT_SIZE;
376 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
377 && !(iflags & ITEM_MATCHED)
378 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
379 || (keep_time && cmp_time(file->modtime, st->st_mtime) != 0))
380 iflags |= ITEM_REPORT_TIME;
381 if ((file->mode & CHMOD_BITS) != (st->st_mode & CHMOD_BITS))
382 iflags |= ITEM_REPORT_PERMS;
383 if (preserve_uid && am_root && file->uid != st->st_uid)
384 iflags |= ITEM_REPORT_OWNER;
385 if (preserve_gid && file->gid != GID_NONE
386 && st->st_gid != file->gid)
387 iflags |= ITEM_REPORT_GROUP;
389 iflags |= ITEM_IS_NEW;
392 if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
393 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
394 if (protocol_version >= 29) {
396 write_int(sock_f_out, ndx);
397 write_shortint(sock_f_out, iflags);
398 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
399 write_byte(sock_f_out, fnamecmp_type);
400 if (iflags & ITEM_XNAME_FOLLOWS)
401 write_vstring(sock_f_out, xname, strlen(xname));
402 } else if (ndx >= 0) {
403 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
404 log_item(code, file, &stats, iflags, xname);
410 /* Perform our quick-check heuristic for determining if a file is unchanged. */
411 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
413 if (st->st_size != file->length)
416 /* if always checksum is set then we use the checksum instead
417 of the file time to determine whether to sync */
418 if (always_checksum && S_ISREG(st->st_mode)) {
419 char sum[MD4_SUM_LENGTH];
420 file_checksum(fn, sum, st->st_size);
421 return memcmp(sum, file->u.sum, checksum_len) == 0;
430 return cmp_time(st->st_mtime, file->modtime) == 0;
435 * set (initialize) the size entries in the per-file sum_struct
436 * calculating dynamic block and checksum sizes.
438 * This is only called from generate_and_send_sums() but is a separate
439 * function to encapsulate the logic.
441 * The block size is a rounded square root of file length.
443 * The checksum size is determined according to:
444 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
445 * provided by Donovan Baarda which gives a probability of rsync
446 * algorithm corrupting data and falling back using the whole md4
449 * This might be made one of several selectable heuristics.
451 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
457 blength = block_size;
458 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
459 blength = BLOCK_SIZE;
464 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
465 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
466 blength = MAX_BLOCK_SIZE;
471 if (len < (int64)blength * blength)
474 } while (c >= 8); /* round to multiple of 8 */
475 blength = MAX(blength, BLOCK_SIZE);
479 if (protocol_version < 27) {
480 s2length = csum_length;
481 } else if (csum_length == SUM_LENGTH) {
482 s2length = SUM_LENGTH;
486 int b = BLOCKSUM_BIAS;
487 for (l = len; l >>= 1; b += 2) {}
488 for (c = blength; (c >>= 1) && b; b--) {}
489 /* add a bit, subtract rollsum, round up. */
490 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
491 s2length = MAX(s2length, csum_length);
492 s2length = MIN(s2length, SUM_LENGTH);
496 sum->blength = blength;
497 sum->s2length = s2length;
498 sum->remainder = len % blength;
499 sum->count = len / blength + (sum->remainder != 0);
501 if (sum->count && verbose > 2) {
503 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
504 (double)sum->count, (long)sum->remainder, (long)sum->blength,
505 sum->s2length, (double)sum->flength);
511 * Generate and send a stream of signatures/checksums that describe a buffer
513 * Generate approximately one checksum every block_len bytes.
515 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
518 struct map_struct *mapbuf;
519 struct sum_struct sum;
522 sum_sizes_sqroot(&sum, len);
523 write_sum_head(f_out, &sum);
525 if (append_mode > 0 && f_copy < 0)
529 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
533 for (i = 0; i < sum.count; i++) {
534 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
535 char *map = map_ptr(mapbuf, offset, n1);
536 char sum2[SUM_LENGTH];
543 full_write(f_copy, map, n1);
548 sum1 = get_checksum1(map, n1);
549 get_checksum2(map, n1, sum2);
553 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
554 (double)i, (double)offset - n1, (long)n1,
555 (unsigned long)sum1);
557 write_int(f_out, sum1);
558 write_buf(f_out, sum2, sum.s2length);
566 /* Try to find a filename in the same dir as "fname" with a similar name. */
567 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
569 int fname_len, fname_suf_len;
570 const char *fname_suf, *fname = file->basename;
571 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
572 int j, lowest_j = -1;
574 fname_len = strlen(fname);
575 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
577 for (j = 0; j < dirlist->count; j++) {
578 struct file_struct *fp = dirlist->files[j];
579 const char *suf, *name;
583 if (!S_ISREG(fp->mode) || !fp->length
584 || fp->flags & FLAG_NO_FUZZY)
589 if (fp->length == file->length
590 && cmp_time(fp->modtime, file->modtime) == 0) {
593 "fuzzy size/modtime match for %s\n",
600 suf = find_filename_suffix(name, len, &suf_len);
602 dist = fuzzy_distance(name, len, fname, fname_len);
603 /* Add some extra weight to how well the suffixes match. */
604 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
607 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
608 name, (int)(dist>>16), (int)(dist&0xFFFF));
610 if (dist <= lowest_dist) {
619 void check_for_finished_hlinks(int itemizing, enum logcode code)
621 struct file_struct *file;
624 while ((ndx = get_hlink_num()) != -1) {
625 if (ndx < 0 || ndx >= the_file_list->count)
628 file = the_file_list->files[ndx];
629 if (!file->link_u.links)
632 hard_link_cluster(file, ndx, itemizing, code);
636 /* This is only called for regular files. We return -2 if we've finished
637 * handling the file, -1 if no dest-linking occurred, or a non-negative
638 * value if we found an alternate basis file. */
639 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
640 char *cmpbuf, STRUCT_STAT *stp, int itemizing,
641 int maybe_ATTRS_REPORT, enum logcode code)
648 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
649 if (link_stat(cmpbuf, stp, 0) < 0 || !S_ISREG(stp->st_mode))
651 switch (match_level) {
657 if (!unchanged_file(cmpbuf, file, stp))
663 if (!unchanged_attrs(file, stp))
665 if (always_checksum && preserve_times
666 && cmp_time(stp->st_mtime, file->modtime))
673 } while (basis_dir[++j] != NULL);
678 if (j != best_match) {
680 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
681 if (link_stat(cmpbuf, stp, 0) < 0)
685 if (match_level == 3 && !copy_dest) {
686 #ifdef SUPPORT_HARD_LINKS
688 int i = itemizing && (verbose > 1 || stdout_format_has_i > 1);
689 if (hard_link_one(file, ndx, fname, 0, stp,
690 cmpbuf, 1, i, code) < 0)
692 if (preserve_hard_links && file->link_u.links) {
694 file->link_u.links->link_dest_used = j + 1;
695 hard_link_cluster(file, ndx, itemizing, code);
700 itemize(file, ndx, 0, stp, 0, 0, NULL);
701 if (verbose > 1 && maybe_ATTRS_REPORT) {
702 rprintf(FCLIENT, "%s is uptodate\n", fname);
707 if (match_level >= 2) {
708 try_a_copy: /* Copy the file locally. */
709 if (copy_file(cmpbuf, fname, file->mode) < 0) {
711 rsyserr(FINFO, errno, "copy_file %s => %s",
712 full_fname(cmpbuf), fname);
717 itemize(file, ndx, 0, stp, ITEM_LOCAL_CHANGE, 0, NULL);
718 set_file_attrs(fname, file, NULL, 0);
719 if (maybe_ATTRS_REPORT
720 && ((!itemizing && verbose && match_level == 2)
721 || (verbose > 1 && match_level == 3))) {
722 code = match_level == 3 ? FCLIENT : FINFO;
723 rprintf(code, "%s%s\n", fname,
724 match_level == 3 ? " is uptodate" : "");
726 if (preserve_hard_links && file->link_u.links)
727 hard_link_cluster(file, ndx, itemizing, code);
731 return FNAMECMP_BASIS_DIR_LOW + j;
734 /* This is only called for non-regular files. We return -2 if we've finished
735 * handling the file, or -1 if no dest-linking occurred, or a non-negative
736 * value if we found an alternate basis file. */
737 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
738 char *cmpbuf, STRUCT_STAT *stp, int itemizing,
739 int maybe_ATTRS_REPORT, enum logcode code)
741 char lnk[MAXPATHLEN];
744 enum nonregtype type;
747 #ifndef SUPPORT_LINKS
748 if (S_ISLNK(file->mode))
751 if (S_ISDIR(file->mode)) {
753 } else if (IS_SPECIAL(file->mode))
755 else if (IS_DEVICE(file->mode))
758 else if (S_ISLNK(file->mode))
763 "internal: try_dests_non() called with invalid mode (%o)\n",
765 exit_cleanup(RERR_UNSUPPORTED);
769 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
770 if (link_stat(cmpbuf, stp, 0) < 0)
774 if (!S_ISDIR(stp->st_mode))
778 if (!IS_SPECIAL(stp->st_mode))
782 if (!IS_DEVICE(stp->st_mode))
787 if (!S_ISLNK(stp->st_mode))
792 if (match_level < 1) {
801 if (stp->st_rdev != file->u.rdev)
806 if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
809 if (strcmp(lnk, file->u.link) != 0)
814 if (match_level < 2) {
818 if (unchanged_attrs(file, stp)) {
823 } while (basis_dir[++j] != NULL);
828 if (j != best_match) {
830 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
831 if (link_stat(cmpbuf, stp, 0) < 0)
835 if (match_level == 3) {
836 #ifdef SUPPORT_HARD_LINKS
838 #ifndef CAN_HARDLINK_SYMLINK
839 && !S_ISLNK(file->mode)
841 #ifndef CAN_HARDLINK_SPECIAL
842 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
844 && !S_ISDIR(file->mode)) {
845 if (do_link(cmpbuf, fname) < 0) {
846 rsyserr(FERROR, errno,
847 "failed to hard-link %s with %s",
851 if (preserve_hard_links && file->link_u.links)
852 hard_link_cluster(file, ndx, itemizing, code);
856 if (itemizing && stdout_format_has_i
857 && (verbose > 1 || stdout_format_has_i > 1)) {
858 int chg = compare_dest && type != TYPE_DIR ? 0
860 + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
861 char *lp = match_level == 3 ? "" : NULL;
862 itemize(file, ndx, 0, stp, chg + ITEM_MATCHED, 0, lp);
864 if (verbose > 1 && maybe_ATTRS_REPORT) {
865 rprintf(FCLIENT, "%s%s is uptodate\n",
866 fname, type == TYPE_DIR ? "/" : "");
874 static int phase = 0;
876 /* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir,
877 * make sure it exists, and has the right permissions/timestamp info. For
878 * all other non-regular files (symlinks, etc.) we create them here. For
879 * regular files that have changed, we try to find a basis file and then
880 * start sending checksums.
882 * When fname is non-null, it must point to a MAXPATHLEN buffer!
884 * Note that f_out is set to -1 when doing final directory-permission and
885 * modification-time repair. */
886 static void recv_generator(char *fname, struct file_struct *file, int ndx,
887 int itemizing, int maybe_ATTRS_REPORT,
888 enum logcode code, int f_out)
890 static int missing_below = -1, excluded_below = -1;
891 static char *parent_dirname = "";
892 static struct file_list *fuzzy_dirlist = NULL;
893 static int need_fuzzy_dirlist = 0;
894 struct file_struct *fuzzy_file = NULL;
895 int fd = -1, f_copy = -1;
896 STRUCT_STAT st, real_st, partial_st;
897 struct file_struct *back_file = NULL;
898 int statret, real_ret, stat_errno;
899 char *fnamecmp, *partialptr, *backupptr = NULL;
900 char fnamecmpbuf[MAXPATHLEN];
902 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
909 flist_free(fuzzy_dirlist);
910 fuzzy_dirlist = NULL;
912 if (missing_below >= 0) {
922 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
924 if (server_filter_list.head) {
925 if (excluded_below >= 0) {
926 if (file->dir.depth > excluded_below)
930 if (check_filter(&server_filter_list, fname,
931 S_ISDIR(file->mode)) < 0) {
932 if (S_ISDIR(file->mode))
933 excluded_below = file->dir.depth;
937 "skipping server-excluded file \"%s\"\n",
944 if (missing_below >= 0) {
945 if (file->dir.depth <= missing_below) {
956 char *dn = file->dirname ? file->dirname : ".";
957 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
958 if (relative_paths && !implied_dirs
959 && do_stat(dn, &st) < 0
960 && create_directory_path(fname) < 0) {
961 rsyserr(FERROR, errno,
962 "recv_generator: mkdir %s failed",
966 flist_free(fuzzy_dirlist);
967 fuzzy_dirlist = NULL;
970 need_fuzzy_dirlist = 1;
974 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
975 fuzzy_dirlist = get_dirlist(dn, -1, 1);
976 need_fuzzy_dirlist = 0;
979 statret = link_stat(fname, &st,
980 keep_dirlinks && S_ISDIR(file->mode));
984 if (ignore_non_existing && statret == -1 && stat_errno == ENOENT) {
986 rprintf(FINFO, "not creating new %s \"%s\"\n",
987 S_ISDIR(file->mode) ? "directory" : "file",
993 /* If we're not preserving permissions, change the file-list's
994 * mode based on the local permissions and some heuristics. */
995 if (!preserve_perms) {
996 int exists = statret == 0
997 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode);
998 file->mode = dest_mode(file->mode, st.st_mode, exists);
1001 if (S_ISDIR(file->mode)) {
1002 /* The file to be received is a directory, so we need
1003 * to prepare appropriately. If there is already a
1004 * file of that name and it is *not* a directory, then
1005 * we need to delete it. If it doesn't exist, then
1006 * (perhaps recursively) create it. */
1007 if (statret == 0 && !S_ISDIR(st.st_mode)) {
1008 if (delete_item(fname, st.st_mode, "directory", del_opts) != 0)
1012 if (dry_run && statret != 0 && missing_below < 0) {
1013 missing_below = file->dir.depth;
1019 if (*fname == '.' && fname[1] == '\0')
1023 if (statret != 0 && basis_dir[0] != NULL) {
1024 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1025 itemizing, maybe_ATTRS_REPORT, code);
1032 if (itemizing && f_out != -1) {
1033 itemize(file, ndx, statret, &st,
1034 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1036 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1037 if (!relative_paths || errno != ENOENT
1038 || create_directory_path(fname) < 0
1039 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
1040 rsyserr(FERROR, errno,
1041 "recv_generator: mkdir %s failed",
1043 file->flags |= FLAG_MISSING;
1044 if (ndx+1 < the_file_list->count
1045 && the_file_list->files[ndx+1]->dir.depth > file->dir.depth) {
1047 "*** Skipping everything below this failed directory ***\n");
1048 missing_below = file->dir.depth;
1053 if (set_file_attrs(fname, file, real_ret ? NULL : &real_st, 0)
1054 && verbose && code != FNONE && f_out != -1)
1055 rprintf(code, "%s/\n", fname);
1056 if (real_ret != 0 && one_file_system)
1057 real_st.st_dev = filesystem_dev;
1058 if (delete_during && f_out != -1 && !phase && dry_run < 2
1059 && (file->flags & FLAG_DEL_HERE))
1060 delete_in_dir(the_file_list, fname, file, &real_st);
1064 if (preserve_hard_links && file->link_u.links
1065 && hard_link_check(file, ndx, fname, statret, &st,
1066 itemizing, code, HL_CHECK_MASTER))
1069 if (preserve_links && S_ISLNK(file->mode)) {
1070 #ifdef SUPPORT_LINKS
1071 if (safe_symlinks && unsafe_symlink(file->u.link, fname)) {
1073 if (the_file_list->count == 1)
1074 fname = f_name(file, NULL);
1076 "ignoring unsafe symlink %s -> \"%s\"\n",
1077 full_fname(fname), file->u.link);
1082 char lnk[MAXPATHLEN];
1085 if (!S_ISLNK(st.st_mode))
1087 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1088 && strncmp(lnk, file->u.link, len) == 0
1089 && file->u.link[len] == '\0') {
1090 /* The link is pointing to the right place. */
1092 itemize(file, ndx, 0, &st, 0, 0, NULL);
1093 set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1094 if (preserve_hard_links && file->link_u.links)
1095 hard_link_cluster(file, ndx, itemizing, code);
1096 if (remove_source_files == 1)
1097 goto return_with_success;
1100 /* Not the right symlink (or not a symlink), so
1102 if (delete_item(fname, st.st_mode, "symlink", del_opts) != 0)
1104 } else if (basis_dir[0] != NULL) {
1105 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1106 itemizing, maybe_ATTRS_REPORT, code);
1108 #ifndef CAN_HARDLINK_SYMLINK
1110 /* Resort to --copy-dest behavior. */
1120 if (preserve_hard_links && file->link_u.links
1121 && hard_link_check(file, ndx, fname, -1, &st,
1122 itemizing, code, HL_SKIP))
1124 if (do_symlink(file->u.link, fname) != 0) {
1125 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
1126 full_fname(fname), file->u.link);
1128 set_file_attrs(fname, file, NULL, 0);
1130 itemize(file, ndx, statret, &st,
1131 ITEM_LOCAL_CHANGE, 0, NULL);
1133 if (code != FNONE && verbose)
1134 rprintf(code, "%s -> %s\n", fname, file->u.link);
1135 if (preserve_hard_links && file->link_u.links)
1136 hard_link_cluster(file, ndx, itemizing, code);
1137 /* This does not check remove_source_files == 1
1138 * because this is one of the items that the old
1139 * --remove-sent-files option would remove. */
1140 if (remove_source_files)
1141 goto return_with_success;
1147 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1148 || (preserve_specials && IS_SPECIAL(file->mode))) {
1151 if (IS_DEVICE(file->mode)) {
1152 if (!IS_DEVICE(st.st_mode))
1156 if (!IS_SPECIAL(st.st_mode))
1161 && (st.st_mode & ~CHMOD_BITS) == (file->mode & ~CHMOD_BITS)
1162 && st.st_rdev == file->u.rdev) {
1163 /* The device or special file is identical. */
1165 itemize(file, ndx, 0, &st, 0, 0, NULL);
1166 set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1167 if (preserve_hard_links && file->link_u.links)
1168 hard_link_cluster(file, ndx, itemizing, code);
1169 if (remove_source_files == 1)
1170 goto return_with_success;
1173 if (delete_item(fname, st.st_mode, t, del_opts) != 0)
1175 } else if (basis_dir[0] != NULL) {
1176 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1177 itemizing, maybe_ATTRS_REPORT, code);
1179 #ifndef CAN_HARDLINK_SPECIAL
1181 /* Resort to --copy-dest behavior. */
1191 if (preserve_hard_links && file->link_u.links
1192 && hard_link_check(file, ndx, fname, -1, &st,
1193 itemizing, code, HL_SKIP))
1196 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
1197 fname, (int)file->mode, (int)file->u.rdev);
1199 if (do_mknod(fname, file->mode, file->u.rdev) < 0) {
1200 rsyserr(FERROR, errno, "mknod %s failed",
1203 set_file_attrs(fname, file, NULL, 0);
1205 itemize(file, ndx, statret, &st,
1206 ITEM_LOCAL_CHANGE, 0, NULL);
1208 if (code != FNONE && verbose)
1209 rprintf(code, "%s\n", fname);
1210 if (preserve_hard_links && file->link_u.links)
1211 hard_link_cluster(file, ndx, itemizing, code);
1212 if (remove_source_files == 1)
1213 goto return_with_success;
1218 if (!S_ISREG(file->mode)) {
1219 if (the_file_list->count == 1)
1220 fname = f_name(file, NULL);
1221 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1225 if (max_size && file->length > max_size) {
1227 if (the_file_list->count == 1)
1228 fname = f_name(file, NULL);
1229 rprintf(FINFO, "%s is over max-size\n", fname);
1233 if (min_size && file->length < min_size) {
1235 if (the_file_list->count == 1)
1236 fname = f_name(file, NULL);
1237 rprintf(FINFO, "%s is under min-size\n", fname);
1242 if (ignore_existing && statret == 0) {
1244 rprintf(FINFO, "%s exists\n", fname);
1248 if (update_only && statret == 0
1249 && cmp_time(st.st_mtime, file->modtime) > 0) {
1251 rprintf(FINFO, "%s is newer\n", fname);
1256 fnamecmp_type = FNAMECMP_FNAME;
1258 if (statret == 0 && !S_ISREG(st.st_mode)) {
1259 if (delete_item(fname, st.st_mode, "regular file", del_opts) != 0)
1262 stat_errno = ENOENT;
1265 if (statret != 0 && basis_dir[0] != NULL) {
1266 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &st,
1267 itemizing, maybe_ATTRS_REPORT, code);
1269 if (remove_source_files == 1)
1270 goto return_with_success;
1274 fnamecmp = fnamecmpbuf;
1283 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1284 && link_stat(partialptr, &partial_st, 0) == 0
1285 && S_ISREG(partial_st.st_mode)) {
1287 goto prepare_to_open;
1291 if (statret != 0 && fuzzy_dirlist && dry_run <= 1) {
1292 int j = find_fuzzy(file, fuzzy_dirlist);
1294 fuzzy_file = fuzzy_dirlist->files[j];
1295 f_name(fuzzy_file, fnamecmpbuf);
1297 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1298 fname, fnamecmpbuf);
1300 st.st_size = fuzzy_file->length;
1302 fnamecmp = fnamecmpbuf;
1303 fnamecmp_type = FNAMECMP_FUZZY;
1308 if (preserve_hard_links && file->link_u.links
1309 && hard_link_check(file, ndx, fname, statret, &st,
1310 itemizing, code, HL_SKIP))
1312 if (stat_errno == ENOENT)
1314 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
1319 if (append_mode && st.st_size > file->length)
1322 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1324 else if (fnamecmp_type == FNAMECMP_FUZZY)
1326 else if (unchanged_file(fnamecmp, file, &st)) {
1328 do_unlink(partialptr);
1329 handle_partial_dir(partialptr, PDIR_DELETE);
1332 itemize(file, ndx, real_ret, &real_st,
1335 set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1336 if (preserve_hard_links && file->link_u.links)
1337 hard_link_cluster(file, ndx, itemizing, code);
1338 if (remove_source_files != 1)
1340 return_with_success:
1343 SIVAL(numbuf, 0, ndx);
1344 send_msg(MSG_SUCCESS, numbuf, 4);
1352 fnamecmp = partialptr;
1353 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1357 if (!do_xfers || read_batch || whole_file)
1360 if (fuzzy_dirlist) {
1361 int j = flist_find(fuzzy_dirlist, file);
1362 if (j >= 0) /* don't use changing file as future fuzzy basis */
1363 fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY;
1367 fd = do_open(fnamecmp, O_RDONLY, 0);
1370 rsyserr(FERROR, errno, "failed to open %s, continuing",
1371 full_fname(fnamecmp));
1373 /* pretend the file didn't exist */
1374 if (preserve_hard_links && file->link_u.links
1375 && hard_link_check(file, ndx, fname, statret, &st,
1376 itemizing, code, HL_SKIP))
1378 statret = real_ret = -1;
1382 if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
1383 if (!(backupptr = get_backup_name(fname))) {
1387 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1389 goto pretend_missing;
1391 if (robust_unlink(backupptr) && errno != ENOENT) {
1392 rsyserr(FERROR, errno, "unlink %s",
1393 full_fname(backupptr));
1398 if ((f_copy = do_open(backupptr,
1399 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1400 rsyserr(FERROR, errno, "open %s",
1401 full_fname(backupptr));
1406 fnamecmp_type = FNAMECMP_BACKUP;
1410 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1411 fnamecmp, (double)st.st_size);
1415 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1418 if (remove_source_files && !delay_updates && !phase)
1419 increment_active_files(ndx, itemizing, code);
1420 write_int(f_out, ndx);
1422 int iflags = ITEM_TRANSFER;
1423 if (always_checksum)
1424 iflags |= ITEM_REPORT_CHECKSUM;
1425 if (fnamecmp_type != FNAMECMP_FNAME)
1426 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1427 if (fnamecmp_type == FNAMECMP_FUZZY)
1428 iflags |= ITEM_XNAME_FOLLOWS;
1429 itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type,
1430 fuzzy_file ? fuzzy_file->basename : NULL);
1434 if (preserve_hard_links && file->link_u.links)
1435 hard_link_cluster(file, ndx, itemizing, code);
1441 if (statret != 0 || whole_file) {
1442 write_sum_head(f_out, NULL);
1446 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
1450 set_file_attrs(backupptr, back_file, NULL, 0);
1452 rprintf(FINFO, "backed up %s to %s\n",
1461 void generate_files(int f_out, struct file_list *flist, char *local_name)
1464 char fbuf[MAXPATHLEN];
1465 int itemizing, maybe_ATTRS_REPORT;
1467 int lull_mod = allowed_lull * 5;
1468 int need_retouch_dir_times = preserve_times && !omit_dir_times;
1469 int need_retouch_dir_perms = 0;
1470 int save_ignore_existing = ignore_existing;
1471 int save_ignore_non_existing = ignore_non_existing;
1472 int save_do_progress = do_progress;
1473 int save_make_backups = make_backups;
1474 int dir_tweaking = !(list_only || local_name || dry_run);
1476 if (protocol_version >= 29) {
1478 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1479 code = logfile_format_has_i ? FNONE : FLOG;
1480 } else if (am_daemon) {
1481 itemizing = logfile_format_has_i && do_xfers;
1482 maybe_ATTRS_REPORT = ATTRS_REPORT;
1483 code = itemizing || !do_xfers ? FCLIENT : FINFO;
1484 } else if (!am_server) {
1485 itemizing = stdout_format_has_i;
1486 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1487 code = itemizing ? FNONE : FINFO;
1490 maybe_ATTRS_REPORT = ATTRS_REPORT;
1495 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
1496 (long)getpid(), flist->count);
1499 if (delete_before && !local_name && flist->count > 0)
1500 do_delete_pass(flist);
1503 if (append_mode || whole_file < 0)
1506 rprintf(FINFO, "delta-transmission %s\n",
1508 ? "disabled for local transfer or --whole-file"
1512 /* Since we often fill up the outgoing socket and then just sit around
1513 * waiting for the other 2 processes to do their thing, we don't want
1514 * to exit on a timeout. If the data stops flowing, the receiver will
1515 * notice that and let us know via the redo pipe (or its closing). */
1518 for (i = 0; i < flist->count; i++) {
1519 struct file_struct *file = flist->files[i];
1521 if (!file->basename)
1525 strlcpy(fbuf, local_name, sizeof fbuf);
1528 recv_generator(fbuf, file, i, itemizing, maybe_ATTRS_REPORT,
1531 /* We need to ensure that any dirs we create have writeable
1532 * permissions during the time we are putting files within
1533 * them. This is then fixed after the transfer is done. */
1535 if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)
1537 mode_t mode = file->mode | S_IWUSR; /* user write */
1538 char *fname = local_name ? local_name : fbuf;
1539 if (do_chmod(fname, mode) < 0) {
1540 rsyserr(FERROR, errno,
1541 "failed to modify permissions on %s",
1544 need_retouch_dir_perms = 1;
1548 if (preserve_hard_links)
1549 check_for_finished_hlinks(itemizing, code);
1551 if (allowed_lull && !(i % lull_mod))
1552 maybe_send_keepalive();
1553 else if (!(i % 200))
1554 maybe_flush_socket();
1556 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
1558 delete_in_dir(NULL, NULL, NULL, NULL);
1561 csum_length = SUM_LENGTH;
1562 max_size = min_size = ignore_existing = ignore_non_existing = 0;
1563 update_only = always_checksum = size_only = 0;
1565 if (append_mode) /* resend w/o append mode */
1566 append_mode = -1; /* ... but only longer files */
1567 make_backups = 0; /* avoid a duplicate backup for inplace processing */
1570 rprintf(FINFO,"generate_files phase=%d\n",phase);
1572 write_int(f_out, -1);
1574 /* files can cycle through the system more than once
1575 * to catch initial checksum errors */
1576 while ((i = get_redo_num(itemizing, code)) != -1) {
1577 struct file_struct *file = flist->files[i];
1579 strlcpy(fbuf, local_name, sizeof fbuf);
1582 recv_generator(fbuf, file, i, itemizing, maybe_ATTRS_REPORT,
1587 ignore_non_existing = save_ignore_non_existing;
1588 ignore_existing = save_ignore_existing;
1589 make_backups = save_make_backups;
1592 rprintf(FINFO,"generate_files phase=%d\n",phase);
1594 write_int(f_out, -1);
1595 /* Reduce round-trip lag-time for a useless delay-updates phase. */
1596 if (protocol_version >= 29 && !delay_updates)
1597 write_int(f_out, -1);
1599 /* Read MSG_DONE for the redo phase (and any prior messages). */
1600 get_redo_num(itemizing, code);
1602 if (protocol_version >= 29) {
1605 rprintf(FINFO, "generate_files phase=%d\n", phase);
1607 write_int(f_out, -1);
1608 /* Read MSG_DONE for delay-updates phase & prior messages. */
1609 get_redo_num(itemizing, code);
1612 do_progress = save_do_progress;
1613 if (delete_after && !local_name && flist->count > 0)
1614 do_delete_pass(flist);
1616 if ((need_retouch_dir_perms || need_retouch_dir_times) && dir_tweaking) {
1618 /* Now we need to fix any directory permissions that were
1619 * modified during the transfer and/or re-set any tweaked
1620 * modified-time values. */
1621 for (i = 0; i < flist->count; i++) {
1622 struct file_struct *file = flist->files[i];
1624 if (!file->basename || !S_ISDIR(file->mode))
1626 if (!need_retouch_dir_times && file->mode & S_IWUSR)
1628 if (file->flags & FLAG_MISSING) {
1629 int missing = file->dir.depth;
1630 while (++i < flist->count) {
1631 file = flist->files[i];
1632 if (file->dir.depth <= missing)
1638 recv_generator(f_name(file, NULL), file, i, itemizing,
1639 maybe_ATTRS_REPORT, code, -1);
1640 if (allowed_lull && !(++j % lull_mod))
1641 maybe_send_keepalive();
1642 else if (!(j % 200))
1643 maybe_flush_socket();
1646 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
1648 if (max_delete >= 0 && deletion_count > max_delete) {
1650 "Deletions stopped due to --max-delete limit (%d skipped)\n",
1651 deletion_count - max_delete);
1652 io_error |= IOERR_DEL_LIMIT;
1656 rprintf(FINFO,"generate_files finished\n");