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;
73 static char empty_sum[MD4_SUM_LENGTH];
74 static int flist_count_offset;
75 static unsigned int file_struct_len;
77 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
78 static void output_flist(struct file_list *flist);
84 /* Figure out how big the file_struct is without trailing padding */
85 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
86 checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
89 static int show_filelist_p(void)
91 return verbose && xfer_dirs && !am_server;
94 static void start_filelist_progress(char *kind)
96 rprintf(FINFO, "%s ... ", kind);
97 if (verbose > 1 || do_progress)
102 static void emit_filelist_progress(int count)
104 rprintf(FINFO, " %d files...\r", count);
107 static void maybe_emit_filelist_progress(int count)
109 if (do_progress && show_filelist_p() && (count % 100) == 0)
110 emit_filelist_progress(count);
113 static void finish_filelist_progress(const struct file_list *flist)
116 /* This overwrites the progress line */
117 rprintf(FINFO, "%d file%sto consider\n",
118 flist->count, flist->count == 1 ? " " : "s ");
120 rprintf(FINFO, "done\n");
123 void show_flist_stats(void)
128 static void list_file_entry(struct file_struct *f)
133 /* this can happen if duplicate names were removed */
137 permstring(perms, f->mode);
140 if (preserve_links && S_ISLNK(f->mode)) {
141 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
143 (double)f->length, timestring(f->modtime),
144 f_name(f, NULL), f->u.link);
148 rprintf(FINFO, "%s %11.0f %s %s\n",
150 (double)f->length, timestring(f->modtime),
156 * Stat either a symlink or its referent, depending on the settings of
157 * copy_links, copy_unsafe_links, etc.
159 * @retval -1 on error
161 * @retval 0 for success
163 * @post If @p path is a symlink, then @p linkbuf (of size @c
164 * MAXPATHLEN) contains the symlink target.
166 * @post @p buffer contains information about the link or the
167 * referrent as appropriate, if they exist.
169 static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
173 return do_stat(path, buffer);
174 if (link_stat(path, buffer, 0) < 0)
176 if (S_ISLNK(buffer->st_mode)) {
177 int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1);
181 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
183 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
186 return do_stat(path, buffer);
191 return do_stat(path, buffer);
195 int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks)
199 return do_stat(path, buffer);
200 if (do_lstat(path, buffer) < 0)
202 if (follow_dirlinks && S_ISLNK(buffer->st_mode)) {
204 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
209 return do_stat(path, buffer);
213 /* This function is used to check if a file should be included/excluded
214 * from the list of files based on its name and type etc. The value of
215 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
216 static int is_excluded(char *fname, int is_dir, int filter_level)
218 #if 0 /* This currently never happens, so avoid a useless compare. */
219 if (filter_level == NO_FILTERS)
223 /* never exclude '.', even if somebody does --exclude '*' */
224 if (fname[0] == '.' && !fname[1])
226 /* Handle the -R version of the '.' dir. */
227 if (fname[0] == '/') {
228 int len = strlen(fname);
229 if (fname[len-1] == '.' && fname[len-2] == '/')
233 if (server_filter_list.head
234 && check_filter(&server_filter_list, fname, is_dir) < 0)
236 if (filter_level != ALL_FILTERS)
239 && check_filter(&filter_list, fname, is_dir) < 0)
244 static int to_wire_mode(mode_t mode)
247 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
248 return (mode & ~(_S_IFMT)) | 0120000;
253 static mode_t from_wire_mode(int mode)
255 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
256 return (mode & ~(_S_IFMT)) | _S_IFLNK;
260 static void send_directory(int f, struct file_list *flist,
261 char *fbuf, int len);
263 static char *flist_dir;
264 static int flist_dir_len;
268 * Make sure @p flist is big enough to hold at least @p flist->count
271 void flist_expand(struct file_list *flist)
273 struct file_struct **new_ptr;
275 if (flist->count < flist->malloced)
278 if (flist->malloced < FLIST_START)
279 flist->malloced = FLIST_START;
280 else if (flist->malloced >= FLIST_LINEAR)
281 flist->malloced += FLIST_LINEAR;
283 flist->malloced *= 2;
286 * In case count jumped or we are starting the list
287 * with a known size just set it.
289 if (flist->malloced < flist->count)
290 flist->malloced = flist->count;
292 new_ptr = realloc_array(flist->files, struct file_struct *,
295 if (verbose >= 2 && flist->malloced != FLIST_START) {
296 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
298 (double)sizeof flist->files[0] * flist->malloced,
299 (new_ptr == flist->files) ? " not" : "");
302 flist->files = new_ptr;
305 out_of_memory("flist_expand");
308 static void send_file_entry(struct file_struct *file, int f)
310 unsigned short flags;
311 static time_t modtime;
315 static uint32 rdev_major;
318 static char lastname[MAXPATHLEN];
319 char fname[MAXPATHLEN];
327 modtime = 0, mode = 0;
328 dev = 0, rdev = makedev(0, 0);
335 io_write_phase = "send_file_entry";
339 flags = file->flags & XMIT_TOP_DIR;
341 if (file->mode == mode)
342 flags |= XMIT_SAME_MODE;
345 if (preserve_devices) {
346 if (protocol_version < 28) {
347 if (IS_DEVICE(mode)) {
348 if (file->u.rdev == rdev)
349 flags |= XMIT_SAME_RDEV_pre28;
353 rdev = makedev(0, 0);
354 } else if (IS_DEVICE(mode)) {
356 if ((uint32)major(rdev) == rdev_major)
357 flags |= XMIT_SAME_RDEV_MAJOR;
359 rdev_major = major(rdev);
360 if ((uint32)minor(rdev) <= 0xFFu)
361 flags |= XMIT_RDEV_MINOR_IS_SMALL;
364 if (file->uid == uid)
365 flags |= XMIT_SAME_UID;
368 if (file->gid == gid)
369 flags |= XMIT_SAME_GID;
372 if (file->modtime == modtime)
373 flags |= XMIT_SAME_TIME;
375 modtime = file->modtime;
377 #ifdef SUPPORT_HARD_LINKS
378 if (file->link_u.idev) {
379 if (file->F_DEV == dev) {
380 if (protocol_version >= 28)
381 flags |= XMIT_SAME_DEV;
384 flags |= XMIT_HAS_IDEV_DATA;
389 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
391 l2 = strlen(fname+l1);
394 flags |= XMIT_SAME_NAME;
396 flags |= XMIT_LONG_NAME;
398 /* We must make sure we don't send a zero flag byte or the
399 * other end will terminate the flist transfer. Note that
400 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
401 * it's harmless way to add a bit to the first flag byte. */
402 if (protocol_version >= 28) {
403 if (!flags && !S_ISDIR(mode))
404 flags |= XMIT_TOP_DIR;
405 if ((flags & 0xFF00) || !flags) {
406 flags |= XMIT_EXTENDED_FLAGS;
407 write_byte(f, flags);
408 write_byte(f, flags >> 8);
410 write_byte(f, flags);
413 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
414 write_byte(f, flags);
416 if (flags & XMIT_SAME_NAME)
418 if (flags & XMIT_LONG_NAME)
422 write_buf(f, fname + l1, l2);
424 write_longint(f, file->length);
425 if (!(flags & XMIT_SAME_TIME))
426 write_int(f, modtime);
427 if (!(flags & XMIT_SAME_MODE))
428 write_int(f, to_wire_mode(mode));
429 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
434 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
439 if (preserve_devices && IS_DEVICE(mode)) {
440 if (protocol_version < 28) {
441 if (!(flags & XMIT_SAME_RDEV_pre28))
442 write_int(f, (int)rdev);
444 if (!(flags & XMIT_SAME_RDEV_MAJOR))
445 write_int(f, major(rdev));
446 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
447 write_byte(f, minor(rdev));
449 write_int(f, minor(rdev));
454 if (preserve_links && S_ISLNK(mode)) {
455 int len = strlen(file->u.link);
457 write_buf(f, file->u.link, len);
461 #ifdef SUPPORT_HARD_LINKS
462 if (flags & XMIT_HAS_IDEV_DATA) {
463 if (protocol_version < 26) {
464 /* 32-bit dev_t and ino_t */
466 write_int(f, file->F_INODE);
468 /* 64-bit dev_t and ino_t */
469 if (!(flags & XMIT_SAME_DEV))
470 write_longint(f, dev);
471 write_longint(f, file->F_INODE);
476 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
481 /* Prior to 28, we sent a useless set of nulls. */
484 write_buf(f, sum, checksum_len);
487 strlcpy(lastname, fname, MAXPATHLEN);
489 io_write_phase = "unknown";
492 static struct file_struct *receive_file_entry(struct file_list *flist,
493 unsigned short flags, int f)
495 static time_t modtime;
499 static uint32 rdev_major;
502 static char lastname[MAXPATHLEN], *lastdir;
503 static int lastdir_depth, lastdir_len = -1;
504 static unsigned int del_hier_name_len = 0;
505 static int in_del_hier = 0;
506 char thisname[MAXPATHLEN];
507 unsigned int l1 = 0, l2 = 0;
508 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
510 char *basename, *dirname, *bp;
511 struct file_struct *file;
514 modtime = 0, mode = 0;
515 dev = 0, rdev = makedev(0, 0);
524 if (flags & XMIT_SAME_NAME)
527 if (flags & XMIT_LONG_NAME)
532 if (l2 >= MAXPATHLEN - l1) {
534 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
535 flags, l1, l2, lastname);
536 overflow_exit("receive_file_entry");
539 strlcpy(thisname, lastname, l1 + 1);
540 read_sbuf(f, &thisname[l1], l2);
541 thisname[l1 + l2] = 0;
543 strlcpy(lastname, thisname, MAXPATHLEN);
545 clean_fname(thisname, 0);
548 sanitize_path(thisname, thisname, "", 0);
550 if ((basename = strrchr(thisname, '/')) != NULL) {
551 dirname_len = ++basename - thisname; /* counts future '\0' */
552 if (lastdir_len == dirname_len - 1
553 && strncmp(thisname, lastdir, lastdir_len) == 0) {
555 dirname_len = 0; /* indicates no copy is needed */
563 basename_len = strlen(basename) + 1; /* count the '\0' */
565 file_length = read_longint(f);
566 if (!(flags & XMIT_SAME_TIME))
567 modtime = (time_t)read_int(f);
568 if (!(flags & XMIT_SAME_MODE))
569 mode = from_wire_mode(read_int(f));
571 if (chmod_modes && !S_ISLNK(mode))
572 mode = tweak_mode(mode, chmod_modes);
574 if (preserve_uid && !(flags & XMIT_SAME_UID))
575 uid = (uid_t)read_int(f);
576 if (preserve_gid && !(flags & XMIT_SAME_GID))
577 gid = (gid_t)read_int(f);
579 if (preserve_devices) {
580 if (protocol_version < 28) {
581 if (IS_DEVICE(mode)) {
582 if (!(flags & XMIT_SAME_RDEV_pre28))
583 rdev = (dev_t)read_int(f);
585 rdev = makedev(0, 0);
586 } else if (IS_DEVICE(mode)) {
588 if (!(flags & XMIT_SAME_RDEV_MAJOR))
589 rdev_major = read_int(f);
590 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
591 rdev_minor = read_byte(f);
593 rdev_minor = read_int(f);
594 rdev = makedev(rdev_major, rdev_minor);
599 if (preserve_links && S_ISLNK(mode)) {
600 linkname_len = read_int(f) + 1; /* count the '\0' */
601 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
602 rprintf(FERROR, "overflow: linkname_len=%d\n",
604 overflow_exit("receive_file_entry");
611 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
613 alloc_len = file_struct_len + dirname_len + basename_len
614 + linkname_len + sum_len;
615 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
617 file = (struct file_struct *)bp;
618 memset(bp, 0, file_struct_len);
619 bp += file_struct_len;
622 file->modtime = modtime;
623 file->length = file_length;
629 file->dirname = lastdir = bp;
630 lastdir_len = dirname_len - 1;
631 memcpy(bp, dirname, dirname_len - 1);
634 lastdir_depth = count_dir_elements(lastdir);
635 file->dir.depth = lastdir_depth + 1;
636 } else if (dirname) {
637 file->dirname = dirname; /* we're reusing lastname */
638 file->dir.depth = lastdir_depth + 1;
643 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
645 if (flags & XMIT_TOP_DIR) {
646 in_del_hier = recurse;
647 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
648 if (relative_paths && del_hier_name_len > 2
649 && basename_len == 1+1 && *basename == '.')
650 del_hier_name_len -= 2;
651 file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
652 } else if (in_del_hier) {
653 if (!relative_paths || !del_hier_name_len
654 || (l1 >= del_hier_name_len
655 && thisname[del_hier_name_len] == '/'))
656 file->flags |= FLAG_DEL_HERE;
663 memcpy(bp, basename, basename_len);
666 if (preserve_devices && IS_DEVICE(mode))
672 read_sbuf(f, bp, linkname_len - 1);
674 sanitize_path(bp, bp, "", lastdir_depth);
679 #ifdef SUPPORT_HARD_LINKS
680 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
681 flags |= XMIT_HAS_IDEV_DATA;
682 if (flags & XMIT_HAS_IDEV_DATA) {
684 if (protocol_version < 26) {
688 if (!(flags & XMIT_SAME_DEV))
689 dev = read_longint(f);
690 inode = read_longint(f);
692 if (flist->hlink_pool) {
693 file->link_u.idev = pool_talloc(flist->hlink_pool,
694 struct idev, 1, "inode_table");
695 file->F_INODE = inode;
701 if (always_checksum && (sum_len || protocol_version < 28)) {
704 file->u.sum = sum = bp;
707 /* Prior to 28, we get a useless set of nulls. */
710 read_buf(f, sum, checksum_len);
713 if (!preserve_perms) {
714 /* set an appropriate set of permissions based on original
715 * permissions and umask. This emulates what GNU cp does */
716 file->mode &= ~orig_umask;
723 * Create a file_struct for a named file by reading its stat()
724 * information and performing extensive checks against global
727 * @return the new file, or NULL if there was an error or this file
728 * should be excluded.
730 * @todo There is a small optimization opportunity here to avoid
731 * stat()ing the file in some circumstances, which has a certain cost.
732 * We are called immediately after doing readdir(), and so we may
733 * already know the d_type of the file. We could for example avoid
734 * statting directories if we're not recursing, but this is not a very
735 * important case. Some systems may not have d_type.
737 struct file_struct *make_file(char *fname, struct file_list *flist,
738 STRUCT_STAT *stp, unsigned short flags,
741 static char *lastdir;
742 static int lastdir_len = -1;
743 struct file_struct *file;
745 char sum[SUM_LENGTH];
746 char thisname[MAXPATHLEN];
747 char linkname[MAXPATHLEN];
748 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
749 char *basename, *dirname, *bp;
751 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
754 if (strlcpy(thisname, fname, sizeof thisname)
755 >= sizeof thisname - flist_dir_len) {
756 rprintf(FINFO, "skipping overly long name: %s\n", fname);
759 clean_fname(thisname, 0);
761 sanitize_path(thisname, thisname, "", 0);
763 memset(sum, 0, SUM_LENGTH);
765 if (stp && S_ISDIR(stp->st_mode))
766 st = *stp; /* Needed for "symlink/." with --relative. */
767 else if (readlink_stat(thisname, &st, linkname) != 0) {
768 int save_errno = errno;
769 /* See if file is excluded before reporting an error. */
770 if (filter_level != NO_FILTERS
771 && is_excluded(thisname, 0, filter_level))
773 if (save_errno == ENOENT) {
775 /* Avoid "vanished" error if symlink points nowhere. */
776 if (copy_links && do_lstat(thisname, &st) == 0
777 && S_ISLNK(st.st_mode)) {
778 io_error |= IOERR_GENERAL;
779 rprintf(FERROR, "symlink has no referent: %s\n",
780 full_fname(thisname));
784 enum logcode c = am_daemon && protocol_version < 28
786 io_error |= IOERR_VANISHED;
787 rprintf(c, "file has vanished: %s\n",
788 full_fname(thisname));
791 io_error |= IOERR_GENERAL;
792 rsyserr(FERROR, save_errno, "readlink %s failed",
793 full_fname(thisname));
798 /* backup.c calls us with filter_level set to NO_FILTERS. */
799 if (filter_level == NO_FILTERS)
802 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
803 rprintf(FINFO, "skipping directory %s\n", thisname);
807 /* We only care about directories because we need to avoid recursing
808 * into a mount-point directory, not to avoid copying a symlinked
809 * file if -L (or similar) was specified. */
810 if (one_file_system && S_ISDIR(st.st_mode) && !(flags & FLAG_TOP_DIR)) {
812 unsigned int len = strlcat(thisname, "/..", sizeof thisname);
813 /* If the directory's .. dir is on a different filesystem,
814 * either mark this dir as a mount-point or skip it. */
815 if (len < sizeof thisname && do_stat(thisname, &st2) == 0
816 && (st.st_dev != st2.st_dev || st.st_ino != st2.st_ino)) {
817 if (one_file_system > 1) {
820 "skipping mount-point dir %s\n",
825 flags |= FLAG_MOUNT_POINT;
827 thisname[len-3] = '\0';
830 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
833 if (lp_ignore_nonreadable(module_id)) {
835 if (!S_ISLNK(st.st_mode))
837 if (access(thisname, R_OK) != 0)
844 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
845 who_am_i(), thisname, filter_level);
848 if ((basename = strrchr(thisname, '/')) != NULL) {
849 dirname_len = ++basename - thisname; /* counts future '\0' */
850 if (lastdir_len == dirname_len - 1
851 && strncmp(thisname, lastdir, lastdir_len) == 0) {
853 dirname_len = 0; /* indicates no copy is needed */
861 basename_len = strlen(basename) + 1; /* count the '\0' */
864 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
869 sum_len = always_checksum && am_sender && S_ISREG(st.st_mode)
870 ? MD4_SUM_LENGTH : 0;
872 alloc_len = file_struct_len + dirname_len + basename_len
873 + linkname_len + sum_len;
875 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
877 if (!(bp = new_array(char, alloc_len)))
878 out_of_memory("make_file");
881 file = (struct file_struct *)bp;
882 memset(bp, 0, file_struct_len);
883 bp += file_struct_len;
886 file->modtime = st.st_mtime;
887 file->length = st.st_size;
888 file->mode = st.st_mode;
889 file->uid = st.st_uid;
890 file->gid = st.st_gid;
892 #ifdef SUPPORT_HARD_LINKS
893 if (flist && flist->hlink_pool) {
894 if (protocol_version < 28) {
895 if (S_ISREG(st.st_mode))
896 file->link_u.idev = pool_talloc(
897 flist->hlink_pool, struct idev, 1,
900 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
901 file->link_u.idev = pool_talloc(
902 flist->hlink_pool, struct idev, 1,
906 if (file->link_u.idev) {
907 file->F_DEV = st.st_dev;
908 file->F_INODE = st.st_ino;
913 file->dirname = lastdir = bp;
914 lastdir_len = dirname_len - 1;
915 memcpy(bp, dirname, dirname_len - 1);
919 file->dirname = dirname;
922 memcpy(bp, basename, basename_len);
925 #ifdef HAVE_STRUCT_STAT_ST_RDEV
926 if (preserve_devices && IS_DEVICE(st.st_mode))
927 file->u.rdev = st.st_rdev;
933 memcpy(bp, linkname, linkname_len);
940 file_checksum(thisname, bp, st.st_size);
944 file->dir.root = flist_dir;
946 /* This code is only used by the receiver when it is building
947 * a list of files for a delete pass. */
948 if (keep_dirlinks && linkname_len && flist) {
950 int save_mode = file->mode;
951 file->mode = S_IFDIR; /* Find a directory with our name. */
952 if (flist_find(the_file_list, file) >= 0
953 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
954 file->modtime = st2.st_mtime;
955 file->length = st2.st_size;
956 file->mode = st2.st_mode;
957 file->uid = st2.st_uid;
958 file->gid = st2.st_gid;
961 file->mode = save_mode;
964 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
965 stats.total_size += st.st_size;
970 static struct file_struct *send_file_name(int f, struct file_list *flist,
971 char *fname, STRUCT_STAT *stp,
972 unsigned short flags)
974 struct file_struct *file;
976 file = make_file(fname, flist, stp, flags,
977 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
981 if (chmod_modes && !S_ISLNK(file->mode))
982 file->mode = tweak_mode(file->mode, chmod_modes);
984 maybe_emit_filelist_progress(flist->count + flist_count_offset);
988 if (file->basename[0]) {
989 flist->files[flist->count++] = file;
990 send_file_entry(file, f);
995 static void send_if_directory(int f, struct file_list *flist,
996 struct file_struct *file,
997 char *fbuf, unsigned int ol)
999 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1001 if (S_ISDIR(file->mode)
1002 && !(file->flags & FLAG_MOUNT_POINT) && f_name(file, fbuf)) {
1004 unsigned int len = strlen(fbuf);
1005 if (len > 1 && fbuf[len-1] == '/')
1007 if (len >= MAXPATHLEN - 1) {
1008 io_error |= IOERR_GENERAL;
1009 rprintf(FERROR, "skipping long-named directory: %s\n",
1013 save_filters = push_local_filters(fbuf, len);
1014 send_directory(f, flist, fbuf, len);
1015 pop_local_filters(save_filters);
1022 /* This function is normally called by the sender, but the receiving side also
1023 * calls it from get_dirlist() with f set to -1 so that we just construct the
1024 * file list in memory without sending it over the wire. Also, get_dirlist()
1025 * might call this with f set to -2, which also indicates that local filter
1026 * rules should be ignored. */
1027 static void send_directory(int f, struct file_list *flist,
1028 char *fbuf, int len)
1034 int start = flist->count;
1036 if (!(d = opendir(fbuf))) {
1037 io_error |= IOERR_GENERAL;
1038 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1043 if (len != 1 || *fbuf != '/')
1046 remainder = MAXPATHLEN - (p - fbuf);
1048 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1049 char *dname = d_name(di);
1050 if (dname[0] == '.' && (dname[1] == '\0'
1051 || (dname[1] == '.' && dname[2] == '\0')))
1053 if (strlcpy(p, dname, remainder) >= remainder) {
1054 io_error |= IOERR_GENERAL;
1056 "cannot send long-named file %s\n",
1061 send_file_name(f, flist, fbuf, NULL, 0);
1067 io_error |= IOERR_GENERAL;
1068 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1074 int i, end = flist->count - 1;
1075 for (i = start; i <= end; i++)
1076 send_if_directory(f, flist, flist->files[i], fbuf, len);
1080 struct file_list *send_file_list(int f, int argc, char *argv[])
1084 char *p, *dir, olddir[sizeof curr_dir];
1085 char lastpath[MAXPATHLEN] = "";
1086 struct file_list *flist;
1087 struct timeval start_tv, end_tv;
1091 if (show_filelist_p())
1092 start_filelist_progress("building file list");
1094 start_write = stats.total_written;
1095 gettimeofday(&start_tv, NULL);
1097 flist = flist_new(WITH_HLINK, "send_file_list");
1099 io_start_buffering_out();
1100 if (filesfrom_fd >= 0) {
1101 if (argv[0] && !push_dir(argv[0])) {
1102 rsyserr(FERROR, errno, "push_dir %s failed",
1103 full_fname(argv[0]));
1104 exit_cleanup(RERR_FILESELECT);
1110 char fbuf[MAXPATHLEN];
1115 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1117 sanitize_path(fbuf, fbuf, "", 0);
1121 strlcpy(fbuf, *argv++, MAXPATHLEN);
1123 sanitize_path(fbuf, fbuf, "", 0);
1127 if (relative_paths) {
1128 /* We clean up fbuf below. */
1130 } else if (!len || fbuf[len - 1] == '/') {
1131 if (len == 2 && fbuf[0] == '.') {
1132 /* Turn "./" into just "." rather than "./." */
1135 if (len + 1 >= MAXPATHLEN)
1136 overflow_exit("send_file_list");
1141 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1142 && (len == 2 || fbuf[len-3] == '/')) {
1143 if (len + 2 >= MAXPATHLEN)
1144 overflow_exit("send_file_list");
1150 is_dot_dir = fbuf[len-1] == '.'
1151 && (len == 1 || fbuf[len-2] == '/');
1154 if (link_stat(fbuf, &st, keep_dirlinks) != 0) {
1155 io_error |= IOERR_GENERAL;
1156 rsyserr(FERROR, errno, "link_stat %s failed",
1161 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1162 rprintf(FINFO, "skipping directory %s\n", fbuf);
1169 if (!relative_paths) {
1170 p = strrchr(fbuf, '/');
1177 len -= p - fbuf + 1;
1182 if ((p = strstr(fbuf, "/./")) != NULL) {
1188 len -= p - fbuf + 3;
1192 /* Get rid of trailing "/" and "/.". */
1194 if (fn[len - 1] == '/') {
1196 if (!--len && !dir) {
1201 else if (len >= 2 && fn[len - 1] == '.'
1202 && fn[len - 2] == '/') {
1204 if (!(len -= 2) && !dir) {
1212 /* Reject a ".." dir in the active part of the path. */
1213 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1214 if ((p[2] == '/' || p[2] == '\0')
1215 && (p == fn || p[-1] == '/')) {
1217 "found \"..\" dir in relative path: %s\n",
1219 exit_cleanup(RERR_SYNTAX);
1230 static char *lastdir;
1231 static int lastdir_len;
1233 strlcpy(olddir, curr_dir, sizeof olddir);
1235 if (!push_dir(dir)) {
1236 io_error |= IOERR_GENERAL;
1237 rsyserr(FERROR, errno, "push_dir %s failed",
1242 if (lastdir && strcmp(lastdir, dir) == 0) {
1243 flist_dir = lastdir;
1244 flist_dir_len = lastdir_len;
1246 flist_dir = lastdir = strdup(dir);
1247 flist_dir_len = lastdir_len = strlen(dir);
1252 memmove(fbuf, fn, len + 1);
1254 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1255 /* Send the implied directories at the start of the
1256 * source spec, so we get their permissions right. */
1257 char *lp = lastpath, *slash = fbuf;
1259 /* Skip any initial directories in our path that we
1260 * have in common with lastpath. */
1261 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1266 if (fn != p || (*lp && *lp != '/')) {
1267 int save_copy_links = copy_links;
1268 int save_xfer_dirs = xfer_dirs;
1269 copy_links = copy_unsafe_links;
1271 while ((slash = strchr(slash+1, '/')) != 0) {
1273 send_file_name(f, flist, fbuf, NULL, 0);
1276 copy_links = save_copy_links;
1277 xfer_dirs = save_xfer_dirs;
1279 strlcpy(lastpath, fbuf, sizeof lastpath);
1284 if (recurse || (xfer_dirs && is_dot_dir)) {
1285 struct file_struct *file;
1286 file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
1288 send_if_directory(f, flist, file, fbuf, len);
1290 send_file_name(f, flist, fbuf, &st, 0);
1295 if (!pop_dir(olddir)) {
1296 rsyserr(FERROR, errno, "pop_dir %s failed",
1297 full_fname(olddir));
1298 exit_cleanup(RERR_FILESELECT);
1303 gettimeofday(&end_tv, NULL);
1304 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1305 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1306 if (stats.flist_buildtime == 0)
1307 stats.flist_buildtime = 1;
1310 send_file_entry(NULL, f);
1312 if (show_filelist_p())
1313 finish_filelist_progress(flist);
1315 gettimeofday(&end_tv, NULL);
1316 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1317 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1319 if (flist->hlink_pool) {
1320 pool_destroy(flist->hlink_pool);
1321 flist->hlink_pool = NULL;
1324 /* Sort the list without removing any duplicates. This allows the
1325 * receiving side to ask for any name they like, which gives us the
1326 * flexibility to change the way we unduplicate names in the future
1327 * without causing a compatibility problem with older versions. */
1328 clean_flist(flist, 0, 0);
1330 /* Now send the uid/gid list. This was introduced in
1331 * protocol version 15 */
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 /* Now send the uid/gid list. This was introduced in
1402 * protocol version 15 */
1403 recv_uid_list(f, flist);
1405 /* Recv the io_error flag */
1406 if (lp_ignore_errors(module_id) || ignore_errors)
1409 io_error |= read_int(f);
1413 output_flist(flist);
1417 for (i = 0; i < flist->count; i++)
1418 list_file_entry(flist->files[i]);
1422 rprintf(FINFO, "recv_file_list done\n");
1424 stats.flist_size = stats.total_read - start_read;
1425 stats.num_files = flist->count;
1430 out_of_memory("recv_file_list");
1431 return NULL; /* not reached */
1434 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1436 return f_name_cmp(*file1, *file2);
1439 /* Search for an identically-named item in the file list. Note that the
1440 * items must agree in their directory-ness, or no match is returned. */
1441 int flist_find(struct file_list *flist, struct file_struct *f)
1443 int low = flist->low, high = flist->high;
1444 int ret, mid, mid_up;
1446 while (low <= high) {
1447 mid = (low + high) / 2;
1448 for (mid_up = mid; !flist->files[mid_up]->basename; mid_up++) {}
1450 ret = f_name_cmp(flist->files[mid_up], f);
1454 if (protocol_version < 29
1455 && S_ISDIR(flist->files[mid_up]->mode)
1456 != S_ISDIR(f->mode))
1469 * Free up any resources a file_struct has allocated
1470 * and clear the file.
1472 void clear_file(int i, struct file_list *flist)
1474 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1475 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1476 memset(flist->files[i], 0, file_struct_len);
1480 * allocate a new file list
1482 struct file_list *flist_new(int with_hlink, char *msg)
1484 struct file_list *flist;
1486 flist = new(struct file_list);
1490 memset(flist, 0, sizeof (struct file_list));
1492 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1493 out_of_memory, POOL_INTERN)))
1496 #ifdef SUPPORT_HARD_LINKS
1497 if (with_hlink && preserve_hard_links) {
1498 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1499 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1508 * free up all elements in a flist
1510 void flist_free(struct file_list *flist)
1512 pool_destroy(flist->file_pool);
1513 pool_destroy(flist->hlink_pool);
1519 * This routine ensures we don't have any duplicate names in our file list.
1520 * duplicate names can cause corruption because of the pipelining
1522 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1528 if (flist->count == 0) {
1533 qsort(flist->files, flist->count,
1534 sizeof flist->files[0], (int (*)())file_compare);
1536 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1537 if (flist->files[i]->basename) {
1542 flist->low = prev_i;
1543 while (++i < flist->count) {
1545 struct file_struct *file = flist->files[i];
1547 if (!file->basename)
1549 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1551 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1552 int save_mode = file->mode;
1553 /* Make sure that this directory doesn't duplicate a
1554 * non-directory earlier in the list. */
1555 flist->high = prev_i;
1556 file->mode = S_IFREG;
1557 j = flist_find(flist, file);
1558 file->mode = save_mode;
1562 struct file_struct *fp = flist->files[j];
1564 /* If one is a dir and the other is not, we want to
1565 * keep the dir because it might have contents in the
1567 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1568 if (S_ISDIR(file->mode))
1574 if (verbose > 1 && !am_server) {
1576 "removing duplicate name %s from file list (%d)\n",
1577 f_name(file, NULL), drop);
1579 /* Make sure that if we unduplicate '.', that we don't
1580 * lose track of a user-specified top directory. */
1581 flist->files[keep]->flags |= flist->files[drop]->flags
1582 & (FLAG_TOP_DIR|FLAG_DEL_HERE);
1584 clear_file(drop, flist);
1587 if (flist->low == drop) {
1589 j < i && !flist->files[j]->basename;
1598 flist->high = no_dups ? prev_i : flist->count - 1;
1601 /* We need to strip off the leading slashes for relative
1602 * paths, but this must be done _after_ the sorting phase. */
1603 for (i = flist->low; i <= flist->high; i++) {
1604 struct file_struct *file = flist->files[i];
1608 if (*file->dirname == '/') {
1609 char *s = file->dirname + 1;
1610 while (*s == '/') s++;
1611 memmove(file->dirname, s, strlen(s) + 1);
1614 if (!*file->dirname)
1615 file->dirname = NULL;
1620 static void output_flist(struct file_list *flist)
1622 char uidbuf[16], gidbuf[16], depthbuf[16];
1623 struct file_struct *file;
1624 const char *who = who_am_i();
1627 for (i = 0; i < flist->count; i++) {
1628 file = flist->files[i];
1629 if ((am_root || am_sender) && preserve_uid)
1630 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1633 if (preserve_gid && file->gid != GID_NONE)
1634 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1638 sprintf(depthbuf, "%d", file->dir.depth);
1639 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1640 who, i, am_sender ? NS(file->dir.root) : depthbuf,
1641 file->dirname ? file->dirname : "",
1642 file->dirname ? "/" : "", NS(file->basename),
1643 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
1644 (double)file->length, uidbuf, gidbuf, file->flags);
1648 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1649 enum fnc_type { t_PATH, t_ITEM };
1651 /* Compare the names of two file_struct entities, similar to how strcmp()
1652 * would do if it were operating on the joined strings.
1654 * Some differences beginning with protocol_version 29: (1) directory names
1655 * are compared with an assumed trailing slash so that they compare in a
1656 * way that would cause them to sort immediately prior to any content they
1657 * may have; (2) a directory of any name compares after a non-directory of
1658 * any name at the same depth; (3) a directory with name "." compares prior
1659 * to anything else. These changes mean that a directory and a non-dir
1660 * with the same name will not compare as equal (protocol_version >= 29).
1662 * The dirname component can be an empty string, but the basename component
1663 * cannot (and never is in the current codebase). The basename component
1664 * may be NULL (for a removed item), in which case it is considered to be
1665 * after any existing item. */
1666 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1669 const uchar *c1, *c2;
1670 enum fnc_state state1, state2;
1671 enum fnc_type type1, type2;
1672 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1674 if (!f1 || !f1->basename) {
1675 if (!f2 || !f2->basename)
1679 if (!f2 || !f2->basename)
1682 c1 = (uchar*)f1->dirname;
1683 c2 = (uchar*)f2->dirname;
1687 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1688 c1 = (uchar*)f1->basename;
1689 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1691 state1 = s_TRAILING;
1704 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1705 c2 = (uchar*)f2->basename;
1706 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1708 state2 = s_TRAILING;
1722 return type1 == t_PATH ? 1 : -1;
1725 if ((dif = (int)*c1++ - (int)*c2++) != 0)
1734 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1735 c1 = (uchar*)f1->basename;
1736 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1738 state1 = s_TRAILING;
1744 state1 = s_TRAILING;
1745 if (type1 == t_PATH) {
1754 if (*c2 && type1 != type2)
1755 return type1 == t_PATH ? 1 : -1;
1764 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1765 c2 = (uchar*)f2->basename;
1766 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1768 state2 = s_TRAILING;
1774 state2 = s_TRAILING;
1775 if (type2 == t_PATH) {
1787 return type1 == t_PATH ? 1 : -1;
1794 /* Return a copy of the full filename of a flist entry, using the indicated
1795 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
1796 * done because we checked the size when creating the file_struct entry.
1798 char *f_name(struct file_struct *f, char *fbuf)
1800 if (!f || !f->basename)
1804 static char names[5][MAXPATHLEN];
1805 static unsigned int n;
1807 n = (n + 1) % (sizeof names / sizeof names[0]);
1813 int len = strlen(f->dirname);
1814 memcpy(fbuf, f->dirname, len);
1816 strcpy(fbuf + len + 1, f->basename);
1818 strcpy(fbuf, f->basename);
1823 /* Do a non-recursive scan of the named directory, possibly ignoring all
1824 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1825 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1826 * buffer (the functions we call will append names onto the end, but the old
1827 * dir value will be restored on exit). */
1828 struct file_list *get_dirlist(char *dirname, int dlen,
1829 int ignore_filter_rules)
1831 struct file_list *dirlist;
1832 char dirbuf[MAXPATHLEN];
1833 int save_recurse = recurse;
1836 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1837 if (dlen >= MAXPATHLEN)
1842 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1845 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
1846 recurse = save_recurse;
1848 flist_count_offset += dirlist->count;
1850 clean_flist(dirlist, 0, 0);
1853 output_flist(dirlist);