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