Reverted one xfer_dirs change from the last commit that should not
[rsync/rsync.git] / generator.c
... / ...
CommitLineData
1/* -*- c-file-style: "linux" -*-
2
3 rsync -- fast file replication program
4
5 Copyright (C) 1996-2000 by Andrew Tridgell
6 Copyright (C) Paul Mackerras 1996
7 Copyright (C) 2002 by Martin Pool <mbp@samba.org>
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
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24#include "rsync.h"
25
26extern int verbose;
27extern int dry_run;
28extern int do_xfers;
29extern int log_format_has_i;
30extern int log_format_has_o_or_i;
31extern int daemon_log_format_has_i;
32extern int am_root;
33extern int am_server;
34extern int am_daemon;
35extern int do_progress;
36extern int recurse;
37extern int relative_paths;
38extern int keep_dirlinks;
39extern int preserve_links;
40extern int preserve_devices;
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_before;
48extern int delete_during;
49extern int delete_after;
50extern int module_id;
51extern int ignore_errors;
52extern int remove_sent_files;
53extern int delay_updates;
54extern int update_only;
55extern int opt_ignore_existing;
56extern int inplace;
57extern int append_mode;
58extern int make_backups;
59extern int csum_length;
60extern int ignore_times;
61extern int size_only;
62extern OFF_T max_size;
63extern int io_error;
64extern int allowed_lull;
65extern int sock_f_out;
66extern int ignore_timeout;
67extern int protocol_version;
68extern int fuzzy_basis;
69extern int always_checksum;
70extern char *partial_dir;
71extern char *basis_dir[];
72extern int compare_dest;
73extern int copy_dest;
74extern int link_dest;
75extern int whole_file;
76extern int list_only;
77extern int read_batch;
78extern int only_existing;
79extern int orig_umask;
80extern int safe_symlinks;
81extern long block_size; /* "long" because popt can't set an int32. */
82extern int max_delete;
83extern int force_delete;
84extern int one_file_system;
85extern struct stats stats;
86extern dev_t filesystem_dev;
87extern char *backup_dir;
88extern char *backup_suffix;
89extern int backup_suffix_len;
90extern struct file_list *the_file_list;
91extern struct filter_list_struct server_filter_list;
92
93static int deletion_count = 0; /* used to implement --max-delete */
94
95
96static int is_backup_file(char *fn)
97{
98 int k = strlen(fn) - backup_suffix_len;
99 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
100}
101
102
103/* Delete a file or directory. If DEL_FORCE_RECURSE is set in the flags, or if
104 * force_delete is set, this will delete recursively as long as DEL_NO_RECURSE
105 * is not set in the flags. */
106static int delete_item(char *fname, int mode, int flags)
107{
108 struct file_list *dirlist;
109 char buf[MAXPATHLEN];
110 int j, dlen, zap_dir, ok;
111 void *save_filters;
112
113 if (!S_ISDIR(mode)) {
114 if (max_delete && ++deletion_count > max_delete)
115 return 0;
116 if (make_backups && (backup_dir || !is_backup_file(fname)))
117 ok = make_backup(fname);
118 else
119 ok = robust_unlink(fname) == 0;
120 if (ok) {
121 if (!(flags & DEL_TERSE))
122 log_delete(fname, mode);
123 return 0;
124 }
125 if (errno == ENOENT) {
126 deletion_count--;
127 return 0;
128 }
129 rsyserr(FERROR, errno, "delete_file: unlink %s failed",
130 full_fname(fname));
131 return -1;
132 }
133
134 zap_dir = (flags & DEL_FORCE_RECURSE || (force_delete && recurse))
135 && !(flags & DEL_NO_RECURSE);
136 if ((max_delete && ++deletion_count > max_delete)
137 || (dry_run && zap_dir)) {
138 ok = 0;
139 errno = ENOTEMPTY;
140 } else if (make_backups && !backup_dir && !is_backup_file(fname)
141 && !(flags & DEL_FORCE_RECURSE))
142 ok = make_backup(fname);
143 else
144 ok = do_rmdir(fname) == 0;
145 if (ok) {
146 if (!(flags & DEL_TERSE))
147 log_delete(fname, mode);
148 return 0;
149 }
150 if (errno == ENOENT) {
151 deletion_count--;
152 return 0;
153 }
154 if (!zap_dir) {
155 rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
156 full_fname(fname));
157 return -1;
158 }
159 flags |= DEL_FORCE_RECURSE; /* mark subdir dels as not "in the way" */
160 deletion_count--;
161
162 dlen = strlcpy(buf, fname, MAXPATHLEN);
163 save_filters = push_local_filters(buf, dlen);
164
165 dirlist = get_dirlist(buf, dlen, 0);
166 for (j = dirlist->count; j--; ) {
167 struct file_struct *fp = dirlist->files[j];
168
169 if (fp->flags & FLAG_MOUNT_POINT)
170 continue;
171
172 f_name_to(fp, buf);
173 delete_item(buf, fp->mode, flags & ~DEL_TERSE);
174 }
175 flist_free(dirlist);
176
177 pop_local_filters(save_filters);
178
179 if (max_delete && ++deletion_count > max_delete)
180 return 0;
181
182 if (do_rmdir(fname) == 0) {
183 if (!(flags & DEL_TERSE))
184 log_delete(fname, mode);
185 } else if (errno != ENOTEMPTY && errno != EEXIST && errno != ENOENT) {
186 rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
187 full_fname(fname));
188 return -1;
189 }
190
191 return 0;
192}
193
194
195/* This function is used to implement per-directory deletion, and is used by
196 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
197 * MAXPATHLEN buffer with the name of the directory in it (the functions we
198 * call will append names onto the end, but the old dir value will be restored
199 * on exit). */
200static void delete_in_dir(struct file_list *flist, char *fbuf,
201 struct file_struct *file)
202{
203 static int min_depth = MAXPATHLEN, cur_depth = -1;
204 static void *filt_array[MAXPATHLEN/2+1];
205 static int already_warned = 0;
206 struct file_list *dirlist;
207 char delbuf[MAXPATHLEN];
208 STRUCT_STAT st;
209 int dlen, i;
210
211 if (!flist) {
212 while (cur_depth >= min_depth)
213 pop_local_filters(filt_array[cur_depth--]);
214 min_depth = MAXPATHLEN;
215 cur_depth = -1;
216 return;
217 }
218
219 if (verbose > 2)
220 rprintf(FINFO, "delete_in_dir(%s)\n", safe_fname(fbuf));
221
222 if (allowed_lull)
223 maybe_send_keepalive();
224
225 if (file->dir.depth >= MAXPATHLEN/2+1)
226 return; /* Impossible... */
227
228 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
229 if (already_warned)
230 return;
231 rprintf(FINFO,
232 "IO error encountered -- skipping file deletion\n");
233 already_warned = 1;
234 return;
235 }
236
237 while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
238 pop_local_filters(filt_array[cur_depth--]);
239 cur_depth = file->dir.depth;
240 if (min_depth > cur_depth)
241 min_depth = cur_depth;
242 dlen = strlen(fbuf);
243 filt_array[cur_depth] = push_local_filters(fbuf, dlen);
244
245 if (link_stat(fbuf, &st, keep_dirlinks) < 0)
246 return;
247
248 if (one_file_system) {
249 if (file->flags & FLAG_TOP_DIR)
250 filesystem_dev = st.st_dev;
251 else if (filesystem_dev != st.st_dev)
252 return;
253 }
254
255 dirlist = get_dirlist(fbuf, dlen, 0);
256
257 /* If an item in dirlist is not found in flist, delete it
258 * from the filesystem. */
259 for (i = dirlist->count; i--; ) {
260 struct file_struct *fp = dirlist->files[i];
261 if (!fp->basename || fp->flags & FLAG_MOUNT_POINT)
262 continue;
263 if (flist_find(flist, fp) < 0) {
264 int mode = fp->mode;
265 f_name_to(fp, delbuf);
266 delete_item(delbuf, mode, DEL_FORCE_RECURSE);
267 }
268 }
269
270 flist_free(dirlist);
271}
272
273/* This deletes any files on the receiving side that are not present on the
274 * sending side. This is used by --delete-before and --delete-after. */
275static void do_delete_pass(struct file_list *flist)
276{
277 char fbuf[MAXPATHLEN];
278 int j;
279
280 if (dry_run > 1 /* destination doesn't exist yet */
281 || list_only)
282 return;
283
284 for (j = 0; j < flist->count; j++) {
285 struct file_struct *file = flist->files[j];
286
287 if (!(file->flags & FLAG_DEL_HERE))
288 continue;
289
290 f_name_to(file, fbuf);
291 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
292 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
293
294 delete_in_dir(flist, fbuf, file);
295 }
296 if (do_progress && !am_server)
297 rprintf(FINFO, " \r");
298}
299
300static int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
301{
302 if (preserve_perms
303 && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
304 return 0;
305
306 if (am_root && preserve_uid && st->st_uid != file->uid)
307 return 0;
308
309 if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid)
310 return 0;
311
312 return 1;
313}
314
315
316void itemize(struct file_struct *file, int ndx, int statret, STRUCT_STAT *st,
317 int32 iflags, uchar fnamecmp_type, char *xname)
318{
319 if (statret == 0) {
320 if (S_ISREG(file->mode) && file->length != st->st_size)
321 iflags |= ITEM_REPORT_SIZE;
322 if (!(iflags & ITEM_NO_DEST_AND_NO_UPDATE)) {
323 int keep_time = !preserve_times ? 0
324 : S_ISDIR(file->mode) ? !omit_dir_times
325 : !S_ISLNK(file->mode);
326
327 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
328 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
329 || (keep_time && cmp_modtime(file->modtime, st->st_mtime) != 0))
330 iflags |= ITEM_REPORT_TIME;
331 if (preserve_perms
332 && (file->mode & CHMOD_BITS) != (st->st_mode & CHMOD_BITS))
333 iflags |= ITEM_REPORT_PERMS;
334 if (preserve_uid && am_root && file->uid != st->st_uid)
335 iflags |= ITEM_REPORT_OWNER;
336 if (preserve_gid && file->gid != GID_NONE
337 && st->st_gid != file->gid)
338 iflags |= ITEM_REPORT_GROUP;
339 }
340 } else
341 iflags |= ITEM_IS_NEW;
342
343 iflags &= 0xffff;
344 if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
345 || (xname && *xname)) && !read_batch) {
346 if (protocol_version >= 29) {
347 if (ndx >= 0)
348 write_int(sock_f_out, ndx);
349 write_shortint(sock_f_out, iflags);
350 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
351 write_byte(sock_f_out, fnamecmp_type);
352 if (iflags & ITEM_XNAME_FOLLOWS)
353 write_vstring(sock_f_out, xname, strlen(xname));
354 } else if (ndx >= 0)
355 log_item(file, &stats, iflags, xname);
356 }
357}
358
359
360/* Perform our quick-check heuristic for determining if a file is unchanged. */
361static int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
362{
363 if (st->st_size != file->length)
364 return 0;
365
366 /* if always checksum is set then we use the checksum instead
367 of the file time to determine whether to sync */
368 if (always_checksum && S_ISREG(st->st_mode)) {
369 char sum[MD4_SUM_LENGTH];
370 file_checksum(fn, sum, st->st_size);
371 return memcmp(sum, file->u.sum, protocol_version < 21 ? 2
372 : MD4_SUM_LENGTH) == 0;
373 }
374
375 if (size_only)
376 return 1;
377
378 if (ignore_times)
379 return 0;
380
381 return cmp_modtime(st->st_mtime, file->modtime) == 0;
382}
383
384
385/*
386 * set (initialize) the size entries in the per-file sum_struct
387 * calculating dynamic block and checksum sizes.
388 *
389 * This is only called from generate_and_send_sums() but is a separate
390 * function to encapsulate the logic.
391 *
392 * The block size is a rounded square root of file length.
393 *
394 * The checksum size is determined according to:
395 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
396 * provided by Donovan Baarda which gives a probability of rsync
397 * algorithm corrupting data and falling back using the whole md4
398 * checksums.
399 *
400 * This might be made one of several selectable heuristics.
401 */
402static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
403{
404 int32 blength;
405 int s2length;
406
407 if (block_size)
408 blength = block_size;
409 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
410 blength = BLOCK_SIZE;
411 else {
412 int32 c;
413 int64 l;
414 int cnt;
415 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
416 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
417 blength = MAX_BLOCK_SIZE;
418 else {
419 blength = 0;
420 do {
421 blength |= c;
422 if (len < (int64)blength * blength)
423 blength &= ~c;
424 c >>= 1;
425 } while (c >= 8); /* round to multiple of 8 */
426 blength = MAX(blength, BLOCK_SIZE);
427 }
428 }
429
430 if (protocol_version < 27) {
431 s2length = csum_length;
432 } else if (csum_length == SUM_LENGTH) {
433 s2length = SUM_LENGTH;
434 } else {
435 int32 c;
436 int64 l;
437 int b = BLOCKSUM_BIAS;
438 for (l = len; l >>= 1; b += 2) {}
439 for (c = blength; c >>= 1 && b; b--) {}
440 /* add a bit, subtract rollsum, round up. */
441 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
442 s2length = MAX(s2length, csum_length);
443 s2length = MIN(s2length, SUM_LENGTH);
444 }
445
446 sum->flength = len;
447 sum->blength = blength;
448 sum->s2length = s2length;
449 sum->remainder = len % blength;
450 sum->count = len / blength + (sum->remainder != 0);
451
452 if (sum->count && verbose > 2) {
453 rprintf(FINFO,
454 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
455 (double)sum->count, (long)sum->remainder, (long)sum->blength,
456 sum->s2length, (double)sum->flength);
457 }
458}
459
460
461/*
462 * Generate and send a stream of signatures/checksums that describe a buffer
463 *
464 * Generate approximately one checksum every block_len bytes.
465 */
466static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
467{
468 int32 i;
469 struct map_struct *mapbuf;
470 struct sum_struct sum;
471 OFF_T offset = 0;
472
473 sum_sizes_sqroot(&sum, len);
474 write_sum_head(f_out, &sum);
475
476 if (append_mode > 0 && f_copy < 0)
477 return;
478
479 if (len > 0)
480 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
481 else
482 mapbuf = NULL;
483
484 for (i = 0; i < sum.count; i++) {
485 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
486 char *map = map_ptr(mapbuf, offset, n1);
487 char sum2[SUM_LENGTH];
488 uint32 sum1;
489
490 len -= n1;
491 offset += n1;
492
493 if (f_copy >= 0) {
494 full_write(f_copy, map, n1);
495 if (append_mode > 0)
496 continue;
497 }
498
499 sum1 = get_checksum1(map, n1);
500 get_checksum2(map, n1, sum2);
501
502 if (verbose > 3) {
503 rprintf(FINFO,
504 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
505 (double)i, (double)offset - n1, (long)n1,
506 (unsigned long)sum1);
507 }
508 write_int(f_out, sum1);
509 write_buf(f_out, sum2, sum.s2length);
510 }
511
512 if (mapbuf)
513 unmap_file(mapbuf);
514}
515
516
517/* Try to find a filename in the same dir as "fname" with a similar name. */
518static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
519{
520 int fname_len, fname_suf_len;
521 const char *fname_suf, *fname = file->basename;
522 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
523 int j, lowest_j = -1;
524
525 fname_len = strlen(fname);
526 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
527
528 for (j = 0; j < dirlist->count; j++) {
529 struct file_struct *fp = dirlist->files[j];
530 const char *suf, *name;
531 int len, suf_len;
532 uint32 dist;
533
534 if (!S_ISREG(fp->mode) || !fp->length
535 || fp->flags & FLAG_NO_FUZZY)
536 continue;
537
538 name = fp->basename;
539
540 if (fp->length == file->length
541 && cmp_modtime(fp->modtime, file->modtime) == 0) {
542 if (verbose > 4) {
543 rprintf(FINFO,
544 "fuzzy size/modtime match for %s\n",
545 name);
546 }
547 return j;
548 }
549
550 len = strlen(name);
551 suf = find_filename_suffix(name, len, &suf_len);
552
553 dist = fuzzy_distance(name, len, fname, fname_len);
554 /* Add some extra weight to how well the suffixes match. */
555 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
556 * 10;
557 if (verbose > 4) {
558 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
559 name, (int)(dist>>16), (int)(dist&0xFFFF));
560 }
561 if (dist <= lowest_dist) {
562 lowest_dist = dist;
563 lowest_j = j;
564 }
565 }
566
567 return lowest_j;
568}
569
570void check_for_finished_hlinks(int itemizing, enum logcode code)
571{
572 struct file_struct *file;
573 int ndx;
574
575 while ((ndx = get_hlink_num()) != -1) {
576 if (ndx < 0 || ndx >= the_file_list->count)
577 continue;
578
579 file = the_file_list->files[ndx];
580 if (!file->link_u.links)
581 continue;
582
583 hard_link_cluster(file, ndx, itemizing, code);
584 }
585}
586
587static int phase = 0;
588
589/* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir,
590 * make sure it exists, and has the right permissions/timestamp info. For
591 * all other non-regular files (symlinks, etc.) we create them here. For
592 * regular files that have changed, we try to find a basis file and then
593 * start sending checksums.
594 *
595 * Note that f_out is set to -1 when doing final directory-permission and
596 * modification-time repair. */
597static void recv_generator(char *fname, struct file_struct *file, int ndx,
598 int itemizing, int maybe_PERMS_REPORT,
599 enum logcode code, int f_out)
600{
601 static int missing_below = -1, excluded_below = -1;
602 static char *fuzzy_dirname = "";
603 static struct file_list *fuzzy_dirlist = NULL;
604 struct file_struct *fuzzy_file = NULL;
605 int fd = -1, f_copy = -1;
606 STRUCT_STAT st, real_st, partial_st;
607 struct file_struct *back_file = NULL;
608 int statret, real_ret, stat_errno;
609 char *fnamecmp, *partialptr, *backupptr = NULL;
610 char fnamecmpbuf[MAXPATHLEN];
611 uchar fnamecmp_type;
612
613 if (list_only)
614 return;
615
616 if (!fname) {
617 if (fuzzy_dirlist) {
618 flist_free(fuzzy_dirlist);
619 fuzzy_dirlist = NULL;
620 fuzzy_dirname = "";
621 }
622 if (missing_below >= 0) {
623 dry_run--;
624 missing_below = -1;
625 }
626 return;
627 }
628
629 if (verbose > 2) {
630 rprintf(FINFO, "recv_generator(%s,%d)\n",
631 safe_fname(fname), ndx);
632 }
633
634 if (server_filter_list.head) {
635 if (excluded_below >= 0) {
636 if (file->dir.depth > excluded_below)
637 goto skipping;
638 excluded_below = -1;
639 }
640 if (check_filter(&server_filter_list, fname,
641 S_ISDIR(file->mode)) < 0) {
642 if (S_ISDIR(file->mode))
643 excluded_below = file->dir.depth;
644 skipping:
645 if (verbose) {
646 rprintf(FINFO,
647 "skipping server-excluded file \"%s\"\n",
648 safe_fname(fname));
649 }
650 return;
651 }
652 }
653
654 if (missing_below >= 0 && file->dir.depth <= missing_below) {
655 dry_run--;
656 missing_below = -1;
657 }
658 if (dry_run > 1) {
659 statret = -1;
660 stat_errno = ENOENT;
661 } else {
662 if (fuzzy_basis && S_ISREG(file->mode)) {
663 char *dn = file->dirname ? file->dirname : ".";
664 if (fuzzy_dirname != dn
665 && strcmp(fuzzy_dirname, dn) != 0) {
666 if (fuzzy_dirlist)
667 flist_free(fuzzy_dirlist);
668 fuzzy_dirlist = get_dirlist(dn, -1, 1);
669 }
670 fuzzy_dirname = dn;
671 }
672
673 statret = link_stat(fname, &st,
674 keep_dirlinks && S_ISDIR(file->mode));
675 stat_errno = errno;
676 }
677
678 if (only_existing && statret == -1 && stat_errno == ENOENT) {
679 /* we only want to update existing files */
680 if (verbose > 1) {
681 rprintf(FINFO, "not creating new %s \"%s\"\n",
682 S_ISDIR(file->mode) ? "directory" : "file",
683 safe_fname(fname));
684 }
685 return;
686 }
687
688 if (statret == 0 && !preserve_perms
689 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode)) {
690 /* if the file exists already and we aren't perserving
691 * permissions then act as though the remote end sent
692 * us the file permissions we already have */
693 file->mode = (file->mode & ~CHMOD_BITS)
694 | (st.st_mode & CHMOD_BITS);
695 }
696
697 if (S_ISDIR(file->mode)) {
698 /* The file to be received is a directory, so we need
699 * to prepare appropriately. If there is already a
700 * file of that name and it is *not* a directory, then
701 * we need to delete it. If it doesn't exist, then
702 * (perhaps recursively) create it. */
703 if (statret == 0 && !S_ISDIR(st.st_mode)) {
704 if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
705 return;
706 statret = -1;
707 }
708 if (dry_run && statret != 0 && missing_below < 0) {
709 missing_below = file->dir.depth;
710 dry_run++;
711 }
712 if (itemizing && f_out != -1) {
713 itemize(file, ndx, statret, &st,
714 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
715 }
716 if (statret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
717 if (!relative_paths || errno != ENOENT
718 || create_directory_path(fname, orig_umask) < 0
719 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
720 rsyserr(FERROR, errno,
721 "recv_generator: mkdir %s failed",
722 full_fname(fname));
723 }
724 }
725 if (set_perms(fname, file, statret ? NULL : &st, 0)
726 && verbose && code && f_out != -1)
727 rprintf(code, "%s/\n", safe_fname(fname));
728 if (delete_during && f_out != -1 && !phase && dry_run < 2
729 && (file->flags & FLAG_DEL_HERE))
730 delete_in_dir(the_file_list, fname, file);
731 return;
732 }
733
734 if (preserve_hard_links && file->link_u.links
735 && hard_link_check(file, ndx, fname, statret, &st,
736 itemizing, code, HL_CHECK_MASTER))
737 return;
738
739 if (preserve_links && S_ISLNK(file->mode)) {
740#ifdef SUPPORT_LINKS
741 if (safe_symlinks && unsafe_symlink(file->u.link, fname)) {
742 if (verbose) {
743 if (the_file_list->count == 1)
744 fname = f_name(file);
745 rprintf(FINFO,
746 "ignoring unsafe symlink %s -> \"%s\"\n",
747 full_fname(fname),
748 safe_fname(file->u.link));
749 }
750 return;
751 }
752 if (statret == 0) {
753 char lnk[MAXPATHLEN];
754 int len;
755
756 if (!S_ISDIR(st.st_mode)
757 && (len = readlink(fname, lnk, MAXPATHLEN-1)) > 0) {
758 lnk[len] = 0;
759 /* A link already pointing to the
760 * right place -- no further action
761 * required. */
762 if (strcmp(lnk, file->u.link) == 0) {
763 if (itemizing) {
764 itemize(file, ndx, 0, &st, 0,
765 0, NULL);
766 }
767 set_perms(fname, file, &st,
768 maybe_PERMS_REPORT);
769 if (preserve_hard_links
770 && file->link_u.links) {
771 hard_link_cluster(file, ndx,
772 itemizing,
773 code);
774 }
775 return;
776 }
777 }
778 /* Not the right symlink (or not a symlink), so
779 * delete it. */
780 if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
781 return;
782 if (!S_ISLNK(st.st_mode))
783 statret = -1;
784 }
785 if (preserve_hard_links && file->link_u.links
786 && hard_link_check(file, ndx, fname, -1, &st,
787 itemizing, code, HL_SKIP))
788 return;
789 if (do_symlink(file->u.link,fname) != 0) {
790 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
791 full_fname(fname), safe_fname(file->u.link));
792 } else {
793 set_perms(fname,file,NULL,0);
794 if (itemizing) {
795 itemize(file, ndx, statret, &st,
796 ITEM_LOCAL_CHANGE, 0, NULL);
797 }
798 if (code && verbose) {
799 rprintf(code, "%s -> %s\n", safe_fname(fname),
800 safe_fname(file->u.link));
801 }
802 if (remove_sent_files && !dry_run) {
803 char numbuf[4];
804 SIVAL(numbuf, 0, ndx);
805 send_msg(MSG_SUCCESS, numbuf, 4);
806 }
807 if (preserve_hard_links && file->link_u.links)
808 hard_link_cluster(file, ndx, itemizing, code);
809 }
810#endif
811 return;
812 }
813
814 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
815 if (statret != 0
816 || (st.st_mode & ~CHMOD_BITS) != (file->mode & ~CHMOD_BITS)
817 || st.st_rdev != file->u.rdev) {
818 if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
819 return;
820 if (preserve_hard_links && file->link_u.links
821 && hard_link_check(file, ndx, fname, -1, &st,
822 itemizing, code, HL_SKIP))
823 return;
824 if (!IS_DEVICE(st.st_mode))
825 statret = -1;
826 if (verbose > 2) {
827 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
828 safe_fname(fname),
829 (int)file->mode, (int)file->u.rdev);
830 }
831 if (do_mknod(fname,file->mode,file->u.rdev) < 0) {
832 rsyserr(FERROR, errno, "mknod %s failed",
833 full_fname(fname));
834 } else {
835 set_perms(fname,file,NULL,0);
836 if (itemizing) {
837 itemize(file, ndx, statret, &st,
838 ITEM_LOCAL_CHANGE, 0, NULL);
839 }
840 if (code && verbose) {
841 rprintf(code, "%s\n",
842 safe_fname(fname));
843 }
844 if (preserve_hard_links && file->link_u.links) {
845 hard_link_cluster(file, ndx,
846 itemizing, code);
847 }
848 }
849 } else {
850 if (itemizing)
851 itemize(file, ndx, statret, &st, 0, 0, NULL);
852 set_perms(fname, file, &st, maybe_PERMS_REPORT);
853 if (preserve_hard_links && file->link_u.links)
854 hard_link_cluster(file, ndx, itemizing, code);
855 }
856 return;
857 }
858
859 if (!S_ISREG(file->mode)) {
860 if (the_file_list->count == 1)
861 fname = f_name(file);
862 rprintf(FINFO, "skipping non-regular file \"%s\"\n",
863 safe_fname(fname));
864 return;
865 }
866
867 if (max_size && file->length > max_size) {
868 if (verbose > 1) {
869 if (the_file_list->count == 1)
870 fname = f_name(file);
871 rprintf(FINFO, "%s is over max-size\n",
872 safe_fname(fname));
873 }
874 return;
875 }
876
877 if (opt_ignore_existing && statret == 0) {
878 if (verbose > 1)
879 rprintf(FINFO, "%s exists\n", safe_fname(fname));
880 return;
881 }
882
883 if (update_only && statret == 0
884 && cmp_modtime(st.st_mtime, file->modtime) > 0) {
885 if (verbose > 1)
886 rprintf(FINFO, "%s is newer\n", safe_fname(fname));
887 return;
888 }
889
890 fnamecmp = fname;
891 fnamecmp_type = FNAMECMP_FNAME;
892
893 if (statret == 0 && !S_ISREG(st.st_mode)) {
894 if (delete_item(fname, st.st_mode, DEL_TERSE) != 0)
895 return;
896 statret = -1;
897 stat_errno = ENOENT;
898 }
899
900 if (statret != 0 && basis_dir[0] != NULL) {
901 int best_match = -1;
902 int match_level = 0;
903 int i = 0;
904 do {
905 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
906 basis_dir[i], fname);
907 if (link_stat(fnamecmpbuf, &st, 0) < 0
908 || !S_ISREG(st.st_mode))
909 continue;
910 switch (match_level) {
911 case 0:
912 best_match = i;
913 match_level = 1;
914 /* FALL THROUGH */
915 case 1:
916 if (!unchanged_file(fnamecmpbuf, file, &st))
917 continue;
918 best_match = i;
919 match_level = 2;
920 if (copy_dest)
921 break;
922 /* FALL THROUGH */
923 case 2:
924 if (!unchanged_attrs(file, &st))
925 continue;
926 best_match = i;
927 match_level = 3;
928 break;
929 }
930 break;
931 } while (basis_dir[++i] != NULL);
932 if (match_level) {
933 statret = 0;
934 if (i != best_match) {
935 i = best_match;
936 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
937 basis_dir[i], fname);
938 if (link_stat(fnamecmpbuf, &st, 0) < 0) {
939 match_level = 0;
940 statret = -1;
941 stat_errno = errno;
942 }
943 }
944#ifdef HAVE_LINK
945 if (link_dest && match_level == 3) {
946 if (hard_link_one(file, ndx, fname, -1, &st,
947 fnamecmpbuf, 1,
948 itemizing && verbose > 1,
949 code) == 0) {
950 if (preserve_hard_links
951 && file->link_u.links) {
952 hard_link_cluster(file, ndx,
953 itemizing,
954 code);
955 }
956 return;
957 }
958 match_level = 2;
959 }
960#endif
961 if (match_level == 2) {
962 /* Copy the file locally. */
963 if (copy_file(fnamecmpbuf, fname, file->mode) < 0) {
964 if (verbose) {
965 rsyserr(FINFO, errno,
966 "copy_file %s => %s",
967 full_fname(fnamecmpbuf),
968 safe_fname(fname));
969 }
970 match_level = 0;
971 statret = -1;
972 } else {
973 if (itemizing) {
974 itemize(file, ndx, 0, &st,
975 ITEM_LOCAL_CHANGE, 0,
976 NULL);
977 } else if (verbose && code) {
978 rprintf(code, "%s\n",
979 safe_fname(fname));
980 }
981 set_perms(fname, file, NULL,
982 maybe_PERMS_REPORT);
983 if (preserve_hard_links
984 && file->link_u.links) {
985 hard_link_cluster(file, ndx,
986 itemizing,
987 code);
988 }
989 return;
990 }
991 } else if (compare_dest || match_level == 1) {
992 fnamecmp = fnamecmpbuf;
993 fnamecmp_type = i;
994 }
995 }
996 }
997
998 real_ret = statret;
999 real_st = st;
1000
1001 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1002 && link_stat(partialptr, &partial_st, 0) == 0
1003 && S_ISREG(partial_st.st_mode)) {
1004 if (statret != 0)
1005 goto prepare_to_open;
1006 } else
1007 partialptr = NULL;
1008
1009 if (statret != 0 && fuzzy_basis && dry_run <= 1) {
1010 int j = find_fuzzy(file, fuzzy_dirlist);
1011 if (j >= 0) {
1012 fuzzy_file = fuzzy_dirlist->files[j];
1013 f_name_to(fuzzy_file, fnamecmpbuf);
1014 if (verbose > 2) {
1015 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1016 safe_fname(fname), safe_fname(fnamecmpbuf));
1017 }
1018 st.st_size = fuzzy_file->length;
1019 statret = 0;
1020 fnamecmp = fnamecmpbuf;
1021 fnamecmp_type = FNAMECMP_FUZZY;
1022 }
1023 }
1024
1025 if (statret != 0) {
1026 if (preserve_hard_links && file->link_u.links
1027 && hard_link_check(file, ndx, fname, statret, &st,
1028 itemizing, code, HL_SKIP))
1029 return;
1030 if (stat_errno == ENOENT)
1031 goto notify_others;
1032 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
1033 full_fname(fname));
1034 return;
1035 }
1036
1037 if (append_mode && st.st_size > file->length)
1038 return;
1039
1040 if (!compare_dest && fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1041 ;
1042 else if (fnamecmp_type == FNAMECMP_FUZZY)
1043 ;
1044 else if (unchanged_file(fnamecmp, file, &st)) {
1045 if (fnamecmp_type == FNAMECMP_FNAME) {
1046 if (itemizing) {
1047 itemize(file, ndx, real_ret, &real_st,
1048 0, 0, NULL);
1049 }
1050 set_perms(fname, file, &st, maybe_PERMS_REPORT);
1051 if (preserve_hard_links && file->link_u.links)
1052 hard_link_cluster(file, ndx, itemizing, code);
1053 return;
1054 }
1055 /* Only --compare-dest gets here. */
1056 itemize(file, ndx, real_ret, &real_st,
1057 ITEM_NO_DEST_AND_NO_UPDATE, 0, NULL);
1058 return;
1059 }
1060
1061prepare_to_open:
1062 if (partialptr) {
1063 st = partial_st;
1064 fnamecmp = partialptr;
1065 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1066 statret = 0;
1067 }
1068
1069 if (!do_xfers || read_batch || whole_file)
1070 goto notify_others;
1071
1072 if (fuzzy_basis) {
1073 int j = flist_find(fuzzy_dirlist, file);
1074 if (j >= 0) /* don't use changing file as future fuzzy basis */
1075 fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY;
1076 }
1077
1078 /* open the file */
1079 fd = do_open(fnamecmp, O_RDONLY, 0);
1080
1081 if (fd == -1) {
1082 rsyserr(FERROR, errno, "failed to open %s, continuing",
1083 full_fname(fnamecmp));
1084 pretend_missing:
1085 /* pretend the file didn't exist */
1086 if (preserve_hard_links && file->link_u.links
1087 && hard_link_check(file, ndx, fname, statret, &st,
1088 itemizing, code, HL_SKIP))
1089 return;
1090 statret = real_ret = -1;
1091 goto notify_others;
1092 }
1093
1094 if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
1095 if (!(backupptr = get_backup_name(fname))) {
1096 close(fd);
1097 return;
1098 }
1099 if (!(back_file = make_file(fname, NULL, NO_FILTERS))) {
1100 close(fd);
1101 goto pretend_missing;
1102 }
1103 if (robust_unlink(backupptr) && errno != ENOENT) {
1104 rsyserr(FERROR, errno, "unlink %s",
1105 full_fname(backupptr));
1106 free(back_file);
1107 close(fd);
1108 return;
1109 }
1110 if ((f_copy = do_open(backupptr,
1111 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1112 rsyserr(FERROR, errno, "open %s",
1113 full_fname(backupptr));
1114 free(back_file);
1115 close(fd);
1116 return;
1117 }
1118 fnamecmp_type = FNAMECMP_BACKUP;
1119 }
1120
1121 if (verbose > 3) {
1122 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1123 safe_fname(fnamecmp), (double)st.st_size);
1124 }
1125
1126 if (verbose > 2)
1127 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1128
1129notify_others:
1130 write_int(f_out, ndx);
1131 if (itemizing) {
1132 int iflags = ITEM_TRANSFER;
1133 if (always_checksum)
1134 iflags |= ITEM_REPORT_CHECKSUM;
1135 if (fnamecmp_type != FNAMECMP_FNAME)
1136 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1137 if (fnamecmp_type == FNAMECMP_FUZZY)
1138 iflags |= ITEM_XNAME_FOLLOWS;
1139 itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type,
1140 fuzzy_file ? fuzzy_file->basename : NULL);
1141 }
1142
1143 if (!do_xfers) {
1144 if (preserve_hard_links && file->link_u.links)
1145 hard_link_cluster(file, ndx, itemizing, code);
1146 return;
1147 }
1148 if (read_batch)
1149 return;
1150
1151 if (statret != 0 || whole_file) {
1152 write_sum_head(f_out, NULL);
1153 return;
1154 }
1155
1156 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
1157
1158 if (f_copy >= 0) {
1159 close(f_copy);
1160 set_perms(backupptr, back_file, NULL, 0);
1161 if (verbose > 1) {
1162 rprintf(FINFO, "backed up %s to %s\n",
1163 safe_fname(fname), safe_fname(backupptr));
1164 }
1165 free(back_file);
1166 }
1167
1168 close(fd);
1169}
1170
1171
1172void generate_files(int f_out, struct file_list *flist, char *local_name)
1173{
1174 int i;
1175 char fbuf[MAXPATHLEN];
1176 int itemizing, maybe_PERMS_REPORT;
1177 enum logcode code;
1178 int lull_mod = allowed_lull * 5;
1179 int need_retouch_dir_times = preserve_times && !omit_dir_times;
1180 int need_retouch_dir_perms = 0;
1181 int save_only_existing = only_existing;
1182 int save_opt_ignore_existing = opt_ignore_existing;
1183 int save_do_progress = do_progress;
1184 int save_make_backups = make_backups;
1185
1186 if (protocol_version >= 29) {
1187 itemizing = 1;
1188 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
1189 code = daemon_log_format_has_i ? 0 : FLOG;
1190 } else if (am_daemon) {
1191 itemizing = daemon_log_format_has_i && do_xfers;
1192 maybe_PERMS_REPORT = PERMS_REPORT;
1193 code = itemizing || !do_xfers ? FCLIENT : FINFO;
1194 } else if (!am_server) {
1195 itemizing = log_format_has_i;
1196 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
1197 code = itemizing ? 0 : FINFO;
1198 } else {
1199 itemizing = 0;
1200 maybe_PERMS_REPORT = PERMS_REPORT;
1201 code = FINFO;
1202 }
1203
1204 if (verbose > 2) {
1205 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
1206 (long)getpid(), flist->count);
1207 }
1208
1209 if (delete_before && !local_name && flist->count > 0)
1210 do_delete_pass(flist);
1211 do_progress = 0;
1212
1213 if (append_mode || whole_file < 0)
1214 whole_file = 0;
1215 if (verbose >= 2) {
1216 rprintf(FINFO, "delta-transmission %s\n",
1217 whole_file
1218 ? "disabled for local transfer or --whole-file"
1219 : "enabled");
1220 }
1221
1222 /* Since we often fill up the outgoing socket and then just sit around
1223 * waiting for the other 2 processes to do their thing, we don't want
1224 * to exit on a timeout. If the data stops flowing, the receiver will
1225 * notice that and let us know via the redo pipe (or its closing). */
1226 ignore_timeout = 1;
1227
1228 for (i = 0; i < flist->count; i++) {
1229 struct file_struct *file = flist->files[i];
1230
1231 if (!file->basename)
1232 continue;
1233
1234 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
1235 file, i, itemizing, maybe_PERMS_REPORT, code,
1236 f_out);
1237
1238 /* We need to ensure that any dirs we create have writeable
1239 * permissions during the time we are putting files within
1240 * them. This is then fixed after the transfer is done. */
1241#ifdef HAVE_CHMOD
1242 if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)
1243 && !list_only) {
1244 int mode = file->mode | S_IWUSR; /* user write */
1245 char *fname = local_name ? local_name : fbuf;
1246 if (do_chmod(fname, mode) < 0) {
1247 rsyserr(FERROR, errno,
1248 "failed to modify permissions on %s",
1249 full_fname(fname));
1250 }
1251 need_retouch_dir_perms = 1;
1252 }
1253#endif
1254
1255 if (preserve_hard_links)
1256 check_for_finished_hlinks(itemizing, code);
1257
1258 if (allowed_lull && !(i % lull_mod))
1259 maybe_send_keepalive();
1260 else if (!(i % 200))
1261 maybe_flush_socket();
1262 }
1263 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
1264 if (delete_during)
1265 delete_in_dir(NULL, NULL, NULL);
1266
1267 phase++;
1268 csum_length = SUM_LENGTH;
1269 only_existing = max_size = opt_ignore_existing = 0;
1270 update_only = always_checksum = size_only = 0;
1271 ignore_times = 1;
1272 if (append_mode) /* resend w/o append mode */
1273 append_mode = -1; /* ... but only longer files */
1274 make_backups = 0; /* avoid a duplicate backup for inplace processing */
1275
1276 if (verbose > 2)
1277 rprintf(FINFO,"generate_files phase=%d\n",phase);
1278
1279 write_int(f_out, -1);
1280
1281 /* files can cycle through the system more than once
1282 * to catch initial checksum errors */
1283 while ((i = get_redo_num(itemizing, code)) != -1) {
1284 struct file_struct *file = flist->files[i];
1285 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
1286 file, i, itemizing, maybe_PERMS_REPORT, code,
1287 f_out);
1288 }
1289
1290 phase++;
1291 only_existing = save_only_existing;
1292 opt_ignore_existing = save_opt_ignore_existing;
1293 make_backups = save_make_backups;
1294
1295 if (verbose > 2)
1296 rprintf(FINFO,"generate_files phase=%d\n",phase);
1297
1298 write_int(f_out, -1);
1299 /* Reduce round-trip lag-time for a useless delay-updates phase. */
1300 if (protocol_version >= 29 && !delay_updates)
1301 write_int(f_out, -1);
1302
1303 /* Read MSG_DONE for the redo phase (and any prior messages). */
1304 get_redo_num(itemizing, code);
1305
1306 if (protocol_version >= 29) {
1307 phase++;
1308 if (verbose > 2)
1309 rprintf(FINFO, "generate_files phase=%d\n", phase);
1310 if (delay_updates)
1311 write_int(f_out, -1);
1312 /* Read MSG_DONE for delay-updates phase & prior messages. */
1313 get_redo_num(itemizing, code);
1314 }
1315
1316 do_progress = save_do_progress;
1317 if (delete_after && !local_name && flist->count > 0)
1318 do_delete_pass(flist);
1319
1320 if ((need_retouch_dir_perms || need_retouch_dir_times)
1321 && !list_only && !local_name && !dry_run) {
1322 int j = 0;
1323 /* Now we need to fix any directory permissions that were
1324 * modified during the transfer and/or re-set any tweaked
1325 * modified-time values. */
1326 for (i = 0; i < flist->count; i++) {
1327 struct file_struct *file = flist->files[i];
1328 if (!file->basename || !S_ISDIR(file->mode))
1329 continue;
1330 if (!need_retouch_dir_times && file->mode & S_IWUSR)
1331 continue;
1332 recv_generator(f_name(file), file, i, itemizing,
1333 maybe_PERMS_REPORT, code, -1);
1334 if (allowed_lull && !(++j % lull_mod))
1335 maybe_send_keepalive();
1336 else if (!(j % 200))
1337 maybe_flush_socket();
1338 }
1339 }
1340 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
1341
1342 if (max_delete > 0 && deletion_count > max_delete) {
1343 rprintf(FINFO,
1344 "Deletions stopped due to --max-delete limit (%d skipped)\n",
1345 deletion_count - max_delete);
1346 io_error |= IOERR_DEL_LIMIT;
1347 }
1348
1349 if (verbose > 2)
1350 rprintf(FINFO,"generate_files finished\n");
1351}