2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4 Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Generate and receive file lists
24 * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html
37 extern int do_progress;
38 extern int always_checksum;
40 extern int ignore_errors;
41 extern int numeric_ids;
44 extern int filesfrom_fd;
45 extern int one_file_system;
46 extern int keep_dirlinks;
47 extern int preserve_links;
48 extern int preserve_hard_links;
49 extern int preserve_perms;
50 extern int preserve_devices;
51 extern int preserve_specials;
52 extern int preserve_uid;
53 extern int preserve_gid;
54 extern int relative_paths;
55 extern int implied_dirs;
56 extern int prune_empty_dirs;
57 extern int copy_links;
58 extern int copy_unsafe_links;
59 extern int protocol_version;
60 extern int sanitize_paths;
61 extern int orig_umask;
62 extern struct stats stats;
63 extern struct file_list *the_file_list;
65 extern char curr_dir[MAXPATHLEN];
67 extern struct chmod_mode_struct *chmod_modes;
69 extern struct filter_list_struct filter_list;
70 extern struct filter_list_struct server_filter_list;
74 dev_t filesystem_dev; /* used to implement -x */
75 unsigned int file_struct_len;
77 static char empty_sum[MD4_SUM_LENGTH];
78 static int flist_count_offset;
79 static int max_dir_depth = 0;
81 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
82 static void output_flist(struct file_list *flist);
88 /* Figure out how big the file_struct is without trailing padding */
89 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
90 checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
93 static int show_filelist_p(void)
95 return verbose && xfer_dirs && !am_server;
98 static void start_filelist_progress(char *kind)
100 rprintf(FINFO, "%s ... ", kind);
101 if (verbose > 1 || do_progress)
102 rprintf(FINFO, "\n");
106 static void emit_filelist_progress(int count)
108 rprintf(FINFO, " %d files...\r", count);
111 static void maybe_emit_filelist_progress(int count)
113 if (do_progress && show_filelist_p() && (count % 100) == 0)
114 emit_filelist_progress(count);
117 static void finish_filelist_progress(const struct file_list *flist)
120 /* This overwrites the progress line */
121 rprintf(FINFO, "%d file%sto consider\n",
122 flist->count, flist->count == 1 ? " " : "s ");
124 rprintf(FINFO, "done\n");
127 void show_flist_stats(void)
132 static void list_file_entry(struct file_struct *f)
137 /* this can happen if duplicate names were removed */
141 permstring(perms, f->mode);
144 if (preserve_links && S_ISLNK(f->mode)) {
145 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
147 (double)f->length, timestring(f->modtime),
148 f_name(f, NULL), f->u.link);
152 rprintf(FINFO, "%s %11.0f %s %s\n",
154 (double)f->length, timestring(f->modtime),
160 * Stat either a symlink or its referent, depending on the settings of
161 * copy_links, copy_unsafe_links, etc.
163 * @retval -1 on error
165 * @retval 0 for success
167 * @post If @p path is a symlink, then @p linkbuf (of size @c
168 * MAXPATHLEN) contains the symlink target.
170 * @post @p buffer contains information about the link or the
171 * referrent as appropriate, if they exist.
173 static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
177 return do_stat(path, buffer);
178 if (link_stat(path, buffer, 0) < 0)
180 if (S_ISLNK(buffer->st_mode)) {
181 int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1);
185 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
187 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
190 return do_stat(path, buffer);
195 return do_stat(path, buffer);
199 int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks)
203 return do_stat(path, buffer);
204 if (do_lstat(path, buffer) < 0)
206 if (follow_dirlinks && S_ISLNK(buffer->st_mode)) {
208 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
213 return do_stat(path, buffer);
217 /* This function is used to check if a file should be included/excluded
218 * from the list of files based on its name and type etc. The value of
219 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
220 static int is_excluded(char *fname, int is_dir, int filter_level)
222 #if 0 /* This currently never happens, so avoid a useless compare. */
223 if (filter_level == NO_FILTERS)
227 /* never exclude '.', even if somebody does --exclude '*' */
228 if (fname[0] == '.' && !fname[1])
230 /* Handle the -R version of the '.' dir. */
231 if (fname[0] == '/') {
232 int len = strlen(fname);
233 if (fname[len-1] == '.' && fname[len-2] == '/')
237 if (server_filter_list.head
238 && check_filter(&server_filter_list, fname, is_dir) < 0)
240 if (filter_level != ALL_FILTERS)
243 && check_filter(&filter_list, fname, is_dir) < 0)
248 static int to_wire_mode(mode_t mode)
251 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
252 return (mode & ~(_S_IFMT)) | 0120000;
257 static mode_t from_wire_mode(int mode)
259 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
260 return (mode & ~(_S_IFMT)) | _S_IFLNK;
264 static void send_directory(int f, struct file_list *flist,
265 char *fbuf, int len);
267 static char *flist_dir;
268 static int flist_dir_len;
272 * Make sure @p flist is big enough to hold at least @p flist->count
275 void flist_expand(struct file_list *flist)
277 struct file_struct **new_ptr;
279 if (flist->count < flist->malloced)
282 if (flist->malloced < FLIST_START)
283 flist->malloced = FLIST_START;
284 else if (flist->malloced >= FLIST_LINEAR)
285 flist->malloced += FLIST_LINEAR;
287 flist->malloced *= 2;
290 * In case count jumped or we are starting the list
291 * with a known size just set it.
293 if (flist->malloced < flist->count)
294 flist->malloced = flist->count;
296 new_ptr = realloc_array(flist->files, struct file_struct *,
299 if (verbose >= 2 && flist->malloced != FLIST_START) {
300 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
302 (double)sizeof flist->files[0] * flist->malloced,
303 (new_ptr == flist->files) ? " not" : "");
306 flist->files = new_ptr;
309 out_of_memory("flist_expand");
312 static void send_file_entry(struct file_struct *file, int f)
314 unsigned short flags;
315 static time_t modtime;
319 static uint32 rdev_major;
322 static char lastname[MAXPATHLEN];
323 char fname[MAXPATHLEN];
331 modtime = 0, mode = 0;
332 dev = 0, rdev = makedev(0, 0);
339 io_write_phase = "send_file_entry";
343 flags = file->flags & XMIT_TOP_DIR;
345 if (file->mode == mode)
346 flags |= XMIT_SAME_MODE;
349 if ((preserve_devices && IS_DEVICE(mode))
350 || (preserve_specials && IS_SPECIAL(mode))) {
351 if (protocol_version < 28) {
352 if (file->u.rdev == rdev)
353 flags |= XMIT_SAME_RDEV_pre28;
358 if ((uint32)major(rdev) == rdev_major)
359 flags |= XMIT_SAME_RDEV_MAJOR;
361 rdev_major = major(rdev);
362 if ((uint32)minor(rdev) <= 0xFFu)
363 flags |= XMIT_RDEV_MINOR_IS_SMALL;
365 } else if (protocol_version < 28)
366 rdev = makedev(0, 0);
367 if (file->uid == uid)
368 flags |= XMIT_SAME_UID;
371 if (file->gid == gid)
372 flags |= XMIT_SAME_GID;
375 if (file->modtime == modtime)
376 flags |= XMIT_SAME_TIME;
378 modtime = file->modtime;
380 #ifdef SUPPORT_HARD_LINKS
381 if (file->link_u.idev) {
382 if (file->F_DEV == dev) {
383 if (protocol_version >= 28)
384 flags |= XMIT_SAME_DEV;
387 flags |= XMIT_HAS_IDEV_DATA;
392 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
394 l2 = strlen(fname+l1);
397 flags |= XMIT_SAME_NAME;
399 flags |= XMIT_LONG_NAME;
401 /* We must make sure we don't send a zero flag byte or the
402 * other end will terminate the flist transfer. Note that
403 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
404 * it's harmless way to add a bit to the first flag byte. */
405 if (protocol_version >= 28) {
406 if (!flags && !S_ISDIR(mode))
407 flags |= XMIT_TOP_DIR;
408 if ((flags & 0xFF00) || !flags) {
409 flags |= XMIT_EXTENDED_FLAGS;
410 write_byte(f, flags);
411 write_byte(f, flags >> 8);
413 write_byte(f, flags);
416 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
417 write_byte(f, flags);
419 if (flags & XMIT_SAME_NAME)
421 if (flags & XMIT_LONG_NAME)
425 write_buf(f, fname + l1, l2);
427 write_longint(f, file->length);
428 if (!(flags & XMIT_SAME_TIME))
429 write_int(f, modtime);
430 if (!(flags & XMIT_SAME_MODE))
431 write_int(f, to_wire_mode(mode));
432 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
437 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
442 if ((preserve_devices && IS_DEVICE(mode))
443 || (preserve_specials && IS_SPECIAL(mode))) {
444 if (protocol_version < 28) {
445 if (!(flags & XMIT_SAME_RDEV_pre28))
446 write_int(f, (int)rdev);
448 if (!(flags & XMIT_SAME_RDEV_MAJOR))
449 write_int(f, major(rdev));
450 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
451 write_byte(f, minor(rdev));
453 write_int(f, minor(rdev));
458 if (preserve_links && S_ISLNK(mode)) {
459 int len = strlen(file->u.link);
461 write_buf(f, file->u.link, len);
465 #ifdef SUPPORT_HARD_LINKS
466 if (flags & XMIT_HAS_IDEV_DATA) {
467 if (protocol_version < 26) {
468 /* 32-bit dev_t and ino_t */
470 write_int(f, file->F_INODE);
472 /* 64-bit dev_t and ino_t */
473 if (!(flags & XMIT_SAME_DEV))
474 write_longint(f, dev);
475 write_longint(f, file->F_INODE);
480 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
485 /* Prior to 28, we sent a useless set of nulls. */
488 write_buf(f, sum, checksum_len);
491 strlcpy(lastname, fname, MAXPATHLEN);
493 io_write_phase = "unknown";
496 static struct file_struct *receive_file_entry(struct file_list *flist,
497 unsigned short flags, int f)
499 static time_t modtime;
503 static uint32 rdev_major;
506 static char lastname[MAXPATHLEN], *lastdir;
507 static int lastdir_depth, lastdir_len = -1;
508 static unsigned int del_hier_name_len = 0;
509 static int in_del_hier = 0;
510 char thisname[MAXPATHLEN];
511 unsigned int l1 = 0, l2 = 0;
512 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
514 char *basename, *dirname, *bp;
515 struct file_struct *file;
518 modtime = 0, mode = 0;
519 dev = 0, rdev = makedev(0, 0);
528 if (flags & XMIT_SAME_NAME)
531 if (flags & XMIT_LONG_NAME)
536 if (l2 >= MAXPATHLEN - l1) {
538 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
539 flags, l1, l2, lastname);
540 overflow_exit("receive_file_entry");
543 strlcpy(thisname, lastname, l1 + 1);
544 read_sbuf(f, &thisname[l1], l2);
545 thisname[l1 + l2] = 0;
547 strlcpy(lastname, thisname, MAXPATHLEN);
549 clean_fname(thisname, 0);
552 sanitize_path(thisname, thisname, "", 0);
554 if ((basename = strrchr(thisname, '/')) != NULL) {
555 dirname_len = ++basename - thisname; /* counts future '\0' */
556 if (lastdir_len == dirname_len - 1
557 && strncmp(thisname, lastdir, lastdir_len) == 0) {
559 dirname_len = 0; /* indicates no copy is needed */
567 basename_len = strlen(basename) + 1; /* count the '\0' */
569 file_length = read_longint(f);
570 if (!(flags & XMIT_SAME_TIME))
571 modtime = (time_t)read_int(f);
572 if (!(flags & XMIT_SAME_MODE))
573 mode = from_wire_mode(read_int(f));
575 if (chmod_modes && !S_ISLNK(mode))
576 mode = tweak_mode(mode, chmod_modes);
578 if (preserve_uid && !(flags & XMIT_SAME_UID))
579 uid = (uid_t)read_int(f);
580 if (preserve_gid && !(flags & XMIT_SAME_GID))
581 gid = (gid_t)read_int(f);
583 if ((preserve_devices && IS_DEVICE(mode))
584 || (preserve_specials && IS_SPECIAL(mode))) {
585 if (protocol_version < 28) {
586 if (!(flags & XMIT_SAME_RDEV_pre28))
587 rdev = (dev_t)read_int(f);
590 if (!(flags & XMIT_SAME_RDEV_MAJOR))
591 rdev_major = read_int(f);
592 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
593 rdev_minor = read_byte(f);
595 rdev_minor = read_int(f);
596 rdev = makedev(rdev_major, rdev_minor);
598 } else if (protocol_version < 28)
599 rdev = makedev(0, 0);
602 if (preserve_links && S_ISLNK(mode)) {
603 linkname_len = read_int(f) + 1; /* count the '\0' */
604 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
605 rprintf(FERROR, "overflow: linkname_len=%d\n",
607 overflow_exit("receive_file_entry");
614 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
616 alloc_len = file_struct_len + dirname_len + basename_len
617 + linkname_len + sum_len;
618 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
620 file = (struct file_struct *)bp;
621 memset(bp, 0, file_struct_len);
622 bp += file_struct_len;
624 file->modtime = modtime;
625 file->length = file_length;
631 file->dirname = lastdir = bp;
632 lastdir_len = dirname_len - 1;
633 memcpy(bp, dirname, dirname_len - 1);
636 lastdir_depth = count_dir_elements(lastdir);
637 file->dir.depth = lastdir_depth + 1;
638 if (lastdir_depth >= max_dir_depth)
639 max_dir_depth = lastdir_depth + 1;
640 } else if (dirname) {
641 file->dirname = dirname; /* we're reusing lastname */
642 file->dir.depth = lastdir_depth + 1;
647 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
649 if (flags & XMIT_TOP_DIR) {
650 in_del_hier = recurse;
651 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
652 if (relative_paths && del_hier_name_len > 2
653 && lastname[del_hier_name_len-1] == '.'
654 && lastname[del_hier_name_len-2] == '/')
655 del_hier_name_len -= 2;
656 file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
657 } else if (in_del_hier) {
658 if (!relative_paths || !del_hier_name_len
659 || (l1 >= del_hier_name_len
660 && lastname[del_hier_name_len] == '/'))
661 file->flags |= FLAG_DEL_HERE;
668 memcpy(bp, basename, basename_len);
671 if ((preserve_devices && IS_DEVICE(mode))
672 || (preserve_specials && IS_SPECIAL(mode)))
678 read_sbuf(f, bp, linkname_len - 1);
680 sanitize_path(bp, bp, "", lastdir_depth);
685 #ifdef SUPPORT_HARD_LINKS
686 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
687 flags |= XMIT_HAS_IDEV_DATA;
688 if (flags & XMIT_HAS_IDEV_DATA) {
690 if (protocol_version < 26) {
694 if (!(flags & XMIT_SAME_DEV))
695 dev = read_longint(f);
696 inode = read_longint(f);
698 if (flist->hlink_pool) {
699 file->link_u.idev = pool_talloc(flist->hlink_pool,
700 struct idev, 1, "inode_table");
701 file->F_INODE = inode;
707 if (always_checksum && (sum_len || protocol_version < 28)) {
710 file->u.sum = sum = bp;
713 /* Prior to 28, we get a useless set of nulls. */
716 read_buf(f, sum, checksum_len);
719 if (!preserve_perms) {
720 /* set an appropriate set of permissions based on original
721 * permissions and umask. This emulates what GNU cp does */
722 file->mode &= ~orig_umask;
729 * Create a file_struct for a named file by reading its stat()
730 * information and performing extensive checks against global
733 * @return the new file, or NULL if there was an error or this file
734 * should be excluded.
736 * @todo There is a small optimization opportunity here to avoid
737 * stat()ing the file in some circumstances, which has a certain cost.
738 * We are called immediately after doing readdir(), and so we may
739 * already know the d_type of the file. We could for example avoid
740 * statting directories if we're not recursing, but this is not a very
741 * important case. Some systems may not have d_type.
743 struct file_struct *make_file(char *fname, struct file_list *flist,
744 STRUCT_STAT *stp, unsigned short flags,
747 static char *lastdir;
748 static int lastdir_len = -1;
749 struct file_struct *file;
751 char sum[SUM_LENGTH];
752 char thisname[MAXPATHLEN];
753 char linkname[MAXPATHLEN];
754 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
755 char *basename, *dirname, *bp;
757 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
760 if (strlcpy(thisname, fname, sizeof thisname)
761 >= sizeof thisname - flist_dir_len) {
762 rprintf(FINFO, "skipping overly long name: %s\n", fname);
765 clean_fname(thisname, 0);
767 sanitize_path(thisname, thisname, "", 0);
769 memset(sum, 0, SUM_LENGTH);
771 if (stp && S_ISDIR(stp->st_mode))
772 st = *stp; /* Needed for "symlink/." with --relative. */
773 else if (readlink_stat(thisname, &st, linkname) != 0) {
774 int save_errno = errno;
775 /* See if file is excluded before reporting an error. */
776 if (filter_level != NO_FILTERS
777 && is_excluded(thisname, 0, filter_level))
779 if (save_errno == ENOENT) {
781 /* Avoid "vanished" error if symlink points nowhere. */
782 if (copy_links && do_lstat(thisname, &st) == 0
783 && S_ISLNK(st.st_mode)) {
784 io_error |= IOERR_GENERAL;
785 rprintf(FERROR, "symlink has no referent: %s\n",
786 full_fname(thisname));
790 enum logcode c = am_daemon && protocol_version < 28
792 io_error |= IOERR_VANISHED;
793 rprintf(c, "file has vanished: %s\n",
794 full_fname(thisname));
797 io_error |= IOERR_GENERAL;
798 rsyserr(FERROR, save_errno, "readlink %s failed",
799 full_fname(thisname));
804 /* backup.c calls us with filter_level set to NO_FILTERS. */
805 if (filter_level == NO_FILTERS)
808 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
809 rprintf(FINFO, "skipping directory %s\n", thisname);
813 /* We only care about directories because we need to avoid recursing
814 * into a mount-point directory, not to avoid copying a symlinked
815 * file if -L (or similar) was specified. */
816 if (one_file_system && st.st_dev != filesystem_dev
817 && S_ISDIR(st.st_mode)) {
818 if (one_file_system > 1) {
820 rprintf(FINFO, "skipping mount-point dir %s\n",
825 flags |= FLAG_MOUNT_POINT;
828 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
831 if (lp_ignore_nonreadable(module_id)) {
833 if (!S_ISLNK(st.st_mode))
835 if (access(thisname, R_OK) != 0)
842 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
843 who_am_i(), thisname, filter_level);
846 if ((basename = strrchr(thisname, '/')) != NULL) {
847 dirname_len = ++basename - thisname; /* counts future '\0' */
848 if (lastdir_len == dirname_len - 1
849 && strncmp(thisname, lastdir, lastdir_len) == 0) {
851 dirname_len = 0; /* indicates no copy is needed */
859 basename_len = strlen(basename) + 1; /* count the '\0' */
862 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
867 sum_len = always_checksum && am_sender && S_ISREG(st.st_mode)
868 ? MD4_SUM_LENGTH : 0;
870 alloc_len = file_struct_len + dirname_len + basename_len
871 + linkname_len + sum_len;
873 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
875 if (!(bp = new_array(char, alloc_len)))
876 out_of_memory("make_file");
879 file = (struct file_struct *)bp;
880 memset(bp, 0, file_struct_len);
881 bp += file_struct_len;
884 file->modtime = st.st_mtime;
885 file->length = st.st_size;
886 file->mode = st.st_mode;
887 file->uid = st.st_uid;
888 file->gid = st.st_gid;
890 #ifdef SUPPORT_HARD_LINKS
891 if (flist && flist->hlink_pool) {
892 if (protocol_version < 28) {
893 if (S_ISREG(st.st_mode))
894 file->link_u.idev = pool_talloc(
895 flist->hlink_pool, struct idev, 1,
898 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
899 file->link_u.idev = pool_talloc(
900 flist->hlink_pool, struct idev, 1,
904 if (file->link_u.idev) {
905 file->F_DEV = st.st_dev;
906 file->F_INODE = st.st_ino;
911 file->dirname = lastdir = bp;
912 lastdir_len = dirname_len - 1;
913 memcpy(bp, dirname, dirname_len - 1);
917 file->dirname = dirname;
920 memcpy(bp, basename, basename_len);
923 #ifdef HAVE_STRUCT_STAT_ST_RDEV
924 if ((preserve_devices && IS_DEVICE(st.st_mode))
925 || (preserve_specials && IS_SPECIAL(st.st_mode)))
926 file->u.rdev = st.st_rdev;
932 memcpy(bp, linkname, linkname_len);
939 file_checksum(thisname, bp, st.st_size);
943 file->dir.root = flist_dir;
945 /* This code is only used by the receiver when it is building
946 * a list of files for a delete pass. */
947 if (keep_dirlinks && linkname_len && flist) {
949 int save_mode = file->mode;
950 file->mode = S_IFDIR; /* Find a directory with our name. */
951 if (flist_find(the_file_list, file) >= 0
952 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
953 file->modtime = st2.st_mtime;
954 file->length = st2.st_size;
955 file->mode = st2.st_mode;
956 file->uid = st2.st_uid;
957 file->gid = st2.st_gid;
960 file->mode = save_mode;
963 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
964 stats.total_size += st.st_size;
969 static struct file_struct *send_file_name(int f, struct file_list *flist,
970 char *fname, STRUCT_STAT *stp,
971 unsigned short flags)
973 struct file_struct *file;
975 file = make_file(fname, flist, stp, flags,
976 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
980 if (chmod_modes && !S_ISLNK(file->mode))
981 file->mode = tweak_mode(file->mode, chmod_modes);
983 maybe_emit_filelist_progress(flist->count + flist_count_offset);
987 if (file->basename[0]) {
988 flist->files[flist->count++] = file;
989 send_file_entry(file, f);
994 static void send_if_directory(int f, struct file_list *flist,
995 struct file_struct *file,
996 char *fbuf, unsigned int ol)
998 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1000 if (S_ISDIR(file->mode)
1001 && !(file->flags & FLAG_MOUNT_POINT) && f_name(file, fbuf)) {
1003 unsigned int len = strlen(fbuf);
1004 if (len > 1 && fbuf[len-1] == '/')
1006 if (len >= MAXPATHLEN - 1) {
1007 io_error |= IOERR_GENERAL;
1008 rprintf(FERROR, "skipping long-named directory: %s\n",
1012 save_filters = push_local_filters(fbuf, len);
1013 send_directory(f, flist, fbuf, len);
1014 pop_local_filters(save_filters);
1021 /* This function is normally called by the sender, but the receiving side also
1022 * calls it from get_dirlist() with f set to -1 so that we just construct the
1023 * file list in memory without sending it over the wire. Also, get_dirlist()
1024 * might call this with f set to -2, which also indicates that local filter
1025 * rules should be ignored. */
1026 static void send_directory(int f, struct file_list *flist,
1027 char *fbuf, int len)
1033 int start = flist->count;
1035 if (!(d = opendir(fbuf))) {
1036 io_error |= IOERR_GENERAL;
1037 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1042 if (len != 1 || *fbuf != '/')
1045 remainder = MAXPATHLEN - (p - fbuf);
1047 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1048 char *dname = d_name(di);
1049 if (dname[0] == '.' && (dname[1] == '\0'
1050 || (dname[1] == '.' && dname[2] == '\0')))
1052 if (strlcpy(p, dname, remainder) >= remainder) {
1053 io_error |= IOERR_GENERAL;
1055 "cannot send long-named file %s\n",
1060 send_file_name(f, flist, fbuf, NULL, 0);
1066 io_error |= IOERR_GENERAL;
1067 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1073 int i, end = flist->count - 1;
1074 for (i = start; i <= end; i++)
1075 send_if_directory(f, flist, flist->files[i], fbuf, len);
1079 struct file_list *send_file_list(int f, int argc, char *argv[])
1083 char *p, *dir, olddir[sizeof curr_dir];
1084 char lastpath[MAXPATHLEN] = "";
1085 struct file_list *flist;
1086 struct timeval start_tv, end_tv;
1090 if (show_filelist_p())
1091 start_filelist_progress("building file list");
1093 start_write = stats.total_written;
1094 gettimeofday(&start_tv, NULL);
1096 flist = flist_new(WITH_HLINK, "send_file_list");
1098 io_start_buffering_out();
1099 if (filesfrom_fd >= 0) {
1100 if (argv[0] && !push_dir(argv[0])) {
1101 rsyserr(FERROR, errno, "push_dir %s failed",
1102 full_fname(argv[0]));
1103 exit_cleanup(RERR_FILESELECT);
1109 char fbuf[MAXPATHLEN];
1114 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1116 sanitize_path(fbuf, fbuf, "", 0);
1120 strlcpy(fbuf, *argv++, MAXPATHLEN);
1122 sanitize_path(fbuf, fbuf, "", 0);
1126 if (relative_paths) {
1127 /* We clean up fbuf below. */
1129 } else if (!len || fbuf[len - 1] == '/') {
1130 if (len == 2 && fbuf[0] == '.') {
1131 /* Turn "./" into just "." rather than "./." */
1134 if (len + 1 >= MAXPATHLEN)
1135 overflow_exit("send_file_list");
1140 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1141 && (len == 2 || fbuf[len-3] == '/')) {
1142 if (len + 2 >= MAXPATHLEN)
1143 overflow_exit("send_file_list");
1149 is_dot_dir = fbuf[len-1] == '.'
1150 && (len == 1 || fbuf[len-2] == '/');
1153 if (link_stat(fbuf, &st, keep_dirlinks) != 0) {
1154 io_error |= IOERR_GENERAL;
1155 rsyserr(FERROR, errno, "link_stat %s failed",
1160 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1161 rprintf(FINFO, "skipping directory %s\n", fbuf);
1168 if (!relative_paths) {
1169 p = strrchr(fbuf, '/');
1176 len -= p - fbuf + 1;
1181 if ((p = strstr(fbuf, "/./")) != NULL) {
1187 len -= p - fbuf + 3;
1191 /* Get rid of trailing "/" and "/.". */
1193 if (fn[len - 1] == '/') {
1195 if (!--len && !dir) {
1200 else if (len >= 2 && fn[len - 1] == '.'
1201 && fn[len - 2] == '/') {
1203 if (!(len -= 2) && !dir) {
1211 /* Reject a ".." dir in the active part of the path. */
1212 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1213 if ((p[2] == '/' || p[2] == '\0')
1214 && (p == fn || p[-1] == '/')) {
1216 "found \"..\" dir in relative path: %s\n",
1218 exit_cleanup(RERR_SYNTAX);
1229 static char *lastdir;
1230 static int lastdir_len;
1232 strlcpy(olddir, curr_dir, sizeof olddir);
1234 if (!push_dir(dir)) {
1235 io_error |= IOERR_GENERAL;
1236 rsyserr(FERROR, errno, "push_dir %s failed",
1241 if (lastdir && strcmp(lastdir, dir) == 0) {
1242 flist_dir = lastdir;
1243 flist_dir_len = lastdir_len;
1245 flist_dir = lastdir = strdup(dir);
1246 flist_dir_len = lastdir_len = strlen(dir);
1251 memmove(fbuf, fn, len + 1);
1253 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1254 /* Send the implied directories at the start of the
1255 * source spec, so we get their permissions right. */
1256 char *lp = lastpath, *slash = fbuf;
1258 /* Skip any initial directories in our path that we
1259 * have in common with lastpath. */
1260 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1265 if (fn != p || (*lp && *lp != '/')) {
1266 int save_copy_links = copy_links;
1267 int save_xfer_dirs = xfer_dirs;
1268 copy_links = copy_unsafe_links;
1270 while ((slash = strchr(slash+1, '/')) != 0) {
1272 send_file_name(f, flist, fbuf, NULL, 0);
1275 copy_links = save_copy_links;
1276 xfer_dirs = save_xfer_dirs;
1278 strlcpy(lastpath, fbuf, sizeof lastpath);
1283 if (one_file_system)
1284 filesystem_dev = st.st_dev;
1286 if (recurse || (xfer_dirs && is_dot_dir)) {
1287 struct file_struct *file;
1288 file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
1290 send_if_directory(f, flist, file, fbuf, len);
1292 send_file_name(f, flist, fbuf, &st, 0);
1297 if (!pop_dir(olddir)) {
1298 rsyserr(FERROR, errno, "pop_dir %s failed",
1299 full_fname(olddir));
1300 exit_cleanup(RERR_FILESELECT);
1305 gettimeofday(&end_tv, NULL);
1306 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1307 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1308 if (stats.flist_buildtime == 0)
1309 stats.flist_buildtime = 1;
1312 send_file_entry(NULL, f);
1314 if (show_filelist_p())
1315 finish_filelist_progress(flist);
1317 gettimeofday(&end_tv, NULL);
1318 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1319 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1321 if (flist->hlink_pool) {
1322 pool_destroy(flist->hlink_pool);
1323 flist->hlink_pool = NULL;
1326 /* Sort the list without removing any duplicates. This allows the
1327 * receiving side to ask for any name they like, which gives us the
1328 * flexibility to change the way we unduplicate names in the future
1329 * without causing a compatibility problem with older versions. */
1330 clean_flist(flist, 0, 0);
1334 /* send the io_error flag */
1335 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1338 stats.flist_size = stats.total_written - start_write;
1339 stats.num_files = flist->count;
1342 output_flist(flist);
1345 rprintf(FINFO, "send_file_list done\n");
1350 struct file_list *recv_file_list(int f)
1352 struct file_list *flist;
1353 unsigned short flags;
1356 if (show_filelist_p())
1357 start_filelist_progress("receiving file list");
1359 start_read = stats.total_read;
1361 flist = flist_new(WITH_HLINK, "recv_file_list");
1364 flist->malloced = 1000;
1365 flist->files = new_array(struct file_struct *, flist->malloced);
1369 while ((flags = read_byte(f)) != 0) {
1370 struct file_struct *file;
1372 flist_expand(flist);
1374 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1375 flags |= read_byte(f) << 8;
1376 file = receive_file_entry(flist, flags, f);
1378 if (S_ISREG(file->mode))
1379 stats.total_size += file->length;
1381 flist->files[flist->count++] = file;
1383 maybe_emit_filelist_progress(flist->count);
1386 rprintf(FINFO, "recv_file_name(%s)\n",
1387 f_name(file, NULL));
1390 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1393 rprintf(FINFO, "received %d names\n", flist->count);
1395 if (show_filelist_p())
1396 finish_filelist_progress(flist);
1398 clean_flist(flist, relative_paths, 1);
1401 recv_uid_list(f, flist);
1403 /* Recv the io_error flag */
1404 if (lp_ignore_errors(module_id) || ignore_errors)
1407 io_error |= read_int(f);
1411 output_flist(flist);
1415 for (i = 0; i < flist->count; i++)
1416 list_file_entry(flist->files[i]);
1420 rprintf(FINFO, "recv_file_list done\n");
1422 stats.flist_size = stats.total_read - start_read;
1423 stats.num_files = flist->count;
1428 out_of_memory("recv_file_list");
1429 return NULL; /* not reached */
1432 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1434 return f_name_cmp(*file1, *file2);
1437 /* Search for an identically-named item in the file list. Note that the
1438 * items must agree in their directory-ness, or no match is returned. */
1439 int flist_find(struct file_list *flist, struct file_struct *f)
1441 int low = flist->low, high = flist->high;
1442 int ret, mid, mid_up;
1444 while (low <= high) {
1445 mid = (low + high) / 2;
1446 if (flist->files[mid]->basename)
1449 /* Scan for the next non-empty entry using the cached
1450 * distance values. If the value isn't fully up-to-
1451 * date, update it. */
1452 mid_up = mid + flist->files[mid]->dir.depth;
1453 if (!flist->files[mid_up]->basename) {
1455 mid_up += flist->files[mid_up]->dir.depth;
1456 } while (!flist->files[mid_up]->basename);
1457 flist->files[mid]->dir.depth = mid_up - mid;
1459 if (mid_up > high) {
1460 /* If there's nothing left above us, set high to
1461 * a non-empty entry below us and continue. */
1462 high = mid - flist->files[mid]->length;
1463 if (!flist->files[high]->basename) {
1465 high -= flist->files[high]->length;
1466 } while (!flist->files[high]->basename);
1467 flist->files[mid]->length = mid - high;
1472 ret = f_name_cmp(flist->files[mid_up], f);
1474 if (protocol_version < 29
1475 && S_ISDIR(flist->files[mid_up]->mode)
1476 != S_ISDIR(f->mode))
1489 * Free up any resources a file_struct has allocated
1490 * and clear the file.
1492 void clear_file(int i, struct file_list *flist)
1494 struct file_struct *file = flist->files[i];
1495 if (flist->hlink_pool && file->link_u.idev)
1496 pool_free(flist->hlink_pool, 0, file->link_u.idev);
1497 memset(file, 0, file_struct_len);
1498 /* In an empty entry, dir.depth is an offset to the next non-empty
1499 * entry. Likewise for length in the opposite direction. We assume
1500 * that we're alone for now since flist_find() will collate adjacent
1501 * items for any entries that are encountered during the find. */
1502 file->length = file->dir.depth = 1;
1506 * allocate a new file list
1508 struct file_list *flist_new(int with_hlink, char *msg)
1510 struct file_list *flist;
1512 flist = new(struct file_list);
1516 memset(flist, 0, sizeof (struct file_list));
1518 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1519 out_of_memory, POOL_INTERN)))
1522 #ifdef SUPPORT_HARD_LINKS
1523 if (with_hlink && preserve_hard_links) {
1524 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1525 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1534 * free up all elements in a flist
1536 void flist_free(struct file_list *flist)
1538 pool_destroy(flist->file_pool);
1539 pool_destroy(flist->hlink_pool);
1545 * This routine ensures we don't have any duplicate names in our file list.
1546 * duplicate names can cause corruption because of the pipelining
1548 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1550 char fbuf[MAXPATHLEN];
1555 if (flist->count == 0) {
1560 qsort(flist->files, flist->count,
1561 sizeof flist->files[0], (int (*)())file_compare);
1563 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1564 if (flist->files[i]->basename) {
1569 flist->low = prev_i;
1570 while (++i < flist->count) {
1572 struct file_struct *file = flist->files[i];
1574 if (!file->basename)
1576 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1578 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1579 int save_mode = file->mode;
1580 /* Make sure that this directory doesn't duplicate a
1581 * non-directory earlier in the list. */
1582 flist->high = prev_i;
1583 file->mode = S_IFREG;
1584 j = flist_find(flist, file);
1585 file->mode = save_mode;
1589 struct file_struct *fp = flist->files[j];
1591 /* If one is a dir and the other is not, we want to
1592 * keep the dir because it might have contents in the
1594 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1595 if (S_ISDIR(file->mode))
1601 if (verbose > 1 && !am_server) {
1603 "removing duplicate name %s from file list (%d)\n",
1604 f_name(file, fbuf), drop);
1606 /* Make sure we don't lose track of a user-specified
1608 flist->files[keep]->flags |= flist->files[drop]->flags
1609 & (FLAG_TOP_DIR|FLAG_DEL_HERE);
1611 clear_file(drop, flist);
1614 if (flist->low == drop) {
1616 j < i && !flist->files[j]->basename;
1625 flist->high = no_dups ? prev_i : flist->count - 1;
1628 /* We need to strip off the leading slashes for relative
1629 * paths, but this must be done _after_ the sorting phase. */
1630 for (i = flist->low; i <= flist->high; i++) {
1631 struct file_struct *file = flist->files[i];
1635 while (*file->dirname == '/')
1637 if (!*file->dirname)
1638 file->dirname = NULL;
1642 if (prune_empty_dirs && no_dups && max_dir_depth) {
1643 int j, cur_depth = 0;
1644 int *maybe_dirs = new_array(int, max_dir_depth);
1648 for (i = flist->low; i <= flist->high; i++) {
1649 struct file_struct *file = flist->files[i];
1651 if (S_ISDIR(file->mode) && file->dir.depth) {
1653 cur_depth = file->dir.depth - 1;
1654 for ( ; j >= cur_depth; j--) {
1655 if (maybe_dirs[j] < 0)
1657 clear_file(maybe_dirs[j], flist);
1659 if (is_excluded(f_name(file, fbuf), 1,
1661 for (j = 0; j <= cur_depth; j++)
1664 maybe_dirs[cur_depth] = i;
1665 } else if (maybe_dirs[cur_depth] >= 0) {
1666 for (j = 0; j <= cur_depth; j++)
1670 for (j = cur_depth; j >= 0; j--) {
1671 if (maybe_dirs[j] < 0)
1673 clear_file(maybe_dirs[j], flist);
1676 for (i = flist->low; i <= flist->high; i++) {
1677 if (flist->files[i]->basename)
1681 for (i = flist->high; i >= flist->low; i--) {
1682 if (flist->files[i]->basename)
1689 static void output_flist(struct file_list *flist)
1691 char uidbuf[16], gidbuf[16], depthbuf[16];
1692 struct file_struct *file;
1693 const char *who = who_am_i();
1696 for (i = 0; i < flist->count; i++) {
1697 file = flist->files[i];
1698 if ((am_root || am_sender) && preserve_uid)
1699 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1702 if (preserve_gid && file->gid != GID_NONE)
1703 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1707 sprintf(depthbuf, "%d", file->dir.depth);
1708 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1709 who, i, am_sender ? NS(file->dir.root) : depthbuf,
1710 file->dirname ? file->dirname : "",
1711 file->dirname ? "/" : "", NS(file->basename),
1712 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
1713 (double)file->length, uidbuf, gidbuf, file->flags);
1717 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1718 enum fnc_type { t_PATH, t_ITEM };
1720 /* Compare the names of two file_struct entities, similar to how strcmp()
1721 * would do if it were operating on the joined strings.
1723 * Some differences beginning with protocol_version 29: (1) directory names
1724 * are compared with an assumed trailing slash so that they compare in a
1725 * way that would cause them to sort immediately prior to any content they
1726 * may have; (2) a directory of any name compares after a non-directory of
1727 * any name at the same depth; (3) a directory with name "." compares prior
1728 * to anything else. These changes mean that a directory and a non-dir
1729 * with the same name will not compare as equal (protocol_version >= 29).
1731 * The dirname component can be an empty string, but the basename component
1732 * cannot (and never is in the current codebase). The basename component
1733 * may be NULL (for a removed item), in which case it is considered to be
1734 * after any existing item. */
1735 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1738 const uchar *c1, *c2;
1739 enum fnc_state state1, state2;
1740 enum fnc_type type1, type2;
1741 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1743 if (!f1 || !f1->basename) {
1744 if (!f2 || !f2->basename)
1748 if (!f2 || !f2->basename)
1751 c1 = (uchar*)f1->dirname;
1752 c2 = (uchar*)f2->dirname;
1756 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1757 c1 = (uchar*)f1->basename;
1758 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1760 state1 = s_TRAILING;
1773 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1774 c2 = (uchar*)f2->basename;
1775 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1777 state2 = s_TRAILING;
1791 return type1 == t_PATH ? 1 : -1;
1794 if ((dif = (int)*c1++ - (int)*c2++) != 0)
1803 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1804 c1 = (uchar*)f1->basename;
1805 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1807 state1 = s_TRAILING;
1813 state1 = s_TRAILING;
1814 if (type1 == t_PATH) {
1823 if (*c2 && type1 != type2)
1824 return type1 == t_PATH ? 1 : -1;
1833 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1834 c2 = (uchar*)f2->basename;
1835 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1837 state2 = s_TRAILING;
1843 state2 = s_TRAILING;
1844 if (type2 == t_PATH) {
1856 return type1 == t_PATH ? 1 : -1;
1863 /* Return a copy of the full filename of a flist entry, using the indicated
1864 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
1865 * done because we checked the size when creating the file_struct entry.
1867 char *f_name(struct file_struct *f, char *fbuf)
1869 if (!f || !f->basename)
1873 static char names[5][MAXPATHLEN];
1874 static unsigned int n;
1876 n = (n + 1) % (sizeof names / sizeof names[0]);
1882 int len = strlen(f->dirname);
1883 memcpy(fbuf, f->dirname, len);
1885 strcpy(fbuf + len + 1, f->basename);
1887 strcpy(fbuf, f->basename);
1892 /* Do a non-recursive scan of the named directory, possibly ignoring all
1893 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1894 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1895 * buffer (the functions we call will append names onto the end, but the old
1896 * dir value will be restored on exit). */
1897 struct file_list *get_dirlist(char *dirname, int dlen,
1898 int ignore_filter_rules)
1900 struct file_list *dirlist;
1901 char dirbuf[MAXPATHLEN];
1902 int save_recurse = recurse;
1905 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1906 if (dlen >= MAXPATHLEN)
1911 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1914 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
1915 recurse = save_recurse;
1917 flist_count_offset += dirlist->count;
1919 clean_flist(dirlist, 0, 0);
1922 output_flist(dirlist);