- Made the itemize test check for CAN_HARDLINK_SYMLINK define instead
[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 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 && !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
586 return 0;
587
588 if (preserve_executability && ((sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0)))
589 return 0;
590
591 if (am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file))
592 return 0;
593
594 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
595 return 0;
596
597#ifdef SUPPORT_ACLS
598 if (preserve_acls && !S_ISLNK(file->mode)) {
599 if (!ACL_READY(*sxp))
600 get_acl(fname, sxp);
601 if (set_acl(NULL, file, sxp) == 0)
602 return 0;
603 }
604#endif
605#ifdef SUPPORT_XATTRS
606 if (preserve_xattrs) {
607 if (!XATTR_READY(*sxp))
608 get_xattr(fname, sxp);
609 if (xattr_diff(file, sxp, 0))
610 return 0;
611 }
612#endif
613
614 return 1;
615}
616
617void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
618 stat_x *sxp, int32 iflags, uchar fnamecmp_type,
619 const char *xname)
620{
621 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
622 int keep_time = !preserve_times ? 0
623 : S_ISDIR(file->mode) ? preserve_times > 1 :
624#if defined HAVE_LUTIMES && defined HAVE_UTIMES
625 (receiver_symlink_times && !(file->flags & FLAG_TIME_FAILED)) ||
626#endif
627 !S_ISLNK(file->mode);
628
629 if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
630 iflags |= ITEM_REPORT_SIZE;
631 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
632 && !(iflags & ITEM_MATCHED)
633 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
634 || (keep_time && cmp_time(file->modtime, sxp->st.st_mtime) != 0))
635 iflags |= ITEM_REPORT_TIME;
636#if !defined HAVE_LCHMOD && !defined HAVE_SETATTRLIST
637 if (S_ISLNK(file->mode)) {
638 ;
639 } else
640#endif
641 if ((preserve_perms || preserve_executability)
642 && !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
643 iflags |= ITEM_REPORT_PERMS;
644 if (uid_ndx && am_root && (uid_t)F_OWNER(file) != sxp->st.st_uid)
645 iflags |= ITEM_REPORT_OWNER;
646 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
647 && sxp->st.st_gid != (gid_t)F_GROUP(file))
648 iflags |= ITEM_REPORT_GROUP;
649#ifdef SUPPORT_ACLS
650 if (preserve_acls && !S_ISLNK(file->mode)) {
651 if (!ACL_READY(*sxp))
652 get_acl(fnamecmp, sxp);
653 if (set_acl(NULL, file, sxp) == 0)
654 iflags |= ITEM_REPORT_ACL;
655 }
656#endif
657#ifdef SUPPORT_XATTRS
658 if (preserve_xattrs) {
659 if (!XATTR_READY(*sxp))
660 get_xattr(fnamecmp, sxp);
661 if (xattr_diff(file, sxp, 1))
662 iflags |= ITEM_REPORT_XATTR;
663 }
664#endif
665 } else {
666#ifdef SUPPORT_XATTRS
667 if (preserve_xattrs && xattr_diff(file, NULL, 1))
668 iflags |= ITEM_REPORT_XATTR;
669#endif
670 iflags |= ITEM_IS_NEW;
671 }
672
673 iflags &= 0xffff;
674 if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || verbose > 1
675 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
676 if (protocol_version >= 29) {
677 if (ndx >= 0)
678 write_ndx(sock_f_out, ndx);
679 write_shortint(sock_f_out, iflags);
680 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
681 write_byte(sock_f_out, fnamecmp_type);
682 if (iflags & ITEM_XNAME_FOLLOWS)
683 write_vstring(sock_f_out, xname, strlen(xname));
684#ifdef SUPPORT_XATTRS
685 if (iflags & ITEM_REPORT_XATTR && !dry_run)
686 send_xattr_request(NULL, file, sock_f_out);
687#endif
688 } else if (ndx >= 0) {
689 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
690 log_item(code, file, &stats, iflags, xname);
691 }
692 }
693}
694
695
696/* Perform our quick-check heuristic for determining if a file is unchanged. */
697int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
698{
699 if (st->st_size != F_LENGTH(file))
700 return 0;
701
702 /* if always checksum is set then we use the checksum instead
703 of the file time to determine whether to sync */
704 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
705 char sum[MAX_DIGEST_LEN];
706 file_checksum(fn, sum, st->st_size);
707 return memcmp(sum, F_SUM(file), checksum_len) == 0;
708 }
709
710 if (size_only > 0)
711 return 1;
712
713 if (ignore_times)
714 return 0;
715
716 return cmp_time(st->st_mtime, file->modtime) == 0;
717}
718
719
720/*
721 * set (initialize) the size entries in the per-file sum_struct
722 * calculating dynamic block and checksum sizes.
723 *
724 * This is only called from generate_and_send_sums() but is a separate
725 * function to encapsulate the logic.
726 *
727 * The block size is a rounded square root of file length.
728 *
729 * The checksum size is determined according to:
730 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
731 * provided by Donovan Baarda which gives a probability of rsync
732 * algorithm corrupting data and falling back using the whole md4
733 * checksums.
734 *
735 * This might be made one of several selectable heuristics.
736 */
737static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
738{
739 int32 blength;
740 int s2length;
741
742 if (block_size)
743 blength = block_size;
744 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
745 blength = BLOCK_SIZE;
746 else {
747 int32 c;
748 int64 l;
749 int cnt;
750 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
751 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
752 blength = MAX_BLOCK_SIZE;
753 else {
754 blength = 0;
755 do {
756 blength |= c;
757 if (len < (int64)blength * blength)
758 blength &= ~c;
759 c >>= 1;
760 } while (c >= 8); /* round to multiple of 8 */
761 blength = MAX(blength, BLOCK_SIZE);
762 }
763 }
764
765 if (protocol_version < 27) {
766 s2length = csum_length;
767 } else if (csum_length == SUM_LENGTH) {
768 s2length = SUM_LENGTH;
769 } else {
770 int32 c;
771 int64 l;
772 int b = BLOCKSUM_BIAS;
773 for (l = len; l >>= 1; b += 2) {}
774 for (c = blength; (c >>= 1) && b; b--) {}
775 /* add a bit, subtract rollsum, round up. */
776 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
777 s2length = MAX(s2length, csum_length);
778 s2length = MIN(s2length, SUM_LENGTH);
779 }
780
781 sum->flength = len;
782 sum->blength = blength;
783 sum->s2length = s2length;
784 sum->remainder = (int32)(len % blength);
785 sum->count = (int32)(len / blength) + (sum->remainder != 0);
786
787 if (sum->count && verbose > 2) {
788 rprintf(FINFO,
789 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
790 (double)sum->count, (long)sum->remainder, (long)sum->blength,
791 sum->s2length, (double)sum->flength);
792 }
793}
794
795
796/*
797 * Generate and send a stream of signatures/checksums that describe a buffer
798 *
799 * Generate approximately one checksum every block_len bytes.
800 */
801static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
802{
803 int32 i;
804 struct map_struct *mapbuf;
805 struct sum_struct sum;
806 OFF_T offset = 0;
807
808 sum_sizes_sqroot(&sum, len);
809 write_sum_head(f_out, &sum);
810
811 if (append_mode > 0 && f_copy < 0)
812 return;
813
814 if (len > 0)
815 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
816 else
817 mapbuf = NULL;
818
819 for (i = 0; i < sum.count; i++) {
820 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
821 char *map = map_ptr(mapbuf, offset, n1);
822 char sum2[SUM_LENGTH];
823 uint32 sum1;
824
825 len -= n1;
826 offset += n1;
827
828 if (f_copy >= 0) {
829 full_write(f_copy, map, n1);
830 if (append_mode > 0)
831 continue;
832 }
833
834 sum1 = get_checksum1(map, n1);
835 get_checksum2(map, n1, sum2);
836
837 if (verbose > 3) {
838 rprintf(FINFO,
839 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
840 (double)i, (double)offset - n1, (long)n1,
841 (unsigned long)sum1);
842 }
843 write_int(f_out, sum1);
844 write_buf(f_out, sum2, sum.s2length);
845 }
846
847 if (mapbuf)
848 unmap_file(mapbuf);
849}
850
851
852/* Try to find a filename in the same dir as "fname" with a similar name. */
853static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
854{
855 int fname_len, fname_suf_len;
856 const char *fname_suf, *fname = file->basename;
857 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
858 int j, lowest_j = -1;
859
860 fname_len = strlen(fname);
861 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
862
863 for (j = 0; j < dirlist->used; j++) {
864 struct file_struct *fp = dirlist->files[j];
865 const char *suf, *name;
866 int len, suf_len;
867 uint32 dist;
868
869 if (!S_ISREG(fp->mode) || !F_LENGTH(fp)
870 || fp->flags & FLAG_FILE_SENT)
871 continue;
872
873 name = fp->basename;
874
875 if (F_LENGTH(fp) == F_LENGTH(file)
876 && cmp_time(fp->modtime, file->modtime) == 0) {
877 if (verbose > 4) {
878 rprintf(FINFO,
879 "fuzzy size/modtime match for %s\n",
880 name);
881 }
882 return j;
883 }
884
885 len = strlen(name);
886 suf = find_filename_suffix(name, len, &suf_len);
887
888 dist = fuzzy_distance(name, len, fname, fname_len);
889 /* Add some extra weight to how well the suffixes match. */
890 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
891 * 10;
892 if (verbose > 4) {
893 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
894 name, (int)(dist>>16), (int)(dist&0xFFFF));
895 }
896 if (dist <= lowest_dist) {
897 lowest_dist = dist;
898 lowest_j = j;
899 }
900 }
901
902 return lowest_j;
903}
904
905/* Copy a file found in our --copy-dest handling. */
906static int copy_altdest_file(const char *src, const char *dest, struct file_struct *file)
907{
908 char buf[MAXPATHLEN];
909 const char *copy_to, *partialptr;
910 int ok, fd_w;
911
912 if (inplace) {
913 /* Let copy_file open the destination in place. */
914 fd_w = -1;
915 copy_to = dest;
916 } else {
917 fd_w = open_tmpfile(buf, dest, file);
918 if (fd_w < 0)
919 return -1;
920 copy_to = buf;
921 }
922 cleanup_set(copy_to, NULL, NULL, -1, -1);
923 if (copy_file(src, copy_to, fd_w, file->mode, 0) < 0) {
924 if (verbose) {
925 rsyserr(FINFO, errno, "copy_file %s => %s",
926 full_fname(src), copy_to);
927 }
928 /* Try to clean up. */
929 unlink(copy_to);
930 cleanup_disable();
931 return -1;
932 }
933 partialptr = partial_dir ? partial_dir_fname(dest) : NULL;
934 ok = finish_transfer(dest, copy_to, src, partialptr, file, 1, 0);
935 cleanup_disable();
936 return ok ? 0 : -1;
937}
938
939/* This is only called for regular files. We return -2 if we've finished
940 * handling the file, -1 if no dest-linking occurred, or a non-negative
941 * value if we found an alternate basis file. */
942static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
943 char *cmpbuf, stat_x *sxp, int itemizing,
944 enum logcode code)
945{
946 int best_match = -1;
947 int match_level = 0;
948 int j = 0;
949
950 do {
951 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
952 if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
953 continue;
954 switch (match_level) {
955 case 0:
956 best_match = j;
957 match_level = 1;
958 /* FALL THROUGH */
959 case 1:
960 if (!unchanged_file(cmpbuf, file, &sxp->st))
961 continue;
962 best_match = j;
963 match_level = 2;
964 /* FALL THROUGH */
965 case 2:
966 if (!unchanged_attrs(cmpbuf, file, sxp))
967 continue;
968 best_match = j;
969 match_level = 3;
970 break;
971 }
972 break;
973 } while (basis_dir[++j] != NULL);
974
975 if (!match_level)
976 return -1;
977
978 if (j != best_match) {
979 j = best_match;
980 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
981 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
982 return -1;
983 }
984
985 if (match_level == 3 && !copy_dest) {
986#ifdef SUPPORT_HARD_LINKS
987 if (link_dest) {
988 if (!hard_link_one(file, fname, cmpbuf, 1))
989 goto try_a_copy;
990 if (preserve_hard_links && F_IS_HLINKED(file))
991 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, j);
992 if (!maybe_ATTRS_REPORT && (verbose > 1 || stdout_format_has_i > 1)) {
993 itemize(cmpbuf, file, ndx, 1, sxp,
994 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
995 0, "");
996 }
997 } else
998#endif
999 if (itemizing)
1000 itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
1001 if (verbose > 1 && maybe_ATTRS_REPORT)
1002 rprintf(FCLIENT, "%s is uptodate\n", fname);
1003 return -2;
1004 }
1005
1006 if (match_level >= 2) {
1007#ifdef SUPPORT_HARD_LINKS
1008 try_a_copy: /* Copy the file locally. */
1009#endif
1010 if (!dry_run && copy_altdest_file(cmpbuf, fname, file) < 0)
1011 return -1;
1012 if (itemizing)
1013 itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
1014 if (maybe_ATTRS_REPORT
1015 && ((!itemizing && verbose && match_level == 2)
1016 || (verbose > 1 && match_level == 3))) {
1017 code = match_level == 3 ? FCLIENT : FINFO;
1018 rprintf(code, "%s%s\n", fname,
1019 match_level == 3 ? " is uptodate" : "");
1020 }
1021#ifdef SUPPORT_HARD_LINKS
1022 if (preserve_hard_links && F_IS_HLINKED(file))
1023 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, -1);
1024#endif
1025 return -2;
1026 }
1027
1028 return FNAMECMP_BASIS_DIR_LOW + j;
1029}
1030
1031/* This is only called for non-regular files. We return -2 if we've finished
1032 * handling the file, or -1 if no dest-linking occurred, or a non-negative
1033 * value if we found an alternate basis file. */
1034static int try_dests_non(struct file_struct *file, char *fname, int ndx,
1035 char *cmpbuf, stat_x *sxp, int itemizing,
1036 enum logcode code)
1037{
1038 char lnk[MAXPATHLEN];
1039 int best_match = -1;
1040 int match_level = 0;
1041 enum nonregtype type;
1042 uint32 *devp;
1043 int len, j = 0;
1044
1045#ifndef SUPPORT_LINKS
1046 if (S_ISLNK(file->mode))
1047 return -1;
1048#endif
1049 if (S_ISDIR(file->mode)) {
1050 type = TYPE_DIR;
1051 } else if (IS_SPECIAL(file->mode))
1052 type = TYPE_SPECIAL;
1053 else if (IS_DEVICE(file->mode))
1054 type = TYPE_DEVICE;
1055#ifdef SUPPORT_LINKS
1056 else if (S_ISLNK(file->mode))
1057 type = TYPE_SYMLINK;
1058#endif
1059 else {
1060 rprintf(FERROR,
1061 "internal: try_dests_non() called with invalid mode (%o)\n",
1062 (int)file->mode);
1063 exit_cleanup(RERR_UNSUPPORTED);
1064 }
1065
1066 do {
1067 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1068 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1069 continue;
1070 switch (type) {
1071 case TYPE_DIR:
1072 if (!S_ISDIR(sxp->st.st_mode))
1073 continue;
1074 break;
1075 case TYPE_SPECIAL:
1076 if (!IS_SPECIAL(sxp->st.st_mode))
1077 continue;
1078 break;
1079 case TYPE_DEVICE:
1080 if (!IS_DEVICE(sxp->st.st_mode))
1081 continue;
1082 break;
1083#ifdef SUPPORT_LINKS
1084 case TYPE_SYMLINK:
1085 if (!S_ISLNK(sxp->st.st_mode))
1086 continue;
1087 break;
1088#endif
1089 }
1090 if (match_level < 1) {
1091 match_level = 1;
1092 best_match = j;
1093 }
1094 switch (type) {
1095 case TYPE_DIR:
1096 break;
1097 case TYPE_SPECIAL:
1098 case TYPE_DEVICE:
1099 devp = F_RDEV_P(file);
1100 if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
1101 continue;
1102 break;
1103#ifdef SUPPORT_LINKS
1104 case TYPE_SYMLINK:
1105 if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
1106 continue;
1107 lnk[len] = '\0';
1108 if (strcmp(lnk, F_SYMLINK(file)) != 0)
1109 continue;
1110 break;
1111#endif
1112 }
1113 if (match_level < 2) {
1114 match_level = 2;
1115 best_match = j;
1116 }
1117 if (unchanged_attrs(cmpbuf, file, sxp)) {
1118 match_level = 3;
1119 best_match = j;
1120 break;
1121 }
1122 } while (basis_dir[++j] != NULL);
1123
1124 if (!match_level)
1125 return -1;
1126
1127 if (j != best_match) {
1128 j = best_match;
1129 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1130 if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1131 return -1;
1132 }
1133
1134 if (match_level == 3) {
1135#ifdef SUPPORT_HARD_LINKS
1136 if (link_dest
1137#ifndef CAN_HARDLINK_SYMLINK
1138 && !S_ISLNK(file->mode)
1139#endif
1140#ifndef CAN_HARDLINK_SPECIAL
1141 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
1142#endif
1143 && !S_ISDIR(file->mode)) {
1144 if (do_link(cmpbuf, fname) < 0) {
1145 rsyserr(FERROR_XFER, errno,
1146 "failed to hard-link %s with %s",
1147 cmpbuf, fname);
1148 return j;
1149 }
1150 if (preserve_hard_links && F_IS_HLINKED(file))
1151 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1152 } else
1153#endif
1154 match_level = 2;
1155 if (itemizing && stdout_format_has_i
1156 && (verbose > 1 || stdout_format_has_i > 1)) {
1157 int chg = compare_dest && type != TYPE_DIR ? 0
1158 : ITEM_LOCAL_CHANGE
1159 + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1160 char *lp = match_level == 3 ? "" : NULL;
1161 itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
1162 }
1163 if (verbose > 1 && maybe_ATTRS_REPORT) {
1164 rprintf(FCLIENT, "%s%s is uptodate\n",
1165 fname, type == TYPE_DIR ? "/" : "");
1166 }
1167 return -2;
1168 }
1169
1170 return j;
1171}
1172
1173static void list_file_entry(struct file_struct *f)
1174{
1175 char permbuf[PERMSTRING_SIZE];
1176 double len;
1177
1178 if (!F_IS_ACTIVE(f)) {
1179 /* this can happen if duplicate names were removed */
1180 return;
1181 }
1182
1183 permstring(permbuf, f->mode);
1184 len = F_LENGTH(f);
1185
1186 /* TODO: indicate '+' if the entry has an ACL. */
1187
1188#ifdef SUPPORT_LINKS
1189 if (preserve_links && S_ISLNK(f->mode)) {
1190 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
1191 permbuf, len, timestring(f->modtime),
1192 f_name(f, NULL), F_SYMLINK(f));
1193 } else
1194#endif
1195 {
1196 rprintf(FINFO, "%s %11.0f %s %s\n",
1197 permbuf, len, timestring(f->modtime),
1198 f_name(f, NULL));
1199 }
1200}
1201
1202static int phase = 0;
1203static int dflt_perms;
1204
1205/* Acts on the indicated item in cur_flist whose name is fname. If a dir,
1206 * make sure it exists, and has the right permissions/timestamp info. For
1207 * all other non-regular files (symlinks, etc.) we create them here. For
1208 * regular files that have changed, we try to find a basis file and then
1209 * start sending checksums. The ndx is the file's unique index value.
1210 *
1211 * When fname is non-null, it must point to a MAXPATHLEN buffer!
1212 *
1213 * Note that f_out is set to -1 when doing final directory-permission and
1214 * modification-time repair. */
1215static void recv_generator(char *fname, struct file_struct *file, int ndx,
1216 int itemizing, enum logcode code, int f_out)
1217{
1218 static int missing_below = -1, excluded_below = -1;
1219 static const char *parent_dirname = "";
1220 static struct file_struct *missing_dir = NULL, *excluded_dir = NULL;
1221 static struct file_list *fuzzy_dirlist = NULL;
1222 static int need_fuzzy_dirlist = 0;
1223 struct file_struct *fuzzy_file = NULL;
1224 int fd = -1, f_copy = -1;
1225 stat_x sx, real_sx;
1226 STRUCT_STAT partial_st;
1227 struct file_struct *back_file = NULL;
1228 int statret, real_ret, stat_errno;
1229 char *fnamecmp, *partialptr, *backupptr = NULL;
1230 char fnamecmpbuf[MAXPATHLEN];
1231 uchar fnamecmp_type;
1232 int implied_dirs_are_missing = relative_paths && !implied_dirs && protocol_version < 30;
1233 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1234 int is_dir = !S_ISDIR(file->mode) ? 0
1235 : inc_recurse && ndx != cur_flist->ndx_start - 1 ? -1
1236 : 1;
1237
1238 if (verbose > 2)
1239 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
1240
1241 if (list_only) {
1242 if (is_dir < 0
1243 || (is_dir && !implied_dirs && file->flags & FLAG_IMPLIED_DIR))
1244 return;
1245 list_file_entry(file);
1246 return;
1247 }
1248
1249 if (server_filter_list.head) {
1250 int filtered = check_filter(&server_filter_list, fname, is_dir) < 0;
1251 if (is_dir < 0 && filtered)
1252 return;
1253 if (excluded_below >= 0) {
1254 if (F_DEPTH(file) > excluded_below
1255 && (!implied_dirs_are_missing || f_name_has_prefix(file, excluded_dir)))
1256 goto skipping;
1257 excluded_below = -1;
1258 }
1259 if (filtered) {
1260 if (is_dir) {
1261 excluded_below = F_DEPTH(file);
1262 excluded_dir = file;
1263 }
1264 skipping:
1265 rprintf(FERROR_XFER,
1266 "skipping daemon-excluded file \"%s\"\n",
1267 fname);
1268 return;
1269 }
1270 }
1271
1272 if (missing_below >= 0) {
1273 if (F_DEPTH(file) <= missing_below
1274 || (implied_dirs_are_missing && !f_name_has_prefix(file, missing_dir))) {
1275 if (dry_run)
1276 dry_run--;
1277 missing_below = -1;
1278 } else if (!dry_run) {
1279 if (is_dir)
1280 file->flags |= FLAG_MISSING_DIR;
1281 return;
1282 }
1283 }
1284#ifdef SUPPORT_ACLS
1285 sx.acc_acl = sx.def_acl = NULL;
1286#endif
1287#ifdef SUPPORT_XATTRS
1288 sx.xattr = NULL;
1289#endif
1290 if (dry_run > 1) {
1291 if (fuzzy_dirlist) {
1292 flist_free(fuzzy_dirlist);
1293 fuzzy_dirlist = NULL;
1294 }
1295 parent_dirname = "";
1296 statret = -1;
1297 stat_errno = ENOENT;
1298 } else {
1299 const char *dn = file->dirname ? file->dirname : ".";
1300 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1301 if (relative_paths && !implied_dirs
1302 && do_stat(dn, &sx.st) < 0
1303 && create_directory_path(fname) < 0) {
1304 rsyserr(FERROR_XFER, errno,
1305 "recv_generator: mkdir %s failed",
1306 full_fname(dn));
1307 }
1308 if (fuzzy_dirlist) {
1309 flist_free(fuzzy_dirlist);
1310 fuzzy_dirlist = NULL;
1311 }
1312 if (fuzzy_basis)
1313 need_fuzzy_dirlist = 1;
1314#ifdef SUPPORT_ACLS
1315 if (!preserve_perms)
1316 dflt_perms = default_perms_for_dir(dn);
1317#endif
1318 }
1319 parent_dirname = dn;
1320
1321 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
1322 strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1323 fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
1324 need_fuzzy_dirlist = 0;
1325 }
1326
1327 statret = link_stat(fname, &sx.st, keep_dirlinks && is_dir);
1328 stat_errno = errno;
1329 }
1330
1331 if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
1332 if (is_dir) {
1333 if (is_dir < 0)
1334 return;
1335 if (missing_below < 0) {
1336 if (dry_run)
1337 dry_run++;
1338 missing_below = F_DEPTH(file);
1339 missing_dir = file;
1340 }
1341 file->flags |= FLAG_MISSING_DIR;
1342 }
1343 if (verbose > 1) {
1344 rprintf(FINFO, "not creating new %s \"%s\"\n",
1345 is_dir ? "directory" : "file", fname);
1346 }
1347 return;
1348 }
1349
1350 if (statret == 0 && !(sx.st.st_mode & S_IWUSR)
1351 && !am_root && sx.st.st_uid == our_uid)
1352 del_opts |= DEL_NO_UID_WRITE;
1353
1354 if (is_dir) {
1355 if (!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
1356 goto cleanup;
1357 if (is_dir < 0) {
1358 /* In inc_recurse mode we want to make sure any missing
1359 * directories get created while we're still processing
1360 * the parent dir (which allows us to touch the parent
1361 * dir's mtime right away). We will handle the dir in
1362 * full later (right before we handle its contents). */
1363 if (statret == 0
1364 && (S_ISDIR(sx.st.st_mode)
1365 || delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0))
1366 goto cleanup; /* Any errors get reported later. */
1367 if (do_mkdir(fname, file->mode & 0700) == 0)
1368 file->flags |= FLAG_DIR_CREATED;
1369 goto cleanup;
1370 }
1371 /* The file to be received is a directory, so we need
1372 * to prepare appropriately. If there is already a
1373 * file of that name and it is *not* a directory, then
1374 * we need to delete it. If it doesn't exist, then
1375 * (perhaps recursively) create it. */
1376 if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1377 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0)
1378 goto skipping_dir_contents;
1379 statret = -1;
1380 }
1381 if (dry_run && statret != 0 && missing_below < 0) {
1382 missing_below = F_DEPTH(file);
1383 missing_dir = file;
1384 dry_run++;
1385 }
1386 real_ret = statret;
1387 real_sx = sx;
1388 if (file->flags & FLAG_DIR_CREATED)
1389 statret = -1;
1390 if (!preserve_perms) { /* See comment in non-dir code below. */
1391 file->mode = dest_mode(file->mode, sx.st.st_mode,
1392 dflt_perms, statret == 0);
1393 }
1394 if (statret != 0 && basis_dir[0] != NULL) {
1395 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1396 itemizing, code);
1397 if (j == -2) {
1398 itemizing = 0;
1399 code = FNONE;
1400 } else if (j >= 0)
1401 statret = 1;
1402 }
1403 if (itemizing && f_out != -1) {
1404 itemize(fname, file, ndx, statret, &sx,
1405 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1406 }
1407 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1408 if (!relative_paths || errno != ENOENT
1409 || create_directory_path(fname) < 0
1410 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
1411 rsyserr(FERROR_XFER, errno,
1412 "recv_generator: mkdir %s failed",
1413 full_fname(fname));
1414 skipping_dir_contents:
1415 rprintf(FERROR,
1416 "*** Skipping any contents from this failed directory ***\n");
1417 missing_below = F_DEPTH(file);
1418 missing_dir = file;
1419 file->flags |= FLAG_MISSING_DIR;
1420 goto cleanup;
1421 }
1422 }
1423 if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
1424 && verbose && code != FNONE && f_out != -1)
1425 rprintf(code, "%s/\n", fname);
1426
1427 /* We need to ensure that the dirs in the transfer have writable
1428 * permissions during the time we are putting files within them.
1429 * This is then fixed after the transfer is done. */
1430#ifdef HAVE_CHMOD
1431 if (!am_root && !(file->mode & S_IWUSR) && dir_tweaking) {
1432 mode_t mode = file->mode | S_IWUSR;
1433 if (do_chmod(fname, mode) < 0) {
1434 rsyserr(FERROR_XFER, errno,
1435 "failed to modify permissions on %s",
1436 full_fname(fname));
1437 }
1438 need_retouch_dir_perms = 1;
1439 }
1440#endif
1441
1442 if (real_ret != 0 && one_file_system)
1443 real_sx.st.st_dev = filesystem_dev;
1444 if (inc_recurse) {
1445 if (one_file_system) {
1446 uint32 *devp = F_DIR_DEV_P(file);
1447 DEV_MAJOR(devp) = major(real_sx.st.st_dev);
1448 DEV_MINOR(devp) = minor(real_sx.st.st_dev);
1449 }
1450 }
1451 else if (delete_during && f_out != -1 && !phase && dry_run < 2
1452 && (file->flags & FLAG_CONTENT_DIR))
1453 delete_in_dir(fname, file, &real_sx.st.st_dev);
1454 goto cleanup;
1455 }
1456
1457 /* If we're not preserving permissions, change the file-list's
1458 * mode based on the local permissions and some heuristics. */
1459 if (!preserve_perms) {
1460 int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
1461 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
1462 exists);
1463 }
1464
1465#ifdef SUPPORT_HARD_LINKS
1466 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1467 && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
1468 goto cleanup;
1469#endif
1470
1471 if (preserve_links && S_ISLNK(file->mode)) {
1472#ifdef SUPPORT_LINKS
1473 const char *sl = F_SYMLINK(file);
1474 if (safe_symlinks && unsafe_symlink(sl, fname)) {
1475 if (verbose) {
1476 if (solo_file)
1477 fname = f_name(file, NULL);
1478 rprintf(FINFO,
1479 "ignoring unsafe symlink %s -> \"%s\"\n",
1480 full_fname(fname), sl);
1481 }
1482 return;
1483 }
1484 if (statret == 0) {
1485 char lnk[MAXPATHLEN];
1486 int len;
1487
1488 if (!S_ISLNK(sx.st.st_mode))
1489 statret = -1;
1490 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1491 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1492 /* The link is pointing to the right place. */
1493 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1494 if (itemizing)
1495 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1496#if defined SUPPORT_HARD_LINKS && defined CAN_HARDLINK_SYMLINK
1497 if (preserve_hard_links && F_IS_HLINKED(file))
1498 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1499#endif
1500 if (remove_source_files == 1)
1501 goto return_with_success;
1502 goto cleanup;
1503 }
1504 /* Not the right symlink (or not a symlink), so
1505 * delete it. */
1506 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_SYMLINK) != 0)
1507 goto cleanup;
1508 } else if (basis_dir[0] != NULL) {
1509 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1510 itemizing, code);
1511 if (j == -2) {
1512#ifndef CAN_HARDLINK_SYMLINK
1513 if (link_dest) {
1514 /* Resort to --copy-dest behavior. */
1515 } else
1516#endif
1517 if (!copy_dest)
1518 goto cleanup;
1519 itemizing = 0;
1520 code = FNONE;
1521 } else if (j >= 0)
1522 statret = 1;
1523 }
1524#ifdef SUPPORT_HARD_LINKS
1525 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1526 cur_flist->in_progress++;
1527 goto cleanup;
1528 }
1529#endif
1530 if (do_symlink(sl, fname) != 0) {
1531 rsyserr(FERROR_XFER, errno, "symlink %s -> \"%s\" failed",
1532 full_fname(fname), sl);
1533 } else {
1534 set_file_attrs(fname, file, NULL, NULL, 0);
1535 if (itemizing) {
1536 itemize(fname, file, ndx, statret, &sx,
1537 ITEM_LOCAL_CHANGE, 0, NULL);
1538 }
1539 if (code != FNONE && verbose)
1540 rprintf(code, "%s -> %s\n", fname, sl);
1541#ifdef SUPPORT_HARD_LINKS
1542 if (preserve_hard_links && F_IS_HLINKED(file))
1543 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1544#endif
1545 /* This does not check remove_source_files == 1
1546 * because this is one of the items that the old
1547 * --remove-sent-files option would remove. */
1548 if (remove_source_files)
1549 goto return_with_success;
1550 }
1551#endif
1552 goto cleanup;
1553 }
1554
1555 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1556 || (preserve_specials && IS_SPECIAL(file->mode))) {
1557 uint32 *devp = F_RDEV_P(file);
1558 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1559 if (statret == 0) {
1560 int del_for_flag;
1561 if (IS_DEVICE(file->mode)) {
1562 if (!IS_DEVICE(sx.st.st_mode))
1563 statret = -1;
1564 del_for_flag = DEL_FOR_DEVICE;
1565 } else {
1566 if (!IS_SPECIAL(sx.st.st_mode))
1567 statret = -1;
1568 del_for_flag = DEL_FOR_SPECIAL;
1569 }
1570 if (statret == 0
1571 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
1572 && sx.st.st_rdev == rdev) {
1573 /* The device or special file is identical. */
1574 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1575 if (itemizing)
1576 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1577#ifdef SUPPORT_HARD_LINKS
1578 if (preserve_hard_links && F_IS_HLINKED(file))
1579 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1580#endif
1581 if (remove_source_files == 1)
1582 goto return_with_success;
1583 goto cleanup;
1584 }
1585 if (delete_item(fname, sx.st.st_mode, del_opts | del_for_flag) != 0)
1586 goto cleanup;
1587 } else if (basis_dir[0] != NULL) {
1588 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1589 itemizing, code);
1590 if (j == -2) {
1591#ifndef CAN_HARDLINK_SPECIAL
1592 if (link_dest) {
1593 /* Resort to --copy-dest behavior. */
1594 } else
1595#endif
1596 if (!copy_dest)
1597 goto cleanup;
1598 itemizing = 0;
1599 code = FNONE;
1600 } else if (j >= 0)
1601 statret = 1;
1602 }
1603#ifdef SUPPORT_HARD_LINKS
1604 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1605 cur_flist->in_progress++;
1606 goto cleanup;
1607 }
1608#endif
1609 if (verbose > 2) {
1610 rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1611 fname, (int)file->mode,
1612 (long)major(rdev), (long)minor(rdev));
1613 }
1614 if (do_mknod(fname, file->mode, rdev) < 0) {
1615 rsyserr(FERROR_XFER, errno, "mknod %s failed",
1616 full_fname(fname));
1617 } else {
1618 set_file_attrs(fname, file, NULL, NULL, 0);
1619 if (itemizing) {
1620 itemize(fname, file, ndx, statret, &sx,
1621 ITEM_LOCAL_CHANGE, 0, NULL);
1622 }
1623 if (code != FNONE && verbose)
1624 rprintf(code, "%s\n", fname);
1625#ifdef SUPPORT_HARD_LINKS
1626 if (preserve_hard_links && F_IS_HLINKED(file))
1627 finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
1628#endif
1629 if (remove_source_files == 1)
1630 goto return_with_success;
1631 }
1632 goto cleanup;
1633 }
1634
1635 if (!S_ISREG(file->mode)) {
1636 if (solo_file)
1637 fname = f_name(file, NULL);
1638 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1639 goto cleanup;
1640 }
1641
1642 if (max_size > 0 && F_LENGTH(file) > max_size) {
1643 if (verbose > 1) {
1644 if (solo_file)
1645 fname = f_name(file, NULL);
1646 rprintf(FINFO, "%s is over max-size\n", fname);
1647 }
1648 goto cleanup;
1649 }
1650 if (min_size > 0 && F_LENGTH(file) < min_size) {
1651 if (verbose > 1) {
1652 if (solo_file)
1653 fname = f_name(file, NULL);
1654 rprintf(FINFO, "%s is under min-size\n", fname);
1655 }
1656 goto cleanup;
1657 }
1658
1659 if (ignore_existing > 0 && statret == 0) {
1660 if (verbose > 1)
1661 rprintf(FINFO, "%s exists\n", fname);
1662 goto cleanup;
1663 }
1664
1665 if (update_only > 0 && statret == 0
1666 && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
1667 if (verbose > 1)
1668 rprintf(FINFO, "%s is newer\n", fname);
1669 goto cleanup;
1670 }
1671
1672 fnamecmp = fname;
1673 fnamecmp_type = FNAMECMP_FNAME;
1674
1675 if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
1676 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE) != 0)
1677 goto cleanup;
1678 statret = -1;
1679 stat_errno = ENOENT;
1680 }
1681
1682 if (statret != 0 && basis_dir[0] != NULL) {
1683 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
1684 itemizing, code);
1685 if (j == -2) {
1686 if (remove_source_files == 1)
1687 goto return_with_success;
1688 goto cleanup;
1689 }
1690 if (j >= 0) {
1691 fnamecmp = fnamecmpbuf;
1692 fnamecmp_type = j;
1693 statret = 0;
1694 }
1695 }
1696
1697 real_ret = statret;
1698 real_sx = sx;
1699
1700 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1701 && link_stat(partialptr, &partial_st, 0) == 0
1702 && S_ISREG(partial_st.st_mode)) {
1703 if (statret != 0)
1704 goto prepare_to_open;
1705 } else
1706 partialptr = NULL;
1707
1708 if (statret != 0 && fuzzy_dirlist && dry_run <= 1) {
1709 int j = find_fuzzy(file, fuzzy_dirlist);
1710 if (j >= 0) {
1711 fuzzy_file = fuzzy_dirlist->files[j];
1712 f_name(fuzzy_file, fnamecmpbuf);
1713 if (verbose > 2) {
1714 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1715 fname, fnamecmpbuf);
1716 }
1717 sx.st.st_size = F_LENGTH(fuzzy_file);
1718 statret = 0;
1719 fnamecmp = fnamecmpbuf;
1720 fnamecmp_type = FNAMECMP_FUZZY;
1721 }
1722 }
1723
1724 if (statret != 0) {
1725#ifdef SUPPORT_HARD_LINKS
1726 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1727 cur_flist->in_progress++;
1728 goto cleanup;
1729 }
1730#endif
1731 if (stat_errno == ENOENT)
1732 goto notify_others;
1733 rsyserr(FERROR_XFER, stat_errno, "recv_generator: failed to stat %s",
1734 full_fname(fname));
1735 goto cleanup;
1736 }
1737
1738 if (append_mode > 0 && sx.st.st_size >= F_LENGTH(file))
1739 goto cleanup;
1740
1741 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1742 ;
1743 else if (fnamecmp_type == FNAMECMP_FUZZY)
1744 ;
1745 else if (unchanged_file(fnamecmp, file, &sx.st)) {
1746 if (partialptr) {
1747 do_unlink(partialptr);
1748 handle_partial_dir(partialptr, PDIR_DELETE);
1749 }
1750 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
1751 if (itemizing)
1752 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
1753#ifdef SUPPORT_HARD_LINKS
1754 if (preserve_hard_links && F_IS_HLINKED(file))
1755 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1756#endif
1757 if (remove_source_files != 1)
1758 goto cleanup;
1759 return_with_success:
1760 if (!dry_run)
1761 send_msg_int(MSG_SUCCESS, ndx);
1762 goto cleanup;
1763 }
1764
1765 prepare_to_open:
1766 if (partialptr) {
1767 sx.st = partial_st;
1768 fnamecmp = partialptr;
1769 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1770 statret = 0;
1771 }
1772
1773 if (!do_xfers)
1774 goto notify_others;
1775
1776 if (read_batch || whole_file) {
1777 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1778 if (!(backupptr = get_backup_name(fname)))
1779 goto cleanup;
1780 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
1781 goto pretend_missing;
1782 if (copy_file(fname, backupptr, -1, back_file->mode, 1) < 0) {
1783 unmake_file(back_file);
1784 back_file = NULL;
1785 goto cleanup;
1786 }
1787 }
1788 goto notify_others;
1789 }
1790
1791 if (fuzzy_dirlist) {
1792 int j = flist_find(fuzzy_dirlist, file);
1793 if (j >= 0) /* don't use changing file as future fuzzy basis */
1794 fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
1795 }
1796
1797 /* open the file */
1798 if ((fd = do_open(fnamecmp, O_RDONLY, 0)) < 0) {
1799 rsyserr(FERROR, errno, "failed to open %s, continuing",
1800 full_fname(fnamecmp));
1801 pretend_missing:
1802 /* pretend the file didn't exist */
1803#ifdef SUPPORT_HARD_LINKS
1804 if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
1805 cur_flist->in_progress++;
1806 goto cleanup;
1807 }
1808#endif
1809 statret = real_ret = -1;
1810 goto notify_others;
1811 }
1812
1813 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1814 if (!(backupptr = get_backup_name(fname))) {
1815 close(fd);
1816 goto cleanup;
1817 }
1818 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1819 close(fd);
1820 goto pretend_missing;
1821 }
1822 if (robust_unlink(backupptr) && errno != ENOENT) {
1823 rsyserr(FERROR_XFER, errno, "unlink %s",
1824 full_fname(backupptr));
1825 unmake_file(back_file);
1826 back_file = NULL;
1827 close(fd);
1828 goto cleanup;
1829 }
1830 if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0
1831 && (errno != ENOENT || make_bak_dir(backupptr) < 0
1832 || (f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0)) {
1833 rsyserr(FERROR_XFER, errno, "open %s",
1834 full_fname(backupptr));
1835 unmake_file(back_file);
1836 back_file = NULL;
1837 close(fd);
1838 goto cleanup;
1839 }
1840 fnamecmp_type = FNAMECMP_BACKUP;
1841 }
1842
1843 if (verbose > 3) {
1844 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1845 fnamecmp, (double)sx.st.st_size);
1846 }
1847
1848 if (verbose > 2)
1849 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1850
1851 notify_others:
1852 if (remove_source_files && !delay_updates && !phase)
1853 increment_active_files(ndx, itemizing, code);
1854 if (inc_recurse && !dry_run)
1855 cur_flist->in_progress++;
1856#ifdef SUPPORT_HARD_LINKS
1857 if (preserve_hard_links && F_IS_HLINKED(file))
1858 file->flags |= FLAG_FILE_SENT;
1859#endif
1860 write_ndx(f_out, ndx);
1861 if (itemizing) {
1862 int iflags = ITEM_TRANSFER;
1863 if (always_checksum > 0)
1864 iflags |= ITEM_REPORT_CHECKSUM;
1865 if (fnamecmp_type != FNAMECMP_FNAME)
1866 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1867 if (fnamecmp_type == FNAMECMP_FUZZY)
1868 iflags |= ITEM_XNAME_FOLLOWS;
1869 itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
1870 fuzzy_file ? fuzzy_file->basename : NULL);
1871#ifdef SUPPORT_ACLS
1872 if (preserve_acls)
1873 free_acl(&real_sx);
1874#endif
1875#ifdef SUPPORT_XATTRS
1876 if (preserve_xattrs)
1877 free_xattr(&real_sx);
1878#endif
1879 }
1880
1881 if (!do_xfers) {
1882#ifdef SUPPORT_HARD_LINKS
1883 if (preserve_hard_links && F_IS_HLINKED(file))
1884 finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
1885#endif
1886 goto cleanup;
1887 }
1888 if (read_batch)
1889 goto cleanup;
1890
1891 if (statret != 0 || whole_file)
1892 write_sum_head(f_out, NULL);
1893 else {
1894 generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy);
1895 close(fd);
1896 }
1897
1898 cleanup:
1899 if (back_file) {
1900 if (f_copy >= 0)
1901 close(f_copy);
1902 set_file_attrs(backupptr, back_file, NULL, NULL, 0);
1903 if (verbose > 1) {
1904 rprintf(FINFO, "backed up %s to %s\n",
1905 fname, backupptr);
1906 }
1907 unmake_file(back_file);
1908 }
1909
1910#ifdef SUPPORT_ACLS
1911 if (preserve_acls)
1912 free_acl(&sx);
1913#endif
1914#ifdef SUPPORT_XATTRS
1915 if (preserve_xattrs)
1916 free_xattr(&sx);
1917#endif
1918 return;
1919}
1920
1921static void touch_up_dirs(struct file_list *flist, int ndx)
1922{
1923 static int counter = 0;
1924 struct file_struct *file;
1925 char *fname;
1926 int i, start, end;
1927
1928 if (ndx < 0) {
1929 start = 0;
1930 end = flist->used - 1;
1931 } else
1932 start = end = ndx;
1933
1934 /* Fix any directory permissions that were modified during the
1935 * transfer and/or re-set any tweaked modified-time values. */
1936 for (i = start; i <= end; i++, counter++) {
1937 file = flist->files[i];
1938 if (!S_ISDIR(file->mode)
1939 || (!implied_dirs && file->flags & FLAG_IMPLIED_DIR))
1940 continue;
1941 if (verbose > 3) {
1942 fname = f_name(file, NULL);
1943 rprintf(FINFO, "touch_up_dirs: %s (%d)\n",
1944 NS(fname), i);
1945 }
1946 if (!F_IS_ACTIVE(file) || file->flags & FLAG_MISSING_DIR
1947 || (!need_retouch_dir_times && file->mode & S_IWUSR))
1948 continue;
1949 fname = f_name(file, NULL);
1950 if (!(file->mode & S_IWUSR))
1951 do_chmod(fname, file->mode);
1952 if (need_retouch_dir_times)
1953 set_modtime(fname, file->modtime, file->mode);
1954 if (allowed_lull && !(counter % lull_mod))
1955 maybe_send_keepalive();
1956 else if (!(counter & 0xFF))
1957 maybe_flush_socket(0);
1958 }
1959}
1960
1961void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
1962{
1963 struct file_struct *file;
1964 struct file_list *flist;
1965 char fbuf[MAXPATHLEN];
1966 int ndx;
1967
1968 while (1) {
1969#ifdef SUPPORT_HARD_LINKS
1970 if (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
1971 flist = flist_for_ndx(ndx);
1972 assert(flist != NULL);
1973 file = flist->files[ndx - flist->ndx_start];
1974 assert(file->flags & FLAG_HLINKED);
1975 finish_hard_link(file, f_name(file, fbuf), ndx, NULL, itemizing, code, -1);
1976 flist->in_progress--;
1977 continue;
1978 }
1979#endif
1980
1981 if (check_redo && (ndx = get_redo_num()) != -1) {
1982 csum_length = SUM_LENGTH;
1983 max_size = -max_size;
1984 min_size = -min_size;
1985 ignore_existing = -ignore_existing;
1986 ignore_non_existing = -ignore_non_existing;
1987 update_only = -update_only;
1988 always_checksum = -always_checksum;
1989 size_only = -size_only;
1990 append_mode = -append_mode;
1991 make_backups = -make_backups; /* avoid dup backup w/inplace */
1992 ignore_times++;
1993
1994 flist = cur_flist;
1995 cur_flist = flist_for_ndx(ndx);
1996
1997 file = cur_flist->files[ndx - cur_flist->ndx_start];
1998 if (solo_file)
1999 strlcpy(fbuf, solo_file, sizeof fbuf);
2000 else
2001 f_name(file, fbuf);
2002 recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
2003 cur_flist->to_redo--;
2004
2005 cur_flist = flist;
2006
2007 csum_length = SHORT_SUM_LENGTH;
2008 max_size = -max_size;
2009 min_size = -min_size;
2010 ignore_existing = -ignore_existing;
2011 ignore_non_existing = -ignore_non_existing;
2012 update_only = -update_only;
2013 always_checksum = -always_checksum;
2014 size_only = -size_only;
2015 append_mode = -append_mode;
2016 make_backups = -make_backups;
2017 ignore_times--;
2018 continue;
2019 }
2020
2021 if (cur_flist == first_flist)
2022 break;
2023
2024 /* We only get here if inc_recurse is enabled. */
2025 if (first_flist->in_progress || first_flist->to_redo)
2026 break;
2027
2028 if (!read_batch) {
2029 write_ndx(sock_f_out, NDX_DONE);
2030 maybe_flush_socket(1);
2031 }
2032
2033 if (delete_during == 2 || !dir_tweaking) {
2034 /* Skip directory touch-up. */
2035 } else if (first_flist->parent_ndx >= 0)
2036 touch_up_dirs(dir_flist, first_flist->parent_ndx);
2037
2038 flist_free(first_flist); /* updates first_flist */
2039 }
2040}
2041
2042void generate_files(int f_out, const char *local_name)
2043{
2044 int i, ndx;
2045 char fbuf[MAXPATHLEN];
2046 int itemizing;
2047 enum logcode code;
2048 int save_do_progress = do_progress;
2049
2050 if (protocol_version >= 29) {
2051 itemizing = 1;
2052 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
2053 code = logfile_format_has_i ? FNONE : FLOG;
2054 } else if (am_daemon) {
2055 itemizing = logfile_format_has_i && do_xfers;
2056 maybe_ATTRS_REPORT = ATTRS_REPORT;
2057 code = itemizing || !do_xfers ? FCLIENT : FINFO;
2058 } else if (!am_server) {
2059 itemizing = stdout_format_has_i;
2060 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
2061 code = itemizing ? FNONE : FINFO;
2062 } else {
2063 itemizing = 0;
2064 maybe_ATTRS_REPORT = ATTRS_REPORT;
2065 code = FINFO;
2066 }
2067 solo_file = local_name;
2068 dir_tweaking = !(list_only || solo_file || dry_run);
2069 need_retouch_dir_times = preserve_times > 1;
2070 lull_mod = allowed_lull * 5;
2071
2072 if (verbose > 2)
2073 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
2074
2075 if (delete_before && !solo_file && cur_flist->used > 0)
2076 do_delete_pass();
2077 if (delete_during == 2) {
2078 deldelay_size = BIGPATHBUFLEN * 4;
2079 deldelay_buf = new_array(char, deldelay_size);
2080 if (!deldelay_buf)
2081 out_of_memory("delete-delay");
2082 }
2083 do_progress = 0;
2084
2085 if (append_mode > 0 || whole_file < 0)
2086 whole_file = 0;
2087 if (verbose >= 2) {
2088 rprintf(FINFO, "delta-transmission %s\n",
2089 whole_file
2090 ? "disabled for local transfer or --whole-file"
2091 : "enabled");
2092 }
2093
2094 /* Since we often fill up the outgoing socket and then just sit around
2095 * waiting for the other 2 processes to do their thing, we don't want
2096 * to exit on a timeout. If the data stops flowing, the receiver will
2097 * notice that and let us know via the redo pipe (or its closing). */
2098 ignore_timeout = 1;
2099
2100 dflt_perms = (ACCESSPERMS & ~orig_umask);
2101
2102 do {
2103#ifdef SUPPORT_HARD_LINKS
2104 if (preserve_hard_links && inc_recurse) {
2105 while (!flist_eof && file_total < FILECNT_LOOKAHEAD/2)
2106 wait_for_receiver();
2107 }
2108#endif
2109
2110 if (inc_recurse && cur_flist->parent_ndx >= 0) {
2111 struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
2112 f_name(fp, fbuf);
2113 ndx = cur_flist->ndx_start - 1;
2114 recv_generator(fbuf, fp, ndx, itemizing, code, f_out);
2115 if (delete_during && dry_run < 2 && !list_only) {
2116 if (BITS_SETnUNSET(fp->flags, FLAG_CONTENT_DIR, FLAG_MISSING_DIR)) {
2117 dev_t dirdev;
2118 if (one_file_system) {
2119 uint32 *devp = F_DIR_DEV_P(fp);
2120 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
2121 } else
2122 dirdev = MAKEDEV(0, 0);
2123 delete_in_dir(f_name(fp, fbuf), fp, &dirdev);
2124 }
2125 }
2126 }
2127 for (i = cur_flist->low; i <= cur_flist->high; i++) {
2128 struct file_struct *file = cur_flist->sorted[i];
2129
2130 if (!F_IS_ACTIVE(file))
2131 continue;
2132
2133 if (unsort_ndx)
2134 ndx = F_NDX(file);
2135 else
2136 ndx = i + cur_flist->ndx_start;
2137
2138 if (solo_file)
2139 strlcpy(fbuf, solo_file, sizeof fbuf);
2140 else
2141 f_name(file, fbuf);
2142 recv_generator(fbuf, file, ndx, itemizing, code, f_out);
2143
2144 check_for_finished_files(itemizing, code, 0);
2145
2146 if (allowed_lull && !(i % lull_mod))
2147 maybe_send_keepalive();
2148 else if (!(i & 0xFF))
2149 maybe_flush_socket(0);
2150 }
2151
2152 if (!inc_recurse) {
2153 write_ndx(f_out, NDX_DONE);
2154 break;
2155 }
2156
2157 while (1) {
2158 check_for_finished_files(itemizing, code, 1);
2159 if (cur_flist->next || flist_eof)
2160 break;
2161 wait_for_receiver();
2162 }
2163 } while ((cur_flist = cur_flist->next) != NULL);
2164
2165 if (delete_during)
2166 delete_in_dir(NULL, NULL, &dev_zero);
2167 phase++;
2168 if (verbose > 2)
2169 rprintf(FINFO, "generate_files phase=%d\n", phase);
2170
2171 while (1) {
2172 check_for_finished_files(itemizing, code, 1);
2173 if (msgdone_cnt)
2174 break;
2175 wait_for_receiver();
2176 }
2177
2178 phase++;
2179 if (verbose > 2)
2180 rprintf(FINFO, "generate_files phase=%d\n", phase);
2181
2182 write_ndx(f_out, NDX_DONE);
2183 /* Reduce round-trip lag-time for a useless delay-updates phase. */
2184 if (protocol_version >= 29 && !delay_updates)
2185 write_ndx(f_out, NDX_DONE);
2186
2187 /* Read MSG_DONE for the redo phase (and any prior messages). */
2188 while (1) {
2189 check_for_finished_files(itemizing, code, 0);
2190 if (msgdone_cnt > 1)
2191 break;
2192 wait_for_receiver();
2193 }
2194
2195 if (protocol_version >= 29) {
2196 phase++;
2197 if (verbose > 2)
2198 rprintf(FINFO, "generate_files phase=%d\n", phase);
2199 if (delay_updates)
2200 write_ndx(f_out, NDX_DONE);
2201 /* Read MSG_DONE for delay-updates phase & prior messages. */
2202 while (msgdone_cnt == 2)
2203 wait_for_receiver();
2204 }
2205
2206 do_progress = save_do_progress;
2207 if (delete_during == 2)
2208 do_delayed_deletions(fbuf);
2209 if (delete_after && !solo_file && file_total > 0)
2210 do_delete_pass();
2211
2212 if ((need_retouch_dir_perms || need_retouch_dir_times)
2213 && dir_tweaking && (!inc_recurse || delete_during == 2))
2214 touch_up_dirs(dir_flist, -1);
2215
2216 if (max_delete >= 0 && deletion_count > max_delete) {
2217 rprintf(FINFO,
2218 "Deletions stopped due to --max-delete limit (%d skipped)\n",
2219 deletion_count - max_delete);
2220 io_error |= IOERR_DEL_LIMIT;
2221 }
2222
2223 if (verbose > 2)
2224 rprintf(FINFO, "generate_files finished\n");
2225}