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