Added handling of the --min-size option.
[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 ignore_existing;
56extern int ignore_non_existing;
57extern int inplace;
58extern int append_mode;
59extern int make_backups;
60extern int csum_length;
61extern int ignore_times;
62extern int size_only;
63extern OFF_T max_size;
64extern int io_error;
65extern int allowed_lull;
66extern int sock_f_out;
67extern int ignore_timeout;
68extern int protocol_version;
69extern int fuzzy_basis;
70extern int always_checksum;
71extern char *partial_dir;
72extern char *basis_dir[];
73extern int compare_dest;
74extern int copy_dest;
75extern int link_dest;
76extern int whole_file;
77extern int list_only;
78extern int read_batch;
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 (ignore_non_existing && statret == -1 && stat_errno == ENOENT) {
679 if (verbose > 1) {
680 rprintf(FINFO, "not creating new %s \"%s\"\n",
681 S_ISDIR(file->mode) ? "directory" : "file",
682 safe_fname(fname));
683 }
684 return;
685 }
686
687 if (statret == 0 && !preserve_perms
688 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode)) {
689 /* if the file exists already and we aren't perserving
690 * permissions then act as though the remote end sent
691 * us the file permissions we already have */
692 file->mode = (file->mode & ~CHMOD_BITS)
693 | (st.st_mode & CHMOD_BITS);
694 }
695
696 if (S_ISDIR(file->mode)) {
697 /* The file to be received is a directory, so we need
698 * to prepare appropriately. If there is already a
699 * file of that name and it is *not* a directory, then
700 * we need to delete it. If it doesn't exist, then
701 * (perhaps recursively) create it. */
702 if (statret == 0 && !S_ISDIR(st.st_mode)) {
703 if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
704 return;
705 statret = -1;
706 }
707 if (dry_run && statret != 0 && missing_below < 0) {
708 missing_below = file->dir.depth;
709 dry_run++;
710 }
711 if (itemizing && f_out != -1) {
712 itemize(file, ndx, statret, &st,
713 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
714 }
715 if (statret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
716 if (!relative_paths || errno != ENOENT
717 || create_directory_path(fname, orig_umask) < 0
718 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
719 rsyserr(FERROR, errno,
720 "recv_generator: mkdir %s failed",
721 full_fname(fname));
722 }
723 }
724 if (set_perms(fname, file, statret ? NULL : &st, 0)
725 && verbose && code && f_out != -1)
726 rprintf(code, "%s/\n", safe_fname(fname));
727 if (delete_during && f_out != -1 && !phase && dry_run < 2
728 && (file->flags & FLAG_DEL_HERE))
729 delete_in_dir(the_file_list, fname, file);
730 return;
731 }
732
733 if (preserve_hard_links && file->link_u.links
734 && hard_link_check(file, ndx, fname, statret, &st,
735 itemizing, code, HL_CHECK_MASTER))
736 return;
737
738 if (preserve_links && S_ISLNK(file->mode)) {
739#ifdef SUPPORT_LINKS
740 if (safe_symlinks && unsafe_symlink(file->u.link, fname)) {
741 if (verbose) {
742 if (the_file_list->count == 1)
743 fname = f_name(file);
744 rprintf(FINFO,
745 "ignoring unsafe symlink %s -> \"%s\"\n",
746 full_fname(fname),
747 safe_fname(file->u.link));
748 }
749 return;
750 }
751 if (statret == 0) {
752 char lnk[MAXPATHLEN];
753 int len;
754
755 if (!S_ISDIR(st.st_mode)
756 && (len = readlink(fname, lnk, MAXPATHLEN-1)) > 0) {
757 lnk[len] = 0;
758 /* A link already pointing to the
759 * right place -- no further action
760 * required. */
761 if (strcmp(lnk, file->u.link) == 0) {
762 if (itemizing) {
763 itemize(file, ndx, 0, &st, 0,
764 0, NULL);
765 }
766 set_perms(fname, file, &st,
767 maybe_PERMS_REPORT);
768 if (preserve_hard_links
769 && file->link_u.links) {
770 hard_link_cluster(file, ndx,
771 itemizing,
772 code);
773 }
774 return;
775 }
776 }
777 /* Not the right symlink (or not a symlink), so
778 * delete it. */
779 if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
780 return;
781 if (!S_ISLNK(st.st_mode))
782 statret = -1;
783 }
784 if (preserve_hard_links && file->link_u.links
785 && hard_link_check(file, ndx, fname, -1, &st,
786 itemizing, code, HL_SKIP))
787 return;
788 if (do_symlink(file->u.link,fname) != 0) {
789 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
790 full_fname(fname), safe_fname(file->u.link));
791 } else {
792 set_perms(fname,file,NULL,0);
793 if (itemizing) {
794 itemize(file, ndx, statret, &st,
795 ITEM_LOCAL_CHANGE, 0, NULL);
796 }
797 if (code && verbose) {
798 rprintf(code, "%s -> %s\n", safe_fname(fname),
799 safe_fname(file->u.link));
800 }
801 if (remove_sent_files && !dry_run) {
802 char numbuf[4];
803 SIVAL(numbuf, 0, ndx);
804 send_msg(MSG_SUCCESS, numbuf, 4);
805 }
806 if (preserve_hard_links && file->link_u.links)
807 hard_link_cluster(file, ndx, itemizing, code);
808 }
809#endif
810 return;
811 }
812
813 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
814 if (statret != 0
815 || (st.st_mode & ~CHMOD_BITS) != (file->mode & ~CHMOD_BITS)
816 || st.st_rdev != file->u.rdev) {
817 if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
818 return;
819 if (preserve_hard_links && file->link_u.links
820 && hard_link_check(file, ndx, fname, -1, &st,
821 itemizing, code, HL_SKIP))
822 return;
823 if (!IS_DEVICE(st.st_mode))
824 statret = -1;
825 if (verbose > 2) {
826 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
827 safe_fname(fname),
828 (int)file->mode, (int)file->u.rdev);
829 }
830 if (do_mknod(fname,file->mode,file->u.rdev) < 0) {
831 rsyserr(FERROR, errno, "mknod %s failed",
832 full_fname(fname));
833 } else {
834 set_perms(fname,file,NULL,0);
835 if (itemizing) {
836 itemize(file, ndx, statret, &st,
837 ITEM_LOCAL_CHANGE, 0, NULL);
838 }
839 if (code && verbose) {
840 rprintf(code, "%s\n",
841 safe_fname(fname));
842 }
843 if (preserve_hard_links && file->link_u.links) {
844 hard_link_cluster(file, ndx,
845 itemizing, code);
846 }
847 }
848 } else {
849 if (itemizing)
850 itemize(file, ndx, statret, &st, 0, 0, NULL);
851 set_perms(fname, file, &st, maybe_PERMS_REPORT);
852 if (preserve_hard_links && file->link_u.links)
853 hard_link_cluster(file, ndx, itemizing, code);
854 }
855 return;
856 }
857
858 if (!S_ISREG(file->mode)) {
859 if (the_file_list->count == 1)
860 fname = f_name(file);
861 rprintf(FINFO, "skipping non-regular file \"%s\"\n",
862 safe_fname(fname));
863 return;
864 }
865
866 if (max_size && file->length > max_size) {
867 if (verbose > 1) {
868 if (the_file_list->count == 1)
869 fname = f_name(file);
870 rprintf(FINFO, "%s is over max-size\n",
871 safe_fname(fname));
872 }
873 return;
874 }
875
876 if (ignore_existing && statret == 0) {
877 if (verbose > 1)
878 rprintf(FINFO, "%s exists\n", safe_fname(fname));
879 return;
880 }
881
882 if (update_only && statret == 0
883 && cmp_modtime(st.st_mtime, file->modtime) > 0) {
884 if (verbose > 1)
885 rprintf(FINFO, "%s is newer\n", safe_fname(fname));
886 return;
887 }
888
889 fnamecmp = fname;
890 fnamecmp_type = FNAMECMP_FNAME;
891
892 if (statret == 0 && !S_ISREG(st.st_mode)) {
893 if (delete_item(fname, st.st_mode, DEL_TERSE) != 0)
894 return;
895 statret = -1;
896 stat_errno = ENOENT;
897 }
898
899 if (statret != 0 && basis_dir[0] != NULL) {
900 int best_match = -1;
901 int match_level = 0;
902 int i = 0;
903 do {
904 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
905 basis_dir[i], fname);
906 if (link_stat(fnamecmpbuf, &st, 0) < 0
907 || !S_ISREG(st.st_mode))
908 continue;
909 switch (match_level) {
910 case 0:
911 best_match = i;
912 match_level = 1;
913 /* FALL THROUGH */
914 case 1:
915 if (!unchanged_file(fnamecmpbuf, file, &st))
916 continue;
917 best_match = i;
918 match_level = 2;
919 if (copy_dest)
920 break;
921 /* FALL THROUGH */
922 case 2:
923 if (!unchanged_attrs(file, &st))
924 continue;
925 best_match = i;
926 match_level = 3;
927 break;
928 }
929 break;
930 } while (basis_dir[++i] != NULL);
931 if (match_level) {
932 statret = 0;
933 if (i != best_match) {
934 i = best_match;
935 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
936 basis_dir[i], fname);
937 if (link_stat(fnamecmpbuf, &st, 0) < 0) {
938 match_level = 0;
939 statret = -1;
940 stat_errno = errno;
941 }
942 }
943#ifdef HAVE_LINK
944 if (link_dest && match_level == 3) {
945 if (hard_link_one(file, ndx, fname, -1, &st,
946 fnamecmpbuf, 1,
947 itemizing && verbose > 1,
948 code) == 0) {
949 if (preserve_hard_links
950 && file->link_u.links) {
951 hard_link_cluster(file, ndx,
952 itemizing,
953 code);
954 }
955 return;
956 }
957 match_level = 2;
958 }
959#endif
960 if (match_level == 2) {
961 /* Copy the file locally. */
962 if (copy_file(fnamecmpbuf, fname, file->mode) < 0) {
963 if (verbose) {
964 rsyserr(FINFO, errno,
965 "copy_file %s => %s",
966 full_fname(fnamecmpbuf),
967 safe_fname(fname));
968 }
969 match_level = 0;
970 statret = -1;
971 } else {
972 if (itemizing) {
973 itemize(file, ndx, 0, &st,
974 ITEM_LOCAL_CHANGE, 0,
975 NULL);
976 } else if (verbose && code) {
977 rprintf(code, "%s\n",
978 safe_fname(fname));
979 }
980 set_perms(fname, file, NULL,
981 maybe_PERMS_REPORT);
982 if (preserve_hard_links
983 && file->link_u.links) {
984 hard_link_cluster(file, ndx,
985 itemizing,
986 code);
987 }
988 return;
989 }
990 } else if (compare_dest || match_level == 1) {
991 fnamecmp = fnamecmpbuf;
992 fnamecmp_type = i;
993 }
994 }
995 }
996
997 real_ret = statret;
998 real_st = st;
999
1000 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1001 && link_stat(partialptr, &partial_st, 0) == 0
1002 && S_ISREG(partial_st.st_mode)) {
1003 if (statret != 0)
1004 goto prepare_to_open;
1005 } else
1006 partialptr = NULL;
1007
1008 if (statret != 0 && fuzzy_basis && dry_run <= 1) {
1009 int j = find_fuzzy(file, fuzzy_dirlist);
1010 if (j >= 0) {
1011 fuzzy_file = fuzzy_dirlist->files[j];
1012 f_name_to(fuzzy_file, fnamecmpbuf);
1013 if (verbose > 2) {
1014 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1015 safe_fname(fname), safe_fname(fnamecmpbuf));
1016 }
1017 st.st_size = fuzzy_file->length;
1018 statret = 0;
1019 fnamecmp = fnamecmpbuf;
1020 fnamecmp_type = FNAMECMP_FUZZY;
1021 }
1022 }
1023
1024 if (statret != 0) {
1025 if (preserve_hard_links && file->link_u.links
1026 && hard_link_check(file, ndx, fname, statret, &st,
1027 itemizing, code, HL_SKIP))
1028 return;
1029 if (stat_errno == ENOENT)
1030 goto notify_others;
1031 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
1032 full_fname(fname));
1033 return;
1034 }
1035
1036 if (append_mode && st.st_size > file->length)
1037 return;
1038
1039 if (!compare_dest && fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1040 ;
1041 else if (fnamecmp_type == FNAMECMP_FUZZY)
1042 ;
1043 else if (unchanged_file(fnamecmp, file, &st)) {
1044 if (fnamecmp_type == FNAMECMP_FNAME) {
1045 if (itemizing) {
1046 itemize(file, ndx, real_ret, &real_st,
1047 0, 0, NULL);
1048 }
1049 set_perms(fname, file, &st, maybe_PERMS_REPORT);
1050 if (preserve_hard_links && file->link_u.links)
1051 hard_link_cluster(file, ndx, itemizing, code);
1052 return;
1053 }
1054 /* Only --compare-dest gets here. */
1055 itemize(file, ndx, real_ret, &real_st,
1056 ITEM_NO_DEST_AND_NO_UPDATE, 0, NULL);
1057 return;
1058 }
1059
1060prepare_to_open:
1061 if (partialptr) {
1062 st = partial_st;
1063 fnamecmp = partialptr;
1064 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1065 statret = 0;
1066 }
1067
1068 if (!do_xfers || read_batch || whole_file)
1069 goto notify_others;
1070
1071 if (fuzzy_basis) {
1072 int j = flist_find(fuzzy_dirlist, file);
1073 if (j >= 0) /* don't use changing file as future fuzzy basis */
1074 fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY;
1075 }
1076
1077 /* open the file */
1078 fd = do_open(fnamecmp, O_RDONLY, 0);
1079
1080 if (fd == -1) {
1081 rsyserr(FERROR, errno, "failed to open %s, continuing",
1082 full_fname(fnamecmp));
1083 pretend_missing:
1084 /* pretend the file didn't exist */
1085 if (preserve_hard_links && file->link_u.links
1086 && hard_link_check(file, ndx, fname, statret, &st,
1087 itemizing, code, HL_SKIP))
1088 return;
1089 statret = real_ret = -1;
1090 goto notify_others;
1091 }
1092
1093 if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
1094 if (!(backupptr = get_backup_name(fname))) {
1095 close(fd);
1096 return;
1097 }
1098 if (!(back_file = make_file(fname, NULL, NO_FILTERS))) {
1099 close(fd);
1100 goto pretend_missing;
1101 }
1102 if (robust_unlink(backupptr) && errno != ENOENT) {
1103 rsyserr(FERROR, errno, "unlink %s",
1104 full_fname(backupptr));
1105 free(back_file);
1106 close(fd);
1107 return;
1108 }
1109 if ((f_copy = do_open(backupptr,
1110 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1111 rsyserr(FERROR, errno, "open %s",
1112 full_fname(backupptr));
1113 free(back_file);
1114 close(fd);
1115 return;
1116 }
1117 fnamecmp_type = FNAMECMP_BACKUP;
1118 }
1119
1120 if (verbose > 3) {
1121 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1122 safe_fname(fnamecmp), (double)st.st_size);
1123 }
1124
1125 if (verbose > 2)
1126 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1127
1128notify_others:
1129 write_int(f_out, ndx);
1130 if (itemizing) {
1131 int iflags = ITEM_TRANSFER;
1132 if (always_checksum)
1133 iflags |= ITEM_REPORT_CHECKSUM;
1134 if (fnamecmp_type != FNAMECMP_FNAME)
1135 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1136 if (fnamecmp_type == FNAMECMP_FUZZY)
1137 iflags |= ITEM_XNAME_FOLLOWS;
1138 itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type,
1139 fuzzy_file ? fuzzy_file->basename : NULL);
1140 }
1141
1142 if (!do_xfers) {
1143 if (preserve_hard_links && file->link_u.links)
1144 hard_link_cluster(file, ndx, itemizing, code);
1145 return;
1146 }
1147 if (read_batch)
1148 return;
1149
1150 if (statret != 0 || whole_file) {
1151 write_sum_head(f_out, NULL);
1152 return;
1153 }
1154
1155 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
1156
1157 if (f_copy >= 0) {
1158 close(f_copy);
1159 set_perms(backupptr, back_file, NULL, 0);
1160 if (verbose > 1) {
1161 rprintf(FINFO, "backed up %s to %s\n",
1162 safe_fname(fname), safe_fname(backupptr));
1163 }
1164 free(back_file);
1165 }
1166
1167 close(fd);
1168}
1169
1170
1171void generate_files(int f_out, struct file_list *flist, char *local_name)
1172{
1173 int i;
1174 char fbuf[MAXPATHLEN];
1175 int itemizing, maybe_PERMS_REPORT;
1176 enum logcode code;
1177 int lull_mod = allowed_lull * 5;
1178 int need_retouch_dir_times = preserve_times && !omit_dir_times;
1179 int need_retouch_dir_perms = 0;
1180 int save_ignore_existing = ignore_existing;
1181 int save_ignore_non_existing = ignore_non_existing;
1182 int save_do_progress = do_progress;
1183 int save_make_backups = make_backups;
1184
1185 if (protocol_version >= 29) {
1186 itemizing = 1;
1187 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
1188 code = daemon_log_format_has_i ? 0 : FLOG;
1189 } else if (am_daemon) {
1190 itemizing = daemon_log_format_has_i && do_xfers;
1191 maybe_PERMS_REPORT = PERMS_REPORT;
1192 code = itemizing || !do_xfers ? FCLIENT : FINFO;
1193 } else if (!am_server) {
1194 itemizing = log_format_has_i;
1195 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
1196 code = itemizing ? 0 : FINFO;
1197 } else {
1198 itemizing = 0;
1199 maybe_PERMS_REPORT = PERMS_REPORT;
1200 code = FINFO;
1201 }
1202
1203 if (verbose > 2) {
1204 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
1205 (long)getpid(), flist->count);
1206 }
1207
1208 if (delete_before && !local_name && flist->count > 0)
1209 do_delete_pass(flist);
1210 do_progress = 0;
1211
1212 if (append_mode || whole_file < 0)
1213 whole_file = 0;
1214 if (verbose >= 2) {
1215 rprintf(FINFO, "delta-transmission %s\n",
1216 whole_file
1217 ? "disabled for local transfer or --whole-file"
1218 : "enabled");
1219 }
1220
1221 /* Since we often fill up the outgoing socket and then just sit around
1222 * waiting for the other 2 processes to do their thing, we don't want
1223 * to exit on a timeout. If the data stops flowing, the receiver will
1224 * notice that and let us know via the redo pipe (or its closing). */
1225 ignore_timeout = 1;
1226
1227 for (i = 0; i < flist->count; i++) {
1228 struct file_struct *file = flist->files[i];
1229
1230 if (!file->basename)
1231 continue;
1232
1233 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
1234 file, i, itemizing, maybe_PERMS_REPORT, code,
1235 f_out);
1236
1237 /* We need to ensure that any dirs we create have writeable
1238 * permissions during the time we are putting files within
1239 * them. This is then fixed after the transfer is done. */
1240#ifdef HAVE_CHMOD
1241 if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)
1242 && !list_only) {
1243 int mode = file->mode | S_IWUSR; /* user write */
1244 char *fname = local_name ? local_name : fbuf;
1245 if (do_chmod(fname, mode) < 0) {
1246 rsyserr(FERROR, errno,
1247 "failed to modify permissions on %s",
1248 full_fname(fname));
1249 }
1250 need_retouch_dir_perms = 1;
1251 }
1252#endif
1253
1254 if (preserve_hard_links)
1255 check_for_finished_hlinks(itemizing, code);
1256
1257 if (allowed_lull && !(i % lull_mod))
1258 maybe_send_keepalive();
1259 else if (!(i % 200))
1260 maybe_flush_socket();
1261 }
1262 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
1263 if (delete_during)
1264 delete_in_dir(NULL, NULL, NULL);
1265
1266 phase++;
1267 csum_length = SUM_LENGTH;
1268 max_size = ignore_existing = ignore_non_existing = 0;
1269 update_only = always_checksum = size_only = 0;
1270 ignore_times = 1;
1271 if (append_mode) /* resend w/o append mode */
1272 append_mode = -1; /* ... but only longer files */
1273 make_backups = 0; /* avoid a duplicate backup for inplace processing */
1274
1275 if (verbose > 2)
1276 rprintf(FINFO,"generate_files phase=%d\n",phase);
1277
1278 write_int(f_out, -1);
1279
1280 /* files can cycle through the system more than once
1281 * to catch initial checksum errors */
1282 while ((i = get_redo_num(itemizing, code)) != -1) {
1283 struct file_struct *file = flist->files[i];
1284 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
1285 file, i, itemizing, maybe_PERMS_REPORT, code,
1286 f_out);
1287 }
1288
1289 phase++;
1290 ignore_non_existing = save_ignore_non_existing;
1291 ignore_existing = save_ignore_existing;
1292 make_backups = save_make_backups;
1293
1294 if (verbose > 2)
1295 rprintf(FINFO,"generate_files phase=%d\n",phase);
1296
1297 write_int(f_out, -1);
1298 /* Reduce round-trip lag-time for a useless delay-updates phase. */
1299 if (protocol_version >= 29 && !delay_updates)
1300 write_int(f_out, -1);
1301
1302 /* Read MSG_DONE for the redo phase (and any prior messages). */
1303 get_redo_num(itemizing, code);
1304
1305 if (protocol_version >= 29) {
1306 phase++;
1307 if (verbose > 2)
1308 rprintf(FINFO, "generate_files phase=%d\n", phase);
1309 if (delay_updates)
1310 write_int(f_out, -1);
1311 /* Read MSG_DONE for delay-updates phase & prior messages. */
1312 get_redo_num(itemizing, code);
1313 }
1314
1315 do_progress = save_do_progress;
1316 if (delete_after && !local_name && flist->count > 0)
1317 do_delete_pass(flist);
1318
1319 if ((need_retouch_dir_perms || need_retouch_dir_times)
1320 && !list_only && !local_name && !dry_run) {
1321 int j = 0;
1322 /* Now we need to fix any directory permissions that were
1323 * modified during the transfer and/or re-set any tweaked
1324 * modified-time values. */
1325 for (i = 0; i < flist->count; i++) {
1326 struct file_struct *file = flist->files[i];
1327 if (!file->basename || !S_ISDIR(file->mode))
1328 continue;
1329 if (!need_retouch_dir_times && file->mode & S_IWUSR)
1330 continue;
1331 recv_generator(f_name(file), file, i, itemizing,
1332 maybe_PERMS_REPORT, code, -1);
1333 if (allowed_lull && !(++j % lull_mod))
1334 maybe_send_keepalive();
1335 else if (!(j % 200))
1336 maybe_flush_socket();
1337 }
1338 }
1339 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
1340
1341 if (max_delete > 0 && deletion_count > max_delete) {
1342 rprintf(FINFO,
1343 "Deletions stopped due to --max-delete limit (%d skipped)\n",
1344 deletion_count - max_delete);
1345 io_error |= IOERR_DEL_LIMIT;
1346 }
1347
1348 if (verbose > 2)
1349 rprintf(FINFO,"generate_files finished\n");
1350}