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