2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4 Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Generate and receive file lists
24 * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html
36 extern int do_progress;
37 extern int always_checksum;
39 extern int ignore_errors;
40 extern int numeric_ids;
43 extern int filesfrom_fd;
44 extern int one_file_system;
45 extern int copy_dirlinks;
46 extern int keep_dirlinks;
47 extern int preserve_links;
48 extern int preserve_hard_links;
49 extern int preserve_devices;
50 extern int preserve_specials;
51 extern int preserve_uid;
52 extern int preserve_gid;
53 extern int relative_paths;
54 extern int implied_dirs;
55 extern int prune_empty_dirs;
56 extern int copy_links;
57 extern int copy_unsafe_links;
58 extern int protocol_version;
59 extern int sanitize_paths;
60 extern struct stats stats;
61 extern struct file_list *the_file_list;
63 extern char curr_dir[MAXPATHLEN];
65 extern struct chmod_mode_struct *chmod_modes;
67 extern struct filter_list_struct filter_list;
68 extern struct filter_list_struct server_filter_list;
72 dev_t filesystem_dev; /* used to implement -x */
73 unsigned int file_struct_len;
75 static char empty_sum[MD4_SUM_LENGTH];
76 static int flist_count_offset;
78 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
79 static void output_flist(struct file_list *flist);
85 /* Figure out how big the file_struct is without trailing padding */
86 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
87 checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
90 static int show_filelist_p(void)
92 return verbose && xfer_dirs && !am_server;
95 static void start_filelist_progress(char *kind)
97 rprintf(FINFO, "%s ... ", kind);
98 if (verbose > 1 || do_progress)
103 static void emit_filelist_progress(int count)
105 rprintf(FINFO, " %d files...\r", count);
108 static void maybe_emit_filelist_progress(int count)
110 if (do_progress && show_filelist_p() && (count % 100) == 0)
111 emit_filelist_progress(count);
114 static void finish_filelist_progress(const struct file_list *flist)
117 /* This overwrites the progress line */
118 rprintf(FINFO, "%d file%sto consider\n",
119 flist->count, flist->count == 1 ? " " : "s ");
121 rprintf(FINFO, "done\n");
124 void show_flist_stats(void)
129 static void list_file_entry(struct file_struct *f)
131 char permbuf[PERMSTRING_SIZE];
134 /* this can happen if duplicate names were removed */
138 permstring(permbuf, f->mode);
141 if (preserve_links && S_ISLNK(f->mode)) {
142 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
144 (double)f->length, timestring(f->modtime),
145 f_name(f, NULL), f->u.link);
149 rprintf(FINFO, "%s %11.0f %s %s\n",
151 (double)f->length, timestring(f->modtime),
157 * Stat either a symlink or its referent, depending on the settings of
158 * copy_links, copy_unsafe_links, etc.
160 * @retval -1 on error
162 * @retval 0 for success
164 * @post If @p path is a symlink, then @p linkbuf (of size @c
165 * MAXPATHLEN) contains the symlink target.
167 * @post @p buffer contains information about the link or the
168 * referrent as appropriate, if they exist.
170 static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
174 return do_stat(path, buffer);
175 if (link_stat(path, buffer, copy_dirlinks) < 0)
177 if (S_ISLNK(buffer->st_mode)) {
178 int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1);
182 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
184 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
187 return do_stat(path, buffer);
192 return do_stat(path, buffer);
196 int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks)
200 return do_stat(path, buffer);
201 if (do_lstat(path, buffer) < 0)
203 if (follow_dirlinks && S_ISLNK(buffer->st_mode)) {
205 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
210 return do_stat(path, buffer);
214 /* This function is used to check if a file should be included/excluded
215 * from the list of files based on its name and type etc. The value of
216 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
217 static int is_excluded(char *fname, int is_dir, int filter_level)
219 #if 0 /* This currently never happens, so avoid a useless compare. */
220 if (filter_level == NO_FILTERS)
224 /* never exclude '.', even if somebody does --exclude '*' */
225 if (fname[0] == '.' && !fname[1])
227 /* Handle the -R version of the '.' dir. */
228 if (fname[0] == '/') {
229 int len = strlen(fname);
230 if (fname[len-1] == '.' && fname[len-2] == '/')
234 if (server_filter_list.head
235 && check_filter(&server_filter_list, fname, is_dir) < 0)
237 if (filter_level != ALL_FILTERS)
240 && check_filter(&filter_list, fname, is_dir) < 0)
245 static int to_wire_mode(mode_t mode)
248 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
249 return (mode & ~(_S_IFMT)) | 0120000;
254 static mode_t from_wire_mode(int mode)
256 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
257 return (mode & ~(_S_IFMT)) | _S_IFLNK;
261 static void send_directory(int f, struct file_list *flist,
262 char *fbuf, int len);
264 static char *flist_dir;
265 static int flist_dir_len;
269 * Make sure @p flist is big enough to hold at least @p flist->count
272 void flist_expand(struct file_list *flist)
274 struct file_struct **new_ptr;
276 if (flist->count < flist->malloced)
279 if (flist->malloced < FLIST_START)
280 flist->malloced = FLIST_START;
281 else if (flist->malloced >= FLIST_LINEAR)
282 flist->malloced += FLIST_LINEAR;
284 flist->malloced *= 2;
287 * In case count jumped or we are starting the list
288 * with a known size just set it.
290 if (flist->malloced < flist->count)
291 flist->malloced = flist->count;
293 new_ptr = realloc_array(flist->files, struct file_struct *,
296 if (verbose >= 2 && flist->malloced != FLIST_START) {
297 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
299 (double)sizeof flist->files[0] * flist->malloced,
300 (new_ptr == flist->files) ? " not" : "");
303 flist->files = new_ptr;
306 out_of_memory("flist_expand");
309 static void send_file_entry(struct file_struct *file, int f)
311 unsigned short flags;
312 static time_t modtime;
316 static uint32 rdev_major;
319 static char lastname[MAXPATHLEN];
320 char fname[MAXPATHLEN];
328 modtime = 0, mode = 0;
329 dev = 0, rdev = makedev(0, 0);
338 flags = file->flags & XMIT_TOP_DIR;
340 if (file->mode == mode)
341 flags |= XMIT_SAME_MODE;
344 if ((preserve_devices && IS_DEVICE(mode))
345 || (preserve_specials && IS_SPECIAL(mode))) {
346 if (protocol_version < 28) {
347 if (file->u.rdev == rdev)
348 flags |= XMIT_SAME_RDEV_pre28;
353 if ((uint32)major(rdev) == rdev_major)
354 flags |= XMIT_SAME_RDEV_MAJOR;
356 rdev_major = major(rdev);
357 if ((uint32)minor(rdev) <= 0xFFu)
358 flags |= XMIT_RDEV_MINOR_IS_SMALL;
360 } else if (protocol_version < 28)
361 rdev = makedev(0, 0);
362 if (file->uid == uid)
363 flags |= XMIT_SAME_UID;
366 if (file->gid == gid)
367 flags |= XMIT_SAME_GID;
370 if (file->modtime == modtime)
371 flags |= XMIT_SAME_TIME;
373 modtime = file->modtime;
375 #ifdef SUPPORT_HARD_LINKS
376 if (file->link_u.idev) {
377 if (file->F_DEV == dev) {
378 if (protocol_version >= 28)
379 flags |= XMIT_SAME_DEV;
382 flags |= XMIT_HAS_IDEV_DATA;
387 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
389 l2 = strlen(fname+l1);
392 flags |= XMIT_SAME_NAME;
394 flags |= XMIT_LONG_NAME;
396 /* We must make sure we don't send a zero flag byte or the
397 * other end will terminate the flist transfer. Note that
398 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
399 * it's harmless way to add a bit to the first flag byte. */
400 if (protocol_version >= 28) {
401 if (!flags && !S_ISDIR(mode))
402 flags |= XMIT_TOP_DIR;
403 if ((flags & 0xFF00) || !flags) {
404 flags |= XMIT_EXTENDED_FLAGS;
405 write_byte(f, flags);
406 write_byte(f, flags >> 8);
408 write_byte(f, flags);
411 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
412 write_byte(f, flags);
414 if (flags & XMIT_SAME_NAME)
416 if (flags & XMIT_LONG_NAME)
420 write_buf(f, fname + l1, l2);
422 write_longint(f, file->length);
423 if (!(flags & XMIT_SAME_TIME))
424 write_int(f, modtime);
425 if (!(flags & XMIT_SAME_MODE))
426 write_int(f, to_wire_mode(mode));
427 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
432 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
437 if ((preserve_devices && IS_DEVICE(mode))
438 || (preserve_specials && IS_SPECIAL(mode))) {
439 if (protocol_version < 28) {
440 if (!(flags & XMIT_SAME_RDEV_pre28))
441 write_int(f, (int)rdev);
443 if (!(flags & XMIT_SAME_RDEV_MAJOR))
444 write_int(f, major(rdev));
445 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
446 write_byte(f, minor(rdev));
448 write_int(f, minor(rdev));
453 if (preserve_links && S_ISLNK(mode)) {
454 int len = strlen(file->u.link);
456 write_buf(f, file->u.link, len);
460 #ifdef SUPPORT_HARD_LINKS
461 if (flags & XMIT_HAS_IDEV_DATA) {
462 if (protocol_version < 26) {
463 /* 32-bit dev_t and ino_t */
465 write_int(f, file->F_INODE);
467 /* 64-bit dev_t and ino_t */
468 if (!(flags & XMIT_SAME_DEV))
469 write_longint(f, dev);
470 write_longint(f, file->F_INODE);
475 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
480 /* Prior to 28, we sent a useless set of nulls. */
483 write_buf(f, sum, checksum_len);
486 strlcpy(lastname, fname, MAXPATHLEN);
489 static struct file_struct *receive_file_entry(struct file_list *flist,
490 unsigned short flags, int f)
492 static time_t modtime;
496 static uint32 rdev_major;
499 static char lastname[MAXPATHLEN], *lastdir;
500 static int lastdir_depth, lastdir_len = -1;
501 static unsigned int del_hier_name_len = 0;
502 static int in_del_hier = 0;
503 char thisname[MAXPATHLEN];
504 unsigned int l1 = 0, l2 = 0;
505 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
507 char *basename, *dirname, *bp;
508 struct file_struct *file;
511 modtime = 0, mode = 0;
512 dev = 0, rdev = makedev(0, 0);
521 if (flags & XMIT_SAME_NAME)
524 if (flags & XMIT_LONG_NAME)
529 if (l2 >= MAXPATHLEN - l1) {
531 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
532 flags, l1, l2, lastname);
533 overflow_exit("receive_file_entry");
536 strlcpy(thisname, lastname, l1 + 1);
537 read_sbuf(f, &thisname[l1], l2);
538 thisname[l1 + l2] = 0;
540 strlcpy(lastname, thisname, MAXPATHLEN);
542 clean_fname(thisname, 0);
545 sanitize_path(thisname, thisname, "", 0);
547 if ((basename = strrchr(thisname, '/')) != NULL) {
548 dirname_len = ++basename - thisname; /* counts future '\0' */
549 if (lastdir_len == dirname_len - 1
550 && strncmp(thisname, lastdir, lastdir_len) == 0) {
552 dirname_len = 0; /* indicates no copy is needed */
560 basename_len = strlen(basename) + 1; /* count the '\0' */
562 file_length = read_longint(f);
563 if (!(flags & XMIT_SAME_TIME))
564 modtime = (time_t)read_int(f);
565 if (!(flags & XMIT_SAME_MODE))
566 mode = from_wire_mode(read_int(f));
568 if (chmod_modes && !S_ISLNK(mode))
569 mode = tweak_mode(mode, chmod_modes);
571 if (preserve_uid && !(flags & XMIT_SAME_UID))
572 uid = (uid_t)read_int(f);
573 if (preserve_gid && !(flags & XMIT_SAME_GID))
574 gid = (gid_t)read_int(f);
576 if ((preserve_devices && IS_DEVICE(mode))
577 || (preserve_specials && IS_SPECIAL(mode))) {
578 if (protocol_version < 28) {
579 if (!(flags & XMIT_SAME_RDEV_pre28))
580 rdev = (dev_t)read_int(f);
583 if (!(flags & XMIT_SAME_RDEV_MAJOR))
584 rdev_major = read_int(f);
585 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
586 rdev_minor = read_byte(f);
588 rdev_minor = read_int(f);
589 rdev = makedev(rdev_major, rdev_minor);
591 } else if (protocol_version < 28)
592 rdev = makedev(0, 0);
595 if (preserve_links && S_ISLNK(mode)) {
596 linkname_len = read_int(f) + 1; /* count the '\0' */
597 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
598 rprintf(FERROR, "overflow: linkname_len=%d\n",
600 overflow_exit("receive_file_entry");
607 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
609 alloc_len = file_struct_len + dirname_len + basename_len
610 + linkname_len + sum_len;
611 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
613 file = (struct file_struct *)bp;
614 memset(bp, 0, file_struct_len);
615 bp += file_struct_len;
617 file->modtime = modtime;
618 file->length = file_length;
624 file->dirname = lastdir = bp;
625 lastdir_len = dirname_len - 1;
626 memcpy(bp, dirname, dirname_len - 1);
629 lastdir_depth = count_dir_elements(lastdir);
630 file->dir.depth = lastdir_depth + 1;
631 } else if (dirname) {
632 file->dirname = dirname; /* we're reusing lastname */
633 file->dir.depth = lastdir_depth + 1;
638 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
640 if (flags & XMIT_TOP_DIR) {
641 in_del_hier = recurse;
642 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
643 if (relative_paths && del_hier_name_len > 2
644 && lastname[del_hier_name_len-1] == '.'
645 && lastname[del_hier_name_len-2] == '/')
646 del_hier_name_len -= 2;
647 file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
648 } else if (in_del_hier) {
649 if (!relative_paths || !del_hier_name_len
650 || (l1 >= del_hier_name_len
651 && lastname[del_hier_name_len] == '/'))
652 file->flags |= FLAG_DEL_HERE;
659 memcpy(bp, basename, basename_len);
662 if ((preserve_devices && IS_DEVICE(mode))
663 || (preserve_specials && IS_SPECIAL(mode)))
669 read_sbuf(f, bp, linkname_len - 1);
671 sanitize_path(bp, bp, "", lastdir_depth);
676 #ifdef SUPPORT_HARD_LINKS
677 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
678 flags |= XMIT_HAS_IDEV_DATA;
679 if (flags & XMIT_HAS_IDEV_DATA) {
681 if (protocol_version < 26) {
685 if (!(flags & XMIT_SAME_DEV))
686 dev = read_longint(f);
687 inode = read_longint(f);
689 if (flist->hlink_pool) {
690 file->link_u.idev = pool_talloc(flist->hlink_pool,
691 struct idev, 1, "inode_table");
692 file->F_INODE = inode;
698 if (always_checksum && (sum_len || protocol_version < 28)) {
701 file->u.sum = sum = bp;
704 /* Prior to 28, we get a useless set of nulls. */
707 read_buf(f, sum, checksum_len);
714 * Create a file_struct for a named file by reading its stat()
715 * information and performing extensive checks against global
718 * @return the new file, or NULL if there was an error or this file
719 * should be excluded.
721 * @todo There is a small optimization opportunity here to avoid
722 * stat()ing the file in some circumstances, which has a certain cost.
723 * We are called immediately after doing readdir(), and so we may
724 * already know the d_type of the file. We could for example avoid
725 * statting directories if we're not recursing, but this is not a very
726 * important case. Some systems may not have d_type.
728 struct file_struct *make_file(char *fname, struct file_list *flist,
729 STRUCT_STAT *stp, unsigned short flags,
732 static char *lastdir;
733 static int lastdir_len = -1;
734 struct file_struct *file;
736 char sum[SUM_LENGTH];
737 char thisname[MAXPATHLEN];
738 char linkname[MAXPATHLEN];
739 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
740 char *basename, *dirname, *bp;
742 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
745 if (strlcpy(thisname, fname, sizeof thisname)
746 >= sizeof thisname - flist_dir_len) {
747 rprintf(FINFO, "skipping overly long name: %s\n", fname);
750 clean_fname(thisname, 0);
752 sanitize_path(thisname, thisname, "", 0);
754 memset(sum, 0, SUM_LENGTH);
756 if (stp && S_ISDIR(stp->st_mode))
757 st = *stp; /* Needed for "symlink/." with --relative. */
758 else if (readlink_stat(thisname, &st, linkname) != 0) {
759 int save_errno = errno;
760 /* See if file is excluded before reporting an error. */
761 if (filter_level != NO_FILTERS
762 && is_excluded(thisname, 0, filter_level))
764 if (save_errno == ENOENT) {
766 /* Avoid "vanished" error if symlink points nowhere. */
767 if (copy_links && do_lstat(thisname, &st) == 0
768 && S_ISLNK(st.st_mode)) {
769 io_error |= IOERR_GENERAL;
770 rprintf(FERROR, "symlink has no referent: %s\n",
771 full_fname(thisname));
775 enum logcode c = am_daemon && protocol_version < 28
777 io_error |= IOERR_VANISHED;
778 rprintf(c, "file has vanished: %s\n",
779 full_fname(thisname));
782 io_error |= IOERR_GENERAL;
783 rsyserr(FERROR, save_errno, "readlink %s failed",
784 full_fname(thisname));
789 /* backup.c calls us with filter_level set to NO_FILTERS. */
790 if (filter_level == NO_FILTERS)
793 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
794 rprintf(FINFO, "skipping directory %s\n", thisname);
798 /* We only care about directories because we need to avoid recursing
799 * into a mount-point directory, not to avoid copying a symlinked
800 * file if -L (or similar) was specified. */
801 if (one_file_system && st.st_dev != filesystem_dev
802 && S_ISDIR(st.st_mode)) {
803 if (one_file_system > 1) {
805 rprintf(FINFO, "skipping mount-point dir %s\n",
810 flags |= FLAG_MOUNT_POINT;
813 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
816 if (lp_ignore_nonreadable(module_id)) {
818 if (!S_ISLNK(st.st_mode))
820 if (access(thisname, R_OK) != 0)
827 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
828 who_am_i(), thisname, filter_level);
831 if ((basename = strrchr(thisname, '/')) != NULL) {
832 dirname_len = ++basename - thisname; /* counts future '\0' */
833 if (lastdir_len == dirname_len - 1
834 && strncmp(thisname, lastdir, lastdir_len) == 0) {
836 dirname_len = 0; /* indicates no copy is needed */
844 basename_len = strlen(basename) + 1; /* count the '\0' */
847 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
852 sum_len = always_checksum && am_sender && S_ISREG(st.st_mode)
853 ? MD4_SUM_LENGTH : 0;
855 alloc_len = file_struct_len + dirname_len + basename_len
856 + linkname_len + sum_len;
858 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
860 if (!(bp = new_array(char, alloc_len)))
861 out_of_memory("make_file");
864 file = (struct file_struct *)bp;
865 memset(bp, 0, file_struct_len);
866 bp += file_struct_len;
869 file->modtime = st.st_mtime;
870 file->length = st.st_size;
871 file->mode = st.st_mode;
872 file->uid = st.st_uid;
873 file->gid = st.st_gid;
875 #ifdef SUPPORT_HARD_LINKS
876 if (flist && flist->hlink_pool) {
877 if (protocol_version < 28) {
878 if (S_ISREG(st.st_mode))
879 file->link_u.idev = pool_talloc(
880 flist->hlink_pool, struct idev, 1,
883 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
884 file->link_u.idev = pool_talloc(
885 flist->hlink_pool, struct idev, 1,
889 if (file->link_u.idev) {
890 file->F_DEV = st.st_dev;
891 file->F_INODE = st.st_ino;
896 file->dirname = lastdir = bp;
897 lastdir_len = dirname_len - 1;
898 memcpy(bp, dirname, dirname_len - 1);
902 file->dirname = dirname;
905 memcpy(bp, basename, basename_len);
908 #ifdef HAVE_STRUCT_STAT_ST_RDEV
909 if ((preserve_devices && IS_DEVICE(st.st_mode))
910 || (preserve_specials && IS_SPECIAL(st.st_mode)))
911 file->u.rdev = st.st_rdev;
917 memcpy(bp, linkname, linkname_len);
924 file_checksum(thisname, bp, st.st_size);
928 file->dir.root = flist_dir;
930 /* This code is only used by the receiver when it is building
931 * a list of files for a delete pass. */
932 if (keep_dirlinks && linkname_len && flist) {
934 int save_mode = file->mode;
935 file->mode = S_IFDIR; /* Find a directory with our name. */
936 if (flist_find(the_file_list, file) >= 0
937 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
938 file->modtime = st2.st_mtime;
939 file->length = st2.st_size;
940 file->mode = st2.st_mode;
941 file->uid = st2.st_uid;
942 file->gid = st2.st_gid;
945 file->mode = save_mode;
948 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
949 stats.total_size += st.st_size;
954 static struct file_struct *send_file_name(int f, struct file_list *flist,
955 char *fname, STRUCT_STAT *stp,
956 unsigned short flags)
958 struct file_struct *file;
960 file = make_file(fname, flist, stp, flags,
961 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
965 if (chmod_modes && !S_ISLNK(file->mode))
966 file->mode = tweak_mode(file->mode, chmod_modes);
968 maybe_emit_filelist_progress(flist->count + flist_count_offset);
972 if (file->basename[0]) {
973 flist->files[flist->count++] = file;
974 send_file_entry(file, f);
979 static void send_if_directory(int f, struct file_list *flist,
980 struct file_struct *file,
981 char *fbuf, unsigned int ol)
983 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
985 if (S_ISDIR(file->mode)
986 && !(file->flags & FLAG_MOUNT_POINT) && f_name(file, fbuf)) {
988 unsigned int len = strlen(fbuf);
989 if (len > 1 && fbuf[len-1] == '/')
991 if (len >= MAXPATHLEN - 1) {
992 io_error |= IOERR_GENERAL;
993 rprintf(FERROR, "skipping long-named directory: %s\n",
997 save_filters = push_local_filters(fbuf, len);
998 send_directory(f, flist, fbuf, len);
999 pop_local_filters(save_filters);
1006 /* This function is normally called by the sender, but the receiving side also
1007 * calls it from get_dirlist() with f set to -1 so that we just construct the
1008 * file list in memory without sending it over the wire. Also, get_dirlist()
1009 * might call this with f set to -2, which also indicates that local filter
1010 * rules should be ignored. */
1011 static void send_directory(int f, struct file_list *flist,
1012 char *fbuf, int len)
1018 int start = flist->count;
1020 if (!(d = opendir(fbuf))) {
1021 io_error |= IOERR_GENERAL;
1022 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1027 if (len != 1 || *fbuf != '/')
1030 remainder = MAXPATHLEN - (p - fbuf);
1032 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1033 char *dname = d_name(di);
1034 if (dname[0] == '.' && (dname[1] == '\0'
1035 || (dname[1] == '.' && dname[2] == '\0')))
1037 if (strlcpy(p, dname, remainder) >= remainder) {
1038 io_error |= IOERR_GENERAL;
1040 "cannot send long-named file %s\n",
1045 send_file_name(f, flist, fbuf, NULL, 0);
1051 io_error |= IOERR_GENERAL;
1052 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1058 int i, end = flist->count - 1;
1059 for (i = start; i <= end; i++)
1060 send_if_directory(f, flist, flist->files[i], fbuf, len);
1064 struct file_list *send_file_list(int f, int argc, char *argv[])
1068 char *p, *dir, olddir[sizeof curr_dir];
1069 char lastpath[MAXPATHLEN] = "";
1070 struct file_list *flist;
1071 struct timeval start_tv, end_tv;
1075 if (show_filelist_p())
1076 start_filelist_progress("building file list");
1078 start_write = stats.total_written;
1079 gettimeofday(&start_tv, NULL);
1081 flist = flist_new(WITH_HLINK, "send_file_list");
1083 io_start_buffering_out();
1084 if (filesfrom_fd >= 0) {
1085 if (argv[0] && !push_dir(argv[0])) {
1086 rsyserr(FERROR, errno, "push_dir %s failed",
1087 full_fname(argv[0]));
1088 exit_cleanup(RERR_FILESELECT);
1094 char fbuf[MAXPATHLEN];
1099 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1101 sanitize_path(fbuf, fbuf, "", 0);
1105 strlcpy(fbuf, *argv++, MAXPATHLEN);
1107 sanitize_path(fbuf, fbuf, "", 0);
1111 if (relative_paths) {
1112 /* We clean up fbuf below. */
1114 } else if (!len || fbuf[len - 1] == '/') {
1115 if (len == 2 && fbuf[0] == '.') {
1116 /* Turn "./" into just "." rather than "./." */
1119 if (len + 1 >= MAXPATHLEN)
1120 overflow_exit("send_file_list");
1125 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1126 && (len == 2 || fbuf[len-3] == '/')) {
1127 if (len + 2 >= MAXPATHLEN)
1128 overflow_exit("send_file_list");
1134 is_dot_dir = fbuf[len-1] == '.'
1135 && (len == 1 || fbuf[len-2] == '/');
1138 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1139 io_error |= IOERR_GENERAL;
1140 rsyserr(FERROR, errno, "link_stat %s failed",
1145 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1146 rprintf(FINFO, "skipping directory %s\n", fbuf);
1153 if (!relative_paths) {
1154 p = strrchr(fbuf, '/');
1161 len -= p - fbuf + 1;
1166 if ((p = strstr(fbuf, "/./")) != NULL) {
1172 len -= p - fbuf + 3;
1176 /* Get rid of trailing "/" and "/.". */
1178 if (fn[len - 1] == '/') {
1180 if (!--len && !dir) {
1185 else if (len >= 2 && fn[len - 1] == '.'
1186 && fn[len - 2] == '/') {
1188 if (!(len -= 2) && !dir) {
1196 /* Reject a ".." dir in the active part of the path. */
1197 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1198 if ((p[2] == '/' || p[2] == '\0')
1199 && (p == fn || p[-1] == '/')) {
1201 "found \"..\" dir in relative path: %s\n",
1203 exit_cleanup(RERR_SYNTAX);
1214 static char *lastdir;
1215 static int lastdir_len;
1217 strlcpy(olddir, curr_dir, sizeof olddir);
1219 if (!push_dir(dir)) {
1220 io_error |= IOERR_GENERAL;
1221 rsyserr(FERROR, errno, "push_dir %s failed",
1226 if (lastdir && strcmp(lastdir, dir) == 0) {
1227 flist_dir = lastdir;
1228 flist_dir_len = lastdir_len;
1230 flist_dir = lastdir = strdup(dir);
1231 flist_dir_len = lastdir_len = strlen(dir);
1236 memmove(fbuf, fn, len + 1);
1238 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1239 /* Send the implied directories at the start of the
1240 * source spec, so we get their permissions right. */
1241 char *lp = lastpath, *slash = fbuf;
1243 /* Skip any initial directories in our path that we
1244 * have in common with lastpath. */
1245 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1250 if (fn != p || (*lp && *lp != '/')) {
1251 int save_copy_links = copy_links;
1252 int save_xfer_dirs = xfer_dirs;
1253 copy_links |= copy_unsafe_links;
1255 while ((slash = strchr(slash+1, '/')) != 0) {
1257 send_file_name(f, flist, fbuf, NULL, 0);
1260 copy_links = save_copy_links;
1261 xfer_dirs = save_xfer_dirs;
1263 strlcpy(lastpath, fbuf, sizeof lastpath);
1268 if (one_file_system)
1269 filesystem_dev = st.st_dev;
1271 if (recurse || (xfer_dirs && is_dot_dir)) {
1272 struct file_struct *file;
1273 file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
1275 send_if_directory(f, flist, file, fbuf, len);
1277 send_file_name(f, flist, fbuf, &st, 0);
1282 if (!pop_dir(olddir)) {
1283 rsyserr(FERROR, errno, "pop_dir %s failed",
1284 full_fname(olddir));
1285 exit_cleanup(RERR_FILESELECT);
1290 gettimeofday(&end_tv, NULL);
1291 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1292 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1293 if (stats.flist_buildtime == 0)
1294 stats.flist_buildtime = 1;
1297 send_file_entry(NULL, f);
1299 if (show_filelist_p())
1300 finish_filelist_progress(flist);
1302 gettimeofday(&end_tv, NULL);
1303 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1304 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1306 if (flist->hlink_pool) {
1307 pool_destroy(flist->hlink_pool);
1308 flist->hlink_pool = NULL;
1311 /* Sort the list without removing any duplicates. This allows the
1312 * receiving side to ask for any name they like, which gives us the
1313 * flexibility to change the way we unduplicate names in the future
1314 * without causing a compatibility problem with older versions. */
1315 clean_flist(flist, 0, 0);
1319 /* send the io_error flag */
1320 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1323 stats.flist_size = stats.total_written - start_write;
1324 stats.num_files = flist->count;
1327 output_flist(flist);
1330 rprintf(FINFO, "send_file_list done\n");
1335 struct file_list *recv_file_list(int f)
1337 struct file_list *flist;
1338 unsigned short flags;
1341 if (show_filelist_p())
1342 start_filelist_progress("receiving file list");
1344 start_read = stats.total_read;
1346 flist = flist_new(WITH_HLINK, "recv_file_list");
1349 flist->malloced = 1000;
1350 flist->files = new_array(struct file_struct *, flist->malloced);
1354 while ((flags = read_byte(f)) != 0) {
1355 struct file_struct *file;
1357 flist_expand(flist);
1359 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1360 flags |= read_byte(f) << 8;
1361 file = receive_file_entry(flist, flags, f);
1363 if (S_ISREG(file->mode) || S_ISLNK(file->mode))
1364 stats.total_size += file->length;
1366 flist->files[flist->count++] = file;
1368 maybe_emit_filelist_progress(flist->count);
1371 rprintf(FINFO, "recv_file_name(%s)\n",
1372 f_name(file, NULL));
1375 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1378 rprintf(FINFO, "received %d names\n", flist->count);
1380 if (show_filelist_p())
1381 finish_filelist_progress(flist);
1383 clean_flist(flist, relative_paths, 1);
1386 recv_uid_list(f, flist);
1388 /* Recv the io_error flag */
1389 if (lp_ignore_errors(module_id) || ignore_errors)
1392 io_error |= read_int(f);
1396 output_flist(flist);
1400 for (i = 0; i < flist->count; i++)
1401 list_file_entry(flist->files[i]);
1405 rprintf(FINFO, "recv_file_list done\n");
1407 stats.flist_size = stats.total_read - start_read;
1408 stats.num_files = flist->count;
1413 out_of_memory("recv_file_list");
1414 return NULL; /* not reached */
1417 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1419 return f_name_cmp(*file1, *file2);
1422 /* Search for an identically-named item in the file list. Note that the
1423 * items must agree in their directory-ness, or no match is returned. */
1424 int flist_find(struct file_list *flist, struct file_struct *f)
1426 int low = flist->low, high = flist->high;
1427 int diff, mid, mid_up;
1429 while (low <= high) {
1430 mid = (low + high) / 2;
1431 if (flist->files[mid]->basename)
1434 /* Scan for the next non-empty entry using the cached
1435 * distance values. If the value isn't fully up-to-
1436 * date, update it. */
1437 mid_up = mid + flist->files[mid]->dir.depth;
1438 if (!flist->files[mid_up]->basename) {
1440 mid_up += flist->files[mid_up]->dir.depth;
1441 } while (!flist->files[mid_up]->basename);
1442 flist->files[mid]->dir.depth = mid_up - mid;
1444 if (mid_up > high) {
1445 /* If there's nothing left above us, set high to
1446 * a non-empty entry below us and continue. */
1447 high = mid - flist->files[mid]->length;
1448 if (!flist->files[high]->basename) {
1450 high -= flist->files[high]->length;
1451 } while (!flist->files[high]->basename);
1452 flist->files[mid]->length = mid - high;
1457 diff = f_name_cmp(flist->files[mid_up], f);
1459 if (protocol_version < 29
1460 && S_ISDIR(flist->files[mid_up]->mode)
1461 != S_ISDIR(f->mode))
1474 * Free up any resources a file_struct has allocated
1475 * and clear the file.
1477 void clear_file(struct file_struct *file, struct file_list *flist)
1479 if (flist->hlink_pool && file->link_u.idev)
1480 pool_free(flist->hlink_pool, 0, file->link_u.idev);
1481 memset(file, 0, file_struct_len);
1482 /* In an empty entry, dir.depth is an offset to the next non-empty
1483 * entry. Likewise for length in the opposite direction. We assume
1484 * that we're alone for now since flist_find() will adjust the counts
1485 * it runs into that aren't up-to-date. */
1486 file->length = file->dir.depth = 1;
1490 * allocate a new file list
1492 struct file_list *flist_new(int with_hlink, char *msg)
1494 struct file_list *flist;
1496 flist = new(struct file_list);
1500 memset(flist, 0, sizeof (struct file_list));
1502 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1503 out_of_memory, POOL_INTERN)))
1506 #ifdef SUPPORT_HARD_LINKS
1507 if (with_hlink && preserve_hard_links) {
1508 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1509 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1518 * free up all elements in a flist
1520 void flist_free(struct file_list *flist)
1522 pool_destroy(flist->file_pool);
1523 pool_destroy(flist->hlink_pool);
1529 * This routine ensures we don't have any duplicate names in our file list.
1530 * duplicate names can cause corruption because of the pipelining
1532 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1534 char fbuf[MAXPATHLEN];
1539 if (flist->count == 0) {
1544 qsort(flist->files, flist->count,
1545 sizeof flist->files[0], (int (*)())file_compare);
1547 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1548 if (flist->files[i]->basename) {
1553 flist->low = prev_i;
1554 while (++i < flist->count) {
1556 struct file_struct *file = flist->files[i];
1558 if (!file->basename)
1560 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1562 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1563 int save_mode = file->mode;
1564 /* Make sure that this directory doesn't duplicate a
1565 * non-directory earlier in the list. */
1566 flist->high = prev_i;
1567 file->mode = S_IFREG;
1568 j = flist_find(flist, file);
1569 file->mode = save_mode;
1573 struct file_struct *fp = flist->files[j];
1575 /* If one is a dir and the other is not, we want to
1576 * keep the dir because it might have contents in the
1578 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1579 if (S_ISDIR(file->mode))
1585 if (verbose > 1 && !am_server) {
1587 "removing duplicate name %s from file list (%d)\n",
1588 f_name(file, fbuf), drop);
1590 /* Make sure we don't lose track of a user-specified
1592 flist->files[keep]->flags |= flist->files[drop]->flags
1593 & (FLAG_TOP_DIR|FLAG_DEL_HERE);
1595 clear_file(flist->files[drop], flist);
1598 if (flist->low == drop) {
1600 j < i && !flist->files[j]->basename;
1609 flist->high = no_dups ? prev_i : flist->count - 1;
1612 /* We need to strip off the leading slashes for relative
1613 * paths, but this must be done _after_ the sorting phase. */
1614 for (i = flist->low; i <= flist->high; i++) {
1615 struct file_struct *file = flist->files[i];
1619 while (*file->dirname == '/')
1621 if (!*file->dirname)
1622 file->dirname = NULL;
1626 if (prune_empty_dirs && no_dups) {
1627 int j, prev_depth = 0;
1629 prev_i = 0; /* It's OK that this isn't really true. */
1631 for (i = flist->low; i <= flist->high; i++) {
1632 struct file_struct *fp, *file = flist->files[i];
1634 /* This temporarily abuses the dir.depth value for a
1635 * directory that is in a chain that might get pruned.
1636 * We restore the old value if it gets a reprieve. */
1637 if (S_ISDIR(file->mode) && file->dir.depth) {
1638 /* Dump empty dirs when coming back down. */
1639 for (j = prev_depth; j >= file->dir.depth; j--) {
1640 fp = flist->files[prev_i];
1641 if (fp->dir.depth >= 0)
1643 prev_i = -fp->dir.depth-1;
1644 clear_file(fp, flist);
1646 prev_depth = file->dir.depth;
1647 if (is_excluded(f_name(file, fbuf), 1,
1649 /* Keep dirs through this dir. */
1650 for (j = prev_depth-1; ; j--) {
1651 fp = flist->files[prev_i];
1652 if (fp->dir.depth >= 0)
1654 prev_i = -fp->dir.depth-1;
1658 file->dir.depth = -prev_i-1;
1661 /* Keep dirs through this non-dir. */
1662 for (j = prev_depth; ; j--) {
1663 fp = flist->files[prev_i];
1664 if (fp->dir.depth >= 0)
1666 prev_i = -fp->dir.depth-1;
1671 /* Dump empty all remaining empty dirs. */
1673 struct file_struct *fp = flist->files[prev_i];
1674 if (fp->dir.depth >= 0)
1676 prev_i = -fp->dir.depth-1;
1677 clear_file(fp, flist);
1680 for (i = flist->low; i <= flist->high; i++) {
1681 if (flist->files[i]->basename)
1685 for (i = flist->high; i >= flist->low; i--) {
1686 if (flist->files[i]->basename)
1693 static void output_flist(struct file_list *flist)
1695 char uidbuf[16], gidbuf[16], depthbuf[16];
1696 struct file_struct *file;
1697 const char *who = who_am_i();
1700 for (i = 0; i < flist->count; i++) {
1701 file = flist->files[i];
1702 if ((am_root || am_sender) && preserve_uid)
1703 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1706 if (preserve_gid && file->gid != GID_NONE)
1707 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1711 sprintf(depthbuf, "%d", file->dir.depth);
1712 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1713 who, i, am_sender ? NS(file->dir.root) : depthbuf,
1714 file->dirname ? file->dirname : "",
1715 file->dirname ? "/" : "", NS(file->basename),
1716 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
1717 (double)file->length, uidbuf, gidbuf, file->flags);
1721 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1722 enum fnc_type { t_PATH, t_ITEM };
1724 /* Compare the names of two file_struct entities, similar to how strcmp()
1725 * would do if it were operating on the joined strings.
1727 * Some differences beginning with protocol_version 29: (1) directory names
1728 * are compared with an assumed trailing slash so that they compare in a
1729 * way that would cause them to sort immediately prior to any content they
1730 * may have; (2) a directory of any name compares after a non-directory of
1731 * any name at the same depth; (3) a directory with name "." compares prior
1732 * to anything else. These changes mean that a directory and a non-dir
1733 * with the same name will not compare as equal (protocol_version >= 29).
1735 * The dirname component can be an empty string, but the basename component
1736 * cannot (and never is in the current codebase). The basename component
1737 * may be NULL (for a removed item), in which case it is considered to be
1738 * after any existing item. */
1739 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1742 const uchar *c1, *c2;
1743 enum fnc_state state1, state2;
1744 enum fnc_type type1, type2;
1745 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1747 if (!f1 || !f1->basename) {
1748 if (!f2 || !f2->basename)
1752 if (!f2 || !f2->basename)
1755 c1 = (uchar*)f1->dirname;
1756 c2 = (uchar*)f2->dirname;
1760 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1761 c1 = (uchar*)f1->basename;
1762 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1764 state1 = s_TRAILING;
1777 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1778 c2 = (uchar*)f2->basename;
1779 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1781 state2 = s_TRAILING;
1795 return type1 == t_PATH ? 1 : -1;
1798 if ((dif = (int)*c1++ - (int)*c2++) != 0)
1807 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1808 c1 = (uchar*)f1->basename;
1809 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1811 state1 = s_TRAILING;
1817 state1 = s_TRAILING;
1818 if (type1 == t_PATH) {
1827 if (*c2 && type1 != type2)
1828 return type1 == t_PATH ? 1 : -1;
1837 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1838 c2 = (uchar*)f2->basename;
1839 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1841 state2 = s_TRAILING;
1847 state2 = s_TRAILING;
1848 if (type2 == t_PATH) {
1860 return type1 == t_PATH ? 1 : -1;
1867 /* Return a copy of the full filename of a flist entry, using the indicated
1868 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
1869 * done because we checked the size when creating the file_struct entry.
1871 char *f_name(struct file_struct *f, char *fbuf)
1873 if (!f || !f->basename)
1877 static char names[5][MAXPATHLEN];
1878 static unsigned int n;
1880 n = (n + 1) % (sizeof names / sizeof names[0]);
1886 int len = strlen(f->dirname);
1887 memcpy(fbuf, f->dirname, len);
1889 strcpy(fbuf + len + 1, f->basename);
1891 strcpy(fbuf, f->basename);
1896 /* Do a non-recursive scan of the named directory, possibly ignoring all
1897 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1898 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1899 * buffer (the functions we call will append names onto the end, but the old
1900 * dir value will be restored on exit). */
1901 struct file_list *get_dirlist(char *dirname, int dlen,
1902 int ignore_filter_rules)
1904 struct file_list *dirlist;
1905 char dirbuf[MAXPATHLEN];
1906 int save_recurse = recurse;
1907 int save_xfer_dirs = xfer_dirs;
1910 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1911 if (dlen >= MAXPATHLEN)
1916 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1920 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
1921 xfer_dirs = save_xfer_dirs;
1922 recurse = save_recurse;
1924 flist_count_offset += dirlist->count;
1926 clean_flist(dirlist, 0, 0);
1929 output_flist(dirlist);