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