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