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