2 * Generate and receive file lists.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2002, 2003, 2004, 2005, 2006 Wayne Davison
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
32 extern int do_progress;
33 extern int always_checksum;
35 extern int ignore_errors;
36 extern int numeric_ids;
39 extern int filesfrom_fd;
40 extern int one_file_system;
41 extern int copy_dirlinks;
42 extern int keep_dirlinks;
43 extern int preserve_links;
44 extern int preserve_hard_links;
45 extern int preserve_devices;
46 extern int preserve_specials;
47 extern int preserve_uid;
48 extern int preserve_gid;
49 extern int relative_paths;
50 extern int implied_dirs;
51 extern int prune_empty_dirs;
52 extern int copy_links;
53 extern int copy_unsafe_links;
54 extern int protocol_version;
55 extern int sanitize_paths;
56 extern struct stats stats;
57 extern struct file_list *the_file_list;
59 extern char curr_dir[MAXPATHLEN];
61 extern struct chmod_mode_struct *chmod_modes;
63 extern struct filter_list_struct filter_list;
64 extern struct filter_list_struct server_filter_list;
68 dev_t filesystem_dev; /* used to implement -x */
69 unsigned int file_struct_len;
71 static char empty_sum[MD4_SUM_LENGTH];
72 static int flist_count_offset;
74 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
75 static void output_flist(struct file_list *flist);
81 /* Figure out how big the file_struct is without trailing padding */
82 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
83 checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
86 static int show_filelist_p(void)
88 return verbose && xfer_dirs && !am_server;
91 static void start_filelist_progress(char *kind)
93 rprintf(FINFO, "%s ... ", kind);
94 if (verbose > 1 || do_progress)
99 static void emit_filelist_progress(int count)
101 rprintf(FINFO, " %d files...\r", count);
104 static void maybe_emit_filelist_progress(int count)
106 if (do_progress && show_filelist_p() && (count % 100) == 0)
107 emit_filelist_progress(count);
110 static void finish_filelist_progress(const struct file_list *flist)
113 /* This overwrites the progress line */
114 rprintf(FINFO, "%d file%sto consider\n",
115 flist->count, flist->count == 1 ? " " : "s ");
117 rprintf(FINFO, "done\n");
120 void show_flist_stats(void)
125 static void list_file_entry(struct file_struct *f)
127 char permbuf[PERMSTRING_SIZE];
130 /* this can happen if duplicate names were removed */
134 permstring(permbuf, f->mode);
137 if (preserve_links && S_ISLNK(f->mode)) {
138 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
140 (double)f->length, timestring(f->modtime),
141 f_name(f, NULL), f->u.link);
145 rprintf(FINFO, "%s %11.0f %s %s\n",
147 (double)f->length, timestring(f->modtime),
153 * Stat either a symlink or its referent, depending on the settings of
154 * copy_links, copy_unsafe_links, etc.
156 * @retval -1 on error
158 * @retval 0 for success
160 * @post If @p path is a symlink, then @p linkbuf (of size @c
161 * MAXPATHLEN) contains the symlink target.
163 * @post @p buffer contains information about the link or the
164 * referrent as appropriate, if they exist.
166 static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
170 return do_stat(path, buffer);
171 if (link_stat(path, buffer, copy_dirlinks) < 0)
173 if (S_ISLNK(buffer->st_mode)) {
174 int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1);
178 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
180 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
183 return do_stat(path, buffer);
188 return do_stat(path, buffer);
192 int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks)
196 return do_stat(path, buffer);
197 if (do_lstat(path, buffer) < 0)
199 if (follow_dirlinks && S_ISLNK(buffer->st_mode)) {
201 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
206 return do_stat(path, buffer);
210 /* This function is used to check if a file should be included/excluded
211 * from the list of files based on its name and type etc. The value of
212 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
213 static int is_excluded(char *fname, int is_dir, int filter_level)
215 #if 0 /* This currently never happens, so avoid a useless compare. */
216 if (filter_level == NO_FILTERS)
220 /* never exclude '.', even if somebody does --exclude '*' */
221 if (fname[0] == '.' && !fname[1])
223 /* Handle the -R version of the '.' dir. */
224 if (fname[0] == '/') {
225 int len = strlen(fname);
226 if (fname[len-1] == '.' && fname[len-2] == '/')
230 if (server_filter_list.head
231 && check_filter(&server_filter_list, fname, is_dir) < 0)
233 if (filter_level != ALL_FILTERS)
236 && check_filter(&filter_list, fname, is_dir) < 0)
241 static int to_wire_mode(mode_t mode)
244 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
245 return (mode & ~(_S_IFMT)) | 0120000;
250 static mode_t from_wire_mode(int mode)
252 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
253 return (mode & ~(_S_IFMT)) | _S_IFLNK;
257 static void send_directory(int f, struct file_list *flist,
258 char *fbuf, int len);
260 static char *flist_dir;
261 static int flist_dir_len;
265 * Make sure @p flist is big enough to hold at least @p flist->count
268 void flist_expand(struct file_list *flist)
270 struct file_struct **new_ptr;
272 if (flist->count < flist->malloced)
275 if (flist->malloced < FLIST_START)
276 flist->malloced = FLIST_START;
277 else if (flist->malloced >= FLIST_LINEAR)
278 flist->malloced += FLIST_LINEAR;
280 flist->malloced *= 2;
283 * In case count jumped or we are starting the list
284 * with a known size just set it.
286 if (flist->malloced < flist->count)
287 flist->malloced = flist->count;
289 new_ptr = realloc_array(flist->files, struct file_struct *,
292 if (verbose >= 2 && flist->malloced != FLIST_START) {
293 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
295 (double)sizeof flist->files[0] * flist->malloced,
296 (new_ptr == flist->files) ? " not" : "");
299 flist->files = new_ptr;
302 out_of_memory("flist_expand");
305 static void send_file_entry(struct file_struct *file, int f)
307 unsigned short flags;
308 static time_t modtime;
312 static uint32 rdev_major;
315 static char lastname[MAXPATHLEN];
316 char fname[MAXPATHLEN];
324 modtime = 0, mode = 0;
325 dev = 0, rdev = makedev(0, 0);
334 flags = file->flags & XMIT_TOP_DIR;
336 if (file->mode == mode)
337 flags |= XMIT_SAME_MODE;
340 if ((preserve_devices && IS_DEVICE(mode))
341 || (preserve_specials && IS_SPECIAL(mode))) {
342 if (protocol_version < 28) {
343 if (file->u.rdev == rdev)
344 flags |= XMIT_SAME_RDEV_pre28;
349 if ((uint32)major(rdev) == rdev_major)
350 flags |= XMIT_SAME_RDEV_MAJOR;
352 rdev_major = major(rdev);
353 if ((uint32)minor(rdev) <= 0xFFu)
354 flags |= XMIT_RDEV_MINOR_IS_SMALL;
356 } else if (protocol_version < 28)
357 rdev = makedev(0, 0);
358 if (file->uid == uid)
359 flags |= XMIT_SAME_UID;
362 if (file->gid == gid)
363 flags |= XMIT_SAME_GID;
366 if (file->modtime == modtime)
367 flags |= XMIT_SAME_TIME;
369 modtime = file->modtime;
371 #ifdef SUPPORT_HARD_LINKS
372 if (file->link_u.idev) {
373 if (file->F_DEV == dev) {
374 if (protocol_version >= 28)
375 flags |= XMIT_SAME_DEV;
378 flags |= XMIT_HAS_IDEV_DATA;
383 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
385 l2 = strlen(fname+l1);
388 flags |= XMIT_SAME_NAME;
390 flags |= XMIT_LONG_NAME;
392 /* We must make sure we don't send a zero flag byte or the
393 * other end will terminate the flist transfer. Note that
394 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
395 * it's harmless way to add a bit to the first flag byte. */
396 if (protocol_version >= 28) {
397 if (!flags && !S_ISDIR(mode))
398 flags |= XMIT_TOP_DIR;
399 if ((flags & 0xFF00) || !flags) {
400 flags |= XMIT_EXTENDED_FLAGS;
401 write_byte(f, flags);
402 write_byte(f, flags >> 8);
404 write_byte(f, flags);
407 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
408 write_byte(f, flags);
410 if (flags & XMIT_SAME_NAME)
412 if (flags & XMIT_LONG_NAME)
416 write_buf(f, fname + l1, l2);
418 write_longint(f, file->length);
419 if (!(flags & XMIT_SAME_TIME))
420 write_int(f, modtime);
421 if (!(flags & XMIT_SAME_MODE))
422 write_int(f, to_wire_mode(mode));
423 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
428 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
433 if ((preserve_devices && IS_DEVICE(mode))
434 || (preserve_specials && IS_SPECIAL(mode))) {
435 if (protocol_version < 28) {
436 if (!(flags & XMIT_SAME_RDEV_pre28))
437 write_int(f, (int)rdev);
439 if (!(flags & XMIT_SAME_RDEV_MAJOR))
440 write_int(f, major(rdev));
441 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
442 write_byte(f, minor(rdev));
444 write_int(f, minor(rdev));
449 if (preserve_links && S_ISLNK(mode)) {
450 int len = strlen(file->u.link);
452 write_buf(f, file->u.link, len);
456 #ifdef SUPPORT_HARD_LINKS
457 if (flags & XMIT_HAS_IDEV_DATA) {
458 if (protocol_version < 26) {
459 /* 32-bit dev_t and ino_t */
461 write_int(f, file->F_INODE);
463 /* 64-bit dev_t and ino_t */
464 if (!(flags & XMIT_SAME_DEV))
465 write_longint(f, dev);
466 write_longint(f, file->F_INODE);
471 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
476 /* Prior to 28, we sent a useless set of nulls. */
479 write_buf(f, sum, checksum_len);
482 strlcpy(lastname, fname, MAXPATHLEN);
485 static struct file_struct *receive_file_entry(struct file_list *flist,
486 unsigned short flags, int f)
488 static time_t modtime;
492 static uint32 rdev_major;
495 static char lastname[MAXPATHLEN], *lastdir;
496 static int lastdir_depth, lastdir_len = -1;
497 static unsigned int del_hier_name_len = 0;
498 static int in_del_hier = 0;
499 char thisname[MAXPATHLEN];
500 unsigned int l1 = 0, l2 = 0;
501 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
503 char *basename, *dirname, *bp;
504 struct file_struct *file;
507 modtime = 0, mode = 0;
508 dev = 0, rdev = makedev(0, 0);
517 if (flags & XMIT_SAME_NAME)
520 if (flags & XMIT_LONG_NAME)
525 if (l2 >= MAXPATHLEN - l1) {
527 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
528 flags, l1, l2, lastname);
529 overflow_exit("receive_file_entry");
532 strlcpy(thisname, lastname, l1 + 1);
533 read_sbuf(f, &thisname[l1], l2);
534 thisname[l1 + l2] = 0;
536 strlcpy(lastname, thisname, MAXPATHLEN);
538 clean_fname(thisname, 0);
541 sanitize_path(thisname, thisname, "", 0);
543 if ((basename = strrchr(thisname, '/')) != NULL) {
544 dirname_len = ++basename - thisname; /* counts future '\0' */
545 if (lastdir_len == dirname_len - 1
546 && strncmp(thisname, lastdir, lastdir_len) == 0) {
548 dirname_len = 0; /* indicates no copy is needed */
556 basename_len = strlen(basename) + 1; /* count the '\0' */
558 file_length = read_longint(f);
559 if (!(flags & XMIT_SAME_TIME))
560 modtime = (time_t)read_int(f);
561 if (!(flags & XMIT_SAME_MODE))
562 mode = from_wire_mode(read_int(f));
564 if (chmod_modes && !S_ISLNK(mode))
565 mode = tweak_mode(mode, chmod_modes);
567 if (preserve_uid && !(flags & XMIT_SAME_UID))
568 uid = (uid_t)read_int(f);
569 if (preserve_gid && !(flags & XMIT_SAME_GID))
570 gid = (gid_t)read_int(f);
572 if ((preserve_devices && IS_DEVICE(mode))
573 || (preserve_specials && IS_SPECIAL(mode))) {
574 if (protocol_version < 28) {
575 if (!(flags & XMIT_SAME_RDEV_pre28))
576 rdev = (dev_t)read_int(f);
579 if (!(flags & XMIT_SAME_RDEV_MAJOR))
580 rdev_major = read_int(f);
581 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
582 rdev_minor = read_byte(f);
584 rdev_minor = read_int(f);
585 rdev = makedev(rdev_major, rdev_minor);
587 } else if (protocol_version < 28)
588 rdev = makedev(0, 0);
591 if (preserve_links && S_ISLNK(mode)) {
592 linkname_len = read_int(f) + 1; /* count the '\0' */
593 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
594 rprintf(FERROR, "overflow: linkname_len=%d\n",
596 overflow_exit("receive_file_entry");
603 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
605 alloc_len = file_struct_len + dirname_len + basename_len
606 + linkname_len + sum_len;
607 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
609 file = (struct file_struct *)bp;
610 memset(bp, 0, file_struct_len);
611 bp += file_struct_len;
613 file->modtime = modtime;
614 file->length = file_length;
620 file->dirname = lastdir = bp;
621 lastdir_len = dirname_len - 1;
622 memcpy(bp, dirname, dirname_len - 1);
625 lastdir_depth = count_dir_elements(lastdir);
626 file->dir.depth = lastdir_depth + 1;
627 } else if (dirname) {
628 file->dirname = dirname; /* we're reusing lastname */
629 file->dir.depth = lastdir_depth + 1;
634 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
636 if (flags & XMIT_TOP_DIR) {
637 in_del_hier = recurse;
638 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
639 if (relative_paths && del_hier_name_len > 2
640 && lastname[del_hier_name_len-1] == '.'
641 && lastname[del_hier_name_len-2] == '/')
642 del_hier_name_len -= 2;
643 file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
644 } else if (in_del_hier) {
645 if (!relative_paths || !del_hier_name_len
646 || (l1 >= del_hier_name_len
647 && lastname[del_hier_name_len] == '/'))
648 file->flags |= FLAG_DEL_HERE;
655 memcpy(bp, basename, basename_len);
658 if ((preserve_devices && IS_DEVICE(mode))
659 || (preserve_specials && IS_SPECIAL(mode)))
665 read_sbuf(f, bp, linkname_len - 1);
667 sanitize_path(bp, bp, "", lastdir_depth);
672 #ifdef SUPPORT_HARD_LINKS
673 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
674 flags |= XMIT_HAS_IDEV_DATA;
675 if (flags & XMIT_HAS_IDEV_DATA) {
677 if (protocol_version < 26) {
681 if (!(flags & XMIT_SAME_DEV))
682 dev = read_longint(f);
683 inode = read_longint(f);
685 if (flist->hlink_pool) {
686 file->link_u.idev = pool_talloc(flist->hlink_pool,
687 struct idev, 1, "inode_table");
688 file->F_INODE = inode;
694 if (always_checksum && (sum_len || protocol_version < 28)) {
697 file->u.sum = sum = bp;
700 /* Prior to 28, we get a useless set of nulls. */
703 read_buf(f, sum, checksum_len);
710 * Create a file_struct for a named file by reading its stat()
711 * information and performing extensive checks against global
714 * @return the new file, or NULL if there was an error or this file
715 * should be excluded.
717 * @todo There is a small optimization opportunity here to avoid
718 * stat()ing the file in some circumstances, which has a certain cost.
719 * We are called immediately after doing readdir(), and so we may
720 * already know the d_type of the file. We could for example avoid
721 * statting directories if we're not recursing, but this is not a very
722 * important case. Some systems may not have d_type.
724 struct file_struct *make_file(char *fname, struct file_list *flist,
725 STRUCT_STAT *stp, unsigned short flags,
728 static char *lastdir;
729 static int lastdir_len = -1;
730 struct file_struct *file;
732 char sum[SUM_LENGTH];
733 char thisname[MAXPATHLEN];
734 char linkname[MAXPATHLEN];
735 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
736 char *basename, *dirname, *bp;
738 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
741 if (strlcpy(thisname, fname, sizeof thisname)
742 >= sizeof thisname - flist_dir_len) {
743 rprintf(FINFO, "skipping overly long name: %s\n", fname);
746 clean_fname(thisname, 0);
748 sanitize_path(thisname, thisname, "", 0);
750 memset(sum, 0, SUM_LENGTH);
752 if (stp && S_ISDIR(stp->st_mode))
753 st = *stp; /* Needed for "symlink/." with --relative. */
754 else if (readlink_stat(thisname, &st, linkname) != 0) {
755 int save_errno = errno;
756 /* See if file is excluded before reporting an error. */
757 if (filter_level != NO_FILTERS
758 && is_excluded(thisname, 0, filter_level))
760 if (save_errno == ENOENT) {
762 /* Avoid "vanished" error if symlink points nowhere. */
763 if (copy_links && do_lstat(thisname, &st) == 0
764 && S_ISLNK(st.st_mode)) {
765 io_error |= IOERR_GENERAL;
766 rprintf(FERROR, "symlink has no referent: %s\n",
767 full_fname(thisname));
771 enum logcode c = am_daemon && protocol_version < 28
773 io_error |= IOERR_VANISHED;
774 rprintf(c, "file has vanished: %s\n",
775 full_fname(thisname));
778 io_error |= IOERR_GENERAL;
779 rsyserr(FERROR, save_errno, "readlink %s failed",
780 full_fname(thisname));
785 /* backup.c calls us with filter_level set to NO_FILTERS. */
786 if (filter_level == NO_FILTERS)
789 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
790 rprintf(FINFO, "skipping directory %s\n", thisname);
794 /* We only care about directories because we need to avoid recursing
795 * into a mount-point directory, not to avoid copying a symlinked
796 * file if -L (or similar) was specified. */
797 if (one_file_system && st.st_dev != filesystem_dev
798 && S_ISDIR(st.st_mode)) {
799 if (one_file_system > 1) {
801 rprintf(FINFO, "skipping mount-point dir %s\n",
806 flags |= FLAG_MOUNT_POINT;
809 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
812 if (lp_ignore_nonreadable(module_id)) {
814 if (!S_ISLNK(st.st_mode))
816 if (access(thisname, R_OK) != 0)
823 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
824 who_am_i(), thisname, filter_level);
827 if ((basename = strrchr(thisname, '/')) != NULL) {
828 dirname_len = ++basename - thisname; /* counts future '\0' */
829 if (lastdir_len == dirname_len - 1
830 && strncmp(thisname, lastdir, lastdir_len) == 0) {
832 dirname_len = 0; /* indicates no copy is needed */
840 basename_len = strlen(basename) + 1; /* count the '\0' */
843 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
848 sum_len = always_checksum && am_sender && S_ISREG(st.st_mode)
849 ? MD4_SUM_LENGTH : 0;
851 alloc_len = file_struct_len + dirname_len + basename_len
852 + linkname_len + sum_len;
854 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
856 if (!(bp = new_array(char, alloc_len)))
857 out_of_memory("make_file");
860 file = (struct file_struct *)bp;
861 memset(bp, 0, file_struct_len);
862 bp += file_struct_len;
865 file->modtime = st.st_mtime;
866 file->length = st.st_size;
867 file->mode = st.st_mode;
868 file->uid = st.st_uid;
869 file->gid = st.st_gid;
871 #ifdef SUPPORT_HARD_LINKS
872 if (flist && flist->hlink_pool) {
873 if (protocol_version < 28) {
874 if (S_ISREG(st.st_mode))
875 file->link_u.idev = pool_talloc(
876 flist->hlink_pool, struct idev, 1,
879 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
880 file->link_u.idev = pool_talloc(
881 flist->hlink_pool, struct idev, 1,
885 if (file->link_u.idev) {
886 file->F_DEV = st.st_dev;
887 file->F_INODE = st.st_ino;
892 file->dirname = lastdir = bp;
893 lastdir_len = dirname_len - 1;
894 memcpy(bp, dirname, dirname_len - 1);
898 file->dirname = dirname;
901 memcpy(bp, basename, basename_len);
904 #ifdef HAVE_STRUCT_STAT_ST_RDEV
905 if ((preserve_devices && IS_DEVICE(st.st_mode))
906 || (preserve_specials && IS_SPECIAL(st.st_mode)))
907 file->u.rdev = st.st_rdev;
913 memcpy(bp, linkname, linkname_len);
920 file_checksum(thisname, bp, st.st_size);
924 file->dir.root = flist_dir;
926 /* This code is only used by the receiver when it is building
927 * a list of files for a delete pass. */
928 if (keep_dirlinks && linkname_len && flist) {
930 int save_mode = file->mode;
931 file->mode = S_IFDIR; /* Find a directory with our name. */
932 if (flist_find(the_file_list, file) >= 0
933 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
934 file->modtime = st2.st_mtime;
935 file->length = st2.st_size;
936 file->mode = st2.st_mode;
937 file->uid = st2.st_uid;
938 file->gid = st2.st_gid;
941 file->mode = save_mode;
944 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
945 stats.total_size += st.st_size;
950 static struct file_struct *send_file_name(int f, struct file_list *flist,
951 char *fname, STRUCT_STAT *stp,
952 unsigned short flags)
954 struct file_struct *file;
956 file = make_file(fname, flist, stp, flags,
957 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
961 if (chmod_modes && !S_ISLNK(file->mode))
962 file->mode = tweak_mode(file->mode, chmod_modes);
964 maybe_emit_filelist_progress(flist->count + flist_count_offset);
968 if (file->basename[0]) {
969 flist->files[flist->count++] = file;
970 send_file_entry(file, f);
975 static void send_if_directory(int f, struct file_list *flist,
976 struct file_struct *file,
977 char *fbuf, unsigned int ol)
979 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
981 if (S_ISDIR(file->mode)
982 && !(file->flags & FLAG_MOUNT_POINT) && f_name(file, fbuf)) {
984 unsigned int len = strlen(fbuf);
985 if (len > 1 && fbuf[len-1] == '/')
987 if (len >= MAXPATHLEN - 1) {
988 io_error |= IOERR_GENERAL;
989 rprintf(FERROR, "skipping long-named directory: %s\n",
993 save_filters = push_local_filters(fbuf, len);
994 send_directory(f, flist, fbuf, len);
995 pop_local_filters(save_filters);
1002 /* This function is normally called by the sender, but the receiving side also
1003 * calls it from get_dirlist() with f set to -1 so that we just construct the
1004 * file list in memory without sending it over the wire. Also, get_dirlist()
1005 * might call this with f set to -2, which also indicates that local filter
1006 * rules should be ignored. */
1007 static void send_directory(int f, struct file_list *flist,
1008 char *fbuf, int len)
1014 int start = flist->count;
1016 if (!(d = opendir(fbuf))) {
1017 io_error |= IOERR_GENERAL;
1018 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1023 if (len != 1 || *fbuf != '/')
1026 remainder = MAXPATHLEN - (p - fbuf);
1028 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1029 char *dname = d_name(di);
1030 if (dname[0] == '.' && (dname[1] == '\0'
1031 || (dname[1] == '.' && dname[2] == '\0')))
1033 if (strlcpy(p, dname, remainder) >= remainder) {
1034 io_error |= IOERR_GENERAL;
1036 "cannot send long-named file %s\n",
1041 send_file_name(f, flist, fbuf, NULL, 0);
1047 io_error |= IOERR_GENERAL;
1048 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1054 int i, end = flist->count - 1;
1055 for (i = start; i <= end; i++)
1056 send_if_directory(f, flist, flist->files[i], fbuf, len);
1060 struct file_list *send_file_list(int f, int argc, char *argv[])
1064 char *p, *dir, olddir[sizeof curr_dir];
1065 char lastpath[MAXPATHLEN] = "";
1066 struct file_list *flist;
1067 struct timeval start_tv, end_tv;
1071 if (show_filelist_p())
1072 start_filelist_progress("building file list");
1074 start_write = stats.total_written;
1075 gettimeofday(&start_tv, NULL);
1077 flist = flist_new(WITH_HLINK, "send_file_list");
1079 io_start_buffering_out();
1080 if (filesfrom_fd >= 0) {
1081 if (argv[0] && !push_dir(argv[0])) {
1082 rsyserr(FERROR, errno, "push_dir %s failed",
1083 full_fname(argv[0]));
1084 exit_cleanup(RERR_FILESELECT);
1090 char fbuf[MAXPATHLEN];
1095 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1097 sanitize_path(fbuf, fbuf, "", 0);
1101 strlcpy(fbuf, *argv++, MAXPATHLEN);
1103 sanitize_path(fbuf, fbuf, "", 0);
1107 if (relative_paths) {
1108 /* We clean up fbuf below. */
1110 } else if (!len || fbuf[len - 1] == '/') {
1111 if (len == 2 && fbuf[0] == '.') {
1112 /* Turn "./" into just "." rather than "./." */
1115 if (len + 1 >= MAXPATHLEN)
1116 overflow_exit("send_file_list");
1121 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1122 && (len == 2 || fbuf[len-3] == '/')) {
1123 if (len + 2 >= MAXPATHLEN)
1124 overflow_exit("send_file_list");
1130 is_dot_dir = fbuf[len-1] == '.'
1131 && (len == 1 || fbuf[len-2] == '/');
1134 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1135 io_error |= IOERR_GENERAL;
1136 rsyserr(FERROR, errno, "link_stat %s failed",
1141 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1142 rprintf(FINFO, "skipping directory %s\n", fbuf);
1149 if (!relative_paths) {
1150 p = strrchr(fbuf, '/');
1157 len -= p - fbuf + 1;
1162 if ((p = strstr(fbuf, "/./")) != NULL) {
1168 len -= p - fbuf + 3;
1172 /* Get rid of trailing "/" and "/.". */
1174 if (fn[len - 1] == '/') {
1176 if (!--len && !dir) {
1181 else if (len >= 2 && fn[len - 1] == '.'
1182 && fn[len - 2] == '/') {
1184 if (!(len -= 2) && !dir) {
1192 /* Reject a ".." dir in the active part of the path. */
1193 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1194 if ((p[2] == '/' || p[2] == '\0')
1195 && (p == fn || p[-1] == '/')) {
1197 "found \"..\" dir in relative path: %s\n",
1199 exit_cleanup(RERR_SYNTAX);
1210 static char *lastdir;
1211 static int lastdir_len;
1213 strlcpy(olddir, curr_dir, sizeof olddir);
1215 if (!push_dir(dir)) {
1216 io_error |= IOERR_GENERAL;
1217 rsyserr(FERROR, errno, "push_dir %s failed",
1222 if (lastdir && strcmp(lastdir, dir) == 0) {
1223 flist_dir = lastdir;
1224 flist_dir_len = lastdir_len;
1226 flist_dir = lastdir = strdup(dir);
1227 flist_dir_len = lastdir_len = strlen(dir);
1232 memmove(fbuf, fn, len + 1);
1234 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1235 /* Send the implied directories at the start of the
1236 * source spec, so we get their permissions right. */
1237 char *lp = lastpath, *slash = fbuf;
1239 /* Skip any initial directories in our path that we
1240 * have in common with lastpath. */
1241 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1246 if (fn != p || (*lp && *lp != '/')) {
1247 int save_copy_links = copy_links;
1248 int save_xfer_dirs = xfer_dirs;
1249 copy_links |= copy_unsafe_links;
1251 while ((slash = strchr(slash+1, '/')) != 0) {
1253 send_file_name(f, flist, fbuf, NULL, 0);
1256 copy_links = save_copy_links;
1257 xfer_dirs = save_xfer_dirs;
1259 strlcpy(lastpath, fbuf, sizeof lastpath);
1264 if (one_file_system)
1265 filesystem_dev = st.st_dev;
1267 if (recurse || (xfer_dirs && is_dot_dir)) {
1268 struct file_struct *file;
1269 file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
1271 send_if_directory(f, flist, file, fbuf, len);
1273 send_file_name(f, flist, fbuf, &st, 0);
1278 if (!pop_dir(olddir)) {
1279 rsyserr(FERROR, errno, "pop_dir %s failed",
1280 full_fname(olddir));
1281 exit_cleanup(RERR_FILESELECT);
1286 gettimeofday(&end_tv, NULL);
1287 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1288 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1289 if (stats.flist_buildtime == 0)
1290 stats.flist_buildtime = 1;
1293 send_file_entry(NULL, f);
1295 if (show_filelist_p())
1296 finish_filelist_progress(flist);
1298 gettimeofday(&end_tv, NULL);
1299 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1300 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1302 if (flist->hlink_pool) {
1303 pool_destroy(flist->hlink_pool);
1304 flist->hlink_pool = NULL;
1307 /* Sort the list without removing any duplicates. This allows the
1308 * receiving side to ask for any name they like, which gives us the
1309 * flexibility to change the way we unduplicate names in the future
1310 * without causing a compatibility problem with older versions. */
1311 clean_flist(flist, 0, 0);
1315 /* send the io_error flag */
1316 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1319 stats.flist_size = stats.total_written - start_write;
1320 stats.num_files = flist->count;
1323 output_flist(flist);
1326 rprintf(FINFO, "send_file_list done\n");
1331 struct file_list *recv_file_list(int f)
1333 struct file_list *flist;
1334 unsigned short flags;
1337 if (show_filelist_p())
1338 start_filelist_progress("receiving file list");
1340 start_read = stats.total_read;
1342 flist = flist_new(WITH_HLINK, "recv_file_list");
1345 flist->malloced = 1000;
1346 flist->files = new_array(struct file_struct *, flist->malloced);
1350 while ((flags = read_byte(f)) != 0) {
1351 struct file_struct *file;
1353 flist_expand(flist);
1355 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1356 flags |= read_byte(f) << 8;
1357 file = receive_file_entry(flist, flags, f);
1359 if (S_ISREG(file->mode) || S_ISLNK(file->mode))
1360 stats.total_size += file->length;
1362 flist->files[flist->count++] = file;
1364 maybe_emit_filelist_progress(flist->count);
1367 rprintf(FINFO, "recv_file_name(%s)\n",
1368 f_name(file, NULL));
1371 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1374 rprintf(FINFO, "received %d names\n", flist->count);
1376 if (show_filelist_p())
1377 finish_filelist_progress(flist);
1379 clean_flist(flist, relative_paths, 1);
1382 recv_uid_list(f, flist);
1384 /* Recv the io_error flag */
1385 if (lp_ignore_errors(module_id) || ignore_errors)
1388 io_error |= read_int(f);
1392 output_flist(flist);
1396 for (i = 0; i < flist->count; i++)
1397 list_file_entry(flist->files[i]);
1401 rprintf(FINFO, "recv_file_list done\n");
1403 stats.flist_size = stats.total_read - start_read;
1404 stats.num_files = flist->count;
1409 out_of_memory("recv_file_list");
1410 return NULL; /* not reached */
1413 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1415 return f_name_cmp(*file1, *file2);
1418 /* Search for an identically-named item in the file list. Note that the
1419 * items must agree in their directory-ness, or no match is returned. */
1420 int flist_find(struct file_list *flist, struct file_struct *f)
1422 int low = flist->low, high = flist->high;
1423 int diff, mid, mid_up;
1425 while (low <= high) {
1426 mid = (low + high) / 2;
1427 if (flist->files[mid]->basename)
1430 /* Scan for the next non-empty entry using the cached
1431 * distance values. If the value isn't fully up-to-
1432 * date, update it. */
1433 mid_up = mid + flist->files[mid]->dir.depth;
1434 if (!flist->files[mid_up]->basename) {
1436 mid_up += flist->files[mid_up]->dir.depth;
1437 } while (!flist->files[mid_up]->basename);
1438 flist->files[mid]->dir.depth = mid_up - mid;
1440 if (mid_up > high) {
1441 /* If there's nothing left above us, set high to
1442 * a non-empty entry below us and continue. */
1443 high = mid - flist->files[mid]->length;
1444 if (!flist->files[high]->basename) {
1446 high -= flist->files[high]->length;
1447 } while (!flist->files[high]->basename);
1448 flist->files[mid]->length = mid - high;
1453 diff = f_name_cmp(flist->files[mid_up], f);
1455 if (protocol_version < 29
1456 && S_ISDIR(flist->files[mid_up]->mode)
1457 != S_ISDIR(f->mode))
1470 * Free up any resources a file_struct has allocated
1471 * and clear the file.
1473 void clear_file(struct file_struct *file, struct file_list *flist)
1475 if (flist->hlink_pool && file->link_u.idev)
1476 pool_free(flist->hlink_pool, 0, file->link_u.idev);
1477 memset(file, 0, file_struct_len);
1478 /* In an empty entry, dir.depth is an offset to the next non-empty
1479 * entry. Likewise for length in the opposite direction. We assume
1480 * that we're alone for now since flist_find() will adjust the counts
1481 * it runs into that aren't up-to-date. */
1482 file->length = file->dir.depth = 1;
1486 * allocate a new file list
1488 struct file_list *flist_new(int with_hlink, char *msg)
1490 struct file_list *flist;
1492 flist = new(struct file_list);
1496 memset(flist, 0, sizeof (struct file_list));
1498 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1499 out_of_memory, POOL_INTERN)))
1502 #ifdef SUPPORT_HARD_LINKS
1503 if (with_hlink && preserve_hard_links) {
1504 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1505 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1514 * free up all elements in a flist
1516 void flist_free(struct file_list *flist)
1518 pool_destroy(flist->file_pool);
1519 pool_destroy(flist->hlink_pool);
1525 * This routine ensures we don't have any duplicate names in our file list.
1526 * duplicate names can cause corruption because of the pipelining
1528 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1530 char fbuf[MAXPATHLEN];
1535 if (flist->count == 0) {
1540 qsort(flist->files, flist->count,
1541 sizeof flist->files[0], (int (*)())file_compare);
1543 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1544 if (flist->files[i]->basename) {
1549 flist->low = prev_i;
1550 while (++i < flist->count) {
1552 struct file_struct *file = flist->files[i];
1554 if (!file->basename)
1556 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1558 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1559 int save_mode = file->mode;
1560 /* Make sure that this directory doesn't duplicate a
1561 * non-directory earlier in the list. */
1562 flist->high = prev_i;
1563 file->mode = S_IFREG;
1564 j = flist_find(flist, file);
1565 file->mode = save_mode;
1569 struct file_struct *fp = flist->files[j];
1571 /* If one is a dir and the other is not, we want to
1572 * keep the dir because it might have contents in the
1574 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1575 if (S_ISDIR(file->mode))
1581 if (verbose > 1 && !am_server) {
1583 "removing duplicate name %s from file list (%d)\n",
1584 f_name(file, fbuf), drop);
1586 /* Make sure we don't lose track of a user-specified
1588 flist->files[keep]->flags |= flist->files[drop]->flags
1589 & (FLAG_TOP_DIR|FLAG_DEL_HERE);
1591 clear_file(flist->files[drop], flist);
1594 if (flist->low == drop) {
1596 j < i && !flist->files[j]->basename;
1605 flist->high = no_dups ? prev_i : flist->count - 1;
1608 /* We need to strip off the leading slashes for relative
1609 * paths, but this must be done _after_ the sorting phase. */
1610 for (i = flist->low; i <= flist->high; i++) {
1611 struct file_struct *file = flist->files[i];
1615 while (*file->dirname == '/')
1617 if (!*file->dirname)
1618 file->dirname = NULL;
1622 if (prune_empty_dirs && no_dups) {
1623 int j, prev_depth = 0;
1625 prev_i = 0; /* It's OK that this isn't really true. */
1627 for (i = flist->low; i <= flist->high; i++) {
1628 struct file_struct *fp, *file = flist->files[i];
1630 /* This temporarily abuses the dir.depth value for a
1631 * directory that is in a chain that might get pruned.
1632 * We restore the old value if it gets a reprieve. */
1633 if (S_ISDIR(file->mode) && file->dir.depth) {
1634 /* Dump empty dirs when coming back down. */
1635 for (j = prev_depth; j >= file->dir.depth; j--) {
1636 fp = flist->files[prev_i];
1637 if (fp->dir.depth >= 0)
1639 prev_i = -fp->dir.depth-1;
1640 clear_file(fp, flist);
1642 prev_depth = file->dir.depth;
1643 if (is_excluded(f_name(file, fbuf), 1,
1645 /* Keep dirs through this dir. */
1646 for (j = prev_depth-1; ; j--) {
1647 fp = flist->files[prev_i];
1648 if (fp->dir.depth >= 0)
1650 prev_i = -fp->dir.depth-1;
1654 file->dir.depth = -prev_i-1;
1657 /* Keep dirs through this non-dir. */
1658 for (j = prev_depth; ; j--) {
1659 fp = flist->files[prev_i];
1660 if (fp->dir.depth >= 0)
1662 prev_i = -fp->dir.depth-1;
1667 /* Dump empty all remaining empty dirs. */
1669 struct file_struct *fp = flist->files[prev_i];
1670 if (fp->dir.depth >= 0)
1672 prev_i = -fp->dir.depth-1;
1673 clear_file(fp, flist);
1676 for (i = flist->low; i <= flist->high; i++) {
1677 if (flist->files[i]->basename)
1681 for (i = flist->high; i >= flist->low; i--) {
1682 if (flist->files[i]->basename)
1689 static void output_flist(struct file_list *flist)
1691 char uidbuf[16], gidbuf[16], depthbuf[16];
1692 struct file_struct *file;
1693 const char *who = who_am_i();
1696 for (i = 0; i < flist->count; i++) {
1697 file = flist->files[i];
1698 if ((am_root || am_sender) && preserve_uid)
1699 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1702 if (preserve_gid && file->gid != GID_NONE)
1703 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1707 sprintf(depthbuf, "%d", file->dir.depth);
1708 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1709 who, i, am_sender ? NS(file->dir.root) : depthbuf,
1710 file->dirname ? file->dirname : "",
1711 file->dirname ? "/" : "", NS(file->basename),
1712 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
1713 (double)file->length, uidbuf, gidbuf, file->flags);
1717 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1718 enum fnc_type { t_PATH, t_ITEM };
1720 /* Compare the names of two file_struct entities, similar to how strcmp()
1721 * would do if it were operating on the joined strings.
1723 * Some differences beginning with protocol_version 29: (1) directory names
1724 * are compared with an assumed trailing slash so that they compare in a
1725 * way that would cause them to sort immediately prior to any content they
1726 * may have; (2) a directory of any name compares after a non-directory of
1727 * any name at the same depth; (3) a directory with name "." compares prior
1728 * to anything else. These changes mean that a directory and a non-dir
1729 * with the same name will not compare as equal (protocol_version >= 29).
1731 * The dirname component can be an empty string, but the basename component
1732 * cannot (and never is in the current codebase). The basename component
1733 * may be NULL (for a removed item), in which case it is considered to be
1734 * after any existing item. */
1735 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1738 const uchar *c1, *c2;
1739 enum fnc_state state1, state2;
1740 enum fnc_type type1, type2;
1741 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1743 if (!f1 || !f1->basename) {
1744 if (!f2 || !f2->basename)
1748 if (!f2 || !f2->basename)
1751 c1 = (uchar*)f1->dirname;
1752 c2 = (uchar*)f2->dirname;
1756 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1757 c1 = (uchar*)f1->basename;
1758 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1760 state1 = s_TRAILING;
1773 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1774 c2 = (uchar*)f2->basename;
1775 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1777 state2 = s_TRAILING;
1791 return type1 == t_PATH ? 1 : -1;
1794 if ((dif = (int)*c1++ - (int)*c2++) != 0)
1803 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1804 c1 = (uchar*)f1->basename;
1805 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1807 state1 = s_TRAILING;
1813 state1 = s_TRAILING;
1814 if (type1 == t_PATH) {
1823 if (*c2 && type1 != type2)
1824 return type1 == t_PATH ? 1 : -1;
1833 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1834 c2 = (uchar*)f2->basename;
1835 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1837 state2 = s_TRAILING;
1843 state2 = s_TRAILING;
1844 if (type2 == t_PATH) {
1856 return type1 == t_PATH ? 1 : -1;
1863 /* Return a copy of the full filename of a flist entry, using the indicated
1864 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
1865 * done because we checked the size when creating the file_struct entry.
1867 char *f_name(struct file_struct *f, char *fbuf)
1869 if (!f || !f->basename)
1873 static char names[5][MAXPATHLEN];
1874 static unsigned int n;
1876 n = (n + 1) % (sizeof names / sizeof names[0]);
1882 int len = strlen(f->dirname);
1883 memcpy(fbuf, f->dirname, len);
1885 strcpy(fbuf + len + 1, f->basename);
1887 strcpy(fbuf, f->basename);
1892 /* Do a non-recursive scan of the named directory, possibly ignoring all
1893 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1894 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1895 * buffer (the functions we call will append names onto the end, but the old
1896 * dir value will be restored on exit). */
1897 struct file_list *get_dirlist(char *dirname, int dlen,
1898 int ignore_filter_rules)
1900 struct file_list *dirlist;
1901 char dirbuf[MAXPATHLEN];
1902 int save_recurse = recurse;
1903 int save_xfer_dirs = xfer_dirs;
1906 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1907 if (dlen >= MAXPATHLEN)
1912 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1916 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
1917 xfer_dirs = save_xfer_dirs;
1918 recurse = save_recurse;
1920 flist_count_offset += dirlist->count;
1922 clean_flist(dirlist, 0, 0);
1925 output_flist(dirlist);