2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4 Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Generate and receive file lists
24 * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html
37 extern int do_progress;
38 extern int always_checksum;
40 extern int ignore_errors;
41 extern int numeric_ids;
44 extern int filesfrom_fd;
45 extern int one_file_system;
46 extern int keep_dirlinks;
47 extern int preserve_links;
48 extern int preserve_hard_links;
49 extern int preserve_perms;
50 extern int preserve_devices;
51 extern int preserve_specials;
52 extern int preserve_uid;
53 extern int preserve_gid;
54 extern int relative_paths;
55 extern int implied_dirs;
56 extern int copy_links;
57 extern int copy_unsafe_links;
58 extern int protocol_version;
59 extern int sanitize_paths;
60 extern int orig_umask;
61 extern struct stats stats;
62 extern struct file_list *the_file_list;
64 extern char curr_dir[MAXPATHLEN];
66 extern struct chmod_mode_struct *chmod_modes;
68 extern struct filter_list_struct filter_list;
69 extern struct filter_list_struct server_filter_list;
73 dev_t filesystem_dev; /* used to implement -x */
74 unsigned int file_struct_len;
76 static char empty_sum[MD4_SUM_LENGTH];
77 static int flist_count_offset;
79 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
80 static void output_flist(struct file_list *flist);
86 /* Figure out how big the file_struct is without trailing padding */
87 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
88 checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
91 static int show_filelist_p(void)
93 return verbose && xfer_dirs && !am_server;
96 static void start_filelist_progress(char *kind)
98 rprintf(FINFO, "%s ... ", kind);
99 if (verbose > 1 || do_progress)
100 rprintf(FINFO, "\n");
104 static void emit_filelist_progress(int count)
106 rprintf(FINFO, " %d files...\r", count);
109 static void maybe_emit_filelist_progress(int count)
111 if (do_progress && show_filelist_p() && (count % 100) == 0)
112 emit_filelist_progress(count);
115 static void finish_filelist_progress(const struct file_list *flist)
118 /* This overwrites the progress line */
119 rprintf(FINFO, "%d file%sto consider\n",
120 flist->count, flist->count == 1 ? " " : "s ");
122 rprintf(FINFO, "done\n");
125 void show_flist_stats(void)
130 static void list_file_entry(struct file_struct *f)
135 /* this can happen if duplicate names were removed */
139 permstring(perms, f->mode);
142 if (preserve_links && S_ISLNK(f->mode)) {
143 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
145 (double)f->length, timestring(f->modtime),
146 f_name(f, NULL), f->u.link);
150 rprintf(FINFO, "%s %11.0f %s %s\n",
152 (double)f->length, timestring(f->modtime),
158 * Stat either a symlink or its referent, depending on the settings of
159 * copy_links, copy_unsafe_links, etc.
161 * @retval -1 on error
163 * @retval 0 for success
165 * @post If @p path is a symlink, then @p linkbuf (of size @c
166 * MAXPATHLEN) contains the symlink target.
168 * @post @p buffer contains information about the link or the
169 * referrent as appropriate, if they exist.
171 static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
175 return do_stat(path, buffer);
176 if (link_stat(path, buffer, 0) < 0)
178 if (S_ISLNK(buffer->st_mode)) {
179 int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1);
183 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
185 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
188 return do_stat(path, buffer);
193 return do_stat(path, buffer);
197 int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks)
201 return do_stat(path, buffer);
202 if (do_lstat(path, buffer) < 0)
204 if (follow_dirlinks && S_ISLNK(buffer->st_mode)) {
206 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
211 return do_stat(path, buffer);
215 /* This function is used to check if a file should be included/excluded
216 * from the list of files based on its name and type etc. The value of
217 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
218 static int is_excluded(char *fname, int is_dir, int filter_level)
220 #if 0 /* This currently never happens, so avoid a useless compare. */
221 if (filter_level == NO_FILTERS)
225 /* never exclude '.', even if somebody does --exclude '*' */
226 if (fname[0] == '.' && !fname[1])
228 /* Handle the -R version of the '.' dir. */
229 if (fname[0] == '/') {
230 int len = strlen(fname);
231 if (fname[len-1] == '.' && fname[len-2] == '/')
235 if (server_filter_list.head
236 && check_filter(&server_filter_list, fname, is_dir) < 0)
238 if (filter_level != ALL_FILTERS)
241 && check_filter(&filter_list, fname, is_dir) < 0)
246 static int to_wire_mode(mode_t mode)
249 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
250 return (mode & ~(_S_IFMT)) | 0120000;
255 static mode_t from_wire_mode(int mode)
257 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
258 return (mode & ~(_S_IFMT)) | _S_IFLNK;
262 static void send_directory(int f, struct file_list *flist,
263 char *fbuf, int len);
265 static char *flist_dir;
266 static int flist_dir_len;
270 * Make sure @p flist is big enough to hold at least @p flist->count
273 void flist_expand(struct file_list *flist)
275 struct file_struct **new_ptr;
277 if (flist->count < flist->malloced)
280 if (flist->malloced < FLIST_START)
281 flist->malloced = FLIST_START;
282 else if (flist->malloced >= FLIST_LINEAR)
283 flist->malloced += FLIST_LINEAR;
285 flist->malloced *= 2;
288 * In case count jumped or we are starting the list
289 * with a known size just set it.
291 if (flist->malloced < flist->count)
292 flist->malloced = flist->count;
294 new_ptr = realloc_array(flist->files, struct file_struct *,
297 if (verbose >= 2 && flist->malloced != FLIST_START) {
298 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
300 (double)sizeof flist->files[0] * flist->malloced,
301 (new_ptr == flist->files) ? " not" : "");
304 flist->files = new_ptr;
307 out_of_memory("flist_expand");
310 static void send_file_entry(struct file_struct *file, int f)
312 unsigned short flags;
313 static time_t modtime;
317 static uint32 rdev_major;
320 static char lastname[MAXPATHLEN];
321 char fname[MAXPATHLEN];
329 modtime = 0, mode = 0;
330 dev = 0, rdev = makedev(0, 0);
337 io_write_phase = "send_file_entry";
341 flags = file->flags & XMIT_TOP_DIR;
343 if (file->mode == mode)
344 flags |= XMIT_SAME_MODE;
347 if ((preserve_devices && IS_DEVICE(mode))
348 || (preserve_specials && IS_SPECIAL(mode))) {
349 if (protocol_version < 28) {
350 if (file->u.rdev == rdev)
351 flags |= XMIT_SAME_RDEV_pre28;
356 if ((uint32)major(rdev) == rdev_major)
357 flags |= XMIT_SAME_RDEV_MAJOR;
359 rdev_major = major(rdev);
360 if ((uint32)minor(rdev) <= 0xFFu)
361 flags |= XMIT_RDEV_MINOR_IS_SMALL;
363 } else if (protocol_version < 28)
364 rdev = makedev(0, 0);
365 if (file->uid == uid)
366 flags |= XMIT_SAME_UID;
369 if (file->gid == gid)
370 flags |= XMIT_SAME_GID;
373 if (file->modtime == modtime)
374 flags |= XMIT_SAME_TIME;
376 modtime = file->modtime;
378 #ifdef SUPPORT_HARD_LINKS
379 if (file->link_u.idev) {
380 if (file->F_DEV == dev) {
381 if (protocol_version >= 28)
382 flags |= XMIT_SAME_DEV;
385 flags |= XMIT_HAS_IDEV_DATA;
390 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
392 l2 = strlen(fname+l1);
395 flags |= XMIT_SAME_NAME;
397 flags |= XMIT_LONG_NAME;
399 /* We must make sure we don't send a zero flag byte or the
400 * other end will terminate the flist transfer. Note that
401 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
402 * it's harmless way to add a bit to the first flag byte. */
403 if (protocol_version >= 28) {
404 if (!flags && !S_ISDIR(mode))
405 flags |= XMIT_TOP_DIR;
406 if ((flags & 0xFF00) || !flags) {
407 flags |= XMIT_EXTENDED_FLAGS;
408 write_byte(f, flags);
409 write_byte(f, flags >> 8);
411 write_byte(f, flags);
414 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
415 write_byte(f, flags);
417 if (flags & XMIT_SAME_NAME)
419 if (flags & XMIT_LONG_NAME)
423 write_buf(f, fname + l1, l2);
425 write_longint(f, file->length);
426 if (!(flags & XMIT_SAME_TIME))
427 write_int(f, modtime);
428 if (!(flags & XMIT_SAME_MODE))
429 write_int(f, to_wire_mode(mode));
430 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
435 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
440 if ((preserve_devices && IS_DEVICE(mode))
441 || (preserve_specials && IS_SPECIAL(mode))) {
442 if (protocol_version < 28) {
443 if (!(flags & XMIT_SAME_RDEV_pre28))
444 write_int(f, (int)rdev);
446 if (!(flags & XMIT_SAME_RDEV_MAJOR))
447 write_int(f, major(rdev));
448 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
449 write_byte(f, minor(rdev));
451 write_int(f, minor(rdev));
456 if (preserve_links && S_ISLNK(mode)) {
457 int len = strlen(file->u.link);
459 write_buf(f, file->u.link, len);
463 #ifdef SUPPORT_HARD_LINKS
464 if (flags & XMIT_HAS_IDEV_DATA) {
465 if (protocol_version < 26) {
466 /* 32-bit dev_t and ino_t */
468 write_int(f, file->F_INODE);
470 /* 64-bit dev_t and ino_t */
471 if (!(flags & XMIT_SAME_DEV))
472 write_longint(f, dev);
473 write_longint(f, file->F_INODE);
478 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
483 /* Prior to 28, we sent a useless set of nulls. */
486 write_buf(f, sum, checksum_len);
489 strlcpy(lastname, fname, MAXPATHLEN);
491 io_write_phase = "unknown";
494 static struct file_struct *receive_file_entry(struct file_list *flist,
495 unsigned short flags, int f)
497 static time_t modtime;
501 static uint32 rdev_major;
504 static char lastname[MAXPATHLEN], *lastdir;
505 static int lastdir_depth, lastdir_len = -1;
506 static unsigned int del_hier_name_len = 0;
507 static int in_del_hier = 0;
508 char thisname[MAXPATHLEN];
509 unsigned int l1 = 0, l2 = 0;
510 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
512 char *basename, *dirname, *bp;
513 struct file_struct *file;
516 modtime = 0, mode = 0;
517 dev = 0, rdev = makedev(0, 0);
526 if (flags & XMIT_SAME_NAME)
529 if (flags & XMIT_LONG_NAME)
534 if (l2 >= MAXPATHLEN - l1) {
536 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
537 flags, l1, l2, lastname);
538 overflow_exit("receive_file_entry");
541 strlcpy(thisname, lastname, l1 + 1);
542 read_sbuf(f, &thisname[l1], l2);
543 thisname[l1 + l2] = 0;
545 strlcpy(lastname, thisname, MAXPATHLEN);
547 clean_fname(thisname, 0);
550 sanitize_path(thisname, thisname, "", 0);
552 if ((basename = strrchr(thisname, '/')) != NULL) {
553 dirname_len = ++basename - thisname; /* counts future '\0' */
554 if (lastdir_len == dirname_len - 1
555 && strncmp(thisname, lastdir, lastdir_len) == 0) {
557 dirname_len = 0; /* indicates no copy is needed */
565 basename_len = strlen(basename) + 1; /* count the '\0' */
567 file_length = read_longint(f);
568 if (!(flags & XMIT_SAME_TIME))
569 modtime = (time_t)read_int(f);
570 if (!(flags & XMIT_SAME_MODE))
571 mode = from_wire_mode(read_int(f));
573 if (chmod_modes && !S_ISLNK(mode))
574 mode = tweak_mode(mode, chmod_modes);
576 if (preserve_uid && !(flags & XMIT_SAME_UID))
577 uid = (uid_t)read_int(f);
578 if (preserve_gid && !(flags & XMIT_SAME_GID))
579 gid = (gid_t)read_int(f);
581 if ((preserve_devices && IS_DEVICE(mode))
582 || (preserve_specials && IS_SPECIAL(mode))) {
583 if (protocol_version < 28) {
584 if (!(flags & XMIT_SAME_RDEV_pre28))
585 rdev = (dev_t)read_int(f);
588 if (!(flags & XMIT_SAME_RDEV_MAJOR))
589 rdev_major = read_int(f);
590 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
591 rdev_minor = read_byte(f);
593 rdev_minor = read_int(f);
594 rdev = makedev(rdev_major, rdev_minor);
596 } else if (protocol_version < 28)
597 rdev = makedev(0, 0);
600 if (preserve_links && S_ISLNK(mode)) {
601 linkname_len = read_int(f) + 1; /* count the '\0' */
602 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
603 rprintf(FERROR, "overflow: linkname_len=%d\n",
605 overflow_exit("receive_file_entry");
612 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
614 alloc_len = file_struct_len + dirname_len + basename_len
615 + linkname_len + sum_len;
616 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
618 file = (struct file_struct *)bp;
619 memset(bp, 0, file_struct_len);
620 bp += file_struct_len;
622 file->modtime = modtime;
623 file->length = file_length;
629 file->dirname = lastdir = bp;
630 lastdir_len = dirname_len - 1;
631 memcpy(bp, dirname, dirname_len - 1);
634 lastdir_depth = count_dir_elements(lastdir);
635 file->dir.depth = lastdir_depth + 1;
636 } else if (dirname) {
637 file->dirname = dirname; /* we're reusing lastname */
638 file->dir.depth = lastdir_depth + 1;
643 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
645 if (flags & XMIT_TOP_DIR) {
646 in_del_hier = recurse;
647 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
648 if (relative_paths && del_hier_name_len > 2
649 && lastname[del_hier_name_len-1] == '.'
650 && lastname[del_hier_name_len-2] == '/')
651 del_hier_name_len -= 2;
652 file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
653 } else if (in_del_hier) {
654 if (!relative_paths || !del_hier_name_len
655 || (l1 >= del_hier_name_len
656 && lastname[del_hier_name_len] == '/'))
657 file->flags |= FLAG_DEL_HERE;
664 memcpy(bp, basename, basename_len);
667 if ((preserve_devices && IS_DEVICE(mode))
668 || (preserve_specials && IS_SPECIAL(mode)))
674 read_sbuf(f, bp, linkname_len - 1);
676 sanitize_path(bp, bp, "", lastdir_depth);
681 #ifdef SUPPORT_HARD_LINKS
682 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
683 flags |= XMIT_HAS_IDEV_DATA;
684 if (flags & XMIT_HAS_IDEV_DATA) {
686 if (protocol_version < 26) {
690 if (!(flags & XMIT_SAME_DEV))
691 dev = read_longint(f);
692 inode = read_longint(f);
694 if (flist->hlink_pool) {
695 file->link_u.idev = pool_talloc(flist->hlink_pool,
696 struct idev, 1, "inode_table");
697 file->F_INODE = inode;
703 if (always_checksum && (sum_len || protocol_version < 28)) {
706 file->u.sum = sum = bp;
709 /* Prior to 28, we get a useless set of nulls. */
712 read_buf(f, sum, checksum_len);
715 if (!preserve_perms) {
716 /* set an appropriate set of permissions based on original
717 * permissions and umask. This emulates what GNU cp does */
718 file->mode &= ~orig_umask;
725 * Create a file_struct for a named file by reading its stat()
726 * information and performing extensive checks against global
729 * @return the new file, or NULL if there was an error or this file
730 * should be excluded.
732 * @todo There is a small optimization opportunity here to avoid
733 * stat()ing the file in some circumstances, which has a certain cost.
734 * We are called immediately after doing readdir(), and so we may
735 * already know the d_type of the file. We could for example avoid
736 * statting directories if we're not recursing, but this is not a very
737 * important case. Some systems may not have d_type.
739 struct file_struct *make_file(char *fname, struct file_list *flist,
740 STRUCT_STAT *stp, unsigned short flags,
743 static char *lastdir;
744 static int lastdir_len = -1;
745 struct file_struct *file;
747 char sum[SUM_LENGTH];
748 char thisname[MAXPATHLEN];
749 char linkname[MAXPATHLEN];
750 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
751 char *basename, *dirname, *bp;
753 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
756 if (strlcpy(thisname, fname, sizeof thisname)
757 >= sizeof thisname - flist_dir_len) {
758 rprintf(FINFO, "skipping overly long name: %s\n", fname);
761 clean_fname(thisname, 0);
763 sanitize_path(thisname, thisname, "", 0);
765 memset(sum, 0, SUM_LENGTH);
767 if (stp && S_ISDIR(stp->st_mode))
768 st = *stp; /* Needed for "symlink/." with --relative. */
769 else if (readlink_stat(thisname, &st, linkname) != 0) {
770 int save_errno = errno;
771 /* See if file is excluded before reporting an error. */
772 if (filter_level != NO_FILTERS
773 && is_excluded(thisname, 0, filter_level))
775 if (save_errno == ENOENT) {
777 /* Avoid "vanished" error if symlink points nowhere. */
778 if (copy_links && do_lstat(thisname, &st) == 0
779 && S_ISLNK(st.st_mode)) {
780 io_error |= IOERR_GENERAL;
781 rprintf(FERROR, "symlink has no referent: %s\n",
782 full_fname(thisname));
786 enum logcode c = am_daemon && protocol_version < 28
788 io_error |= IOERR_VANISHED;
789 rprintf(c, "file has vanished: %s\n",
790 full_fname(thisname));
793 io_error |= IOERR_GENERAL;
794 rsyserr(FERROR, save_errno, "readlink %s failed",
795 full_fname(thisname));
800 /* backup.c calls us with filter_level set to NO_FILTERS. */
801 if (filter_level == NO_FILTERS)
804 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
805 rprintf(FINFO, "skipping directory %s\n", thisname);
809 /* We only care about directories because we need to avoid recursing
810 * into a mount-point directory, not to avoid copying a symlinked
811 * file if -L (or similar) was specified. */
812 if (one_file_system && st.st_dev != filesystem_dev
813 && S_ISDIR(st.st_mode)) {
814 if (one_file_system > 1) {
816 rprintf(FINFO, "skipping mount-point dir %s\n",
821 flags |= FLAG_MOUNT_POINT;
824 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
827 if (lp_ignore_nonreadable(module_id)) {
829 if (!S_ISLNK(st.st_mode))
831 if (access(thisname, R_OK) != 0)
838 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
839 who_am_i(), thisname, filter_level);
842 if ((basename = strrchr(thisname, '/')) != NULL) {
843 dirname_len = ++basename - thisname; /* counts future '\0' */
844 if (lastdir_len == dirname_len - 1
845 && strncmp(thisname, lastdir, lastdir_len) == 0) {
847 dirname_len = 0; /* indicates no copy is needed */
855 basename_len = strlen(basename) + 1; /* count the '\0' */
858 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
863 sum_len = always_checksum && am_sender && S_ISREG(st.st_mode)
864 ? MD4_SUM_LENGTH : 0;
866 alloc_len = file_struct_len + dirname_len + basename_len
867 + linkname_len + sum_len;
869 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
871 if (!(bp = new_array(char, alloc_len)))
872 out_of_memory("make_file");
875 file = (struct file_struct *)bp;
876 memset(bp, 0, file_struct_len);
877 bp += file_struct_len;
880 file->modtime = st.st_mtime;
881 file->length = st.st_size;
882 file->mode = st.st_mode;
883 file->uid = st.st_uid;
884 file->gid = st.st_gid;
886 #ifdef SUPPORT_HARD_LINKS
887 if (flist && flist->hlink_pool) {
888 if (protocol_version < 28) {
889 if (S_ISREG(st.st_mode))
890 file->link_u.idev = pool_talloc(
891 flist->hlink_pool, struct idev, 1,
894 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
895 file->link_u.idev = pool_talloc(
896 flist->hlink_pool, struct idev, 1,
900 if (file->link_u.idev) {
901 file->F_DEV = st.st_dev;
902 file->F_INODE = st.st_ino;
907 file->dirname = lastdir = bp;
908 lastdir_len = dirname_len - 1;
909 memcpy(bp, dirname, dirname_len - 1);
913 file->dirname = dirname;
916 memcpy(bp, basename, basename_len);
919 #ifdef HAVE_STRUCT_STAT_ST_RDEV
920 if ((preserve_devices && IS_DEVICE(st.st_mode))
921 || (preserve_specials && IS_SPECIAL(st.st_mode)))
922 file->u.rdev = st.st_rdev;
928 memcpy(bp, linkname, linkname_len);
935 file_checksum(thisname, bp, st.st_size);
939 file->dir.root = flist_dir;
941 /* This code is only used by the receiver when it is building
942 * a list of files for a delete pass. */
943 if (keep_dirlinks && linkname_len && flist) {
945 int save_mode = file->mode;
946 file->mode = S_IFDIR; /* Find a directory with our name. */
947 if (flist_find(the_file_list, file) >= 0
948 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
949 file->modtime = st2.st_mtime;
950 file->length = st2.st_size;
951 file->mode = st2.st_mode;
952 file->uid = st2.st_uid;
953 file->gid = st2.st_gid;
956 file->mode = save_mode;
959 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
960 stats.total_size += st.st_size;
965 static struct file_struct *send_file_name(int f, struct file_list *flist,
966 char *fname, STRUCT_STAT *stp,
967 unsigned short flags)
969 struct file_struct *file;
971 file = make_file(fname, flist, stp, flags,
972 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
976 if (chmod_modes && !S_ISLNK(file->mode))
977 file->mode = tweak_mode(file->mode, chmod_modes);
979 maybe_emit_filelist_progress(flist->count + flist_count_offset);
983 if (file->basename[0]) {
984 flist->files[flist->count++] = file;
985 send_file_entry(file, f);
990 static void send_if_directory(int f, struct file_list *flist,
991 struct file_struct *file,
992 char *fbuf, unsigned int ol)
994 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
996 if (S_ISDIR(file->mode)
997 && !(file->flags & FLAG_MOUNT_POINT) && f_name(file, fbuf)) {
999 unsigned int len = strlen(fbuf);
1000 if (len > 1 && fbuf[len-1] == '/')
1002 if (len >= MAXPATHLEN - 1) {
1003 io_error |= IOERR_GENERAL;
1004 rprintf(FERROR, "skipping long-named directory: %s\n",
1008 save_filters = push_local_filters(fbuf, len);
1009 send_directory(f, flist, fbuf, len);
1010 pop_local_filters(save_filters);
1017 /* This function is normally called by the sender, but the receiving side also
1018 * calls it from get_dirlist() with f set to -1 so that we just construct the
1019 * file list in memory without sending it over the wire. Also, get_dirlist()
1020 * might call this with f set to -2, which also indicates that local filter
1021 * rules should be ignored. */
1022 static void send_directory(int f, struct file_list *flist,
1023 char *fbuf, int len)
1029 int start = flist->count;
1031 if (!(d = opendir(fbuf))) {
1032 io_error |= IOERR_GENERAL;
1033 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1038 if (len != 1 || *fbuf != '/')
1041 remainder = MAXPATHLEN - (p - fbuf);
1043 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1044 char *dname = d_name(di);
1045 if (dname[0] == '.' && (dname[1] == '\0'
1046 || (dname[1] == '.' && dname[2] == '\0')))
1048 if (strlcpy(p, dname, remainder) >= remainder) {
1049 io_error |= IOERR_GENERAL;
1051 "cannot send long-named file %s\n",
1056 send_file_name(f, flist, fbuf, NULL, 0);
1062 io_error |= IOERR_GENERAL;
1063 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1069 int i, end = flist->count - 1;
1070 for (i = start; i <= end; i++)
1071 send_if_directory(f, flist, flist->files[i], fbuf, len);
1075 struct file_list *send_file_list(int f, int argc, char *argv[])
1079 char *p, *dir, olddir[sizeof curr_dir];
1080 char lastpath[MAXPATHLEN] = "";
1081 struct file_list *flist;
1082 struct timeval start_tv, end_tv;
1086 if (show_filelist_p())
1087 start_filelist_progress("building file list");
1089 start_write = stats.total_written;
1090 gettimeofday(&start_tv, NULL);
1092 flist = flist_new(WITH_HLINK, "send_file_list");
1094 io_start_buffering_out();
1095 if (filesfrom_fd >= 0) {
1096 if (argv[0] && !push_dir(argv[0])) {
1097 rsyserr(FERROR, errno, "push_dir %s failed",
1098 full_fname(argv[0]));
1099 exit_cleanup(RERR_FILESELECT);
1105 char fbuf[MAXPATHLEN];
1110 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1112 sanitize_path(fbuf, fbuf, "", 0);
1116 strlcpy(fbuf, *argv++, MAXPATHLEN);
1118 sanitize_path(fbuf, fbuf, "", 0);
1122 if (relative_paths) {
1123 /* We clean up fbuf below. */
1125 } else if (!len || fbuf[len - 1] == '/') {
1126 if (len == 2 && fbuf[0] == '.') {
1127 /* Turn "./" into just "." rather than "./." */
1130 if (len + 1 >= MAXPATHLEN)
1131 overflow_exit("send_file_list");
1136 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1137 && (len == 2 || fbuf[len-3] == '/')) {
1138 if (len + 2 >= MAXPATHLEN)
1139 overflow_exit("send_file_list");
1145 is_dot_dir = fbuf[len-1] == '.'
1146 && (len == 1 || fbuf[len-2] == '/');
1149 if (link_stat(fbuf, &st, keep_dirlinks) != 0) {
1150 io_error |= IOERR_GENERAL;
1151 rsyserr(FERROR, errno, "link_stat %s failed",
1156 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1157 rprintf(FINFO, "skipping directory %s\n", fbuf);
1164 if (!relative_paths) {
1165 p = strrchr(fbuf, '/');
1172 len -= p - fbuf + 1;
1177 if ((p = strstr(fbuf, "/./")) != NULL) {
1183 len -= p - fbuf + 3;
1187 /* Get rid of trailing "/" and "/.". */
1189 if (fn[len - 1] == '/') {
1191 if (!--len && !dir) {
1196 else if (len >= 2 && fn[len - 1] == '.'
1197 && fn[len - 2] == '/') {
1199 if (!(len -= 2) && !dir) {
1207 /* Reject a ".." dir in the active part of the path. */
1208 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1209 if ((p[2] == '/' || p[2] == '\0')
1210 && (p == fn || p[-1] == '/')) {
1212 "found \"..\" dir in relative path: %s\n",
1214 exit_cleanup(RERR_SYNTAX);
1225 static char *lastdir;
1226 static int lastdir_len;
1228 strlcpy(olddir, curr_dir, sizeof olddir);
1230 if (!push_dir(dir)) {
1231 io_error |= IOERR_GENERAL;
1232 rsyserr(FERROR, errno, "push_dir %s failed",
1237 if (lastdir && strcmp(lastdir, dir) == 0) {
1238 flist_dir = lastdir;
1239 flist_dir_len = lastdir_len;
1241 flist_dir = lastdir = strdup(dir);
1242 flist_dir_len = lastdir_len = strlen(dir);
1247 memmove(fbuf, fn, len + 1);
1249 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1250 /* Send the implied directories at the start of the
1251 * source spec, so we get their permissions right. */
1252 char *lp = lastpath, *slash = fbuf;
1254 /* Skip any initial directories in our path that we
1255 * have in common with lastpath. */
1256 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1261 if (fn != p || (*lp && *lp != '/')) {
1262 int save_copy_links = copy_links;
1263 int save_xfer_dirs = xfer_dirs;
1264 copy_links = copy_unsafe_links;
1266 while ((slash = strchr(slash+1, '/')) != 0) {
1268 send_file_name(f, flist, fbuf, NULL, 0);
1271 copy_links = save_copy_links;
1272 xfer_dirs = save_xfer_dirs;
1274 strlcpy(lastpath, fbuf, sizeof lastpath);
1279 if (one_file_system)
1280 filesystem_dev = st.st_dev;
1282 if (recurse || (xfer_dirs && is_dot_dir)) {
1283 struct file_struct *file;
1284 file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
1286 send_if_directory(f, flist, file, fbuf, len);
1288 send_file_name(f, flist, fbuf, &st, 0);
1293 if (!pop_dir(olddir)) {
1294 rsyserr(FERROR, errno, "pop_dir %s failed",
1295 full_fname(olddir));
1296 exit_cleanup(RERR_FILESELECT);
1301 gettimeofday(&end_tv, NULL);
1302 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1303 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1304 if (stats.flist_buildtime == 0)
1305 stats.flist_buildtime = 1;
1308 send_file_entry(NULL, f);
1310 if (show_filelist_p())
1311 finish_filelist_progress(flist);
1313 gettimeofday(&end_tv, NULL);
1314 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1315 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1317 if (flist->hlink_pool) {
1318 pool_destroy(flist->hlink_pool);
1319 flist->hlink_pool = NULL;
1322 /* Sort the list without removing any duplicates. This allows the
1323 * receiving side to ask for any name they like, which gives us the
1324 * flexibility to change the way we unduplicate names in the future
1325 * without causing a compatibility problem with older versions. */
1326 clean_flist(flist, 0, 0);
1330 /* send the io_error flag */
1331 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1334 stats.flist_size = stats.total_written - start_write;
1335 stats.num_files = flist->count;
1338 output_flist(flist);
1341 rprintf(FINFO, "send_file_list done\n");
1346 struct file_list *recv_file_list(int f)
1348 struct file_list *flist;
1349 unsigned short flags;
1352 if (show_filelist_p())
1353 start_filelist_progress("receiving file list");
1355 start_read = stats.total_read;
1357 flist = flist_new(WITH_HLINK, "recv_file_list");
1360 flist->malloced = 1000;
1361 flist->files = new_array(struct file_struct *, flist->malloced);
1365 while ((flags = read_byte(f)) != 0) {
1366 struct file_struct *file;
1368 flist_expand(flist);
1370 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1371 flags |= read_byte(f) << 8;
1372 file = receive_file_entry(flist, flags, f);
1374 if (S_ISREG(file->mode))
1375 stats.total_size += file->length;
1377 flist->files[flist->count++] = file;
1379 maybe_emit_filelist_progress(flist->count);
1382 rprintf(FINFO, "recv_file_name(%s)\n",
1383 f_name(file, NULL));
1386 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1389 rprintf(FINFO, "received %d names\n", flist->count);
1391 if (show_filelist_p())
1392 finish_filelist_progress(flist);
1394 clean_flist(flist, relative_paths, 1);
1397 recv_uid_list(f, flist);
1399 /* Recv the io_error flag */
1400 if (lp_ignore_errors(module_id) || ignore_errors)
1403 io_error |= read_int(f);
1407 output_flist(flist);
1411 for (i = 0; i < flist->count; i++)
1412 list_file_entry(flist->files[i]);
1416 rprintf(FINFO, "recv_file_list done\n");
1418 stats.flist_size = stats.total_read - start_read;
1419 stats.num_files = flist->count;
1424 out_of_memory("recv_file_list");
1425 return NULL; /* not reached */
1428 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1430 return f_name_cmp(*file1, *file2);
1433 /* Search for an identically-named item in the file list. Note that the
1434 * items must agree in their directory-ness, or no match is returned. */
1435 int flist_find(struct file_list *flist, struct file_struct *f)
1437 int low = flist->low, high = flist->high;
1438 int ret, mid, mid_up;
1440 while (low <= high) {
1441 mid = (low + high) / 2;
1442 for (mid_up = mid; !flist->files[mid_up]->basename; mid_up++) {}
1444 ret = f_name_cmp(flist->files[mid_up], f);
1448 if (protocol_version < 29
1449 && S_ISDIR(flist->files[mid_up]->mode)
1450 != S_ISDIR(f->mode))
1463 * Free up any resources a file_struct has allocated
1464 * and clear the file.
1466 void clear_file(int i, struct file_list *flist)
1468 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1469 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1470 memset(flist->files[i], 0, file_struct_len);
1474 * allocate a new file list
1476 struct file_list *flist_new(int with_hlink, char *msg)
1478 struct file_list *flist;
1480 flist = new(struct file_list);
1484 memset(flist, 0, sizeof (struct file_list));
1486 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1487 out_of_memory, POOL_INTERN)))
1490 #ifdef SUPPORT_HARD_LINKS
1491 if (with_hlink && preserve_hard_links) {
1492 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1493 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1502 * free up all elements in a flist
1504 void flist_free(struct file_list *flist)
1506 pool_destroy(flist->file_pool);
1507 pool_destroy(flist->hlink_pool);
1513 * This routine ensures we don't have any duplicate names in our file list.
1514 * duplicate names can cause corruption because of the pipelining
1516 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1522 if (flist->count == 0) {
1527 qsort(flist->files, flist->count,
1528 sizeof flist->files[0], (int (*)())file_compare);
1530 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1531 if (flist->files[i]->basename) {
1536 flist->low = prev_i;
1537 while (++i < flist->count) {
1539 struct file_struct *file = flist->files[i];
1541 if (!file->basename)
1543 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1545 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1546 int save_mode = file->mode;
1547 /* Make sure that this directory doesn't duplicate a
1548 * non-directory earlier in the list. */
1549 flist->high = prev_i;
1550 file->mode = S_IFREG;
1551 j = flist_find(flist, file);
1552 file->mode = save_mode;
1556 struct file_struct *fp = flist->files[j];
1558 /* If one is a dir and the other is not, we want to
1559 * keep the dir because it might have contents in the
1561 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1562 if (S_ISDIR(file->mode))
1568 if (verbose > 1 && !am_server) {
1570 "removing duplicate name %s from file list (%d)\n",
1571 f_name(file, NULL), drop);
1573 /* Make sure that if we unduplicate '.', that we don't
1574 * lose track of a user-specified top directory. */
1575 flist->files[keep]->flags |= flist->files[drop]->flags
1576 & (FLAG_TOP_DIR|FLAG_DEL_HERE);
1578 clear_file(drop, flist);
1581 if (flist->low == drop) {
1583 j < i && !flist->files[j]->basename;
1592 flist->high = no_dups ? prev_i : flist->count - 1;
1595 /* We need to strip off the leading slashes for relative
1596 * paths, but this must be done _after_ the sorting phase. */
1597 for (i = flist->low; i <= flist->high; i++) {
1598 struct file_struct *file = flist->files[i];
1602 if (*file->dirname == '/') {
1603 char *s = file->dirname + 1;
1604 while (*s == '/') s++;
1605 memmove(file->dirname, s, strlen(s) + 1);
1608 if (!*file->dirname)
1609 file->dirname = NULL;
1614 static void output_flist(struct file_list *flist)
1616 char uidbuf[16], gidbuf[16], depthbuf[16];
1617 struct file_struct *file;
1618 const char *who = who_am_i();
1621 for (i = 0; i < flist->count; i++) {
1622 file = flist->files[i];
1623 if ((am_root || am_sender) && preserve_uid)
1624 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1627 if (preserve_gid && file->gid != GID_NONE)
1628 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1632 sprintf(depthbuf, "%d", file->dir.depth);
1633 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1634 who, i, am_sender ? NS(file->dir.root) : depthbuf,
1635 file->dirname ? file->dirname : "",
1636 file->dirname ? "/" : "", NS(file->basename),
1637 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
1638 (double)file->length, uidbuf, gidbuf, file->flags);
1642 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1643 enum fnc_type { t_PATH, t_ITEM };
1645 /* Compare the names of two file_struct entities, similar to how strcmp()
1646 * would do if it were operating on the joined strings.
1648 * Some differences beginning with protocol_version 29: (1) directory names
1649 * are compared with an assumed trailing slash so that they compare in a
1650 * way that would cause them to sort immediately prior to any content they
1651 * may have; (2) a directory of any name compares after a non-directory of
1652 * any name at the same depth; (3) a directory with name "." compares prior
1653 * to anything else. These changes mean that a directory and a non-dir
1654 * with the same name will not compare as equal (protocol_version >= 29).
1656 * The dirname component can be an empty string, but the basename component
1657 * cannot (and never is in the current codebase). The basename component
1658 * may be NULL (for a removed item), in which case it is considered to be
1659 * after any existing item. */
1660 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1663 const uchar *c1, *c2;
1664 enum fnc_state state1, state2;
1665 enum fnc_type type1, type2;
1666 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1668 if (!f1 || !f1->basename) {
1669 if (!f2 || !f2->basename)
1673 if (!f2 || !f2->basename)
1676 c1 = (uchar*)f1->dirname;
1677 c2 = (uchar*)f2->dirname;
1681 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1682 c1 = (uchar*)f1->basename;
1683 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1685 state1 = s_TRAILING;
1698 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1699 c2 = (uchar*)f2->basename;
1700 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1702 state2 = s_TRAILING;
1716 return type1 == t_PATH ? 1 : -1;
1719 if ((dif = (int)*c1++ - (int)*c2++) != 0)
1728 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1729 c1 = (uchar*)f1->basename;
1730 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1732 state1 = s_TRAILING;
1738 state1 = s_TRAILING;
1739 if (type1 == t_PATH) {
1748 if (*c2 && type1 != type2)
1749 return type1 == t_PATH ? 1 : -1;
1758 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1759 c2 = (uchar*)f2->basename;
1760 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1762 state2 = s_TRAILING;
1768 state2 = s_TRAILING;
1769 if (type2 == t_PATH) {
1781 return type1 == t_PATH ? 1 : -1;
1788 /* Return a copy of the full filename of a flist entry, using the indicated
1789 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
1790 * done because we checked the size when creating the file_struct entry.
1792 char *f_name(struct file_struct *f, char *fbuf)
1794 if (!f || !f->basename)
1798 static char names[5][MAXPATHLEN];
1799 static unsigned int n;
1801 n = (n + 1) % (sizeof names / sizeof names[0]);
1807 int len = strlen(f->dirname);
1808 memcpy(fbuf, f->dirname, len);
1810 strcpy(fbuf + len + 1, f->basename);
1812 strcpy(fbuf, f->basename);
1817 /* Do a non-recursive scan of the named directory, possibly ignoring all
1818 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1819 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1820 * buffer (the functions we call will append names onto the end, but the old
1821 * dir value will be restored on exit). */
1822 struct file_list *get_dirlist(char *dirname, int dlen,
1823 int ignore_filter_rules)
1825 struct file_list *dirlist;
1826 char dirbuf[MAXPATHLEN];
1827 int save_recurse = recurse;
1830 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1831 if (dlen >= MAXPATHLEN)
1836 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1839 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
1840 recurse = save_recurse;
1842 flist_count_offset += dirlist->count;
1844 clean_flist(dirlist, 0, 0);
1847 output_flist(dirlist);