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