1 /* -*- c-file-style: "linux" -*-
3 rsync -- fast file replication program
5 Copyright (C) 1996-2000 by Andrew Tridgell
6 Copyright (C) Paul Mackerras 1996
7 Copyright (C) 2002 by Martin Pool <mbp@samba.org>
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
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 extern int log_format_has_i;
30 extern int log_format_has_o_or_i;
31 extern int daemon_log_format_has_i;
35 extern int do_progress;
37 extern int relative_paths;
38 extern int keep_dirlinks;
39 extern int preserve_links;
40 extern int preserve_devices;
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_before;
48 extern int delete_during;
49 extern int delete_after;
51 extern int ignore_errors;
52 extern int remove_sent_files;
53 extern int delay_updates;
54 extern int update_only;
55 extern int opt_ignore_existing;
57 extern int append_mode;
58 extern int make_backups;
59 extern int csum_length;
60 extern int ignore_times;
62 extern OFF_T max_size;
64 extern int allowed_lull;
65 extern int sock_f_out;
66 extern int ignore_timeout;
67 extern int protocol_version;
68 extern int fuzzy_basis;
69 extern int always_checksum;
70 extern char *partial_dir;
71 extern char *basis_dir[];
72 extern int compare_dest;
75 extern int whole_file;
77 extern int read_batch;
78 extern int only_existing;
79 extern int orig_umask;
80 extern int safe_symlinks;
81 extern long block_size; /* "long" because popt can't set an int32. */
82 extern int max_delete;
83 extern int force_delete;
84 extern int one_file_system;
85 extern struct stats stats;
86 extern dev_t filesystem_dev;
87 extern char *backup_dir;
88 extern char *backup_suffix;
89 extern int backup_suffix_len;
90 extern struct file_list *the_file_list;
91 extern struct filter_list_struct server_filter_list;
93 static int deletion_count = 0; /* used to implement --max-delete */
96 static int is_backup_file(char *fn)
98 int k = strlen(fn) - backup_suffix_len;
99 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
103 /* Delete a file or directory. If DEL_FORCE_RECURSE is set in the flags, or if
104 * force_delete is set, this will delete recursively as long as DEL_NO_RECURSE
105 * is not set in the flags. */
106 static int delete_item(char *fname, int mode, int flags)
108 struct file_list *dirlist;
109 char buf[MAXPATHLEN];
110 int j, dlen, zap_dir, ok;
113 if (!S_ISDIR(mode)) {
114 if (max_delete && ++deletion_count > max_delete)
116 if (make_backups && (backup_dir || !is_backup_file(fname)))
117 ok = make_backup(fname);
119 ok = robust_unlink(fname) == 0;
121 if (!(flags & DEL_TERSE))
122 log_delete(fname, mode);
125 if (errno == ENOENT) {
129 rsyserr(FERROR, errno, "delete_file: unlink %s failed",
134 zap_dir = (flags & DEL_FORCE_RECURSE || (force_delete && recurse))
135 && !(flags & DEL_NO_RECURSE);
136 if ((max_delete && ++deletion_count > max_delete)
137 || (dry_run && zap_dir)) {
140 } else if (make_backups && !backup_dir && !is_backup_file(fname)
141 && !(flags & DEL_FORCE_RECURSE))
142 ok = make_backup(fname);
144 ok = do_rmdir(fname) == 0;
146 if (!(flags & DEL_TERSE))
147 log_delete(fname, mode);
150 if (errno == ENOENT) {
155 rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
159 flags |= DEL_FORCE_RECURSE; /* mark subdir dels as not "in the way" */
162 dlen = strlcpy(buf, fname, MAXPATHLEN);
163 save_filters = push_local_filters(buf, dlen);
165 dirlist = get_dirlist(buf, dlen, 0);
166 for (j = dirlist->count; j--; ) {
167 struct file_struct *fp = dirlist->files[j];
169 if (fp->flags & FLAG_MOUNT_POINT)
173 delete_item(buf, fp->mode, flags & ~DEL_TERSE);
177 pop_local_filters(save_filters);
179 if (max_delete && ++deletion_count > max_delete)
182 if (do_rmdir(fname) == 0) {
183 if (!(flags & DEL_TERSE))
184 log_delete(fname, mode);
185 } else if (errno != ENOTEMPTY && errno != EEXIST && errno != ENOENT) {
186 rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
195 /* This function is used to implement per-directory deletion, and is used by
196 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
197 * MAXPATHLEN buffer with the name of the directory in it (the functions we
198 * call will append names onto the end, but the old dir value will be restored
200 static void delete_in_dir(struct file_list *flist, char *fbuf,
201 struct file_struct *file)
203 static int min_depth = MAXPATHLEN, cur_depth = -1;
204 static void *filt_array[MAXPATHLEN/2+1];
205 static int already_warned = 0;
206 struct file_list *dirlist;
207 char delbuf[MAXPATHLEN];
212 while (cur_depth >= min_depth)
213 pop_local_filters(filt_array[cur_depth--]);
214 min_depth = MAXPATHLEN;
220 rprintf(FINFO, "delete_in_dir(%s)\n", safe_fname(fbuf));
223 maybe_send_keepalive();
225 if (file->dir.depth >= MAXPATHLEN/2+1)
226 return; /* Impossible... */
228 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
232 "IO error encountered -- skipping file deletion\n");
237 while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
238 pop_local_filters(filt_array[cur_depth--]);
239 cur_depth = file->dir.depth;
240 if (min_depth > cur_depth)
241 min_depth = cur_depth;
243 filt_array[cur_depth] = push_local_filters(fbuf, dlen);
245 if (link_stat(fbuf, &st, keep_dirlinks) < 0)
248 if (one_file_system) {
249 if (file->flags & FLAG_TOP_DIR)
250 filesystem_dev = st.st_dev;
251 else if (filesystem_dev != st.st_dev)
255 dirlist = get_dirlist(fbuf, dlen, 0);
257 /* If an item in dirlist is not found in flist, delete it
258 * from the filesystem. */
259 for (i = dirlist->count; i--; ) {
260 struct file_struct *fp = dirlist->files[i];
261 if (!fp->basename || fp->flags & FLAG_MOUNT_POINT)
263 if (flist_find(flist, fp) < 0) {
265 f_name_to(fp, delbuf);
266 delete_item(delbuf, mode, DEL_FORCE_RECURSE);
273 /* This deletes any files on the receiving side that are not present on the
274 * sending side. This is used by --delete-before and --delete-after. */
275 static void do_delete_pass(struct file_list *flist)
277 char fbuf[MAXPATHLEN];
280 if (dry_run > 1 /* destination doesn't exist yet */
284 for (j = 0; j < flist->count; j++) {
285 struct file_struct *file = flist->files[j];
287 if (!(file->flags & FLAG_DEL_HERE))
290 f_name_to(file, fbuf);
291 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
292 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
294 delete_in_dir(flist, fbuf, file);
296 if (do_progress && !am_server)
297 rprintf(FINFO, " \r");
300 static int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
303 && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
306 if (am_root && preserve_uid && st->st_uid != file->uid)
309 if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid)
316 void itemize(struct file_struct *file, int ndx, int statret, STRUCT_STAT *st,
317 int32 iflags, uchar fnamecmp_type, char *xname)
320 if (S_ISREG(file->mode) && file->length != st->st_size)
321 iflags |= ITEM_REPORT_SIZE;
322 if (!(iflags & ITEM_NO_DEST_AND_NO_UPDATE)) {
323 int keep_time = !preserve_times ? 0
324 : S_ISDIR(file->mode) ? !omit_dir_times
325 : !S_ISLNK(file->mode);
327 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
328 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
329 || (keep_time && cmp_modtime(file->modtime, st->st_mtime) != 0))
330 iflags |= ITEM_REPORT_TIME;
332 && (file->mode & CHMOD_BITS) != (st->st_mode & CHMOD_BITS))
333 iflags |= ITEM_REPORT_PERMS;
334 if (preserve_uid && am_root && file->uid != st->st_uid)
335 iflags |= ITEM_REPORT_OWNER;
336 if (preserve_gid && file->gid != GID_NONE
337 && st->st_gid != file->gid)
338 iflags |= ITEM_REPORT_GROUP;
341 iflags |= ITEM_IS_NEW;
344 if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
345 || (xname && *xname)) && !read_batch) {
346 if (protocol_version >= 29) {
348 write_int(sock_f_out, ndx);
349 write_shortint(sock_f_out, iflags);
350 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
351 write_byte(sock_f_out, fnamecmp_type);
352 if (iflags & ITEM_XNAME_FOLLOWS)
353 write_vstring(sock_f_out, xname, strlen(xname));
355 log_item(file, &stats, iflags, xname);
360 /* Perform our quick-check heuristic for determining if a file is unchanged. */
361 static int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
363 if (st->st_size != file->length)
366 /* if always checksum is set then we use the checksum instead
367 of the file time to determine whether to sync */
368 if (always_checksum && S_ISREG(st->st_mode)) {
369 char sum[MD4_SUM_LENGTH];
370 file_checksum(fn, sum, st->st_size);
371 return memcmp(sum, file->u.sum, protocol_version < 21 ? 2
372 : MD4_SUM_LENGTH) == 0;
381 return cmp_modtime(st->st_mtime, file->modtime) == 0;
386 * set (initialize) the size entries in the per-file sum_struct
387 * calculating dynamic block and checksum sizes.
389 * This is only called from generate_and_send_sums() but is a separate
390 * function to encapsulate the logic.
392 * The block size is a rounded square root of file length.
394 * The checksum size is determined according to:
395 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
396 * provided by Donovan Baarda which gives a probability of rsync
397 * algorithm corrupting data and falling back using the whole md4
400 * This might be made one of several selectable heuristics.
402 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
408 blength = block_size;
409 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
410 blength = BLOCK_SIZE;
415 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
416 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
417 blength = MAX_BLOCK_SIZE;
422 if (len < (int64)blength * blength)
425 } while (c >= 8); /* round to multiple of 8 */
426 blength = MAX(blength, BLOCK_SIZE);
430 if (protocol_version < 27) {
431 s2length = csum_length;
432 } else if (csum_length == SUM_LENGTH) {
433 s2length = SUM_LENGTH;
437 int b = BLOCKSUM_BIAS;
438 for (l = len; l >>= 1; b += 2) {}
439 for (c = blength; c >>= 1 && b; b--) {}
440 /* add a bit, subtract rollsum, round up. */
441 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
442 s2length = MAX(s2length, csum_length);
443 s2length = MIN(s2length, SUM_LENGTH);
447 sum->blength = blength;
448 sum->s2length = s2length;
449 sum->count = (len + (blength - 1)) / blength;
450 sum->remainder = (len % blength);
452 if (sum->count && verbose > 2) {
454 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
455 (double)sum->count, (long)sum->remainder, (long)sum->blength,
456 sum->s2length, (double)sum->flength);
462 * Generate and send a stream of signatures/checksums that describe a buffer
464 * Generate approximately one checksum every block_len bytes.
466 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
469 struct map_struct *mapbuf;
470 struct sum_struct sum;
473 sum_sizes_sqroot(&sum, len);
474 write_sum_head(f_out, &sum);
476 if (append_mode > 0 && f_copy < 0)
480 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
484 for (i = 0; i < sum.count; i++) {
485 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
486 char *map = map_ptr(mapbuf, offset, n1);
487 char sum2[SUM_LENGTH];
494 full_write(f_copy, map, n1);
499 sum1 = get_checksum1(map, n1);
500 get_checksum2(map, n1, sum2);
504 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
505 (double)i, (double)offset - n1, (long)n1,
506 (unsigned long)sum1);
508 write_int(f_out, sum1);
509 write_buf(f_out, sum2, sum.s2length);
517 /* Try to find a filename in the same dir as "fname" with a similar name. */
518 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
520 int fname_len, fname_suf_len;
521 const char *fname_suf, *fname = file->basename;
522 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
523 int j, lowest_j = -1;
525 fname_len = strlen(fname);
526 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
528 for (j = 0; j < dirlist->count; j++) {
529 struct file_struct *fp = dirlist->files[j];
530 const char *suf, *name;
534 if (!S_ISREG(fp->mode) || !fp->length
535 || fp->flags & FLAG_NO_FUZZY)
540 if (fp->length == file->length
541 && cmp_modtime(fp->modtime, file->modtime) == 0) {
544 "fuzzy size/modtime match for %s\n",
551 suf = find_filename_suffix(name, len, &suf_len);
553 dist = fuzzy_distance(name, len, fname, fname_len);
554 /* Add some extra weight to how well the suffixes match. */
555 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
558 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
559 name, (int)(dist>>16), (int)(dist&0xFFFF));
561 if (dist <= lowest_dist) {
570 void check_for_finished_hlinks(int itemizing, enum logcode code)
572 struct file_struct *file;
575 while ((ndx = get_hlink_num()) != -1) {
576 if (ndx < 0 || ndx >= the_file_list->count)
579 file = the_file_list->files[ndx];
580 if (!file->link_u.links)
583 hard_link_cluster(file, ndx, itemizing, code);
587 static int phase = 0;
589 /* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir,
590 * make sure it exists, and has the right permissions/timestamp info. For
591 * all other non-regular files (symlinks, etc.) we create them here. For
592 * regular files that have changed, we try to find a basis file and then
593 * start sending checksums.
595 * Note that f_out is set to -1 when doing final directory-permission and
596 * modification-time repair. */
597 static void recv_generator(char *fname, struct file_struct *file, int ndx,
598 int itemizing, int maybe_PERMS_REPORT,
599 enum logcode code, int f_out)
601 static int missing_below = -1, excluded_below = -1;
602 static char *fuzzy_dirname = "";
603 static struct file_list *fuzzy_dirlist = NULL;
604 struct file_struct *fuzzy_file = NULL;
605 int fd = -1, f_copy = -1;
606 STRUCT_STAT st, real_st, partial_st;
607 struct file_struct *back_file = NULL;
608 int statret, real_ret, stat_errno;
609 char *fnamecmp, *partialptr, *backupptr = NULL;
610 char fnamecmpbuf[MAXPATHLEN];
618 flist_free(fuzzy_dirlist);
619 fuzzy_dirlist = NULL;
622 if (missing_below >= 0) {
630 rprintf(FINFO, "recv_generator(%s,%d)\n",
631 safe_fname(fname), ndx);
634 if (server_filter_list.head) {
635 if (excluded_below >= 0) {
636 if (file->dir.depth > excluded_below)
640 if (check_filter(&server_filter_list, fname,
641 S_ISDIR(file->mode)) < 0) {
642 if (S_ISDIR(file->mode))
643 excluded_below = file->dir.depth;
647 "skipping server-excluded file \"%s\"\n",
654 if (missing_below >= 0 && file->dir.depth <= missing_below) {
662 if (fuzzy_basis && S_ISREG(file->mode)) {
663 char *dn = file->dirname ? file->dirname : ".";
664 if (fuzzy_dirname != dn
665 && strcmp(fuzzy_dirname, dn) != 0) {
667 flist_free(fuzzy_dirlist);
668 fuzzy_dirlist = get_dirlist(dn, -1, 1);
673 statret = link_stat(fname, &st,
674 keep_dirlinks && S_ISDIR(file->mode));
678 if (only_existing && statret == -1 && stat_errno == ENOENT) {
679 /* we only want to update existing files */
681 rprintf(FINFO, "not creating new %s \"%s\"\n",
682 S_ISDIR(file->mode) ? "directory" : "file",
688 if (statret == 0 && !preserve_perms
689 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode)) {
690 /* if the file exists already and we aren't perserving
691 * permissions then act as though the remote end sent
692 * us the file permissions we already have */
693 file->mode = (file->mode & ~CHMOD_BITS)
694 | (st.st_mode & CHMOD_BITS);
697 if (S_ISDIR(file->mode)) {
698 /* The file to be received is a directory, so we need
699 * to prepare appropriately. If there is already a
700 * file of that name and it is *not* a directory, then
701 * we need to delete it. If it doesn't exist, then
702 * (perhaps recursively) create it. */
703 if (statret == 0 && !S_ISDIR(st.st_mode)) {
704 if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
708 if (dry_run && statret != 0 && missing_below < 0) {
709 missing_below = file->dir.depth;
712 if (itemizing && f_out != -1) {
713 itemize(file, ndx, statret, &st,
714 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
716 if (statret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
717 if (!relative_paths || errno != ENOENT
718 || create_directory_path(fname, orig_umask) < 0
719 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
720 rsyserr(FERROR, errno,
721 "recv_generator: mkdir %s failed",
725 if (set_perms(fname, file, statret ? NULL : &st, 0)
726 && verbose && code && f_out != -1)
727 rprintf(code, "%s/\n", safe_fname(fname));
728 if (delete_during && f_out != -1 && !phase && dry_run < 2
729 && (file->flags & FLAG_DEL_HERE))
730 delete_in_dir(the_file_list, fname, file);
734 if (preserve_links && S_ISLNK(file->mode)) {
736 if (safe_symlinks && unsafe_symlink(file->u.link, fname)) {
738 if (the_file_list->count == 1)
739 fname = f_name(file);
741 "ignoring unsafe symlink %s -> \"%s\"\n",
743 safe_fname(file->u.link));
748 char lnk[MAXPATHLEN];
751 if (!S_ISDIR(st.st_mode)
752 && (len = readlink(fname, lnk, MAXPATHLEN-1)) > 0) {
754 /* A link already pointing to the
755 * right place -- no further action
757 if (strcmp(lnk, file->u.link) == 0) {
759 itemize(file, ndx, 0, &st, 0,
762 set_perms(fname, file, &st,
767 /* Not the right symlink (or not a symlink), so
769 if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
771 if (!S_ISLNK(st.st_mode))
774 if (do_symlink(file->u.link,fname) != 0) {
775 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
776 full_fname(fname), safe_fname(file->u.link));
778 set_perms(fname,file,NULL,0);
780 itemize(file, ndx, statret, &st,
781 ITEM_LOCAL_CHANGE, 0, NULL);
783 if (code && verbose) {
784 rprintf(code, "%s -> %s\n", safe_fname(fname),
785 safe_fname(file->u.link));
787 if (remove_sent_files && !dry_run) {
789 SIVAL(numbuf, 0, ndx);
790 send_msg(MSG_SUCCESS, numbuf, 4);
797 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
799 st.st_mode != file->mode ||
800 st.st_rdev != file->u.rdev) {
801 if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
803 if (!IS_DEVICE(st.st_mode))
806 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
808 (int)file->mode, (int)file->u.rdev);
810 if (do_mknod(fname,file->mode,file->u.rdev) < 0) {
811 rsyserr(FERROR, errno, "mknod %s failed",
814 set_perms(fname,file,NULL,0);
816 itemize(file, ndx, statret, &st,
817 ITEM_LOCAL_CHANGE, 0, NULL);
819 if (code && verbose) {
820 rprintf(code, "%s\n",
826 itemize(file, ndx, statret, &st, 0, 0, NULL);
827 set_perms(fname, file, &st, maybe_PERMS_REPORT);
832 if (preserve_hard_links
833 && hard_link_check(file, ndx, fname, statret, &st,
834 itemizing, code, HL_CHECK_MASTER))
837 if (!S_ISREG(file->mode)) {
838 if (the_file_list->count == 1)
839 fname = f_name(file);
840 rprintf(FINFO, "skipping non-regular file \"%s\"\n",
845 if (max_size && file->length > max_size) {
847 if (the_file_list->count == 1)
848 fname = f_name(file);
849 rprintf(FINFO, "%s is over max-size\n",
855 if (opt_ignore_existing && statret == 0) {
857 rprintf(FINFO, "%s exists\n", safe_fname(fname));
861 if (update_only && statret == 0
862 && cmp_modtime(st.st_mtime, file->modtime) > 0) {
864 rprintf(FINFO, "%s is newer\n", safe_fname(fname));
869 fnamecmp_type = FNAMECMP_FNAME;
871 if (statret == 0 && !S_ISREG(st.st_mode)) {
872 if (delete_item(fname, st.st_mode, DEL_TERSE) != 0)
878 if (statret != 0 && basis_dir[0] != NULL) {
883 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
884 basis_dir[i], fname);
885 if (link_stat(fnamecmpbuf, &st, 0) < 0
886 || !S_ISREG(st.st_mode))
888 switch (match_level) {
894 if (!unchanged_file(fnamecmpbuf, file, &st))
902 if (!unchanged_attrs(file, &st))
909 } while (basis_dir[++i] != NULL);
912 if (i != best_match) {
914 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
915 basis_dir[i], fname);
916 if (link_stat(fnamecmpbuf, &st, 0) < 0) {
923 if (link_dest && match_level == 3) {
924 if (hard_link_one(file, ndx, fname, -1, &st,
926 itemizing && verbose > 1,
928 if (preserve_hard_links
929 && file->link_u.links) {
930 hard_link_cluster(file, ndx,
939 if (match_level == 2) {
940 /* Copy the file locally. */
941 if (copy_file(fnamecmpbuf, fname, file->mode) < 0) {
943 rsyserr(FINFO, errno,
944 "copy_file %s => %s",
945 full_fname(fnamecmpbuf),
952 itemize(file, ndx, 0, &st,
953 ITEM_LOCAL_CHANGE, 0,
955 } else if (verbose && code) {
956 rprintf(code, "%s\n",
959 set_perms(fname, file, NULL,
961 if (preserve_hard_links
962 && file->link_u.links) {
963 hard_link_cluster(file, ndx,
969 } else if (compare_dest || match_level == 1) {
970 fnamecmp = fnamecmpbuf;
979 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
980 && link_stat(partialptr, &partial_st, 0) == 0
981 && S_ISREG(partial_st.st_mode)) {
983 goto prepare_to_open;
987 if (statret != 0 && fuzzy_basis && dry_run <= 1) {
988 int j = find_fuzzy(file, fuzzy_dirlist);
990 fuzzy_file = fuzzy_dirlist->files[j];
991 f_name_to(fuzzy_file, fnamecmpbuf);
993 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
994 safe_fname(fname), safe_fname(fnamecmpbuf));
996 st.st_size = fuzzy_file->length;
998 fnamecmp = fnamecmpbuf;
999 fnamecmp_type = FNAMECMP_FUZZY;
1004 if (preserve_hard_links
1005 && hard_link_check(file, ndx, fname, statret, &st,
1006 itemizing, code, HL_SKIP))
1008 if (stat_errno == ENOENT)
1011 rsyserr(FERROR, stat_errno,
1012 "recv_generator: failed to stat %s",
1018 if (append_mode && st.st_size > file->length)
1021 if (!compare_dest && fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1023 else if (fnamecmp_type == FNAMECMP_FUZZY)
1025 else if (unchanged_file(fnamecmp, file, &st)) {
1026 if (fnamecmp_type == FNAMECMP_FNAME) {
1028 itemize(file, ndx, real_ret, &real_st,
1031 set_perms(fname, file, &st, maybe_PERMS_REPORT);
1032 if (preserve_hard_links && file->link_u.links)
1033 hard_link_cluster(file, ndx, itemizing, code);
1036 /* Only --compare-dest gets here. */
1037 itemize(file, ndx, real_ret, &real_st,
1038 ITEM_NO_DEST_AND_NO_UPDATE, 0, NULL);
1045 fnamecmp = partialptr;
1046 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1050 if (!do_xfers || read_batch || whole_file)
1054 int j = flist_find(fuzzy_dirlist, file);
1055 if (j >= 0) /* don't use changing file as future fuzzy basis */
1056 fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY;
1060 fd = do_open(fnamecmp, O_RDONLY, 0);
1063 rsyserr(FERROR, errno, "failed to open %s, continuing",
1064 full_fname(fnamecmp));
1066 /* pretend the file didn't exist */
1067 if (preserve_hard_links
1068 && hard_link_check(file, ndx, fname, statret, &st,
1069 itemizing, code, HL_SKIP))
1071 statret = real_ret = -1;
1075 if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
1076 if (!(backupptr = get_backup_name(fname))) {
1080 if (!(back_file = make_file(fname, NULL, NO_FILTERS))) {
1082 goto pretend_missing;
1084 if (robust_unlink(backupptr) && errno != ENOENT) {
1085 rsyserr(FERROR, errno, "unlink %s",
1086 full_fname(backupptr));
1091 if ((f_copy = do_open(backupptr,
1092 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1093 rsyserr(FERROR, errno, "open %s",
1094 full_fname(backupptr));
1099 fnamecmp_type = FNAMECMP_BACKUP;
1103 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1104 safe_fname(fnamecmp), (double)st.st_size);
1108 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1111 write_int(f_out, ndx);
1113 int iflags = ITEM_TRANSFER;
1114 if (always_checksum)
1115 iflags |= ITEM_REPORT_CHECKSUM;
1116 if (fnamecmp_type != FNAMECMP_FNAME)
1117 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1118 if (fnamecmp_type == FNAMECMP_FUZZY)
1119 iflags |= ITEM_XNAME_FOLLOWS;
1120 itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type,
1121 fuzzy_file ? fuzzy_file->basename : NULL);
1125 if (preserve_hard_links && file->link_u.links)
1126 hard_link_cluster(file, ndx, itemizing, code);
1132 if (statret != 0 || whole_file) {
1133 write_sum_head(f_out, NULL);
1137 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
1141 set_perms(backupptr, back_file, NULL, 0);
1143 rprintf(FINFO, "backed up %s to %s\n",
1144 safe_fname(fname), safe_fname(backupptr));
1153 void generate_files(int f_out, struct file_list *flist, char *local_name)
1156 char fbuf[MAXPATHLEN];
1157 int itemizing, maybe_PERMS_REPORT;
1159 int lull_mod = allowed_lull * 5;
1160 int need_retouch_dir_times = preserve_times && !omit_dir_times;
1161 int need_retouch_dir_perms = 0;
1162 int save_only_existing = only_existing;
1163 int save_opt_ignore_existing = opt_ignore_existing;
1164 int save_do_progress = do_progress;
1165 int save_make_backups = make_backups;
1167 if (protocol_version >= 29) {
1169 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
1170 code = daemon_log_format_has_i ? 0 : FLOG;
1171 } else if (am_daemon) {
1172 itemizing = daemon_log_format_has_i && do_xfers;
1173 maybe_PERMS_REPORT = PERMS_REPORT;
1174 code = itemizing || !do_xfers ? FCLIENT : FINFO;
1175 } else if (!am_server) {
1176 itemizing = log_format_has_i;
1177 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
1178 code = itemizing ? 0 : FINFO;
1181 maybe_PERMS_REPORT = PERMS_REPORT;
1186 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
1187 (long)getpid(), flist->count);
1190 if (delete_before && !local_name && flist->count > 0)
1191 do_delete_pass(flist);
1194 if (append_mode || whole_file < 0)
1197 rprintf(FINFO, "delta-transmission %s\n",
1199 ? "disabled for local transfer or --whole-file"
1203 /* Since we often fill up the outgoing socket and then just sit around
1204 * waiting for the other 2 processes to do their thing, we don't want
1205 * to exit on a timeout. If the data stops flowing, the receiver will
1206 * notice that and let us know via the redo pipe (or its closing). */
1209 for (i = 0; i < flist->count; i++) {
1210 struct file_struct *file = flist->files[i];
1212 if (!file->basename)
1215 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
1216 file, i, itemizing, maybe_PERMS_REPORT, code,
1219 /* We need to ensure that any dirs we create have writeable
1220 * permissions during the time we are putting files within
1221 * them. This is then fixed after the transfer is done. */
1223 if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)
1225 int mode = file->mode | S_IWUSR; /* user write */
1226 char *fname = local_name ? local_name : fbuf;
1227 if (do_chmod(fname, mode) < 0) {
1228 rsyserr(FERROR, errno,
1229 "failed to modify permissions on %s",
1232 need_retouch_dir_perms = 1;
1236 if (preserve_hard_links)
1237 check_for_finished_hlinks(itemizing, code);
1239 if (allowed_lull && !(i % lull_mod))
1240 maybe_send_keepalive();
1241 else if (!(i % 200))
1242 maybe_flush_socket();
1244 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
1246 delete_in_dir(NULL, NULL, NULL);
1249 csum_length = SUM_LENGTH;
1250 only_existing = max_size = opt_ignore_existing = 0;
1251 update_only = always_checksum = size_only = 0;
1253 if (append_mode) /* resend w/o append mode */
1254 append_mode = -1; /* ... but only longer files */
1255 make_backups = 0; /* avoid a duplicate backup for inplace processing */
1258 rprintf(FINFO,"generate_files phase=%d\n",phase);
1260 write_int(f_out, -1);
1262 /* files can cycle through the system more than once
1263 * to catch initial checksum errors */
1264 while ((i = get_redo_num(itemizing, code)) != -1) {
1265 struct file_struct *file = flist->files[i];
1266 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
1267 file, i, itemizing, maybe_PERMS_REPORT, code,
1272 only_existing = save_only_existing;
1273 opt_ignore_existing = save_opt_ignore_existing;
1274 make_backups = save_make_backups;
1277 rprintf(FINFO,"generate_files phase=%d\n",phase);
1279 write_int(f_out, -1);
1280 /* Reduce round-trip lag-time for a useless delay-updates phase. */
1281 if (protocol_version >= 29 && !delay_updates)
1282 write_int(f_out, -1);
1284 /* Read MSG_DONE for the redo phase (and any prior messages). */
1285 get_redo_num(itemizing, code);
1287 if (protocol_version >= 29) {
1290 rprintf(FINFO, "generate_files phase=%d\n", phase);
1292 write_int(f_out, -1);
1293 /* Read MSG_DONE for delay-updates phase & prior messages. */
1294 get_redo_num(itemizing, code);
1297 do_progress = save_do_progress;
1298 if (delete_after && !local_name && flist->count > 0)
1299 do_delete_pass(flist);
1301 if ((need_retouch_dir_perms || need_retouch_dir_times)
1302 && !list_only && !local_name && !dry_run) {
1304 /* Now we need to fix any directory permissions that were
1305 * modified during the transfer and/or re-set any tweaked
1306 * modified-time values. */
1307 for (i = 0; i < flist->count; i++) {
1308 struct file_struct *file = flist->files[i];
1309 if (!file->basename || !S_ISDIR(file->mode))
1311 if (!need_retouch_dir_times && file->mode & S_IWUSR)
1313 recv_generator(f_name(file), file, i, itemizing,
1314 maybe_PERMS_REPORT, code, -1);
1315 if (allowed_lull && !(++j % lull_mod))
1316 maybe_send_keepalive();
1317 else if (!(j % 200))
1318 maybe_flush_socket();
1321 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
1323 if (max_delete > 0 && deletion_count > max_delete) {
1325 "Deletions stopped due to --max-delete limit (%d skipped)\n",
1326 deletion_count - max_delete);
1327 io_error |= IOERR_DEL_LIMIT;
1331 rprintf(FINFO,"generate_files finished\n");