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