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