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 /* The 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;
199 rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n",
203 dlen = strlen(fname);
204 save_filters = push_local_filters(fname, dlen);
206 dirlist = get_dirlist(fname, dlen, 0);
209 if (dlen != 1 || *fname != '/')
211 remainder = MAXPATHLEN - (p - fname);
213 /* We do our own recursion, so make delete_item() non-recursive. */
214 flags &= ~DEL_RECURSE;
217 for (j = dirlist->count; j--; ) {
218 struct file_struct *fp = dirlist->files[j];
220 if (fp->flags & FLAG_MOUNT_POINT) {
223 "mount point, %s, pins parent directory\n",
230 strlcpy(p, fp->basename, remainder);
231 if (S_ISDIR(fp->mode)) {
232 /* Save stack by recursing to ourself directly. */
233 result = delete_dir_contents(fname, flags);
234 if (result == DR_PINNED)
236 else if (result != DR_SUCCESS && ret == DR_SUCCESS)
239 result = delete_item(fname, fp->mode, NULL, flags);
240 if (result == DR_PINNED)
242 else if (result != DR_SUCCESS && ret == DR_SUCCESS)
248 pop_local_filters(save_filters);
255 /* This function is used to implement per-directory deletion, and is used by
256 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
257 * MAXPATHLEN buffer with the name of the directory in it (the functions we
258 * call will append names onto the end, but the old dir value will be restored
260 static void delete_in_dir(struct file_list *flist, char *fbuf,
261 struct file_struct *file, STRUCT_STAT *stp)
263 static int min_depth = MAXPATHLEN, cur_depth = -1;
264 static void *filt_array[MAXPATHLEN/2+1];
265 static int already_warned = 0;
266 struct file_list *dirlist;
267 char delbuf[MAXPATHLEN];
271 while (cur_depth >= min_depth)
272 pop_local_filters(filt_array[cur_depth--]);
273 min_depth = MAXPATHLEN;
279 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
282 maybe_send_keepalive();
284 if (file->dir.depth >= MAXPATHLEN/2+1)
285 return; /* Impossible... */
287 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
291 "IO error encountered -- skipping file deletion\n");
296 while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
297 pop_local_filters(filt_array[cur_depth--]);
298 cur_depth = file->dir.depth;
299 if (min_depth > cur_depth)
300 min_depth = cur_depth;
302 filt_array[cur_depth] = push_local_filters(fbuf, dlen);
304 if (one_file_system) {
305 if (file->flags & FLAG_TOP_DIR)
306 filesystem_dev = stp->st_dev;
307 else if (filesystem_dev != stp->st_dev)
311 dirlist = get_dirlist(fbuf, dlen, 0);
313 /* If an item in dirlist is not found in flist, delete it
314 * from the filesystem. */
315 for (i = dirlist->count; i--; ) {
316 struct file_struct *fp = dirlist->files[i];
319 if (fp->flags & FLAG_MOUNT_POINT) {
321 rprintf(FINFO, "mount point %s not deleted\n",
325 if (flist_find(flist, fp) < 0) {
327 delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
334 /* This deletes any files on the receiving side that are not present on the
335 * sending side. This is used by --delete-before and --delete-after. */
336 static void do_delete_pass(struct file_list *flist)
338 char fbuf[MAXPATHLEN];
342 /* dry_run is incremented when the destination doesn't exist yet. */
343 if (dry_run > 1 || list_only)
346 for (j = 0; j < flist->count; j++) {
347 struct file_struct *file = flist->files[j];
349 if (!(file->flags & FLAG_DEL_HERE))
353 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
354 rprintf(FINFO, "deleting in %s\n", fbuf);
356 if (link_stat(fbuf, &st, keep_dirlinks) < 0
357 || !S_ISDIR(st.st_mode))
360 delete_in_dir(flist, fbuf, file, &st);
362 delete_in_dir(NULL, NULL, NULL, NULL);
364 if (do_progress && !am_server)
365 rprintf(FINFO, " \r");
368 int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
371 && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
374 if (am_root && preserve_uid && st->st_uid != file->uid)
377 if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid)
383 void itemize(struct file_struct *file, int ndx, int statret, STRUCT_STAT *st,
384 int32 iflags, uchar fnamecmp_type, char *xname)
386 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
387 int keep_time = !preserve_times ? 0
388 : S_ISDIR(file->mode) ? !omit_dir_times
389 : !S_ISLNK(file->mode);
391 if (S_ISREG(file->mode) && file->length != st->st_size)
392 iflags |= ITEM_REPORT_SIZE;
393 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
394 && !(iflags & ITEM_MATCHED)
395 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
396 || (keep_time && cmp_time(file->modtime, st->st_mtime) != 0))
397 iflags |= ITEM_REPORT_TIME;
398 if ((file->mode & CHMOD_BITS) != (st->st_mode & CHMOD_BITS))
399 iflags |= ITEM_REPORT_PERMS;
400 if (preserve_uid && am_root && file->uid != st->st_uid)
401 iflags |= ITEM_REPORT_OWNER;
402 if (preserve_gid && file->gid != GID_NONE
403 && st->st_gid != file->gid)
404 iflags |= ITEM_REPORT_GROUP;
406 iflags |= ITEM_IS_NEW;
409 if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
410 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
411 if (protocol_version >= 29) {
413 write_int(sock_f_out, ndx);
414 write_shortint(sock_f_out, iflags);
415 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
416 write_byte(sock_f_out, fnamecmp_type);
417 if (iflags & ITEM_XNAME_FOLLOWS)
418 write_vstring(sock_f_out, xname, strlen(xname));
419 } else if (ndx >= 0) {
420 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
421 log_item(code, file, &stats, iflags, xname);
427 /* Perform our quick-check heuristic for determining if a file is unchanged. */
428 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
430 if (st->st_size != file->length)
433 /* if always checksum is set then we use the checksum instead
434 of the file time to determine whether to sync */
435 if (always_checksum && S_ISREG(st->st_mode)) {
436 char sum[MD4_SUM_LENGTH];
437 file_checksum(fn, sum, st->st_size);
438 return memcmp(sum, file->u.sum, checksum_len) == 0;
447 return cmp_time(st->st_mtime, file->modtime) == 0;
452 * set (initialize) the size entries in the per-file sum_struct
453 * calculating dynamic block and checksum sizes.
455 * This is only called from generate_and_send_sums() but is a separate
456 * function to encapsulate the logic.
458 * The block size is a rounded square root of file length.
460 * The checksum size is determined according to:
461 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
462 * provided by Donovan Baarda which gives a probability of rsync
463 * algorithm corrupting data and falling back using the whole md4
466 * This might be made one of several selectable heuristics.
468 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
474 blength = block_size;
475 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
476 blength = BLOCK_SIZE;
481 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
482 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
483 blength = MAX_BLOCK_SIZE;
488 if (len < (int64)blength * blength)
491 } while (c >= 8); /* round to multiple of 8 */
492 blength = MAX(blength, BLOCK_SIZE);
496 if (protocol_version < 27) {
497 s2length = csum_length;
498 } else if (csum_length == SUM_LENGTH) {
499 s2length = SUM_LENGTH;
503 int b = BLOCKSUM_BIAS;
504 for (l = len; l >>= 1; b += 2) {}
505 for (c = blength; (c >>= 1) && b; b--) {}
506 /* add a bit, subtract rollsum, round up. */
507 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
508 s2length = MAX(s2length, csum_length);
509 s2length = MIN(s2length, SUM_LENGTH);
513 sum->blength = blength;
514 sum->s2length = s2length;
515 sum->remainder = len % blength;
516 sum->count = len / blength + (sum->remainder != 0);
518 if (sum->count && verbose > 2) {
520 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
521 (double)sum->count, (long)sum->remainder, (long)sum->blength,
522 sum->s2length, (double)sum->flength);
528 * Generate and send a stream of signatures/checksums that describe a buffer
530 * Generate approximately one checksum every block_len bytes.
532 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
535 struct map_struct *mapbuf;
536 struct sum_struct sum;
539 sum_sizes_sqroot(&sum, len);
540 write_sum_head(f_out, &sum);
542 if (append_mode > 0 && f_copy < 0)
546 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
550 for (i = 0; i < sum.count; i++) {
551 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
552 char *map = map_ptr(mapbuf, offset, n1);
553 char sum2[SUM_LENGTH];
560 full_write(f_copy, map, n1);
565 sum1 = get_checksum1(map, n1);
566 get_checksum2(map, n1, sum2);
570 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
571 (double)i, (double)offset - n1, (long)n1,
572 (unsigned long)sum1);
574 write_int(f_out, sum1);
575 write_buf(f_out, sum2, sum.s2length);
583 /* Try to find a filename in the same dir as "fname" with a similar name. */
584 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
586 int fname_len, fname_suf_len;
587 const char *fname_suf, *fname = file->basename;
588 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
589 int j, lowest_j = -1;
591 fname_len = strlen(fname);
592 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
594 for (j = 0; j < dirlist->count; j++) {
595 struct file_struct *fp = dirlist->files[j];
596 const char *suf, *name;
600 if (!S_ISREG(fp->mode) || !fp->length
601 || fp->flags & FLAG_NO_FUZZY)
606 if (fp->length == file->length
607 && cmp_time(fp->modtime, file->modtime) == 0) {
610 "fuzzy size/modtime match for %s\n",
617 suf = find_filename_suffix(name, len, &suf_len);
619 dist = fuzzy_distance(name, len, fname, fname_len);
620 /* Add some extra weight to how well the suffixes match. */
621 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
624 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
625 name, (int)(dist>>16), (int)(dist&0xFFFF));
627 if (dist <= lowest_dist) {
636 void check_for_finished_hlinks(int itemizing, enum logcode code)
638 struct file_struct *file;
641 while ((ndx = get_hlink_num()) != -1) {
642 if (ndx < 0 || ndx >= the_file_list->count)
645 file = the_file_list->files[ndx];
646 if (!file->link_u.links)
649 hard_link_cluster(file, ndx, itemizing, code);
653 /* This is only called for regular files. We return -2 if we've finished
654 * handling the file, -1 if no dest-linking occurred, or a non-negative
655 * value if we found an alternate basis file. */
656 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
657 char *cmpbuf, STRUCT_STAT *stp, int itemizing,
658 int maybe_ATTRS_REPORT, enum logcode code)
665 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
666 if (link_stat(cmpbuf, stp, 0) < 0 || !S_ISREG(stp->st_mode))
668 switch (match_level) {
674 if (!unchanged_file(cmpbuf, file, stp))
680 if (!unchanged_attrs(file, stp))
682 if (always_checksum && preserve_times
683 && cmp_time(stp->st_mtime, file->modtime))
690 } while (basis_dir[++j] != NULL);
695 if (j != best_match) {
697 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
698 if (link_stat(cmpbuf, stp, 0) < 0)
702 if (match_level == 3 && !copy_dest) {
703 #ifdef SUPPORT_HARD_LINKS
705 int i = itemizing && (verbose > 1 || stdout_format_has_i > 1);
706 if (hard_link_one(file, ndx, fname, 0, stp,
707 cmpbuf, 1, i, code) < 0)
709 if (preserve_hard_links && file->link_u.links) {
711 file->link_u.links->link_dest_used = j + 1;
712 hard_link_cluster(file, ndx, itemizing, code);
717 itemize(file, ndx, 0, stp, 0, 0, NULL);
718 if (verbose > 1 && maybe_ATTRS_REPORT) {
719 rprintf(FCLIENT, "%s is uptodate\n", fname);
724 if (match_level >= 2) {
725 try_a_copy: /* Copy the file locally. */
726 if (copy_file(cmpbuf, fname, file->mode) < 0) {
728 rsyserr(FINFO, errno, "copy_file %s => %s",
729 full_fname(cmpbuf), fname);
734 itemize(file, ndx, 0, stp, ITEM_LOCAL_CHANGE, 0, NULL);
735 set_file_attrs(fname, file, NULL, 0);
736 if (maybe_ATTRS_REPORT
737 && ((!itemizing && verbose && match_level == 2)
738 || (verbose > 1 && match_level == 3))) {
739 code = match_level == 3 ? FCLIENT : FINFO;
740 rprintf(code, "%s%s\n", fname,
741 match_level == 3 ? " is uptodate" : "");
743 if (preserve_hard_links && file->link_u.links)
744 hard_link_cluster(file, ndx, itemizing, code);
748 return FNAMECMP_BASIS_DIR_LOW + j;
751 /* This is only called for non-regular files. We return -2 if we've finished
752 * handling the file, or -1 if no dest-linking occurred, or a non-negative
753 * value if we found an alternate basis file. */
754 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
755 char *cmpbuf, STRUCT_STAT *stp, int itemizing,
756 int maybe_ATTRS_REPORT, enum logcode code)
758 char lnk[MAXPATHLEN];
761 enum nonregtype type;
764 #ifndef SUPPORT_LINKS
765 if (S_ISLNK(file->mode))
768 if (S_ISDIR(file->mode)) {
770 } else if (IS_SPECIAL(file->mode))
772 else if (IS_DEVICE(file->mode))
775 else if (S_ISLNK(file->mode))
780 "internal: try_dests_non() called with invalid mode (%o)\n",
782 exit_cleanup(RERR_UNSUPPORTED);
786 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
787 if (link_stat(cmpbuf, stp, 0) < 0)
791 if (!S_ISDIR(stp->st_mode))
795 if (!IS_SPECIAL(stp->st_mode))
799 if (!IS_DEVICE(stp->st_mode))
804 if (!S_ISLNK(stp->st_mode))
809 if (match_level < 1) {
818 if (stp->st_rdev != file->u.rdev)
823 if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
826 if (strcmp(lnk, file->u.link) != 0)
831 if (match_level < 2) {
835 if (unchanged_attrs(file, stp)) {
840 } while (basis_dir[++j] != NULL);
845 if (j != best_match) {
847 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
848 if (link_stat(cmpbuf, stp, 0) < 0)
852 if (match_level == 3) {
853 #ifdef SUPPORT_HARD_LINKS
855 #ifndef CAN_HARDLINK_SYMLINK
856 && !S_ISLNK(file->mode)
858 #ifndef CAN_HARDLINK_SPECIAL
859 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
861 && !S_ISDIR(file->mode)) {
862 if (do_link(cmpbuf, fname) < 0) {
863 rsyserr(FERROR, errno,
864 "failed to hard-link %s with %s",
868 if (preserve_hard_links && file->link_u.links)
869 hard_link_cluster(file, ndx, itemizing, code);
873 if (itemizing && stdout_format_has_i
874 && (verbose > 1 || stdout_format_has_i > 1)) {
875 int chg = compare_dest && type != TYPE_DIR ? 0
877 + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
878 char *lp = match_level == 3 ? "" : NULL;
879 itemize(file, ndx, 0, stp, chg + ITEM_MATCHED, 0, lp);
881 if (verbose > 1 && maybe_ATTRS_REPORT) {
882 rprintf(FCLIENT, "%s%s is uptodate\n",
883 fname, type == TYPE_DIR ? "/" : "");
891 static int phase = 0;
893 /* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir,
894 * make sure it exists, and has the right permissions/timestamp info. For
895 * all other non-regular files (symlinks, etc.) we create them here. For
896 * regular files that have changed, we try to find a basis file and then
897 * start sending checksums.
899 * When fname is non-null, it must point to a MAXPATHLEN buffer!
901 * Note that f_out is set to -1 when doing final directory-permission and
902 * modification-time repair. */
903 static void recv_generator(char *fname, struct file_struct *file, int ndx,
904 int itemizing, int maybe_ATTRS_REPORT,
905 enum logcode code, int f_out)
907 static int missing_below = -1, excluded_below = -1;
908 static char *parent_dirname = "";
909 static struct file_list *fuzzy_dirlist = NULL;
910 static int need_fuzzy_dirlist = 0;
911 struct file_struct *fuzzy_file = NULL;
912 int fd = -1, f_copy = -1;
913 STRUCT_STAT st, real_st, partial_st;
914 struct file_struct *back_file = NULL;
915 int statret, real_ret, stat_errno;
916 char *fnamecmp, *partialptr, *backupptr = NULL;
917 char fnamecmpbuf[MAXPATHLEN];
919 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
926 flist_free(fuzzy_dirlist);
927 fuzzy_dirlist = NULL;
929 if (missing_below >= 0) {
939 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
941 if (server_filter_list.head) {
942 if (excluded_below >= 0) {
943 if (file->dir.depth > excluded_below)
947 if (check_filter(&server_filter_list, fname,
948 S_ISDIR(file->mode)) < 0) {
949 if (S_ISDIR(file->mode))
950 excluded_below = file->dir.depth;
954 "skipping server-excluded file \"%s\"\n",
961 if (missing_below >= 0) {
962 if (file->dir.depth <= missing_below) {
973 char *dn = file->dirname ? file->dirname : ".";
974 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
975 if (relative_paths && !implied_dirs
976 && do_stat(dn, &st) < 0
977 && create_directory_path(fname) < 0) {
978 rsyserr(FERROR, errno,
979 "recv_generator: mkdir %s failed",
983 flist_free(fuzzy_dirlist);
984 fuzzy_dirlist = NULL;
987 need_fuzzy_dirlist = 1;
991 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
992 fuzzy_dirlist = get_dirlist(dn, -1, 1);
993 need_fuzzy_dirlist = 0;
996 statret = link_stat(fname, &st,
997 keep_dirlinks && S_ISDIR(file->mode));
1001 if (ignore_non_existing && statret == -1 && stat_errno == ENOENT) {
1003 rprintf(FINFO, "not creating new %s \"%s\"\n",
1004 S_ISDIR(file->mode) ? "directory" : "file",
1010 /* If we're not preserving permissions, change the file-list's
1011 * mode based on the local permissions and some heuristics. */
1012 if (!preserve_perms) {
1013 int exists = statret == 0
1014 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode);
1015 file->mode = dest_mode(file->mode, st.st_mode, exists);
1018 if (S_ISDIR(file->mode)) {
1019 /* The file to be received is a directory, so we need
1020 * to prepare appropriately. If there is already a
1021 * file of that name and it is *not* a directory, then
1022 * we need to delete it. If it doesn't exist, then
1023 * (perhaps recursively) create it. */
1024 if (statret == 0 && !S_ISDIR(st.st_mode)) {
1025 if (delete_item(fname, st.st_mode, "directory", del_opts) != 0)
1029 if (dry_run && statret != 0 && missing_below < 0) {
1030 missing_below = file->dir.depth;
1036 if (*fname == '.' && fname[1] == '\0')
1040 if (statret != 0 && basis_dir[0] != NULL) {
1041 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1042 itemizing, maybe_ATTRS_REPORT, code);
1049 if (itemizing && f_out != -1) {
1050 itemize(file, ndx, statret, &st,
1051 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1053 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1054 if (!relative_paths || errno != ENOENT
1055 || create_directory_path(fname) < 0
1056 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
1057 rsyserr(FERROR, errno,
1058 "recv_generator: mkdir %s failed",
1060 file->flags |= FLAG_MISSING;
1061 if (ndx+1 < the_file_list->count
1062 && the_file_list->files[ndx+1]->dir.depth > file->dir.depth) {
1064 "*** Skipping everything below this failed directory ***\n");
1065 missing_below = file->dir.depth;
1070 if (set_file_attrs(fname, file, real_ret ? NULL : &real_st, 0)
1071 && verbose && code != FNONE && f_out != -1)
1072 rprintf(code, "%s/\n", fname);
1073 if (real_ret != 0 && one_file_system)
1074 real_st.st_dev = filesystem_dev;
1075 if (delete_during && f_out != -1 && !phase && dry_run < 2
1076 && (file->flags & FLAG_DEL_HERE))
1077 delete_in_dir(the_file_list, fname, file, &real_st);
1081 if (preserve_hard_links && file->link_u.links
1082 && hard_link_check(file, ndx, fname, statret, &st,
1083 itemizing, code, HL_CHECK_MASTER))
1086 if (preserve_links && S_ISLNK(file->mode)) {
1087 #ifdef SUPPORT_LINKS
1088 if (safe_symlinks && unsafe_symlink(file->u.link, fname)) {
1090 if (the_file_list->count == 1)
1091 fname = f_name(file, NULL);
1093 "ignoring unsafe symlink %s -> \"%s\"\n",
1094 full_fname(fname), file->u.link);
1099 char lnk[MAXPATHLEN];
1102 if (!S_ISLNK(st.st_mode))
1104 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1105 && strncmp(lnk, file->u.link, len) == 0
1106 && file->u.link[len] == '\0') {
1107 /* The link is pointing to the right place. */
1109 itemize(file, ndx, 0, &st, 0, 0, NULL);
1110 set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1111 if (preserve_hard_links && file->link_u.links)
1112 hard_link_cluster(file, ndx, itemizing, code);
1113 if (remove_source_files == 1)
1114 goto return_with_success;
1117 /* Not the right symlink (or not a symlink), so
1119 if (delete_item(fname, st.st_mode, "symlink", del_opts) != 0)
1121 } else if (basis_dir[0] != NULL) {
1122 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1123 itemizing, maybe_ATTRS_REPORT, code);
1125 #ifndef CAN_HARDLINK_SYMLINK
1127 /* Resort to --copy-dest behavior. */
1137 if (preserve_hard_links && file->link_u.links
1138 && hard_link_check(file, ndx, fname, -1, &st,
1139 itemizing, code, HL_SKIP))
1141 if (do_symlink(file->u.link, fname) != 0) {
1142 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
1143 full_fname(fname), file->u.link);
1145 set_file_attrs(fname, file, NULL, 0);
1147 itemize(file, ndx, statret, &st,
1148 ITEM_LOCAL_CHANGE, 0, NULL);
1150 if (code != FNONE && verbose)
1151 rprintf(code, "%s -> %s\n", fname, file->u.link);
1152 if (preserve_hard_links && file->link_u.links)
1153 hard_link_cluster(file, ndx, itemizing, code);
1154 /* This does not check remove_source_files == 1
1155 * because this is one of the items that the old
1156 * --remove-sent-files option would remove. */
1157 if (remove_source_files)
1158 goto return_with_success;
1164 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1165 || (preserve_specials && IS_SPECIAL(file->mode))) {
1168 if (IS_DEVICE(file->mode)) {
1169 if (!IS_DEVICE(st.st_mode))
1173 if (!IS_SPECIAL(st.st_mode))
1178 && (st.st_mode & ~CHMOD_BITS) == (file->mode & ~CHMOD_BITS)
1179 && st.st_rdev == file->u.rdev) {
1180 /* The device or special file is identical. */
1182 itemize(file, ndx, 0, &st, 0, 0, NULL);
1183 set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1184 if (preserve_hard_links && file->link_u.links)
1185 hard_link_cluster(file, ndx, itemizing, code);
1186 if (remove_source_files == 1)
1187 goto return_with_success;
1190 if (delete_item(fname, st.st_mode, t, del_opts) != 0)
1192 } else if (basis_dir[0] != NULL) {
1193 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1194 itemizing, maybe_ATTRS_REPORT, code);
1196 #ifndef CAN_HARDLINK_SPECIAL
1198 /* Resort to --copy-dest behavior. */
1208 if (preserve_hard_links && file->link_u.links
1209 && hard_link_check(file, ndx, fname, -1, &st,
1210 itemizing, code, HL_SKIP))
1213 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
1214 fname, (int)file->mode, (int)file->u.rdev);
1216 if (do_mknod(fname, file->mode, file->u.rdev) < 0) {
1217 rsyserr(FERROR, errno, "mknod %s failed",
1220 set_file_attrs(fname, file, NULL, 0);
1222 itemize(file, ndx, statret, &st,
1223 ITEM_LOCAL_CHANGE, 0, NULL);
1225 if (code != FNONE && verbose)
1226 rprintf(code, "%s\n", fname);
1227 if (preserve_hard_links && file->link_u.links)
1228 hard_link_cluster(file, ndx, itemizing, code);
1229 if (remove_source_files == 1)
1230 goto return_with_success;
1235 if (!S_ISREG(file->mode)) {
1236 if (the_file_list->count == 1)
1237 fname = f_name(file, NULL);
1238 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1242 if (max_size && file->length > max_size) {
1244 if (the_file_list->count == 1)
1245 fname = f_name(file, NULL);
1246 rprintf(FINFO, "%s is over max-size\n", fname);
1250 if (min_size && file->length < min_size) {
1252 if (the_file_list->count == 1)
1253 fname = f_name(file, NULL);
1254 rprintf(FINFO, "%s is under min-size\n", fname);
1259 if (ignore_existing && statret == 0) {
1261 rprintf(FINFO, "%s exists\n", fname);
1265 if (update_only && statret == 0
1266 && cmp_time(st.st_mtime, file->modtime) > 0) {
1268 rprintf(FINFO, "%s is newer\n", fname);
1273 fnamecmp_type = FNAMECMP_FNAME;
1275 if (statret == 0 && !S_ISREG(st.st_mode)) {
1276 if (delete_item(fname, st.st_mode, "regular file", del_opts) != 0)
1279 stat_errno = ENOENT;
1282 if (statret != 0 && basis_dir[0] != NULL) {
1283 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &st,
1284 itemizing, maybe_ATTRS_REPORT, code);
1286 if (remove_source_files == 1)
1287 goto return_with_success;
1291 fnamecmp = fnamecmpbuf;
1300 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1301 && link_stat(partialptr, &partial_st, 0) == 0
1302 && S_ISREG(partial_st.st_mode)) {
1304 goto prepare_to_open;
1308 if (statret != 0 && fuzzy_dirlist && dry_run <= 1) {
1309 int j = find_fuzzy(file, fuzzy_dirlist);
1311 fuzzy_file = fuzzy_dirlist->files[j];
1312 f_name(fuzzy_file, fnamecmpbuf);
1314 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1315 fname, fnamecmpbuf);
1317 st.st_size = fuzzy_file->length;
1319 fnamecmp = fnamecmpbuf;
1320 fnamecmp_type = FNAMECMP_FUZZY;
1325 if (preserve_hard_links && file->link_u.links
1326 && hard_link_check(file, ndx, fname, statret, &st,
1327 itemizing, code, HL_SKIP))
1329 if (stat_errno == ENOENT)
1331 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
1336 if (append_mode && st.st_size > file->length)
1339 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1341 else if (fnamecmp_type == FNAMECMP_FUZZY)
1343 else if (unchanged_file(fnamecmp, file, &st)) {
1345 do_unlink(partialptr);
1346 handle_partial_dir(partialptr, PDIR_DELETE);
1349 itemize(file, ndx, real_ret, &real_st,
1352 set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1353 if (preserve_hard_links && file->link_u.links)
1354 hard_link_cluster(file, ndx, itemizing, code);
1355 if (remove_source_files != 1)
1357 return_with_success:
1360 SIVAL(numbuf, 0, ndx);
1361 send_msg(MSG_SUCCESS, numbuf, 4);
1369 fnamecmp = partialptr;
1370 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1374 if (!do_xfers || read_batch || whole_file)
1377 if (fuzzy_dirlist) {
1378 int j = flist_find(fuzzy_dirlist, file);
1379 if (j >= 0) /* don't use changing file as future fuzzy basis */
1380 fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY;
1384 fd = do_open(fnamecmp, O_RDONLY, 0);
1387 rsyserr(FERROR, errno, "failed to open %s, continuing",
1388 full_fname(fnamecmp));
1390 /* pretend the file didn't exist */
1391 if (preserve_hard_links && file->link_u.links
1392 && hard_link_check(file, ndx, fname, statret, &st,
1393 itemizing, code, HL_SKIP))
1395 statret = real_ret = -1;
1399 if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
1400 if (!(backupptr = get_backup_name(fname))) {
1404 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1406 goto pretend_missing;
1408 if (robust_unlink(backupptr) && errno != ENOENT) {
1409 rsyserr(FERROR, errno, "unlink %s",
1410 full_fname(backupptr));
1415 if ((f_copy = do_open(backupptr,
1416 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1417 rsyserr(FERROR, errno, "open %s",
1418 full_fname(backupptr));
1423 fnamecmp_type = FNAMECMP_BACKUP;
1427 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1428 fnamecmp, (double)st.st_size);
1432 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1435 if (remove_source_files && !delay_updates && !phase)
1436 increment_active_files(ndx, itemizing, code);
1437 write_int(f_out, ndx);
1439 int iflags = ITEM_TRANSFER;
1440 if (always_checksum)
1441 iflags |= ITEM_REPORT_CHECKSUM;
1442 if (fnamecmp_type != FNAMECMP_FNAME)
1443 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1444 if (fnamecmp_type == FNAMECMP_FUZZY)
1445 iflags |= ITEM_XNAME_FOLLOWS;
1446 itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type,
1447 fuzzy_file ? fuzzy_file->basename : NULL);
1451 if (preserve_hard_links && file->link_u.links)
1452 hard_link_cluster(file, ndx, itemizing, code);
1458 if (statret != 0 || whole_file) {
1459 write_sum_head(f_out, NULL);
1463 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
1467 set_file_attrs(backupptr, back_file, NULL, 0);
1469 rprintf(FINFO, "backed up %s to %s\n",
1478 void generate_files(int f_out, struct file_list *flist, char *local_name)
1481 char fbuf[MAXPATHLEN];
1482 int itemizing, maybe_ATTRS_REPORT;
1484 int lull_mod = allowed_lull * 5;
1485 int need_retouch_dir_times = preserve_times && !omit_dir_times;
1486 int need_retouch_dir_perms = 0;
1487 int save_ignore_existing = ignore_existing;
1488 int save_ignore_non_existing = ignore_non_existing;
1489 int save_do_progress = do_progress;
1490 int save_make_backups = make_backups;
1491 int dir_tweaking = !(list_only || local_name || dry_run);
1493 if (protocol_version >= 29) {
1495 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1496 code = logfile_format_has_i ? FNONE : FLOG;
1497 } else if (am_daemon) {
1498 itemizing = logfile_format_has_i && do_xfers;
1499 maybe_ATTRS_REPORT = ATTRS_REPORT;
1500 code = itemizing || !do_xfers ? FCLIENT : FINFO;
1501 } else if (!am_server) {
1502 itemizing = stdout_format_has_i;
1503 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1504 code = itemizing ? FNONE : FINFO;
1507 maybe_ATTRS_REPORT = ATTRS_REPORT;
1512 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
1513 (long)getpid(), flist->count);
1516 if (delete_before && !local_name && flist->count > 0)
1517 do_delete_pass(flist);
1520 if (append_mode || whole_file < 0)
1523 rprintf(FINFO, "delta-transmission %s\n",
1525 ? "disabled for local transfer or --whole-file"
1529 /* Since we often fill up the outgoing socket and then just sit around
1530 * waiting for the other 2 processes to do their thing, we don't want
1531 * to exit on a timeout. If the data stops flowing, the receiver will
1532 * notice that and let us know via the redo pipe (or its closing). */
1535 for (i = 0; i < flist->count; i++) {
1536 struct file_struct *file = flist->files[i];
1538 if (!file->basename)
1542 strlcpy(fbuf, local_name, sizeof fbuf);
1545 recv_generator(fbuf, file, i, itemizing, maybe_ATTRS_REPORT,
1548 /* We need to ensure that any dirs we create have writeable
1549 * permissions during the time we are putting files within
1550 * them. This is then fixed after the transfer is done. */
1552 if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)
1554 mode_t mode = file->mode | S_IWUSR; /* user write */
1555 char *fname = local_name ? local_name : fbuf;
1556 if (do_chmod(fname, mode) < 0) {
1557 rsyserr(FERROR, errno,
1558 "failed to modify permissions on %s",
1561 need_retouch_dir_perms = 1;
1565 if (preserve_hard_links)
1566 check_for_finished_hlinks(itemizing, code);
1568 if (allowed_lull && !(i % lull_mod))
1569 maybe_send_keepalive();
1570 else if (!(i % 200))
1571 maybe_flush_socket();
1573 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
1575 delete_in_dir(NULL, NULL, NULL, NULL);
1578 csum_length = SUM_LENGTH;
1579 max_size = min_size = ignore_existing = ignore_non_existing = 0;
1580 update_only = always_checksum = size_only = 0;
1582 if (append_mode) /* resend w/o append mode */
1583 append_mode = -1; /* ... but only longer files */
1584 make_backups = 0; /* avoid a duplicate backup for inplace processing */
1587 rprintf(FINFO,"generate_files phase=%d\n",phase);
1589 write_int(f_out, -1);
1591 /* files can cycle through the system more than once
1592 * to catch initial checksum errors */
1593 while ((i = get_redo_num(itemizing, code)) != -1) {
1594 struct file_struct *file = flist->files[i];
1596 strlcpy(fbuf, local_name, sizeof fbuf);
1599 recv_generator(fbuf, file, i, itemizing, maybe_ATTRS_REPORT,
1604 ignore_non_existing = save_ignore_non_existing;
1605 ignore_existing = save_ignore_existing;
1606 make_backups = save_make_backups;
1609 rprintf(FINFO,"generate_files phase=%d\n",phase);
1611 write_int(f_out, -1);
1612 /* Reduce round-trip lag-time for a useless delay-updates phase. */
1613 if (protocol_version >= 29 && !delay_updates)
1614 write_int(f_out, -1);
1616 /* Read MSG_DONE for the redo phase (and any prior messages). */
1617 get_redo_num(itemizing, code);
1619 if (protocol_version >= 29) {
1622 rprintf(FINFO, "generate_files phase=%d\n", phase);
1624 write_int(f_out, -1);
1625 /* Read MSG_DONE for delay-updates phase & prior messages. */
1626 get_redo_num(itemizing, code);
1629 do_progress = save_do_progress;
1630 if (delete_after && !local_name && flist->count > 0)
1631 do_delete_pass(flist);
1633 if ((need_retouch_dir_perms || need_retouch_dir_times) && dir_tweaking) {
1635 /* Now we need to fix any directory permissions that were
1636 * modified during the transfer and/or re-set any tweaked
1637 * modified-time values. */
1638 for (i = 0; i < flist->count; i++) {
1639 struct file_struct *file = flist->files[i];
1641 if (!file->basename || !S_ISDIR(file->mode))
1643 if (!need_retouch_dir_times && file->mode & S_IWUSR)
1645 if (file->flags & FLAG_MISSING) {
1646 int missing = file->dir.depth;
1647 while (++i < flist->count) {
1648 file = flist->files[i];
1649 if (file->dir.depth <= missing)
1655 recv_generator(f_name(file, NULL), file, i, itemizing,
1656 maybe_ATTRS_REPORT, code, -1);
1657 if (allowed_lull && !(++j % lull_mod))
1658 maybe_send_keepalive();
1659 else if (!(j % 200))
1660 maybe_flush_socket();
1663 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
1665 if (max_delete >= 0 && deletion_count > max_delete) {
1667 "Deletions stopped due to --max-delete limit (%d skipped)\n",
1668 deletion_count - max_delete);
1669 io_error |= IOERR_DEL_LIMIT;
1673 rprintf(FINFO,"generate_files finished\n");