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