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_uid;
52 extern int preserve_gid;
53 extern int relative_paths;
54 extern int implied_dirs;
55 extern int copy_links;
56 extern int copy_unsafe_links;
57 extern int protocol_version;
58 extern int sanitize_paths;
59 extern int orig_umask;
60 extern struct stats stats;
61 extern struct file_list *the_file_list;
63 extern char curr_dir[MAXPATHLEN];
65 extern struct filter_list_struct filter_list;
66 extern struct filter_list_struct server_filter_list;
69 dev_t filesystem_dev; /* used to implement -x */
71 static char empty_sum[MD4_SUM_LENGTH];
72 static unsigned int file_struct_len;
73 static struct file_list *sorting_flist;
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;
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)
101 static void emit_filelist_progress(const struct file_list *flist)
103 rprintf(FINFO, " %d files...\r", flist->count);
107 static void maybe_emit_filelist_progress(const struct file_list *flist)
109 if (do_progress && show_filelist_p() && (flist->count % 100) == 0)
110 emit_filelist_progress(flist);
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)
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 safe_fname(f_name(f)), safe_fname(f->u.link));
150 rprintf(FINFO, "%s %11.0f %s %s\n",
152 (double)f->length, timestring(f->modtime),
153 safe_fname(f_name(f)));
159 * Stat either a symlink or its referent, depending on the settings of
160 * copy_links, copy_unsafe_links, etc.
162 * @retval -1 on error
164 * @retval 0 for success
166 * @post If @p path is a symlink, then @p linkbuf (of size @c
167 * MAXPATHLEN) contains the symlink target.
169 * @post @p buffer contains information about the link or the
170 * referrent as appropriate, if they exist.
172 static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
176 return do_stat(path, buffer);
177 if (link_stat(path, buffer, 0) < 0)
179 if (S_ISLNK(buffer->st_mode)) {
180 int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1);
184 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
186 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
187 safe_fname(path), safe_fname(linkbuf));
189 return do_stat(path, buffer);
194 return do_stat(path, buffer);
198 int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks)
202 return do_stat(path, buffer);
203 if (do_lstat(path, buffer) < 0)
205 if (follow_dirlinks && S_ISLNK(buffer->st_mode)) {
207 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
212 return do_stat(path, buffer);
216 /* This function is used to check if a file should be included/excluded
217 * from the list of files based on its name and type etc. The value of
218 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
219 static int is_excluded(char *fname, int is_dir, int filter_level)
221 #if 0 /* This currently never happens, so avoid a useless compare. */
222 if (filter_level == NO_FILTERS)
226 /* never exclude '.', even if somebody does --exclude '*' */
227 if (fname[0] == '.' && !fname[1])
229 /* Handle the -R version of the '.' dir. */
230 if (fname[0] == '/') {
231 int len = strlen(fname);
232 if (fname[len-1] == '.' && fname[len-2] == '/')
236 if (server_filter_list.head
237 && check_filter(&server_filter_list, fname, is_dir) < 0)
239 if (filter_level != ALL_FILTERS)
242 && check_filter(&filter_list, fname, is_dir) < 0)
247 static int to_wire_mode(mode_t mode)
250 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
251 return (mode & ~(_S_IFMT)) | 0120000;
256 static mode_t from_wire_mode(int mode)
258 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
259 return (mode & ~(_S_IFMT)) | _S_IFLNK;
264 static void send_directory(int f, struct file_list *flist,
265 char *fbuf, int len);
267 static char *flist_dir;
268 static int flist_dir_len;
272 * Make sure @p flist is big enough to hold at least @p flist->count
275 void flist_expand(struct file_list *flist)
277 struct file_struct **new_ptr;
279 if (flist->count < flist->malloced)
282 if (flist->malloced < FLIST_START)
283 flist->malloced = FLIST_START;
284 else if (flist->malloced >= FLIST_LINEAR)
285 flist->malloced += FLIST_LINEAR;
287 flist->malloced *= 2;
290 * In case count jumped or we are starting the list
291 * with a known size just set it.
293 if (flist->malloced < flist->count)
294 flist->malloced = flist->count;
296 new_ptr = realloc_array(flist->files, struct file_struct *,
299 if (verbose >= 2 && flist->malloced != FLIST_START) {
300 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
302 (double)sizeof flist->files[0] * flist->malloced,
303 (new_ptr == flist->files) ? " not" : "");
306 flist->files = new_ptr;
309 out_of_memory("flist_expand");
312 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
314 unsigned short flags;
315 static time_t modtime;
319 static uint32 rdev_major;
322 static char lastname[MAXPATHLEN];
323 char fname[MAXPATHLEN];
331 modtime = 0, mode = 0;
332 dev = 0, rdev = makedev(0, 0);
339 io_write_phase = "send_file_entry";
341 f_name_to(file, fname);
345 if (file->mode == mode)
346 flags |= XMIT_SAME_MODE;
349 if (preserve_devices) {
350 if (protocol_version < 28) {
351 if (IS_DEVICE(mode)) {
352 if (file->u.rdev == rdev)
353 flags |= XMIT_SAME_RDEV_pre28;
357 rdev = makedev(0, 0);
358 } else if (IS_DEVICE(mode)) {
360 if ((uint32)major(rdev) == rdev_major)
361 flags |= XMIT_SAME_RDEV_MAJOR;
363 rdev_major = major(rdev);
364 if ((uint32)minor(rdev) <= 0xFFu)
365 flags |= XMIT_RDEV_MINOR_IS_SMALL;
368 if (file->uid == uid)
369 flags |= XMIT_SAME_UID;
372 if (file->gid == gid)
373 flags |= XMIT_SAME_GID;
376 if (file->modtime == modtime)
377 flags |= XMIT_SAME_TIME;
379 modtime = file->modtime;
381 #ifdef SUPPORT_HARD_LINKS
382 if (file->link_u.idev) {
383 if (file->F_DEV == dev) {
384 if (protocol_version >= 28)
385 flags |= XMIT_SAME_DEV;
388 flags |= XMIT_HAS_IDEV_DATA;
393 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
395 l2 = strlen(fname+l1);
398 flags |= XMIT_SAME_NAME;
400 flags |= XMIT_LONG_NAME;
402 /* We must make sure we don't send a zero flag byte or the
403 * other end will terminate the flist transfer. Note that
404 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
405 * it's harmless way to add a bit to the first flag byte. */
406 if (protocol_version >= 28) {
407 if (!flags && !S_ISDIR(mode))
408 flags |= XMIT_TOP_DIR;
409 if ((flags & 0xFF00) || !flags) {
410 flags |= XMIT_EXTENDED_FLAGS;
411 write_byte(f, flags);
412 write_byte(f, flags >> 8);
414 write_byte(f, flags);
416 if (!(flags & 0xFF) && !S_ISDIR(mode))
417 flags |= XMIT_TOP_DIR;
419 flags |= XMIT_LONG_NAME;
420 write_byte(f, flags);
422 if (flags & XMIT_SAME_NAME)
424 if (flags & XMIT_LONG_NAME)
428 write_buf(f, fname + l1, l2);
430 write_longint(f, file->length);
431 if (!(flags & XMIT_SAME_TIME))
432 write_int(f, modtime);
433 if (!(flags & XMIT_SAME_MODE))
434 write_int(f, to_wire_mode(mode));
435 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
440 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
445 if (preserve_devices && IS_DEVICE(mode)) {
446 if (protocol_version < 28) {
447 if (!(flags & XMIT_SAME_RDEV_pre28))
448 write_int(f, (int)rdev);
450 if (!(flags & XMIT_SAME_RDEV_MAJOR))
451 write_int(f, major(rdev));
452 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
453 write_byte(f, minor(rdev));
455 write_int(f, minor(rdev));
460 if (preserve_links && S_ISLNK(mode)) {
461 int len = strlen(file->u.link);
463 write_buf(f, file->u.link, len);
467 #ifdef SUPPORT_HARD_LINKS
468 if (flags & XMIT_HAS_IDEV_DATA) {
469 if (protocol_version < 26) {
470 /* 32-bit dev_t and ino_t */
472 write_int(f, file->F_INODE);
474 /* 64-bit dev_t and ino_t */
475 if (!(flags & XMIT_SAME_DEV))
476 write_longint(f, dev);
477 write_longint(f, file->F_INODE);
482 if (always_checksum) {
486 else if (protocol_version < 28) {
487 /* Prior to 28, we sent a useless set of nulls. */
493 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
497 strlcpy(lastname, fname, MAXPATHLEN);
499 io_write_phase = "unknown";
504 static struct file_struct *receive_file_entry(struct file_list *flist,
505 unsigned short flags, int f)
507 static time_t modtime;
511 static uint32 rdev_major;
514 static char lastname[MAXPATHLEN], *lastdir;
515 static int lastdir_depth, lastdir_len = -1;
516 static unsigned int del_hier_name_len = 0;
517 static int in_del_hier = 0;
518 char thisname[MAXPATHLEN];
519 unsigned int l1 = 0, l2 = 0;
520 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
522 char *basename, *dirname, *bp;
523 struct file_struct *file;
526 modtime = 0, mode = 0;
527 dev = 0, rdev = makedev(0, 0);
536 if (flags & XMIT_SAME_NAME)
539 if (flags & XMIT_LONG_NAME)
544 if (l2 >= MAXPATHLEN - l1) {
546 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
547 flags, l1, l2, safe_fname(lastname));
548 overflow("receive_file_entry");
551 strlcpy(thisname, lastname, l1 + 1);
552 read_sbuf(f, &thisname[l1], l2);
553 thisname[l1 + l2] = 0;
555 strlcpy(lastname, thisname, MAXPATHLEN);
557 clean_fname(thisname, 0);
560 sanitize_path(thisname, thisname, "", 0);
562 if ((basename = strrchr(thisname, '/')) != NULL) {
563 dirname_len = ++basename - thisname; /* counts future '\0' */
564 if (lastdir_len == dirname_len - 1
565 && strncmp(thisname, lastdir, lastdir_len) == 0) {
567 dirname_len = 0; /* indicates no copy is needed */
575 basename_len = strlen(basename) + 1; /* count the '\0' */
577 file_length = read_longint(f);
578 if (!(flags & XMIT_SAME_TIME))
579 modtime = (time_t)read_int(f);
580 if (!(flags & XMIT_SAME_MODE))
581 mode = from_wire_mode(read_int(f));
583 if (preserve_uid && !(flags & XMIT_SAME_UID))
584 uid = (uid_t)read_int(f);
585 if (preserve_gid && !(flags & XMIT_SAME_GID))
586 gid = (gid_t)read_int(f);
588 if (preserve_devices) {
589 if (protocol_version < 28) {
590 if (IS_DEVICE(mode)) {
591 if (!(flags & XMIT_SAME_RDEV_pre28))
592 rdev = (dev_t)read_int(f);
594 rdev = makedev(0, 0);
595 } else if (IS_DEVICE(mode)) {
597 if (!(flags & XMIT_SAME_RDEV_MAJOR))
598 rdev_major = read_int(f);
599 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
600 rdev_minor = read_byte(f);
602 rdev_minor = read_int(f);
603 rdev = makedev(rdev_major, rdev_minor);
608 if (preserve_links && S_ISLNK(mode)) {
609 linkname_len = read_int(f) + 1; /* count the '\0' */
610 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
611 rprintf(FERROR, "overflow: linkname_len=%d\n",
613 overflow("receive_file_entry");
620 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
622 alloc_len = file_struct_len + dirname_len + basename_len
623 + linkname_len + sum_len;
624 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
626 file = (struct file_struct *)bp;
627 memset(bp, 0, file_struct_len);
628 bp += file_struct_len;
631 file->modtime = modtime;
632 file->length = file_length;
638 file->dirname = lastdir = bp;
639 lastdir_len = dirname_len - 1;
640 memcpy(bp, dirname, dirname_len - 1);
643 lastdir_depth = count_dir_elements(lastdir);
644 file->dir.depth = lastdir_depth + 1;
645 } else if (dirname) {
646 file->dirname = dirname; /* we're reusing lastname */
647 file->dir.depth = lastdir_depth + 1;
652 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
654 if (flags & XMIT_TOP_DIR) {
656 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
657 file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
658 } else if (in_del_hier) {
659 if (!relative_paths || !del_hier_name_len
660 || (l1 >= del_hier_name_len
661 && thisname[del_hier_name_len] == '/'))
662 file->flags |= FLAG_DEL_HERE;
669 memcpy(bp, basename, basename_len);
672 if (preserve_devices && IS_DEVICE(mode))
678 read_sbuf(f, bp, linkname_len - 1);
680 sanitize_path(bp, bp, "", lastdir_depth);
685 #ifdef SUPPORT_HARD_LINKS
686 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
687 flags |= XMIT_HAS_IDEV_DATA;
688 if (flags & XMIT_HAS_IDEV_DATA) {
690 if (protocol_version < 26) {
694 if (!(flags & XMIT_SAME_DEV))
695 dev = read_longint(f);
696 inode = read_longint(f);
698 if (flist->hlink_pool) {
699 file->link_u.idev = pool_talloc(flist->hlink_pool,
700 struct idev, 1, "inode_table");
701 file->F_INODE = inode;
707 if (always_checksum) {
710 file->u.sum = sum = bp;
712 } else if (protocol_version < 28) {
713 /* Prior to 28, we get a useless set of nulls. */
719 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
723 if (!preserve_perms) {
724 /* set an appropriate set of permissions based on original
725 * permissions and umask. This emulates what GNU cp does */
726 file->mode &= ~orig_umask;
734 * Create a file_struct for a named file by reading its stat()
735 * information and performing extensive checks against global
738 * @return the new file, or NULL if there was an error or this file
739 * should be excluded.
741 * @todo There is a small optimization opportunity here to avoid
742 * stat()ing the file in some circumstances, which has a certain cost.
743 * We are called immediately after doing readdir(), and so we may
744 * already know the d_type of the file. We could for example avoid
745 * statting directories if we're not recursing, but this is not a very
746 * important case. Some systems may not have d_type.
748 struct file_struct *make_file(char *fname, struct file_list *flist,
751 static char *lastdir;
752 static int lastdir_len = -1;
753 struct file_struct *file;
755 char sum[SUM_LENGTH];
756 char thisname[MAXPATHLEN];
757 char linkname[MAXPATHLEN];
758 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
759 char *basename, *dirname, *bp;
760 unsigned short flags = 0;
762 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
765 if (strlcpy(thisname, fname, sizeof thisname)
766 >= sizeof thisname - flist_dir_len) {
767 rprintf(FINFO, "skipping overly long name: %s\n",
771 clean_fname(thisname, 0);
773 sanitize_path(thisname, thisname, "", 0);
775 memset(sum, 0, SUM_LENGTH);
777 if (readlink_stat(thisname, &st, linkname) != 0) {
778 int save_errno = errno;
779 /* See if file is excluded before reporting an error. */
780 if (filter_level != NO_FILTERS
781 && is_excluded(thisname, 0, filter_level))
783 if (save_errno == ENOENT) {
785 /* Avoid "vanished" error if symlink points nowhere. */
786 if (copy_links && do_lstat(thisname, &st) == 0
787 && S_ISLNK(st.st_mode)) {
788 io_error |= IOERR_GENERAL;
789 rprintf(FERROR, "symlink has no referent: %s\n",
790 full_fname(thisname));
794 enum logcode c = am_daemon && protocol_version < 28
796 io_error |= IOERR_VANISHED;
797 rprintf(c, "file has vanished: %s\n",
798 full_fname(thisname));
801 io_error |= IOERR_GENERAL;
802 rsyserr(FERROR, save_errno, "readlink %s failed",
803 full_fname(thisname));
808 /* backup.c calls us with filter_level set to NO_FILTERS. */
809 if (filter_level == NO_FILTERS)
812 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
813 rprintf(FINFO, "skipping directory %s\n", safe_fname(thisname));
817 /* We only care about directories because we need to avoid recursing
818 * into a mount-point directory, not to avoid copying a symlinked
819 * file if -L (or similar) was specified. */
820 if (one_file_system && st.st_dev != filesystem_dev
821 && S_ISDIR(st.st_mode))
822 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(), safe_fname(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 && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
865 alloc_len = file_struct_len + dirname_len + basename_len
866 + linkname_len + sum_len;
868 bp = pool_alloc(flist->file_pool, alloc_len,
869 "receive_file_entry");
871 if (!(bp = new_array(char, alloc_len)))
872 out_of_memory("receive_file_entry");
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 file->u.rdev = st.st_rdev;
927 memcpy(bp, linkname, linkname_len);
934 file_checksum(thisname, bp, st.st_size);
938 file->dir.root = flist_dir;
940 /* This code is only used by the receiver when it is building
941 * a list of files for a delete pass. */
942 if (keep_dirlinks && linkname_len && flist) {
944 int save_mode = file->mode;
945 file->mode = S_IFDIR; /* find a directory w/our name */
946 if (flist_find(the_file_list, file) >= 0
947 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
948 file->modtime = st2.st_mtime;
949 file->length = st2.st_size;
950 file->mode = st2.st_mode;
951 file->uid = st2.st_uid;
952 file->gid = st2.st_gid;
955 file->mode = save_mode;
958 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
959 stats.total_size += st.st_size;
965 void send_file_name(int f, struct file_list *flist, char *fname,
966 int recursive, unsigned short base_flags)
968 struct file_struct *file;
969 char fbuf[MAXPATHLEN];
971 file = make_file(fname, flist, f == -2 ? SERVER_FILTERS : ALL_FILTERS);
975 maybe_emit_filelist_progress(flist);
979 if (file->basename[0]) {
980 flist->files[flist->count++] = file;
981 send_file_entry(file, f, base_flags);
984 if (recursive && S_ISDIR(file->mode)
985 && !(file->flags & FLAG_MOUNT_POINT) && f_name_to(file, fbuf)) {
987 unsigned int len = strlen(fbuf);
988 if (len > 1 && fbuf[len-1] == '/')
990 if (len >= MAXPATHLEN - 1) {
991 io_error |= IOERR_GENERAL;
992 rprintf(FERROR, "skipping long-named directory: %s\n",
996 save_filters = push_local_filters(fbuf, len);
997 send_directory(f, flist, fbuf, len);
998 pop_local_filters(save_filters);
1003 /* Note that the "recurse" value either contains -1, for infinite recursion, or
1004 * a number >= 0 indicating how many levels of recursion we will allow. This
1005 * function is normally called by the sender, but the receiving side also calls
1006 * it from delete_in_dir() with f set to -1 so that we just construct the file
1007 * list in memory without sending it over the wire. Also, get_dirlist() might
1008 * call this with f set to -2, which indicates that local filter rules should
1010 static void send_directory(int f, struct file_list *flist,
1011 char *fbuf, int len)
1018 if (!(d = opendir(fbuf))) {
1019 io_error |= IOERR_GENERAL;
1020 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1025 if (len != 1 || *fbuf != '/')
1028 remainder = MAXPATHLEN - (p - fbuf);
1030 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1031 char *dname = d_name(di);
1032 if (dname[0] == '.' && (dname[1] == '\0'
1033 || (dname[1] == '.' && dname[2] == '\0')))
1035 if (strlcpy(p, dname, remainder) < remainder) {
1036 int do_subdirs = recurse >= 1 ? recurse-- : recurse;
1037 send_file_name(f, flist, fbuf, do_subdirs, 0);
1039 io_error |= IOERR_GENERAL;
1041 "cannot send long-named file %s\n",
1049 io_error |= IOERR_GENERAL;
1050 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1057 struct file_list *send_file_list(int f, int argc, char *argv[])
1061 char *p, *dir, olddir[sizeof curr_dir];
1062 char lastpath[MAXPATHLEN] = "";
1063 struct file_list *flist;
1064 struct timeval start_tv, end_tv;
1068 if (show_filelist_p())
1069 start_filelist_progress("building file list");
1071 start_write = stats.total_written;
1072 gettimeofday(&start_tv, NULL);
1074 flist = flist_new(WITH_HLINK, "send_file_list");
1076 io_start_buffering_out();
1077 if (filesfrom_fd >= 0) {
1078 if (argv[0] && !push_dir(argv[0])) {
1079 rsyserr(FERROR, errno, "push_dir %s failed",
1080 full_fname(argv[0]));
1081 exit_cleanup(RERR_FILESELECT);
1087 char fname2[MAXPATHLEN];
1088 char *fname = fname2;
1092 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1094 sanitize_path(fname, fname, "", 0);
1098 strlcpy(fname, *argv++, MAXPATHLEN);
1100 sanitize_path(fname, fname, "", 0);
1104 if (!l || fname[l - 1] == '/') {
1105 if (l == 2 && fname[0] == '.') {
1106 /* Turn "./" into just "." rather than "./." */
1108 } else if (l < MAXPATHLEN) {
1113 if (fname[l-1] == '.' && (l == 1 || fname[l-2] == '/')) {
1114 if (!recurse && xfer_dirs)
1115 recurse = 1; /* allow one level */
1116 } else if (recurse > 0)
1119 if (link_stat(fname, &st, keep_dirlinks) != 0) {
1120 io_error |= IOERR_GENERAL;
1121 rsyserr(FERROR, errno, "link_stat %s failed",
1126 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1127 rprintf(FINFO, "skipping directory %s\n",
1135 if (!relative_paths) {
1136 p = strrchr(fname, '/');
1145 } else if (implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1146 /* this ensures we send the intermediate directories,
1147 thus getting their permissions right */
1148 char *lp = lastpath, *fn = fname, *slash = fname;
1150 /* Skip any initial directories in our path that we
1151 * have in common with lastpath. */
1152 while (*fn && *lp == *fn) {
1158 if (fn != p || (*lp && *lp != '/')) {
1159 int save_copy_links = copy_links;
1160 int save_xfer_dirs = xfer_dirs;
1161 copy_links = copy_unsafe_links;
1163 while ((slash = strchr(slash+1, '/')) != 0) {
1165 send_file_name(f, flist, fname, 0, 0);
1168 copy_links = save_copy_links;
1169 xfer_dirs = save_xfer_dirs;
1171 strlcpy(lastpath, fname, sizeof lastpath);
1180 static char *lastdir;
1181 static int lastdir_len;
1183 strcpy(olddir, curr_dir); /* can't overflow */
1185 if (!push_dir(dir)) {
1186 io_error |= IOERR_GENERAL;
1187 rsyserr(FERROR, errno, "push_dir %s failed",
1192 if (lastdir && strcmp(lastdir, dir) == 0) {
1193 flist_dir = lastdir;
1194 flist_dir_len = lastdir_len;
1196 flist_dir = lastdir = strdup(dir);
1197 flist_dir_len = lastdir_len = strlen(dir);
1201 if (one_file_system)
1202 filesystem_dev = st.st_dev;
1204 do_subdirs = recurse >= 1 ? recurse-- : recurse;
1205 send_file_name(f, flist, fname, do_subdirs, XMIT_TOP_DIR);
1210 if (!pop_dir(olddir)) {
1211 rsyserr(FERROR, errno, "pop_dir %s failed",
1213 exit_cleanup(RERR_FILESELECT);
1218 gettimeofday(&end_tv, NULL);
1219 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1220 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1221 if (stats.flist_buildtime == 0)
1222 stats.flist_buildtime = 1;
1225 send_file_entry(NULL, f, 0);
1227 if (show_filelist_p())
1228 finish_filelist_progress(flist);
1230 gettimeofday(&end_tv, NULL);
1231 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1232 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1234 if (flist->hlink_pool) {
1235 pool_destroy(flist->hlink_pool);
1236 flist->hlink_pool = NULL;
1239 /* Sort the list without removing any duplicates. This allows the
1240 * receiving side to ask for any name they like, which gives us the
1241 * flexibility to change the way we unduplicate names in the future
1242 * without causing a compatibility problem with older versions. */
1243 clean_flist(flist, 0, 0);
1245 /* Now send the uid/gid list. This was introduced in
1246 * protocol version 15 */
1249 /* send the io_error flag */
1250 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1253 stats.flist_size = stats.total_written - start_write;
1254 stats.num_files = flist->count;
1257 output_flist(flist);
1260 rprintf(FINFO, "send_file_list done\n");
1266 struct file_list *recv_file_list(int f)
1268 struct file_list *flist;
1269 unsigned short flags;
1272 if (show_filelist_p())
1273 start_filelist_progress("receiving file list");
1275 start_read = stats.total_read;
1277 flist = flist_new(WITH_HLINK, "recv_file_list");
1280 flist->malloced = 1000;
1281 flist->files = new_array(struct file_struct *, flist->malloced);
1286 while ((flags = read_byte(f)) != 0) {
1287 struct file_struct *file;
1289 flist_expand(flist);
1291 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1292 flags |= read_byte(f) << 8;
1293 file = receive_file_entry(flist, flags, f);
1295 if (S_ISREG(file->mode))
1296 stats.total_size += file->length;
1298 flist->files[flist->count++] = file;
1300 maybe_emit_filelist_progress(flist);
1303 rprintf(FINFO, "recv_file_name(%s)\n",
1304 safe_fname(f_name(file)));
1307 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1310 rprintf(FINFO, "received %d names\n", flist->count);
1312 if (show_filelist_p())
1313 finish_filelist_progress(flist);
1315 clean_flist(flist, relative_paths, 1);
1318 /* Now send the uid/gid list. This was introduced in
1319 * protocol version 15 */
1320 recv_uid_list(f, flist);
1322 /* Recv the io_error flag */
1323 if (lp_ignore_errors(module_id) || ignore_errors)
1326 io_error |= read_int(f);
1330 output_flist(flist);
1334 for (i = 0; i < flist->count; i++)
1335 list_file_entry(flist->files[i]);
1339 rprintf(FINFO, "recv_file_list done\n");
1341 stats.flist_size = stats.total_read - start_read;
1342 stats.num_files = flist->count;
1347 out_of_memory("recv_file_list");
1348 return NULL; /* not reached */
1352 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1354 return f_name_cmp(*file1, *file2);
1358 /* Search for an identically-named item in the file list. Note that the
1359 * items must agree in their directory-ness, or no match is returned. */
1360 int flist_find(struct file_list *flist, struct file_struct *f)
1362 int low = flist->low, high = flist->high;
1363 int ret, mid, mid_up;
1365 while (low <= high) {
1366 mid = (low + high) / 2;
1367 for (mid_up = mid; !flist->files[mid_up]->basename; mid_up++) {}
1369 ret = f_name_cmp(flist->files[mid_up], f);
1373 if (protocol_version < 29
1374 && S_ISDIR(flist->files[mid_up]->mode)
1375 != S_ISDIR(f->mode))
1389 * Free up any resources a file_struct has allocated
1390 * and clear the file.
1392 void clear_file(int i, struct file_list *flist)
1394 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1395 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1396 memset(flist->files[i], 0, file_struct_len);
1401 * allocate a new file list
1403 struct file_list *flist_new(int with_hlink, char *msg)
1405 struct file_list *flist;
1407 flist = new(struct file_list);
1411 memset(flist, 0, sizeof (struct file_list));
1413 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1414 out_of_memory, POOL_INTERN)))
1417 #ifdef SUPPORT_HARD_LINKS
1418 if (with_hlink && preserve_hard_links) {
1419 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1420 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1429 * free up all elements in a flist
1431 void flist_free(struct file_list *flist)
1433 pool_destroy(flist->file_pool);
1434 pool_destroy(flist->hlink_pool);
1441 * This routine ensures we don't have any duplicate names in our file list.
1442 * duplicate names can cause corruption because of the pipelining
1444 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1448 if (!flist || flist->count == 0)
1451 sorting_flist = flist;
1452 qsort(flist->files, flist->count,
1453 sizeof flist->files[0], (int (*)())file_compare);
1454 sorting_flist = NULL;
1456 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1457 if (flist->files[i]->basename) {
1462 flist->low = prev_i;
1463 while (++i < flist->count) {
1465 struct file_struct *file = flist->files[i];
1467 if (!file->basename)
1469 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1471 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1472 int save_mode = file->mode;
1473 /* Make sure that this directory doesn't duplicate a
1474 * non-directory earlier in the list. */
1475 flist->high = prev_i;
1476 file->mode = S_IFREG;
1477 j = flist_find(flist, file);
1478 file->mode = save_mode;
1482 struct file_struct *fp = flist->files[j];
1484 /* If one is a dir and the other is not, we want to
1485 * keep the dir because it might have contents in the
1487 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1488 if (S_ISDIR(file->mode))
1494 if (verbose > 1 && !am_server) {
1496 "removing duplicate name %s from file list (%d)\n",
1497 safe_fname(f_name(file)), drop);
1499 /* Make sure that if we unduplicate '.', that we don't
1500 * lose track of a user-specified top directory. */
1501 if (flist->files[drop]->flags & FLAG_TOP_DIR)
1502 flist->files[keep]->flags |= FLAG_TOP_DIR;
1504 clear_file(drop, flist);
1507 if (flist->low == drop) {
1509 j < i && !flist->files[j]->basename;
1518 flist->high = no_dups ? prev_i : flist->count - 1;
1521 /* We need to strip off the leading slashes for relative
1522 * paths, but this must be done _after_ the sorting phase. */
1523 for (i = flist->low; i <= flist->high; i++) {
1524 struct file_struct *file = flist->files[i];
1528 if (*file->dirname == '/') {
1529 char *s = file->dirname + 1;
1530 while (*s == '/') s++;
1531 memmove(file->dirname, s, strlen(s) + 1);
1534 if (!*file->dirname)
1535 file->dirname = NULL;
1541 static void output_flist(struct file_list *flist)
1543 char uidbuf[16], gidbuf[16], depthbuf[16];
1544 struct file_struct *file;
1545 const char *who = who_am_i();
1548 for (i = 0; i < flist->count; i++) {
1549 file = flist->files[i];
1550 if ((am_root || am_sender) && preserve_uid)
1551 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1554 if (preserve_gid && file->gid != GID_NONE)
1555 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1559 sprintf(depthbuf, "%d", file->dir.depth);
1560 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1561 who, i, am_sender ? NS(file->dir.root) : depthbuf,
1562 file->dirname ? safe_fname(file->dirname) : "",
1563 file->dirname ? "/" : "", NS(file->basename),
1564 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
1565 (double)file->length, uidbuf, gidbuf, file->flags);
1570 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1571 enum fnc_type { t_PATH, t_ITEM };
1573 /* Compare the names of two file_struct entities, similar to how strcmp()
1574 * would do if it were operating on the joined strings.
1576 * Some differences beginning with protocol_version 29: (1) directory names
1577 * are compared with an assumed trailing slash so that they compare in a
1578 * way that would cause them to sort immediately prior to any content they
1579 * may have; (2) a directory of any name compares after a non-directory of
1580 * any name at the same depth; (3) a directory with name "." compares prior
1581 * to anything else. These changes mean that a directory and a non-dir
1582 * with the same name will not compare as equal (protocol_version >= 29).
1584 * The dirname component can be an empty string, but the basename component
1585 * cannot (and never is in the current codebase). The basename component
1586 * may be NULL (for a removed item), in which case it is considered to be
1587 * after any existing item. */
1588 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1591 const uchar *c1, *c2;
1592 enum fnc_state state1, state2;
1593 enum fnc_type type1, type2;
1594 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1596 if (!f1 || !f1->basename) {
1597 if (!f2 || !f2->basename)
1601 if (!f2 || !f2->basename)
1604 c1 = (uchar*)f1->dirname;
1605 c2 = (uchar*)f2->dirname;
1609 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1610 c1 = (uchar*)f1->basename;
1611 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1613 state1 = s_TRAILING;
1626 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1627 c2 = (uchar*)f2->basename;
1628 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1630 state2 = s_TRAILING;
1644 return type1 == t_PATH ? 1 : -1;
1647 if ((dif = (int)*c1++ - (int)*c2++) != 0)
1656 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1658 c1 = (uchar*)f1->basename;
1661 state1 = s_TRAILING;
1662 if (type1 == t_PATH) {
1671 if (*c2 && type1 != type2)
1672 return type1 == t_PATH ? 1 : -1;
1677 if (state1 == s_SLASH && sorting_flist) {
1679 /* Optimize for future comparisons. */
1681 j < sorting_flist->count;
1683 struct file_struct *fp
1684 = sorting_flist->files[j];
1685 if (fp->dirname == f2->dirname)
1686 fp->dirname = f1->dirname;
1693 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1695 c2 = (uchar*)f2->basename;
1698 state2 = s_TRAILING;
1699 if (type2 == t_PATH) {
1711 return type1 == t_PATH ? 1 : -1;
1719 /* Return a copy of the full filename of a flist entry, using the indicated
1720 * buffer. No size-checking is done because we checked the size when creating
1721 * the file_struct entry.
1723 char *f_name_to(struct file_struct *f, char *fbuf)
1725 if (!f || !f->basename)
1729 int len = strlen(f->dirname);
1730 memcpy(fbuf, f->dirname, len);
1732 strcpy(fbuf + len + 1, f->basename);
1734 strcpy(fbuf, f->basename);
1739 /* Like f_name_to(), but we rotate through 5 static buffers of our own. */
1740 char *f_name(struct file_struct *f)
1742 static char names[5][MAXPATHLEN];
1743 static unsigned int n;
1745 n = (n + 1) % (sizeof names / sizeof names[0]);
1747 return f_name_to(f, names[n]);
1751 /* Do a non-recursive scan of the named directory, possibly ignoring all
1752 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1753 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1754 * buffer (the functions we call will append names onto the end, but the old
1755 * dir value will be restored on exit). */
1756 struct file_list *get_dirlist(char *dirname, int dlen,
1757 int ignore_filter_rules)
1759 struct file_list *dirlist;
1760 char dirbuf[MAXPATHLEN];
1761 int save_recurse = recurse;
1764 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1765 if (dlen >= MAXPATHLEN)
1770 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1773 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
1774 recurse = save_recurse;
1776 clean_flist(dirlist, 0, 0);
1779 output_flist(dirlist);