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