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 */
73 unsigned int file_struct_len;
75 static char empty_sum[MD4_SUM_LENGTH];
76 static int flist_count_offset;
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->ids->uid == uid)
366 flags |= XMIT_SAME_UID;
368 uid = file->ids->uid;
369 if (file->ids->gid == gid)
370 flags |= XMIT_SAME_GID;
372 gid = file->ids->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_ISLNK(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;
622 file->modtime = modtime;
623 file->length = file_length;
625 file->ids = id_pair(uid, gid);
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 && lastname[del_hier_name_len-1] == '.'
649 && lastname[del_hier_name_len-2] == '/')
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 && lastname[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 && st.st_dev != filesystem_dev
811 && S_ISDIR(st.st_mode)) {
812 if (one_file_system > 1) {
814 rprintf(FINFO, "skipping mount-point dir %s\n",
819 flags |= FLAG_MOUNT_POINT;
822 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
825 if (lp_ignore_nonreadable(module_id)) {
827 if (!S_ISLNK(st.st_mode))
829 if (access(thisname, R_OK) != 0)
836 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
837 who_am_i(), thisname, filter_level);
840 if ((basename = strrchr(thisname, '/')) != NULL) {
841 dirname_len = ++basename - thisname; /* counts future '\0' */
842 if (lastdir_len == dirname_len - 1
843 && strncmp(thisname, lastdir, lastdir_len) == 0) {
845 dirname_len = 0; /* indicates no copy is needed */
853 basename_len = strlen(basename) + 1; /* count the '\0' */
856 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
861 sum_len = always_checksum && am_sender && S_ISREG(st.st_mode)
862 ? MD4_SUM_LENGTH : 0;
864 alloc_len = file_struct_len + dirname_len + basename_len
865 + linkname_len + sum_len;
867 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
869 if (!(bp = new_array(char, alloc_len)))
870 out_of_memory("make_file");
873 file = (struct file_struct *)bp;
874 memset(bp, 0, file_struct_len);
875 bp += file_struct_len;
878 file->modtime = st.st_mtime;
879 file->length = st.st_size;
880 file->mode = st.st_mode;
881 file->ids = id_pair(st.st_uid, st.st_gid);
883 #ifdef SUPPORT_HARD_LINKS
884 if (flist && flist->hlink_pool) {
885 if (protocol_version < 28) {
886 if (S_ISREG(st.st_mode))
887 file->link_u.idev = pool_talloc(
888 flist->hlink_pool, struct idev, 1,
891 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
892 file->link_u.idev = pool_talloc(
893 flist->hlink_pool, struct idev, 1,
897 if (file->link_u.idev) {
898 file->F_DEV = st.st_dev;
899 file->F_INODE = st.st_ino;
904 file->dirname = lastdir = bp;
905 lastdir_len = dirname_len - 1;
906 memcpy(bp, dirname, dirname_len - 1);
910 file->dirname = dirname;
913 memcpy(bp, basename, basename_len);
916 #ifdef HAVE_STRUCT_STAT_ST_RDEV
917 if (preserve_devices && IS_DEVICE(st.st_mode))
918 file->u.rdev = st.st_rdev;
924 memcpy(bp, linkname, linkname_len);
931 file_checksum(thisname, bp, st.st_size);
935 file->dir.root = flist_dir;
937 /* This code is only used by the receiver when it is building
938 * a list of files for a delete pass. */
939 if (keep_dirlinks && linkname_len && flist) {
941 int save_mode = file->mode;
942 file->mode = S_IFDIR; /* Find a directory with our name. */
943 if (flist_find(the_file_list, file) >= 0
944 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
945 file->modtime = st2.st_mtime;
946 file->length = st2.st_size;
947 file->mode = st2.st_mode;
948 file->ids = id_pair(st2.st_uid, st2.st_gid);
951 file->mode = save_mode;
954 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
955 stats.total_size += st.st_size;
960 static struct file_struct *send_file_name(int f, struct file_list *flist,
961 char *fname, STRUCT_STAT *stp,
962 unsigned short flags)
964 struct file_struct *file;
966 file = make_file(fname, flist, stp, flags,
967 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
971 if (chmod_modes && !S_ISLNK(file->mode))
972 file->mode = tweak_mode(file->mode, chmod_modes);
974 maybe_emit_filelist_progress(flist->count + flist_count_offset);
978 if (file->basename[0]) {
979 flist->files[flist->count++] = file;
980 send_file_entry(file, f);
985 static void send_if_directory(int f, struct file_list *flist,
986 struct file_struct *file,
987 char *fbuf, unsigned int ol)
989 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
991 if (S_ISDIR(file->mode)
992 && !(file->flags & FLAG_MOUNT_POINT) && f_name(file, fbuf)) {
994 unsigned int len = strlen(fbuf);
995 if (len > 1 && fbuf[len-1] == '/')
997 if (len >= MAXPATHLEN - 1) {
998 io_error |= IOERR_GENERAL;
999 rprintf(FERROR, "skipping long-named directory: %s\n",
1003 save_filters = push_local_filters(fbuf, len);
1004 send_directory(f, flist, fbuf, len);
1005 pop_local_filters(save_filters);
1012 /* This function is normally called by the sender, but the receiving side also
1013 * calls it from get_dirlist() with f set to -1 so that we just construct the
1014 * file list in memory without sending it over the wire. Also, get_dirlist()
1015 * might call this with f set to -2, which also indicates that local filter
1016 * rules should be ignored. */
1017 static void send_directory(int f, struct file_list *flist,
1018 char *fbuf, int len)
1024 int start = flist->count;
1026 if (!(d = opendir(fbuf))) {
1027 io_error |= IOERR_GENERAL;
1028 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1033 if (len != 1 || *fbuf != '/')
1036 remainder = MAXPATHLEN - (p - fbuf);
1038 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1039 char *dname = d_name(di);
1040 if (dname[0] == '.' && (dname[1] == '\0'
1041 || (dname[1] == '.' && dname[2] == '\0')))
1043 if (strlcpy(p, dname, remainder) >= remainder) {
1044 io_error |= IOERR_GENERAL;
1046 "cannot send long-named file %s\n",
1051 send_file_name(f, flist, fbuf, NULL, 0);
1057 io_error |= IOERR_GENERAL;
1058 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1064 int i, end = flist->count - 1;
1065 for (i = start; i <= end; i++)
1066 send_if_directory(f, flist, flist->files[i], fbuf, len);
1070 struct file_list *send_file_list(int f, int argc, char *argv[])
1074 char *p, *dir, olddir[sizeof curr_dir];
1075 char lastpath[MAXPATHLEN] = "";
1076 struct file_list *flist;
1077 struct timeval start_tv, end_tv;
1081 if (show_filelist_p())
1082 start_filelist_progress("building file list");
1084 start_write = stats.total_written;
1085 gettimeofday(&start_tv, NULL);
1087 flist = flist_new(WITH_HLINK, "send_file_list");
1089 io_start_buffering_out();
1090 if (filesfrom_fd >= 0) {
1091 if (argv[0] && !push_dir(argv[0])) {
1092 rsyserr(FERROR, errno, "push_dir %s failed",
1093 full_fname(argv[0]));
1094 exit_cleanup(RERR_FILESELECT);
1100 char fbuf[MAXPATHLEN];
1105 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1107 sanitize_path(fbuf, fbuf, "", 0);
1111 strlcpy(fbuf, *argv++, MAXPATHLEN);
1113 sanitize_path(fbuf, fbuf, "", 0);
1117 if (relative_paths) {
1118 /* We clean up fbuf below. */
1120 } else if (!len || fbuf[len - 1] == '/') {
1121 if (len == 2 && fbuf[0] == '.') {
1122 /* Turn "./" into just "." rather than "./." */
1125 if (len + 1 >= MAXPATHLEN)
1126 overflow_exit("send_file_list");
1131 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1132 && (len == 2 || fbuf[len-3] == '/')) {
1133 if (len + 2 >= MAXPATHLEN)
1134 overflow_exit("send_file_list");
1140 is_dot_dir = fbuf[len-1] == '.'
1141 && (len == 1 || fbuf[len-2] == '/');
1144 if (link_stat(fbuf, &st, keep_dirlinks) != 0) {
1145 io_error |= IOERR_GENERAL;
1146 rsyserr(FERROR, errno, "link_stat %s failed",
1151 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1152 rprintf(FINFO, "skipping directory %s\n", fbuf);
1159 if (!relative_paths) {
1160 p = strrchr(fbuf, '/');
1167 len -= p - fbuf + 1;
1172 if ((p = strstr(fbuf, "/./")) != NULL) {
1178 len -= p - fbuf + 3;
1182 /* Get rid of trailing "/" and "/.". */
1184 if (fn[len - 1] == '/') {
1186 if (!--len && !dir) {
1191 else if (len >= 2 && fn[len - 1] == '.'
1192 && fn[len - 2] == '/') {
1194 if (!(len -= 2) && !dir) {
1202 /* Reject a ".." dir in the active part of the path. */
1203 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1204 if ((p[2] == '/' || p[2] == '\0')
1205 && (p == fn || p[-1] == '/')) {
1207 "found \"..\" dir in relative path: %s\n",
1209 exit_cleanup(RERR_SYNTAX);
1220 static char *lastdir;
1221 static int lastdir_len;
1223 strlcpy(olddir, curr_dir, sizeof olddir);
1225 if (!push_dir(dir)) {
1226 io_error |= IOERR_GENERAL;
1227 rsyserr(FERROR, errno, "push_dir %s failed",
1232 if (lastdir && strcmp(lastdir, dir) == 0) {
1233 flist_dir = lastdir;
1234 flist_dir_len = lastdir_len;
1236 flist_dir = lastdir = strdup(dir);
1237 flist_dir_len = lastdir_len = strlen(dir);
1242 memmove(fbuf, fn, len + 1);
1244 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1245 /* Send the implied directories at the start of the
1246 * source spec, so we get their permissions right. */
1247 char *lp = lastpath, *slash = fbuf;
1249 /* Skip any initial directories in our path that we
1250 * have in common with lastpath. */
1251 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1256 if (fn != p || (*lp && *lp != '/')) {
1257 int save_copy_links = copy_links;
1258 int save_xfer_dirs = xfer_dirs;
1259 copy_links = copy_unsafe_links;
1261 while ((slash = strchr(slash+1, '/')) != 0) {
1263 send_file_name(f, flist, fbuf, NULL, 0);
1266 copy_links = save_copy_links;
1267 xfer_dirs = save_xfer_dirs;
1269 strlcpy(lastpath, fbuf, sizeof lastpath);
1274 if (one_file_system)
1275 filesystem_dev = st.st_dev;
1277 if (recurse || (xfer_dirs && is_dot_dir)) {
1278 struct file_struct *file;
1279 file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
1281 send_if_directory(f, flist, file, fbuf, len);
1283 send_file_name(f, flist, fbuf, &st, 0);
1288 if (!pop_dir(olddir)) {
1289 rsyserr(FERROR, errno, "pop_dir %s failed",
1290 full_fname(olddir));
1291 exit_cleanup(RERR_FILESELECT);
1296 gettimeofday(&end_tv, NULL);
1297 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1298 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1299 if (stats.flist_buildtime == 0)
1300 stats.flist_buildtime = 1;
1303 send_file_entry(NULL, f);
1305 if (show_filelist_p())
1306 finish_filelist_progress(flist);
1308 gettimeofday(&end_tv, NULL);
1309 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1310 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1312 if (flist->hlink_pool) {
1313 pool_destroy(flist->hlink_pool);
1314 flist->hlink_pool = NULL;
1317 /* Sort the list without removing any duplicates. This allows the
1318 * receiving side to ask for any name they like, which gives us the
1319 * flexibility to change the way we unduplicate names in the future
1320 * without causing a compatibility problem with older versions. */
1321 clean_flist(flist, 0, 0);
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);
1394 /* Recv the io_error flag */
1395 if (lp_ignore_errors(module_id) || ignore_errors)
1398 io_error |= read_int(f);
1402 output_flist(flist);
1406 for (i = 0; i < flist->count; i++)
1407 list_file_entry(flist->files[i]);
1411 rprintf(FINFO, "recv_file_list done\n");
1413 stats.flist_size = stats.total_read - start_read;
1414 stats.num_files = flist->count;
1419 out_of_memory("recv_file_list");
1420 return NULL; /* not reached */
1423 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1425 return f_name_cmp(*file1, *file2);
1428 /* Search for an identically-named item in the file list. Note that the
1429 * items must agree in their directory-ness, or no match is returned. */
1430 int flist_find(struct file_list *flist, struct file_struct *f)
1432 int low = flist->low, high = flist->high;
1433 int ret, mid, mid_up;
1435 while (low <= high) {
1436 mid = (low + high) / 2;
1437 for (mid_up = mid; !flist->files[mid_up]->basename; mid_up++) {}
1439 ret = f_name_cmp(flist->files[mid_up], f);
1443 if (protocol_version < 29
1444 && S_ISDIR(flist->files[mid_up]->mode)
1445 != S_ISDIR(f->mode))
1458 * Free up any resources a file_struct has allocated
1459 * and clear the file.
1461 void clear_file(int i, struct file_list *flist)
1463 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1464 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1465 memset(flist->files[i], 0, file_struct_len);
1469 * allocate a new file list
1471 struct file_list *flist_new(int with_hlink, char *msg)
1473 struct file_list *flist;
1475 flist = new(struct file_list);
1479 memset(flist, 0, sizeof (struct file_list));
1481 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1482 out_of_memory, POOL_INTERN)))
1485 #ifdef SUPPORT_HARD_LINKS
1486 if (with_hlink && preserve_hard_links) {
1487 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1488 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1497 * free up all elements in a flist
1499 void flist_free(struct file_list *flist)
1501 pool_destroy(flist->file_pool);
1502 pool_destroy(flist->hlink_pool);
1508 * This routine ensures we don't have any duplicate names in our file list.
1509 * duplicate names can cause corruption because of the pipelining
1511 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1517 if (flist->count == 0) {
1522 qsort(flist->files, flist->count,
1523 sizeof flist->files[0], (int (*)())file_compare);
1525 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1526 if (flist->files[i]->basename) {
1531 flist->low = prev_i;
1532 while (++i < flist->count) {
1534 struct file_struct *file = flist->files[i];
1536 if (!file->basename)
1538 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1540 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1541 int save_mode = file->mode;
1542 /* Make sure that this directory doesn't duplicate a
1543 * non-directory earlier in the list. */
1544 flist->high = prev_i;
1545 file->mode = S_IFREG;
1546 j = flist_find(flist, file);
1547 file->mode = save_mode;
1551 struct file_struct *fp = flist->files[j];
1553 /* If one is a dir and the other is not, we want to
1554 * keep the dir because it might have contents in the
1556 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1557 if (S_ISDIR(file->mode))
1563 if (verbose > 1 && !am_server) {
1565 "removing duplicate name %s from file list (%d)\n",
1566 f_name(file, NULL), drop);
1568 /* Make sure that if we unduplicate '.', that we don't
1569 * lose track of a user-specified top directory. */
1570 flist->files[keep]->flags |= flist->files[drop]->flags
1571 & (FLAG_TOP_DIR|FLAG_DEL_HERE);
1573 clear_file(drop, flist);
1576 if (flist->low == drop) {
1578 j < i && !flist->files[j]->basename;
1587 flist->high = no_dups ? prev_i : flist->count - 1;
1590 /* We need to strip off the leading slashes for relative
1591 * paths, but this must be done _after_ the sorting phase. */
1592 for (i = flist->low; i <= flist->high; i++) {
1593 struct file_struct *file = flist->files[i];
1597 if (*file->dirname == '/') {
1598 char *s = file->dirname + 1;
1599 while (*s == '/') s++;
1600 memmove(file->dirname, s, strlen(s) + 1);
1603 if (!*file->dirname)
1604 file->dirname = NULL;
1609 static void output_flist(struct file_list *flist)
1611 char uidbuf[16], gidbuf[16], depthbuf[16];
1612 struct file_struct *file;
1613 const char *who = who_am_i();
1616 for (i = 0; i < flist->count; i++) {
1617 file = flist->files[i];
1618 if ((am_root || am_sender) && preserve_uid)
1619 sprintf(uidbuf, " uid=%ld", (long)file->ids->uid);
1622 if (preserve_gid && file->ids->gid != GID_NONE)
1623 sprintf(gidbuf, " gid=%ld", (long)file->ids->gid);
1627 sprintf(depthbuf, "%d", file->dir.depth);
1628 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1629 who, i, am_sender ? NS(file->dir.root) : depthbuf,
1630 file->dirname ? file->dirname : "",
1631 file->dirname ? "/" : "", NS(file->basename),
1632 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
1633 (double)file->length, uidbuf, gidbuf, file->flags);
1637 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1638 enum fnc_type { t_PATH, t_ITEM };
1640 /* Compare the names of two file_struct entities, similar to how strcmp()
1641 * would do if it were operating on the joined strings.
1643 * Some differences beginning with protocol_version 29: (1) directory names
1644 * are compared with an assumed trailing slash so that they compare in a
1645 * way that would cause them to sort immediately prior to any content they
1646 * may have; (2) a directory of any name compares after a non-directory of
1647 * any name at the same depth; (3) a directory with name "." compares prior
1648 * to anything else. These changes mean that a directory and a non-dir
1649 * with the same name will not compare as equal (protocol_version >= 29).
1651 * The dirname component can be an empty string, but the basename component
1652 * cannot (and never is in the current codebase). The basename component
1653 * may be NULL (for a removed item), in which case it is considered to be
1654 * after any existing item. */
1655 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1658 const uchar *c1, *c2;
1659 enum fnc_state state1, state2;
1660 enum fnc_type type1, type2;
1661 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1663 if (!f1 || !f1->basename) {
1664 if (!f2 || !f2->basename)
1668 if (!f2 || !f2->basename)
1671 c1 = (uchar*)f1->dirname;
1672 c2 = (uchar*)f2->dirname;
1676 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1677 c1 = (uchar*)f1->basename;
1678 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1680 state1 = s_TRAILING;
1693 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1694 c2 = (uchar*)f2->basename;
1695 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1697 state2 = s_TRAILING;
1711 return type1 == t_PATH ? 1 : -1;
1714 if ((dif = (int)*c1++ - (int)*c2++) != 0)
1723 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1724 c1 = (uchar*)f1->basename;
1725 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1727 state1 = s_TRAILING;
1733 state1 = s_TRAILING;
1734 if (type1 == t_PATH) {
1743 if (*c2 && type1 != type2)
1744 return type1 == t_PATH ? 1 : -1;
1753 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1754 c2 = (uchar*)f2->basename;
1755 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1757 state2 = s_TRAILING;
1763 state2 = s_TRAILING;
1764 if (type2 == t_PATH) {
1776 return type1 == t_PATH ? 1 : -1;
1783 /* Return a copy of the full filename of a flist entry, using the indicated
1784 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
1785 * done because we checked the size when creating the file_struct entry.
1787 char *f_name(struct file_struct *f, char *fbuf)
1789 if (!f || !f->basename)
1793 static char names[5][MAXPATHLEN];
1794 static unsigned int n;
1796 n = (n + 1) % (sizeof names / sizeof names[0]);
1802 int len = strlen(f->dirname);
1803 memcpy(fbuf, f->dirname, len);
1805 strcpy(fbuf + len + 1, f->basename);
1807 strcpy(fbuf, f->basename);
1812 /* Do a non-recursive scan of the named directory, possibly ignoring all
1813 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1814 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1815 * buffer (the functions we call will append names onto the end, but the old
1816 * dir value will be restored on exit). */
1817 struct file_list *get_dirlist(char *dirname, int dlen,
1818 int ignore_filter_rules)
1820 struct file_list *dirlist;
1821 char dirbuf[MAXPATHLEN];
1822 int save_recurse = recurse;
1825 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1826 if (dlen >= MAXPATHLEN)
1831 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1834 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
1835 recurse = save_recurse;
1837 flist_count_offset += dirlist->count;
1839 clean_flist(dirlist, 0, 0);
1842 output_flist(dirlist);