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