1 /*
2 * Routines that are exclusive to the generator process.
3 *
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-2020 Wayne Davison
8 *
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.
13 *
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.
18 *
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.
21 */
22
23 #include "rsync.h"
24 #include "inums.h"
25 #include "ifuncs.h"
26
27 extern int dry_run;
28 extern int do_xfers;
29 extern int stdout_format_has_i;
30 extern int logfile_format_has_i;
31 extern int am_root;
32 extern int am_server;
33 extern int am_daemon;
34 extern int inc_recurse;
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 write_devices;
43 extern int preserve_specials;
44 extern int preserve_hard_links;
45 extern int preserve_executability;
46 extern int preserve_fileflags;
47 extern int preserve_perms;
48 extern int preserve_times;
49 extern int force_change;
50 extern int delete_mode;
51 extern int delete_before;
52 extern int delete_during;
53 extern int delete_after;
54 extern int missing_args;
55 extern int msgdone_cnt;
56 extern int ignore_errors;
57 extern int remove_source_files;
58 extern int delay_updates;
59 extern int update_only;
60 extern int human_readable;
61 extern int ignore_existing;
62 extern int ignore_non_existing;
63 extern int want_xattr_optim;
64 extern int modify_window;
65 extern int inplace;
66 extern int append_mode;
67 extern int make_backups;
68 extern int csum_length;
69 extern int ignore_times;
70 extern int size_only;
71 extern OFF_T max_size;
72 extern OFF_T min_size;
73 extern int io_error;
74 extern int flist_eof;
75 extern int allowed_lull;
76 extern int sock_f_out;
77 extern int protocol_version;
78 extern int file_total;
79 extern int fuzzy_basis;
80 extern int always_checksum;
81 extern int flist_csum_len;
82 extern char *partial_dir;
83 extern int alt_dest_type;
84 extern int whole_file;
85 extern int list_only;
86 extern int read_batch;
87 extern int write_batch;
88 extern int safe_symlinks;
89 extern int32 block_size;
90 extern int unsort_ndx;
91 extern int max_delete;
92 extern int force_delete;
93 extern int one_file_system;
94 extern int skipped_deletes;
95 extern dev_t filesystem_dev;
96 extern mode_t orig_umask;
97 extern uid_t our_uid;
98 extern char *tmpdir;
99 extern char *basis_dir[MAX_BASIS_DIRS+1];
100 extern struct file_list *cur_flist, *first_flist, *dir_flist;
101 extern filter_rule_list filter_list, daemon_filter_list;
102
103 int maybe_ATTRS_REPORT = 0;
104 int maybe_ATTRS_ACCURATE_TIME = 0;
105
106 static dev_t dev_zero;
107 static int deldelay_size = 0, deldelay_cnt = 0;
108 static char *deldelay_buf = NULL;
109 static int deldelay_fd = -1;
110 static int loopchk_limit;
111 static int dir_tweaking;
112 static int symlink_timeset_failed_flags;
113 static int need_retouch_dir_times;
114 static int need_retouch_dir_perms;
115 static const char *solo_file = NULL;
116
117 enum nonregtype {
118 TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
119 };
120
121 /* Forward declarations. */
122 #ifdef SUPPORT_HARD_LINKS
123 static void handle_skipped_hlink(struct file_struct *file, int itemizing,
124 enum logcode code, int f_out);
125 #endif
126
127 #define EARLY_DELAY_DONE_MSG() (!delay_updates)
128 #define EARLY_DELETE_DONE_MSG() (!(delete_during == 2 || delete_after))
129
start_delete_delay_temp(void)130 static int start_delete_delay_temp(void)
131 {
132 char fnametmp[MAXPATHLEN];
133 int save_dry_run = dry_run;
134
135 dry_run = 0;
136 if (!get_tmpname(fnametmp, "deldelay", False)
137 || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
138 rprintf(FINFO, "NOTE: Unable to create delete-delay temp file%s.\n",
139 inc_recurse ? "" : " -- switching to --delete-after");
140 delete_during = 0;
141 delete_after = !inc_recurse;
142 dry_run = save_dry_run;
143 return 0;
144 }
145 unlink(fnametmp);
146 dry_run = save_dry_run;
147 return 1;
148 }
149
flush_delete_delay(void)150 static int flush_delete_delay(void)
151 {
152 if (deldelay_fd < 0 && !start_delete_delay_temp())
153 return 0;
154 if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
155 rsyserr(FERROR, errno, "flush of delete-delay buffer");
156 delete_during = 0;
157 delete_after = !inc_recurse;
158 close(deldelay_fd);
159 return 0;
160 }
161 deldelay_cnt = 0;
162 return 1;
163 }
164
remember_delete(struct file_struct * file,const char * fname,int flags)165 static int remember_delete(struct file_struct *file, const char *fname, int flags)
166 {
167 int len;
168
169 if (deldelay_cnt == deldelay_size && !flush_delete_delay())
170 return 0;
171
172 if (flags & DEL_NO_UID_WRITE)
173 deldelay_buf[deldelay_cnt++] = '!';
174
175 while (1) {
176 len = snprintf(deldelay_buf + deldelay_cnt, deldelay_size - deldelay_cnt,
177 "%x %s%c", (int)file->mode, fname, '\0');
178 if ((deldelay_cnt += len) <= deldelay_size)
179 break;
180 deldelay_cnt -= len;
181 if (!flush_delete_delay())
182 return 0;
183 }
184
185 return 1;
186 }
187
read_delay_line(char * buf,int * flags_p)188 static int read_delay_line(char *buf, int *flags_p)
189 {
190 static int read_pos = 0;
191 int j, len, mode;
192 char *bp, *past_space;
193
194 while (1) {
195 for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
196 if (j < deldelay_cnt)
197 break;
198 if (deldelay_fd < 0) {
199 if (j > read_pos)
200 goto invalid_data;
201 return -1;
202 }
203 deldelay_cnt -= read_pos;
204 if (deldelay_cnt == deldelay_size)
205 goto invalid_data;
206 if (deldelay_cnt && read_pos) {
207 memmove(deldelay_buf, deldelay_buf + read_pos,
208 deldelay_cnt);
209 }
210 len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
211 deldelay_size - deldelay_cnt);
212 if (len == 0) {
213 if (deldelay_cnt) {
214 rprintf(FERROR, "ERROR: unexpected EOF in delete-delay file.\n");
215 }
216 return -1;
217 }
218 if (len < 0) {
219 rsyserr(FERROR, errno,
220 "reading delete-delay file");
221 return -1;
222 }
223 deldelay_cnt += len;
224 read_pos = 0;
225 }
226
227 bp = deldelay_buf + read_pos;
228 if (*bp == '!') {
229 bp++;
230 *flags_p = DEL_NO_UID_WRITE;
231 } else
232 *flags_p = 0;
233
234 if (sscanf(bp, "%x ", &mode) != 1) {
235 invalid_data:
236 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
237 return -1;
238 }
239 past_space = strchr(bp, ' ') + 1;
240 len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
241 read_pos = j + 1;
242
243 if (len > MAXPATHLEN) {
244 rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
245 return -1;
246 }
247
248 /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
249 * instead of returning a pointer to our buffer. */
250 memcpy(buf, past_space, len);
251
252 return mode;
253 }
254
do_delayed_deletions(char * delbuf)255 static void do_delayed_deletions(char *delbuf)
256 {
257 int mode, flags;
258
259 if (deldelay_fd >= 0) {
260 if (deldelay_cnt && !flush_delete_delay())
261 return;
262 lseek(deldelay_fd, 0, 0);
263 }
264 while ((mode = read_delay_line(delbuf, &flags)) >= 0)
265 delete_item(delbuf, mode, flags | DEL_RECURSE);
266 if (deldelay_fd >= 0)
267 close(deldelay_fd);
268 }
269
270 /* This function is used to implement per-directory deletion, and is used by
271 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
272 * MAXPATHLEN buffer with the name of the directory in it (the functions we
273 * call will append names onto the end, but the old dir value will be restored
274 * on exit). */
delete_in_dir(char * fbuf,struct file_struct * file,dev_t * fs_dev)275 static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
276 {
277 static int already_warned = 0;
278 static struct hashtable *dev_tbl;
279 struct file_list *dirlist;
280 char delbuf[MAXPATHLEN];
281 int dlen, i;
282
283 if (!fbuf) {
284 change_local_filter_dir(NULL, 0, 0);
285 return;
286 }
287
288 if (DEBUG_GTE(DEL, 2))
289 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
290
291 if (allowed_lull)
292 maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
293
294 if (io_error & IOERR_GENERAL && !ignore_errors) {
295 if (already_warned)
296 return;
297 rprintf(FINFO,
298 "IO error encountered -- skipping file deletion\n");
299 already_warned = 1;
300 return;
301 }
302
303 dlen = strlen(fbuf);
304 change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
305
306 if (one_file_system) {
307 if (!dev_tbl)
308 dev_tbl = hashtable_create(16, HT_KEY64);
309 if (file->flags & FLAG_TOP_DIR) {
310 hashtable_find(dev_tbl, *fs_dev+1, "");
311 filesystem_dev = *fs_dev;
312 } else if (filesystem_dev != *fs_dev) {
313 if (!hashtable_find(dev_tbl, *fs_dev+1, NULL))
314 return;
315 filesystem_dev = *fs_dev; /* it's a prior top-dir dev */
316 }
317 }
318
319 dirlist = get_dirlist(fbuf, dlen, 0);
320
321 /* If an item in dirlist is not found in flist, delete it
322 * from the filesystem. */
323 for (i = dirlist->used; i--; ) {
324 struct file_struct *fp = dirlist->files[i];
325 if (!F_IS_ACTIVE(fp))
326 continue;
327 if (fp->flags & FLAG_MOUNT_DIR && S_ISDIR(fp->mode)) {
328 if (INFO_GTE(MOUNT, 1))
329 rprintf(FINFO, "cannot delete mount point: %s\n",
330 f_name(fp, NULL));
331 continue;
332 }
333 /* Here we want to match regardless of file type. Replacement
334 * of a file with one of another type is handled separately by
335 * a delete_item call with a DEL_MAKE_ROOM flag. */
336 if (flist_find_ignore_dirness(cur_flist, fp) < 0) {
337 int flags = DEL_RECURSE;
338 if (!(fp->mode & S_IWUSR) && !am_root && fp->flags & FLAG_OWNED_BY_US)
339 flags |= DEL_NO_UID_WRITE;
340 f_name(fp, delbuf);
341 if (delete_during == 2) {
342 if (!remember_delete(fp, delbuf, flags))
343 break;
344 } else
345 delete_item(delbuf, fp->mode, flags);
346 }
347 }
348
349 flist_free(dirlist);
350 }
351
352 /* This deletes any files on the receiving side that are not present on the
353 * sending side. This is used by --delete-before and --delete-after. */
do_delete_pass(void)354 static void do_delete_pass(void)
355 {
356 char fbuf[MAXPATHLEN];
357 STRUCT_STAT st;
358 int j;
359
360 /* dry_run is incremented when the destination doesn't exist yet. */
361 if (dry_run > 1 || list_only)
362 return;
363
364 for (j = 0; j < cur_flist->used; j++) {
365 struct file_struct *file = cur_flist->sorted[j];
366
367 if (!F_IS_ACTIVE(file))
368 continue;
369
370 f_name(file, fbuf);
371
372 if (!(file->flags & FLAG_CONTENT_DIR)) {
373 change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(file));
374 continue;
375 }
376
377 if (DEBUG_GTE(DEL, 1) && file->flags & FLAG_TOP_DIR)
378 rprintf(FINFO, "deleting in %s\n", fbuf);
379
380 if (link_stat(fbuf, &st, keep_dirlinks) < 0
381 || !S_ISDIR(st.st_mode))
382 continue;
383
384 delete_in_dir(fbuf, file, &st.st_dev);
385 }
386 delete_in_dir(NULL, NULL, &dev_zero);
387
388 if (INFO_GTE(FLIST, 2) && !am_server)
389 rprintf(FINFO, " \r");
390 }
391
mtime_differs(STRUCT_STAT * stp,struct file_struct * file)392 static inline int mtime_differs(STRUCT_STAT *stp, struct file_struct *file)
393 {
394 #ifdef ST_MTIME_NSEC
395 return !same_time(stp->st_mtime, stp->ST_MTIME_NSEC, file->modtime, F_MOD_NSEC_or_0(file));
396 #else
397 return !same_time(stp->st_mtime, 0, file->modtime, 0);
398 #endif
399 }
400
any_time_differs(stat_x * sxp,struct file_struct * file,UNUSED (const char * fname))401 static inline int any_time_differs(stat_x *sxp, struct file_struct *file, UNUSED(const char *fname))
402 {
403 int differs = mtime_differs(&sxp->st, file);
404 #ifdef SUPPORT_CRTIMES
405 if (!differs && crtimes_ndx) {
406 if (sxp->crtime == 0)
407 sxp->crtime = get_create_time(fname);
408 differs = !same_time(sxp->crtime, 0, F_CRTIME(file), 0);
409 }
410 #endif
411 return differs;
412 }
413
perms_differ(struct file_struct * file,stat_x * sxp)414 static inline int perms_differ(struct file_struct *file, stat_x *sxp)
415 {
416 if (preserve_perms)
417 return !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS);
418
419 if (preserve_executability)
420 return (sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0);
421
422 return 0;
423 }
424
ownership_differs(struct file_struct * file,stat_x * sxp)425 static inline int ownership_differs(struct file_struct *file, stat_x *sxp)
426 {
427 if (am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file))
428 return 1;
429
430 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
431 return 1;
432
433 return 0;
434 }
435
436 #ifdef SUPPORT_ACLS
acls_differ(const char * fname,struct file_struct * file,stat_x * sxp)437 static inline int acls_differ(const char *fname, struct file_struct *file, stat_x *sxp)
438 {
439 if (preserve_acls) {
440 if (!ACL_READY(*sxp))
441 get_acl(fname, sxp);
442 if (set_acl(NULL, file, sxp, file->mode))
443 return 1;
444 }
445
446 return 0;
447 }
448 #endif
449
450 #ifdef SUPPORT_XATTRS
xattrs_differ(const char * fname,struct file_struct * file,stat_x * sxp)451 static inline int xattrs_differ(const char *fname, struct file_struct *file, stat_x *sxp)
452 {
453 if (preserve_xattrs) {
454 if (!XATTR_READY(*sxp))
455 get_xattr(fname, sxp);
456 if (xattr_diff(file, sxp, 0))
457 return 1;
458 }
459
460 return 0;
461 }
462 #endif
463
unchanged_attrs(const char * fname,struct file_struct * file,stat_x * sxp)464 int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
465 {
466 if (S_ISLNK(file->mode)) {
467 #ifdef CAN_SET_SYMLINK_TIMES
468 if (preserve_times & PRESERVE_LINK_TIMES && any_time_differs(sxp, file, fname))
469 return 0;
470 #endif
471 #ifdef CAN_CHMOD_SYMLINK
472 if (perms_differ(file, sxp))
473 return 0;
474 #endif
475 #ifdef CAN_CHOWN_SYMLINK
476 if (ownership_differs(file, sxp))
477 return 0;
478 #endif
479 #if defined SUPPORT_ACLS && 0 /* no current symlink-ACL support */
480 if (acls_differ(fname, file, sxp))
481 return 0;
482 #endif
483 #if defined SUPPORT_XATTRS && !defined NO_SYMLINK_XATTRS
484 if (xattrs_differ(fname, file, sxp))
485 return 0;
486 #endif
487 } else {
488 if (preserve_times && any_time_differs(sxp, file, fname))
489 return 0;
490 if (perms_differ(file, sxp))
491 return 0;
492 #ifdef SUPPORT_FILEFLAGS
493 if (preserve_fileflags && sxp->st.st_flags != F_FFLAGS(file))
494 return 0;
495 #endif
496 if (ownership_differs(file, sxp))
497 return 0;
498 #ifdef SUPPORT_ACLS
499 if (acls_differ(fname, file, sxp))
500 return 0;
501 #endif
502 #ifdef SUPPORT_XATTRS
503 if (xattrs_differ(fname, file, sxp))
504 return 0;
505 #endif
506 }
507
508 return 1;
509 }
510
itemize(const char * fnamecmp,struct file_struct * file,int ndx,int statret,stat_x * sxp,int32 iflags,uchar fnamecmp_type,const char * xname)511 void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
512 stat_x *sxp, int32 iflags, uchar fnamecmp_type,
513 const char *xname)
514 {
515 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
516 int keep_time = !preserve_times ? 0
517 : S_ISDIR(file->mode) ? preserve_times & PRESERVE_DIR_TIMES
518 : S_ISLNK(file->mode) ? preserve_times & PRESERVE_LINK_TIMES
519 : 1;
520
521 if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
522 iflags |= ITEM_REPORT_SIZE;
523 if (file->flags & FLAG_TIME_FAILED) { /* symlinks only */
524 if (iflags & ITEM_LOCAL_CHANGE)
525 iflags |= symlink_timeset_failed_flags;
526 } else if (keep_time
527 ? mtime_differs(&sxp->st, file)
528 : iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !(iflags & ITEM_MATCHED)
529 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
530 iflags |= ITEM_REPORT_TIME;
531 if (atimes_ndx && !S_ISDIR(file->mode) && !S_ISLNK(file->mode)
532 && !same_time(F_ATIME(file), 0, sxp->st.st_atime, 0))
533 iflags |= ITEM_REPORT_ATIME;
534 #ifdef SUPPORT_CRTIMES
535 if (crtimes_ndx) {
536 if (sxp->crtime == 0)
537 sxp->crtime = get_create_time(fnamecmp);
538 if (!same_time(sxp->crtime, 0, F_CRTIME(file), 0))
539 iflags |= ITEM_REPORT_CRTIME;
540 }
541 #endif
542 #if !defined HAVE_LCHMOD && !defined HAVE_SETATTRLIST
543 if (S_ISLNK(file->mode)) {
544 ;
545 } else
546 #endif
547 if (preserve_perms) {
548 if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
549 iflags |= ITEM_REPORT_PERMS;
550 } else if (preserve_executability
551 && ((sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0)))
552 iflags |= ITEM_REPORT_PERMS;
553 if (uid_ndx && am_root && (uid_t)F_OWNER(file) != sxp->st.st_uid)
554 iflags |= ITEM_REPORT_OWNER;
555 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
556 iflags |= ITEM_REPORT_GROUP;
557 #ifdef SUPPORT_FILEFLAGS
558 if (preserve_fileflags && !S_ISLNK(file->mode)
559 && sxp->st.st_flags != F_FFLAGS(file))
560 iflags |= ITEM_REPORT_FFLAGS;
561 #endif
562 #ifdef SUPPORT_ACLS
563 if (preserve_acls && !S_ISLNK(file->mode)) {
564 if (!ACL_READY(*sxp))
565 get_acl(fnamecmp, sxp);
566 if (set_acl(NULL, file, sxp, file->mode))
567 iflags |= ITEM_REPORT_ACL;
568 }
569 #endif
570 #ifdef SUPPORT_XATTRS
571 if (preserve_xattrs) {
572 if (!XATTR_READY(*sxp))
573 get_xattr(fnamecmp, sxp);
574 if (xattr_diff(file, sxp, 1))
575 iflags |= ITEM_REPORT_XATTR;
576 }
577 #endif
578 } else {
579 #ifdef SUPPORT_XATTRS
580 if (preserve_xattrs && xattr_diff(file, NULL, 1))
581 iflags |= ITEM_REPORT_XATTR;
582 #endif
583 iflags |= ITEM_IS_NEW;
584 }
585
586 iflags &= 0xffff;
587 if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || INFO_GTE(NAME, 2)
588 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
589 if (protocol_version >= 29) {
590 if (ndx >= 0)
591 write_ndx(sock_f_out, ndx);
592 write_shortint(sock_f_out, iflags);
593 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
594 write_byte(sock_f_out, fnamecmp_type);
595 if (iflags & ITEM_XNAME_FOLLOWS)
596 write_vstring(sock_f_out, xname, strlen(xname));
597 #ifdef SUPPORT_XATTRS
598 if (preserve_xattrs && do_xfers
599 && iflags & (ITEM_REPORT_XATTR|ITEM_TRANSFER)) {
600 int fd = iflags & ITEM_REPORT_XATTR
601 && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE))
602 ? sock_f_out : -1;
603 send_xattr_request(NULL, file, fd);
604 }
605 #endif
606 } else if (ndx >= 0) {
607 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
608 log_item(code, file, iflags, xname);
609 }
610 }
611 }
612
613
614 /* Perform our quick-check heuristic for determining if a file is unchanged. */
unchanged_file(char * fn,struct file_struct * file,STRUCT_STAT * st)615 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
616 {
617 if (st->st_size != F_LENGTH(file))
618 return 0;
619
620 /* if always checksum is set then we use the checksum instead
621 of the file time to determine whether to sync */
622 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
623 char sum[MAX_DIGEST_LEN];
624 file_checksum(fn, st, sum);
625 return memcmp(sum, F_SUM(file), flist_csum_len) == 0;
626 }
627
628 if (size_only > 0)
629 return 1;
630
631 if (ignore_times)
632 return 0;
633
634 return !mtime_differs(st, file);
635 }
636
637
638 /*
639 * set (initialize) the size entries in the per-file sum_struct
640 * calculating dynamic block and checksum sizes.
641 *
642 * This is only called from generate_and_send_sums() but is a separate
643 * function to encapsulate the logic.
644 *
645 * The block size is a rounded square root of file length.
646 *
647 * The checksum size is determined according to:
648 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
649 * provided by Donovan Baarda which gives a probability of rsync
650 * algorithm corrupting data and falling back using the whole md4
651 * checksums.
652 *
653 * This might be made one of several selectable heuristics.
654 */
sum_sizes_sqroot(struct sum_struct * sum,int64 len)655 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
656 {
657 int32 blength;
658 int s2length;
659 int64 l;
660
661 if (len < 0) {
662 /* The file length overflowed our int64 var, so we can't process this file. */
663 sum->count = -1; /* indicate overflow error */
664 return;
665 }
666
667 if (block_size)
668 blength = block_size;
669 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
670 blength = BLOCK_SIZE;
671 else {
672 int32 max_blength = protocol_version < 30 ? OLD_MAX_BLOCK_SIZE : MAX_BLOCK_SIZE;
673 int32 c;
674 int cnt;
675 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
676 if (c < 0 || c >= max_blength)
677 blength = max_blength;
678 else {
679 blength = 0;
680 do {
681 blength |= c;
682 if (len < (int64)blength * blength)
683 blength &= ~c;
684 c >>= 1;
685 } while (c >= 8); /* round to multiple of 8 */
686 blength = MAX(blength, BLOCK_SIZE);
687 }
688 }
689
690 if (protocol_version < 27) {
691 s2length = csum_length;
692 } else if (csum_length == SUM_LENGTH) {
693 s2length = SUM_LENGTH;
694 } else {
695 int32 c;
696 int b = BLOCKSUM_BIAS;
697 for (l = len; l >>= 1; b += 2) {}
698 for (c = blength; (c >>= 1) && b; b--) {}
699 /* add a bit, subtract rollsum, round up. */
700 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
701 s2length = MAX(s2length, csum_length);
702 s2length = MIN(s2length, SUM_LENGTH);
703 }
704
705 sum->flength = len;
706 sum->blength = blength;
707 sum->s2length = s2length;
708 sum->remainder = (int32)(len % blength);
709 sum->count = (int32)(l = (len / blength) + (sum->remainder != 0));
710
711 if ((int64)sum->count != l)
712 sum->count = -1;
713
714 if (sum->count && DEBUG_GTE(DELTASUM, 2)) {
715 rprintf(FINFO,
716 "count=%s rem=%ld blength=%ld s2length=%d flength=%s\n",
717 big_num(sum->count), (long)sum->remainder, (long)sum->blength,
718 sum->s2length, big_num(sum->flength));
719 }
720 }
721
722
723 /*
724 * Generate and send a stream of signatures/checksums that describe a buffer
725 *
726 * Generate approximately one checksum every block_len bytes.
727 */
generate_and_send_sums(int fd,OFF_T len,int f_out,int f_copy)728 static int generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
729 {
730 int32 i;
731 struct map_struct *mapbuf;
732 struct sum_struct sum;
733 OFF_T offset = 0;
734
735 sum_sizes_sqroot(&sum, len);
736 if (sum.count < 0)
737 return -1;
738 write_sum_head(f_out, &sum);
739
740 if (append_mode > 0 && f_copy < 0)
741 return 0;
742
743 if (len > 0)
744 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
745 else
746 mapbuf = NULL;
747
748 for (i = 0; i < sum.count; i++) {
749 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
750 char *map = map_ptr(mapbuf, offset, n1);
751 char sum2[SUM_LENGTH];
752 uint32 sum1;
753
754 len -= n1;
755 offset += n1;
756
757 if (f_copy >= 0) {
758 full_write(f_copy, map, n1);
759 if (append_mode > 0)
760 continue;
761 }
762
763 sum1 = get_checksum1(map, n1);
764 get_checksum2(map, n1, sum2);
765
766 if (DEBUG_GTE(DELTASUM, 3)) {
767 rprintf(FINFO,
768 "chunk[%s] offset=%s len=%ld sum1=%08lx\n",
769 big_num(i), big_num(offset - n1), (long)n1,
770 (unsigned long)sum1);
771 }
772 write_int(f_out, sum1);
773 write_buf(f_out, sum2, sum.s2length);
774 }
775
776 if (mapbuf)
777 unmap_file(mapbuf);
778
779 return 0;
780 }
781
782
783 /* Try to find a filename in the same dir as "fname" with a similar name. */
find_fuzzy(struct file_struct * file,struct file_list * dirlist_array[],uchar * fnamecmp_type_ptr)784 static struct file_struct *find_fuzzy(struct file_struct *file, struct file_list *dirlist_array[], uchar *fnamecmp_type_ptr)
785 {
786 int fname_len, fname_suf_len;
787 const char *fname_suf, *fname = file->basename;
788 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
789 int i, j;
790 struct file_struct *lowest_fp = NULL;
791
792 fname_len = strlen(fname);
793 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
794
795 /* Try to find an exact size+mtime match first. */
796 for (i = 0; i < fuzzy_basis; i++) {
797 struct file_list *dirlist = dirlist_array[i];
798
799 if (!dirlist)
800 continue;
801
802 for (j = 0; j < dirlist->used; j++) {
803 struct file_struct *fp = dirlist->files[j];
804
805 if (!F_IS_ACTIVE(fp))
806 continue;
807
808 if (!S_ISREG(fp->mode) || !F_LENGTH(fp) || fp->flags & FLAG_FILE_SENT)
809 continue;
810
811 if (F_LENGTH(fp) == F_LENGTH(file) && same_time(fp->modtime, 0, file->modtime, 0)) {
812 if (DEBUG_GTE(FUZZY, 2))
813 rprintf(FINFO, "fuzzy size/modtime match for %s\n", f_name(fp, NULL));
814 *fnamecmp_type_ptr = FNAMECMP_FUZZY + i;
815 return fp;
816 }
817
818 }
819 }
820
821 for (i = 0; i < fuzzy_basis; i++) {
822 struct file_list *dirlist = dirlist_array[i];
823
824 if (!dirlist)
825 continue;
826
827 for (j = 0; j < dirlist->used; j++) {
828 struct file_struct *fp = dirlist->files[j];
829 const char *suf, *name;
830 int len, suf_len;
831 uint32 dist;
832
833 if (!F_IS_ACTIVE(fp))
834 continue;
835
836 if (!S_ISREG(fp->mode) || !F_LENGTH(fp) || fp->flags & FLAG_FILE_SENT)
837 continue;
838
839 name = fp->basename;
840 len = strlen(name);
841 suf = find_filename_suffix(name, len, &suf_len);
842
843 dist = fuzzy_distance(name, len, fname, fname_len);
844 /* Add some extra weight to how well the suffixes match. */
845 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len) * 10;
846 if (DEBUG_GTE(FUZZY, 2)) {
847 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
848 f_name(fp, NULL), (int)(dist>>16), (int)(dist&0xFFFF));
849 }
850 if (dist <= lowest_dist) {
851 lowest_dist = dist;
852 lowest_fp = fp;
853 *fnamecmp_type_ptr = FNAMECMP_FUZZY + i;
854 }
855 }
856 }
857
858 return lowest_fp;
859 }
860
861 /* Copy a file found in our --copy-dest handling. */
copy_altdest_file(const char * src,const char * dest,struct file_struct * file)862 static int copy_altdest_file(const char *src, const char *dest, struct file_struct *file)
863 {
864 char buf[MAXPATHLEN];
865 const char *copy_to, *partialptr;
866 int save_preserve_xattrs = preserve_xattrs;
867 int ok, fd_w;
868
869 if (inplace) {
870 /* Let copy_file open the destination in place. */
871 fd_w = -1;
872 copy_to = dest;
873 } else {
874 fd_w = open_tmpfile(buf, dest, file);
875 if (fd_w < 0)
876 return -1;
877 copy_to = buf;
878 }
879 cleanup_set(copy_to, NULL, NULL, -1, -1);
880 if (copy_file(src, copy_to, fd_w, file->mode) < 0) {
881 if (INFO_GTE(COPY, 1)) {
882 rsyserr(FINFO, errno, "copy_file %s => %s",
883 full_fname(src), copy_to);
884 }
885 /* Try to clean up. */
886 unlink(copy_to);
887 cleanup_disable();
888 return -1;
889 }
890 partialptr = partial_dir ? partial_dir_fname(dest) : NULL;
891 preserve_xattrs = 0; /* xattrs were copied with file */
892 ok = finish_transfer(dest, copy_to, src, partialptr, file, 1, 0);
893 preserve_xattrs = save_preserve_xattrs;
894 cleanup_disable();
895 return ok ? 0 : -1;
896 }
897
898 /* This is only called for regular files. We return -2 if we've finished
899 * handling the file, -1 if no dest-linking occurred, or a non-negative
900 * value if we found an alternate basis file. If we're called with the
901 * find_exact_for_existing flag, the destination file already exists, so
902 * we only try to find an exact alt-dest match. In this case, the returns
903 * are only -2 & -1 (both as above). */
try_dests_reg(struct file_struct * file,char * fname,int ndx,char * cmpbuf,stat_x * sxp,int find_exact_for_existing,int itemizing,enum logcode code)904 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
905 char *cmpbuf, stat_x *sxp, int find_exact_for_existing,
906 int itemizing, enum logcode code)
907 {
908 STRUCT_STAT real_st = sxp->st;
909 int best_match = -1;
910 int match_level = 0;
911 int j = 0;
912
913 do {
914 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
915 if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
916 continue;
917 if (match_level == 0) {
918 best_match = j;
919 match_level = 1;
920 }
921 if (!unchanged_file(cmpbuf, file, &sxp->st))
922 continue;
923 if (match_level == 1) {
924 best_match = j;
925 match_level = 2;
926 }
927 if (unchanged_attrs(cmpbuf, file, sxp)) {
928 best_match = j;
929 match_level = 3;
930 break;
931 }
932 free_stat_x(sxp);
933 } while (basis_dir[++j] != NULL);
934
935 if (!match_level)
936 goto got_nothing_for_ya;
937
938 if (j != best_match) {
939 j = best_match;
940 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
941 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
942 goto got_nothing_for_ya;
943 }
944
945 if (match_level == 3 && alt_dest_type != COPY_DEST) {
946 if (find_exact_for_existing) {
947 if (alt_dest_type == LINK_DEST && real_st.st_dev == sxp->st.st_dev && real_st.st_ino == sxp->st.st_ino)
948 return -1;
949 if (do_unlink(fname) < 0 && errno != ENOENT)
950 goto got_nothing_for_ya;
951 }
952 #ifdef SUPPORT_HARD_LINKS
953 if (alt_dest_type == LINK_DEST) {
954 if (!hard_link_one(file, fname, cmpbuf, 1))
955 goto try_a_copy;
956 if (atimes_ndx)
957 set_file_attrs(fname, file, sxp, NULL, 0);
958 if (preserve_hard_links && F_IS_HLINKED(file))
959 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, j);
960 if (!maybe_ATTRS_REPORT && (INFO_GTE(NAME, 2) || stdout_format_has_i > 1)) {
961 itemize(cmpbuf, file, ndx, 1, sxp,
962 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
963 0, "");
964 }
965 } else
966 #endif
967 {
968 if (itemizing)
969 itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
970 }
971 if (INFO_GTE(NAME, 2) && maybe_ATTRS_REPORT)
972 rprintf(FCLIENT, "%s is uptodate\n", fname);
973 return -2;
974 }
975
976 if (find_exact_for_existing)
977 goto got_nothing_for_ya;
978
979 if (match_level >= 2) {
980 #ifdef SUPPORT_HARD_LINKS
981 try_a_copy: /* Copy the file locally. */
982 #endif
983 if (!dry_run && copy_altdest_file(cmpbuf, fname, file) < 0) {
984 if (find_exact_for_existing) /* Can get here via hard-link failure */
985 goto got_nothing_for_ya;
986 return -1;
987 }
988 if (itemizing)
989 itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
990 if (maybe_ATTRS_REPORT
991 && ((!itemizing && INFO_GTE(NAME, 1) && match_level == 2)
992 || (INFO_GTE(NAME, 2) && match_level == 3))) {
993 code = match_level == 3 ? FCLIENT : FINFO;
994 rprintf(code, "%s%s\n", fname,
995 match_level == 3 ? " is uptodate" : "");
996 }
997 #ifdef SUPPORT_HARD_LINKS
998 if (preserve_hard_links && F_IS_HLINKED(file))
999 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, -1);
1000 #endif
1001 return -2;
1002 }
1003
1004 return FNAMECMP_BASIS_DIR_LOW + j;
1005
1006 got_nothing_for_ya:
1007 sxp->st = real_st;
1008 return -1;
1009 }
1010
1011 /* This is only called for non-regular files. We return -2 if we've finished
1012 * handling the file, or -1 if no dest-linking occurred, or a non-negative
1013 * value if we found an alternate basis file. */
try_dests_non(struct file_struct * file,char * fname,int ndx,char * cmpbuf,stat_x * sxp,int itemizing,enum logcode code)1014 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
1015 char *cmpbuf, stat_x *sxp, int itemizing,
1016 enum logcode code)
1017 {
1018 int best_match = -1;
1019 int match_level = 0;
1020 enum nonregtype type;
1021 uint32 *devp;
1022 #ifdef SUPPORT_LINKS
1023 char lnk[MAXPATHLEN];
1024 int len;
1025 #endif
1026 int j = 0;
1027
1028 #ifndef SUPPORT_LINKS
1029 if (S_ISLNK(file->mode))
1030 return -1;
1031 #endif
1032 if (S_ISDIR(file->mode)) {
1033 type = TYPE_DIR;
1034 } else if (IS_SPECIAL(file->mode))
1035 type = TYPE_SPECIAL;
1036 else if (IS_DEVICE(file->mode))
1037 type = TYPE_DEVICE;
1038 #ifdef SUPPORT_LINKS
1039 else if (S_ISLNK(file->mode))
1040 type = TYPE_SYMLINK;
1041 #endif
1042 else {
1043 rprintf(FERROR,
1044 "internal: try_dests_non() called with invalid mode (%o)\n",
1045 (int)file->mode);
1046 exit_cleanup(RERR_UNSUPPORTED);
1047 }
1048
1049 do {
1050 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1051 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1052 continue;
1053 switch (type) {
1054 case TYPE_DIR:
1055 if (!S_ISDIR(sxp->st.st_mode))
1056 continue;
1057 break;
1058 case TYPE_SPECIAL:
1059 if (!IS_SPECIAL(sxp->st.st_mode))
1060 continue;
1061 break;
1062 case TYPE_DEVICE:
1063 if (!IS_DEVICE(sxp->st.st_mode))
1064 continue;
1065 break;
1066 case TYPE_SYMLINK:
1067 #ifdef SUPPORT_LINKS
1068 if (!S_ISLNK(sxp->st.st_mode))
1069 continue;
1070 break;
1071 #else
1072 return -1;
1073 #endif
1074 }
1075 if (match_level < 1) {
1076 match_level = 1;
1077 best_match = j;
1078 }
1079 switch (type) {
1080 case TYPE_DIR:
1081 case TYPE_SPECIAL:
1082 break;
1083 case TYPE_DEVICE:
1084 devp = F_RDEV_P(file);
1085 if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
1086 continue;
1087 break;
1088 case TYPE_SYMLINK:
1089 #ifdef SUPPORT_LINKS
1090 if ((len = do_readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
1091 continue;
1092 lnk[len] = '\0';
1093 if (strcmp(lnk, F_SYMLINK(file)) != 0)
1094 continue;
1095 break;
1096 #else
1097 return -1;
1098 #endif
1099 }
1100 if (match_level < 2) {
1101 match_level = 2;
1102 best_match = j;
1103 }
1104 if (unchanged_attrs(cmpbuf, file, sxp)) {
1105 match_level = 3;
1106 best_match = j;
1107 break;
1108 }
1109 } while (basis_dir[++j] != NULL);
1110
1111 if (!match_level)
1112 return -1;
1113
1114 if (j != best_match) {
1115 j = best_match;
1116 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1117 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1118 return -1;
1119 }
1120
1121 if (match_level == 3) {
1122 #ifdef SUPPORT_HARD_LINKS
1123 if (alt_dest_type == LINK_DEST
1124 #ifndef CAN_HARDLINK_SYMLINK
1125 && !S_ISLNK(file->mode)
1126 #endif
1127 #ifndef CAN_HARDLINK_SPECIAL
1128 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
1129 #endif
1130 && !S_ISDIR(file->mode)) {
1131 if (do_link(cmpbuf, fname) < 0) {
1132 rsyserr(FERROR_XFER, errno,
1133 "failed to hard-link %s with %s",
1134 cmpbuf, fname);
1135 return j;
1136 }
1137 if (preserve_hard_links && F_IS_HLINKED(file))
1138 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1139 } else
1140 #endif
1141 match_level = 2;
1142 if (itemizing && stdout_format_has_i
1143 && (INFO_GTE(NAME, 2) || stdout_format_has_i > 1)) {
1144 int chg = alt_dest_type == COMPARE_DEST && type != TYPE_DIR ? 0
1145 : ITEM_LOCAL_CHANGE + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1146 char *lp = match_level == 3 ? "" : NULL;
1147 itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
1148 }
1149 if (INFO_GTE(NAME, 2) && maybe_ATTRS_REPORT) {
1150 rprintf(FCLIENT, "%s%s is uptodate\n",
1151 fname, type == TYPE_DIR ? "/" : "");
1152 }
1153 return -2;
1154 }
1155
1156 return j;
1157 }
1158
list_file_entry(struct file_struct * f)1159 static void list_file_entry(struct file_struct *f)
1160 {
1161 char permbuf[PERMSTRING_SIZE];
1162 const char *mtime_str = timestring(f->modtime);
1163 int size_width = human_readable ? 14 : 11;
1164 int mtime_width = 1 + strlen(mtime_str);
1165 int atime_width = atimes_ndx ? mtime_width : 0;
1166 int crtime_width = crtimes_ndx ? mtime_width : 0;
1167
1168 if (!F_IS_ACTIVE(f)) {
1169 /* this can happen if duplicate names were removed */
1170 return;
1171 }
1172
1173 /* TODO: indicate '+' if the entry has an ACL. */
1174
1175 if (missing_args == 2 && f->mode == 0) {
1176 rprintf(FINFO, "%-*s %s\n",
1177 10 + 1 + size_width + mtime_width + atime_width + crtime_width, "*missing",
1178 f_name(f, NULL));
1179 } else {
1180 const char *atime_str = atimes_ndx && !S_ISDIR(f->mode) ? timestring(F_ATIME(f)) : "";
1181 const char *crtime_str = crtimes_ndx ? timestring(F_CRTIME(f)) : "";
1182 const char *arrow, *lnk;
1183
1184 permstring(permbuf, f->mode);
1185
1186 #ifdef SUPPORT_LINKS
1187 if (preserve_links && S_ISLNK(f->mode)) {
1188 arrow = " -> ";
1189 lnk = F_SYMLINK(f);
1190 } else
1191 #endif
1192 arrow = lnk = "";
1193
1194 rprintf(FINFO, "%s %*s %s%*s%*s %s%s%s\n",
1195 permbuf, size_width, human_num(F_LENGTH(f)),
1196 timestring(f->modtime), atime_width, atime_str, crtime_width, crtime_str,
1197 f_name(f, NULL), arrow, lnk);
1198 }
1199 }
1200
1201 static int phase = 0;
1202 static int dflt_perms;
1203
1204 static int implied_dirs_are_missing;
1205 /* Helper for recv_generator's skip_dir and dry_missing_dir tests. */
is_below(struct file_struct * file,struct file_struct * subtree)1206 static BOOL is_below(struct file_struct *file, struct file_struct *subtree)
1207 {
1208 return F_DEPTH(file) > F_DEPTH(subtree)
1209 && (!implied_dirs_are_missing || f_name_has_prefix(file, subtree));
1210 }
1211
1212 /* Acts on the indicated item in cur_flist whose name is fname. If a dir,
1213 * make sure it exists, and has the right permissions/timestamp info. For
1214 * all other non-regular files (symlinks, etc.) we create them here. For
1215 * regular files that have changed, we try to find a basis file and then
1216 * start sending checksums. The ndx is the file's unique index value.
1217 *
1218 * The fname parameter must point to a MAXPATHLEN buffer! (e.g it gets
1219 * passed to delete_item(), which can use it during a recursive delete.)
1220 *
1221 * Note that f_out is set to -1 when doing final directory-permission and
1222 * modification-time repair. */
recv_generator(char * fname,struct file_struct * file,int ndx,int itemizing,enum logcode code,int f_out)1223 static void recv_generator(char *fname, struct file_struct *file, int ndx,
1224 int itemizing, enum logcode code, int f_out)
1225 {
1226 static const char *parent_dirname = "";
1227 static struct file_struct *prior_dir_file = NULL;
1228 /* Missing dir not created due to --dry-run; will still be scanned. */
1229 static struct file_struct *dry_missing_dir = NULL;
1230 /* Missing dir whose contents are skipped altogether due to
1231 * --ignore-non-existing, daemon exclude, or mkdir failure. */
1232 static struct file_struct *skip_dir = NULL;
1233 static struct file_list *fuzzy_dirlist[MAX_BASIS_DIRS+1];
1234 static int need_fuzzy_dirlist = 0;
1235 struct file_struct *fuzzy_file = NULL;
1236 int fd = -1, f_copy = -1;
1237 stat_x sx, real_sx;
1238 STRUCT_STAT partial_st;
1239 struct file_struct *back_file = NULL;
1240 int statret, real_ret, stat_errno;
1241 char *fnamecmp, *partialptr, *backupptr = NULL;
1242 char fnamecmpbuf[MAXPATHLEN];
1243 uchar fnamecmp_type;
1244 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1245 int is_dir = !S_ISDIR(file->mode) ? 0
1246 : inc_recurse && ndx != cur_flist->ndx_start - 1 ? -1
1247 : 1;
1248
1249 if (DEBUG_GTE(GENR, 1))
1250 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
1251
1252 if (list_only) {
1253 if (is_dir < 0
1254 || (is_dir && !implied_dirs && file->flags & FLAG_IMPLIED_DIR))
1255 return;
1256 list_file_entry(file);
1257 return;
1258 }
1259
1260 maybe_ATTRS_ACCURATE_TIME = always_checksum ? ATTRS_ACCURATE_TIME : 0;
1261
1262 if (skip_dir) {
1263 if (is_below(file, skip_dir)) {
1264 if (is_dir)
1265 file->flags |= FLAG_MISSING_DIR;
1266 #ifdef SUPPORT_HARD_LINKS
1267 else if (F_IS_HLINKED(file))
1268 handle_skipped_hlink(file, itemizing, code, f_out);
1269 #endif
1270 return;
1271 }
1272 skip_dir = NULL;
1273 }
1274
1275 init_stat_x(&sx);
1276 if (daemon_filter_list.head && (*fname != '.' || fname[1])) {
1277 if (check_filter(&daemon_filter_list, FLOG, fname, is_dir) < 0) {
1278 if (is_dir < 0)
1279 return;
1280 #ifdef SUPPORT_HARD_LINKS
1281 if (F_IS_HLINKED(file))
1282 handle_skipped_hlink(file, itemizing, code, f_out);
1283 #endif
1284 rprintf(FERROR_XFER,
1285 "ERROR: daemon refused to receive %s \"%s\"\n",
1286 is_dir ? "directory" : "file", fname);
1287 if (is_dir)
1288 goto skipping_dir_contents;
1289 return;
1290 }
1291 }
1292 sx.crtime = 0;
1293
1294 if (dry_run > 1 || (dry_missing_dir && is_below(file, dry_missing_dir))) {
1295 int i;
1296 parent_is_dry_missing:
1297 for (i = 0; i < fuzzy_basis; i++) {
1298 if (fuzzy_dirlist[i]) {
1299 flist_free(fuzzy_dirlist[i]);
1300 fuzzy_dirlist[i] = NULL;
1301 }
1302 }
1303 parent_dirname = "";
1304 statret = -1;
1305 stat_errno = ENOENT;
1306 } else {
1307 const char *dn = file->dirname ? file->dirname : ".";
1308 dry_missing_dir = NULL;
1309 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1310 /* Each parent dir must be in the file list or the flist data is bad.
1311 * Optimization: most of the time the parent dir will be the last dir
1312 * this function was asked to process in the file list. */
1313 if (!inc_recurse
1314 && (*dn != '.' || dn[1]) /* Avoid an issue with --relative and the "." dir. */
1315 && (!prior_dir_file || strcmp(dn, f_name(prior_dir_file, NULL)) != 0)) {
1316 int ok = 0, j = flist_find_name(cur_flist, dn, -1);
1317 if (j >= 0) {
1318 struct file_struct *f = cur_flist->sorted[j];
1319 if (S_ISDIR(f->mode) || (missing_args == 2 && !file->mode && !f->mode))
1320 ok = 1;
1321 }
1322 /* The --delete-missing-args option can actually put invalid entries into
1323 * the file list, so if that option was specified, we'll just complain about
1324 * it and allow it. */
1325 if (!ok && missing_args == 2 && file->mode == 0 && j < 0)
1326 rprintf(FERROR, "WARNING: parent dir is absent in the file list: %s\n", dn);
1327 else if (!ok) {
1328 rprintf(FERROR, "ABORTING due to invalid path from sender: %s/%s\n",
1329 dn, file->basename);
1330 exit_cleanup(RERR_PROTOCOL);
1331 }
1332 }
1333 if (relative_paths && !implied_dirs && file->mode != 0
1334 && do_stat(dn, &sx.st) < 0) {
1335 if (dry_run)
1336 goto parent_is_dry_missing;
1337 if (make_path(fname, MKP_DROP_NAME | MKP_SKIP_SLASH) < 0) {
1338 rsyserr(FERROR_XFER, errno,
1339 "recv_generator: mkdir %s failed",
1340 full_fname(dn));
1341 }
1342 }
1343 if (fuzzy_basis) {
1344 int i;
1345 for (i = 0; i < fuzzy_basis; i++) {
1346 if (fuzzy_dirlist[i]) {
1347 flist_free(fuzzy_dirlist[i]);
1348 fuzzy_dirlist[i] = NULL;
1349 }
1350 }
1351 need_fuzzy_dirlist = 1;
1352 }
1353 #ifdef SUPPORT_ACLS
1354 if (!preserve_perms)
1355 dflt_perms = default_perms_for_dir(dn);
1356 #endif
1357 }
1358 parent_dirname = dn;
1359
1360 statret = link_stat(fname, &sx.st, keep_dirlinks && is_dir);
1361 stat_errno = errno;
1362 }
1363
1364 if (missing_args == 2 && file->mode == 0) {
1365 if (filter_list.head && check_filter(&filter_list, FINFO, fname, is_dir) < 0)
1366 return;
1367 if (statret == 0)
1368 delete_item(fname, sx.st.st_mode, del_opts);
1369 return;
1370 }
1371
1372 if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
1373 if (is_dir) {
1374 if (is_dir < 0)
1375 return;
1376 skip_dir = file;
1377 file->flags |= FLAG_MISSING_DIR;
1378 }
1379 #ifdef SUPPORT_HARD_LINKS
1380 else if (F_IS_HLINKED(file))
1381 handle_skipped_hlink(file, itemizing, code, f_out);
1382 #endif
1383 if (INFO_GTE(SKIP, 1)) {
1384 rprintf(FINFO, "not creating new %s \"%s\"\n",
1385 is_dir ? "directory" : "file", fname);
1386 }
1387 return;
1388 }
1389
1390 if (statret == 0 && !(sx.st.st_mode & S_IWUSR)
1391 && !am_root && sx.st.st_uid == our_uid)
1392 del_opts |= DEL_NO_UID_WRITE;
1393
1394 if (ignore_existing > 0 && statret == 0
1395 && (!is_dir || !S_ISDIR(sx.st.st_mode))) {
1396 if (INFO_GTE(SKIP, 1) && is_dir >= 0)
1397 rprintf(FINFO, "%s exists\n", fname);
1398 #ifdef SUPPORT_HARD_LINKS
1399 if (F_IS_HLINKED(file))
1400 handle_skipped_hlink(file, itemizing, code, f_out);
1401 #endif
1402 goto cleanup;
1403 }
1404
1405 fnamecmp = fname;
1406
1407 if (is_dir) {
1408 mode_t added_perms;
1409 if (!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
1410 goto cleanup;
1411 if (am_root < 0) {
1412 /* For --fake-super, the dir must be useable by the copying
1413 * user, just like it would be for root. */
1414 added_perms = S_IRUSR|S_IWUSR|S_IXUSR;
1415 } else
1416 added_perms = 0;
1417 if (is_dir < 0) {
1418 if (!(preserve_times & PRESERVE_DIR_TIMES))
1419 goto cleanup;
1420 /* In inc_recurse mode we want to make sure any missing
1421 * directories get created while we're still processing
1422 * the parent dir (which allows us to touch the parent
1423 * dir's mtime right away). We will handle the dir in
1424 * full later (right before we handle its contents). */
1425 if (statret == 0
1426 && (S_ISDIR(sx.st.st_mode)
1427 || delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0))
1428 goto cleanup; /* Any errors get reported later. */
1429 if (do_mkdir(fname, (file->mode|added_perms) & 0700) == 0)
1430 file->flags |= FLAG_DIR_CREATED;
1431 goto cleanup;
1432 }
1433 /* The file to be received is a directory, so we need
1434 * to prepare appropriately. If there is already a
1435 * file of that name and it is *not* a directory, then
1436 * we need to delete it. If it doesn't exist, then
1437 * (perhaps recursively) create it. */
1438 if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1439 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0)
1440 goto skipping_dir_contents;
1441 statret = -1;
1442 }
1443 if (dry_run && statret != 0) {
1444 if (!dry_missing_dir)
1445 dry_missing_dir = file;
1446 file->flags |= FLAG_MISSING_DIR;
1447 }
1448 init_stat_x(&real_sx);
1449 real_sx.st = sx.st;
1450 real_ret = statret;
1451 if (file->flags & FLAG_DIR_CREATED)
1452 statret = -1;
1453 if (!preserve_perms) { /* See comment in non-dir code below. */
1454 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms, statret == 0);
1455 }
1456 #ifdef SUPPORT_FORCE_CHANGE
1457 if (force_change && !preserve_fileflags)
1458 F_FFLAGS(file) = sx.st.st_flags;
1459 #endif
1460 if (statret != 0 && basis_dir[0] != NULL) {
1461 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx, itemizing, code);
1462 if (j == -2) {
1463 itemizing = 0;
1464 code = FNONE;
1465 statret = 1;
1466 } else if (j >= 0) {
1467 statret = 1;
1468 fnamecmp = fnamecmpbuf;
1469 }
1470 }
1471 if (itemizing && f_out != -1) {
1472 itemize(fnamecmp, file, ndx, statret, &sx,
1473 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1474 }
1475 if (real_ret != 0 && do_mkdir(fname,file->mode|added_perms) < 0 && errno != EEXIST) {
1476 if (!relative_paths || errno != ENOENT
1477 || make_path(fname, MKP_DROP_NAME | MKP_SKIP_SLASH) < 0
1478 || (do_mkdir(fname, file->mode|added_perms) < 0 && errno != EEXIST)) {
1479 rsyserr(FERROR_XFER, errno,
1480 "recv_generator: mkdir %s failed",
1481 full_fname(fname));
1482 skipping_dir_contents:
1483 rprintf(FERROR, "*** Skipping any contents from this failed directory ***\n");
1484 skip_dir = file;
1485 file->flags |= FLAG_MISSING_DIR;
1486 goto cleanup;
1487 }
1488 }
1489
1490 #ifdef SUPPORT_XATTRS
1491 if (preserve_xattrs && statret == 1)
1492 copy_xattrs(fnamecmpbuf, fname);
1493 #endif
1494 if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
1495 && INFO_GTE(NAME, 1) && code != FNONE && f_out != -1)
1496 rprintf(code, "%s/\n", fname);
1497
1498 /* We need to ensure that the dirs in the transfer have both
1499 * readable and writable permissions during the time we are
1500 * putting files within them. This is then restored to the
1501 * former permissions after the transfer is done. */
1502 #ifdef SUPPORT_FORCE_CHANGE
1503 if (force_change && F_FFLAGS(file) & force_change
1504 && make_mutable(fname, file->mode, F_FFLAGS(file), force_change))
1505 need_retouch_dir_perms = 1;
1506 #endif
1507 #ifdef HAVE_CHMOD
1508 if (!am_root && (file->mode & S_IRWXU) != S_IRWXU && dir_tweaking) {
1509 mode_t mode = file->mode | S_IRWXU;
1510 if (do_chmod(fname, mode, 0) < 0) {
1511 rsyserr(FERROR_XFER, errno,
1512 "failed to modify permissions on %s",
1513 full_fname(fname));
1514 }
1515 need_retouch_dir_perms = 1;
1516 }
1517 #endif
1518
1519 if (real_ret != 0 && one_file_system)
1520 real_sx.st.st_dev = filesystem_dev;
1521 if (inc_recurse) {
1522 if (one_file_system) {
1523 uint32 *devp = F_DIR_DEV_P(file);
1524 DEV_MAJOR(devp) = major(real_sx.st.st_dev);
1525 DEV_MINOR(devp) = minor(real_sx.st.st_dev);
1526 }
1527 }
1528 else if (delete_during && f_out != -1 && !phase
1529 && !(file->flags & FLAG_MISSING_DIR)) {
1530 if (file->flags & FLAG_CONTENT_DIR)
1531 delete_in_dir(fname, file, &real_sx.st.st_dev);
1532 else
1533 change_local_filter_dir(fname, strlen(fname), F_DEPTH(file));
1534 }
1535 prior_dir_file = file;
1536 goto cleanup;
1537 }
1538
1539 /* If we're not preserving permissions, change the file-list's
1540 * mode based on the local permissions and some heuristics. */
1541 if (!preserve_perms) {
1542 int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
1543 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms, exists);
1544 }
1545 #ifdef SUPPORT_FORCE_CHANGE
1546 if (force_change && !preserve_fileflags)
1547 F_FFLAGS(file) = sx.st.st_flags;
1548 #endif
1549
1550 #ifdef SUPPORT_HARD_LINKS
1551 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1552 && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
1553 goto cleanup;
1554 #endif
1555
1556 if (preserve_links && S_ISLNK(file->mode)) {
1557 #ifdef SUPPORT_LINKS
1558 const char *sl = F_SYMLINK(file);
1559 if (safe_symlinks && unsafe_symlink(sl, fname)) {
1560 if (INFO_GTE(NAME, 1)) {
1561 if (solo_file) {
1562 /* fname contains the destination path, but we
1563 * want to report the source path. */
1564 fname = f_name(file, NULL);
1565 }
1566 rprintf(FINFO,
1567 "ignoring unsafe symlink \"%s\" -> \"%s\"\n",
1568 fname, sl);
1569 }
1570 goto cleanup;
1571 }
1572 if (statret == 0) {
1573 char lnk[MAXPATHLEN];
1574 int len;
1575
1576 if (S_ISLNK(sx.st.st_mode)
1577 && (len = do_readlink(fname, lnk, MAXPATHLEN-1)) > 0
1578 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1579 /* The link is pointing to the right place. */
1580 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1581 if (itemizing)
1582 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1583 #ifdef SUPPORT_HARD_LINKS
1584 if (preserve_hard_links && F_IS_HLINKED(file))
1585 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1586 #endif
1587 if (remove_source_files == 1)
1588 goto return_with_success;
1589 goto cleanup;
1590 }
1591 } else if (basis_dir[0] != NULL) {
1592 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx, itemizing, code);
1593 if (j == -2) {
1594 #ifndef CAN_HARDLINK_SYMLINK
1595 if (alt_dest_type == LINK_DEST) {
1596 /* Resort to --copy-dest behavior. */
1597 } else
1598 #endif
1599 if (alt_dest_type != COPY_DEST)
1600 goto cleanup;
1601 itemizing = 0;
1602 code = FNONE;
1603 } else if (j >= 0) {
1604 statret = 1;
1605 fnamecmp = fnamecmpbuf;
1606 }
1607 }
1608 if (atomic_create(file, fname, sl, NULL, MAKEDEV(0, 0), &sx, statret == 0 ? DEL_FOR_SYMLINK : 0)) {
1609 set_file_attrs(fname, file, NULL, NULL, 0);
1610 if (itemizing) {
1611 if (statret == 0 && !S_ISLNK(sx.st.st_mode))
1612 statret = -1;
1613 itemize(fnamecmp, file, ndx, statret, &sx,
1614 ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
1615 }
1616 if (code != FNONE && INFO_GTE(NAME, 1))
1617 rprintf(code, "%s -> %s\n", fname, sl);
1618 #ifdef SUPPORT_HARD_LINKS
1619 if (preserve_hard_links && F_IS_HLINKED(file))
1620 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1621 #endif
1622 /* This does not check remove_source_files == 1
1623 * because this is one of the items that the old
1624 * --remove-sent-files option would remove. */
1625 if (remove_source_files)
1626 goto return_with_success;
1627 }
1628 #endif
1629 goto cleanup;
1630 }
1631
1632 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1633 || (preserve_specials && IS_SPECIAL(file->mode))) {
1634 dev_t rdev;
1635 int del_for_flag = 0;
1636 if (IS_DEVICE(file->mode)) {
1637 uint32 *devp = F_RDEV_P(file);
1638 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1639 } else
1640 rdev = 0;
1641 if (statret == 0) {
1642 if (IS_DEVICE(file->mode)) {
1643 if (!IS_DEVICE(sx.st.st_mode))
1644 statret = -1;
1645 del_for_flag = DEL_FOR_DEVICE;
1646 } else {
1647 if (!IS_SPECIAL(sx.st.st_mode))
1648 statret = -1;
1649 del_for_flag = DEL_FOR_SPECIAL;
1650 }
1651 if (statret == 0
1652 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
1653 && (IS_SPECIAL(sx.st.st_mode) || sx.st.st_rdev == rdev)) {
1654 /* The device or special file is identical. */
1655 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1656 if (itemizing)
1657 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1658 #ifdef SUPPORT_HARD_LINKS
1659 if (preserve_hard_links && F_IS_HLINKED(file))
1660 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1661 #endif
1662 if (remove_source_files == 1)
1663 goto return_with_success;
1664 goto cleanup;
1665 }
1666 } else if (basis_dir[0] != NULL) {
1667 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx, itemizing, code);
1668 if (j == -2) {
1669 #ifndef CAN_HARDLINK_SPECIAL
1670 if (alt_dest_type == LINK_DEST) {
1671 /* Resort to --copy-dest behavior. */
1672 } else
1673 #endif
1674 if (alt_dest_type != COPY_DEST)
1675 goto cleanup;
1676 itemizing = 0;
1677 code = FNONE;
1678 } else if (j >= 0) {
1679 statret = 1;
1680 fnamecmp = fnamecmpbuf;
1681 }
1682 }
1683 if (DEBUG_GTE(GENR, 1)) {
1684 rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1685 fname, (int)file->mode,
1686 (long)major(rdev), (long)minor(rdev));
1687 }
1688 if (atomic_create(file, fname, NULL, NULL, rdev, &sx, del_for_flag)) {
1689 set_file_attrs(fname, file, NULL, NULL, 0);
1690 if (itemizing) {
1691 itemize(fnamecmp, file, ndx, statret, &sx,
1692 ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
1693 }
1694 if (code != FNONE && INFO_GTE(NAME, 1))
1695 rprintf(code, "%s\n", fname);
1696 #ifdef SUPPORT_HARD_LINKS
1697 if (preserve_hard_links && F_IS_HLINKED(file))
1698 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1699 #endif
1700 if (remove_source_files == 1)
1701 goto return_with_success;
1702 }
1703 goto cleanup;
1704 }
1705
1706 if (!S_ISREG(file->mode)) {
1707 if (solo_file)
1708 fname = f_name(file, NULL);
1709 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1710 goto cleanup;
1711 }
1712
1713 if (max_size >= 0 && F_LENGTH(file) > max_size) {
1714 if (INFO_GTE(SKIP, 1)) {
1715 if (solo_file)
1716 fname = f_name(file, NULL);
1717 rprintf(FINFO, "%s is over max-size\n", fname);
1718 }
1719 goto cleanup;
1720 }
1721 if (min_size >= 0 && F_LENGTH(file) < min_size) {
1722 if (INFO_GTE(SKIP, 1)) {
1723 if (solo_file)
1724 fname = f_name(file, NULL);
1725 rprintf(FINFO, "%s is under min-size\n", fname);
1726 }
1727 goto cleanup;
1728 }
1729
1730 if (update_only > 0 && statret == 0 && file->modtime - sx.st.st_mtime <= modify_window) {
1731 if (INFO_GTE(SKIP, 1))
1732 rprintf(FINFO, "%s is newer\n", fname);
1733 #ifdef SUPPORT_HARD_LINKS
1734 if (F_IS_HLINKED(file))
1735 handle_skipped_hlink(file, itemizing, code, f_out);
1736 #endif
1737 goto cleanup;
1738 }
1739
1740 fnamecmp_type = FNAMECMP_FNAME;
1741
1742 if (statret == 0 && !(S_ISREG(sx.st.st_mode) || (write_devices && IS_DEVICE(sx.st.st_mode)))) {
1743 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE) != 0)
1744 goto cleanup;
1745 statret = -1;
1746 stat_errno = ENOENT;
1747 }
1748
1749 if (basis_dir[0] != NULL && (statret != 0 || alt_dest_type != COPY_DEST)) {
1750 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx, statret == 0, itemizing, code);
1751 if (j == -2) {
1752 if (remove_source_files == 1)
1753 goto return_with_success;
1754 goto cleanup;
1755 }
1756 if (j >= 0) {
1757 fnamecmp = fnamecmpbuf;
1758 fnamecmp_type = j;
1759 statret = 0;
1760 }
1761 }
1762
1763 init_stat_x(&real_sx);
1764 real_sx.st = sx.st; /* Don't copy xattr/acl pointers, as they would free wrong. */
1765 real_ret = statret;
1766
1767 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1768 && link_stat(partialptr, &partial_st, 0) == 0
1769 && S_ISREG(partial_st.st_mode)) {
1770 if (statret != 0)
1771 goto prepare_to_open;
1772 } else
1773 partialptr = NULL;
1774
1775 if (statret != 0 && fuzzy_basis) {
1776 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
1777 const char *dn = file->dirname ? file->dirname : ".";
1778 int i;
1779 strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1780 for (i = 0; i < fuzzy_basis; i++) {
1781 if (i && pathjoin(fnamecmpbuf, MAXPATHLEN, basis_dir[i-1], dn) >= MAXPATHLEN)
1782 continue;
1783 fuzzy_dirlist[i] = get_dirlist(fnamecmpbuf, -1, GDL_IGNORE_FILTER_RULES | GDL_PERHAPS_DIR);
1784 if (fuzzy_dirlist[i] && fuzzy_dirlist[i]->used == 0) {
1785 flist_free(fuzzy_dirlist[i]);
1786 fuzzy_dirlist[i] = NULL;
1787 }
1788 }
1789 need_fuzzy_dirlist = 0;
1790 }
1791
1792 /* Sets fnamecmp_type to FNAMECMP_FUZZY or above. */
1793 fuzzy_file = find_fuzzy(file, fuzzy_dirlist, &fnamecmp_type);
1794 if (fuzzy_file) {
1795 f_name(fuzzy_file, fnamecmpbuf);
1796 if (DEBUG_GTE(FUZZY, 1)) {
1797 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1798 fname, fnamecmpbuf);
1799 }
1800 sx.st.st_size = F_LENGTH(fuzzy_file);
1801 statret = 0;
1802 fnamecmp = fnamecmpbuf;
1803 }
1804 }
1805
1806 if (statret != 0) {
1807 #ifdef SUPPORT_HARD_LINKS
1808 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1809 cur_flist->in_progress++;
1810 goto cleanup;
1811 }
1812 #endif
1813 if (stat_errno == ENOENT)
1814 goto notify_others;
1815 rsyserr(FERROR_XFER, stat_errno, "recv_generator: failed to stat %s",
1816 full_fname(fname));
1817 goto cleanup;
1818 }
1819
1820 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1821 ;
1822 else if (fnamecmp_type >= FNAMECMP_FUZZY)
1823 ;
1824 else if (unchanged_file(fnamecmp, file, &sx.st)) {
1825 if (partialptr) {
1826 do_unlink(partialptr);
1827 handle_partial_dir(partialptr, PDIR_DELETE);
1828 }
1829 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT | maybe_ATTRS_ACCURATE_TIME);
1830 if (itemizing)
1831 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
1832 #ifdef SUPPORT_HARD_LINKS
1833 if (preserve_hard_links && F_IS_HLINKED(file))
1834 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1835 #endif
1836 if (remove_source_files != 1)
1837 goto cleanup;
1838 return_with_success:
1839 if (!dry_run)
1840 send_msg_int(MSG_SUCCESS, ndx);
1841 goto cleanup;
1842 }
1843
1844 if (append_mode > 0 && sx.st.st_size >= F_LENGTH(file)) {
1845 #ifdef SUPPORT_HARD_LINKS
1846 if (F_IS_HLINKED(file))
1847 handle_skipped_hlink(file, itemizing, code, f_out);
1848 #endif
1849 goto cleanup;
1850 }
1851
1852 prepare_to_open:
1853 if (partialptr) {
1854 sx.st = partial_st;
1855 fnamecmp = partialptr;
1856 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1857 statret = 0;
1858 }
1859
1860 if (!do_xfers)
1861 goto notify_others;
1862
1863 if (read_batch || whole_file) {
1864 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1865 if (!(backupptr = get_backup_name(fname)))
1866 goto cleanup;
1867 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
1868 goto pretend_missing;
1869 if (copy_file(fname, backupptr, -1, back_file->mode) < 0) {
1870 unmake_file(back_file);
1871 back_file = NULL;
1872 goto cleanup;
1873 }
1874 }
1875 goto notify_others;
1876 }
1877
1878 if (fuzzy_dirlist[0]) {
1879 int j = flist_find(fuzzy_dirlist[0], file);
1880 if (j >= 0) /* don't use changing file as future fuzzy basis */
1881 fuzzy_dirlist[0]->files[j]->flags |= FLAG_FILE_SENT;
1882 }
1883
1884 /* open the file */
1885 if ((fd = do_open(fnamecmp, O_RDONLY, 0)) < 0) {
1886 rsyserr(FERROR, errno, "failed to open %s, continuing",
1887 full_fname(fnamecmp));
1888 pretend_missing:
1889 /* pretend the file didn't exist */
1890 #ifdef SUPPORT_HARD_LINKS
1891 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1892 cur_flist->in_progress++;
1893 goto cleanup;
1894 }
1895 #endif
1896 statret = real_ret = -1;
1897 goto notify_others;
1898 }
1899
1900 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1901 if (!(backupptr = get_backup_name(fname))) {
1902 close(fd);
1903 goto cleanup;
1904 }
1905 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1906 close(fd);
1907 goto pretend_missing;
1908 }
1909 if (robust_unlink(backupptr) && errno != ENOENT) {
1910 rsyserr(FERROR_XFER, errno, "unlink %s",
1911 full_fname(backupptr));
1912 unmake_file(back_file);
1913 back_file = NULL;
1914 close(fd);
1915 goto cleanup;
1916 }
1917 if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1918 rsyserr(FERROR_XFER, errno, "open %s", full_fname(backupptr));
1919 unmake_file(back_file);
1920 back_file = NULL;
1921 close(fd);
1922 goto cleanup;
1923 }
1924 fnamecmp_type = FNAMECMP_BACKUP;
1925 }
1926
1927 if (DEBUG_GTE(DELTASUM, 3)) {
1928 rprintf(FINFO, "gen mapped %s of size %s\n",
1929 fnamecmp, big_num(sx.st.st_size));
1930 }
1931
1932 if (DEBUG_GTE(DELTASUM, 2))
1933 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1934
1935 notify_others:
1936 if (remove_source_files && !delay_updates && !phase && !dry_run)
1937 increment_active_files(ndx, itemizing, code);
1938 if (inc_recurse && (!dry_run || write_batch < 0))
1939 cur_flist->in_progress++;
1940 #ifdef SUPPORT_HARD_LINKS
1941 if (preserve_hard_links && F_IS_HLINKED(file))
1942 file->flags |= FLAG_FILE_SENT;
1943 #endif
1944 write_ndx(f_out, ndx);
1945 if (itemizing) {
1946 int iflags = ITEM_TRANSFER;
1947 if (always_checksum > 0)
1948 iflags |= ITEM_REPORT_CHANGE;
1949 if (fnamecmp_type != FNAMECMP_FNAME)
1950 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1951 if (fnamecmp_type >= FNAMECMP_FUZZY)
1952 iflags |= ITEM_XNAME_FOLLOWS;
1953 itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
1954 fuzzy_file ? fuzzy_file->basename : NULL);
1955 free_stat_x(&real_sx);
1956 }
1957
1958 if (!do_xfers) {
1959 #ifdef SUPPORT_HARD_LINKS
1960 if (preserve_hard_links && F_IS_HLINKED(file))
1961 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1962 #endif
1963 goto cleanup;
1964 }
1965 if (read_batch)
1966 goto cleanup;
1967
1968 if (statret != 0 || whole_file)
1969 write_sum_head(f_out, NULL);
1970 else if (sx.st.st_size <= 0) {
1971 write_sum_head(f_out, NULL);
1972 close(fd);
1973 } else {
1974 if (generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy) < 0) {
1975 rprintf(FWARNING,
1976 "WARNING: file is too large for checksum sending: %s\n",
1977 fnamecmp);
1978 write_sum_head(f_out, NULL);
1979 }
1980 close(fd);
1981 }
1982
1983 cleanup:
1984 if (back_file) {
1985 int save_preserve_xattrs = preserve_xattrs;
1986 if (f_copy >= 0)
1987 close(f_copy);
1988 #ifdef SUPPORT_XATTRS
1989 if (preserve_xattrs) {
1990 copy_xattrs(fname, backupptr);
1991 preserve_xattrs = 0;
1992 }
1993 #endif
1994 set_file_attrs(backupptr, back_file, NULL, NULL, 0);
1995 preserve_xattrs = save_preserve_xattrs;
1996 if (INFO_GTE(BACKUP, 1)) {
1997 rprintf(FINFO, "backed up %s to %s\n",
1998 fname, backupptr);
1999 }
2000 unmake_file(back_file);
2001 }
2002
2003 free_stat_x(&sx);
2004 }
2005
2006 /* If we are replacing an existing hard link, symlink, device, or special file,
2007 * create a temp-name item and rename it into place. A symlimk specifies slnk,
2008 * a hard link specifies hlnk, otherwise we create a device based on rdev.
2009 * Specify 0 for the del_for_flag if there is not a file to replace. This
2010 * returns 1 on success and 0 on failure. */
atomic_create(struct file_struct * file,char * fname,const char * slnk,const char * hlnk,dev_t rdev,stat_x * sxp,int del_for_flag)2011 int atomic_create(struct file_struct *file, char *fname, const char *slnk, const char *hlnk,
2012 dev_t rdev, stat_x *sxp, int del_for_flag)
2013 {
2014 char tmpname[MAXPATHLEN];
2015 const char *create_name;
2016 int skip_atomic, dir_in_the_way = del_for_flag && S_ISDIR(sxp->st.st_mode);
2017
2018 if (!del_for_flag || dir_in_the_way || tmpdir || !get_tmpname(tmpname, fname, True))
2019 skip_atomic = 1;
2020 else
2021 skip_atomic = 0;
2022
2023 if (del_for_flag) {
2024 if (make_backups > 0 && !dir_in_the_way) {
2025 if (!make_backup(fname, skip_atomic))
2026 return 0;
2027 } else if (skip_atomic) {
2028 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
2029 if (delete_item(fname, sxp->st.st_mode, del_opts | del_for_flag) != 0)
2030 return 0;
2031 }
2032 }
2033
2034 create_name = skip_atomic ? fname : tmpname;
2035
2036 if (slnk) {
2037 #ifdef SUPPORT_LINKS
2038 if (do_symlink(slnk, create_name) < 0) {
2039 rsyserr(FERROR_XFER, errno, "symlink %s -> \"%s\" failed",
2040 full_fname(create_name), slnk);
2041 return 0;
2042 }
2043 #else
2044 return 0;
2045 #endif
2046 } else if (hlnk) {
2047 #ifdef SUPPORT_HARD_LINKS
2048 if (!hard_link_one(file, create_name, hlnk, 0))
2049 return 0;
2050 #else
2051 return 0;
2052 #endif
2053 } else {
2054 if (do_mknod(create_name, file->mode, rdev) < 0) {
2055 rsyserr(FERROR_XFER, errno, "mknod %s failed",
2056 full_fname(create_name));
2057 return 0;
2058 }
2059 }
2060
2061 if (!skip_atomic) {
2062 if (do_rename(tmpname, fname) < 0) {
2063 rsyserr(FERROR_XFER, errno, "rename %s -> \"%s\" failed",
2064 full_fname(tmpname), full_fname(fname));
2065 do_unlink(tmpname);
2066 return 0;
2067 }
2068 }
2069
2070 return 1;
2071 }
2072
2073 #ifdef SUPPORT_HARD_LINKS
handle_skipped_hlink(struct file_struct * file,int itemizing,enum logcode code,int f_out)2074 static void handle_skipped_hlink(struct file_struct *file, int itemizing,
2075 enum logcode code, int f_out)
2076 {
2077 char fbuf[MAXPATHLEN];
2078 int new_last_ndx;
2079 struct file_list *save_flist = cur_flist;
2080
2081 /* If we skip the last item in a chain of links and there was a
2082 * prior non-skipped hard-link waiting to finish, finish it now. */
2083 if ((new_last_ndx = skip_hard_link(file, &cur_flist)) < 0)
2084 return;
2085
2086 file = cur_flist->files[new_last_ndx - cur_flist->ndx_start];
2087 cur_flist->in_progress--; /* undo prior increment */
2088 f_name(file, fbuf);
2089 recv_generator(fbuf, file, new_last_ndx, itemizing, code, f_out);
2090
2091 cur_flist = save_flist;
2092 }
2093 #endif
2094
touch_up_dirs(struct file_list * flist,int ndx)2095 static void touch_up_dirs(struct file_list *flist, int ndx)
2096 {
2097 static int counter = 0;
2098 struct file_struct *file;
2099 char *fname;
2100 BOOL fix_dir_perms;
2101 int i, start, end;
2102
2103 if (ndx < 0) {
2104 start = 0;
2105 end = flist->used - 1;
2106 } else
2107 start = end = ndx;
2108
2109 /* Fix any directory permissions that were modified during the
2110 * transfer and/or re-set any tweaked modified-time values. */
2111 for (i = start; i <= end; i++, counter++) {
2112 file = flist->files[i];
2113 if (!F_IS_ACTIVE(file))
2114 continue;
2115 if (!S_ISDIR(file->mode)
2116 || (!implied_dirs && file->flags & FLAG_IMPLIED_DIR))
2117 continue;
2118 if (DEBUG_GTE(TIME, 2)) {
2119 fname = f_name(file, NULL);
2120 rprintf(FINFO, "touch_up_dirs: %s (%d)\n",
2121 NS(fname), i);
2122 }
2123 /* Be sure not to retouch permissions with --fake-super. */
2124 fix_dir_perms = !am_root && !(file->mode & S_IWUSR);
2125 if (file->flags & FLAG_MISSING_DIR || !(need_retouch_dir_times || fix_dir_perms))
2126 continue;
2127 fname = f_name(file, NULL);
2128 if (fix_dir_perms)
2129 do_chmod(fname, file->mode, 0);
2130 if (need_retouch_dir_times) {
2131 STRUCT_STAT st;
2132 if (link_stat(fname, &st, 0) == 0 && mtime_differs(&st, file)) {
2133 st.st_mtime = file->modtime;
2134 #ifdef ST_MTIME_NSEC
2135 st.ST_MTIME_NSEC = F_MOD_NSEC_or_0(file);
2136 #endif
2137 #ifdef SUPPORT_FORCE_CHANGE
2138 st.st_mode = file->mode;
2139 st.st_flags = 0;
2140 #endif
2141 set_times(fname, &st);
2142 }
2143 }
2144 #ifdef SUPPORT_FORCE_CHANGE
2145 if (force_change && F_FFLAGS(file) & force_change)
2146 undo_make_mutable(fname, F_FFLAGS(file));
2147 #endif
2148 if (counter >= loopchk_limit) {
2149 if (allowed_lull)
2150 maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
2151 else
2152 maybe_flush_socket(0);
2153 counter = 0;
2154 }
2155 }
2156 }
2157
check_for_finished_files(int itemizing,enum logcode code,int check_redo)2158 void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
2159 {
2160 struct file_struct *file;
2161 struct file_list *flist;
2162 char fbuf[MAXPATHLEN];
2163 int ndx;
2164
2165 while (1) {
2166 #ifdef SUPPORT_HARD_LINKS
2167 if (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
2168 int send_failed = (ndx == -2);
2169 if (send_failed)
2170 ndx = get_hlink_num();
2171 flist = flist_for_ndx(ndx, "check_for_finished_files.1");
2172 file = flist->files[ndx - flist->ndx_start];
2173 assert(file->flags & FLAG_HLINKED);
2174 if (send_failed)
2175 handle_skipped_hlink(file, itemizing, code, sock_f_out);
2176 else
2177 finish_hard_link(file, f_name(file, fbuf), ndx, NULL, itemizing, code, -1);
2178 flist->in_progress--;
2179 continue;
2180 }
2181 #endif
2182
2183 if (check_redo && (ndx = get_redo_num()) != -1) {
2184 OFF_T save_max_size = max_size;
2185 OFF_T save_min_size = min_size;
2186 csum_length = SUM_LENGTH;
2187 max_size = -1;
2188 min_size = -1;
2189 ignore_existing = -ignore_existing;
2190 ignore_non_existing = -ignore_non_existing;
2191 update_only = -update_only;
2192 always_checksum = -always_checksum;
2193 size_only = -size_only;
2194 append_mode = -append_mode;
2195 make_backups = -make_backups; /* avoid dup backup w/inplace */
2196 ignore_times++;
2197
2198 flist = cur_flist;
2199 cur_flist = flist_for_ndx(ndx, "check_for_finished_files.2");
2200
2201 file = cur_flist->files[ndx - cur_flist->ndx_start];
2202 if (solo_file)
2203 strlcpy(fbuf, solo_file, sizeof fbuf);
2204 else
2205 f_name(file, fbuf);
2206 recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
2207 cur_flist->to_redo--;
2208
2209 cur_flist = flist;
2210
2211 csum_length = SHORT_SUM_LENGTH;
2212 max_size = save_max_size;
2213 min_size = save_min_size;
2214 ignore_existing = -ignore_existing;
2215 ignore_non_existing = -ignore_non_existing;
2216 update_only = -update_only;
2217 always_checksum = -always_checksum;
2218 size_only = -size_only;
2219 append_mode = -append_mode;
2220 make_backups = -make_backups;
2221 ignore_times--;
2222 continue;
2223 }
2224
2225 if (cur_flist == first_flist)
2226 break;
2227
2228 /* We only get here if inc_recurse is enabled. */
2229 if (first_flist->in_progress || first_flist->to_redo)
2230 break;
2231
2232 write_ndx(sock_f_out, NDX_DONE);
2233 if (!read_batch && !flist_eof) {
2234 int old_total = 0;
2235 for (flist = first_flist; flist != cur_flist; flist = flist->next)
2236 old_total += flist->used;
2237 maybe_flush_socket(!flist_eof && file_total - old_total < MIN_FILECNT_LOOKAHEAD/2);
2238 }
2239
2240 if (delete_during == 2 || !dir_tweaking) {
2241 /* Skip directory touch-up. */
2242 } else if (first_flist->parent_ndx >= 0)
2243 touch_up_dirs(dir_flist, first_flist->parent_ndx);
2244
2245 flist_free(first_flist); /* updates first_flist */
2246 }
2247 }
2248
generate_files(int f_out,const char * local_name)2249 void generate_files(int f_out, const char *local_name)
2250 {
2251 int i, ndx, next_loopchk = 0;
2252 char fbuf[MAXPATHLEN];
2253 int itemizing;
2254 enum logcode code;
2255 int save_info_flist = info_levels[INFO_FLIST];
2256 int save_info_progress = info_levels[INFO_PROGRESS];
2257
2258 if (protocol_version >= 29) {
2259 itemizing = 1;
2260 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
2261 code = logfile_format_has_i ? FNONE : FLOG;
2262 } else if (am_daemon) {
2263 itemizing = logfile_format_has_i && do_xfers;
2264 maybe_ATTRS_REPORT = ATTRS_REPORT;
2265 code = itemizing || !do_xfers ? FCLIENT : FINFO;
2266 } else if (!am_server) {
2267 itemizing = stdout_format_has_i;
2268 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
2269 code = itemizing ? FNONE : FINFO;
2270 } else {
2271 itemizing = 0;
2272 maybe_ATTRS_REPORT = ATTRS_REPORT;
2273 code = FINFO;
2274 }
2275 solo_file = local_name;
2276 dir_tweaking = !(list_only || solo_file || dry_run);
2277 need_retouch_dir_times = preserve_times & PRESERVE_DIR_TIMES;
2278 loopchk_limit = allowed_lull ? allowed_lull * 5 : 200;
2279 symlink_timeset_failed_flags = ITEM_REPORT_TIME
2280 | (protocol_version >= 30 || !am_server ? ITEM_REPORT_TIMEFAIL : 0);
2281 implied_dirs_are_missing = relative_paths && !implied_dirs && protocol_version < 30;
2282
2283 if (DEBUG_GTE(GENR, 1))
2284 rprintf(FINFO, "generator starting pid=%d\n", (int)getpid());
2285
2286 if (delete_before && !solo_file && cur_flist->used > 0)
2287 do_delete_pass();
2288 if (delete_during == 2) {
2289 deldelay_size = BIGPATHBUFLEN * 4;
2290 deldelay_buf = new_array(char, deldelay_size);
2291 }
2292 info_levels[INFO_FLIST] = info_levels[INFO_PROGRESS] = 0;
2293
2294 if (append_mode > 0 || whole_file < 0)
2295 whole_file = 0;
2296 if (DEBUG_GTE(FLIST, 1)) {
2297 rprintf(FINFO, "delta-transmission %s\n",
2298 whole_file
2299 ? "disabled for local transfer or --whole-file"
2300 : "enabled");
2301 }
2302
2303 dflt_perms = (ACCESSPERMS & ~orig_umask);
2304
2305 do {
2306 #ifdef SUPPORT_HARD_LINKS
2307 if (preserve_hard_links && inc_recurse) {
2308 while (!flist_eof && file_total < MIN_FILECNT_LOOKAHEAD/2)
2309 wait_for_receiver();
2310 }
2311 #endif
2312
2313 if (inc_recurse && cur_flist->parent_ndx >= 0) {
2314 struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
2315 if (solo_file)
2316 strlcpy(fbuf, solo_file, sizeof fbuf);
2317 else
2318 f_name(fp, fbuf);
2319 ndx = cur_flist->ndx_start - 1;
2320 recv_generator(fbuf, fp, ndx, itemizing, code, f_out);
2321 if (delete_during && dry_run < 2 && !list_only
2322 && !(fp->flags & FLAG_MISSING_DIR)) {
2323 if (fp->flags & FLAG_CONTENT_DIR) {
2324 dev_t dirdev;
2325 if (one_file_system) {
2326 uint32 *devp = F_DIR_DEV_P(fp);
2327 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
2328 } else
2329 dirdev = MAKEDEV(0, 0);
2330 delete_in_dir(fbuf, fp, &dirdev);
2331 } else
2332 change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(fp));
2333 }
2334 }
2335 for (i = cur_flist->low; i <= cur_flist->high; i++) {
2336 struct file_struct *file = cur_flist->sorted[i];
2337
2338 if (!F_IS_ACTIVE(file))
2339 continue;
2340
2341 if (unsort_ndx)
2342 ndx = F_NDX(file);
2343 else
2344 ndx = i + cur_flist->ndx_start;
2345
2346 if (solo_file)
2347 strlcpy(fbuf, solo_file, sizeof fbuf);
2348 else
2349 f_name(file, fbuf);
2350 recv_generator(fbuf, file, ndx, itemizing, code, f_out);
2351
2352 check_for_finished_files(itemizing, code, 0);
2353
2354 if (i + cur_flist->ndx_start >= next_loopchk) {
2355 if (allowed_lull)
2356 maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
2357 else
2358 maybe_flush_socket(0);
2359 next_loopchk += loopchk_limit;
2360 }
2361 }
2362
2363 if (!inc_recurse) {
2364 write_ndx(f_out, NDX_DONE);
2365 break;
2366 }
2367
2368 while (1) {
2369 check_for_finished_files(itemizing, code, 1);
2370 if (cur_flist->next || flist_eof)
2371 break;
2372 wait_for_receiver();
2373 }
2374 } while ((cur_flist = cur_flist->next) != NULL);
2375
2376 if (delete_during)
2377 delete_in_dir(NULL, NULL, &dev_zero);
2378 phase++;
2379 if (DEBUG_GTE(GENR, 1))
2380 rprintf(FINFO, "generate_files phase=%d\n", phase);
2381
2382 while (1) {
2383 check_for_finished_files(itemizing, code, 1);
2384 if (msgdone_cnt)
2385 break;
2386 wait_for_receiver();
2387 }
2388
2389 phase++;
2390 if (DEBUG_GTE(GENR, 1))
2391 rprintf(FINFO, "generate_files phase=%d\n", phase);
2392
2393 write_ndx(f_out, NDX_DONE);
2394
2395 /* Reduce round-trip lag-time for a useless delay-updates phase. */
2396 if (protocol_version >= 29 && EARLY_DELAY_DONE_MSG())
2397 write_ndx(f_out, NDX_DONE);
2398
2399 if (protocol_version >= 31 && EARLY_DELETE_DONE_MSG()) {
2400 if ((INFO_GTE(STATS, 2) && (delete_mode || force_delete)) || read_batch)
2401 write_del_stats(f_out);
2402 if (EARLY_DELAY_DONE_MSG()) /* Can't send this before delay */
2403 write_ndx(f_out, NDX_DONE);
2404 }
2405
2406 /* Read MSG_DONE for the redo phase (and any prior messages). */
2407 while (1) {
2408 check_for_finished_files(itemizing, code, 0);
2409 if (msgdone_cnt > 1)
2410 break;
2411 wait_for_receiver();
2412 }
2413
2414 if (protocol_version >= 29) {
2415 phase++;
2416 if (DEBUG_GTE(GENR, 1))
2417 rprintf(FINFO, "generate_files phase=%d\n", phase);
2418 if (!EARLY_DELAY_DONE_MSG()) {
2419 write_ndx(f_out, NDX_DONE);
2420 if (protocol_version >= 31 && EARLY_DELETE_DONE_MSG())
2421 write_ndx(f_out, NDX_DONE);
2422 }
2423 /* Read MSG_DONE for delay-updates phase & prior messages. */
2424 while (msgdone_cnt == 2)
2425 wait_for_receiver();
2426 }
2427
2428 info_levels[INFO_FLIST] = save_info_flist;
2429 info_levels[INFO_PROGRESS] = save_info_progress;
2430
2431 if (delete_during == 2)
2432 do_delayed_deletions(fbuf);
2433 if (delete_after && !solo_file && file_total > 0)
2434 do_delete_pass();
2435
2436 if (max_delete >= 0 && skipped_deletes) {
2437 rprintf(FWARNING,
2438 "Deletions stopped due to --max-delete limit (%d skipped)\n",
2439 skipped_deletes);
2440 io_error |= IOERR_DEL_LIMIT;
2441 }
2442
2443 if (protocol_version >= 31) {
2444 if (!EARLY_DELETE_DONE_MSG()) {
2445 if (INFO_GTE(STATS, 2) || read_batch)
2446 write_del_stats(f_out);
2447 write_ndx(f_out, NDX_DONE);
2448 }
2449
2450 /* Read MSG_DONE for late-delete phase & prior messages. */
2451 while (msgdone_cnt == 3)
2452 wait_for_receiver();
2453 }
2454
2455 if ((need_retouch_dir_perms || need_retouch_dir_times)
2456 && dir_tweaking && (!inc_recurse || delete_during == 2))
2457 touch_up_dirs(dir_flist, -1);
2458
2459 if (DEBUG_GTE(GENR, 1))
2460 rprintf(FINFO, "generate_files finished\n");
2461 }
2462