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