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;
78 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
79 static void output_flist(struct file_list *flist);
85 /* Figure out how big the file_struct is without trailing padding */
86 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
87 checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
90 static int show_filelist_p(void)
92 return verbose && xfer_dirs && !am_server;
95 static void start_filelist_progress(char *kind)
97 rprintf(FINFO, "%s ... ", kind);
98 if (verbose > 1 || do_progress)
103 static void emit_filelist_progress(int count)
105 rprintf(FINFO, " %d files...\r", count);
108 static void maybe_emit_filelist_progress(int count)
110 if (do_progress && show_filelist_p() && (count % 100) == 0)
111 emit_filelist_progress(count);
114 static void finish_filelist_progress(const struct file_list *flist)
117 /* This overwrites the progress line */
118 rprintf(FINFO, "%d file%sto consider\n",
119 flist->count, flist->count == 1 ? " " : "s ");
121 rprintf(FINFO, "done\n");
124 void show_flist_stats(void)
129 static void list_file_entry(struct file_struct *f)
134 /* this can happen if duplicate names were removed */
138 permstring(perms, f->mode);
141 if (preserve_links && S_ISLNK(f->mode)) {
142 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
144 (double)f->length, timestring(f->modtime),
145 f_name(f, NULL), f->u.link);
149 rprintf(FINFO, "%s %11.0f %s %s\n",
151 (double)f->length, timestring(f->modtime),
157 * Stat either a symlink or its referent, depending on the settings of
158 * copy_links, copy_unsafe_links, etc.
160 * @retval -1 on error
162 * @retval 0 for success
164 * @post If @p path is a symlink, then @p linkbuf (of size @c
165 * MAXPATHLEN) contains the symlink target.
167 * @post @p buffer contains information about the link or the
168 * referrent as appropriate, if they exist.
170 static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
174 return do_stat(path, buffer);
175 if (link_stat(path, buffer, 0) < 0)
177 if (S_ISLNK(buffer->st_mode)) {
178 int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1);
182 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
184 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
187 return do_stat(path, buffer);
192 return do_stat(path, buffer);
196 int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks)
200 return do_stat(path, buffer);
201 if (do_lstat(path, buffer) < 0)
203 if (follow_dirlinks && S_ISLNK(buffer->st_mode)) {
205 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
210 return do_stat(path, buffer);
214 /* This function is used to check if a file should be included/excluded
215 * from the list of files based on its name and type etc. The value of
216 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
217 static int is_excluded(char *fname, int is_dir, int filter_level)
219 #if 0 /* This currently never happens, so avoid a useless compare. */
220 if (filter_level == NO_FILTERS)
224 /* never exclude '.', even if somebody does --exclude '*' */
225 if (fname[0] == '.' && !fname[1])
227 /* Handle the -R version of the '.' dir. */
228 if (fname[0] == '/') {
229 int len = strlen(fname);
230 if (fname[len-1] == '.' && fname[len-2] == '/')
234 if (server_filter_list.head
235 && check_filter(&server_filter_list, fname, is_dir) < 0)
237 if (filter_level != ALL_FILTERS)
240 && check_filter(&filter_list, fname, is_dir) < 0)
245 static int to_wire_mode(mode_t mode)
248 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
249 return (mode & ~(_S_IFMT)) | 0120000;
254 static mode_t from_wire_mode(int mode)
256 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
257 return (mode & ~(_S_IFMT)) | _S_IFLNK;
261 static void send_directory(int f, struct file_list *flist,
262 char *fbuf, int len);
264 static char *flist_dir;
265 static int flist_dir_len;
269 * Make sure @p flist is big enough to hold at least @p flist->count
272 void flist_expand(struct file_list *flist)
274 struct file_struct **new_ptr;
276 if (flist->count < flist->malloced)
279 if (flist->malloced < FLIST_START)
280 flist->malloced = FLIST_START;
281 else if (flist->malloced >= FLIST_LINEAR)
282 flist->malloced += FLIST_LINEAR;
284 flist->malloced *= 2;
287 * In case count jumped or we are starting the list
288 * with a known size just set it.
290 if (flist->malloced < flist->count)
291 flist->malloced = flist->count;
293 new_ptr = realloc_array(flist->files, struct file_struct *,
296 if (verbose >= 2 && flist->malloced != FLIST_START) {
297 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
299 (double)sizeof flist->files[0] * flist->malloced,
300 (new_ptr == flist->files) ? " not" : "");
303 flist->files = new_ptr;
306 out_of_memory("flist_expand");
309 static void send_file_entry(struct file_struct *file, int f)
311 unsigned short flags;
312 static time_t modtime;
316 static uint32 rdev_major;
319 static char lastname[MAXPATHLEN];
320 char fname[MAXPATHLEN];
328 modtime = 0, mode = 0;
329 dev = 0, rdev = makedev(0, 0);
336 io_write_phase = "send_file_entry";
340 flags = file->flags & XMIT_TOP_DIR;
342 if (file->mode == mode)
343 flags |= XMIT_SAME_MODE;
346 if (preserve_devices) {
347 if (protocol_version < 28) {
348 if (IS_DEVICE(mode)) {
349 if (file->u.rdev == rdev)
350 flags |= XMIT_SAME_RDEV_pre28;
354 rdev = makedev(0, 0);
355 } else if (IS_DEVICE(mode)) {
357 if ((uint32)major(rdev) == rdev_major)
358 flags |= XMIT_SAME_RDEV_MAJOR;
360 rdev_major = major(rdev);
361 if ((uint32)minor(rdev) <= 0xFFu)
362 flags |= XMIT_RDEV_MINOR_IS_SMALL;
365 if (file->uid == uid)
366 flags |= XMIT_SAME_UID;
369 if (file->gid == gid)
370 flags |= XMIT_SAME_GID;
373 if (file->modtime == modtime)
374 flags |= XMIT_SAME_TIME;
376 modtime = file->modtime;
378 #ifdef SUPPORT_HARD_LINKS
379 if (file->link_u.idev) {
380 if (file->F_DEV == dev) {
381 if (protocol_version >= 28)
382 flags |= XMIT_SAME_DEV;
385 flags |= XMIT_HAS_IDEV_DATA;
390 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
392 l2 = strlen(fname+l1);
395 flags |= XMIT_SAME_NAME;
397 flags |= XMIT_LONG_NAME;
399 /* We must make sure we don't send a zero flag byte or the
400 * other end will terminate the flist transfer. Note that
401 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
402 * it's harmless way to add a bit to the first flag byte. */
403 if (protocol_version >= 28) {
404 if (!flags && !S_ISDIR(mode))
405 flags |= XMIT_TOP_DIR;
406 if ((flags & 0xFF00) || !flags) {
407 flags |= XMIT_EXTENDED_FLAGS;
408 write_byte(f, flags);
409 write_byte(f, flags >> 8);
411 write_byte(f, flags);
414 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
415 write_byte(f, flags);
417 if (flags & XMIT_SAME_NAME)
419 if (flags & XMIT_LONG_NAME)
423 write_buf(f, fname + l1, l2);
425 write_longint(f, file->length);
426 if (!(flags & XMIT_SAME_TIME))
427 write_int(f, modtime);
428 if (!(flags & XMIT_SAME_MODE))
429 write_int(f, to_wire_mode(mode));
430 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
435 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
440 if (preserve_devices && IS_DEVICE(mode)) {
441 if (protocol_version < 28) {
442 if (!(flags & XMIT_SAME_RDEV_pre28))
443 write_int(f, (int)rdev);
445 if (!(flags & XMIT_SAME_RDEV_MAJOR))
446 write_int(f, major(rdev));
447 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
448 write_byte(f, minor(rdev));
450 write_int(f, minor(rdev));
455 if (preserve_links && S_ISLNK(mode)) {
456 int len = strlen(file->u.link);
458 write_buf(f, file->u.link, len);
462 #ifdef SUPPORT_HARD_LINKS
463 if (flags & XMIT_HAS_IDEV_DATA) {
464 if (protocol_version < 26) {
465 /* 32-bit dev_t and ino_t */
467 write_int(f, file->F_INODE);
469 /* 64-bit dev_t and ino_t */
470 if (!(flags & XMIT_SAME_DEV))
471 write_longint(f, dev);
472 write_longint(f, file->F_INODE);
477 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
482 /* Prior to 28, we sent a useless set of nulls. */
485 write_buf(f, sum, checksum_len);
488 strlcpy(lastname, fname, MAXPATHLEN);
490 io_write_phase = "unknown";
493 static struct file_struct *receive_file_entry(struct file_list *flist,
494 unsigned short flags, int f)
496 static time_t modtime;
500 static uint32 rdev_major;
503 static char lastname[MAXPATHLEN], *lastdir;
504 static int lastdir_depth, lastdir_len = -1;
505 static unsigned int del_hier_name_len = 0;
506 static int in_del_hier = 0;
507 char thisname[MAXPATHLEN];
508 unsigned int l1 = 0, l2 = 0;
509 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
511 char *basename, *dirname, *bp;
512 struct file_struct *file;
515 modtime = 0, mode = 0;
516 dev = 0, rdev = makedev(0, 0);
525 if (flags & XMIT_SAME_NAME)
528 if (flags & XMIT_LONG_NAME)
533 if (l2 >= MAXPATHLEN - l1) {
535 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
536 flags, l1, l2, lastname);
537 overflow_exit("receive_file_entry");
540 strlcpy(thisname, lastname, l1 + 1);
541 read_sbuf(f, &thisname[l1], l2);
542 thisname[l1 + l2] = 0;
544 strlcpy(lastname, thisname, MAXPATHLEN);
546 clean_fname(thisname, 0);
549 sanitize_path(thisname, thisname, "", 0);
551 if ((basename = strrchr(thisname, '/')) != NULL) {
552 dirname_len = ++basename - thisname; /* counts future '\0' */
553 if (lastdir_len == dirname_len - 1
554 && strncmp(thisname, lastdir, lastdir_len) == 0) {
556 dirname_len = 0; /* indicates no copy is needed */
564 basename_len = strlen(basename) + 1; /* count the '\0' */
566 file_length = read_longint(f);
567 if (!(flags & XMIT_SAME_TIME))
568 modtime = (time_t)read_int(f);
569 if (!(flags & XMIT_SAME_MODE))
570 mode = from_wire_mode(read_int(f));
572 if (chmod_modes && (S_ISREG(mode) || S_ISDIR(mode)))
573 mode = tweak_mode(mode, chmod_modes);
575 if (preserve_uid && !(flags & XMIT_SAME_UID))
576 uid = (uid_t)read_int(f);
577 if (preserve_gid && !(flags & XMIT_SAME_GID))
578 gid = (gid_t)read_int(f);
580 if (preserve_devices) {
581 if (protocol_version < 28) {
582 if (IS_DEVICE(mode)) {
583 if (!(flags & XMIT_SAME_RDEV_pre28))
584 rdev = (dev_t)read_int(f);
586 rdev = makedev(0, 0);
587 } else if (IS_DEVICE(mode)) {
589 if (!(flags & XMIT_SAME_RDEV_MAJOR))
590 rdev_major = read_int(f);
591 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
592 rdev_minor = read_byte(f);
594 rdev_minor = read_int(f);
595 rdev = makedev(rdev_major, rdev_minor);
600 if (preserve_links && S_ISLNK(mode)) {
601 linkname_len = read_int(f) + 1; /* count the '\0' */
602 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
603 rprintf(FERROR, "overflow: linkname_len=%d\n",
605 overflow_exit("receive_file_entry");
612 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
614 alloc_len = file_struct_len + dirname_len + basename_len
615 + linkname_len + sum_len;
616 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
618 file = (struct file_struct *)bp;
619 memset(bp, 0, file_struct_len);
620 bp += file_struct_len;
623 file->modtime = modtime;
624 file->length = file_length;
630 file->dirname = lastdir = bp;
631 lastdir_len = dirname_len - 1;
632 memcpy(bp, dirname, dirname_len - 1);
635 lastdir_depth = count_dir_elements(lastdir);
636 file->dir.depth = lastdir_depth + 1;
637 } else if (dirname) {
638 file->dirname = dirname; /* we're reusing lastname */
639 file->dir.depth = lastdir_depth + 1;
644 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
646 if (flags & XMIT_TOP_DIR) {
647 in_del_hier = recurse;
648 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
649 if (relative_paths && del_hier_name_len > 2
650 && basename_len == 1+1 && *basename == '.')
651 del_hier_name_len -= 2;
652 file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
653 } else if (in_del_hier) {
654 if (!relative_paths || !del_hier_name_len
655 || (l1 >= del_hier_name_len
656 && thisname[del_hier_name_len] == '/'))
657 file->flags |= FLAG_DEL_HERE;
664 memcpy(bp, basename, basename_len);
667 if (preserve_devices && IS_DEVICE(mode))
673 read_sbuf(f, bp, linkname_len - 1);
675 sanitize_path(bp, bp, "", lastdir_depth);
680 #ifdef SUPPORT_HARD_LINKS
681 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
682 flags |= XMIT_HAS_IDEV_DATA;
683 if (flags & XMIT_HAS_IDEV_DATA) {
685 if (protocol_version < 26) {
689 if (!(flags & XMIT_SAME_DEV))
690 dev = read_longint(f);
691 inode = read_longint(f);
693 if (flist->hlink_pool) {
694 file->link_u.idev = pool_talloc(flist->hlink_pool,
695 struct idev, 1, "inode_table");
696 file->F_INODE = inode;
702 if (always_checksum && (sum_len || protocol_version < 28)) {
705 file->u.sum = sum = bp;
708 /* Prior to 28, we get a useless set of nulls. */
711 read_buf(f, sum, checksum_len);
714 if (!preserve_perms) {
715 /* set an appropriate set of permissions based on original
716 * permissions and umask. This emulates what GNU cp does */
717 file->mode &= ~orig_umask;
724 * Create a file_struct for a named file by reading its stat()
725 * information and performing extensive checks against global
728 * @return the new file, or NULL if there was an error or this file
729 * should be excluded.
731 * @todo There is a small optimization opportunity here to avoid
732 * stat()ing the file in some circumstances, which has a certain cost.
733 * We are called immediately after doing readdir(), and so we may
734 * already know the d_type of the file. We could for example avoid
735 * statting directories if we're not recursing, but this is not a very
736 * important case. Some systems may not have d_type.
738 struct file_struct *make_file(char *fname, struct file_list *flist,
739 STRUCT_STAT *stp, unsigned short flags,
742 static char *lastdir;
743 static int lastdir_len = -1;
744 struct file_struct *file;
746 char sum[SUM_LENGTH];
747 char thisname[MAXPATHLEN];
748 char linkname[MAXPATHLEN];
749 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
750 char *basename, *dirname, *bp;
752 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
755 if (strlcpy(thisname, fname, sizeof thisname)
756 >= sizeof thisname - flist_dir_len) {
757 rprintf(FINFO, "skipping overly long name: %s\n", fname);
760 clean_fname(thisname, 0);
762 sanitize_path(thisname, thisname, "", 0);
764 memset(sum, 0, SUM_LENGTH);
766 if (stp && S_ISDIR(stp->st_mode))
767 st = *stp; /* Needed for "symlink/." with --relative. */
768 else if (readlink_stat(thisname, &st, linkname) != 0) {
769 int save_errno = errno;
770 /* See if file is excluded before reporting an error. */
771 if (filter_level != NO_FILTERS
772 && is_excluded(thisname, 0, filter_level))
774 if (save_errno == ENOENT) {
776 /* Avoid "vanished" error if symlink points nowhere. */
777 if (copy_links && do_lstat(thisname, &st) == 0
778 && S_ISLNK(st.st_mode)) {
779 io_error |= IOERR_GENERAL;
780 rprintf(FERROR, "symlink has no referent: %s\n",
781 full_fname(thisname));
785 enum logcode c = am_daemon && protocol_version < 28
787 io_error |= IOERR_VANISHED;
788 rprintf(c, "file has vanished: %s\n",
789 full_fname(thisname));
792 io_error |= IOERR_GENERAL;
793 rsyserr(FERROR, save_errno, "readlink %s failed",
794 full_fname(thisname));
799 /* backup.c calls us with filter_level set to NO_FILTERS. */
800 if (filter_level == NO_FILTERS)
803 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
804 rprintf(FINFO, "skipping directory %s\n", thisname);
808 /* We only care about directories because we need to avoid recursing
809 * into a mount-point directory, not to avoid copying a symlinked
810 * file if -L (or similar) was specified. */
811 if (one_file_system && st.st_dev != filesystem_dev
812 && S_ISDIR(st.st_mode)) {
813 if (one_file_system > 1)
815 flags |= FLAG_MOUNT_POINT;
818 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
821 if (lp_ignore_nonreadable(module_id)) {
823 if (!S_ISLNK(st.st_mode))
825 if (access(thisname, R_OK) != 0)
832 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
833 who_am_i(), thisname, filter_level);
836 if ((basename = strrchr(thisname, '/')) != NULL) {
837 dirname_len = ++basename - thisname; /* counts future '\0' */
838 if (lastdir_len == dirname_len - 1
839 && strncmp(thisname, lastdir, lastdir_len) == 0) {
841 dirname_len = 0; /* indicates no copy is needed */
849 basename_len = strlen(basename) + 1; /* count the '\0' */
852 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
857 sum_len = always_checksum && am_sender && S_ISREG(st.st_mode)
858 ? MD4_SUM_LENGTH : 0;
860 alloc_len = file_struct_len + dirname_len + basename_len
861 + linkname_len + sum_len;
863 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
865 if (!(bp = new_array(char, alloc_len)))
866 out_of_memory("make_file");
869 file = (struct file_struct *)bp;
870 memset(bp, 0, file_struct_len);
871 bp += file_struct_len;
874 file->modtime = st.st_mtime;
875 file->length = st.st_size;
876 file->mode = st.st_mode;
877 file->uid = st.st_uid;
878 file->gid = st.st_gid;
880 #ifdef SUPPORT_HARD_LINKS
881 if (flist && flist->hlink_pool) {
882 if (protocol_version < 28) {
883 if (S_ISREG(st.st_mode))
884 file->link_u.idev = pool_talloc(
885 flist->hlink_pool, struct idev, 1,
888 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
889 file->link_u.idev = pool_talloc(
890 flist->hlink_pool, struct idev, 1,
894 if (file->link_u.idev) {
895 file->F_DEV = st.st_dev;
896 file->F_INODE = st.st_ino;
901 file->dirname = lastdir = bp;
902 lastdir_len = dirname_len - 1;
903 memcpy(bp, dirname, dirname_len - 1);
907 file->dirname = dirname;
910 memcpy(bp, basename, basename_len);
913 #ifdef HAVE_STRUCT_STAT_ST_RDEV
914 if (preserve_devices && IS_DEVICE(st.st_mode))
915 file->u.rdev = st.st_rdev;
921 memcpy(bp, linkname, linkname_len);
928 file_checksum(thisname, bp, st.st_size);
932 file->dir.root = flist_dir;
934 /* This code is only used by the receiver when it is building
935 * a list of files for a delete pass. */
936 if (keep_dirlinks && linkname_len && flist) {
938 int save_mode = file->mode;
939 file->mode = S_IFDIR; /* Find a directory with our name. */
940 if (flist_find(the_file_list, file) >= 0
941 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
942 file->modtime = st2.st_mtime;
943 file->length = st2.st_size;
944 file->mode = st2.st_mode;
945 file->uid = st2.st_uid;
946 file->gid = st2.st_gid;
949 file->mode = save_mode;
952 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
953 stats.total_size += st.st_size;
958 static struct file_struct *send_file_name(int f, struct file_list *flist,
959 char *fname, STRUCT_STAT *stp,
960 unsigned short flags)
962 struct file_struct *file;
964 file = make_file(fname, flist, stp, flags,
965 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
969 if (chmod_modes && (S_ISREG(file->mode) || S_ISDIR(file->mode)))
970 file->mode = tweak_mode(file->mode, chmod_modes);
972 maybe_emit_filelist_progress(flist->count + flist_count_offset);
976 if (file->basename[0]) {
977 flist->files[flist->count++] = file;
978 send_file_entry(file, f);
983 static void send_if_directory(int f, struct file_list *flist,
984 struct file_struct *file,
985 char *fbuf, unsigned int ol)
987 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
989 if (S_ISDIR(file->mode)
990 && !(file->flags & FLAG_MOUNT_POINT) && f_name(file, fbuf)) {
992 unsigned int len = strlen(fbuf);
993 if (len > 1 && fbuf[len-1] == '/')
995 if (len >= MAXPATHLEN - 1) {
996 io_error |= IOERR_GENERAL;
997 rprintf(FERROR, "skipping long-named directory: %s\n",
1001 save_filters = push_local_filters(fbuf, len);
1002 send_directory(f, flist, fbuf, len);
1003 pop_local_filters(save_filters);
1010 /* This function is normally called by the sender, but the receiving side also
1011 * calls it from get_dirlist() with f set to -1 so that we just construct the
1012 * file list in memory without sending it over the wire. Also, get_dirlist()
1013 * might call this with f set to -2, which also indicates that local filter
1014 * rules should be ignored. */
1015 static void send_directory(int f, struct file_list *flist,
1016 char *fbuf, int len)
1022 int start = flist->count;
1024 if (!(d = opendir(fbuf))) {
1025 io_error |= IOERR_GENERAL;
1026 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1031 if (len != 1 || *fbuf != '/')
1034 remainder = MAXPATHLEN - (p - fbuf);
1036 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1037 char *dname = d_name(di);
1038 if (dname[0] == '.' && (dname[1] == '\0'
1039 || (dname[1] == '.' && dname[2] == '\0')))
1041 if (strlcpy(p, dname, remainder) >= remainder) {
1042 io_error |= IOERR_GENERAL;
1044 "cannot send long-named file %s\n",
1049 send_file_name(f, flist, fbuf, NULL, 0);
1055 io_error |= IOERR_GENERAL;
1056 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1062 int i, end = flist->count - 1;
1063 for (i = start; i <= end; i++)
1064 send_if_directory(f, flist, flist->files[i], fbuf, len);
1068 struct file_list *send_file_list(int f, int argc, char *argv[])
1072 char *p, *dir, olddir[sizeof curr_dir];
1073 char lastpath[MAXPATHLEN] = "";
1074 struct file_list *flist;
1075 struct timeval start_tv, end_tv;
1079 if (show_filelist_p())
1080 start_filelist_progress("building file list");
1082 start_write = stats.total_written;
1083 gettimeofday(&start_tv, NULL);
1085 flist = flist_new(WITH_HLINK, "send_file_list");
1087 io_start_buffering_out();
1088 if (filesfrom_fd >= 0) {
1089 if (argv[0] && !push_dir(argv[0])) {
1090 rsyserr(FERROR, errno, "push_dir %s failed",
1091 full_fname(argv[0]));
1092 exit_cleanup(RERR_FILESELECT);
1098 char fbuf[MAXPATHLEN];
1103 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1105 sanitize_path(fbuf, fbuf, "", 0);
1109 strlcpy(fbuf, *argv++, MAXPATHLEN);
1111 sanitize_path(fbuf, fbuf, "", 0);
1115 if (relative_paths) {
1116 /* We clean up fbuf below. */
1118 } else if (!len || fbuf[len - 1] == '/') {
1119 if (len == 2 && fbuf[0] == '.') {
1120 /* Turn "./" into just "." rather than "./." */
1123 if (len + 1 >= MAXPATHLEN)
1124 overflow_exit("send_file_list");
1129 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1130 && (len == 2 || fbuf[len-3] == '/')) {
1131 if (len + 2 >= MAXPATHLEN)
1132 overflow_exit("send_file_list");
1138 is_dot_dir = fbuf[len-1] == '.'
1139 && (len == 1 || fbuf[len-2] == '/');
1142 if (link_stat(fbuf, &st, keep_dirlinks) != 0) {
1143 io_error |= IOERR_GENERAL;
1144 rsyserr(FERROR, errno, "link_stat %s failed",
1149 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1150 rprintf(FINFO, "skipping directory %s\n", fbuf);
1157 if (!relative_paths) {
1158 p = strrchr(fbuf, '/');
1165 len -= p - fbuf + 1;
1170 if ((p = strstr(fbuf, "/./")) != NULL) {
1176 len -= p - fbuf + 3;
1180 /* Get rid of trailing "/" and "/.". */
1182 if (fn[len - 1] == '/') {
1184 if (!--len && !dir) {
1189 else if (len >= 2 && fn[len - 1] == '.'
1190 && fn[len - 2] == '/') {
1192 if (!(len -= 2) && !dir) {
1200 /* Reject a ".." dir in the active part of the path. */
1201 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1202 if ((p[2] == '/' || p[2] == '\0')
1203 && (p == fn || p[-1] == '/')) {
1205 "found \"..\" dir in relative path: %s\n",
1207 exit_cleanup(RERR_SYNTAX);
1218 static char *lastdir;
1219 static int lastdir_len;
1221 strlcpy(olddir, curr_dir, sizeof olddir);
1223 if (!push_dir(dir)) {
1224 io_error |= IOERR_GENERAL;
1225 rsyserr(FERROR, errno, "push_dir %s failed",
1230 if (lastdir && strcmp(lastdir, dir) == 0) {
1231 flist_dir = lastdir;
1232 flist_dir_len = lastdir_len;
1234 flist_dir = lastdir = strdup(dir);
1235 flist_dir_len = lastdir_len = strlen(dir);
1240 memmove(fbuf, fn, len + 1);
1242 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1243 /* Send the implied directories at the start of the
1244 * source spec, so we get their permissions right. */
1245 char *lp = lastpath, *slash = fbuf;
1247 /* Skip any initial directories in our path that we
1248 * have in common with lastpath. */
1249 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1254 if (fn != p || (*lp && *lp != '/')) {
1255 int save_copy_links = copy_links;
1256 int save_xfer_dirs = xfer_dirs;
1257 copy_links = copy_unsafe_links;
1259 while ((slash = strchr(slash+1, '/')) != 0) {
1261 send_file_name(f, flist, fbuf, NULL, 0);
1264 copy_links = save_copy_links;
1265 xfer_dirs = save_xfer_dirs;
1267 strlcpy(lastpath, fbuf, sizeof lastpath);
1272 if (one_file_system)
1273 filesystem_dev = st.st_dev;
1275 if (recurse || (xfer_dirs && is_dot_dir)) {
1276 struct file_struct *file;
1277 file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
1279 send_if_directory(f, flist, file, fbuf, len);
1281 send_file_name(f, flist, fbuf, &st, 0);
1286 if (!pop_dir(olddir)) {
1287 rsyserr(FERROR, errno, "pop_dir %s failed",
1288 full_fname(olddir));
1289 exit_cleanup(RERR_FILESELECT);
1294 gettimeofday(&end_tv, NULL);
1295 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1296 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1297 if (stats.flist_buildtime == 0)
1298 stats.flist_buildtime = 1;
1301 send_file_entry(NULL, f);
1303 if (show_filelist_p())
1304 finish_filelist_progress(flist);
1306 gettimeofday(&end_tv, NULL);
1307 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1308 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1310 if (flist->hlink_pool) {
1311 pool_destroy(flist->hlink_pool);
1312 flist->hlink_pool = NULL;
1315 /* Sort the list without removing any duplicates. This allows the
1316 * receiving side to ask for any name they like, which gives us the
1317 * flexibility to change the way we unduplicate names in the future
1318 * without causing a compatibility problem with older versions. */
1319 clean_flist(flist, 0, 0);
1321 /* Now send the uid/gid list. This was introduced in
1322 * protocol version 15 */
1325 /* send the io_error flag */
1326 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1329 stats.flist_size = stats.total_written - start_write;
1330 stats.num_files = flist->count;
1333 output_flist(flist);
1336 rprintf(FINFO, "send_file_list done\n");
1341 struct file_list *recv_file_list(int f)
1343 struct file_list *flist;
1344 unsigned short flags;
1347 if (show_filelist_p())
1348 start_filelist_progress("receiving file list");
1350 start_read = stats.total_read;
1352 flist = flist_new(WITH_HLINK, "recv_file_list");
1355 flist->malloced = 1000;
1356 flist->files = new_array(struct file_struct *, flist->malloced);
1360 while ((flags = read_byte(f)) != 0) {
1361 struct file_struct *file;
1363 flist_expand(flist);
1365 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1366 flags |= read_byte(f) << 8;
1367 file = receive_file_entry(flist, flags, f);
1369 if (S_ISREG(file->mode))
1370 stats.total_size += file->length;
1372 flist->files[flist->count++] = file;
1374 maybe_emit_filelist_progress(flist->count);
1377 rprintf(FINFO, "recv_file_name(%s)\n",
1378 f_name(file, NULL));
1381 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1384 rprintf(FINFO, "received %d names\n", flist->count);
1386 if (show_filelist_p())
1387 finish_filelist_progress(flist);
1389 clean_flist(flist, relative_paths, 1);
1392 /* Now send the uid/gid list. This was introduced in
1393 * protocol version 15 */
1394 recv_uid_list(f, flist);
1396 /* Recv the io_error flag */
1397 if (lp_ignore_errors(module_id) || ignore_errors)
1400 io_error |= read_int(f);
1404 output_flist(flist);
1408 for (i = 0; i < flist->count; i++)
1409 list_file_entry(flist->files[i]);
1413 rprintf(FINFO, "recv_file_list done\n");
1415 stats.flist_size = stats.total_read - start_read;
1416 stats.num_files = flist->count;
1421 out_of_memory("recv_file_list");
1422 return NULL; /* not reached */
1425 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1427 return f_name_cmp(*file1, *file2);
1430 /* Search for an identically-named item in the file list. Note that the
1431 * items must agree in their directory-ness, or no match is returned. */
1432 int flist_find(struct file_list *flist, struct file_struct *f)
1434 int low = flist->low, high = flist->high;
1435 int ret, mid, mid_up;
1437 while (low <= high) {
1438 mid = (low + high) / 2;
1439 for (mid_up = mid; !flist->files[mid_up]->basename; mid_up++) {}
1441 ret = f_name_cmp(flist->files[mid_up], f);
1445 if (protocol_version < 29
1446 && S_ISDIR(flist->files[mid_up]->mode)
1447 != S_ISDIR(f->mode))
1460 * Free up any resources a file_struct has allocated
1461 * and clear the file.
1463 void clear_file(int i, struct file_list *flist)
1465 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1466 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1467 memset(flist->files[i], 0, file_struct_len);
1471 * allocate a new file list
1473 struct file_list *flist_new(int with_hlink, char *msg)
1475 struct file_list *flist;
1477 flist = new(struct file_list);
1481 memset(flist, 0, sizeof (struct file_list));
1483 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1484 out_of_memory, POOL_INTERN)))
1487 #ifdef SUPPORT_HARD_LINKS
1488 if (with_hlink && preserve_hard_links) {
1489 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1490 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1499 * free up all elements in a flist
1501 void flist_free(struct file_list *flist)
1503 pool_destroy(flist->file_pool);
1504 pool_destroy(flist->hlink_pool);
1510 * This routine ensures we don't have any duplicate names in our file list.
1511 * duplicate names can cause corruption because of the pipelining
1513 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1519 if (flist->count == 0) {
1524 qsort(flist->files, flist->count,
1525 sizeof flist->files[0], (int (*)())file_compare);
1527 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1528 if (flist->files[i]->basename) {
1533 flist->low = prev_i;
1534 while (++i < flist->count) {
1536 struct file_struct *file = flist->files[i];
1538 if (!file->basename)
1540 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1542 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1543 int save_mode = file->mode;
1544 /* Make sure that this directory doesn't duplicate a
1545 * non-directory earlier in the list. */
1546 flist->high = prev_i;
1547 file->mode = S_IFREG;
1548 j = flist_find(flist, file);
1549 file->mode = save_mode;
1553 struct file_struct *fp = flist->files[j];
1555 /* If one is a dir and the other is not, we want to
1556 * keep the dir because it might have contents in the
1558 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1559 if (S_ISDIR(file->mode))
1565 if (verbose > 1 && !am_server) {
1567 "removing duplicate name %s from file list (%d)\n",
1568 f_name(file, NULL), drop);
1570 /* Make sure that if we unduplicate '.', that we don't
1571 * lose track of a user-specified top directory. */
1572 flist->files[keep]->flags |= flist->files[drop]->flags
1573 & (FLAG_TOP_DIR|FLAG_DEL_HERE);
1575 clear_file(drop, flist);
1578 if (flist->low == drop) {
1580 j < i && !flist->files[j]->basename;
1589 flist->high = no_dups ? prev_i : flist->count - 1;
1592 /* We need to strip off the leading slashes for relative
1593 * paths, but this must be done _after_ the sorting phase. */
1594 for (i = flist->low; i <= flist->high; i++) {
1595 struct file_struct *file = flist->files[i];
1599 if (*file->dirname == '/') {
1600 char *s = file->dirname + 1;
1601 while (*s == '/') s++;
1602 memmove(file->dirname, s, strlen(s) + 1);
1605 if (!*file->dirname)
1606 file->dirname = NULL;
1611 static void output_flist(struct file_list *flist)
1613 char uidbuf[16], gidbuf[16], depthbuf[16];
1614 struct file_struct *file;
1615 const char *who = who_am_i();
1618 for (i = 0; i < flist->count; i++) {
1619 file = flist->files[i];
1620 if ((am_root || am_sender) && preserve_uid)
1621 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1624 if (preserve_gid && file->gid != GID_NONE)
1625 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1629 sprintf(depthbuf, "%d", file->dir.depth);
1630 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1631 who, i, am_sender ? NS(file->dir.root) : depthbuf,
1632 file->dirname ? file->dirname : "",
1633 file->dirname ? "/" : "", NS(file->basename),
1634 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
1635 (double)file->length, uidbuf, gidbuf, file->flags);
1639 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1640 enum fnc_type { t_PATH, t_ITEM };
1642 /* Compare the names of two file_struct entities, similar to how strcmp()
1643 * would do if it were operating on the joined strings.
1645 * Some differences beginning with protocol_version 29: (1) directory names
1646 * are compared with an assumed trailing slash so that they compare in a
1647 * way that would cause them to sort immediately prior to any content they
1648 * may have; (2) a directory of any name compares after a non-directory of
1649 * any name at the same depth; (3) a directory with name "." compares prior
1650 * to anything else. These changes mean that a directory and a non-dir
1651 * with the same name will not compare as equal (protocol_version >= 29).
1653 * The dirname component can be an empty string, but the basename component
1654 * cannot (and never is in the current codebase). The basename component
1655 * may be NULL (for a removed item), in which case it is considered to be
1656 * after any existing item. */
1657 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1660 const uchar *c1, *c2;
1661 enum fnc_state state1, state2;
1662 enum fnc_type type1, type2;
1663 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1665 if (!f1 || !f1->basename) {
1666 if (!f2 || !f2->basename)
1670 if (!f2 || !f2->basename)
1673 c1 = (uchar*)f1->dirname;
1674 c2 = (uchar*)f2->dirname;
1678 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1679 c1 = (uchar*)f1->basename;
1680 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1682 state1 = s_TRAILING;
1695 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1696 c2 = (uchar*)f2->basename;
1697 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1699 state2 = s_TRAILING;
1713 return type1 == t_PATH ? 1 : -1;
1716 if ((dif = (int)*c1++ - (int)*c2++) != 0)
1725 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1726 c1 = (uchar*)f1->basename;
1727 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1729 state1 = s_TRAILING;
1735 state1 = s_TRAILING;
1736 if (type1 == t_PATH) {
1745 if (*c2 && type1 != type2)
1746 return type1 == t_PATH ? 1 : -1;
1755 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1756 c2 = (uchar*)f2->basename;
1757 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1759 state2 = s_TRAILING;
1765 state2 = s_TRAILING;
1766 if (type2 == t_PATH) {
1778 return type1 == t_PATH ? 1 : -1;
1785 /* Return a copy of the full filename of a flist entry, using the indicated
1786 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
1787 * done because we checked the size when creating the file_struct entry.
1789 char *f_name(struct file_struct *f, char *fbuf)
1791 if (!f || !f->basename)
1795 static char names[5][MAXPATHLEN];
1796 static unsigned int n;
1798 n = (n + 1) % (sizeof names / sizeof names[0]);
1804 int len = strlen(f->dirname);
1805 memcpy(fbuf, f->dirname, len);
1807 strcpy(fbuf + len + 1, f->basename);
1809 strcpy(fbuf, f->basename);
1814 /* Do a non-recursive scan of the named directory, possibly ignoring all
1815 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1816 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1817 * buffer (the functions we call will append names onto the end, but the old
1818 * dir value will be restored on exit). */
1819 struct file_list *get_dirlist(char *dirname, int dlen,
1820 int ignore_filter_rules)
1822 struct file_list *dirlist;
1823 char dirbuf[MAXPATHLEN];
1824 int save_recurse = recurse;
1827 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1828 if (dlen >= MAXPATHLEN)
1833 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1836 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
1837 recurse = save_recurse;
1839 flist_count_offset += dirlist->count;
1841 clean_flist(dirlist, 0, 0);
1844 output_flist(dirlist);