Fixed --one-file-system handling when dealing with multiple
[rsync.git] / generator.c
blob530c274814e9f6212bdc38921e01caeaeb116ad6
1 /*
2 * Routines that are exclusive to the generator process.
4 * Copyright (C) 1996-2000 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2003-2007 Wayne Davison
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, visit the http://fsf.org website.
23 #include "rsync.h"
25 extern int verbose;
26 extern int dry_run;
27 extern int do_xfers;
28 extern int stdout_format_has_i;
29 extern int logfile_format_has_i;
30 extern int am_root;
31 extern int am_server;
32 extern int am_daemon;
33 extern int inc_recurse;
34 extern int do_progress;
35 extern int relative_paths;
36 extern int implied_dirs;
37 extern int keep_dirlinks;
38 extern int preserve_acls;
39 extern int preserve_xattrs;
40 extern int preserve_links;
41 extern int preserve_devices;
42 extern int preserve_specials;
43 extern int preserve_hard_links;
44 extern int preserve_perms;
45 extern int preserve_times;
46 extern int uid_ndx;
47 extern int gid_ndx;
48 extern int delete_mode;
49 extern int delete_before;
50 extern int delete_during;
51 extern int delete_after;
52 extern int msgdone_cnt;
53 extern int ignore_errors;
54 extern int remove_source_files;
55 extern int delay_updates;
56 extern int update_only;
57 extern int ignore_existing;
58 extern int ignore_non_existing;
59 extern int inplace;
60 extern int append_mode;
61 extern int make_backups;
62 extern int csum_length;
63 extern int ignore_times;
64 extern int size_only;
65 extern OFF_T max_size;
66 extern OFF_T min_size;
67 extern int io_error;
68 extern int flist_eof;
69 extern int allowed_lull;
70 extern int sock_f_out;
71 extern int ignore_timeout;
72 extern int protocol_version;
73 extern int file_total;
74 extern int fuzzy_basis;
75 extern int always_checksum;
76 extern int checksum_len;
77 extern char *partial_dir;
78 extern char *basis_dir[];
79 extern int compare_dest;
80 extern int copy_dest;
81 extern int link_dest;
82 extern int whole_file;
83 extern int list_only;
84 extern int read_batch;
85 extern int safe_symlinks;
86 extern long block_size; /* "long" because popt can't set an int32. */
87 extern int max_delete;
88 extern int force_delete;
89 extern int one_file_system;
90 extern struct stats stats;
91 extern dev_t filesystem_dev;
92 extern mode_t orig_umask;
93 extern char *backup_dir;
94 extern char *backup_suffix;
95 extern int backup_suffix_len;
96 extern struct file_list *cur_flist, *first_flist, *dir_flist;
97 extern struct filter_list_struct server_filter_list;
98 #ifdef ICONV_OPTION
99 extern int ic_ndx;
100 #endif
102 int ignore_perishable = 0;
103 int non_perishable_cnt = 0;
104 int maybe_ATTRS_REPORT = 0;
106 static dev_t dev_zero;
107 static int deletion_count = 0; /* used to implement --max-delete */
108 static int deldelay_size = 0, deldelay_cnt = 0;
109 static char *deldelay_buf = NULL;
110 static int deldelay_fd = -1;
111 static int lull_mod;
112 static int dir_tweaking;
113 static int need_retouch_dir_times;
114 static int need_retouch_dir_perms;
115 static const char *solo_file = NULL;
117 /* For calling delete_item() and delete_dir_contents(). */
118 #define DEL_RECURSE (1<<1) /* recurse */
119 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
121 enum nonregtype {
122 TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
125 enum delret {
126 DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_NOT_EMPTY
129 /* Forward declaration for delete_item(). */
130 static enum delret delete_dir_contents(char *fname, int flags);
133 static int is_backup_file(char *fn)
135 int k = strlen(fn) - backup_suffix_len;
136 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
139 /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
140 * delete recursively.
142 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
143 * a directory! (The buffer is used for recursion, but returned unchanged.)
145 static enum delret delete_item(char *fbuf, int mode, char *replace, int flags)
147 enum delret ret;
148 char *what;
149 int ok;
151 if (verbose > 2) {
152 rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
153 fbuf, mode, flags);
156 if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
157 ignore_perishable = 1;
158 /* If DEL_RECURSE is not set, this just reports emptiness. */
159 ret = delete_dir_contents(fbuf, flags);
160 ignore_perishable = 0;
161 if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT)
162 goto check_ret;
163 /* OK: try to delete the directory. */
166 if (!replace && max_delete >= 0 && ++deletion_count > max_delete)
167 return DR_AT_LIMIT;
169 if (S_ISDIR(mode)) {
170 what = "rmdir";
171 ok = do_rmdir(fbuf) == 0;
172 } else if (make_backups > 0 && (backup_dir || !is_backup_file(fbuf))) {
173 what = "make_backup";
174 ok = make_backup(fbuf);
175 } else {
176 what = "unlink";
177 ok = robust_unlink(fbuf) == 0;
180 if (ok) {
181 if (!replace)
182 log_delete(fbuf, mode);
183 ret = DR_SUCCESS;
184 } else {
185 if (S_ISDIR(mode) && errno == ENOTEMPTY) {
186 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
187 fbuf);
188 ret = DR_NOT_EMPTY;
189 } else if (errno != ENOENT) {
190 rsyserr(FERROR, errno, "delete_file: %s(%s) failed",
191 what, fbuf);
192 ret = DR_FAILURE;
193 } else {
194 deletion_count--;
195 ret = DR_SUCCESS;
199 check_ret:
200 if (replace && ret != DR_SUCCESS) {
201 rprintf(FERROR, "could not make way for new %s: %s\n",
202 replace, fbuf);
204 return ret;
207 /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
208 * its contents, otherwise just checks for content. Returns DR_SUCCESS or
209 * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
210 * buffer is used for recursion, but returned unchanged.)
212 static enum delret delete_dir_contents(char *fname, int flags)
214 struct file_list *dirlist;
215 enum delret ret;
216 unsigned remainder;
217 void *save_filters;
218 int j, dlen;
219 char *p;
221 if (verbose > 3) {
222 rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n",
223 fname, flags);
226 dlen = strlen(fname);
227 save_filters = push_local_filters(fname, dlen);
229 non_perishable_cnt = 0;
230 dirlist = get_dirlist(fname, dlen, 0);
231 ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
233 if (!dirlist->used)
234 goto done;
236 if (!(flags & DEL_RECURSE)) {
237 ret = DR_NOT_EMPTY;
238 goto done;
241 p = fname + dlen;
242 if (dlen != 1 || *fname != '/')
243 *p++ = '/';
244 remainder = MAXPATHLEN - (p - fname);
246 /* We do our own recursion, so make delete_item() non-recursive. */
247 flags = (flags & ~DEL_RECURSE) | DEL_DIR_IS_EMPTY;
249 for (j = dirlist->used; j--; ) {
250 struct file_struct *fp = dirlist->files[j];
252 if (fp->flags & FLAG_MOUNT_DIR) {
253 if (verbose > 1) {
254 rprintf(FINFO,
255 "mount point, %s, pins parent directory\n",
256 f_name(fp, NULL));
258 ret = DR_NOT_EMPTY;
259 continue;
262 strlcpy(p, fp->basename, remainder);
263 /* Save stack by recursing to ourself directly. */
264 if (S_ISDIR(fp->mode)
265 && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
266 ret = DR_NOT_EMPTY;
267 if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
268 ret = DR_NOT_EMPTY;
271 fname[dlen] = '\0';
273 done:
274 flist_free(dirlist);
275 pop_local_filters(save_filters);
277 if (ret == DR_NOT_EMPTY) {
278 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
279 fname);
281 return ret;
284 static int start_delete_delay_temp(void)
286 char fnametmp[MAXPATHLEN];
287 int save_dry_run = dry_run;
289 dry_run = 0;
290 if (!get_tmpname(fnametmp, "deldelay")
291 || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
292 rprintf(FINFO, "NOTE: Unable to create delete-delay temp file%s.\n",
293 inc_recurse ? "" : " -- switching to --delete-after");
294 delete_during = 0;
295 delete_after = !inc_recurse;
296 dry_run = save_dry_run;
297 return 0;
299 unlink(fnametmp);
300 dry_run = save_dry_run;
301 return 1;
304 static int flush_delete_delay(void)
306 if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
307 rsyserr(FERROR, errno, "flush of delete-delay buffer");
308 delete_during = 0;
309 delete_after = 1;
310 close(deldelay_fd);
311 return 0;
313 deldelay_cnt = 0;
314 return 1;
317 static int remember_delete(struct file_struct *file, const char *fname)
319 int len;
321 while (1) {
322 len = snprintf(deldelay_buf + deldelay_cnt,
323 deldelay_size - deldelay_cnt,
324 "%x %s%c", (int)file->mode, fname, '\0');
325 if ((deldelay_cnt += len) <= deldelay_size)
326 break;
327 if (deldelay_fd < 0 && !start_delete_delay_temp())
328 return 0;
329 deldelay_cnt -= len;
330 if (!flush_delete_delay())
331 return 0;
334 return 1;
337 static int read_delay_line(char *buf)
339 static int read_pos = 0;
340 int j, len, mode;
341 char *bp, *past_space;
343 while (1) {
344 for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
345 if (j < deldelay_cnt)
346 break;
347 if (deldelay_fd < 0) {
348 if (j > read_pos)
349 goto invalid_data;
350 return -1;
352 deldelay_cnt -= read_pos;
353 if (deldelay_cnt == deldelay_size)
354 goto invalid_data;
355 if (deldelay_cnt && read_pos) {
356 memmove(deldelay_buf, deldelay_buf + read_pos,
357 deldelay_cnt);
359 len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
360 deldelay_size - deldelay_cnt);
361 if (len == 0) {
362 if (deldelay_cnt) {
363 rprintf(FERROR,
364 "ERROR: unexpected EOF in delete-delay file.\n");
366 return -1;
368 if (len < 0) {
369 rsyserr(FERROR, errno,
370 "reading delete-delay file");
371 return -1;
373 deldelay_cnt += len;
374 read_pos = 0;
377 bp = deldelay_buf + read_pos;
379 if (sscanf(bp, "%x ", &mode) != 1) {
380 invalid_data:
381 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
382 return -1;
384 past_space = strchr(bp, ' ') + 1;
385 len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
386 read_pos = j + 1;
388 if (len > MAXPATHLEN) {
389 rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
390 return -1;
393 /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
394 * instead of returning a pointer to our buffer. */
395 memcpy(buf, past_space, len);
397 return mode;
400 static void do_delayed_deletions(char *delbuf)
402 int mode;
404 if (deldelay_fd >= 0) {
405 if (deldelay_cnt && !flush_delete_delay())
406 return;
407 lseek(deldelay_fd, 0, 0);
409 while ((mode = read_delay_line(delbuf)) >= 0)
410 delete_item(delbuf, mode, NULL, DEL_RECURSE);
411 if (deldelay_fd >= 0)
412 close(deldelay_fd);
415 /* This function is used to implement per-directory deletion, and is used by
416 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
417 * MAXPATHLEN buffer with the name of the directory in it (the functions we
418 * call will append names onto the end, but the old dir value will be restored
419 * on exit). */
420 static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
422 static int already_warned = 0;
423 struct file_list *dirlist;
424 char delbuf[MAXPATHLEN];
425 int dlen, i;
427 if (!fbuf) {
428 change_local_filter_dir(NULL, 0, 0);
429 return;
432 if (verbose > 2)
433 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
435 if (allowed_lull)
436 maybe_send_keepalive();
438 if (io_error && !ignore_errors) {
439 if (already_warned)
440 return;
441 rprintf(FINFO,
442 "IO error encountered -- skipping file deletion\n");
443 already_warned = 1;
444 return;
447 dlen = strlen(fbuf);
448 change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
450 if (one_file_system) {
451 if (file->flags & FLAG_TOP_DIR)
452 filesystem_dev = *fs_dev;
453 else if (filesystem_dev != *fs_dev)
454 return;
457 dirlist = get_dirlist(fbuf, dlen, 0);
459 /* If an item in dirlist is not found in flist, delete it
460 * from the filesystem. */
461 for (i = dirlist->used; i--; ) {
462 struct file_struct *fp = dirlist->files[i];
463 if (!F_IS_ACTIVE(fp))
464 continue;
465 if (fp->flags & FLAG_MOUNT_DIR) {
466 if (verbose > 1)
467 rprintf(FINFO, "cannot delete mount point: %s\n",
468 f_name(fp, NULL));
469 continue;
471 if (flist_find(cur_flist, fp) < 0) {
472 f_name(fp, delbuf);
473 if (delete_during == 2) {
474 if (!remember_delete(fp, delbuf))
475 break;
476 } else
477 delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
481 flist_free(dirlist);
484 /* This deletes any files on the receiving side that are not present on the
485 * sending side. This is used by --delete-before and --delete-after. */
486 static void do_delete_pass(void)
488 char fbuf[MAXPATHLEN];
489 STRUCT_STAT st;
490 int j;
492 /* dry_run is incremented when the destination doesn't exist yet. */
493 if (dry_run > 1 || list_only)
494 return;
496 for (j = 0; j < cur_flist->used; j++) {
497 struct file_struct *file = cur_flist->sorted[j];
499 if (!(file->flags & FLAG_CONTENT_DIR))
500 continue;
502 f_name(file, fbuf);
503 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
504 rprintf(FINFO, "deleting in %s\n", fbuf);
506 if (link_stat(fbuf, &st, keep_dirlinks) < 0
507 || !S_ISDIR(st.st_mode))
508 continue;
510 delete_in_dir(fbuf, file, &st.st_dev);
512 delete_in_dir(NULL, NULL, &dev_zero);
514 if (do_progress && !am_server)
515 rprintf(FINFO, " \r");
518 int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
520 if (preserve_perms && !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
521 return 0;
523 if (am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file))
524 return 0;
526 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
527 return 0;
529 #ifdef SUPPORT_ACLS
530 if (preserve_acls && !S_ISLNK(file->mode)) {
531 if (!ACL_READY(*sxp))
532 get_acl(fname, sxp);
533 if (set_acl(NULL, file, sxp) == 0)
534 return 0;
536 #endif
537 #ifdef SUPPORT_XATTRS
538 if (preserve_xattrs) {
539 if (!XATTR_READY(*sxp))
540 get_xattr(fname, sxp);
541 if (xattr_diff(file, sxp, 0))
542 return 0;
544 #endif
546 return 1;
549 void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
550 stat_x *sxp, int32 iflags, uchar fnamecmp_type,
551 const char *xname)
553 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
554 int keep_time = !preserve_times ? 0
555 : S_ISDIR(file->mode) ? preserve_times > 1
556 : !S_ISLNK(file->mode);
558 if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
559 iflags |= ITEM_REPORT_SIZE;
560 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
561 && !(iflags & ITEM_MATCHED)
562 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
563 || (keep_time && cmp_time(file->modtime, sxp->st.st_mtime) != 0))
564 iflags |= ITEM_REPORT_TIME;
565 #if !defined HAVE_LCHMOD && !defined HAVE_SETATTRLIST
566 if (S_ISLNK(file->mode)) {
568 } else
569 #endif
570 if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
571 iflags |= ITEM_REPORT_PERMS;
572 if (uid_ndx && am_root && (uid_t)F_OWNER(file) != sxp->st.st_uid)
573 iflags |= ITEM_REPORT_OWNER;
574 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
575 && sxp->st.st_gid != (gid_t)F_GROUP(file))
576 iflags |= ITEM_REPORT_GROUP;
577 #ifdef SUPPORT_ACLS
578 if (preserve_acls && !S_ISLNK(file->mode)) {
579 if (!ACL_READY(*sxp))
580 get_acl(fnamecmp, sxp);
581 if (set_acl(NULL, file, sxp) == 0)
582 iflags |= ITEM_REPORT_ACL;
584 #endif
585 #ifdef SUPPORT_XATTRS
586 if (preserve_xattrs) {
587 if (!XATTR_READY(*sxp))
588 get_xattr(fnamecmp, sxp);
589 if (xattr_diff(file, sxp, 1))
590 iflags |= ITEM_REPORT_XATTR;
592 #endif
593 } else {
594 #ifdef SUPPORT_XATTRS
595 if (preserve_xattrs && xattr_diff(file, NULL, 1))
596 iflags |= ITEM_REPORT_XATTR;
597 #endif
598 iflags |= ITEM_IS_NEW;
601 iflags &= 0xffff;
602 if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || verbose > 1
603 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
604 if (protocol_version >= 29) {
605 if (ndx >= 0)
606 write_ndx(sock_f_out, ndx);
607 write_shortint(sock_f_out, iflags);
608 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
609 write_byte(sock_f_out, fnamecmp_type);
610 if (iflags & ITEM_XNAME_FOLLOWS)
611 write_vstring(sock_f_out, xname, strlen(xname));
612 #ifdef SUPPORT_XATTRS
613 if (iflags & ITEM_REPORT_XATTR && !dry_run)
614 send_xattr_request(NULL, file, sock_f_out);
615 #endif
616 } else if (ndx >= 0) {
617 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
618 log_item(code, file, &stats, iflags, xname);
624 /* Perform our quick-check heuristic for determining if a file is unchanged. */
625 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
627 if (st->st_size != F_LENGTH(file))
628 return 0;
630 /* if always checksum is set then we use the checksum instead
631 of the file time to determine whether to sync */
632 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
633 char sum[MAX_DIGEST_LEN];
634 file_checksum(fn, sum, st->st_size);
635 return memcmp(sum, F_SUM(file), checksum_len) == 0;
638 if (size_only > 0)
639 return 1;
641 if (ignore_times)
642 return 0;
644 return cmp_time(st->st_mtime, file->modtime) == 0;
649 * set (initialize) the size entries in the per-file sum_struct
650 * calculating dynamic block and checksum sizes.
652 * This is only called from generate_and_send_sums() but is a separate
653 * function to encapsulate the logic.
655 * The block size is a rounded square root of file length.
657 * The checksum size is determined according to:
658 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
659 * provided by Donovan Baarda which gives a probability of rsync
660 * algorithm corrupting data and falling back using the whole md4
661 * checksums.
663 * This might be made one of several selectable heuristics.
665 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
667 int32 blength;
668 int s2length;
670 if (block_size)
671 blength = block_size;
672 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
673 blength = BLOCK_SIZE;
674 else {
675 int32 c;
676 int64 l;
677 int cnt;
678 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
679 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
680 blength = MAX_BLOCK_SIZE;
681 else {
682 blength = 0;
683 do {
684 blength |= c;
685 if (len < (int64)blength * blength)
686 blength &= ~c;
687 c >>= 1;
688 } while (c >= 8); /* round to multiple of 8 */
689 blength = MAX(blength, BLOCK_SIZE);
693 if (protocol_version < 27) {
694 s2length = csum_length;
695 } else if (csum_length == SUM_LENGTH) {
696 s2length = SUM_LENGTH;
697 } else {
698 int32 c;
699 int64 l;
700 int b = BLOCKSUM_BIAS;
701 for (l = len; l >>= 1; b += 2) {}
702 for (c = blength; (c >>= 1) && b; b--) {}
703 /* add a bit, subtract rollsum, round up. */
704 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
705 s2length = MAX(s2length, csum_length);
706 s2length = MIN(s2length, SUM_LENGTH);
709 sum->flength = len;
710 sum->blength = blength;
711 sum->s2length = s2length;
712 sum->remainder = (int32)(len % blength);
713 sum->count = (int32)(len / blength) + (sum->remainder != 0);
715 if (sum->count && verbose > 2) {
716 rprintf(FINFO,
717 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
718 (double)sum->count, (long)sum->remainder, (long)sum->blength,
719 sum->s2length, (double)sum->flength);
725 * Generate and send a stream of signatures/checksums that describe a buffer
727 * Generate approximately one checksum every block_len bytes.
729 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
731 int32 i;
732 struct map_struct *mapbuf;
733 struct sum_struct sum;
734 OFF_T offset = 0;
736 sum_sizes_sqroot(&sum, len);
737 write_sum_head(f_out, &sum);
739 if (append_mode > 0 && f_copy < 0)
740 return;
742 if (len > 0)
743 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
744 else
745 mapbuf = NULL;
747 for (i = 0; i < sum.count; i++) {
748 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
749 char *map = map_ptr(mapbuf, offset, n1);
750 char sum2[SUM_LENGTH];
751 uint32 sum1;
753 len -= n1;
754 offset += n1;
756 if (f_copy >= 0) {
757 full_write(f_copy, map, n1);
758 if (append_mode > 0)
759 continue;
762 sum1 = get_checksum1(map, n1);
763 get_checksum2(map, n1, sum2);
765 if (verbose > 3) {
766 rprintf(FINFO,
767 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
768 (double)i, (double)offset - n1, (long)n1,
769 (unsigned long)sum1);
771 write_int(f_out, sum1);
772 write_buf(f_out, sum2, sum.s2length);
775 if (mapbuf)
776 unmap_file(mapbuf);
780 /* Try to find a filename in the same dir as "fname" with a similar name. */
781 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
783 int fname_len, fname_suf_len;
784 const char *fname_suf, *fname = file->basename;
785 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
786 int j, lowest_j = -1;
788 fname_len = strlen(fname);
789 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
791 for (j = 0; j < dirlist->used; j++) {
792 struct file_struct *fp = dirlist->files[j];
793 const char *suf, *name;
794 int len, suf_len;
795 uint32 dist;
797 if (!S_ISREG(fp->mode) || !F_LENGTH(fp)
798 || fp->flags & FLAG_FILE_SENT)
799 continue;
801 name = fp->basename;
803 if (F_LENGTH(fp) == F_LENGTH(file)
804 && cmp_time(fp->modtime, file->modtime) == 0) {
805 if (verbose > 4) {
806 rprintf(FINFO,
807 "fuzzy size/modtime match for %s\n",
808 name);
810 return j;
813 len = strlen(name);
814 suf = find_filename_suffix(name, len, &suf_len);
816 dist = fuzzy_distance(name, len, fname, fname_len);
817 /* Add some extra weight to how well the suffixes match. */
818 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
819 * 10;
820 if (verbose > 4) {
821 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
822 name, (int)(dist>>16), (int)(dist&0xFFFF));
824 if (dist <= lowest_dist) {
825 lowest_dist = dist;
826 lowest_j = j;
830 return lowest_j;
833 /* Copy a file found in our --copy-dest handling. */
834 static int copy_altdest_file(const char *src, const char *dest, struct file_struct *file)
836 char buf[MAXPATHLEN];
837 const char *copy_to, *partialptr;
838 int fd_w;
840 if (inplace) {
841 /* Let copy_file open the destination in place. */
842 fd_w = -1;
843 copy_to = dest;
844 } else {
845 fd_w = open_tmpfile(buf, dest, file);
846 if (fd_w < 0)
847 return -1;
848 copy_to = buf;
850 cleanup_set(copy_to, NULL, NULL, -1, -1);
851 if (copy_file(src, copy_to, fd_w, file->mode, 0) < 0) {
852 if (verbose) {
853 rsyserr(FINFO, errno, "copy_file %s => %s",
854 full_fname(src), copy_to);
856 /* Try to clean up. */
857 unlink(copy_to);
858 cleanup_disable();
859 return -1;
861 partialptr = partial_dir ? partial_dir_fname(dest) : NULL;
862 if (partialptr && *partialptr == '/')
863 partialptr = NULL;
864 finish_transfer(dest, copy_to, src, partialptr, file, 1, 0);
865 cleanup_disable();
866 return 0;
869 /* This is only called for regular files. We return -2 if we've finished
870 * handling the file, -1 if no dest-linking occurred, or a non-negative
871 * value if we found an alternate basis file. */
872 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
873 char *cmpbuf, stat_x *sxp, int itemizing,
874 enum logcode code)
876 int best_match = -1;
877 int match_level = 0;
878 int j = 0;
880 do {
881 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
882 if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
883 continue;
884 switch (match_level) {
885 case 0:
886 best_match = j;
887 match_level = 1;
888 /* FALL THROUGH */
889 case 1:
890 if (!unchanged_file(cmpbuf, file, &sxp->st))
891 continue;
892 best_match = j;
893 match_level = 2;
894 /* FALL THROUGH */
895 case 2:
896 if (!unchanged_attrs(cmpbuf, file, sxp))
897 continue;
898 if (always_checksum > 0 && preserve_times
899 && cmp_time(sxp->st.st_mtime, file->modtime))
900 continue;
901 best_match = j;
902 match_level = 3;
903 break;
905 break;
906 } while (basis_dir[++j] != NULL);
908 if (!match_level)
909 return -1;
911 if (j != best_match) {
912 j = best_match;
913 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
914 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
915 return -1;
918 if (match_level == 3 && !copy_dest) {
919 #ifdef SUPPORT_HARD_LINKS
920 if (link_dest) {
921 if (!hard_link_one(file, fname, cmpbuf, 1))
922 goto try_a_copy;
923 if (preserve_hard_links && F_IS_HLINKED(file))
924 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, j);
925 if (itemizing && (verbose > 1 || stdout_format_has_i > 1)) {
926 itemize(cmpbuf, file, ndx, 1, sxp,
927 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
928 0, "");
930 } else
931 #endif
932 if (itemizing)
933 itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
934 if (verbose > 1 && maybe_ATTRS_REPORT)
935 rprintf(FCLIENT, "%s is uptodate\n", fname);
936 return -2;
939 if (match_level >= 2) {
940 #ifdef SUPPORT_HARD_LINKS
941 try_a_copy: /* Copy the file locally. */
942 #endif
943 if (!dry_run && copy_altdest_file(cmpbuf, fname, file) < 0)
944 return -1;
945 if (itemizing)
946 itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
947 #ifdef SUPPORT_XATTRS
948 if (preserve_xattrs)
949 xattr_clear_locals(file);
950 #endif
951 if (maybe_ATTRS_REPORT
952 && ((!itemizing && verbose && match_level == 2)
953 || (verbose > 1 && match_level == 3))) {
954 code = match_level == 3 ? FCLIENT : FINFO;
955 rprintf(code, "%s%s\n", fname,
956 match_level == 3 ? " is uptodate" : "");
958 #ifdef SUPPORT_HARD_LINKS
959 if (preserve_hard_links && F_IS_HLINKED(file))
960 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, -1);
961 #endif
962 return -2;
965 return FNAMECMP_BASIS_DIR_LOW + j;
968 /* This is only called for non-regular files. We return -2 if we've finished
969 * handling the file, or -1 if no dest-linking occurred, or a non-negative
970 * value if we found an alternate basis file. */
971 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
972 char *cmpbuf, stat_x *sxp, int itemizing,
973 enum logcode code)
975 char lnk[MAXPATHLEN];
976 int best_match = -1;
977 int match_level = 0;
978 enum nonregtype type;
979 uint32 *devp;
980 int len, j = 0;
982 #ifndef SUPPORT_LINKS
983 if (S_ISLNK(file->mode))
984 return -1;
985 #endif
986 if (S_ISDIR(file->mode)) {
987 type = TYPE_DIR;
988 } else if (IS_SPECIAL(file->mode))
989 type = TYPE_SPECIAL;
990 else if (IS_DEVICE(file->mode))
991 type = TYPE_DEVICE;
992 #ifdef SUPPORT_LINKS
993 else if (S_ISLNK(file->mode))
994 type = TYPE_SYMLINK;
995 #endif
996 else {
997 rprintf(FERROR,
998 "internal: try_dests_non() called with invalid mode (%o)\n",
999 (int)file->mode);
1000 exit_cleanup(RERR_UNSUPPORTED);
1003 do {
1004 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1005 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1006 continue;
1007 switch (type) {
1008 case TYPE_DIR:
1009 if (!S_ISDIR(sxp->st.st_mode))
1010 continue;
1011 break;
1012 case TYPE_SPECIAL:
1013 if (!IS_SPECIAL(sxp->st.st_mode))
1014 continue;
1015 break;
1016 case TYPE_DEVICE:
1017 if (!IS_DEVICE(sxp->st.st_mode))
1018 continue;
1019 break;
1020 #ifdef SUPPORT_LINKS
1021 case TYPE_SYMLINK:
1022 if (!S_ISLNK(sxp->st.st_mode))
1023 continue;
1024 break;
1025 #endif
1027 if (match_level < 1) {
1028 match_level = 1;
1029 best_match = j;
1031 switch (type) {
1032 case TYPE_DIR:
1033 break;
1034 case TYPE_SPECIAL:
1035 case TYPE_DEVICE:
1036 devp = F_RDEV_P(file);
1037 if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
1038 continue;
1039 break;
1040 #ifdef SUPPORT_LINKS
1041 case TYPE_SYMLINK:
1042 if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
1043 continue;
1044 lnk[len] = '\0';
1045 if (strcmp(lnk, F_SYMLINK(file)) != 0)
1046 continue;
1047 break;
1048 #endif
1050 if (match_level < 2) {
1051 match_level = 2;
1052 best_match = j;
1054 if (unchanged_attrs(cmpbuf, file, sxp)) {
1055 match_level = 3;
1056 best_match = j;
1057 break;
1059 } while (basis_dir[++j] != NULL);
1061 if (!match_level)
1062 return -1;
1064 if (j != best_match) {
1065 j = best_match;
1066 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1067 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1068 return -1;
1071 if (match_level == 3) {
1072 #ifdef SUPPORT_HARD_LINKS
1073 if (link_dest
1074 #ifndef CAN_HARDLINK_SYMLINK
1075 && !S_ISLNK(file->mode)
1076 #endif
1077 #ifndef CAN_HARDLINK_SPECIAL
1078 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
1079 #endif
1080 && !S_ISDIR(file->mode)) {
1081 if (do_link(cmpbuf, fname) < 0) {
1082 rsyserr(FERROR, errno,
1083 "failed to hard-link %s with %s",
1084 cmpbuf, fname);
1085 return j;
1087 if (preserve_hard_links && F_IS_HLINKED(file))
1088 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1089 } else
1090 #endif
1091 match_level = 2;
1092 if (itemizing && stdout_format_has_i
1093 && (verbose > 1 || stdout_format_has_i > 1)) {
1094 int chg = compare_dest && type != TYPE_DIR ? 0
1095 : ITEM_LOCAL_CHANGE
1096 + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1097 char *lp = match_level == 3 ? "" : NULL;
1098 itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
1100 if (verbose > 1 && maybe_ATTRS_REPORT) {
1101 rprintf(FCLIENT, "%s%s is uptodate\n",
1102 fname, type == TYPE_DIR ? "/" : "");
1104 return -2;
1107 return j;
1110 static void list_file_entry(struct file_struct *f)
1112 char permbuf[PERMSTRING_SIZE];
1113 double len;
1115 if (!F_IS_ACTIVE(f)) {
1116 /* this can happen if duplicate names were removed */
1117 return;
1120 permstring(permbuf, f->mode);
1121 len = F_LENGTH(f);
1123 /* TODO: indicate '+' if the entry has an ACL. */
1125 #ifdef SUPPORT_LINKS
1126 if (preserve_links && S_ISLNK(f->mode)) {
1127 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
1128 permbuf, len, timestring(f->modtime),
1129 f_name(f, NULL), F_SYMLINK(f));
1130 } else
1131 #endif
1133 rprintf(FINFO, "%s %11.0f %s %s\n",
1134 permbuf, len, timestring(f->modtime),
1135 f_name(f, NULL));
1139 static int phase = 0;
1140 static int dflt_perms;
1142 /* Acts on the indicated item in cur_flist whose name is fname. If a dir,
1143 * make sure it exists, and has the right permissions/timestamp info. For
1144 * all other non-regular files (symlinks, etc.) we create them here. For
1145 * regular files that have changed, we try to find a basis file and then
1146 * start sending checksums. The ndx is the file's unique index value.
1148 * When fname is non-null, it must point to a MAXPATHLEN buffer!
1150 * Note that f_out is set to -1 when doing final directory-permission and
1151 * modification-time repair. */
1152 static void recv_generator(char *fname, struct file_struct *file, int ndx,
1153 int itemizing, enum logcode code, int f_out)
1155 static int missing_below = -1, excluded_below = -1;
1156 static const char *parent_dirname = "";
1157 static struct file_struct *missing_dir = NULL, *excluded_dir = NULL;
1158 static struct file_list *fuzzy_dirlist = NULL;
1159 static int need_fuzzy_dirlist = 0;
1160 struct file_struct *fuzzy_file = NULL;
1161 int fd = -1, f_copy = -1;
1162 stat_x sx, real_sx;
1163 STRUCT_STAT partial_st;
1164 struct file_struct *back_file = NULL;
1165 int statret, real_ret, stat_errno;
1166 char *fnamecmp, *partialptr, *backupptr = NULL;
1167 char fnamecmpbuf[MAXPATHLEN];
1168 uchar fnamecmp_type;
1169 int implied_dirs_are_missing = relative_paths && !implied_dirs && protocol_version < 30;
1170 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1172 if (verbose > 2)
1173 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
1175 if (list_only) {
1176 if (S_ISDIR(file->mode)
1177 && ((!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
1178 || (inc_recurse && ndx != cur_flist->ndx_start - 1)))
1179 return;
1180 list_file_entry(file);
1181 return;
1184 if (server_filter_list.head) {
1185 if (excluded_below >= 0) {
1186 if (F_DEPTH(file) > excluded_below
1187 && (!implied_dirs_are_missing || f_name_has_prefix(file, excluded_dir)))
1188 goto skipping;
1189 excluded_below = -1;
1191 if (check_filter(&server_filter_list, fname,
1192 S_ISDIR(file->mode)) < 0) {
1193 if (S_ISDIR(file->mode)) {
1194 excluded_below = F_DEPTH(file);
1195 excluded_dir = file;
1197 skipping:
1198 if (verbose) {
1199 rprintf(FINFO,
1200 "skipping server-excluded file \"%s\"\n",
1201 fname);
1203 return;
1207 if (missing_below >= 0) {
1208 if (F_DEPTH(file) <= missing_below
1209 || (implied_dirs_are_missing && !f_name_has_prefix(file, missing_dir))) {
1210 if (dry_run)
1211 dry_run--;
1212 missing_below = -1;
1213 } else if (!dry_run) {
1214 if (S_ISDIR(file->mode))
1215 file->flags |= FLAG_MISSING_DIR;
1216 return;
1219 #ifdef SUPPORT_ACLS
1220 sx.acc_acl = sx.def_acl = NULL;
1221 #endif
1222 #ifdef SUPPORT_XATTRS
1223 sx.xattr = NULL;
1224 #endif
1225 if (dry_run > 1) {
1226 if (fuzzy_dirlist) {
1227 flist_free(fuzzy_dirlist);
1228 fuzzy_dirlist = NULL;
1230 parent_dirname = "";
1231 statret = -1;
1232 stat_errno = ENOENT;
1233 } else {
1234 const char *dn = file->dirname ? file->dirname : ".";
1235 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1236 if (relative_paths && !implied_dirs
1237 && do_stat(dn, &sx.st) < 0
1238 && create_directory_path(fname) < 0) {
1239 rsyserr(FERROR, errno,
1240 "recv_generator: mkdir %s failed",
1241 full_fname(dn));
1243 if (fuzzy_dirlist) {
1244 flist_free(fuzzy_dirlist);
1245 fuzzy_dirlist = NULL;
1247 if (fuzzy_basis)
1248 need_fuzzy_dirlist = 1;
1249 #ifdef SUPPORT_ACLS
1250 if (!preserve_perms)
1251 dflt_perms = default_perms_for_dir(dn);
1252 #endif
1254 parent_dirname = dn;
1256 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
1257 strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1258 fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
1259 need_fuzzy_dirlist = 0;
1262 statret = link_stat(fname, &sx.st,
1263 keep_dirlinks && S_ISDIR(file->mode));
1264 stat_errno = errno;
1267 if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
1268 if (verbose > 1) {
1269 rprintf(FINFO, "not creating new %s \"%s\"\n",
1270 S_ISDIR(file->mode) ? "directory" : "file",
1271 fname);
1273 if (S_ISDIR(file->mode)) {
1274 if (missing_below < 0) {
1275 if (dry_run)
1276 dry_run++;
1277 missing_below = F_DEPTH(file);
1278 missing_dir = file;
1280 file->flags |= FLAG_MISSING_DIR;
1282 return;
1285 if (S_ISDIR(file->mode)) {
1286 if (!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
1287 goto cleanup;
1288 if (inc_recurse && ndx != cur_flist->ndx_start - 1) {
1289 /* In inc_recurse mode we want to make sure any missing
1290 * directories get created while we're still processing
1291 * the parent dir (which allows us to touch the parent
1292 * dir's mtime right away). We will handle the dir in
1293 * full later (right before we handle its contents). */
1294 if (statret == 0
1295 && (S_ISDIR(sx.st.st_mode)
1296 || delete_item(fname, sx.st.st_mode, "directory", del_opts) != 0))
1297 goto cleanup; /* Any errors get reported later. */
1298 if (do_mkdir(fname, file->mode & 0700) == 0)
1299 file->flags |= FLAG_DIR_CREATED;
1300 goto cleanup;
1302 /* The file to be received is a directory, so we need
1303 * to prepare appropriately. If there is already a
1304 * file of that name and it is *not* a directory, then
1305 * we need to delete it. If it doesn't exist, then
1306 * (perhaps recursively) create it. */
1307 if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1308 if (delete_item(fname, sx.st.st_mode, "directory", del_opts) != 0)
1309 goto skipping_dir_contents;
1310 statret = -1;
1312 if (dry_run && statret != 0 && missing_below < 0) {
1313 missing_below = F_DEPTH(file);
1314 missing_dir = file;
1315 dry_run++;
1317 real_ret = statret;
1318 real_sx = sx;
1319 if (file->flags & FLAG_DIR_CREATED)
1320 statret = -1;
1321 if (!preserve_perms) { /* See comment in non-dir code below. */
1322 file->mode = dest_mode(file->mode, sx.st.st_mode,
1323 dflt_perms, statret == 0);
1325 if (statret != 0 && basis_dir[0] != NULL) {
1326 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1327 itemizing, code);
1328 if (j == -2) {
1329 itemizing = 0;
1330 code = FNONE;
1331 } else if (j >= 0)
1332 statret = 1;
1334 if (itemizing && f_out != -1) {
1335 itemize(fname, file, ndx, statret, &sx,
1336 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1338 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1339 if (!relative_paths || errno != ENOENT
1340 || create_directory_path(fname) < 0
1341 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
1342 rsyserr(FERROR, errno,
1343 "recv_generator: mkdir %s failed",
1344 full_fname(fname));
1345 skipping_dir_contents:
1346 rprintf(FERROR,
1347 "*** Skipping any contents from this failed directory ***\n");
1348 missing_below = F_DEPTH(file);
1349 missing_dir = file;
1350 file->flags |= FLAG_MISSING_DIR;
1351 goto cleanup;
1354 if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
1355 && verbose && code != FNONE && f_out != -1)
1356 rprintf(code, "%s/\n", fname);
1358 /* We need to ensure that the dirs in the transfer have writable
1359 * permissions during the time we are putting files within them.
1360 * This is then fixed after the transfer is done. */
1361 #ifdef HAVE_CHMOD
1362 if (!am_root && !(file->mode & S_IWUSR) && dir_tweaking) {
1363 mode_t mode = file->mode | S_IWUSR;
1364 if (do_chmod(fname, mode) < 0) {
1365 rsyserr(FERROR, errno,
1366 "failed to modify permissions on %s",
1367 full_fname(fname));
1369 need_retouch_dir_perms = 1;
1371 #endif
1373 if (real_ret != 0 && one_file_system)
1374 real_sx.st.st_dev = filesystem_dev;
1375 if (inc_recurse) {
1376 if (one_file_system) {
1377 uint32 *devp = F_DIR_DEV_P(file);
1378 DEV_MAJOR(devp) = major(real_sx.st.st_dev);
1379 DEV_MINOR(devp) = minor(real_sx.st.st_dev);
1382 else if (delete_during && f_out != -1 && !phase && dry_run < 2
1383 && (file->flags & FLAG_CONTENT_DIR))
1384 delete_in_dir(fname, file, &real_sx.st.st_dev);
1385 goto cleanup;
1388 /* If we're not preserving permissions, change the file-list's
1389 * mode based on the local permissions and some heuristics. */
1390 if (!preserve_perms) {
1391 int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
1392 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
1393 exists);
1396 #ifdef SUPPORT_HARD_LINKS
1397 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1398 && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
1399 goto cleanup;
1400 #endif
1402 if (preserve_links && S_ISLNK(file->mode)) {
1403 #ifdef SUPPORT_LINKS
1404 const char *sl = F_SYMLINK(file);
1405 if (safe_symlinks && unsafe_symlink(sl, fname)) {
1406 if (verbose) {
1407 if (solo_file)
1408 fname = f_name(file, NULL);
1409 rprintf(FINFO,
1410 "ignoring unsafe symlink %s -> \"%s\"\n",
1411 full_fname(fname), sl);
1413 return;
1415 if (statret == 0) {
1416 char lnk[MAXPATHLEN];
1417 int len;
1419 if (!S_ISLNK(sx.st.st_mode))
1420 statret = -1;
1421 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1422 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1423 /* The link is pointing to the right place. */
1424 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1425 if (itemizing)
1426 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1427 #ifdef SUPPORT_HARD_LINKS
1428 if (preserve_hard_links && F_IS_HLINKED(file))
1429 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1430 #endif
1431 if (remove_source_files == 1)
1432 goto return_with_success;
1433 goto cleanup;
1435 /* Not the right symlink (or not a symlink), so
1436 * delete it. */
1437 if (delete_item(fname, sx.st.st_mode, "symlink", del_opts) != 0)
1438 goto cleanup;
1439 } else if (basis_dir[0] != NULL) {
1440 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1441 itemizing, code);
1442 if (j == -2) {
1443 #ifndef CAN_HARDLINK_SYMLINK
1444 if (link_dest) {
1445 /* Resort to --copy-dest behavior. */
1446 } else
1447 #endif
1448 if (!copy_dest)
1449 goto cleanup;
1450 itemizing = 0;
1451 code = FNONE;
1452 } else if (j >= 0)
1453 statret = 1;
1455 #ifdef SUPPORT_HARD_LINKS
1456 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1457 cur_flist->in_progress++;
1458 goto cleanup;
1460 #endif
1461 if (do_symlink(sl, fname) != 0) {
1462 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
1463 full_fname(fname), sl);
1464 } else {
1465 set_file_attrs(fname, file, NULL, NULL, 0);
1466 if (itemizing) {
1467 itemize(fname, file, ndx, statret, &sx,
1468 ITEM_LOCAL_CHANGE, 0, NULL);
1470 if (code != FNONE && verbose)
1471 rprintf(code, "%s -> %s\n", fname, sl);
1472 #ifdef SUPPORT_HARD_LINKS
1473 if (preserve_hard_links && F_IS_HLINKED(file))
1474 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1475 #endif
1476 /* This does not check remove_source_files == 1
1477 * because this is one of the items that the old
1478 * --remove-sent-files option would remove. */
1479 if (remove_source_files)
1480 goto return_with_success;
1482 #endif
1483 goto cleanup;
1486 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1487 || (preserve_specials && IS_SPECIAL(file->mode))) {
1488 uint32 *devp = F_RDEV_P(file);
1489 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1490 if (statret == 0) {
1491 char *t;
1492 if (IS_DEVICE(file->mode)) {
1493 if (!IS_DEVICE(sx.st.st_mode))
1494 statret = -1;
1495 t = "device file";
1496 } else {
1497 if (!IS_SPECIAL(sx.st.st_mode))
1498 statret = -1;
1499 t = "special file";
1501 if (statret == 0
1502 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
1503 && sx.st.st_rdev == rdev) {
1504 /* The device or special file is identical. */
1505 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1506 if (itemizing)
1507 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1508 #ifdef SUPPORT_HARD_LINKS
1509 if (preserve_hard_links && F_IS_HLINKED(file))
1510 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1511 #endif
1512 if (remove_source_files == 1)
1513 goto return_with_success;
1514 goto cleanup;
1516 if (delete_item(fname, sx.st.st_mode, t, del_opts) != 0)
1517 goto cleanup;
1518 } else if (basis_dir[0] != NULL) {
1519 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1520 itemizing, code);
1521 if (j == -2) {
1522 #ifndef CAN_HARDLINK_SPECIAL
1523 if (link_dest) {
1524 /* Resort to --copy-dest behavior. */
1525 } else
1526 #endif
1527 if (!copy_dest)
1528 goto cleanup;
1529 itemizing = 0;
1530 code = FNONE;
1531 } else if (j >= 0)
1532 statret = 1;
1534 #ifdef SUPPORT_HARD_LINKS
1535 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1536 cur_flist->in_progress++;
1537 goto cleanup;
1539 #endif
1540 if (verbose > 2) {
1541 rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1542 fname, (int)file->mode,
1543 (long)major(rdev), (long)minor(rdev));
1545 if (do_mknod(fname, file->mode, rdev) < 0) {
1546 rsyserr(FERROR, errno, "mknod %s failed",
1547 full_fname(fname));
1548 } else {
1549 set_file_attrs(fname, file, NULL, NULL, 0);
1550 if (itemizing) {
1551 itemize(fname, file, ndx, statret, &sx,
1552 ITEM_LOCAL_CHANGE, 0, NULL);
1554 if (code != FNONE && verbose)
1555 rprintf(code, "%s\n", fname);
1556 #ifdef SUPPORT_HARD_LINKS
1557 if (preserve_hard_links && F_IS_HLINKED(file))
1558 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1559 #endif
1560 if (remove_source_files == 1)
1561 goto return_with_success;
1563 goto cleanup;
1566 if (!S_ISREG(file->mode)) {
1567 if (solo_file)
1568 fname = f_name(file, NULL);
1569 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1570 goto cleanup;
1573 if (max_size > 0 && F_LENGTH(file) > max_size) {
1574 if (verbose > 1) {
1575 if (solo_file)
1576 fname = f_name(file, NULL);
1577 rprintf(FINFO, "%s is over max-size\n", fname);
1579 goto cleanup;
1581 if (min_size > 0 && F_LENGTH(file) < min_size) {
1582 if (verbose > 1) {
1583 if (solo_file)
1584 fname = f_name(file, NULL);
1585 rprintf(FINFO, "%s is under min-size\n", fname);
1587 goto cleanup;
1590 if (ignore_existing > 0 && statret == 0) {
1591 if (verbose > 1)
1592 rprintf(FINFO, "%s exists\n", fname);
1593 goto cleanup;
1596 if (update_only > 0 && statret == 0
1597 && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
1598 if (verbose > 1)
1599 rprintf(FINFO, "%s is newer\n", fname);
1600 goto cleanup;
1603 fnamecmp = fname;
1604 fnamecmp_type = FNAMECMP_FNAME;
1606 if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
1607 if (delete_item(fname, sx.st.st_mode, "regular file", del_opts) != 0)
1608 goto cleanup;
1609 statret = -1;
1610 stat_errno = ENOENT;
1613 if (statret != 0 && basis_dir[0] != NULL) {
1614 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
1615 itemizing, code);
1616 if (j == -2) {
1617 if (remove_source_files == 1)
1618 goto return_with_success;
1619 goto cleanup;
1621 if (j >= 0) {
1622 fnamecmp = fnamecmpbuf;
1623 fnamecmp_type = j;
1624 statret = 0;
1628 real_ret = statret;
1629 real_sx = sx;
1631 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1632 && link_stat(partialptr, &partial_st, 0) == 0
1633 && S_ISREG(partial_st.st_mode)) {
1634 if (statret != 0)
1635 goto prepare_to_open;
1636 } else
1637 partialptr = NULL;
1639 if (statret != 0 && fuzzy_dirlist && dry_run <= 1) {
1640 int j = find_fuzzy(file, fuzzy_dirlist);
1641 if (j >= 0) {
1642 fuzzy_file = fuzzy_dirlist->files[j];
1643 f_name(fuzzy_file, fnamecmpbuf);
1644 if (verbose > 2) {
1645 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1646 fname, fnamecmpbuf);
1648 sx.st.st_size = F_LENGTH(fuzzy_file);
1649 statret = 0;
1650 fnamecmp = fnamecmpbuf;
1651 fnamecmp_type = FNAMECMP_FUZZY;
1655 if (statret != 0) {
1656 #ifdef SUPPORT_HARD_LINKS
1657 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1658 cur_flist->in_progress++;
1659 goto cleanup;
1661 #endif
1662 if (stat_errno == ENOENT)
1663 goto notify_others;
1664 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
1665 full_fname(fname));
1666 goto cleanup;
1669 if (append_mode > 0 && sx.st.st_size >= F_LENGTH(file))
1670 goto cleanup;
1672 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1674 else if (fnamecmp_type == FNAMECMP_FUZZY)
1676 else if (unchanged_file(fnamecmp, file, &sx.st)) {
1677 if (partialptr) {
1678 do_unlink(partialptr);
1679 handle_partial_dir(partialptr, PDIR_DELETE);
1681 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1682 if (itemizing)
1683 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
1684 #ifdef SUPPORT_HARD_LINKS
1685 if (preserve_hard_links && F_IS_HLINKED(file))
1686 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1687 #endif
1688 if (remove_source_files != 1)
1689 goto cleanup;
1690 return_with_success:
1691 if (!dry_run)
1692 send_msg_int(MSG_SUCCESS, ndx);
1693 goto cleanup;
1696 prepare_to_open:
1697 if (partialptr) {
1698 sx.st = partial_st;
1699 fnamecmp = partialptr;
1700 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1701 statret = 0;
1704 if (!do_xfers)
1705 goto notify_others;
1707 if (read_batch || whole_file) {
1708 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1709 if (!(backupptr = get_backup_name(fname)))
1710 goto cleanup;
1711 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
1712 goto pretend_missing;
1713 if (copy_file(fname, backupptr, -1, back_file->mode, 1) < 0) {
1714 unmake_file(back_file);
1715 back_file = NULL;
1716 goto cleanup;
1719 goto notify_others;
1722 if (fuzzy_dirlist) {
1723 int j = flist_find(fuzzy_dirlist, file);
1724 if (j >= 0) /* don't use changing file as future fuzzy basis */
1725 fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
1728 /* open the file */
1729 if ((fd = do_open(fnamecmp, O_RDONLY, 0)) < 0) {
1730 rsyserr(FERROR, errno, "failed to open %s, continuing",
1731 full_fname(fnamecmp));
1732 pretend_missing:
1733 /* pretend the file didn't exist */
1734 #ifdef SUPPORT_HARD_LINKS
1735 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1736 cur_flist->in_progress++;
1737 goto cleanup;
1739 #endif
1740 statret = real_ret = -1;
1741 goto notify_others;
1744 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1745 if (!(backupptr = get_backup_name(fname))) {
1746 close(fd);
1747 goto cleanup;
1749 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1750 close(fd);
1751 goto pretend_missing;
1753 if (robust_unlink(backupptr) && errno != ENOENT) {
1754 rsyserr(FERROR, errno, "unlink %s",
1755 full_fname(backupptr));
1756 unmake_file(back_file);
1757 back_file = NULL;
1758 close(fd);
1759 goto cleanup;
1761 if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0
1762 && (errno != ENOENT || make_bak_dir(backupptr) < 0
1763 || (f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0)) {
1764 rsyserr(FERROR, errno, "open %s",
1765 full_fname(backupptr));
1766 unmake_file(back_file);
1767 back_file = NULL;
1768 close(fd);
1769 goto cleanup;
1771 fnamecmp_type = FNAMECMP_BACKUP;
1774 if (verbose > 3) {
1775 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1776 fnamecmp, (double)sx.st.st_size);
1779 if (verbose > 2)
1780 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1782 notify_others:
1783 if (remove_source_files && !delay_updates && !phase)
1784 increment_active_files(ndx, itemizing, code);
1785 if (inc_recurse && !dry_run)
1786 cur_flist->in_progress++;
1787 #ifdef SUPPORT_HARD_LINKS
1788 if (preserve_hard_links && F_IS_HLINKED(file))
1789 file->flags |= FLAG_FILE_SENT;
1790 #endif
1791 write_ndx(f_out, ndx);
1792 if (itemizing) {
1793 int iflags = ITEM_TRANSFER;
1794 if (always_checksum > 0)
1795 iflags |= ITEM_REPORT_CHECKSUM;
1796 if (fnamecmp_type != FNAMECMP_FNAME)
1797 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1798 if (fnamecmp_type == FNAMECMP_FUZZY)
1799 iflags |= ITEM_XNAME_FOLLOWS;
1800 itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
1801 fuzzy_file ? fuzzy_file->basename : NULL);
1802 #ifdef SUPPORT_ACLS
1803 if (preserve_acls)
1804 free_acl(&real_sx);
1805 #endif
1806 #ifdef SUPPORT_XATTRS
1807 if (preserve_xattrs)
1808 free_xattr(&real_sx);
1809 #endif
1812 if (!do_xfers) {
1813 #ifdef SUPPORT_HARD_LINKS
1814 if (preserve_hard_links && F_IS_HLINKED(file))
1815 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1816 #endif
1817 goto cleanup;
1819 if (read_batch)
1820 goto cleanup;
1822 if (statret != 0 || whole_file)
1823 write_sum_head(f_out, NULL);
1824 else {
1825 generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy);
1826 close(fd);
1829 cleanup:
1830 if (back_file) {
1831 if (f_copy >= 0)
1832 close(f_copy);
1833 set_file_attrs(backupptr, back_file, NULL, NULL, 0);
1834 if (verbose > 1) {
1835 rprintf(FINFO, "backed up %s to %s\n",
1836 fname, backupptr);
1838 unmake_file(back_file);
1841 #ifdef SUPPORT_ACLS
1842 if (preserve_acls)
1843 free_acl(&sx);
1844 #endif
1845 #ifdef SUPPORT_XATTRS
1846 if (preserve_xattrs)
1847 free_xattr(&sx);
1848 #endif
1849 return;
1852 static void touch_up_dirs(struct file_list *flist, int ndx)
1854 static int counter = 0;
1855 struct file_struct *file;
1856 char *fname;
1857 int i, start, end;
1859 if (ndx < 0) {
1860 start = 0;
1861 end = flist->used - 1;
1862 } else
1863 start = end = ndx;
1865 /* Fix any directory permissions that were modified during the
1866 * transfer and/or re-set any tweaked modified-time values. */
1867 for (i = start; i <= end; i++, counter++) {
1868 file = flist->files[i];
1869 if (!S_ISDIR(file->mode)
1870 || (!implied_dirs && file->flags & FLAG_IMPLIED_DIR))
1871 continue;
1872 if (verbose > 3) {
1873 fname = f_name(file, NULL);
1874 rprintf(FINFO, "touch_up_dirs: %s (%d)\n",
1875 NS(fname), i);
1877 if (!F_IS_ACTIVE(file) || file->flags & FLAG_MISSING_DIR
1878 || (!need_retouch_dir_times && file->mode & S_IWUSR))
1879 continue;
1880 fname = f_name(file, NULL);
1881 if (!(file->mode & S_IWUSR))
1882 do_chmod(fname, file->mode);
1883 if (need_retouch_dir_times)
1884 set_modtime(fname, file->modtime, file->mode);
1885 if (allowed_lull && !(counter % lull_mod))
1886 maybe_send_keepalive();
1887 else if (!(counter & 0xFF))
1888 maybe_flush_socket(0);
1892 void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
1894 struct file_struct *file;
1895 struct file_list *flist;
1896 char fbuf[MAXPATHLEN];
1897 int ndx;
1899 #ifdef SUPPORT_HARD_LINKS
1900 while (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
1901 flist = flist_for_ndx(ndx);
1902 assert(flist != NULL);
1903 file = flist->files[ndx - flist->ndx_start];
1904 assert(file->flags & FLAG_HLINKED);
1905 finish_hard_link(file, f_name(file, fbuf), ndx, NULL, itemizing, code, -1);
1906 flist->in_progress--;
1908 #endif
1910 while (check_redo && (ndx = get_redo_num()) != -1) {
1911 csum_length = SUM_LENGTH;
1912 max_size = -max_size;
1913 min_size = -min_size;
1914 ignore_existing = -ignore_existing;
1915 ignore_non_existing = -ignore_non_existing;
1916 update_only = -update_only;
1917 always_checksum = -always_checksum;
1918 size_only = -size_only;
1919 append_mode = -append_mode;
1920 make_backups = -make_backups; /* avoid dup backup w/inplace */
1921 ignore_times++;
1923 flist = cur_flist;
1924 cur_flist = flist_for_ndx(ndx);
1926 file = cur_flist->files[ndx - cur_flist->ndx_start];
1927 if (solo_file)
1928 strlcpy(fbuf, solo_file, sizeof fbuf);
1929 else
1930 f_name(file, fbuf);
1931 recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
1932 cur_flist->to_redo--;
1934 cur_flist = flist;
1936 csum_length = SHORT_SUM_LENGTH;
1937 max_size = -max_size;
1938 min_size = -min_size;
1939 ignore_existing = -ignore_existing;
1940 ignore_non_existing = -ignore_non_existing;
1941 update_only = -update_only;
1942 always_checksum = -always_checksum;
1943 size_only = -size_only;
1944 append_mode = -append_mode;
1945 make_backups = -make_backups;
1946 ignore_times--;
1949 while (cur_flist != first_flist) { /* only possible with inc_recurse */
1950 if (first_flist->in_progress || first_flist->to_redo)
1951 break;
1953 if (!read_batch) {
1954 write_ndx(sock_f_out, NDX_DONE);
1955 maybe_flush_socket(1);
1958 if (delete_during == 2 || !dir_tweaking) {
1959 /* Skip directory touch-up. */
1960 } else if (first_flist->ndx_start != 0)
1961 touch_up_dirs(dir_flist, first_flist->parent_ndx);
1963 flist_free(first_flist); /* updates first_flist */
1967 void generate_files(int f_out, const char *local_name)
1969 int i, ndx;
1970 char fbuf[MAXPATHLEN];
1971 int itemizing;
1972 enum logcode code;
1973 int save_do_progress = do_progress;
1975 if (protocol_version >= 29) {
1976 itemizing = 1;
1977 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1978 code = logfile_format_has_i ? FNONE : FLOG;
1979 } else if (am_daemon) {
1980 itemizing = logfile_format_has_i && do_xfers;
1981 maybe_ATTRS_REPORT = ATTRS_REPORT;
1982 code = itemizing || !do_xfers ? FCLIENT : FINFO;
1983 } else if (!am_server) {
1984 itemizing = stdout_format_has_i;
1985 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1986 code = itemizing ? FNONE : FINFO;
1987 } else {
1988 itemizing = 0;
1989 maybe_ATTRS_REPORT = ATTRS_REPORT;
1990 code = FINFO;
1992 solo_file = local_name;
1993 dir_tweaking = !(list_only || solo_file || dry_run);
1994 need_retouch_dir_times = preserve_times > 1;
1995 lull_mod = allowed_lull * 5;
1997 if (verbose > 2)
1998 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
2000 if (delete_before && !solo_file && cur_flist->used > 0)
2001 do_delete_pass();
2002 if (delete_during == 2) {
2003 deldelay_size = BIGPATHBUFLEN * 4;
2004 deldelay_buf = new_array(char, deldelay_size);
2005 if (!deldelay_buf)
2006 out_of_memory("delete-delay");
2008 do_progress = 0;
2010 if (append_mode > 0 || whole_file < 0)
2011 whole_file = 0;
2012 if (verbose >= 2) {
2013 rprintf(FINFO, "delta-transmission %s\n",
2014 whole_file
2015 ? "disabled for local transfer or --whole-file"
2016 : "enabled");
2019 /* Since we often fill up the outgoing socket and then just sit around
2020 * waiting for the other 2 processes to do their thing, we don't want
2021 * to exit on a timeout. If the data stops flowing, the receiver will
2022 * notice that and let us know via the redo pipe (or its closing). */
2023 ignore_timeout = 1;
2025 dflt_perms = (ACCESSPERMS & ~orig_umask);
2027 do {
2028 #ifdef SUPPORT_HARD_LINKS
2029 if (preserve_hard_links && inc_recurse) {
2030 while (!flist_eof && file_total < FILECNT_LOOKAHEAD/2)
2031 wait_for_receiver();
2033 #endif
2035 if (inc_recurse && cur_flist->ndx_start) {
2036 struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
2037 f_name(fp, fbuf);
2038 ndx = cur_flist->ndx_start - 1;
2039 recv_generator(fbuf, fp, ndx, itemizing, code, f_out);
2040 if (delete_during && dry_run < 2) {
2041 if (BITS_SETnUNSET(fp->flags, FLAG_CONTENT_DIR, FLAG_MISSING_DIR)) {
2042 dev_t dirdev;
2043 if (one_file_system) {
2044 uint32 *devp = F_DIR_DEV_P(fp);
2045 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
2046 } else
2047 dirdev = MAKEDEV(0, 0);
2048 delete_in_dir(f_name(fp, fbuf), fp, &dirdev);
2052 for (i = cur_flist->low; i <= cur_flist->high; i++) {
2053 struct file_struct *file = cur_flist->sorted[i];
2055 if (!F_IS_ACTIVE(file))
2056 continue;
2058 #ifdef ICONV_OPTION
2059 if (ic_ndx)
2060 ndx = F_NDX(file);
2061 else
2062 #endif
2063 ndx = i + cur_flist->ndx_start;
2065 if (solo_file)
2066 strlcpy(fbuf, solo_file, sizeof fbuf);
2067 else
2068 f_name(file, fbuf);
2069 recv_generator(fbuf, file, ndx, itemizing, code, f_out);
2071 check_for_finished_files(itemizing, code, 0);
2073 if (allowed_lull && !(i % lull_mod))
2074 maybe_send_keepalive();
2075 else if (!(i & 0xFF))
2076 maybe_flush_socket(0);
2079 if (!inc_recurse) {
2080 write_ndx(f_out, NDX_DONE);
2081 break;
2084 while (1) {
2085 check_for_finished_files(itemizing, code, 1);
2086 if (cur_flist->next || flist_eof)
2087 break;
2088 wait_for_receiver();
2090 } while ((cur_flist = cur_flist->next) != NULL);
2092 if (delete_during)
2093 delete_in_dir(NULL, NULL, &dev_zero);
2094 phase++;
2095 if (verbose > 2)
2096 rprintf(FINFO, "generate_files phase=%d\n", phase);
2098 while (1) {
2099 check_for_finished_files(itemizing, code, 1);
2100 if (msgdone_cnt)
2101 break;
2102 wait_for_receiver();
2105 phase++;
2106 if (verbose > 2)
2107 rprintf(FINFO, "generate_files phase=%d\n", phase);
2109 write_ndx(f_out, NDX_DONE);
2110 /* Reduce round-trip lag-time for a useless delay-updates phase. */
2111 if (protocol_version >= 29 && !delay_updates)
2112 write_ndx(f_out, NDX_DONE);
2114 /* Read MSG_DONE for the redo phase (and any prior messages). */
2115 while (1) {
2116 check_for_finished_files(itemizing, code, 0);
2117 if (msgdone_cnt > 1)
2118 break;
2119 wait_for_receiver();
2122 if (protocol_version >= 29) {
2123 phase++;
2124 if (verbose > 2)
2125 rprintf(FINFO, "generate_files phase=%d\n", phase);
2126 if (delay_updates)
2127 write_ndx(f_out, NDX_DONE);
2128 /* Read MSG_DONE for delay-updates phase & prior messages. */
2129 while (msgdone_cnt == 2)
2130 wait_for_receiver();
2133 do_progress = save_do_progress;
2134 if (delete_during == 2)
2135 do_delayed_deletions(fbuf);
2136 if (delete_after && !solo_file && file_total > 0)
2137 do_delete_pass();
2139 if ((need_retouch_dir_perms || need_retouch_dir_times)
2140 && dir_tweaking && (!inc_recurse || delete_during == 2))
2141 touch_up_dirs(dir_flist, -1);
2143 if (max_delete >= 0 && deletion_count > max_delete) {
2144 rprintf(FINFO,
2145 "Deletions stopped due to --max-delete limit (%d skipped)\n",
2146 deletion_count - max_delete);
2147 io_error |= IOERR_DEL_LIMIT;
2150 if (verbose > 2)
2151 rprintf(FINFO, "generate_files finished\n");