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