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