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