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_uid;
52 extern int preserve_gid;
53 extern int relative_paths;
54 extern int implied_dirs;
55 extern int copy_links;
56 extern int copy_unsafe_links;
57 extern int protocol_version;
58 extern int sanitize_paths;
59 extern int orig_umask;
60 extern struct stats stats;
61 extern struct file_list *the_file_list;
63 extern char curr_dir[MAXPATHLEN];
65 extern struct chmod_mode_struct *chmod_modes;
67 extern struct filter_list_struct filter_list;
68 extern struct filter_list_struct server_filter_list;
72 dev_t filesystem_dev; /* used to implement -x */
74 static char empty_sum[MD4_SUM_LENGTH];
75 static int flist_count_offset;
76 static unsigned int file_struct_len;
77 static struct file_list *sorting_flist;
79 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
80 static void output_flist(struct file_list *flist);
86 /* Figure out how big the file_struct is without trailing padding */
87 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
88 checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
91 static int show_filelist_p(void)
93 return verbose && xfer_dirs && !am_server;
96 static void start_filelist_progress(char *kind)
98 rprintf(FINFO, "%s ... ", kind);
99 if (verbose > 1 || do_progress)
100 rprintf(FINFO, "\n");
104 static void emit_filelist_progress(int count)
106 rprintf(FINFO, " %d files...\r", count);
109 static void maybe_emit_filelist_progress(int count)
111 if (do_progress && show_filelist_p() && (count % 100) == 0)
112 emit_filelist_progress(count);
115 static void finish_filelist_progress(const struct file_list *flist)
118 /* This overwrites the progress line */
119 rprintf(FINFO, "%d file%sto consider\n",
120 flist->count, flist->count == 1 ? " " : "s ");
122 rprintf(FINFO, "done\n");
125 void show_flist_stats(void)
130 static void list_file_entry(struct file_struct *f)
135 /* this can happen if duplicate names were removed */
139 permstring(perms, f->mode);
142 if (preserve_links && S_ISLNK(f->mode)) {
143 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
145 (double)f->length, timestring(f->modtime),
146 safe_fname(f_name(f)), safe_fname(f->u.link));
150 rprintf(FINFO, "%s %11.0f %s %s\n",
152 (double)f->length, timestring(f->modtime),
153 safe_fname(f_name(f)));
158 * Stat either a symlink or its referent, depending on the settings of
159 * copy_links, copy_unsafe_links, etc.
161 * @retval -1 on error
163 * @retval 0 for success
165 * @post If @p path is a symlink, then @p linkbuf (of size @c
166 * MAXPATHLEN) contains the symlink target.
168 * @post @p buffer contains information about the link or the
169 * referrent as appropriate, if they exist.
171 static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
175 return do_stat(path, buffer);
176 if (link_stat(path, buffer, 0) < 0)
178 if (S_ISLNK(buffer->st_mode)) {
179 int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1);
183 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
185 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
186 safe_fname(path), safe_fname(linkbuf));
188 return do_stat(path, buffer);
193 return do_stat(path, buffer);
197 int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks)
201 return do_stat(path, buffer);
202 if (do_lstat(path, buffer) < 0)
204 if (follow_dirlinks && S_ISLNK(buffer->st_mode)) {
206 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
211 return do_stat(path, buffer);
215 /* This function is used to check if a file should be included/excluded
216 * from the list of files based on its name and type etc. The value of
217 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
218 static int is_excluded(char *fname, int is_dir, int filter_level)
220 #if 0 /* This currently never happens, so avoid a useless compare. */
221 if (filter_level == NO_FILTERS)
225 /* never exclude '.', even if somebody does --exclude '*' */
226 if (fname[0] == '.' && !fname[1])
228 /* Handle the -R version of the '.' dir. */
229 if (fname[0] == '/') {
230 int len = strlen(fname);
231 if (fname[len-1] == '.' && fname[len-2] == '/')
235 if (server_filter_list.head
236 && check_filter(&server_filter_list, fname, is_dir) < 0)
238 if (filter_level != ALL_FILTERS)
241 && check_filter(&filter_list, fname, is_dir) < 0)
246 static int to_wire_mode(mode_t mode)
249 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
250 return (mode & ~(_S_IFMT)) | 0120000;
255 static mode_t from_wire_mode(int mode)
257 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
258 return (mode & ~(_S_IFMT)) | _S_IFLNK;
262 static void send_directory(int f, struct file_list *flist,
263 char *fbuf, int len);
265 static char *flist_dir;
266 static int flist_dir_len;
270 * Make sure @p flist is big enough to hold at least @p flist->count
273 void flist_expand(struct file_list *flist)
275 struct file_struct **new_ptr;
277 if (flist->count < flist->malloced)
280 if (flist->malloced < FLIST_START)
281 flist->malloced = FLIST_START;
282 else if (flist->malloced >= FLIST_LINEAR)
283 flist->malloced += FLIST_LINEAR;
285 flist->malloced *= 2;
288 * In case count jumped or we are starting the list
289 * with a known size just set it.
291 if (flist->malloced < flist->count)
292 flist->malloced = flist->count;
294 new_ptr = realloc_array(flist->files, struct file_struct *,
297 if (verbose >= 2 && flist->malloced != FLIST_START) {
298 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
300 (double)sizeof flist->files[0] * flist->malloced,
301 (new_ptr == flist->files) ? " not" : "");
304 flist->files = new_ptr;
307 out_of_memory("flist_expand");
310 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
312 unsigned short flags;
313 static time_t modtime;
317 static uint32 rdev_major;
320 static char lastname[MAXPATHLEN];
321 char fname[MAXPATHLEN];
329 modtime = 0, mode = 0;
330 dev = 0, rdev = makedev(0, 0);
337 io_write_phase = "send_file_entry";
339 f_name_to(file, fname);
343 if (file->mode == mode)
344 flags |= XMIT_SAME_MODE;
347 if (preserve_devices) {
348 if (protocol_version < 28) {
349 if (IS_DEVICE(mode)) {
350 if (file->u.rdev == rdev)
351 flags |= XMIT_SAME_RDEV_pre28;
355 rdev = makedev(0, 0);
356 } else if (IS_DEVICE(mode)) {
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;
366 if (file->uid == uid)
367 flags |= XMIT_SAME_UID;
370 if (file->gid == gid)
371 flags |= XMIT_SAME_GID;
374 if (file->modtime == modtime)
375 flags |= XMIT_SAME_TIME;
377 modtime = file->modtime;
379 #ifdef SUPPORT_HARD_LINKS
380 if (file->link_u.idev) {
381 if (file->F_DEV == dev) {
382 if (protocol_version >= 28)
383 flags |= XMIT_SAME_DEV;
386 flags |= XMIT_HAS_IDEV_DATA;
391 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
393 l2 = strlen(fname+l1);
396 flags |= XMIT_SAME_NAME;
398 flags |= XMIT_LONG_NAME;
400 /* We must make sure we don't send a zero flag byte or the
401 * other end will terminate the flist transfer. Note that
402 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
403 * it's harmless way to add a bit to the first flag byte. */
404 if (protocol_version >= 28) {
405 if (!flags && !S_ISDIR(mode))
406 flags |= XMIT_TOP_DIR;
407 if ((flags & 0xFF00) || !flags) {
408 flags |= XMIT_EXTENDED_FLAGS;
409 write_byte(f, flags);
410 write_byte(f, flags >> 8);
412 write_byte(f, flags);
415 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
416 write_byte(f, flags);
418 if (flags & XMIT_SAME_NAME)
420 if (flags & XMIT_LONG_NAME)
424 write_buf(f, fname + l1, l2);
426 write_longint(f, file->length);
427 if (!(flags & XMIT_SAME_TIME))
428 write_int(f, modtime);
429 if (!(flags & XMIT_SAME_MODE))
430 write_int(f, to_wire_mode(mode));
431 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
436 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
441 if (preserve_devices && IS_DEVICE(mode)) {
442 if (protocol_version < 28) {
443 if (!(flags & XMIT_SAME_RDEV_pre28))
444 write_int(f, (int)rdev);
446 if (!(flags & XMIT_SAME_RDEV_MAJOR))
447 write_int(f, major(rdev));
448 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
449 write_byte(f, minor(rdev));
451 write_int(f, minor(rdev));
456 if (preserve_links && S_ISLNK(mode)) {
457 int len = strlen(file->u.link);
459 write_buf(f, file->u.link, len);
463 #ifdef SUPPORT_HARD_LINKS
464 if (flags & XMIT_HAS_IDEV_DATA) {
465 if (protocol_version < 26) {
466 /* 32-bit dev_t and ino_t */
468 write_int(f, file->F_INODE);
470 /* 64-bit dev_t and ino_t */
471 if (!(flags & XMIT_SAME_DEV))
472 write_longint(f, dev);
473 write_longint(f, file->F_INODE);
478 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
483 /* Prior to 28, we sent a useless set of nulls. */
486 write_buf(f, sum, checksum_len);
489 strlcpy(lastname, fname, MAXPATHLEN);
491 io_write_phase = "unknown";
494 static struct file_struct *receive_file_entry(struct file_list *flist,
495 unsigned short flags, int f)
497 static time_t modtime;
501 static uint32 rdev_major;
504 static char lastname[MAXPATHLEN], *lastdir;
505 static int lastdir_depth, lastdir_len = -1;
506 static unsigned int del_hier_name_len = 0;
507 static int in_del_hier = 0;
508 char thisname[MAXPATHLEN];
509 unsigned int l1 = 0, l2 = 0;
510 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
512 char *basename, *dirname, *bp;
513 struct file_struct *file;
516 modtime = 0, mode = 0;
517 dev = 0, rdev = makedev(0, 0);
526 if (flags & XMIT_SAME_NAME)
529 if (flags & XMIT_LONG_NAME)
534 if (l2 >= MAXPATHLEN - l1) {
536 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
537 flags, l1, l2, safe_fname(lastname));
538 overflow_exit("receive_file_entry");
541 strlcpy(thisname, lastname, l1 + 1);
542 read_sbuf(f, &thisname[l1], l2);
543 thisname[l1 + l2] = 0;
545 strlcpy(lastname, thisname, MAXPATHLEN);
547 clean_fname(thisname, 0);
550 sanitize_path(thisname, thisname, "", 0);
552 if ((basename = strrchr(thisname, '/')) != NULL) {
553 dirname_len = ++basename - thisname; /* counts future '\0' */
554 if (lastdir_len == dirname_len - 1
555 && strncmp(thisname, lastdir, lastdir_len) == 0) {
557 dirname_len = 0; /* indicates no copy is needed */
565 basename_len = strlen(basename) + 1; /* count the '\0' */
567 file_length = read_longint(f);
568 if (!(flags & XMIT_SAME_TIME))
569 modtime = (time_t)read_int(f);
570 if (!(flags & XMIT_SAME_MODE))
571 mode = from_wire_mode(read_int(f));
573 if (preserve_uid && !(flags & XMIT_SAME_UID))
574 uid = (uid_t)read_int(f);
575 if (preserve_gid && !(flags & XMIT_SAME_GID))
576 gid = (gid_t)read_int(f);
578 if (preserve_devices) {
579 if (protocol_version < 28) {
580 if (IS_DEVICE(mode)) {
581 if (!(flags & XMIT_SAME_RDEV_pre28))
582 rdev = (dev_t)read_int(f);
584 rdev = makedev(0, 0);
585 } else if (IS_DEVICE(mode)) {
587 if (!(flags & XMIT_SAME_RDEV_MAJOR))
588 rdev_major = read_int(f);
589 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
590 rdev_minor = read_byte(f);
592 rdev_minor = read_int(f);
593 rdev = makedev(rdev_major, rdev_minor);
598 if (preserve_links && S_ISLNK(mode)) {
599 linkname_len = read_int(f) + 1; /* count the '\0' */
600 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
601 rprintf(FERROR, "overflow: linkname_len=%d\n",
603 overflow_exit("receive_file_entry");
610 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
612 alloc_len = file_struct_len + dirname_len + basename_len
613 + linkname_len + sum_len;
614 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
616 file = (struct file_struct *)bp;
617 memset(bp, 0, file_struct_len);
618 bp += file_struct_len;
621 file->modtime = modtime;
622 file->length = file_length;
628 file->dirname = lastdir = bp;
629 lastdir_len = dirname_len - 1;
630 memcpy(bp, dirname, dirname_len - 1);
633 lastdir_depth = count_dir_elements(lastdir);
634 file->dir.depth = lastdir_depth + 1;
635 } else if (dirname) {
636 file->dirname = dirname; /* we're reusing lastname */
637 file->dir.depth = lastdir_depth + 1;
642 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
644 if (flags & XMIT_TOP_DIR) {
645 in_del_hier = recurse;
646 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
647 if (relative_paths && del_hier_name_len > 2
648 && basename_len == 1+1 && *basename == '.')
649 del_hier_name_len -= 2;
650 file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
651 } else if (in_del_hier) {
652 if (!relative_paths || !del_hier_name_len
653 || (l1 >= del_hier_name_len
654 && thisname[del_hier_name_len] == '/'))
655 file->flags |= FLAG_DEL_HERE;
662 memcpy(bp, basename, basename_len);
665 if (preserve_devices && IS_DEVICE(mode))
671 read_sbuf(f, bp, linkname_len - 1);
673 sanitize_path(bp, bp, "", lastdir_depth);
678 #ifdef SUPPORT_HARD_LINKS
679 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
680 flags |= XMIT_HAS_IDEV_DATA;
681 if (flags & XMIT_HAS_IDEV_DATA) {
683 if (protocol_version < 26) {
687 if (!(flags & XMIT_SAME_DEV))
688 dev = read_longint(f);
689 inode = read_longint(f);
691 if (flist->hlink_pool) {
692 file->link_u.idev = pool_talloc(flist->hlink_pool,
693 struct idev, 1, "inode_table");
694 file->F_INODE = inode;
700 if (always_checksum && (sum_len || protocol_version < 28)) {
703 file->u.sum = sum = bp;
706 /* Prior to 28, we get a useless set of nulls. */
709 read_buf(f, sum, checksum_len);
712 if (!preserve_perms) {
713 /* set an appropriate set of permissions based on original
714 * permissions and umask. This emulates what GNU cp does */
715 file->mode &= ~orig_umask;
722 * Create a file_struct for a named file by reading its stat()
723 * information and performing extensive checks against global
726 * @return the new file, or NULL if there was an error or this file
727 * should be excluded.
729 * @todo There is a small optimization opportunity here to avoid
730 * stat()ing the file in some circumstances, which has a certain cost.
731 * We are called immediately after doing readdir(), and so we may
732 * already know the d_type of the file. We could for example avoid
733 * statting directories if we're not recursing, but this is not a very
734 * important case. Some systems may not have d_type.
736 struct file_struct *make_file(char *fname, struct file_list *flist,
739 static char *lastdir;
740 static int lastdir_len = -1;
741 struct file_struct *file;
743 char sum[SUM_LENGTH];
744 char thisname[MAXPATHLEN];
745 char linkname[MAXPATHLEN];
746 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
747 char *basename, *dirname, *bp;
748 unsigned short flags = 0;
750 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
753 if (strlcpy(thisname, fname, sizeof thisname)
754 >= sizeof thisname - flist_dir_len) {
755 rprintf(FINFO, "skipping overly long name: %s\n",
759 clean_fname(thisname, 0);
761 sanitize_path(thisname, thisname, "", 0);
763 memset(sum, 0, SUM_LENGTH);
765 if (readlink_stat(thisname, &st, linkname) != 0) {
766 int save_errno = errno;
767 /* See if file is excluded before reporting an error. */
768 if (filter_level != NO_FILTERS
769 && is_excluded(thisname, 0, filter_level))
771 if (save_errno == ENOENT) {
773 /* Avoid "vanished" error if symlink points nowhere. */
774 if (copy_links && do_lstat(thisname, &st) == 0
775 && S_ISLNK(st.st_mode)) {
776 io_error |= IOERR_GENERAL;
777 rprintf(FERROR, "symlink has no referent: %s\n",
778 full_fname(thisname));
782 enum logcode c = am_daemon && protocol_version < 28
784 io_error |= IOERR_VANISHED;
785 rprintf(c, "file has vanished: %s\n",
786 full_fname(thisname));
789 io_error |= IOERR_GENERAL;
790 rsyserr(FERROR, save_errno, "readlink %s failed",
791 full_fname(thisname));
796 /* backup.c calls us with filter_level set to NO_FILTERS. */
797 if (filter_level == NO_FILTERS)
800 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
801 rprintf(FINFO, "skipping directory %s\n", safe_fname(thisname));
805 /* We only care about directories because we need to avoid recursing
806 * into a mount-point directory, not to avoid copying a symlinked
807 * file if -L (or similar) was specified. */
808 if (one_file_system && st.st_dev != filesystem_dev
809 && S_ISDIR(st.st_mode))
810 flags |= FLAG_MOUNT_POINT;
812 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
815 if (lp_ignore_nonreadable(module_id)) {
817 if (!S_ISLNK(st.st_mode))
819 if (access(thisname, R_OK) != 0)
826 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
827 who_am_i(), safe_fname(thisname), filter_level);
830 if ((basename = strrchr(thisname, '/')) != NULL) {
831 dirname_len = ++basename - thisname; /* counts future '\0' */
832 if (lastdir_len == dirname_len - 1
833 && strncmp(thisname, lastdir, lastdir_len) == 0) {
835 dirname_len = 0; /* indicates no copy is needed */
843 basename_len = strlen(basename) + 1; /* count the '\0' */
846 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
851 sum_len = always_checksum && am_sender && S_ISREG(st.st_mode)
852 ? MD4_SUM_LENGTH : 0;
854 alloc_len = file_struct_len + dirname_len + basename_len
855 + linkname_len + sum_len;
857 bp = pool_alloc(flist->file_pool, alloc_len,
858 "receive_file_entry");
860 if (!(bp = new_array(char, alloc_len)))
861 out_of_memory("receive_file_entry");
864 file = (struct file_struct *)bp;
865 memset(bp, 0, file_struct_len);
866 bp += file_struct_len;
869 file->modtime = st.st_mtime;
870 file->length = st.st_size;
871 if (chmod_modes && am_sender && (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)))
872 file->mode = tweak_mode(st.st_mode, chmod_modes);
874 file->mode = st.st_mode;
875 file->uid = st.st_uid;
876 file->gid = st.st_gid;
878 #ifdef SUPPORT_HARD_LINKS
879 if (flist && flist->hlink_pool) {
880 if (protocol_version < 28) {
881 if (S_ISREG(st.st_mode))
882 file->link_u.idev = pool_talloc(
883 flist->hlink_pool, struct idev, 1,
886 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
887 file->link_u.idev = pool_talloc(
888 flist->hlink_pool, struct idev, 1,
892 if (file->link_u.idev) {
893 file->F_DEV = st.st_dev;
894 file->F_INODE = st.st_ino;
899 file->dirname = lastdir = bp;
900 lastdir_len = dirname_len - 1;
901 memcpy(bp, dirname, dirname_len - 1);
905 file->dirname = dirname;
908 memcpy(bp, basename, basename_len);
911 #ifdef HAVE_STRUCT_STAT_ST_RDEV
912 if (preserve_devices && IS_DEVICE(st.st_mode))
913 file->u.rdev = st.st_rdev;
919 memcpy(bp, linkname, linkname_len);
926 file_checksum(thisname, bp, st.st_size);
930 file->dir.root = flist_dir;
932 /* This code is only used by the receiver when it is building
933 * a list of files for a delete pass. */
934 if (keep_dirlinks && linkname_len && flist) {
936 int save_mode = file->mode;
937 file->mode = S_IFDIR; /* find a directory w/our name */
938 if (flist_find(the_file_list, file) >= 0
939 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
940 file->modtime = st2.st_mtime;
941 file->length = st2.st_size;
942 file->mode = st2.st_mode;
943 file->uid = st2.st_uid;
944 file->gid = st2.st_gid;
947 file->mode = save_mode;
950 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
951 stats.total_size += st.st_size;
956 static struct file_struct *send_file_name(int f, struct file_list *flist,
957 char *fname, unsigned short base_flags)
959 struct file_struct *file;
961 file = make_file(fname, flist, f == -2 ? SERVER_FILTERS : ALL_FILTERS);
965 maybe_emit_filelist_progress(flist->count + flist_count_offset);
969 if (file->basename[0]) {
970 flist->files[flist->count++] = file;
971 send_file_entry(file, f, base_flags);
976 static void send_if_directory(int f, struct file_list *flist,
977 struct file_struct *file,
978 char *fbuf, unsigned int ol)
980 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
982 if (S_ISDIR(file->mode)
983 && !(file->flags & FLAG_MOUNT_POINT) && f_name_to(file, fbuf)) {
985 unsigned int len = strlen(fbuf);
986 if (len > 1 && fbuf[len-1] == '/')
988 if (len >= MAXPATHLEN - 1) {
989 io_error |= IOERR_GENERAL;
990 rprintf(FERROR, "skipping long-named directory: %s\n",
994 save_filters = push_local_filters(fbuf, len);
995 send_directory(f, flist, fbuf, len);
996 pop_local_filters(save_filters);
1003 /* This function is normally called by the sender, but the receiving side also
1004 * calls it from get_dirlist() with f set to -1 so that we just construct the
1005 * file list in memory without sending it over the wire. Also, get_dirlist()
1006 * might call this with f set to -2, which also indicates that local filter
1007 * rules should be ignored. */
1008 static void send_directory(int f, struct file_list *flist,
1009 char *fbuf, int len)
1015 int start = flist->count;
1017 if (!(d = opendir(fbuf))) {
1018 io_error |= IOERR_GENERAL;
1019 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1024 if (len != 1 || *fbuf != '/')
1027 remainder = MAXPATHLEN - (p - fbuf);
1029 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1030 char *dname = d_name(di);
1031 if (dname[0] == '.' && (dname[1] == '\0'
1032 || (dname[1] == '.' && dname[2] == '\0')))
1034 if (strlcpy(p, dname, remainder) < remainder)
1035 send_file_name(f, flist, fbuf, 0);
1037 io_error |= IOERR_GENERAL;
1039 "cannot send long-named file %s\n",
1047 io_error |= IOERR_GENERAL;
1048 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1054 int i, end = flist->count - 1;
1055 for (i = start; i <= end; i++)
1056 send_if_directory(f, flist, flist->files[i], fbuf, len);
1060 struct file_list *send_file_list(int f, int argc, char *argv[])
1064 char *p, *dir, olddir[sizeof curr_dir];
1065 char lastpath[MAXPATHLEN] = "";
1066 struct file_list *flist;
1067 struct timeval start_tv, end_tv;
1071 if (show_filelist_p())
1072 start_filelist_progress("building file list");
1074 start_write = stats.total_written;
1075 gettimeofday(&start_tv, NULL);
1077 flist = flist_new(WITH_HLINK, "send_file_list");
1079 io_start_buffering_out();
1080 if (filesfrom_fd >= 0) {
1081 if (argv[0] && !push_dir(argv[0])) {
1082 rsyserr(FERROR, errno, "push_dir %s failed",
1083 full_fname(argv[0]));
1084 exit_cleanup(RERR_FILESELECT);
1090 char fbuf[MAXPATHLEN];
1095 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1097 sanitize_path(fbuf, fbuf, "", 0);
1101 strlcpy(fbuf, *argv++, MAXPATHLEN);
1103 sanitize_path(fbuf, fbuf, "", 0);
1107 if (!len || fbuf[len - 1] == '/') {
1108 if (len == 2 && fbuf[0] == '.') {
1109 /* Turn "./" into just "." rather than "./." */
1112 if (len + 1 >= MAXPATHLEN)
1113 overflow_exit("send_file_list");
1118 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1119 && (len == 2 || fbuf[len-3] == '/')) {
1120 if (len + 2 >= MAXPATHLEN)
1121 overflow_exit("send_file_list");
1127 is_dot_dir = fbuf[len-1] == '.'
1128 && (len == 1 || fbuf[len-2] == '/');
1131 if (link_stat(fbuf, &st, keep_dirlinks) != 0) {
1132 io_error |= IOERR_GENERAL;
1133 rsyserr(FERROR, errno, "link_stat %s failed",
1138 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1139 rprintf(FINFO, "skipping directory %s\n",
1147 if (!relative_paths) {
1148 p = strrchr(fbuf, '/');
1155 len -= p - fbuf + 1;
1159 } else if ((p = strstr(fbuf, "/./")) != NULL) {
1165 len -= p - fbuf + 3;
1176 static char *lastdir;
1177 static int lastdir_len;
1179 strlcpy(olddir, curr_dir, sizeof olddir);
1181 if (!push_dir(dir)) {
1182 io_error |= IOERR_GENERAL;
1183 rsyserr(FERROR, errno, "push_dir %s failed",
1188 if (lastdir && strcmp(lastdir, dir) == 0) {
1189 flist_dir = lastdir;
1190 flist_dir_len = lastdir_len;
1192 flist_dir = lastdir = strdup(dir);
1193 flist_dir_len = lastdir_len = strlen(dir);
1198 memmove(fbuf, fn, len + 1);
1200 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1201 /* Send the implied directories at the start of the
1202 * source spec, so we get their permissions right. */
1203 char *lp = lastpath, *slash = fbuf;
1205 /* Skip any initial directories in our path that we
1206 * have in common with lastpath. */
1207 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1212 if (fn != p || (*lp && *lp != '/')) {
1213 int save_copy_links = copy_links;
1214 int save_xfer_dirs = xfer_dirs;
1215 copy_links = copy_unsafe_links;
1217 while ((slash = strchr(slash+1, '/')) != 0) {
1219 send_file_name(f, flist, fbuf, 0);
1222 copy_links = save_copy_links;
1223 xfer_dirs = save_xfer_dirs;
1225 strlcpy(lastpath, fbuf, sizeof lastpath);
1230 if (one_file_system)
1231 filesystem_dev = st.st_dev;
1233 if (recurse || (xfer_dirs && is_dot_dir)) {
1234 struct file_struct *file;
1235 if ((file = send_file_name(f, flist, fbuf, XMIT_TOP_DIR)))
1236 send_if_directory(f, flist, file, fbuf, len);
1238 send_file_name(f, flist, fbuf, 0);
1243 if (!pop_dir(olddir)) {
1244 rsyserr(FERROR, errno, "pop_dir %s failed",
1245 full_fname(olddir));
1246 exit_cleanup(RERR_FILESELECT);
1251 gettimeofday(&end_tv, NULL);
1252 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1253 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1254 if (stats.flist_buildtime == 0)
1255 stats.flist_buildtime = 1;
1258 send_file_entry(NULL, f, 0);
1260 if (show_filelist_p())
1261 finish_filelist_progress(flist);
1263 gettimeofday(&end_tv, NULL);
1264 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1265 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1267 if (flist->hlink_pool) {
1268 pool_destroy(flist->hlink_pool);
1269 flist->hlink_pool = NULL;
1272 /* Sort the list without removing any duplicates. This allows the
1273 * receiving side to ask for any name they like, which gives us the
1274 * flexibility to change the way we unduplicate names in the future
1275 * without causing a compatibility problem with older versions. */
1276 clean_flist(flist, 0, 0);
1278 /* Now send the uid/gid list. This was introduced in
1279 * protocol version 15 */
1282 /* send the io_error flag */
1283 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1286 stats.flist_size = stats.total_written - start_write;
1287 stats.num_files = flist->count;
1290 output_flist(flist);
1293 rprintf(FINFO, "send_file_list done\n");
1298 struct file_list *recv_file_list(int f)
1300 struct file_list *flist;
1301 unsigned short flags;
1304 if (show_filelist_p())
1305 start_filelist_progress("receiving file list");
1307 start_read = stats.total_read;
1309 flist = flist_new(WITH_HLINK, "recv_file_list");
1312 flist->malloced = 1000;
1313 flist->files = new_array(struct file_struct *, flist->malloced);
1317 while ((flags = read_byte(f)) != 0) {
1318 struct file_struct *file;
1320 flist_expand(flist);
1322 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1323 flags |= read_byte(f) << 8;
1324 file = receive_file_entry(flist, flags, f);
1326 if (S_ISREG(file->mode))
1327 stats.total_size += file->length;
1329 flist->files[flist->count++] = file;
1331 maybe_emit_filelist_progress(flist->count);
1334 rprintf(FINFO, "recv_file_name(%s)\n",
1335 safe_fname(f_name(file)));
1338 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1341 rprintf(FINFO, "received %d names\n", flist->count);
1343 if (show_filelist_p())
1344 finish_filelist_progress(flist);
1346 clean_flist(flist, relative_paths, 1);
1349 /* Now send the uid/gid list. This was introduced in
1350 * protocol version 15 */
1351 recv_uid_list(f, flist);
1353 /* Recv the io_error flag */
1354 if (lp_ignore_errors(module_id) || ignore_errors)
1357 io_error |= read_int(f);
1361 output_flist(flist);
1365 for (i = 0; i < flist->count; i++)
1366 list_file_entry(flist->files[i]);
1370 rprintf(FINFO, "recv_file_list done\n");
1372 stats.flist_size = stats.total_read - start_read;
1373 stats.num_files = flist->count;
1378 out_of_memory("recv_file_list");
1379 return NULL; /* not reached */
1382 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1384 return f_name_cmp(*file1, *file2);
1387 /* Search for an identically-named item in the file list. Note that the
1388 * items must agree in their directory-ness, or no match is returned. */
1389 int flist_find(struct file_list *flist, struct file_struct *f)
1391 int low = flist->low, high = flist->high;
1392 int ret, mid, mid_up;
1394 while (low <= high) {
1395 mid = (low + high) / 2;
1396 for (mid_up = mid; !flist->files[mid_up]->basename; mid_up++) {}
1398 ret = f_name_cmp(flist->files[mid_up], f);
1402 if (protocol_version < 29
1403 && S_ISDIR(flist->files[mid_up]->mode)
1404 != S_ISDIR(f->mode))
1417 * Free up any resources a file_struct has allocated
1418 * and clear the file.
1420 void clear_file(int i, struct file_list *flist)
1422 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1423 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1424 memset(flist->files[i], 0, file_struct_len);
1428 * allocate a new file list
1430 struct file_list *flist_new(int with_hlink, char *msg)
1432 struct file_list *flist;
1434 flist = new(struct file_list);
1438 memset(flist, 0, sizeof (struct file_list));
1440 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1441 out_of_memory, POOL_INTERN)))
1444 #ifdef SUPPORT_HARD_LINKS
1445 if (with_hlink && preserve_hard_links) {
1446 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1447 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1456 * free up all elements in a flist
1458 void flist_free(struct file_list *flist)
1460 pool_destroy(flist->file_pool);
1461 pool_destroy(flist->hlink_pool);
1467 * This routine ensures we don't have any duplicate names in our file list.
1468 * duplicate names can cause corruption because of the pipelining
1470 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1476 if (flist->count == 0) {
1481 sorting_flist = flist;
1482 qsort(flist->files, flist->count,
1483 sizeof flist->files[0], (int (*)())file_compare);
1484 sorting_flist = NULL;
1486 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1487 if (flist->files[i]->basename) {
1492 flist->low = prev_i;
1493 while (++i < flist->count) {
1495 struct file_struct *file = flist->files[i];
1497 if (!file->basename)
1499 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1501 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1502 int save_mode = file->mode;
1503 /* Make sure that this directory doesn't duplicate a
1504 * non-directory earlier in the list. */
1505 flist->high = prev_i;
1506 file->mode = S_IFREG;
1507 j = flist_find(flist, file);
1508 file->mode = save_mode;
1512 struct file_struct *fp = flist->files[j];
1514 /* If one is a dir and the other is not, we want to
1515 * keep the dir because it might have contents in the
1517 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1518 if (S_ISDIR(file->mode))
1524 if (verbose > 1 && !am_server) {
1526 "removing duplicate name %s from file list (%d)\n",
1527 safe_fname(f_name(file)), drop);
1529 /* Make sure that if we unduplicate '.', that we don't
1530 * lose track of a user-specified top directory. */
1531 flist->files[keep]->flags |= flist->files[drop]->flags
1532 & (FLAG_TOP_DIR|FLAG_DEL_HERE);
1534 clear_file(drop, flist);
1537 if (flist->low == drop) {
1539 j < i && !flist->files[j]->basename;
1548 flist->high = no_dups ? prev_i : flist->count - 1;
1551 /* We need to strip off the leading slashes for relative
1552 * paths, but this must be done _after_ the sorting phase. */
1553 for (i = flist->low; i <= flist->high; i++) {
1554 struct file_struct *file = flist->files[i];
1558 if (*file->dirname == '/') {
1559 char *s = file->dirname + 1;
1560 while (*s == '/') s++;
1561 memmove(file->dirname, s, strlen(s) + 1);
1564 if (!*file->dirname)
1565 file->dirname = NULL;
1570 static void output_flist(struct file_list *flist)
1572 char uidbuf[16], gidbuf[16], depthbuf[16];
1573 struct file_struct *file;
1574 const char *who = who_am_i();
1577 for (i = 0; i < flist->count; i++) {
1578 file = flist->files[i];
1579 if ((am_root || am_sender) && preserve_uid)
1580 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1583 if (preserve_gid && file->gid != GID_NONE)
1584 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1588 sprintf(depthbuf, "%d", file->dir.depth);
1589 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1590 who, i, am_sender ? NS(file->dir.root) : depthbuf,
1591 file->dirname ? safe_fname(file->dirname) : "",
1592 file->dirname ? "/" : "", NS(file->basename),
1593 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
1594 (double)file->length, uidbuf, gidbuf, file->flags);
1598 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1599 enum fnc_type { t_PATH, t_ITEM };
1601 /* Compare the names of two file_struct entities, similar to how strcmp()
1602 * would do if it were operating on the joined strings.
1604 * Some differences beginning with protocol_version 29: (1) directory names
1605 * are compared with an assumed trailing slash so that they compare in a
1606 * way that would cause them to sort immediately prior to any content they
1607 * may have; (2) a directory of any name compares after a non-directory of
1608 * any name at the same depth; (3) a directory with name "." compares prior
1609 * to anything else. These changes mean that a directory and a non-dir
1610 * with the same name will not compare as equal (protocol_version >= 29).
1612 * The dirname component can be an empty string, but the basename component
1613 * cannot (and never is in the current codebase). The basename component
1614 * may be NULL (for a removed item), in which case it is considered to be
1615 * after any existing item. */
1616 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1619 const uchar *c1, *c2;
1620 enum fnc_state state1, state2;
1621 enum fnc_type type1, type2;
1622 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1624 if (!f1 || !f1->basename) {
1625 if (!f2 || !f2->basename)
1629 if (!f2 || !f2->basename)
1632 c1 = (uchar*)f1->dirname;
1633 c2 = (uchar*)f2->dirname;
1637 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1638 c1 = (uchar*)f1->basename;
1639 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1641 state1 = s_TRAILING;
1654 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1655 c2 = (uchar*)f2->basename;
1656 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1658 state2 = s_TRAILING;
1672 return type1 == t_PATH ? 1 : -1;
1675 if ((dif = (int)*c1++ - (int)*c2++) != 0)
1684 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1685 c1 = (uchar*)f1->basename;
1686 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1688 state1 = s_TRAILING;
1694 state1 = s_TRAILING;
1695 if (type1 == t_PATH) {
1704 if (*c2 && type1 != type2)
1705 return type1 == t_PATH ? 1 : -1;
1714 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1715 c2 = (uchar*)f2->basename;
1716 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1718 state2 = s_TRAILING;
1724 state2 = s_TRAILING;
1725 if (type2 == t_PATH) {
1737 return type1 == t_PATH ? 1 : -1;
1744 /* Return a copy of the full filename of a flist entry, using the indicated
1745 * buffer. No size-checking is done because we checked the size when creating
1746 * the file_struct entry.
1748 char *f_name_to(struct file_struct *f, char *fbuf)
1750 if (!f || !f->basename)
1754 int len = strlen(f->dirname);
1755 memcpy(fbuf, f->dirname, len);
1757 strcpy(fbuf + len + 1, f->basename);
1759 strcpy(fbuf, f->basename);
1763 /* Like f_name_to(), but we rotate through 5 static buffers of our own. */
1764 char *f_name(struct file_struct *f)
1766 static char names[5][MAXPATHLEN];
1767 static unsigned int n;
1769 n = (n + 1) % (sizeof names / sizeof names[0]);
1771 return f_name_to(f, names[n]);
1774 /* Do a non-recursive scan of the named directory, possibly ignoring all
1775 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1776 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1777 * buffer (the functions we call will append names onto the end, but the old
1778 * dir value will be restored on exit). */
1779 struct file_list *get_dirlist(char *dirname, int dlen,
1780 int ignore_filter_rules)
1782 struct file_list *dirlist;
1783 char dirbuf[MAXPATHLEN];
1784 int save_recurse = recurse;
1787 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1788 if (dlen >= MAXPATHLEN)
1793 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1796 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
1797 recurse = save_recurse;
1799 flist_count_offset += dirlist->count;
1801 clean_flist(dirlist, 0, 0);
1804 output_flist(dirlist);