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