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