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(FCLIENT, "%s ... ", kind);
94 if (verbose > 1 || do_progress)
95 rprintf(FCLIENT, "\n");
99 static void emit_filelist_progress(int count)
101 rprintf(FCLIENT, " %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),
152 /* Stat either a symlink or its referent, depending on the settings of
153 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
155 * If path is the name of a symlink, then the linkbuf buffer (which must hold
156 * MAXPATHLEN chars) will be set to the symlink's target string.
158 * The stat structure pointed to by stp will contain information about the
159 * link or the referent as appropriate, if they exist. */
160 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
163 if (link_stat(path, stp, copy_dirlinks) < 0)
165 if (S_ISLNK(stp->st_mode)) {
166 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
169 linkbuf[llen] = '\0';
170 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
172 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
175 return safe_stat(path, stp);
180 return do_stat(path, stp);
184 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
188 return safe_stat(path, stp);
189 if (do_lstat(path, stp) < 0)
191 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
193 if (safe_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
198 return do_stat(path, stp);
202 /* This function is used to check if a file should be included/excluded
203 * from the list of files based on its name and type etc. The value of
204 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
205 static int is_excluded(char *fname, int is_dir, int filter_level)
207 #if 0 /* This currently never happens, so avoid a useless compare. */
208 if (filter_level == NO_FILTERS)
212 /* never exclude '.', even if somebody does --exclude '*' */
213 if (fname[0] == '.' && !fname[1])
215 /* Handle the -R version of the '.' dir. */
216 if (fname[0] == '/') {
217 int len = strlen(fname);
218 if (fname[len-1] == '.' && fname[len-2] == '/')
222 if (server_filter_list.head
223 && check_filter(&server_filter_list, fname, is_dir) < 0)
225 if (filter_level != ALL_FILTERS)
228 && check_filter(&filter_list, fname, is_dir) < 0)
233 static int to_wire_mode(mode_t mode)
236 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
237 return (mode & ~(_S_IFMT)) | 0120000;
242 static mode_t from_wire_mode(int mode)
244 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
245 return (mode & ~(_S_IFMT)) | _S_IFLNK;
249 static void send_directory(int f, struct file_list *flist,
250 char *fbuf, int len);
252 static char *flist_dir;
253 static int flist_dir_len;
257 * Make sure @p flist is big enough to hold at least @p flist->count
260 void flist_expand(struct file_list *flist)
262 struct file_struct **new_ptr;
264 if (flist->count < flist->malloced)
267 if (flist->malloced < FLIST_START)
268 flist->malloced = FLIST_START;
269 else if (flist->malloced >= FLIST_LINEAR)
270 flist->malloced += FLIST_LINEAR;
272 flist->malloced *= 2;
275 * In case count jumped or we are starting the list
276 * with a known size just set it.
278 if (flist->malloced < flist->count)
279 flist->malloced = flist->count;
281 new_ptr = realloc_array(flist->files, struct file_struct *,
284 if (verbose >= 2 && flist->malloced != FLIST_START) {
285 rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n",
287 (double)sizeof flist->files[0] * flist->malloced,
288 (new_ptr == flist->files) ? " not" : "");
291 flist->files = new_ptr;
294 out_of_memory("flist_expand");
297 static void send_file_entry(struct file_struct *file, int f)
299 unsigned short flags;
300 static time_t modtime;
304 static uint32 rdev_major;
307 static char lastname[MAXPATHLEN];
308 char fname[MAXPATHLEN];
316 modtime = 0, mode = 0;
317 dev = 0, rdev = makedev(0, 0);
326 flags = file->flags & XMIT_TOP_DIR;
328 if (file->mode == mode)
329 flags |= XMIT_SAME_MODE;
332 if ((preserve_devices && IS_DEVICE(mode))
333 || (preserve_specials && IS_SPECIAL(mode))) {
334 if (protocol_version < 28) {
335 if (file->u.rdev == rdev)
336 flags |= XMIT_SAME_RDEV_pre28;
341 if ((uint32)major(rdev) == rdev_major)
342 flags |= XMIT_SAME_RDEV_MAJOR;
344 rdev_major = major(rdev);
345 if ((uint32)minor(rdev) <= 0xFFu)
346 flags |= XMIT_RDEV_MINOR_IS_SMALL;
348 } else if (protocol_version < 28)
349 rdev = makedev(0, 0);
350 if (file->uid == uid)
351 flags |= XMIT_SAME_UID;
354 if (file->gid == gid)
355 flags |= XMIT_SAME_GID;
358 if (file->modtime == modtime)
359 flags |= XMIT_SAME_TIME;
361 modtime = file->modtime;
363 #ifdef SUPPORT_HARD_LINKS
364 if (file->link_u.idev) {
365 if (file->F_DEV == dev) {
366 if (protocol_version >= 28)
367 flags |= XMIT_SAME_DEV;
370 flags |= XMIT_HAS_IDEV_DATA;
375 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
377 l2 = strlen(fname+l1);
380 flags |= XMIT_SAME_NAME;
382 flags |= XMIT_LONG_NAME;
384 /* We must make sure we don't send a zero flag byte or the
385 * other end will terminate the flist transfer. Note that
386 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
387 * it's harmless way to add a bit to the first flag byte. */
388 if (protocol_version >= 28) {
389 if (!flags && !S_ISDIR(mode))
390 flags |= XMIT_TOP_DIR;
391 if ((flags & 0xFF00) || !flags) {
392 flags |= XMIT_EXTENDED_FLAGS;
393 write_byte(f, flags);
394 write_byte(f, flags >> 8);
396 write_byte(f, flags);
399 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
400 write_byte(f, flags);
402 if (flags & XMIT_SAME_NAME)
404 if (flags & XMIT_LONG_NAME)
408 write_buf(f, fname + l1, l2);
410 write_longint(f, file->length);
411 if (!(flags & XMIT_SAME_TIME))
412 write_int(f, modtime);
413 if (!(flags & XMIT_SAME_MODE))
414 write_int(f, to_wire_mode(mode));
415 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
420 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
425 if ((preserve_devices && IS_DEVICE(mode))
426 || (preserve_specials && IS_SPECIAL(mode))) {
427 if (protocol_version < 28) {
428 if (!(flags & XMIT_SAME_RDEV_pre28))
429 write_int(f, (int)rdev);
431 if (!(flags & XMIT_SAME_RDEV_MAJOR))
432 write_int(f, major(rdev));
433 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
434 write_byte(f, minor(rdev));
436 write_int(f, minor(rdev));
441 if (preserve_links && S_ISLNK(mode)) {
442 int len = strlen(file->u.link);
444 write_buf(f, file->u.link, len);
448 #ifdef SUPPORT_HARD_LINKS
449 if (flags & XMIT_HAS_IDEV_DATA) {
450 if (protocol_version < 26) {
451 /* 32-bit dev_t and ino_t */
453 write_int(f, file->F_INODE);
455 /* 64-bit dev_t and ino_t */
456 if (!(flags & XMIT_SAME_DEV))
457 write_longint(f, dev);
458 write_longint(f, file->F_INODE);
463 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
468 /* Prior to 28, we sent a useless set of nulls. */
471 write_buf(f, sum, checksum_len);
474 strlcpy(lastname, fname, MAXPATHLEN);
477 static struct file_struct *receive_file_entry(struct file_list *flist,
478 unsigned short flags, int f)
480 static time_t modtime;
484 static uint32 rdev_major;
487 static char lastname[MAXPATHLEN], *lastdir;
488 static int lastdir_depth, lastdir_len = -1;
489 static unsigned int del_hier_name_len = 0;
490 static int in_del_hier = 0;
491 char thisname[MAXPATHLEN];
492 unsigned int l1 = 0, l2 = 0;
493 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
495 char *basename, *dirname, *bp;
496 struct file_struct *file;
499 modtime = 0, mode = 0;
500 dev = 0, rdev = makedev(0, 0);
509 if (flags & XMIT_SAME_NAME)
512 if (flags & XMIT_LONG_NAME)
517 if (l2 >= MAXPATHLEN - l1) {
519 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
520 flags, l1, l2, lastname);
521 overflow_exit("receive_file_entry");
524 strlcpy(thisname, lastname, l1 + 1);
525 read_sbuf(f, &thisname[l1], l2);
526 thisname[l1 + l2] = 0;
528 strlcpy(lastname, thisname, MAXPATHLEN);
530 clean_fname(thisname, 0);
533 sanitize_path(thisname, thisname, "", 0, NULL);
535 if ((basename = strrchr(thisname, '/')) != NULL) {
536 dirname_len = ++basename - thisname; /* counts future '\0' */
537 if (lastdir_len == dirname_len - 1
538 && strncmp(thisname, lastdir, lastdir_len) == 0) {
540 dirname_len = 0; /* indicates no copy is needed */
548 basename_len = strlen(basename) + 1; /* count the '\0' */
550 file_length = read_longint(f);
551 if (!(flags & XMIT_SAME_TIME))
552 modtime = (time_t)read_int(f);
553 if (!(flags & XMIT_SAME_MODE))
554 mode = from_wire_mode(read_int(f));
556 if (chmod_modes && !S_ISLNK(mode))
557 mode = tweak_mode(mode, chmod_modes);
559 if (preserve_uid && !(flags & XMIT_SAME_UID))
560 uid = (uid_t)read_int(f);
561 if (preserve_gid && !(flags & XMIT_SAME_GID))
562 gid = (gid_t)read_int(f);
564 if ((preserve_devices && IS_DEVICE(mode))
565 || (preserve_specials && IS_SPECIAL(mode))) {
566 if (protocol_version < 28) {
567 if (!(flags & XMIT_SAME_RDEV_pre28))
568 rdev = (dev_t)read_int(f);
571 if (!(flags & XMIT_SAME_RDEV_MAJOR))
572 rdev_major = read_int(f);
573 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
574 rdev_minor = read_byte(f);
576 rdev_minor = read_int(f);
577 rdev = makedev(rdev_major, rdev_minor);
579 } else if (protocol_version < 28)
580 rdev = makedev(0, 0);
583 if (preserve_links && S_ISLNK(mode)) {
584 linkname_len = read_int(f) + 1; /* count the '\0' */
585 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
586 rprintf(FERROR, "overflow: linkname_len=%d\n",
588 overflow_exit("receive_file_entry");
595 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
597 alloc_len = file_struct_len + dirname_len + basename_len
598 + linkname_len + sum_len;
599 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
601 file = (struct file_struct *)bp;
602 memset(bp, 0, file_struct_len);
603 bp += file_struct_len;
605 file->modtime = modtime;
606 file->length = file_length;
612 file->dirname = lastdir = bp;
613 lastdir_len = dirname_len - 1;
614 memcpy(bp, dirname, dirname_len - 1);
617 lastdir_depth = count_dir_elements(lastdir);
618 file->dir.depth = lastdir_depth + 1;
619 } else if (dirname) {
620 file->dirname = dirname; /* we're reusing lastname */
621 file->dir.depth = lastdir_depth + 1;
626 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
628 if (flags & XMIT_TOP_DIR) {
629 in_del_hier = recurse;
630 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
631 if (relative_paths && del_hier_name_len > 2
632 && lastname[del_hier_name_len-1] == '.'
633 && lastname[del_hier_name_len-2] == '/')
634 del_hier_name_len -= 2;
635 file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
636 } else if (in_del_hier) {
637 if (!relative_paths || !del_hier_name_len
638 || (l1 >= del_hier_name_len
639 && lastname[del_hier_name_len] == '/'))
640 file->flags |= FLAG_DEL_HERE;
647 memcpy(bp, basename, basename_len);
650 if ((preserve_devices && IS_DEVICE(mode))
651 || (preserve_specials && IS_SPECIAL(mode)))
657 read_sbuf(f, bp, linkname_len - 1);
662 #ifdef SUPPORT_HARD_LINKS
663 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
664 flags |= XMIT_HAS_IDEV_DATA;
665 if (flags & XMIT_HAS_IDEV_DATA) {
667 if (protocol_version < 26) {
671 if (!(flags & XMIT_SAME_DEV))
672 dev = read_longint(f);
673 inode = read_longint(f);
675 if (flist->hlink_pool) {
676 file->link_u.idev = pool_talloc(flist->hlink_pool,
677 struct idev, 1, "inode_table");
678 file->F_INODE = inode;
684 if (always_checksum && (sum_len || protocol_version < 28)) {
687 file->u.sum = sum = bp;
690 /* Prior to 28, we get a useless set of nulls. */
693 read_buf(f, sum, checksum_len);
700 * Create a file_struct for a named file by reading its stat()
701 * information and performing extensive checks against global
704 * @return the new file, or NULL if there was an error or this file
705 * should be excluded.
707 * @todo There is a small optimization opportunity here to avoid
708 * stat()ing the file in some circumstances, which has a certain cost.
709 * We are called immediately after doing readdir(), and so we may
710 * already know the d_type of the file. We could for example avoid
711 * statting directories if we're not recursing, but this is not a very
712 * important case. Some systems may not have d_type.
714 struct file_struct *make_file(char *fname, struct file_list *flist,
715 STRUCT_STAT *stp, unsigned short flags,
718 static char *lastdir;
719 static int lastdir_len = -1;
720 struct file_struct *file;
722 char sum[SUM_LENGTH];
723 char thisname[MAXPATHLEN];
724 char linkname[MAXPATHLEN];
725 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
726 char *basename, *dirname, *bp;
728 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
731 if (strlcpy(thisname, fname, sizeof thisname)
732 >= sizeof thisname - flist_dir_len) {
733 rprintf(FINFO, "skipping overly long name: %s\n", fname);
736 clean_fname(thisname, 0);
738 sanitize_path(thisname, thisname, "", 0, NULL);
740 memset(sum, 0, SUM_LENGTH);
742 if (stp && S_ISDIR(stp->st_mode))
743 st = *stp; /* Needed for "symlink/." with --relative. */
744 else if (readlink_stat(thisname, &st, linkname) != 0) {
745 int save_errno = errno;
746 /* See if file is excluded before reporting an error. */
747 if (filter_level != NO_FILTERS
748 && is_excluded(thisname, 0, filter_level))
750 if (save_errno == ENOENT) {
752 /* Avoid "vanished" error if symlink points nowhere. */
753 if (copy_links && do_lstat(thisname, &st) == 0
754 && S_ISLNK(st.st_mode)) {
755 io_error |= IOERR_GENERAL;
756 rprintf(FERROR, "symlink has no referent: %s\n",
757 full_fname(thisname));
761 enum logcode c = am_daemon && protocol_version < 28
763 io_error |= IOERR_VANISHED;
764 rprintf(c, "file has vanished: %s\n",
765 full_fname(thisname));
768 io_error |= IOERR_GENERAL;
769 rsyserr(FERROR, save_errno, "readlink %s failed",
770 full_fname(thisname));
775 /* backup.c calls us with filter_level set to NO_FILTERS. */
776 if (filter_level == NO_FILTERS)
779 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
780 rprintf(FINFO, "skipping directory %s\n", thisname);
784 /* We only care about directories because we need to avoid recursing
785 * into a mount-point directory, not to avoid copying a symlinked
786 * file if -L (or similar) was specified. */
787 if (one_file_system && st.st_dev != filesystem_dev
788 && S_ISDIR(st.st_mode)) {
789 if (one_file_system > 1) {
791 rprintf(FINFO, "skipping mount-point dir %s\n",
796 flags |= FLAG_MOUNT_POINT;
799 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
802 if (lp_ignore_nonreadable(module_id)) {
804 if (!S_ISLNK(st.st_mode))
806 if (access(thisname, R_OK) != 0)
813 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
814 who_am_i(), thisname, filter_level);
817 if ((basename = strrchr(thisname, '/')) != NULL) {
818 dirname_len = ++basename - thisname; /* counts future '\0' */
819 if (lastdir_len == dirname_len - 1
820 && strncmp(thisname, lastdir, lastdir_len) == 0) {
822 dirname_len = 0; /* indicates no copy is needed */
830 basename_len = strlen(basename) + 1; /* count the '\0' */
833 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
838 sum_len = always_checksum && am_sender && S_ISREG(st.st_mode)
839 ? MD4_SUM_LENGTH : 0;
841 alloc_len = file_struct_len + dirname_len + basename_len
842 + linkname_len + sum_len;
844 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
846 if (!(bp = new_array(char, alloc_len)))
847 out_of_memory("make_file");
850 file = (struct file_struct *)bp;
851 memset(bp, 0, file_struct_len);
852 bp += file_struct_len;
855 file->modtime = st.st_mtime;
856 file->length = st.st_size;
857 file->mode = st.st_mode;
858 file->uid = st.st_uid;
859 file->gid = st.st_gid;
861 #ifdef SUPPORT_HARD_LINKS
862 if (flist && flist->hlink_pool) {
863 if (protocol_version < 28) {
864 if (S_ISREG(st.st_mode))
865 file->link_u.idev = pool_talloc(
866 flist->hlink_pool, struct idev, 1,
869 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
870 file->link_u.idev = pool_talloc(
871 flist->hlink_pool, struct idev, 1,
875 if (file->link_u.idev) {
876 file->F_DEV = st.st_dev;
877 file->F_INODE = st.st_ino;
882 file->dirname = lastdir = bp;
883 lastdir_len = dirname_len - 1;
884 memcpy(bp, dirname, dirname_len - 1);
888 file->dirname = dirname;
891 memcpy(bp, basename, basename_len);
894 #ifdef HAVE_STRUCT_STAT_ST_RDEV
895 if ((preserve_devices && IS_DEVICE(st.st_mode))
896 || (preserve_specials && IS_SPECIAL(st.st_mode)))
897 file->u.rdev = st.st_rdev;
903 memcpy(bp, linkname, linkname_len);
910 file_checksum(thisname, bp, st.st_size);
914 file->dir.root = flist_dir;
916 /* This code is only used by the receiver when it is building
917 * a list of files for a delete pass. */
918 if (keep_dirlinks && linkname_len && flist) {
920 int save_mode = file->mode;
921 file->mode = S_IFDIR; /* Find a directory with our name. */
922 if (flist_find(the_file_list, file) >= 0
923 && safe_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
924 file->modtime = st2.st_mtime;
925 file->length = st2.st_size;
926 file->mode = st2.st_mode;
927 file->uid = st2.st_uid;
928 file->gid = st2.st_gid;
931 file->mode = save_mode;
934 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
935 stats.total_size += st.st_size;
940 static struct file_struct *send_file_name(int f, struct file_list *flist,
941 char *fname, STRUCT_STAT *stp,
942 unsigned short flags)
944 struct file_struct *file;
946 file = make_file(fname, flist, stp, flags,
947 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
951 if (chmod_modes && !S_ISLNK(file->mode))
952 file->mode = tweak_mode(file->mode, chmod_modes);
954 maybe_emit_filelist_progress(flist->count + flist_count_offset);
958 if (file->basename[0]) {
959 flist->files[flist->count++] = file;
960 send_file_entry(file, f);
965 static void send_if_directory(int f, struct file_list *flist,
966 struct file_struct *file,
967 char *fbuf, unsigned int ol)
969 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
971 if (S_ISDIR(file->mode)
972 && !(file->flags & FLAG_MOUNT_POINT) && f_name(file, fbuf)) {
974 unsigned int len = strlen(fbuf);
975 if (len > 1 && fbuf[len-1] == '/')
977 if (len >= MAXPATHLEN - 1) {
978 io_error |= IOERR_GENERAL;
979 rprintf(FERROR, "skipping long-named directory: %s\n",
983 save_filters = push_local_filters(fbuf, len);
984 send_directory(f, flist, fbuf, len);
985 pop_local_filters(save_filters);
992 /* This function is normally called by the sender, but the receiving side also
993 * calls it from get_dirlist() with f set to -1 so that we just construct the
994 * file list in memory without sending it over the wire. Also, get_dirlist()
995 * might call this with f set to -2, which also indicates that local filter
996 * rules should be ignored. */
997 static void send_directory(int f, struct file_list *flist,
1004 int start = flist->count;
1006 if (!(d = opendir(fbuf))) {
1007 io_error |= IOERR_GENERAL;
1008 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1013 if (len != 1 || *fbuf != '/')
1016 remainder = MAXPATHLEN - (p - fbuf);
1018 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1019 char *dname = d_name(di);
1020 if (dname[0] == '.' && (dname[1] == '\0'
1021 || (dname[1] == '.' && dname[2] == '\0')))
1023 if (strlcpy(p, dname, remainder) >= remainder) {
1024 io_error |= IOERR_GENERAL;
1026 "cannot send long-named file %s\n",
1031 send_file_name(f, flist, fbuf, NULL, 0);
1037 io_error |= IOERR_GENERAL;
1038 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1044 int i, end = flist->count - 1;
1045 for (i = start; i <= end; i++)
1046 send_if_directory(f, flist, flist->files[i], fbuf, len);
1050 struct file_list *send_file_list(int f, int argc, char *argv[])
1054 char *p, *dir, olddir[sizeof curr_dir];
1055 char lastpath[MAXPATHLEN] = "";
1056 struct file_list *flist;
1057 struct timeval start_tv, end_tv;
1061 rprintf(FLOG, "building file list\n");
1062 if (show_filelist_p())
1063 start_filelist_progress("building file list");
1065 start_write = stats.total_written;
1066 gettimeofday(&start_tv, NULL);
1068 flist = flist_new(WITH_HLINK, "send_file_list");
1070 io_start_buffering_out();
1071 if (filesfrom_fd >= 0) {
1073 die_on_unsafe_path(argv[0], 0);
1074 if (argv[0] && !push_dir(argv[0])) {
1075 rsyserr(FERROR, errno, "push_dir %s failed",
1076 full_fname(argv[0]));
1077 exit_cleanup(RERR_FILESELECT);
1083 char fbuf[MAXPATHLEN];
1088 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1090 sanitize_path(fbuf, fbuf, "", 0, NULL);
1094 strlcpy(fbuf, *argv++, MAXPATHLEN);
1096 sanitize_path(fbuf, fbuf, "", 0, NULL);
1100 if (relative_paths) {
1101 /* We clean up fbuf below. */
1103 } else if (!len || fbuf[len - 1] == '/') {
1104 if (len == 2 && fbuf[0] == '.') {
1105 /* Turn "./" into just "." rather than "./." */
1108 if (len + 1 >= MAXPATHLEN)
1109 overflow_exit("send_file_list");
1114 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1115 && (len == 2 || fbuf[len-3] == '/')) {
1116 if (len + 2 >= MAXPATHLEN)
1117 overflow_exit("send_file_list");
1123 is_dot_dir = fbuf[len-1] == '.'
1124 && (len == 1 || fbuf[len-2] == '/');
1128 die_on_unsafe_path(fbuf, 1);
1129 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1130 io_error |= IOERR_GENERAL;
1131 rsyserr(FERROR, errno, "link_stat %s failed",
1136 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1137 rprintf(FINFO, "skipping directory %s\n", fbuf);
1144 if (!relative_paths) {
1145 p = strrchr(fbuf, '/');
1152 len -= p - fbuf + 1;
1157 if ((p = strstr(fbuf, "/./")) != NULL) {
1163 len -= p - fbuf + 3;
1167 /* Get rid of trailing "/" and "/.". */
1169 if (fn[len - 1] == '/') {
1171 if (!--len && !dir) {
1176 else if (len >= 2 && fn[len - 1] == '.'
1177 && fn[len - 2] == '/') {
1179 if (!(len -= 2) && !dir) {
1187 /* Reject a ".." dir in the active part of the path. */
1188 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1189 if ((p[2] == '/' || p[2] == '\0')
1190 && (p == fn || p[-1] == '/')) {
1192 "found \"..\" dir in relative path: %s\n",
1194 exit_cleanup(RERR_SYNTAX);
1205 static char *lastdir;
1206 static int lastdir_len;
1208 strlcpy(olddir, curr_dir, sizeof olddir);
1210 if (!push_dir(dir)) {
1211 io_error |= IOERR_GENERAL;
1212 rsyserr(FERROR, errno, "push_dir %s failed",
1217 if (lastdir && strcmp(lastdir, dir) == 0) {
1218 flist_dir = lastdir;
1219 flist_dir_len = lastdir_len;
1221 flist_dir = lastdir = strdup(dir);
1222 flist_dir_len = lastdir_len = strlen(dir);
1227 memmove(fbuf, fn, len + 1);
1229 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1230 /* Send the implied directories at the start of the
1231 * source spec, so we get their permissions right. */
1232 char *lp = lastpath, *slash = fbuf;
1234 /* Skip any initial directories in our path that we
1235 * have in common with lastpath. */
1236 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1241 if (fn != p || (*lp && *lp != '/')) {
1242 int save_copy_links = copy_links;
1243 int save_xfer_dirs = xfer_dirs;
1244 copy_links |= copy_unsafe_links;
1246 while ((slash = strchr(slash+1, '/')) != 0) {
1248 send_file_name(f, flist, fbuf, NULL, 0);
1251 copy_links = save_copy_links;
1252 xfer_dirs = save_xfer_dirs;
1254 strlcpy(lastpath, fbuf, sizeof lastpath);
1259 if (one_file_system)
1260 filesystem_dev = st.st_dev;
1262 if (recurse || (xfer_dirs && is_dot_dir)) {
1263 struct file_struct *file;
1264 file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
1266 send_if_directory(f, flist, file, fbuf, len);
1268 send_file_name(f, flist, fbuf, &st, 0);
1273 if (!pop_dir(olddir)) {
1274 rsyserr(FERROR, errno, "pop_dir %s failed",
1275 full_fname(olddir));
1276 exit_cleanup(RERR_FILESELECT);
1281 gettimeofday(&end_tv, NULL);
1282 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1283 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1284 if (stats.flist_buildtime == 0)
1285 stats.flist_buildtime = 1;
1288 send_file_entry(NULL, f);
1290 if (show_filelist_p())
1291 finish_filelist_progress(flist);
1293 gettimeofday(&end_tv, NULL);
1294 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1295 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1297 if (flist->hlink_pool) {
1298 pool_destroy(flist->hlink_pool);
1299 flist->hlink_pool = NULL;
1302 /* Sort the list without removing any duplicates. This allows the
1303 * receiving side to ask for any name they like, which gives us the
1304 * flexibility to change the way we unduplicate names in the future
1305 * without causing a compatibility problem with older versions. */
1306 clean_flist(flist, 0, 0);
1310 /* send the io_error flag */
1311 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1314 stats.flist_size = stats.total_written - start_write;
1315 stats.num_files = flist->count;
1318 output_flist(flist);
1321 rprintf(FINFO, "send_file_list done\n");
1326 struct file_list *recv_file_list(int f)
1328 struct file_list *flist;
1329 unsigned short flags;
1332 rprintf(FLOG, "receiving file list\n");
1333 if (show_filelist_p())
1334 start_filelist_progress("receiving file list");
1336 start_read = stats.total_read;
1338 flist = flist_new(WITH_HLINK, "recv_file_list");
1341 flist->malloced = 1000;
1342 flist->files = new_array(struct file_struct *, flist->malloced);
1346 while ((flags = read_byte(f)) != 0) {
1347 struct file_struct *file;
1349 flist_expand(flist);
1351 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1352 flags |= read_byte(f) << 8;
1353 file = receive_file_entry(flist, flags, f);
1355 if (S_ISREG(file->mode) || S_ISLNK(file->mode))
1356 stats.total_size += file->length;
1358 flist->files[flist->count++] = file;
1360 maybe_emit_filelist_progress(flist->count);
1363 rprintf(FINFO, "recv_file_name(%s)\n",
1364 f_name(file, NULL));
1367 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1370 rprintf(FINFO, "received %d names\n", flist->count);
1372 if (show_filelist_p())
1373 finish_filelist_progress(flist);
1375 clean_flist(flist, relative_paths, 1);
1378 recv_uid_list(f, flist);
1380 /* Recv the io_error flag */
1381 if (lp_ignore_errors(module_id) || ignore_errors)
1384 io_error |= read_int(f);
1388 output_flist(flist);
1392 for (i = 0; i < flist->count; i++)
1393 list_file_entry(flist->files[i]);
1397 rprintf(FINFO, "recv_file_list done\n");
1399 stats.flist_size = stats.total_read - start_read;
1400 stats.num_files = flist->count;
1405 out_of_memory("recv_file_list");
1406 return NULL; /* not reached */
1409 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1411 return f_name_cmp(*file1, *file2);
1414 /* Search for an identically-named item in the file list. Note that the
1415 * items must agree in their directory-ness, or no match is returned. */
1416 int flist_find(struct file_list *flist, struct file_struct *f)
1418 int low = flist->low, high = flist->high;
1419 int diff, mid, mid_up;
1421 while (low <= high) {
1422 mid = (low + high) / 2;
1423 if (flist->files[mid]->basename)
1426 /* Scan for the next non-empty entry using the cached
1427 * distance values. If the value isn't fully up-to-
1428 * date, update it. */
1429 mid_up = mid + flist->files[mid]->dir.depth;
1430 if (!flist->files[mid_up]->basename) {
1432 mid_up += flist->files[mid_up]->dir.depth;
1433 } while (!flist->files[mid_up]->basename);
1434 flist->files[mid]->dir.depth = mid_up - mid;
1436 if (mid_up > high) {
1437 /* If there's nothing left above us, set high to
1438 * a non-empty entry below us and continue. */
1439 high = mid - flist->files[mid]->length;
1440 if (!flist->files[high]->basename) {
1442 high -= flist->files[high]->length;
1443 } while (!flist->files[high]->basename);
1444 flist->files[mid]->length = mid - high;
1449 diff = f_name_cmp(flist->files[mid_up], f);
1451 if (protocol_version < 29
1452 && S_ISDIR(flist->files[mid_up]->mode)
1453 != S_ISDIR(f->mode))
1466 * Free up any resources a file_struct has allocated
1467 * and clear the file.
1469 void clear_file(struct file_struct *file, struct file_list *flist)
1471 if (flist->hlink_pool && file->link_u.idev)
1472 pool_free(flist->hlink_pool, 0, file->link_u.idev);
1473 memset(file, 0, file_struct_len);
1474 /* In an empty entry, dir.depth is an offset to the next non-empty
1475 * entry. Likewise for length in the opposite direction. We assume
1476 * that we're alone for now since flist_find() will adjust the counts
1477 * it runs into that aren't up-to-date. */
1478 file->length = file->dir.depth = 1;
1482 * allocate a new file list
1484 struct file_list *flist_new(int with_hlink, char *msg)
1486 struct file_list *flist;
1488 flist = new(struct file_list);
1492 memset(flist, 0, sizeof (struct file_list));
1494 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1495 out_of_memory, POOL_INTERN)))
1498 #ifdef SUPPORT_HARD_LINKS
1499 if (with_hlink && preserve_hard_links) {
1500 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1501 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1510 * free up all elements in a flist
1512 void flist_free(struct file_list *flist)
1514 pool_destroy(flist->file_pool);
1515 pool_destroy(flist->hlink_pool);
1521 * This routine ensures we don't have any duplicate names in our file list.
1522 * duplicate names can cause corruption because of the pipelining
1524 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1526 char fbuf[MAXPATHLEN];
1531 if (flist->count == 0) {
1536 qsort(flist->files, flist->count,
1537 sizeof flist->files[0], (int (*)())file_compare);
1539 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1540 if (flist->files[i]->basename) {
1545 flist->low = prev_i;
1546 while (++i < flist->count) {
1548 struct file_struct *file = flist->files[i];
1550 if (!file->basename)
1552 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1554 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1555 int save_mode = file->mode;
1556 /* Make sure that this directory doesn't duplicate a
1557 * non-directory earlier in the list. */
1558 flist->high = prev_i;
1559 file->mode = S_IFREG;
1560 j = flist_find(flist, file);
1561 file->mode = save_mode;
1565 struct file_struct *fp = flist->files[j];
1567 /* If one is a dir and the other is not, we want to
1568 * keep the dir because it might have contents in the
1570 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1571 if (S_ISDIR(file->mode))
1577 if (verbose > 1 && !am_server) {
1579 "removing duplicate name %s from file list (%d)\n",
1580 f_name(file, fbuf), drop);
1582 /* Make sure we don't lose track of a user-specified
1584 flist->files[keep]->flags |= flist->files[drop]->flags
1585 & (FLAG_TOP_DIR|FLAG_DEL_HERE);
1587 clear_file(flist->files[drop], flist);
1590 if (flist->low == drop) {
1592 j < i && !flist->files[j]->basename;
1601 flist->high = no_dups ? prev_i : flist->count - 1;
1604 /* We need to strip off the leading slashes for relative
1605 * paths, but this must be done _after_ the sorting phase. */
1606 for (i = flist->low; i <= flist->high; i++) {
1607 struct file_struct *file = flist->files[i];
1611 while (*file->dirname == '/')
1613 if (!*file->dirname)
1614 file->dirname = NULL;
1618 if (prune_empty_dirs && no_dups) {
1619 int j, prev_depth = 0;
1621 prev_i = 0; /* It's OK that this isn't really true. */
1623 for (i = flist->low; i <= flist->high; i++) {
1624 struct file_struct *fp, *file = flist->files[i];
1626 /* This temporarily abuses the dir.depth value for a
1627 * directory that is in a chain that might get pruned.
1628 * We restore the old value if it gets a reprieve. */
1629 if (S_ISDIR(file->mode) && file->dir.depth) {
1630 /* Dump empty dirs when coming back down. */
1631 for (j = prev_depth; j >= file->dir.depth; j--) {
1632 fp = flist->files[prev_i];
1633 if (fp->dir.depth >= 0)
1635 prev_i = -fp->dir.depth-1;
1636 clear_file(fp, flist);
1638 prev_depth = file->dir.depth;
1639 if (is_excluded(f_name(file, fbuf), 1,
1641 /* Keep dirs through this dir. */
1642 for (j = prev_depth-1; ; j--) {
1643 fp = flist->files[prev_i];
1644 if (fp->dir.depth >= 0)
1646 prev_i = -fp->dir.depth-1;
1650 file->dir.depth = -prev_i-1;
1653 /* Keep dirs through this non-dir. */
1654 for (j = prev_depth; ; j--) {
1655 fp = flist->files[prev_i];
1656 if (fp->dir.depth >= 0)
1658 prev_i = -fp->dir.depth-1;
1663 /* Dump empty all remaining empty dirs. */
1665 struct file_struct *fp = flist->files[prev_i];
1666 if (fp->dir.depth >= 0)
1668 prev_i = -fp->dir.depth-1;
1669 clear_file(fp, flist);
1672 for (i = flist->low; i <= flist->high; i++) {
1673 if (flist->files[i]->basename)
1677 for (i = flist->high; i >= flist->low; i--) {
1678 if (flist->files[i]->basename)
1685 static void output_flist(struct file_list *flist)
1687 char uidbuf[16], gidbuf[16], depthbuf[16];
1688 struct file_struct *file;
1689 const char *who = who_am_i();
1692 for (i = 0; i < flist->count; i++) {
1693 file = flist->files[i];
1694 if ((am_root || am_sender) && preserve_uid)
1695 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1698 if (preserve_gid && file->gid != GID_NONE)
1699 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1703 sprintf(depthbuf, "%d", file->dir.depth);
1704 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1705 who, i, am_sender ? NS(file->dir.root) : depthbuf,
1706 file->dirname ? file->dirname : "",
1707 file->dirname ? "/" : "", NS(file->basename),
1708 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
1709 (double)file->length, uidbuf, gidbuf, file->flags);
1713 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1714 enum fnc_type { t_PATH, t_ITEM };
1716 /* Compare the names of two file_struct entities, similar to how strcmp()
1717 * would do if it were operating on the joined strings.
1719 * Some differences beginning with protocol_version 29: (1) directory names
1720 * are compared with an assumed trailing slash so that they compare in a
1721 * way that would cause them to sort immediately prior to any content they
1722 * may have; (2) a directory of any name compares after a non-directory of
1723 * any name at the same depth; (3) a directory with name "." compares prior
1724 * to anything else. These changes mean that a directory and a non-dir
1725 * with the same name will not compare as equal (protocol_version >= 29).
1727 * The dirname component can be an empty string, but the basename component
1728 * cannot (and never is in the current codebase). The basename component
1729 * may be NULL (for a removed item), in which case it is considered to be
1730 * after any existing item. */
1731 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1734 const uchar *c1, *c2;
1735 enum fnc_state state1, state2;
1736 enum fnc_type type1, type2;
1737 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1739 if (!f1 || !f1->basename) {
1740 if (!f2 || !f2->basename)
1744 if (!f2 || !f2->basename)
1747 c1 = (uchar*)f1->dirname;
1748 c2 = (uchar*)f2->dirname;
1752 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1753 c1 = (uchar*)f1->basename;
1754 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1756 state1 = s_TRAILING;
1769 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1770 c2 = (uchar*)f2->basename;
1771 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1773 state2 = s_TRAILING;
1787 return type1 == t_PATH ? 1 : -1;
1790 if ((dif = (int)*c1++ - (int)*c2++) != 0)
1799 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1800 c1 = (uchar*)f1->basename;
1801 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1803 state1 = s_TRAILING;
1809 state1 = s_TRAILING;
1810 if (type1 == t_PATH) {
1819 if (*c2 && type1 != type2)
1820 return type1 == t_PATH ? 1 : -1;
1829 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1830 c2 = (uchar*)f2->basename;
1831 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1833 state2 = s_TRAILING;
1839 state2 = s_TRAILING;
1840 if (type2 == t_PATH) {
1852 return type1 == t_PATH ? 1 : -1;
1859 /* Return a copy of the full filename of a flist entry, using the indicated
1860 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
1861 * done because we checked the size when creating the file_struct entry.
1863 char *f_name(struct file_struct *f, char *fbuf)
1865 if (!f || !f->basename)
1869 static char names[5][MAXPATHLEN];
1870 static unsigned int n;
1872 n = (n + 1) % (sizeof names / sizeof names[0]);
1878 int len = strlen(f->dirname);
1879 memcpy(fbuf, f->dirname, len);
1881 strcpy(fbuf + len + 1, f->basename);
1883 strcpy(fbuf, f->basename);
1888 /* Do a non-recursive scan of the named directory, possibly ignoring all
1889 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1890 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1891 * buffer (the functions we call will append names onto the end, but the old
1892 * dir value will be restored on exit). */
1893 struct file_list *get_dirlist(char *dirname, int dlen,
1894 int ignore_filter_rules)
1896 struct file_list *dirlist;
1897 char dirbuf[MAXPATHLEN];
1898 int save_recurse = recurse;
1899 int save_xfer_dirs = xfer_dirs;
1902 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1903 if (dlen >= MAXPATHLEN)
1908 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1912 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
1913 xfer_dirs = save_xfer_dirs;
1914 recurse = save_recurse;
1916 flist_count_offset += dirlist->count;
1918 clean_flist(dirlist, 0, 0);
1921 output_flist(dirlist);