Added init_stat_x() to avoid duplication of acl/xattr init code.
[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-2008 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 3 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, visit the http://fsf.org website.
21 */
22
23#include "rsync.h"
24#include "inums.h"
25#include "ifuncs.h"
26
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 inc_recurse;
35extern int relative_paths;
36extern int implied_dirs;
37extern int keep_dirlinks;
38extern int preserve_acls;
39extern int preserve_xattrs;
40extern int preserve_links;
41extern int preserve_devices;
42extern int preserve_specials;
43extern int preserve_hard_links;
44extern int preserve_executability;
45extern int preserve_perms;
46extern int preserve_times;
47extern int uid_ndx;
48extern int gid_ndx;
49extern int delete_mode;
50extern int delete_before;
51extern int delete_during;
52extern int delete_after;
53extern int msgdone_cnt;
54extern int ignore_errors;
55extern int remove_source_files;
56extern int delay_updates;
57extern int update_only;
58extern int human_readable;
59extern int ignore_existing;
60extern int ignore_non_existing;
61extern int inplace;
62extern int append_mode;
63extern int make_backups;
64extern int csum_length;
65extern int ignore_times;
66extern int size_only;
67extern OFF_T max_size;
68extern OFF_T min_size;
69extern int io_error;
70extern int flist_eof;
71extern int allowed_lull;
72extern int sock_f_out;
73extern int ignore_timeout;
74extern int protocol_version;
75extern int file_total;
76extern int fuzzy_basis;
77extern int always_checksum;
78extern int checksum_len;
79extern char *partial_dir;
80extern int compare_dest;
81extern int copy_dest;
82extern int link_dest;
83extern int whole_file;
84extern int list_only;
85extern int read_batch;
86extern int safe_symlinks;
87extern long block_size; /* "long" because popt can't set an int32. */
88extern int unsort_ndx;
89extern int max_delete;
90extern int force_delete;
91extern int one_file_system;
92extern struct stats stats;
93extern dev_t filesystem_dev;
94extern mode_t orig_umask;
95extern uid_t our_uid;
96extern char *backup_dir;
97extern char *backup_suffix;
98extern int backup_suffix_len;
99extern char *basis_dir[MAX_BASIS_DIRS+1];
100extern struct file_list *cur_flist, *first_flist, *dir_flist;
101extern struct filter_list_struct daemon_filter_list;
102
103int ignore_perishable = 0;
104int non_perishable_cnt = 0;
105int maybe_ATTRS_REPORT = 0;
106
107static dev_t dev_zero;
108static int skipped_deletes = 0;
109static int deldelay_size = 0, deldelay_cnt = 0;
110static char *deldelay_buf = NULL;
111static int deldelay_fd = -1;
112static int loopchk_limit;
113static int dir_tweaking;
114static int symlink_timeset_failed_flags;
115static int need_retouch_dir_times;
116static int need_retouch_dir_perms;
117static const char *solo_file = NULL;
118
119/* For calling delete_item() and delete_dir_contents(). */
120#define DEL_NO_UID_WRITE (1<<0) /* file/dir has our uid w/o write perm */
121#define DEL_RECURSE (1<<1) /* if dir, delete all contents */
122#define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
123#define DEL_FOR_FILE (1<<3) /* making room for a replacement file */
124#define DEL_FOR_DIR (1<<4) /* making room for a replacement dir */
125#define DEL_FOR_SYMLINK (1<<5) /* making room for a replacement symlink */
126#define DEL_FOR_DEVICE (1<<6) /* making room for a replacement device */
127#define DEL_FOR_SPECIAL (1<<7) /* making room for a replacement special */
128
129#define DEL_MAKE_ROOM (DEL_FOR_FILE|DEL_FOR_DIR|DEL_FOR_SYMLINK|DEL_FOR_DEVICE|DEL_FOR_SPECIAL)
130
131enum nonregtype {
132 TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
133};
134
135enum delret {
136 DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_NOT_EMPTY
137};
138
139/* Forward declarations. */
140static enum delret delete_dir_contents(char *fname, uint16 flags);
141#ifdef SUPPORT_HARD_LINKS
142static void handle_skipped_hlink(struct file_struct *file, int itemizing,
143 enum logcode code, int f_out);
144#endif
145
146static int is_backup_file(char *fn)
147{
148 int k = strlen(fn) - backup_suffix_len;
149 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
150}
151
152/* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
153 * delete recursively.
154 *
155 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
156 * a directory! (The buffer is used for recursion, but returned unchanged.)
157 */
158static enum delret delete_item(char *fbuf, uint16 mode, uint16 flags)
159{
160 enum delret ret;
161 char *what;
162 int ok;
163
164 if (DEBUG_GTE(DEL, 2)) {
165 rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
166 fbuf, (int)mode, (int)flags);
167 }
168
169 if (flags & DEL_NO_UID_WRITE)
170 do_chmod(fbuf, mode | S_IWUSR);
171
172 if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
173 int save_uid_ndx = uid_ndx;
174 /* This only happens on the first call to delete_item() since
175 * delete_dir_contents() always calls us w/DEL_DIR_IS_EMPTY. */
176 if (!uid_ndx)
177 uid_ndx = ++file_extra_cnt;
178 ignore_perishable = 1;
179 /* If DEL_RECURSE is not set, this just reports emptiness. */
180 ret = delete_dir_contents(fbuf, flags);
181 ignore_perishable = 0;
182 if (!save_uid_ndx) {
183 --file_extra_cnt;
184 uid_ndx = 0;
185 }
186 if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT)
187 goto check_ret;
188 /* OK: try to delete the directory. */
189 }
190
191 if (!(flags & DEL_MAKE_ROOM) && max_delete >= 0 && stats.deleted_files >= max_delete) {
192 skipped_deletes++;
193 return DR_AT_LIMIT;
194 }
195
196 if (S_ISDIR(mode)) {
197 what = "rmdir";
198 ok = do_rmdir(fbuf) == 0;
199 } else if (make_backups > 0 && (backup_dir || !is_backup_file(fbuf))) {
200 what = "make_backup";
201 ok = make_backup(fbuf);
202 } else {
203 what = "unlink";
204 ok = robust_unlink(fbuf) == 0;
205 }
206
207 if (ok) {
208 if (!(flags & DEL_MAKE_ROOM)) {
209 log_delete(fbuf, mode);
210 stats.deleted_files++;
211 if (S_ISREG(mode)) {
212 /* Nothing more to count */
213 } else if (S_ISDIR(mode))
214 stats.deleted_dirs++;
215#ifdef SUPPORT_LINKS
216 else if (S_ISLNK(mode))
217 stats.deleted_symlinks++;
218#endif
219 else if (IS_DEVICE(mode))
220 stats.deleted_symlinks++;
221 else
222 stats.deleted_specials++;
223 }
224 ret = DR_SUCCESS;
225 } else {
226 if (S_ISDIR(mode) && errno == ENOTEMPTY) {
227 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
228 fbuf);
229 ret = DR_NOT_EMPTY;
230 } else if (errno != ENOENT) {
231 rsyserr(FERROR, errno, "delete_file: %s(%s) failed",
232 what, fbuf);
233 ret = DR_FAILURE;
234 } else
235 ret = DR_SUCCESS;
236 }
237
238 check_ret:
239 if (ret != DR_SUCCESS && flags & DEL_MAKE_ROOM) {
240 const char *desc;
241 switch (flags & DEL_MAKE_ROOM) {
242 case DEL_FOR_FILE: desc = "regular file"; break;
243 case DEL_FOR_DIR: desc = "directory"; break;
244 case DEL_FOR_SYMLINK: desc = "symlink"; break;
245 case DEL_FOR_DEVICE: desc = "device file"; break;
246 case DEL_FOR_SPECIAL: desc = "special file"; break;
247 default: exit_cleanup(RERR_UNSUPPORTED); /* IMPOSSIBLE */
248 }
249 rprintf(FERROR_XFER, "could not make way for new %s: %s\n",
250 desc, fbuf);
251 }
252 return ret;
253}
254
255/* The directory is about to be deleted: if DEL_RECURSE is given, delete all
256 * its contents, otherwise just checks for content. Returns DR_SUCCESS or
257 * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
258 * buffer is used for recursion, but returned unchanged.)
259 */
260static enum delret delete_dir_contents(char *fname, uint16 flags)
261{
262 struct file_list *dirlist;
263 enum delret ret;
264 unsigned remainder;
265 void *save_filters;
266 int j, dlen;
267 char *p;
268
269 if (DEBUG_GTE(DEL, 3)) {
270 rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n",
271 fname, flags);
272 }
273
274 dlen = strlen(fname);
275 save_filters = push_local_filters(fname, dlen);
276
277 non_perishable_cnt = 0;
278 dirlist = get_dirlist(fname, dlen, 0);
279 ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
280
281 if (!dirlist->used)
282 goto done;
283
284 if (!(flags & DEL_RECURSE)) {
285 ret = DR_NOT_EMPTY;
286 goto done;
287 }
288
289 p = fname + dlen;
290 if (dlen != 1 || *fname != '/')
291 *p++ = '/';
292 remainder = MAXPATHLEN - (p - fname);
293
294 /* We do our own recursion, so make delete_item() non-recursive. */
295 flags = (flags & ~(DEL_RECURSE|DEL_MAKE_ROOM|DEL_NO_UID_WRITE))
296 | DEL_DIR_IS_EMPTY;
297
298 for (j = dirlist->used; j--; ) {
299 struct file_struct *fp = dirlist->files[j];
300
301 if (fp->flags & FLAG_MOUNT_DIR && S_ISDIR(fp->mode)) {
302 if (DEBUG_GTE(DEL, 1)) {
303 rprintf(FINFO,
304 "mount point, %s, pins parent directory\n",
305 f_name(fp, NULL));
306 }
307 ret = DR_NOT_EMPTY;
308 continue;
309 }
310
311 strlcpy(p, fp->basename, remainder);
312 if (!(fp->mode & S_IWUSR) && !am_root && (uid_t)F_OWNER(fp) == our_uid)
313 do_chmod(fname, fp->mode | S_IWUSR);
314 /* Save stack by recursing to ourself directly. */
315 if (S_ISDIR(fp->mode)) {
316 if (delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
317 ret = DR_NOT_EMPTY;
318 }
319 if (delete_item(fname, fp->mode, flags) != DR_SUCCESS)
320 ret = DR_NOT_EMPTY;
321 }
322
323 fname[dlen] = '\0';
324
325 done:
326 flist_free(dirlist);
327 pop_local_filters(save_filters);
328
329 if (ret == DR_NOT_EMPTY) {
330 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
331 fname);
332 }
333 return ret;
334}
335
336static int start_delete_delay_temp(void)
337{
338 char fnametmp[MAXPATHLEN];
339 int save_dry_run = dry_run;
340
341 dry_run = 0;
342 if (!get_tmpname(fnametmp, "deldelay")
343 || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
344 rprintf(FINFO, "NOTE: Unable to create delete-delay temp file%s.\n",
345 inc_recurse ? "" : " -- switching to --delete-after");
346 delete_during = 0;
347 delete_after = !inc_recurse;
348 dry_run = save_dry_run;
349 return 0;
350 }
351 unlink(fnametmp);
352 dry_run = save_dry_run;
353 return 1;
354}
355
356static int flush_delete_delay(void)
357{
358 if (deldelay_fd < 0 && !start_delete_delay_temp())
359 return 0;
360 if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
361 rsyserr(FERROR, errno, "flush of delete-delay buffer");
362 delete_during = 0;
363 delete_after = !inc_recurse;
364 close(deldelay_fd);
365 return 0;
366 }
367 deldelay_cnt = 0;
368 return 1;
369}
370
371static int remember_delete(struct file_struct *file, const char *fname, int flags)
372{
373 int len;
374
375 if (deldelay_cnt == deldelay_size && !flush_delete_delay())
376 return 0;
377
378 if (flags & DEL_NO_UID_WRITE)
379 deldelay_buf[deldelay_cnt++] = '!';
380
381 while (1) {
382 len = snprintf(deldelay_buf + deldelay_cnt,
383 deldelay_size - deldelay_cnt,
384 "%x %s%c",
385 (int)file->mode, fname, '\0');
386 if ((deldelay_cnt += len) <= deldelay_size)
387 break;
388 deldelay_cnt -= len;
389 if (!flush_delete_delay())
390 return 0;
391 }
392
393 return 1;
394}
395
396static int read_delay_line(char *buf, int *flags_p)
397{
398 static int read_pos = 0;
399 int j, len, mode;
400 char *bp, *past_space;
401
402 while (1) {
403 for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
404 if (j < deldelay_cnt)
405 break;
406 if (deldelay_fd < 0) {
407 if (j > read_pos)
408 goto invalid_data;
409 return -1;
410 }
411 deldelay_cnt -= read_pos;
412 if (deldelay_cnt == deldelay_size)
413 goto invalid_data;
414 if (deldelay_cnt && read_pos) {
415 memmove(deldelay_buf, deldelay_buf + read_pos,
416 deldelay_cnt);
417 }
418 len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
419 deldelay_size - deldelay_cnt);
420 if (len == 0) {
421 if (deldelay_cnt) {
422 rprintf(FERROR,
423 "ERROR: unexpected EOF in delete-delay file.\n");
424 }
425 return -1;
426 }
427 if (len < 0) {
428 rsyserr(FERROR, errno,
429 "reading delete-delay file");
430 return -1;
431 }
432 deldelay_cnt += len;
433 read_pos = 0;
434 }
435
436 bp = deldelay_buf + read_pos;
437 if (*bp == '!') {
438 bp++;
439 *flags_p = DEL_NO_UID_WRITE;
440 } else
441 *flags_p = 0;
442
443 if (sscanf(bp, "%x ", &mode) != 1) {
444 invalid_data:
445 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
446 return -1;
447 }
448 past_space = strchr(bp, ' ') + 1;
449 len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
450 read_pos = j + 1;
451
452 if (len > MAXPATHLEN) {
453 rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
454 return -1;
455 }
456
457 /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
458 * instead of returning a pointer to our buffer. */
459 memcpy(buf, past_space, len);
460
461 return mode;
462}
463
464static void do_delayed_deletions(char *delbuf)
465{
466 int mode, flags;
467
468 if (deldelay_fd >= 0) {
469 if (deldelay_cnt && !flush_delete_delay())
470 return;
471 lseek(deldelay_fd, 0, 0);
472 }
473 while ((mode = read_delay_line(delbuf, &flags)) >= 0)
474 delete_item(delbuf, mode, flags | DEL_RECURSE);
475 if (deldelay_fd >= 0)
476 close(deldelay_fd);
477}
478
479/* This function is used to implement per-directory deletion, and is used by
480 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
481 * MAXPATHLEN buffer with the name of the directory in it (the functions we
482 * call will append names onto the end, but the old dir value will be restored
483 * on exit). */
484static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
485{
486 static int already_warned = 0;
487 struct file_list *dirlist;
488 char delbuf[MAXPATHLEN];
489 int dlen, i;
490 int save_uid_ndx = uid_ndx;
491
492 if (!fbuf) {
493 change_local_filter_dir(NULL, 0, 0);
494 return;
495 }
496
497 if (DEBUG_GTE(DEL, 2))
498 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
499
500 if (allowed_lull)
501 maybe_send_keepalive();
502
503 if (io_error && !ignore_errors) {
504 if (already_warned)
505 return;
506 rprintf(FINFO,
507 "IO error encountered -- skipping file deletion\n");
508 already_warned = 1;
509 return;
510 }
511
512 dlen = strlen(fbuf);
513 change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
514
515 if (one_file_system) {
516 if (file->flags & FLAG_TOP_DIR)
517 filesystem_dev = *fs_dev;
518 else if (filesystem_dev != *fs_dev)
519 return;
520 }
521
522 if (!uid_ndx)
523 uid_ndx = ++file_extra_cnt;
524
525 dirlist = get_dirlist(fbuf, dlen, 0);
526
527 /* If an item in dirlist is not found in flist, delete it
528 * from the filesystem. */
529 for (i = dirlist->used; i--; ) {
530 struct file_struct *fp = dirlist->files[i];
531 if (!F_IS_ACTIVE(fp))
532 continue;
533 if (fp->flags & FLAG_MOUNT_DIR && S_ISDIR(fp->mode)) {
534 if (INFO_GTE(MOUNT, 1))
535 rprintf(FINFO, "cannot delete mount point: %s\n",
536 f_name(fp, NULL));
537 continue;
538 }
539 /* Here we want to match regardless of file type. Replacement
540 * of a file with one of another type is handled separately by
541 * a delete_item call with a DEL_MAKE_ROOM flag. */
542 if (flist_find_ignore_dirness(cur_flist, fp) < 0) {
543 int flags = DEL_RECURSE;
544 if (!(fp->mode & S_IWUSR) && !am_root && (uid_t)F_OWNER(fp) == our_uid)
545 flags |= DEL_NO_UID_WRITE;
546 f_name(fp, delbuf);
547 if (delete_during == 2) {
548 if (!remember_delete(fp, delbuf, flags))
549 break;
550 } else
551 delete_item(delbuf, fp->mode, flags);
552 }
553 }
554
555 flist_free(dirlist);
556
557 if (!save_uid_ndx) {
558 --file_extra_cnt;
559 uid_ndx = 0;
560 }
561}
562
563/* This deletes any files on the receiving side that are not present on the
564 * sending side. This is used by --delete-before and --delete-after. */
565static void do_delete_pass(void)
566{
567 char fbuf[MAXPATHLEN];
568 STRUCT_STAT st;
569 int j;
570
571 /* dry_run is incremented when the destination doesn't exist yet. */
572 if (dry_run > 1 || list_only)
573 return;
574
575 for (j = 0; j < cur_flist->used; j++) {
576 struct file_struct *file = cur_flist->sorted[j];
577
578 f_name(file, fbuf);
579
580 if (!(file->flags & FLAG_CONTENT_DIR)) {
581 change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(file));
582 continue;
583 }
584
585 if (DEBUG_GTE(DEL, 1) && file->flags & FLAG_TOP_DIR)
586 rprintf(FINFO, "deleting in %s\n", fbuf);
587
588 if (link_stat(fbuf, &st, keep_dirlinks) < 0
589 || !S_ISDIR(st.st_mode))
590 continue;
591
592 delete_in_dir(fbuf, file, &st.st_dev);
593 }
594 delete_in_dir(NULL, NULL, &dev_zero);
595
596 if (INFO_GTE(FLIST, 2) && !am_server)
597 rprintf(FINFO, " \r");
598}
599
600int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
601{
602#if !defined HAVE_LUTIMES || !defined HAVE_UTIMES
603 if (S_ISLNK(file->mode)) {
604 ;
605 } else
606#endif
607 if (preserve_times && cmp_time(sxp->st.st_mtime, file->modtime) != 0)
608 return 0;
609
610 if (preserve_perms) {
611 if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
612 return 0;
613 } else if (preserve_executability
614 && ((sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0)))
615 return 0;
616
617 if (am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file))
618 return 0;
619
620 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
621 return 0;
622
623#ifdef SUPPORT_ACLS
624 if (preserve_acls && !S_ISLNK(file->mode)) {
625 if (!ACL_READY(*sxp))
626 get_acl(fname, sxp);
627 if (set_acl(NULL, file, sxp) == 0)
628 return 0;
629 }
630#endif
631#ifdef SUPPORT_XATTRS
632 if (preserve_xattrs) {
633 if (!XATTR_READY(*sxp))
634 get_xattr(fname, sxp);
635 if (xattr_diff(file, sxp, 0))
636 return 0;
637 }
638#endif
639
640 return 1;
641}
642
643void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
644 stat_x *sxp, int32 iflags, uchar fnamecmp_type,
645 const char *xname)
646{
647 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
648 int keep_time = !preserve_times ? 0
649 : S_ISDIR(file->mode) ? preserve_times > 1 :
650#if defined HAVE_LUTIMES && defined HAVE_UTIMES
651 1;
652#else
653 !S_ISLNK(file->mode);
654#endif
655
656 if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
657 iflags |= ITEM_REPORT_SIZE;
658 if (file->flags & FLAG_TIME_FAILED) { /* symlinks only */
659 if (iflags & ITEM_LOCAL_CHANGE)
660 iflags |= symlink_timeset_failed_flags;
661 } else if (keep_time
662 ? cmp_time(file->modtime, sxp->st.st_mtime) != 0
663 : iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !(iflags & ITEM_MATCHED)
664 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
665 iflags |= ITEM_REPORT_TIME;
666#if !defined HAVE_LCHMOD && !defined HAVE_SETATTRLIST
667 if (S_ISLNK(file->mode)) {
668 ;
669 } else
670#endif
671 if (preserve_perms) {
672 if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
673 iflags |= ITEM_REPORT_PERMS;
674 } else if (preserve_executability
675 && ((sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0)))
676 iflags |= ITEM_REPORT_PERMS;
677 if (uid_ndx && am_root && (uid_t)F_OWNER(file) != sxp->st.st_uid)
678 iflags |= ITEM_REPORT_OWNER;
679 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
680 && sxp->st.st_gid != (gid_t)F_GROUP(file))
681 iflags |= ITEM_REPORT_GROUP;
682#ifdef SUPPORT_ACLS
683 if (preserve_acls && !S_ISLNK(file->mode)) {
684 if (!ACL_READY(*sxp))
685 get_acl(fnamecmp, sxp);
686 if (set_acl(NULL, file, sxp) == 0)
687 iflags |= ITEM_REPORT_ACL;
688 }
689#endif
690#ifdef SUPPORT_XATTRS
691 if (preserve_xattrs) {
692 if (!XATTR_READY(*sxp))
693 get_xattr(fnamecmp, sxp);
694 if (xattr_diff(file, sxp, 1))
695 iflags |= ITEM_REPORT_XATTR;
696 }
697#endif
698 } else {
699#ifdef SUPPORT_XATTRS
700 if (preserve_xattrs && xattr_diff(file, NULL, 1))
701 iflags |= ITEM_REPORT_XATTR;
702#endif
703 iflags |= ITEM_IS_NEW;
704 }
705
706 iflags &= 0xffff;
707 if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || INFO_GTE(NAME, 2)
708 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
709 if (protocol_version >= 29) {
710 if (ndx >= 0)
711 write_ndx(sock_f_out, ndx);
712 write_shortint(sock_f_out, iflags);
713 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
714 write_byte(sock_f_out, fnamecmp_type);
715 if (iflags & ITEM_XNAME_FOLLOWS)
716 write_vstring(sock_f_out, xname, strlen(xname));
717#ifdef SUPPORT_XATTRS
718 if (preserve_xattrs && !dry_run
719 && iflags & (ITEM_REPORT_XATTR|ITEM_TRANSFER)) {
720 send_xattr_request(NULL, file,
721 iflags & ITEM_REPORT_XATTR ? sock_f_out : -1);
722 }
723#endif
724 } else if (ndx >= 0) {
725 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
726 log_item(code, file, &stats, iflags, xname);
727 }
728 }
729}
730
731
732/* Perform our quick-check heuristic for determining if a file is unchanged. */
733int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
734{
735 if (st->st_size != F_LENGTH(file))
736 return 0;
737
738 /* if always checksum is set then we use the checksum instead
739 of the file time to determine whether to sync */
740 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
741 char sum[MAX_DIGEST_LEN];
742 file_checksum(fn, sum, st->st_size);
743 return memcmp(sum, F_SUM(file), checksum_len) == 0;
744 }
745
746 if (size_only > 0)
747 return 1;
748
749 if (ignore_times)
750 return 0;
751
752 return cmp_time(st->st_mtime, file->modtime) == 0;
753}
754
755
756/*
757 * set (initialize) the size entries in the per-file sum_struct
758 * calculating dynamic block and checksum sizes.
759 *
760 * This is only called from generate_and_send_sums() but is a separate
761 * function to encapsulate the logic.
762 *
763 * The block size is a rounded square root of file length.
764 *
765 * The checksum size is determined according to:
766 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
767 * provided by Donovan Baarda which gives a probability of rsync
768 * algorithm corrupting data and falling back using the whole md4
769 * checksums.
770 *
771 * This might be made one of several selectable heuristics.
772 */
773static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
774{
775 int32 blength;
776 int s2length;
777 int64 l;
778
779 if (block_size)
780 blength = block_size;
781 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
782 blength = BLOCK_SIZE;
783 else {
784 int32 max_blength = protocol_version < 30 ? OLD_MAX_BLOCK_SIZE : MAX_BLOCK_SIZE;
785 int32 c;
786 int cnt;
787 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
788 if (c < 0 || c >= max_blength)
789 blength = max_blength;
790 else {
791 blength = 0;
792 do {
793 blength |= c;
794 if (len < (int64)blength * blength)
795 blength &= ~c;
796 c >>= 1;
797 } while (c >= 8); /* round to multiple of 8 */
798 blength = MAX(blength, BLOCK_SIZE);
799 }
800 }
801
802 if (protocol_version < 27) {
803 s2length = csum_length;
804 } else if (csum_length == SUM_LENGTH) {
805 s2length = SUM_LENGTH;
806 } else {
807 int32 c;
808 int b = BLOCKSUM_BIAS;
809 for (l = len; l >>= 1; b += 2) {}
810 for (c = blength; (c >>= 1) && b; b--) {}
811 /* add a bit, subtract rollsum, round up. */
812 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
813 s2length = MAX(s2length, csum_length);
814 s2length = MIN(s2length, SUM_LENGTH);
815 }
816
817 sum->flength = len;
818 sum->blength = blength;
819 sum->s2length = s2length;
820 sum->remainder = (int32)(len % blength);
821 sum->count = (int32)(l = (len / blength) + (sum->remainder != 0));
822
823 if ((int64)sum->count != l)
824 sum->count = -1;
825
826 if (sum->count && DEBUG_GTE(DELTASUM, 2)) {
827 rprintf(FINFO,
828 "count=%s rem=%ld blength=%ld s2length=%d flength=%s\n",
829 big_num(sum->count), (long)sum->remainder, (long)sum->blength,
830 sum->s2length, big_num(sum->flength));
831 }
832}
833
834
835/*
836 * Generate and send a stream of signatures/checksums that describe a buffer
837 *
838 * Generate approximately one checksum every block_len bytes.
839 */
840static int generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
841{
842 int32 i;
843 struct map_struct *mapbuf;
844 struct sum_struct sum;
845 OFF_T offset = 0;
846
847 sum_sizes_sqroot(&sum, len);
848 if (sum.count < 0)
849 return -1;
850 write_sum_head(f_out, &sum);
851
852 if (append_mode > 0 && f_copy < 0)
853 return 0;
854
855 if (len > 0)
856 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
857 else
858 mapbuf = NULL;
859
860 for (i = 0; i < sum.count; i++) {
861 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
862 char *map = map_ptr(mapbuf, offset, n1);
863 char sum2[SUM_LENGTH];
864 uint32 sum1;
865
866 len -= n1;
867 offset += n1;
868
869 if (f_copy >= 0) {
870 full_write(f_copy, map, n1);
871 if (append_mode > 0)
872 continue;
873 }
874
875 sum1 = get_checksum1(map, n1);
876 get_checksum2(map, n1, sum2);
877
878 if (DEBUG_GTE(DELTASUM, 3)) {
879 rprintf(FINFO,
880 "chunk[%s] offset=%s len=%ld sum1=%08lx\n",
881 big_num(i), big_num(offset - n1), (long)n1,
882 (unsigned long)sum1);
883 }
884 write_int(f_out, sum1);
885 write_buf(f_out, sum2, sum.s2length);
886 }
887
888 if (mapbuf)
889 unmap_file(mapbuf);
890
891 return 0;
892}
893
894
895/* Try to find a filename in the same dir as "fname" with a similar name. */
896static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
897{
898 int fname_len, fname_suf_len;
899 const char *fname_suf, *fname = file->basename;
900 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
901 int j, lowest_j = -1;
902
903 fname_len = strlen(fname);
904 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
905
906 for (j = 0; j < dirlist->used; j++) {
907 struct file_struct *fp = dirlist->files[j];
908 const char *suf, *name;
909 int len, suf_len;
910 uint32 dist;
911
912 if (!S_ISREG(fp->mode) || !F_LENGTH(fp)
913 || fp->flags & FLAG_FILE_SENT)
914 continue;
915
916 name = fp->basename;
917
918 if (F_LENGTH(fp) == F_LENGTH(file)
919 && cmp_time(fp->modtime, file->modtime) == 0) {
920 if (DEBUG_GTE(FUZZY, 2)) {
921 rprintf(FINFO,
922 "fuzzy size/modtime match for %s\n",
923 name);
924 }
925 return j;
926 }
927
928 len = strlen(name);
929 suf = find_filename_suffix(name, len, &suf_len);
930
931 dist = fuzzy_distance(name, len, fname, fname_len);
932 /* Add some extra weight to how well the suffixes match. */
933 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
934 * 10;
935 if (DEBUG_GTE(FUZZY, 2)) {
936 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
937 name, (int)(dist>>16), (int)(dist&0xFFFF));
938 }
939 if (dist <= lowest_dist) {
940 lowest_dist = dist;
941 lowest_j = j;
942 }
943 }
944
945 return lowest_j;
946}
947
948/* Copy a file found in our --copy-dest handling. */
949static int copy_altdest_file(const char *src, const char *dest, struct file_struct *file)
950{
951 char buf[MAXPATHLEN];
952 const char *copy_to, *partialptr;
953 int save_preserve_xattrs = preserve_xattrs;
954 int ok, fd_w;
955
956 if (inplace) {
957 /* Let copy_file open the destination in place. */
958 fd_w = -1;
959 copy_to = dest;
960 } else {
961 fd_w = open_tmpfile(buf, dest, file);
962 if (fd_w < 0)
963 return -1;
964 copy_to = buf;
965 }
966 cleanup_set(copy_to, NULL, NULL, -1, -1);
967 if (copy_file(src, copy_to, fd_w, file->mode, 0) < 0) {
968 if (INFO_GTE(COPY, 1)) {
969 rsyserr(FINFO, errno, "copy_file %s => %s",
970 full_fname(src), copy_to);
971 }
972 /* Try to clean up. */
973 unlink(copy_to);
974 cleanup_disable();
975 return -1;
976 }
977 partialptr = partial_dir ? partial_dir_fname(dest) : NULL;
978 preserve_xattrs = 0; /* xattrs were copied with file */
979 ok = finish_transfer(dest, copy_to, src, partialptr, file, 1, 0);
980 preserve_xattrs = save_preserve_xattrs;
981 cleanup_disable();
982 return ok ? 0 : -1;
983}
984
985/* This is only called for regular files. We return -2 if we've finished
986 * handling the file, -1 if no dest-linking occurred, or a non-negative
987 * value if we found an alternate basis file. */
988static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
989 char *cmpbuf, stat_x *sxp, int itemizing,
990 enum logcode code)
991{
992 int best_match = -1;
993 int match_level = 0;
994 int j = 0;
995
996 do {
997 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
998 if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
999 continue;
1000 switch (match_level) {
1001 case 0:
1002 best_match = j;
1003 match_level = 1;
1004 /* FALL THROUGH */
1005 case 1:
1006 if (!unchanged_file(cmpbuf, file, &sxp->st))
1007 continue;
1008 best_match = j;
1009 match_level = 2;
1010 /* FALL THROUGH */
1011 case 2:
1012 if (!unchanged_attrs(cmpbuf, file, sxp))
1013 continue;
1014 best_match = j;
1015 match_level = 3;
1016 break;
1017 }
1018 break;
1019 } while (basis_dir[++j] != NULL);
1020
1021 if (!match_level)
1022 return -1;
1023
1024 if (j != best_match) {
1025 j = best_match;
1026 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1027 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1028 return -1;
1029 }
1030
1031 if (match_level == 3 && !copy_dest) {
1032#ifdef SUPPORT_HARD_LINKS
1033 if (link_dest) {
1034 if (!hard_link_one(file, fname, cmpbuf, 1))
1035 goto try_a_copy;
1036 if (preserve_hard_links && F_IS_HLINKED(file))
1037 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, j);
1038 if (!maybe_ATTRS_REPORT && (INFO_GTE(NAME, 2) || stdout_format_has_i > 1)) {
1039 itemize(cmpbuf, file, ndx, 1, sxp,
1040 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
1041 0, "");
1042 }
1043 } else
1044#endif
1045 if (itemizing)
1046 itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
1047 if (INFO_GTE(NAME, 2) && maybe_ATTRS_REPORT)
1048 rprintf(FCLIENT, "%s is uptodate\n", fname);
1049 return -2;
1050 }
1051
1052 if (match_level >= 2) {
1053#ifdef SUPPORT_HARD_LINKS
1054 try_a_copy: /* Copy the file locally. */
1055#endif
1056 if (!dry_run && copy_altdest_file(cmpbuf, fname, file) < 0)
1057 return -1;
1058 if (itemizing)
1059 itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
1060 if (maybe_ATTRS_REPORT
1061 && ((!itemizing && INFO_GTE(NAME, 1) && match_level == 2)
1062 || (INFO_GTE(NAME, 2) && match_level == 3))) {
1063 code = match_level == 3 ? FCLIENT : FINFO;
1064 rprintf(code, "%s%s\n", fname,
1065 match_level == 3 ? " is uptodate" : "");
1066 }
1067#ifdef SUPPORT_HARD_LINKS
1068 if (preserve_hard_links && F_IS_HLINKED(file))
1069 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, -1);
1070#endif
1071 return -2;
1072 }
1073
1074 return FNAMECMP_BASIS_DIR_LOW + j;
1075}
1076
1077/* This is only called for non-regular files. We return -2 if we've finished
1078 * handling the file, or -1 if no dest-linking occurred, or a non-negative
1079 * value if we found an alternate basis file. */
1080static int try_dests_non(struct file_struct *file, char *fname, int ndx,
1081 char *cmpbuf, stat_x *sxp, int itemizing,
1082 enum logcode code)
1083{
1084 char lnk[MAXPATHLEN];
1085 int best_match = -1;
1086 int match_level = 0;
1087 enum nonregtype type;
1088 uint32 *devp;
1089 int len, j = 0;
1090
1091#ifndef SUPPORT_LINKS
1092 if (S_ISLNK(file->mode))
1093 return -1;
1094#endif
1095 if (S_ISDIR(file->mode)) {
1096 type = TYPE_DIR;
1097 } else if (IS_SPECIAL(file->mode))
1098 type = TYPE_SPECIAL;
1099 else if (IS_DEVICE(file->mode))
1100 type = TYPE_DEVICE;
1101#ifdef SUPPORT_LINKS
1102 else if (S_ISLNK(file->mode))
1103 type = TYPE_SYMLINK;
1104#endif
1105 else {
1106 rprintf(FERROR,
1107 "internal: try_dests_non() called with invalid mode (%o)\n",
1108 (int)file->mode);
1109 exit_cleanup(RERR_UNSUPPORTED);
1110 }
1111
1112 do {
1113 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1114 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1115 continue;
1116 switch (type) {
1117 case TYPE_DIR:
1118 if (!S_ISDIR(sxp->st.st_mode))
1119 continue;
1120 break;
1121 case TYPE_SPECIAL:
1122 if (!IS_SPECIAL(sxp->st.st_mode))
1123 continue;
1124 break;
1125 case TYPE_DEVICE:
1126 if (!IS_DEVICE(sxp->st.st_mode))
1127 continue;
1128 break;
1129#ifdef SUPPORT_LINKS
1130 case TYPE_SYMLINK:
1131 if (!S_ISLNK(sxp->st.st_mode))
1132 continue;
1133 break;
1134#endif
1135 }
1136 if (match_level < 1) {
1137 match_level = 1;
1138 best_match = j;
1139 }
1140 switch (type) {
1141 case TYPE_DIR:
1142 break;
1143 case TYPE_SPECIAL:
1144 case TYPE_DEVICE:
1145 devp = F_RDEV_P(file);
1146 if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
1147 continue;
1148 break;
1149#ifdef SUPPORT_LINKS
1150 case TYPE_SYMLINK:
1151 if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
1152 continue;
1153 lnk[len] = '\0';
1154 if (strcmp(lnk, F_SYMLINK(file)) != 0)
1155 continue;
1156 break;
1157#endif
1158 }
1159 if (match_level < 2) {
1160 match_level = 2;
1161 best_match = j;
1162 }
1163 if (unchanged_attrs(cmpbuf, file, sxp)) {
1164 match_level = 3;
1165 best_match = j;
1166 break;
1167 }
1168 } while (basis_dir[++j] != NULL);
1169
1170 if (!match_level)
1171 return -1;
1172
1173 if (j != best_match) {
1174 j = best_match;
1175 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1176 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1177 return -1;
1178 }
1179
1180 if (match_level == 3) {
1181#ifdef SUPPORT_HARD_LINKS
1182 if (link_dest
1183#ifndef CAN_HARDLINK_SYMLINK
1184 && !S_ISLNK(file->mode)
1185#endif
1186#ifndef CAN_HARDLINK_SPECIAL
1187 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
1188#endif
1189 && !S_ISDIR(file->mode)) {
1190 if (do_link(cmpbuf, fname) < 0) {
1191 rsyserr(FERROR_XFER, errno,
1192 "failed to hard-link %s with %s",
1193 cmpbuf, fname);
1194 return j;
1195 }
1196 if (preserve_hard_links && F_IS_HLINKED(file))
1197 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1198 } else
1199#endif
1200 match_level = 2;
1201 if (itemizing && stdout_format_has_i
1202 && (INFO_GTE(NAME, 2) || stdout_format_has_i > 1)) {
1203 int chg = compare_dest && type != TYPE_DIR ? 0
1204 : ITEM_LOCAL_CHANGE + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1205 char *lp = match_level == 3 ? "" : NULL;
1206 itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
1207 }
1208 if (INFO_GTE(NAME, 2) && maybe_ATTRS_REPORT) {
1209 rprintf(FCLIENT, "%s%s is uptodate\n",
1210 fname, type == TYPE_DIR ? "/" : "");
1211 }
1212 return -2;
1213 }
1214
1215 return j;
1216}
1217
1218static void list_file_entry(struct file_struct *f)
1219{
1220 char permbuf[PERMSTRING_SIZE];
1221 int64 len;
1222 int colwidth = human_readable ? 14 : 11;
1223
1224 if (!F_IS_ACTIVE(f)) {
1225 /* this can happen if duplicate names were removed */
1226 return;
1227 }
1228
1229 permstring(permbuf, f->mode);
1230 len = F_LENGTH(f);
1231
1232 /* TODO: indicate '+' if the entry has an ACL. */
1233
1234#ifdef SUPPORT_LINKS
1235 if (preserve_links && S_ISLNK(f->mode)) {
1236 rprintf(FINFO, "%s %*s %s %s -> %s\n",
1237 permbuf, colwidth, comma_num(len),
1238 timestring(f->modtime), f_name(f, NULL),
1239 F_SYMLINK(f));
1240 } else
1241#endif
1242 {
1243 rprintf(FINFO, "%s %*s %s %s\n",
1244 permbuf, colwidth, comma_num(len),
1245 timestring(f->modtime), f_name(f, NULL));
1246 }
1247}
1248
1249static int phase = 0;
1250static int dflt_perms;
1251
1252static int implied_dirs_are_missing;
1253/* Helper for recv_generator's skip_dir and dry_missing_dir tests. */
1254static BOOL is_below(struct file_struct *file, struct file_struct *subtree)
1255{
1256 return F_DEPTH(file) > F_DEPTH(subtree)
1257 && (!implied_dirs_are_missing || f_name_has_prefix(file, subtree));
1258}
1259
1260/* Acts on the indicated item in cur_flist whose name is fname. If a dir,
1261 * make sure it exists, and has the right permissions/timestamp info. For
1262 * all other non-regular files (symlinks, etc.) we create them here. For
1263 * regular files that have changed, we try to find a basis file and then
1264 * start sending checksums. The ndx is the file's unique index value.
1265 *
1266 * The fname parameter must point to a MAXPATHLEN buffer! (e.g it gets
1267 * passed to delete_item(), which can use it during a recursive delete.)
1268 *
1269 * Note that f_out is set to -1 when doing final directory-permission and
1270 * modification-time repair. */
1271static void recv_generator(char *fname, struct file_struct *file, int ndx,
1272 int itemizing, enum logcode code, int f_out)
1273{
1274 static const char *parent_dirname = "";
1275 /* Missing dir not created due to --dry-run; will still be scanned. */
1276 static struct file_struct *dry_missing_dir = NULL;
1277 /* Missing dir whose contents are skipped altogether due to
1278 * --ignore-non-existing, daemon exclude, or mkdir failure. */
1279 static struct file_struct *skip_dir = NULL;
1280 static struct file_list *fuzzy_dirlist = NULL;
1281 static int need_fuzzy_dirlist = 0;
1282 struct file_struct *fuzzy_file = NULL;
1283 int fd = -1, f_copy = -1;
1284 stat_x sx, real_sx;
1285 STRUCT_STAT partial_st;
1286 struct file_struct *back_file = NULL;
1287 int statret, real_ret, stat_errno;
1288 char *fnamecmp, *partialptr, *backupptr = NULL;
1289 char fnamecmpbuf[MAXPATHLEN];
1290 uchar fnamecmp_type;
1291 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1292 int is_dir = !S_ISDIR(file->mode) ? 0
1293 : inc_recurse && ndx != cur_flist->ndx_start - 1 ? -1
1294 : 1;
1295
1296 if (DEBUG_GTE(GENR, 1))
1297 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
1298
1299 if (list_only) {
1300 if (is_dir < 0
1301 || (is_dir && !implied_dirs && file->flags & FLAG_IMPLIED_DIR))
1302 return;
1303 list_file_entry(file);
1304 return;
1305 }
1306
1307 if (skip_dir) {
1308 if (is_below(file, skip_dir)) {
1309 if (is_dir)
1310 file->flags |= FLAG_MISSING_DIR;
1311#ifdef SUPPORT_HARD_LINKS
1312 else if (F_IS_HLINKED(file))
1313 handle_skipped_hlink(file, itemizing, code, f_out);
1314#endif
1315 return;
1316 }
1317 skip_dir = NULL;
1318 }
1319
1320 if (daemon_filter_list.head && (*fname != '.' || fname[1])) {
1321 if (check_filter(&daemon_filter_list, FLOG, fname, is_dir) < 0) {
1322 if (is_dir < 0)
1323 return;
1324#ifdef SUPPORT_HARD_LINKS
1325 if (F_IS_HLINKED(file))
1326 handle_skipped_hlink(file, itemizing, code, f_out);
1327#endif
1328 rprintf(FERROR_XFER,
1329 "skipping daemon-excluded %s \"%s\"\n",
1330 is_dir ? "directory" : "file", fname);
1331 if (is_dir)
1332 goto skipping_dir_contents;
1333 return;
1334 }
1335 }
1336
1337 init_stat_x(&sx);
1338 if (dry_run > 1 || (dry_missing_dir && is_below(file, dry_missing_dir))) {
1339 parent_is_dry_missing:
1340 if (fuzzy_dirlist) {
1341 flist_free(fuzzy_dirlist);
1342 fuzzy_dirlist = NULL;
1343 }
1344 parent_dirname = "";
1345 statret = -1;
1346 stat_errno = ENOENT;
1347 } else {
1348 const char *dn = file->dirname ? file->dirname : ".";
1349 dry_missing_dir = NULL;
1350 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1351 if (relative_paths && !implied_dirs
1352 && do_stat(dn, &sx.st) < 0) {
1353 if (dry_run)
1354 goto parent_is_dry_missing;
1355 if (create_directory_path(fname) < 0) {
1356 rsyserr(FERROR_XFER, errno,
1357 "recv_generator: mkdir %s failed",
1358 full_fname(dn));
1359 }
1360 }
1361 if (fuzzy_dirlist) {
1362 flist_free(fuzzy_dirlist);
1363 fuzzy_dirlist = NULL;
1364 }
1365 if (fuzzy_basis)
1366 need_fuzzy_dirlist = 1;
1367#ifdef SUPPORT_ACLS
1368 if (!preserve_perms)
1369 dflt_perms = default_perms_for_dir(dn);
1370#endif
1371 }
1372 parent_dirname = dn;
1373
1374 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
1375 strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1376 fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
1377 need_fuzzy_dirlist = 0;
1378 }
1379
1380 statret = link_stat(fname, &sx.st, keep_dirlinks && is_dir);
1381 stat_errno = errno;
1382 }
1383
1384 if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
1385 if (is_dir) {
1386 if (is_dir < 0)
1387 return;
1388 skip_dir = file;
1389 file->flags |= FLAG_MISSING_DIR;
1390 }
1391#ifdef SUPPORT_HARD_LINKS
1392 else if (F_IS_HLINKED(file))
1393 handle_skipped_hlink(file, itemizing, code, f_out);
1394#endif
1395 if (INFO_GTE(SKIP, 1)) {
1396 rprintf(FINFO, "not creating new %s \"%s\"\n",
1397 is_dir ? "directory" : "file", fname);
1398 }
1399 return;
1400 }
1401
1402 if (statret == 0 && !(sx.st.st_mode & S_IWUSR)
1403 && !am_root && sx.st.st_uid == our_uid)
1404 del_opts |= DEL_NO_UID_WRITE;
1405
1406 if (ignore_existing > 0 && statret == 0
1407 && (!is_dir || !S_ISDIR(sx.st.st_mode))) {
1408 if (INFO_GTE(SKIP, 1) && is_dir >= 0)
1409 rprintf(FINFO, "%s exists\n", fname);
1410#ifdef SUPPORT_HARD_LINKS
1411 if (F_IS_HLINKED(file))
1412 handle_skipped_hlink(file, itemizing, code, f_out);
1413#endif
1414 goto cleanup;
1415 }
1416
1417 if (is_dir) {
1418 if (!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
1419 goto cleanup;
1420 if (is_dir < 0) {
1421 /* In inc_recurse mode we want to make sure any missing
1422 * directories get created while we're still processing
1423 * the parent dir (which allows us to touch the parent
1424 * dir's mtime right away). We will handle the dir in
1425 * full later (right before we handle its contents). */
1426 if (statret == 0
1427 && (S_ISDIR(sx.st.st_mode)
1428 || delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0))
1429 goto cleanup; /* Any errors get reported later. */
1430 if (do_mkdir(fname, file->mode & 0700) == 0)
1431 file->flags |= FLAG_DIR_CREATED;
1432 goto cleanup;
1433 }
1434 /* The file to be received is a directory, so we need
1435 * to prepare appropriately. If there is already a
1436 * file of that name and it is *not* a directory, then
1437 * we need to delete it. If it doesn't exist, then
1438 * (perhaps recursively) create it. */
1439 if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1440 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0)
1441 goto skipping_dir_contents;
1442 statret = -1;
1443 }
1444 if (dry_run && statret != 0) {
1445 if (!dry_missing_dir)
1446 dry_missing_dir = file;
1447 file->flags |= FLAG_MISSING_DIR;
1448 }
1449 real_ret = statret;
1450 real_sx = sx;
1451 if (file->flags & FLAG_DIR_CREATED)
1452 statret = -1;
1453 if (!preserve_perms) { /* See comment in non-dir code below. */
1454 file->mode = dest_mode(file->mode, sx.st.st_mode,
1455 dflt_perms, statret == 0);
1456 }
1457 if (statret != 0 && basis_dir[0] != NULL) {
1458 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1459 itemizing, code);
1460 if (j == -2) {
1461 itemizing = 0;
1462 code = FNONE;
1463 statret = 1;
1464 } else if (j >= 0)
1465 statret = 1;
1466 }
1467 if (itemizing && f_out != -1) {
1468 itemize(fname, file, ndx, statret, &sx,
1469 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1470 }
1471 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1472 if (!relative_paths || errno != ENOENT
1473 || create_directory_path(fname) < 0
1474 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
1475 rsyserr(FERROR_XFER, errno,
1476 "recv_generator: mkdir %s failed",
1477 full_fname(fname));
1478 skipping_dir_contents:
1479 rprintf(FERROR,
1480 "*** Skipping any contents from this failed directory ***\n");
1481 skip_dir = file;
1482 file->flags |= FLAG_MISSING_DIR;
1483 goto cleanup;
1484 }
1485 }
1486#ifdef SUPPORT_XATTRS
1487 if (preserve_xattrs && statret == 1)
1488 copy_xattrs(fnamecmpbuf, fname);
1489#endif
1490 if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
1491 && INFO_GTE(NAME, 1) && code != FNONE && f_out != -1)
1492 rprintf(code, "%s/\n", fname);
1493
1494 /* We need to ensure that the dirs in the transfer have writable
1495 * permissions during the time we are putting files within them.
1496 * This is then fixed after the transfer is done. */
1497#ifdef HAVE_CHMOD
1498 if (!am_root && !(file->mode & S_IWUSR) && dir_tweaking) {
1499 mode_t mode = file->mode | S_IWUSR;
1500 if (do_chmod(fname, mode) < 0) {
1501 rsyserr(FERROR_XFER, errno,
1502 "failed to modify permissions on %s",
1503 full_fname(fname));
1504 }
1505 need_retouch_dir_perms = 1;
1506 }
1507#endif
1508
1509 if (real_ret != 0 && one_file_system)
1510 real_sx.st.st_dev = filesystem_dev;
1511 if (inc_recurse) {
1512 if (one_file_system) {
1513 uint32 *devp = F_DIR_DEV_P(file);
1514 DEV_MAJOR(devp) = major(real_sx.st.st_dev);
1515 DEV_MINOR(devp) = minor(real_sx.st.st_dev);
1516 }
1517 }
1518 else if (delete_during && f_out != -1 && !phase
1519 && !(file->flags & FLAG_MISSING_DIR)) {
1520 if (file->flags & FLAG_CONTENT_DIR)
1521 delete_in_dir(fname, file, &real_sx.st.st_dev);
1522 else
1523 change_local_filter_dir(fname, strlen(fname), F_DEPTH(file));
1524 }
1525 goto cleanup;
1526 }
1527
1528 /* If we're not preserving permissions, change the file-list's
1529 * mode based on the local permissions and some heuristics. */
1530 if (!preserve_perms) {
1531 int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
1532 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
1533 exists);
1534 }
1535
1536#ifdef SUPPORT_HARD_LINKS
1537 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1538 && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
1539 goto cleanup;
1540#endif
1541
1542 if (preserve_links && S_ISLNK(file->mode)) {
1543#ifdef SUPPORT_LINKS
1544 const char *sl = F_SYMLINK(file);
1545 if (safe_symlinks && unsafe_symlink(sl, fname)) {
1546 if (INFO_GTE(NAME, 1)) {
1547 if (solo_file)
1548 fname = f_name(file, NULL);
1549 rprintf(FINFO,
1550 "ignoring unsafe symlink %s -> \"%s\"\n",
1551 full_fname(fname), sl);
1552 }
1553 return;
1554 }
1555 if (statret == 0) {
1556 char lnk[MAXPATHLEN];
1557 int len;
1558
1559 if (!S_ISLNK(sx.st.st_mode))
1560 statret = -1;
1561 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1562 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1563 /* The link is pointing to the right place. */
1564 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1565 if (itemizing)
1566 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1567#if defined SUPPORT_HARD_LINKS && defined CAN_HARDLINK_SYMLINK
1568 if (preserve_hard_links && F_IS_HLINKED(file))
1569 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1570#endif
1571 if (remove_source_files == 1)
1572 goto return_with_success;
1573 goto cleanup;
1574 }
1575 /* Not the right symlink (or not a symlink), so
1576 * delete it. */
1577 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_SYMLINK) != 0)
1578 goto cleanup;
1579 } else if (basis_dir[0] != NULL) {
1580 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1581 itemizing, code);
1582 if (j == -2) {
1583#ifndef CAN_HARDLINK_SYMLINK
1584 if (link_dest) {
1585 /* Resort to --copy-dest behavior. */
1586 } else
1587#endif
1588 if (!copy_dest)
1589 goto cleanup;
1590 itemizing = 0;
1591 code = FNONE;
1592 } else if (j >= 0)
1593 statret = 1;
1594 }
1595#ifdef SUPPORT_HARD_LINKS
1596 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1597 cur_flist->in_progress++;
1598 goto cleanup;
1599 }
1600#endif
1601 if (do_symlink(sl, fname) != 0) {
1602 rsyserr(FERROR_XFER, errno, "symlink %s -> \"%s\" failed",
1603 full_fname(fname), sl);
1604 } else {
1605 set_file_attrs(fname, file, NULL, NULL, 0);
1606 if (itemizing) {
1607 itemize(fname, file, ndx, statret, &sx,
1608 ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
1609 }
1610 if (code != FNONE && INFO_GTE(NAME, 1))
1611 rprintf(code, "%s -> %s\n", fname, sl);
1612#ifdef SUPPORT_HARD_LINKS
1613 if (preserve_hard_links && F_IS_HLINKED(file))
1614 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1615#endif
1616 /* This does not check remove_source_files == 1
1617 * because this is one of the items that the old
1618 * --remove-sent-files option would remove. */
1619 if (remove_source_files)
1620 goto return_with_success;
1621 }
1622#endif
1623 goto cleanup;
1624 }
1625
1626 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1627 || (preserve_specials && IS_SPECIAL(file->mode))) {
1628 uint32 *devp = F_RDEV_P(file);
1629 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1630 if (statret == 0) {
1631 int del_for_flag;
1632 if (IS_DEVICE(file->mode)) {
1633 if (!IS_DEVICE(sx.st.st_mode))
1634 statret = -1;
1635 del_for_flag = DEL_FOR_DEVICE;
1636 } else {
1637 if (!IS_SPECIAL(sx.st.st_mode))
1638 statret = -1;
1639 del_for_flag = DEL_FOR_SPECIAL;
1640 }
1641 if (statret == 0
1642 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
1643 && sx.st.st_rdev == rdev) {
1644 /* The device or special file is identical. */
1645 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1646 if (itemizing)
1647 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1648#ifdef SUPPORT_HARD_LINKS
1649 if (preserve_hard_links && F_IS_HLINKED(file))
1650 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1651#endif
1652 if (remove_source_files == 1)
1653 goto return_with_success;
1654 goto cleanup;
1655 }
1656 if (delete_item(fname, sx.st.st_mode, del_opts | del_for_flag) != 0)
1657 goto cleanup;
1658 } else if (basis_dir[0] != NULL) {
1659 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1660 itemizing, code);
1661 if (j == -2) {
1662#ifndef CAN_HARDLINK_SPECIAL
1663 if (link_dest) {
1664 /* Resort to --copy-dest behavior. */
1665 } else
1666#endif
1667 if (!copy_dest)
1668 goto cleanup;
1669 itemizing = 0;
1670 code = FNONE;
1671 } else if (j >= 0)
1672 statret = 1;
1673 }
1674#ifdef SUPPORT_HARD_LINKS
1675 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1676 cur_flist->in_progress++;
1677 goto cleanup;
1678 }
1679#endif
1680 if (DEBUG_GTE(GENR, 1)) {
1681 rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1682 fname, (int)file->mode,
1683 (long)major(rdev), (long)minor(rdev));
1684 }
1685 if (do_mknod(fname, file->mode, rdev) < 0) {
1686 rsyserr(FERROR_XFER, errno, "mknod %s failed",
1687 full_fname(fname));
1688 } else {
1689 set_file_attrs(fname, file, NULL, NULL, 0);
1690 if (itemizing) {
1691 itemize(fname, file, ndx, statret, &sx,
1692 ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
1693 }
1694 if (code != FNONE && INFO_GTE(NAME, 1))
1695 rprintf(code, "%s\n", fname);
1696#ifdef SUPPORT_HARD_LINKS
1697 if (preserve_hard_links && F_IS_HLINKED(file))
1698 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1699#endif
1700 if (remove_source_files == 1)
1701 goto return_with_success;
1702 }
1703 goto cleanup;
1704 }
1705
1706 if (!S_ISREG(file->mode)) {
1707 if (solo_file)
1708 fname = f_name(file, NULL);
1709 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1710 goto cleanup;
1711 }
1712
1713 if (max_size > 0 && F_LENGTH(file) > max_size) {
1714 if (INFO_GTE(SKIP, 1)) {
1715 if (solo_file)
1716 fname = f_name(file, NULL);
1717 rprintf(FINFO, "%s is over max-size\n", fname);
1718 }
1719 goto cleanup;
1720 }
1721 if (min_size > 0 && F_LENGTH(file) < min_size) {
1722 if (INFO_GTE(SKIP, 1)) {
1723 if (solo_file)
1724 fname = f_name(file, NULL);
1725 rprintf(FINFO, "%s is under min-size\n", fname);
1726 }
1727 goto cleanup;
1728 }
1729
1730 if (update_only > 0 && statret == 0
1731 && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
1732 if (INFO_GTE(SKIP, 1))
1733 rprintf(FINFO, "%s is newer\n", fname);
1734#ifdef SUPPORT_HARD_LINKS
1735 if (F_IS_HLINKED(file))
1736 handle_skipped_hlink(file, itemizing, code, f_out);
1737#endif
1738 goto cleanup;
1739 }
1740
1741 fnamecmp = fname;
1742 fnamecmp_type = FNAMECMP_FNAME;
1743
1744 if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
1745 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE) != 0)
1746 goto cleanup;
1747 statret = -1;
1748 stat_errno = ENOENT;
1749 }
1750
1751 if (statret != 0 && basis_dir[0] != NULL) {
1752 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
1753 itemizing, code);
1754 if (j == -2) {
1755 if (remove_source_files == 1)
1756 goto return_with_success;
1757 goto cleanup;
1758 }
1759 if (j >= 0) {
1760 fnamecmp = fnamecmpbuf;
1761 fnamecmp_type = j;
1762 statret = 0;
1763 }
1764 }
1765
1766 real_ret = statret;
1767 real_sx = sx;
1768
1769 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1770 && link_stat(partialptr, &partial_st, 0) == 0
1771 && S_ISREG(partial_st.st_mode)) {
1772 if (statret != 0)
1773 goto prepare_to_open;
1774 } else
1775 partialptr = NULL;
1776
1777 if (statret != 0 && fuzzy_dirlist) {
1778 int j = find_fuzzy(file, fuzzy_dirlist);
1779 if (j >= 0) {
1780 fuzzy_file = fuzzy_dirlist->files[j];
1781 f_name(fuzzy_file, fnamecmpbuf);
1782 if (DEBUG_GTE(FUZZY, 1)) {
1783 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1784 fname, fnamecmpbuf);
1785 }
1786 sx.st.st_size = F_LENGTH(fuzzy_file);
1787 statret = 0;
1788 fnamecmp = fnamecmpbuf;
1789 fnamecmp_type = FNAMECMP_FUZZY;
1790 }
1791 }
1792
1793 if (statret != 0) {
1794#ifdef SUPPORT_HARD_LINKS
1795 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1796 cur_flist->in_progress++;
1797 goto cleanup;
1798 }
1799#endif
1800 if (stat_errno == ENOENT)
1801 goto notify_others;
1802 rsyserr(FERROR_XFER, stat_errno, "recv_generator: failed to stat %s",
1803 full_fname(fname));
1804 goto cleanup;
1805 }
1806
1807 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1808 ;
1809 else if (fnamecmp_type == FNAMECMP_FUZZY)
1810 ;
1811 else if (unchanged_file(fnamecmp, file, &sx.st)) {
1812 if (partialptr) {
1813 do_unlink(partialptr);
1814 handle_partial_dir(partialptr, PDIR_DELETE);
1815 }
1816 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1817 if (itemizing)
1818 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
1819#ifdef SUPPORT_HARD_LINKS
1820 if (preserve_hard_links && F_IS_HLINKED(file))
1821 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1822#endif
1823 if (remove_source_files != 1)
1824 goto cleanup;
1825 return_with_success:
1826 if (!dry_run)
1827 send_msg_int(MSG_SUCCESS, ndx);
1828 goto cleanup;
1829 }
1830
1831 if (append_mode > 0 && sx.st.st_size >= F_LENGTH(file)) {
1832#ifdef SUPPORT_HARD_LINKS
1833 if (F_IS_HLINKED(file))
1834 handle_skipped_hlink(file, itemizing, code, f_out);
1835#endif
1836 goto cleanup;
1837 }
1838
1839 prepare_to_open:
1840 if (partialptr) {
1841 sx.st = partial_st;
1842 fnamecmp = partialptr;
1843 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1844 statret = 0;
1845 }
1846
1847 if (!do_xfers)
1848 goto notify_others;
1849
1850 if (read_batch || whole_file) {
1851 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1852 if (!(backupptr = get_backup_name(fname)))
1853 goto cleanup;
1854 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
1855 goto pretend_missing;
1856 if (copy_file(fname, backupptr, -1, back_file->mode, 1) < 0) {
1857 unmake_file(back_file);
1858 back_file = NULL;
1859 goto cleanup;
1860 }
1861 }
1862 goto notify_others;
1863 }
1864
1865 if (fuzzy_dirlist) {
1866 int j = flist_find(fuzzy_dirlist, file);
1867 if (j >= 0) /* don't use changing file as future fuzzy basis */
1868 fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
1869 }
1870
1871 /* open the file */
1872 if ((fd = do_open(fnamecmp, O_RDONLY, 0)) < 0) {
1873 rsyserr(FERROR, errno, "failed to open %s, continuing",
1874 full_fname(fnamecmp));
1875 pretend_missing:
1876 /* pretend the file didn't exist */
1877#ifdef SUPPORT_HARD_LINKS
1878 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1879 cur_flist->in_progress++;
1880 goto cleanup;
1881 }
1882#endif
1883 statret = real_ret = -1;
1884 goto notify_others;
1885 }
1886
1887 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1888 if (!(backupptr = get_backup_name(fname))) {
1889 close(fd);
1890 goto cleanup;
1891 }
1892 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1893 close(fd);
1894 goto pretend_missing;
1895 }
1896 if (robust_unlink(backupptr) && errno != ENOENT) {
1897 rsyserr(FERROR_XFER, errno, "unlink %s",
1898 full_fname(backupptr));
1899 unmake_file(back_file);
1900 back_file = NULL;
1901 close(fd);
1902 goto cleanup;
1903 }
1904 if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1905 int save_errno = errno ? errno : EINVAL; /* 0 paranoia */
1906 if (errno == ENOENT && make_bak_dir(backupptr) == 0) {
1907 if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0)
1908 save_errno = errno ? errno : save_errno;
1909 else
1910 save_errno = 0;
1911 }
1912 if (save_errno) {
1913 rsyserr(FERROR_XFER, save_errno, "open %s", full_fname(backupptr));
1914 unmake_file(back_file);
1915 back_file = NULL;
1916 close(fd);
1917 goto cleanup;
1918 }
1919 }
1920 fnamecmp_type = FNAMECMP_BACKUP;
1921 }
1922
1923 if (DEBUG_GTE(DELTASUM, 3)) {
1924 rprintf(FINFO, "gen mapped %s of size %s\n",
1925 fnamecmp, big_num(sx.st.st_size));
1926 }
1927
1928 if (DEBUG_GTE(DELTASUM, 2))
1929 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1930
1931 notify_others:
1932 if (remove_source_files && !delay_updates && !phase && !dry_run)
1933 increment_active_files(ndx, itemizing, code);
1934 if (inc_recurse && !dry_run)
1935 cur_flist->in_progress++;
1936#ifdef SUPPORT_HARD_LINKS
1937 if (preserve_hard_links && F_IS_HLINKED(file))
1938 file->flags |= FLAG_FILE_SENT;
1939#endif
1940 write_ndx(f_out, ndx);
1941 if (itemizing) {
1942 int iflags = ITEM_TRANSFER;
1943 if (always_checksum > 0)
1944 iflags |= ITEM_REPORT_CHANGE;
1945 if (fnamecmp_type != FNAMECMP_FNAME)
1946 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1947 if (fnamecmp_type == FNAMECMP_FUZZY)
1948 iflags |= ITEM_XNAME_FOLLOWS;
1949 itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
1950 fuzzy_file ? fuzzy_file->basename : NULL);
1951#ifdef SUPPORT_ACLS
1952 if (preserve_acls)
1953 free_acl(&real_sx);
1954#endif
1955#ifdef SUPPORT_XATTRS
1956 if (preserve_xattrs)
1957 free_xattr(&real_sx);
1958#endif
1959 }
1960
1961 if (!do_xfers) {
1962#ifdef SUPPORT_HARD_LINKS
1963 if (preserve_hard_links && F_IS_HLINKED(file))
1964 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1965#endif
1966 goto cleanup;
1967 }
1968 if (read_batch)
1969 goto cleanup;
1970
1971 if (statret != 0 || whole_file)
1972 write_sum_head(f_out, NULL);
1973 else if (sx.st.st_size <= 0) {
1974 write_sum_head(f_out, NULL);
1975 close(fd);
1976 } else {
1977 if (generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy) < 0) {
1978 rprintf(FWARNING,
1979 "WARNING: file is too large for checksum sending: %s\n",
1980 fnamecmp);
1981 write_sum_head(f_out, NULL);
1982 }
1983 close(fd);
1984 }
1985
1986 cleanup:
1987 if (back_file) {
1988 int save_preserve_xattrs = preserve_xattrs;
1989 if (f_copy >= 0)
1990 close(f_copy);
1991#ifdef SUPPORT_XATTRS
1992 if (preserve_xattrs) {
1993 copy_xattrs(fname, backupptr);
1994 preserve_xattrs = 0;
1995 }
1996#endif
1997 set_file_attrs(backupptr, back_file, NULL, NULL, 0);
1998 preserve_xattrs = save_preserve_xattrs;
1999 if (INFO_GTE(BACKUP, 1)) {
2000 rprintf(FINFO, "backed up %s to %s\n",
2001 fname, backupptr);
2002 }
2003 unmake_file(back_file);
2004 }
2005
2006#ifdef SUPPORT_ACLS
2007 if (preserve_acls)
2008 free_acl(&sx);
2009#endif
2010#ifdef SUPPORT_XATTRS
2011 if (preserve_xattrs)
2012 free_xattr(&sx);
2013#endif
2014 return;
2015}
2016
2017#ifdef SUPPORT_HARD_LINKS
2018static void handle_skipped_hlink(struct file_struct *file, int itemizing,
2019 enum logcode code, int f_out)
2020{
2021 char fbuf[MAXPATHLEN];
2022 int new_last_ndx;
2023 struct file_list *save_flist = cur_flist;
2024
2025 /* If we skip the last item in a chain of links and there was a
2026 * prior non-skipped hard-link waiting to finish, finish it now. */
2027 if ((new_last_ndx = skip_hard_link(file, &cur_flist)) < 0)
2028 return;
2029
2030 file = cur_flist->files[new_last_ndx - cur_flist->ndx_start];
2031 cur_flist->in_progress--; /* undo prior increment */
2032 f_name(file, fbuf);
2033 recv_generator(fbuf, file, new_last_ndx, itemizing, code, f_out);
2034
2035 cur_flist = save_flist;
2036}
2037#endif
2038
2039static void touch_up_dirs(struct file_list *flist, int ndx)
2040{
2041 static int counter = 0;
2042 struct file_struct *file;
2043 char *fname;
2044 int i, start, end;
2045
2046 if (ndx < 0) {
2047 start = 0;
2048 end = flist->used - 1;
2049 } else
2050 start = end = ndx;
2051
2052 /* Fix any directory permissions that were modified during the
2053 * transfer and/or re-set any tweaked modified-time values. */
2054 for (i = start; i <= end; i++, counter++) {
2055 file = flist->files[i];
2056 if (!S_ISDIR(file->mode)
2057 || (!implied_dirs && file->flags & FLAG_IMPLIED_DIR))
2058 continue;
2059 if (DEBUG_GTE(TIME, 2)) {
2060 fname = f_name(file, NULL);
2061 rprintf(FINFO, "touch_up_dirs: %s (%d)\n",
2062 NS(fname), i);
2063 }
2064 if (!F_IS_ACTIVE(file) || file->flags & FLAG_MISSING_DIR
2065 || (!need_retouch_dir_times && file->mode & S_IWUSR))
2066 continue;
2067 fname = f_name(file, NULL);
2068 if (!(file->mode & S_IWUSR))
2069 do_chmod(fname, file->mode);
2070 if (need_retouch_dir_times) {
2071 STRUCT_STAT st;
2072 if (link_stat(fname, &st, 0) == 0
2073 && cmp_time(st.st_mtime, file->modtime) != 0)
2074 set_modtime(fname, file->modtime, file->mode);
2075 }
2076 if (counter >= loopchk_limit) {
2077 if (allowed_lull)
2078 maybe_send_keepalive();
2079 else
2080 maybe_flush_socket(0);
2081 counter = 0;
2082 }
2083 }
2084}
2085
2086void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
2087{
2088 struct file_struct *file;
2089 struct file_list *flist;
2090 char fbuf[MAXPATHLEN];
2091 int ndx;
2092
2093 while (1) {
2094#ifdef SUPPORT_HARD_LINKS
2095 if (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
2096 flist = flist_for_ndx(ndx, "check_for_finished_files.1");
2097 file = flist->files[ndx - flist->ndx_start];
2098 assert(file->flags & FLAG_HLINKED);
2099 finish_hard_link(file, f_name(file, fbuf), ndx, NULL, itemizing, code, -1);
2100 flist->in_progress--;
2101 continue;
2102 }
2103#endif
2104
2105 if (check_redo && (ndx = get_redo_num()) != -1) {
2106 csum_length = SUM_LENGTH;
2107 max_size = -max_size;
2108 min_size = -min_size;
2109 ignore_existing = -ignore_existing;
2110 ignore_non_existing = -ignore_non_existing;
2111 update_only = -update_only;
2112 always_checksum = -always_checksum;
2113 size_only = -size_only;
2114 append_mode = -append_mode;
2115 make_backups = -make_backups; /* avoid dup backup w/inplace */
2116 ignore_times++;
2117
2118 flist = cur_flist;
2119 cur_flist = flist_for_ndx(ndx, "check_for_finished_files.2");
2120
2121 file = cur_flist->files[ndx - cur_flist->ndx_start];
2122 if (solo_file)
2123 strlcpy(fbuf, solo_file, sizeof fbuf);
2124 else
2125 f_name(file, fbuf);
2126 recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
2127 cur_flist->to_redo--;
2128
2129 cur_flist = flist;
2130
2131 csum_length = SHORT_SUM_LENGTH;
2132 max_size = -max_size;
2133 min_size = -min_size;
2134 ignore_existing = -ignore_existing;
2135 ignore_non_existing = -ignore_non_existing;
2136 update_only = -update_only;
2137 always_checksum = -always_checksum;
2138 size_only = -size_only;
2139 append_mode = -append_mode;
2140 make_backups = -make_backups;
2141 ignore_times--;
2142 continue;
2143 }
2144
2145 if (cur_flist == first_flist)
2146 break;
2147
2148 /* We only get here if inc_recurse is enabled. */
2149 if (first_flist->in_progress || first_flist->to_redo)
2150 break;
2151
2152 if (!read_batch) {
2153 write_ndx(sock_f_out, NDX_DONE);
2154 maybe_flush_socket(1);
2155 }
2156
2157 if (delete_during == 2 || !dir_tweaking) {
2158 /* Skip directory touch-up. */
2159 } else if (first_flist->parent_ndx >= 0)
2160 touch_up_dirs(dir_flist, first_flist->parent_ndx);
2161
2162 flist_free(first_flist); /* updates first_flist */
2163 }
2164}
2165
2166void generate_files(int f_out, const char *local_name)
2167{
2168 int i, ndx, next_loopchk = 0;
2169 char fbuf[MAXPATHLEN];
2170 int itemizing;
2171 enum logcode code;
2172 int save_info_flist = info_levels[INFO_FLIST];
2173 int save_info_progress = info_levels[INFO_PROGRESS];
2174
2175 if (protocol_version >= 29) {
2176 itemizing = 1;
2177 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
2178 code = logfile_format_has_i ? FNONE : FLOG;
2179 } else if (am_daemon) {
2180 itemizing = logfile_format_has_i && do_xfers;
2181 maybe_ATTRS_REPORT = ATTRS_REPORT;
2182 code = itemizing || !do_xfers ? FCLIENT : FINFO;
2183 } else if (!am_server) {
2184 itemizing = stdout_format_has_i;
2185 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
2186 code = itemizing ? FNONE : FINFO;
2187 } else {
2188 itemizing = 0;
2189 maybe_ATTRS_REPORT = ATTRS_REPORT;
2190 code = FINFO;
2191 }
2192 solo_file = local_name;
2193 dir_tweaking = !(list_only || solo_file || dry_run);
2194 need_retouch_dir_times = preserve_times > 1;
2195 loopchk_limit = allowed_lull ? allowed_lull * 5 : 200;
2196 symlink_timeset_failed_flags = ITEM_REPORT_TIME
2197 | (protocol_version >= 30 || !am_server ? ITEM_REPORT_TIMEFAIL : 0);
2198 implied_dirs_are_missing = relative_paths && !implied_dirs && protocol_version < 30;
2199
2200 if (DEBUG_GTE(GENR, 1))
2201 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
2202
2203 if (delete_before && !solo_file && cur_flist->used > 0)
2204 do_delete_pass();
2205 if (delete_during == 2) {
2206 deldelay_size = BIGPATHBUFLEN * 4;
2207 deldelay_buf = new_array(char, deldelay_size);
2208 if (!deldelay_buf)
2209 out_of_memory("delete-delay");
2210 }
2211 info_levels[INFO_FLIST] = info_levels[INFO_PROGRESS] = 0;
2212
2213 if (append_mode > 0 || whole_file < 0)
2214 whole_file = 0;
2215 if (DEBUG_GTE(FLIST, 1)) {
2216 rprintf(FINFO, "delta-transmission %s\n",
2217 whole_file
2218 ? "disabled for local transfer or --whole-file"
2219 : "enabled");
2220 }
2221
2222 /* Since we often fill up the outgoing socket and then just sit around
2223 * waiting for the other 2 processes to do their thing, we don't want
2224 * to exit on a timeout. If the data stops flowing, the receiver will
2225 * notice that and let us know via the redo pipe (or its closing). */
2226 ignore_timeout = 1;
2227
2228 dflt_perms = (ACCESSPERMS & ~orig_umask);
2229
2230 do {
2231#ifdef SUPPORT_HARD_LINKS
2232 if (preserve_hard_links && inc_recurse) {
2233 while (!flist_eof && file_total < FILECNT_LOOKAHEAD/2)
2234 wait_for_receiver();
2235 }
2236#endif
2237
2238 if (inc_recurse && cur_flist->parent_ndx >= 0) {
2239 struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
2240 if (solo_file)
2241 strlcpy(fbuf, solo_file, sizeof fbuf);
2242 else
2243 f_name(fp, fbuf);
2244 ndx = cur_flist->ndx_start - 1;
2245 recv_generator(fbuf, fp, ndx, itemizing, code, f_out);
2246 if (delete_during && dry_run < 2 && !list_only
2247 && !(fp->flags & FLAG_MISSING_DIR)) {
2248 if (fp->flags & FLAG_CONTENT_DIR) {
2249 dev_t dirdev;
2250 if (one_file_system) {
2251 uint32 *devp = F_DIR_DEV_P(fp);
2252 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
2253 } else
2254 dirdev = MAKEDEV(0, 0);
2255 delete_in_dir(fbuf, fp, &dirdev);
2256 } else
2257 change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(fp));
2258 }
2259 }
2260 for (i = cur_flist->low; i <= cur_flist->high; i++) {
2261 struct file_struct *file = cur_flist->sorted[i];
2262
2263 if (!F_IS_ACTIVE(file))
2264 continue;
2265
2266 if (unsort_ndx)
2267 ndx = F_NDX(file);
2268 else
2269 ndx = i + cur_flist->ndx_start;
2270
2271 if (solo_file)
2272 strlcpy(fbuf, solo_file, sizeof fbuf);
2273 else
2274 f_name(file, fbuf);
2275 recv_generator(fbuf, file, ndx, itemizing, code, f_out);
2276
2277 check_for_finished_files(itemizing, code, 0);
2278
2279 if (i + cur_flist->ndx_start >= next_loopchk) {
2280 if (allowed_lull)
2281 maybe_send_keepalive();
2282 else
2283 maybe_flush_socket(0);
2284 next_loopchk += loopchk_limit;
2285 }
2286 }
2287
2288 if (!inc_recurse) {
2289 write_ndx(f_out, NDX_DONE);
2290 break;
2291 }
2292
2293 while (1) {
2294 check_for_finished_files(itemizing, code, 1);
2295 if (cur_flist->next || flist_eof)
2296 break;
2297 wait_for_receiver();
2298 }
2299 } while ((cur_flist = cur_flist->next) != NULL);
2300
2301 if (read_batch && inc_recurse)
2302 write_ndx(f_out, NDX_DONE);
2303
2304 if (delete_during)
2305 delete_in_dir(NULL, NULL, &dev_zero);
2306 phase++;
2307 if (DEBUG_GTE(GENR, 1))
2308 rprintf(FINFO, "generate_files phase=%d\n", phase);
2309
2310 while (1) {
2311 check_for_finished_files(itemizing, code, 1);
2312 if (msgdone_cnt)
2313 break;
2314 wait_for_receiver();
2315 }
2316
2317 phase++;
2318 if (DEBUG_GTE(GENR, 1))
2319 rprintf(FINFO, "generate_files phase=%d\n", phase);
2320
2321 write_ndx(f_out, NDX_DONE);
2322 write_del_stats(f_out);
2323
2324 /* Reduce round-trip lag-time for a useless delay-updates phase. */
2325 if (protocol_version >= 29 && !delay_updates)
2326 write_ndx(f_out, NDX_DONE);
2327
2328 /* Read MSG_DONE for the redo phase (and any prior messages). */
2329 while (1) {
2330 check_for_finished_files(itemizing, code, 0);
2331 if (msgdone_cnt > 1)
2332 break;
2333 wait_for_receiver();
2334 }
2335
2336 if (protocol_version >= 29) {
2337 phase++;
2338 if (DEBUG_GTE(GENR, 1))
2339 rprintf(FINFO, "generate_files phase=%d\n", phase);
2340 if (delay_updates)
2341 write_ndx(f_out, NDX_DONE);
2342 /* Read MSG_DONE for delay-updates phase & prior messages. */
2343 while (msgdone_cnt == 2)
2344 wait_for_receiver();
2345 }
2346
2347 info_levels[INFO_FLIST] = save_info_flist;
2348 info_levels[INFO_PROGRESS] = save_info_progress;
2349
2350 if (delete_during == 2)
2351 do_delayed_deletions(fbuf);
2352 if (delete_after && !solo_file && file_total > 0)
2353 do_delete_pass();
2354
2355 if ((need_retouch_dir_perms || need_retouch_dir_times)
2356 && dir_tweaking && (!inc_recurse || delete_during == 2))
2357 touch_up_dirs(dir_flist, -1);
2358
2359 if (max_delete >= 0 && skipped_deletes) {
2360 rprintf(FWARNING,
2361 "Deletions stopped due to --max-delete limit (%d skipped)\n",
2362 skipped_deletes);
2363 io_error |= IOERR_DEL_LIMIT;
2364 }
2365
2366 if (DEBUG_GTE(GENR, 1))
2367 rprintf(FINFO, "generate_files finished\n");
2368}