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 curr_dir_depth;
41 extern int one_file_system;
42 extern int copy_dirlinks;
43 extern int keep_dirlinks;
44 extern int preserve_links;
45 extern int preserve_hard_links;
46 extern int preserve_devices;
47 extern int preserve_specials;
48 extern int preserve_uid;
49 extern int preserve_gid;
50 extern int relative_paths;
51 extern int implied_dirs;
52 extern int prune_empty_dirs;
53 extern int copy_links;
54 extern int copy_unsafe_links;
55 extern int protocol_version;
56 extern int sanitize_paths;
57 extern struct stats stats;
58 extern struct file_list *the_file_list;
60 extern char curr_dir[MAXPATHLEN];
62 extern struct chmod_mode_struct *chmod_modes;
64 extern struct filter_list_struct filter_list;
65 extern struct filter_list_struct server_filter_list;
69 dev_t filesystem_dev; /* used to implement -x */
70 unsigned int file_struct_len;
72 static char empty_sum[MD4_SUM_LENGTH];
73 static int flist_count_offset;
75 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
76 static void output_flist(struct file_list *flist);
82 /* Figure out how big the file_struct is without trailing padding */
83 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
84 checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
87 static int show_filelist_p(void)
89 return verbose && xfer_dirs && !am_server;
92 static void start_filelist_progress(char *kind)
94 rprintf(FINFO, "%s ... ", kind);
95 if (verbose > 1 || do_progress)
100 static void emit_filelist_progress(int count)
102 rprintf(FINFO, " %d files...\r", count);
105 static void maybe_emit_filelist_progress(int count)
107 if (do_progress && show_filelist_p() && (count % 100) == 0)
108 emit_filelist_progress(count);
111 static void finish_filelist_progress(const struct file_list *flist)
114 /* This overwrites the progress line */
115 rprintf(FINFO, "%d file%sto consider\n",
116 flist->count, flist->count == 1 ? " " : "s ");
118 rprintf(FINFO, "done\n");
121 void show_flist_stats(void)
126 static void list_file_entry(struct file_struct *f)
128 char permbuf[PERMSTRING_SIZE];
131 /* this can happen if duplicate names were removed */
135 permstring(permbuf, f->mode);
138 if (preserve_links && S_ISLNK(f->mode)) {
139 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
141 (double)f->length, timestring(f->modtime),
142 f_name(f, NULL), f->u.link);
146 rprintf(FINFO, "%s %11.0f %s %s\n",
148 (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. Returns -1 on error, 0 on success.
156 * If path is the name of a symlink, then the linkbuf buffer (which must hold
157 * MAXPATHLEN chars) will be set to the symlink's target string.
159 * The stat structure pointed to by stp will contain information about the
160 * link or the referent as appropriate, if they exist. */
161 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
164 if (link_stat(path, stp, copy_dirlinks) < 0)
166 if (S_ISLNK(stp->st_mode)) {
167 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
170 linkbuf[llen] = '\0';
171 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
173 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
176 return safe_stat(path, stp);
181 return do_stat(path, stp);
185 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
189 return safe_stat(path, stp);
190 if (do_lstat(path, stp) < 0)
192 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
194 if (safe_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
199 return do_stat(path, stp);
203 /* This function is used to check if a file should be included/excluded
204 * from the list of files based on its name and type etc. The value of
205 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
206 static int is_excluded(char *fname, int is_dir, int filter_level)
208 #if 0 /* This currently never happens, so avoid a useless compare. */
209 if (filter_level == NO_FILTERS)
213 /* never exclude '.', even if somebody does --exclude '*' */
214 if (fname[0] == '.' && !fname[1])
216 /* Handle the -R version of the '.' dir. */
217 if (fname[0] == '/') {
218 int len = strlen(fname);
219 if (fname[len-1] == '.' && fname[len-2] == '/')
223 if (server_filter_list.head
224 && check_filter(&server_filter_list, fname, is_dir) < 0)
226 if (filter_level != ALL_FILTERS)
229 && check_filter(&filter_list, fname, is_dir) < 0)
234 static int to_wire_mode(mode_t mode)
237 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
238 return (mode & ~(_S_IFMT)) | 0120000;
243 static mode_t from_wire_mode(int mode)
245 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
246 return (mode & ~(_S_IFMT)) | _S_IFLNK;
250 static void send_directory(int f, struct file_list *flist,
251 char *fbuf, int len);
253 static char *flist_dir;
254 static int flist_dir_len;
258 * Make sure @p flist is big enough to hold at least @p flist->count
261 void flist_expand(struct file_list *flist)
263 struct file_struct **new_ptr;
265 if (flist->count < flist->malloced)
268 if (flist->malloced < FLIST_START)
269 flist->malloced = FLIST_START;
270 else if (flist->malloced >= FLIST_LINEAR)
271 flist->malloced += FLIST_LINEAR;
273 flist->malloced *= 2;
276 * In case count jumped or we are starting the list
277 * with a known size just set it.
279 if (flist->malloced < flist->count)
280 flist->malloced = flist->count;
282 new_ptr = realloc_array(flist->files, struct file_struct *,
285 if (verbose >= 2 && flist->malloced != FLIST_START) {
286 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
288 (double)sizeof flist->files[0] * flist->malloced,
289 (new_ptr == flist->files) ? " not" : "");
292 flist->files = new_ptr;
295 out_of_memory("flist_expand");
298 static void send_file_entry(struct file_struct *file, int f)
300 unsigned short flags;
301 static time_t modtime;
305 static uint32 rdev_major;
308 static char lastname[MAXPATHLEN];
309 char fname[MAXPATHLEN];
317 modtime = 0, mode = 0;
318 dev = 0, rdev = makedev(0, 0);
327 flags = file->flags & XMIT_TOP_DIR;
329 if (file->mode == mode)
330 flags |= XMIT_SAME_MODE;
333 if ((preserve_devices && IS_DEVICE(mode))
334 || (preserve_specials && IS_SPECIAL(mode))) {
335 if (protocol_version < 28) {
336 if (file->u.rdev == rdev)
337 flags |= XMIT_SAME_RDEV_pre28;
342 if ((uint32)major(rdev) == rdev_major)
343 flags |= XMIT_SAME_RDEV_MAJOR;
345 rdev_major = major(rdev);
346 if ((uint32)minor(rdev) <= 0xFFu)
347 flags |= XMIT_RDEV_MINOR_IS_SMALL;
349 } else if (protocol_version < 28)
350 rdev = makedev(0, 0);
351 if (file->uid == uid)
352 flags |= XMIT_SAME_UID;
355 if (file->gid == gid)
356 flags |= XMIT_SAME_GID;
359 if (file->modtime == modtime)
360 flags |= XMIT_SAME_TIME;
362 modtime = file->modtime;
364 #ifdef SUPPORT_HARD_LINKS
365 if (file->link_u.idev) {
366 if (file->F_DEV == dev) {
367 if (protocol_version >= 28)
368 flags |= XMIT_SAME_DEV;
371 flags |= XMIT_HAS_IDEV_DATA;
376 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
378 l2 = strlen(fname+l1);
381 flags |= XMIT_SAME_NAME;
383 flags |= XMIT_LONG_NAME;
385 /* We must make sure we don't send a zero flag byte or the
386 * other end will terminate the flist transfer. Note that
387 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
388 * it's harmless way to add a bit to the first flag byte. */
389 if (protocol_version >= 28) {
390 if (!flags && !S_ISDIR(mode))
391 flags |= XMIT_TOP_DIR;
392 if ((flags & 0xFF00) || !flags) {
393 flags |= XMIT_EXTENDED_FLAGS;
394 write_byte(f, flags);
395 write_byte(f, flags >> 8);
397 write_byte(f, flags);
400 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
401 write_byte(f, flags);
403 if (flags & XMIT_SAME_NAME)
405 if (flags & XMIT_LONG_NAME)
409 write_buf(f, fname + l1, l2);
411 write_longint(f, file->length);
412 if (!(flags & XMIT_SAME_TIME))
413 write_int(f, modtime);
414 if (!(flags & XMIT_SAME_MODE))
415 write_int(f, to_wire_mode(mode));
416 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
421 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
426 if ((preserve_devices && IS_DEVICE(mode))
427 || (preserve_specials && IS_SPECIAL(mode))) {
428 if (protocol_version < 28) {
429 if (!(flags & XMIT_SAME_RDEV_pre28))
430 write_int(f, (int)rdev);
432 if (!(flags & XMIT_SAME_RDEV_MAJOR))
433 write_int(f, major(rdev));
434 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
435 write_byte(f, minor(rdev));
437 write_int(f, minor(rdev));
442 if (preserve_links && S_ISLNK(mode)) {
443 int len = strlen(file->u.link);
445 write_buf(f, file->u.link, len);
449 #ifdef SUPPORT_HARD_LINKS
450 if (flags & XMIT_HAS_IDEV_DATA) {
451 if (protocol_version < 26) {
452 /* 32-bit dev_t and ino_t */
454 write_int(f, file->F_INODE);
456 /* 64-bit dev_t and ino_t */
457 if (!(flags & XMIT_SAME_DEV))
458 write_longint(f, dev);
459 write_longint(f, file->F_INODE);
464 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
469 /* Prior to 28, we sent a useless set of nulls. */
472 write_buf(f, sum, checksum_len);
475 strlcpy(lastname, fname, MAXPATHLEN);
478 static struct file_struct *receive_file_entry(struct file_list *flist,
479 unsigned short flags, int f)
481 static time_t modtime;
485 static uint32 rdev_major;
488 static char lastname[MAXPATHLEN], *lastdir;
489 static int lastdir_depth, lastdir_len = -1;
490 static unsigned int del_hier_name_len = 0;
491 static int in_del_hier = 0;
492 char thisname[MAXPATHLEN];
493 unsigned int l1 = 0, l2 = 0;
494 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
496 char *basename, *dirname, *bp;
497 struct file_struct *file;
500 modtime = 0, mode = 0;
501 dev = 0, rdev = makedev(0, 0);
510 if (flags & XMIT_SAME_NAME)
513 if (flags & XMIT_LONG_NAME)
518 if (l2 >= MAXPATHLEN - l1) {
520 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
521 flags, l1, l2, lastname);
522 overflow_exit("receive_file_entry");
525 strlcpy(thisname, lastname, l1 + 1);
526 read_sbuf(f, &thisname[l1], l2);
527 thisname[l1 + l2] = 0;
529 strlcpy(lastname, thisname, MAXPATHLEN);
531 clean_fname(thisname, 0);
534 sanitize_path(thisname, thisname, "", 0, NULL);
536 if ((basename = strrchr(thisname, '/')) != NULL) {
537 dirname_len = ++basename - thisname; /* counts future '\0' */
538 if (lastdir_len == dirname_len - 1
539 && strncmp(thisname, lastdir, lastdir_len) == 0) {
541 dirname_len = 0; /* indicates no copy is needed */
549 basename_len = strlen(basename) + 1; /* count the '\0' */
551 file_length = read_longint(f);
552 if (!(flags & XMIT_SAME_TIME))
553 modtime = (time_t)read_int(f);
554 if (!(flags & XMIT_SAME_MODE))
555 mode = from_wire_mode(read_int(f));
557 if (chmod_modes && !S_ISLNK(mode))
558 mode = tweak_mode(mode, chmod_modes);
560 if (preserve_uid && !(flags & XMIT_SAME_UID))
561 uid = (uid_t)read_int(f);
562 if (preserve_gid && !(flags & XMIT_SAME_GID))
563 gid = (gid_t)read_int(f);
565 if ((preserve_devices && IS_DEVICE(mode))
566 || (preserve_specials && IS_SPECIAL(mode))) {
567 if (protocol_version < 28) {
568 if (!(flags & XMIT_SAME_RDEV_pre28))
569 rdev = (dev_t)read_int(f);
572 if (!(flags & XMIT_SAME_RDEV_MAJOR))
573 rdev_major = read_int(f);
574 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
575 rdev_minor = read_byte(f);
577 rdev_minor = read_int(f);
578 rdev = makedev(rdev_major, rdev_minor);
580 } else if (protocol_version < 28)
581 rdev = makedev(0, 0);
584 if (preserve_links && S_ISLNK(mode)) {
585 linkname_len = read_int(f) + 1; /* count the '\0' */
586 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
587 rprintf(FERROR, "overflow: linkname_len=%d\n",
589 overflow_exit("receive_file_entry");
596 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
598 alloc_len = file_struct_len + dirname_len + basename_len
599 + linkname_len + sum_len;
600 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
602 file = (struct file_struct *)bp;
603 memset(bp, 0, file_struct_len);
604 bp += file_struct_len;
606 file->modtime = modtime;
607 file->length = file_length;
613 file->dirname = lastdir = bp;
614 lastdir_len = dirname_len - 1;
615 memcpy(bp, dirname, dirname_len - 1);
618 lastdir_depth = count_dir_elements(lastdir);
619 file->dir.depth = lastdir_depth + 1;
620 } else if (dirname) {
621 file->dirname = dirname; /* we're reusing lastname */
622 file->dir.depth = lastdir_depth + 1;
627 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
629 if (flags & XMIT_TOP_DIR) {
630 in_del_hier = recurse;
631 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
632 if (relative_paths && del_hier_name_len > 2
633 && lastname[del_hier_name_len-1] == '.'
634 && lastname[del_hier_name_len-2] == '/')
635 del_hier_name_len -= 2;
636 file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
637 } else if (in_del_hier) {
638 if (!relative_paths || !del_hier_name_len
639 || (l1 >= del_hier_name_len
640 && lastname[del_hier_name_len] == '/'))
641 file->flags |= FLAG_DEL_HERE;
648 memcpy(bp, basename, basename_len);
651 if ((preserve_devices && IS_DEVICE(mode))
652 || (preserve_specials && IS_SPECIAL(mode)))
658 read_sbuf(f, bp, linkname_len - 1);
663 #ifdef SUPPORT_HARD_LINKS
664 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
665 flags |= XMIT_HAS_IDEV_DATA;
666 if (flags & XMIT_HAS_IDEV_DATA) {
668 if (protocol_version < 26) {
672 if (!(flags & XMIT_SAME_DEV))
673 dev = read_longint(f);
674 inode = read_longint(f);
676 if (flist->hlink_pool) {
677 file->link_u.idev = pool_talloc(flist->hlink_pool,
678 struct idev, 1, "inode_table");
679 file->F_INODE = inode;
685 if (always_checksum && (sum_len || protocol_version < 28)) {
688 file->u.sum = sum = bp;
691 /* Prior to 28, we get a useless set of nulls. */
694 read_buf(f, sum, checksum_len);
701 * Create a file_struct for a named file by reading its stat()
702 * information and performing extensive checks against global
705 * @return the new file, or NULL if there was an error or this file
706 * should be excluded.
708 * @todo There is a small optimization opportunity here to avoid
709 * stat()ing the file in some circumstances, which has a certain cost.
710 * We are called immediately after doing readdir(), and so we may
711 * already know the d_type of the file. We could for example avoid
712 * statting directories if we're not recursing, but this is not a very
713 * important case. Some systems may not have d_type.
715 struct file_struct *make_file(char *fname, struct file_list *flist,
716 STRUCT_STAT *stp, unsigned short flags,
719 static char *lastdir;
720 static int lastdir_len = -1;
721 struct file_struct *file;
723 char sum[SUM_LENGTH];
724 char thisname[MAXPATHLEN];
725 char linkname[MAXPATHLEN];
726 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
727 char *basename, *dirname, *bp;
729 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
732 if (strlcpy(thisname, fname, sizeof thisname)
733 >= sizeof thisname - flist_dir_len) {
734 rprintf(FINFO, "skipping overly long name: %s\n", fname);
737 clean_fname(thisname, 0);
739 sanitize_path(thisname, thisname, "", 0, NULL);
741 memset(sum, 0, SUM_LENGTH);
743 if (stp && S_ISDIR(stp->st_mode))
744 st = *stp; /* Needed for "symlink/." with --relative. */
745 else if (readlink_stat(thisname, &st, linkname) != 0) {
746 int save_errno = errno;
747 /* See if file is excluded before reporting an error. */
748 if (filter_level != NO_FILTERS
749 && is_excluded(thisname, 0, filter_level))
751 if (save_errno == ENOENT) {
753 /* Avoid "vanished" error if symlink points nowhere. */
754 if (copy_links && do_lstat(thisname, &st) == 0
755 && S_ISLNK(st.st_mode)) {
756 io_error |= IOERR_GENERAL;
757 rprintf(FERROR, "symlink has no referent: %s\n",
758 full_fname(thisname));
762 enum logcode c = am_daemon && protocol_version < 28
764 io_error |= IOERR_VANISHED;
765 rprintf(c, "file has vanished: %s\n",
766 full_fname(thisname));
769 io_error |= IOERR_GENERAL;
770 rsyserr(FERROR, save_errno, "readlink %s failed",
771 full_fname(thisname));
776 /* backup.c calls us with filter_level set to NO_FILTERS. */
777 if (filter_level == NO_FILTERS)
780 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
781 rprintf(FINFO, "skipping directory %s\n", thisname);
785 /* We only care about directories because we need to avoid recursing
786 * into a mount-point directory, not to avoid copying a symlinked
787 * file if -L (or similar) was specified. */
788 if (one_file_system && st.st_dev != filesystem_dev
789 && S_ISDIR(st.st_mode)) {
790 if (one_file_system > 1) {
792 rprintf(FINFO, "skipping mount-point dir %s\n",
797 flags |= FLAG_MOUNT_POINT;
800 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
803 if (lp_ignore_nonreadable(module_id)) {
805 if (!S_ISLNK(st.st_mode))
807 if (access(thisname, R_OK) != 0)
814 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
815 who_am_i(), thisname, filter_level);
818 if ((basename = strrchr(thisname, '/')) != NULL) {
819 dirname_len = ++basename - thisname; /* counts future '\0' */
820 if (lastdir_len == dirname_len - 1
821 && strncmp(thisname, lastdir, lastdir_len) == 0) {
823 dirname_len = 0; /* indicates no copy is needed */
831 basename_len = strlen(basename) + 1; /* count the '\0' */
834 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
839 sum_len = always_checksum && am_sender && S_ISREG(st.st_mode)
840 ? MD4_SUM_LENGTH : 0;
842 alloc_len = file_struct_len + dirname_len + basename_len
843 + linkname_len + sum_len;
845 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
847 if (!(bp = new_array(char, alloc_len)))
848 out_of_memory("make_file");
851 file = (struct file_struct *)bp;
852 memset(bp, 0, file_struct_len);
853 bp += file_struct_len;
856 file->modtime = st.st_mtime;
857 file->length = st.st_size;
858 file->mode = st.st_mode;
859 file->uid = st.st_uid;
860 file->gid = st.st_gid;
862 #ifdef SUPPORT_HARD_LINKS
863 if (flist && flist->hlink_pool) {
864 if (protocol_version < 28) {
865 if (S_ISREG(st.st_mode))
866 file->link_u.idev = pool_talloc(
867 flist->hlink_pool, struct idev, 1,
870 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
871 file->link_u.idev = pool_talloc(
872 flist->hlink_pool, struct idev, 1,
876 if (file->link_u.idev) {
877 file->F_DEV = st.st_dev;
878 file->F_INODE = st.st_ino;
883 file->dirname = lastdir = bp;
884 lastdir_len = dirname_len - 1;
885 memcpy(bp, dirname, dirname_len - 1);
889 file->dirname = dirname;
892 memcpy(bp, basename, basename_len);
895 #ifdef HAVE_STRUCT_STAT_ST_RDEV
896 if ((preserve_devices && IS_DEVICE(st.st_mode))
897 || (preserve_specials && IS_SPECIAL(st.st_mode)))
898 file->u.rdev = st.st_rdev;
904 memcpy(bp, linkname, linkname_len);
911 file_checksum(thisname, bp, st.st_size);
915 file->dir.root = flist_dir;
917 /* This code is only used by the receiver when it is building
918 * a list of files for a delete pass. */
919 if (keep_dirlinks && linkname_len && flist) {
921 int save_mode = file->mode;
922 file->mode = S_IFDIR; /* Find a directory with our name. */
923 if (flist_find(the_file_list, file) >= 0
924 && safe_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
925 file->modtime = st2.st_mtime;
926 file->length = st2.st_size;
927 file->mode = st2.st_mode;
928 file->uid = st2.st_uid;
929 file->gid = st2.st_gid;
932 file->mode = save_mode;
935 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
936 stats.total_size += st.st_size;
941 static struct file_struct *send_file_name(int f, struct file_list *flist,
942 char *fname, STRUCT_STAT *stp,
943 unsigned short flags)
945 struct file_struct *file;
947 file = make_file(fname, flist, stp, flags,
948 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
952 if (chmod_modes && !S_ISLNK(file->mode))
953 file->mode = tweak_mode(file->mode, chmod_modes);
955 maybe_emit_filelist_progress(flist->count + flist_count_offset);
959 if (file->basename[0]) {
960 flist->files[flist->count++] = file;
961 send_file_entry(file, f);
966 static void send_if_directory(int f, struct file_list *flist,
967 struct file_struct *file,
968 char *fbuf, unsigned int ol)
970 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
972 if (S_ISDIR(file->mode)
973 && !(file->flags & FLAG_MOUNT_POINT) && f_name(file, fbuf)) {
975 unsigned int len = strlen(fbuf);
976 if (len > 1 && fbuf[len-1] == '/')
978 if (len >= MAXPATHLEN - 1) {
979 io_error |= IOERR_GENERAL;
980 rprintf(FERROR, "skipping long-named directory: %s\n",
984 save_filters = push_local_filters(fbuf, len);
985 send_directory(f, flist, fbuf, len);
986 pop_local_filters(save_filters);
993 /* This function is normally called by the sender, but the receiving side also
994 * calls it from get_dirlist() with f set to -1 so that we just construct the
995 * file list in memory without sending it over the wire. Also, get_dirlist()
996 * might call this with f set to -2, which also indicates that local filter
997 * rules should be ignored. */
998 static void send_directory(int f, struct file_list *flist,
1005 int start = flist->count;
1007 if (!(d = opendir(fbuf))) {
1008 io_error |= IOERR_GENERAL;
1009 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1014 if (len != 1 || *fbuf != '/')
1017 remainder = MAXPATHLEN - (p - fbuf);
1019 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1020 char *dname = d_name(di);
1021 if (dname[0] == '.' && (dname[1] == '\0'
1022 || (dname[1] == '.' && dname[2] == '\0')))
1024 if (strlcpy(p, dname, remainder) >= remainder) {
1025 io_error |= IOERR_GENERAL;
1027 "cannot send long-named file %s\n",
1032 send_file_name(f, flist, fbuf, NULL, 0);
1038 io_error |= IOERR_GENERAL;
1039 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1045 int i, end = flist->count - 1;
1046 for (i = start; i <= end; i++)
1047 send_if_directory(f, flist, flist->files[i], fbuf, len);
1051 struct file_list *send_file_list(int f, int argc, char *argv[])
1055 char *p, *dir, olddir[sizeof curr_dir];
1056 char lastpath[MAXPATHLEN] = "";
1057 struct file_list *flist;
1058 struct timeval start_tv, end_tv;
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 if (show_filelist_p())
1333 start_filelist_progress("receiving file list");
1335 start_read = stats.total_read;
1337 flist = flist_new(WITH_HLINK, "recv_file_list");
1340 flist->malloced = 1000;
1341 flist->files = new_array(struct file_struct *, flist->malloced);
1345 while ((flags = read_byte(f)) != 0) {
1346 struct file_struct *file;
1348 flist_expand(flist);
1350 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1351 flags |= read_byte(f) << 8;
1352 file = receive_file_entry(flist, flags, f);
1354 if (S_ISREG(file->mode) || S_ISLNK(file->mode))
1355 stats.total_size += file->length;
1357 flist->files[flist->count++] = file;
1359 maybe_emit_filelist_progress(flist->count);
1362 rprintf(FINFO, "recv_file_name(%s)\n",
1363 f_name(file, NULL));
1366 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1369 rprintf(FINFO, "received %d names\n", flist->count);
1371 if (show_filelist_p())
1372 finish_filelist_progress(flist);
1374 clean_flist(flist, relative_paths, 1);
1377 recv_uid_list(f, flist);
1379 /* Recv the io_error flag */
1380 if (lp_ignore_errors(module_id) || ignore_errors)
1383 io_error |= read_int(f);
1387 output_flist(flist);
1391 for (i = 0; i < flist->count; i++)
1392 list_file_entry(flist->files[i]);
1396 rprintf(FINFO, "recv_file_list done\n");
1398 stats.flist_size = stats.total_read - start_read;
1399 stats.num_files = flist->count;
1404 out_of_memory("recv_file_list");
1405 return NULL; /* not reached */
1408 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1410 return f_name_cmp(*file1, *file2);
1413 /* Search for an identically-named item in the file list. Note that the
1414 * items must agree in their directory-ness, or no match is returned. */
1415 int flist_find(struct file_list *flist, struct file_struct *f)
1417 int low = flist->low, high = flist->high;
1418 int diff, mid, mid_up;
1420 while (low <= high) {
1421 mid = (low + high) / 2;
1422 if (flist->files[mid]->basename)
1425 /* Scan for the next non-empty entry using the cached
1426 * distance values. If the value isn't fully up-to-
1427 * date, update it. */
1428 mid_up = mid + flist->files[mid]->dir.depth;
1429 if (!flist->files[mid_up]->basename) {
1431 mid_up += flist->files[mid_up]->dir.depth;
1432 } while (!flist->files[mid_up]->basename);
1433 flist->files[mid]->dir.depth = mid_up - mid;
1435 if (mid_up > high) {
1436 /* If there's nothing left above us, set high to
1437 * a non-empty entry below us and continue. */
1438 high = mid - flist->files[mid]->length;
1439 if (!flist->files[high]->basename) {
1441 high -= flist->files[high]->length;
1442 } while (!flist->files[high]->basename);
1443 flist->files[mid]->length = mid - high;
1448 diff = f_name_cmp(flist->files[mid_up], f);
1450 if (protocol_version < 29
1451 && S_ISDIR(flist->files[mid_up]->mode)
1452 != S_ISDIR(f->mode))
1465 * Free up any resources a file_struct has allocated
1466 * and clear the file.
1468 void clear_file(struct file_struct *file, struct file_list *flist)
1470 if (flist->hlink_pool && file->link_u.idev)
1471 pool_free(flist->hlink_pool, 0, file->link_u.idev);
1472 memset(file, 0, file_struct_len);
1473 /* In an empty entry, dir.depth is an offset to the next non-empty
1474 * entry. Likewise for length in the opposite direction. We assume
1475 * that we're alone for now since flist_find() will adjust the counts
1476 * it runs into that aren't up-to-date. */
1477 file->length = file->dir.depth = 1;
1481 * allocate a new file list
1483 struct file_list *flist_new(int with_hlink, char *msg)
1485 struct file_list *flist;
1487 flist = new(struct file_list);
1491 memset(flist, 0, sizeof (struct file_list));
1493 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1494 out_of_memory, POOL_INTERN)))
1497 #ifdef SUPPORT_HARD_LINKS
1498 if (with_hlink && preserve_hard_links) {
1499 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1500 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1509 * free up all elements in a flist
1511 void flist_free(struct file_list *flist)
1513 pool_destroy(flist->file_pool);
1514 pool_destroy(flist->hlink_pool);
1520 * This routine ensures we don't have any duplicate names in our file list.
1521 * duplicate names can cause corruption because of the pipelining
1523 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1525 char fbuf[MAXPATHLEN];
1530 if (flist->count == 0) {
1535 qsort(flist->files, flist->count,
1536 sizeof flist->files[0], (int (*)())file_compare);
1538 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1539 if (flist->files[i]->basename) {
1544 flist->low = prev_i;
1545 while (++i < flist->count) {
1547 struct file_struct *file = flist->files[i];
1549 if (!file->basename)
1551 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1553 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1554 int save_mode = file->mode;
1555 /* Make sure that this directory doesn't duplicate a
1556 * non-directory earlier in the list. */
1557 flist->high = prev_i;
1558 file->mode = S_IFREG;
1559 j = flist_find(flist, file);
1560 file->mode = save_mode;
1564 struct file_struct *fp = flist->files[j];
1566 /* If one is a dir and the other is not, we want to
1567 * keep the dir because it might have contents in the
1569 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1570 if (S_ISDIR(file->mode))
1576 if (verbose > 1 && !am_server) {
1578 "removing duplicate name %s from file list (%d)\n",
1579 f_name(file, fbuf), drop);
1581 /* Make sure we don't lose track of a user-specified
1583 flist->files[keep]->flags |= flist->files[drop]->flags
1584 & (FLAG_TOP_DIR|FLAG_DEL_HERE);
1586 clear_file(flist->files[drop], flist);
1589 if (flist->low == drop) {
1591 j < i && !flist->files[j]->basename;
1600 flist->high = no_dups ? prev_i : flist->count - 1;
1603 /* We need to strip off the leading slashes for relative
1604 * paths, but this must be done _after_ the sorting phase. */
1605 for (i = flist->low; i <= flist->high; i++) {
1606 struct file_struct *file = flist->files[i];
1610 while (*file->dirname == '/')
1612 if (!*file->dirname)
1613 file->dirname = NULL;
1617 if (prune_empty_dirs && no_dups) {
1618 int j, prev_depth = 0;
1620 prev_i = 0; /* It's OK that this isn't really true. */
1622 for (i = flist->low; i <= flist->high; i++) {
1623 struct file_struct *fp, *file = flist->files[i];
1625 /* This temporarily abuses the dir.depth value for a
1626 * directory that is in a chain that might get pruned.
1627 * We restore the old value if it gets a reprieve. */
1628 if (S_ISDIR(file->mode) && file->dir.depth) {
1629 /* Dump empty dirs when coming back down. */
1630 for (j = prev_depth; j >= file->dir.depth; j--) {
1631 fp = flist->files[prev_i];
1632 if (fp->dir.depth >= 0)
1634 prev_i = -fp->dir.depth-1;
1635 clear_file(fp, flist);
1637 prev_depth = file->dir.depth;
1638 if (is_excluded(f_name(file, fbuf), 1,
1640 /* Keep dirs through this dir. */
1641 for (j = prev_depth-1; ; j--) {
1642 fp = flist->files[prev_i];
1643 if (fp->dir.depth >= 0)
1645 prev_i = -fp->dir.depth-1;
1649 file->dir.depth = -prev_i-1;
1652 /* Keep dirs through this non-dir. */
1653 for (j = prev_depth; ; j--) {
1654 fp = flist->files[prev_i];
1655 if (fp->dir.depth >= 0)
1657 prev_i = -fp->dir.depth-1;
1662 /* Dump empty all remaining empty dirs. */
1664 struct file_struct *fp = flist->files[prev_i];
1665 if (fp->dir.depth >= 0)
1667 prev_i = -fp->dir.depth-1;
1668 clear_file(fp, flist);
1671 for (i = flist->low; i <= flist->high; i++) {
1672 if (flist->files[i]->basename)
1676 for (i = flist->high; i >= flist->low; i--) {
1677 if (flist->files[i]->basename)
1684 static void output_flist(struct file_list *flist)
1686 char uidbuf[16], gidbuf[16], depthbuf[16];
1687 struct file_struct *file;
1688 const char *who = who_am_i();
1691 for (i = 0; i < flist->count; i++) {
1692 file = flist->files[i];
1693 if ((am_root || am_sender) && preserve_uid)
1694 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1697 if (preserve_gid && file->gid != GID_NONE)
1698 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1702 sprintf(depthbuf, "%d", file->dir.depth);
1703 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1704 who, i, am_sender ? NS(file->dir.root) : depthbuf,
1705 file->dirname ? file->dirname : "",
1706 file->dirname ? "/" : "", NS(file->basename),
1707 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
1708 (double)file->length, uidbuf, gidbuf, file->flags);
1712 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1713 enum fnc_type { t_PATH, t_ITEM };
1715 /* Compare the names of two file_struct entities, similar to how strcmp()
1716 * would do if it were operating on the joined strings.
1718 * Some differences beginning with protocol_version 29: (1) directory names
1719 * are compared with an assumed trailing slash so that they compare in a
1720 * way that would cause them to sort immediately prior to any content they
1721 * may have; (2) a directory of any name compares after a non-directory of
1722 * any name at the same depth; (3) a directory with name "." compares prior
1723 * to anything else. These changes mean that a directory and a non-dir
1724 * with the same name will not compare as equal (protocol_version >= 29).
1726 * The dirname component can be an empty string, but the basename component
1727 * cannot (and never is in the current codebase). The basename component
1728 * may be NULL (for a removed item), in which case it is considered to be
1729 * after any existing item. */
1730 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1733 const uchar *c1, *c2;
1734 enum fnc_state state1, state2;
1735 enum fnc_type type1, type2;
1736 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1738 if (!f1 || !f1->basename) {
1739 if (!f2 || !f2->basename)
1743 if (!f2 || !f2->basename)
1746 c1 = (uchar*)f1->dirname;
1747 c2 = (uchar*)f2->dirname;
1751 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1752 c1 = (uchar*)f1->basename;
1753 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1755 state1 = s_TRAILING;
1768 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1769 c2 = (uchar*)f2->basename;
1770 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1772 state2 = s_TRAILING;
1786 return type1 == t_PATH ? 1 : -1;
1789 if ((dif = (int)*c1++ - (int)*c2++) != 0)
1798 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1799 c1 = (uchar*)f1->basename;
1800 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1802 state1 = s_TRAILING;
1808 state1 = s_TRAILING;
1809 if (type1 == t_PATH) {
1818 if (*c2 && type1 != type2)
1819 return type1 == t_PATH ? 1 : -1;
1828 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1829 c2 = (uchar*)f2->basename;
1830 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1832 state2 = s_TRAILING;
1838 state2 = s_TRAILING;
1839 if (type2 == t_PATH) {
1851 return type1 == t_PATH ? 1 : -1;
1858 /* Return a copy of the full filename of a flist entry, using the indicated
1859 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
1860 * done because we checked the size when creating the file_struct entry.
1862 char *f_name(struct file_struct *f, char *fbuf)
1864 if (!f || !f->basename)
1868 static char names[5][MAXPATHLEN];
1869 static unsigned int n;
1871 n = (n + 1) % (sizeof names / sizeof names[0]);
1877 int len = strlen(f->dirname);
1878 memcpy(fbuf, f->dirname, len);
1880 strcpy(fbuf + len + 1, f->basename);
1882 strcpy(fbuf, f->basename);
1887 /* Do a non-recursive scan of the named directory, possibly ignoring all
1888 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1889 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1890 * buffer (the functions we call will append names onto the end, but the old
1891 * dir value will be restored on exit). */
1892 struct file_list *get_dirlist(char *dirname, int dlen,
1893 int ignore_filter_rules)
1895 struct file_list *dirlist;
1896 char dirbuf[MAXPATHLEN];
1897 int save_recurse = recurse;
1898 int save_xfer_dirs = xfer_dirs;
1901 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1902 if (dlen >= MAXPATHLEN)
1907 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1911 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
1912 xfer_dirs = save_xfer_dirs;
1913 recurse = save_recurse;
1915 flist_count_offset += dirlist->count;
1917 clean_flist(dirlist, 0, 0);
1920 output_flist(dirlist);