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