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 int flist_count_offset;
73 static unsigned int file_struct_len;
74 static struct file_list *sorting_flist;
76 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
77 static void output_flist(struct file_list *flist);
83 /* Figure out how big the file_struct is without trailing padding */
84 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)
100 static void emit_filelist_progress(int count)
102 rprintf(FINFO, " %d files...\r", count);
105 static void maybe_emit_filelist_progress(int count)
107 if (do_progress && show_filelist_p() && (count % 100) == 0)
108 emit_filelist_progress(count);
111 static void finish_filelist_progress(const struct file_list *flist)
114 /* This overwrites the progress line */
115 rprintf(FINFO, "%d file%sto consider\n",
116 flist->count, flist->count == 1 ? " " : "s ");
118 rprintf(FINFO, "done\n");
121 void show_flist_stats(void)
126 static void list_file_entry(struct file_struct *f)
131 /* this can happen if duplicate names were removed */
135 permstring(perms, f->mode);
138 if (preserve_links && S_ISLNK(f->mode)) {
139 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
141 (double)f->length, timestring(f->modtime),
142 safe_fname(f_name(f)), safe_fname(f->u.link));
146 rprintf(FINFO, "%s %11.0f %s %s\n",
148 (double)f->length, timestring(f->modtime),
149 safe_fname(f_name(f)));
154 * Stat either a symlink or its referent, depending on the settings of
155 * copy_links, copy_unsafe_links, etc.
157 * @retval -1 on error
159 * @retval 0 for success
161 * @post If @p path is a symlink, then @p linkbuf (of size @c
162 * MAXPATHLEN) contains the symlink target.
164 * @post @p buffer contains information about the link or the
165 * referrent as appropriate, if they exist.
167 static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
171 return do_stat(path, buffer);
172 if (link_stat(path, buffer, 0) < 0)
174 if (S_ISLNK(buffer->st_mode)) {
175 int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1);
179 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
181 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
182 safe_fname(path), safe_fname(linkbuf));
184 return do_stat(path, buffer);
189 return do_stat(path, buffer);
193 int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks)
197 return do_stat(path, buffer);
198 if (do_lstat(path, buffer) < 0)
200 if (follow_dirlinks && S_ISLNK(buffer->st_mode)) {
202 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
207 return do_stat(path, buffer);
211 /* This function is used to check if a file should be included/excluded
212 * from the list of files based on its name and type etc. The value of
213 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
214 static int is_excluded(char *fname, int is_dir, int filter_level)
216 #if 0 /* This currently never happens, so avoid a useless compare. */
217 if (filter_level == NO_FILTERS)
221 /* never exclude '.', even if somebody does --exclude '*' */
222 if (fname[0] == '.' && !fname[1])
224 /* Handle the -R version of the '.' dir. */
225 if (fname[0] == '/') {
226 int len = strlen(fname);
227 if (fname[len-1] == '.' && fname[len-2] == '/')
231 if (server_filter_list.head
232 && check_filter(&server_filter_list, fname, is_dir) < 0)
234 if (filter_level != ALL_FILTERS)
237 && check_filter(&filter_list, fname, is_dir) < 0)
242 static int to_wire_mode(mode_t mode)
245 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
246 return (mode & ~(_S_IFMT)) | 0120000;
251 static mode_t from_wire_mode(int mode)
253 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
254 return (mode & ~(_S_IFMT)) | _S_IFLNK;
258 static void send_directory(int f, struct file_list *flist,
259 char *fbuf, int len);
261 static char *flist_dir;
262 static int flist_dir_len;
266 * Make sure @p flist is big enough to hold at least @p flist->count
269 void flist_expand(struct file_list *flist)
271 struct file_struct **new_ptr;
273 if (flist->count < flist->malloced)
276 if (flist->malloced < FLIST_START)
277 flist->malloced = FLIST_START;
278 else if (flist->malloced >= FLIST_LINEAR)
279 flist->malloced += FLIST_LINEAR;
281 flist->malloced *= 2;
284 * In case count jumped or we are starting the list
285 * with a known size just set it.
287 if (flist->malloced < flist->count)
288 flist->malloced = flist->count;
290 new_ptr = realloc_array(flist->files, struct file_struct *,
293 if (verbose >= 2 && flist->malloced != FLIST_START) {
294 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
296 (double)sizeof flist->files[0] * flist->malloced,
297 (new_ptr == flist->files) ? " not" : "");
300 flist->files = new_ptr;
303 out_of_memory("flist_expand");
306 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
308 unsigned short flags;
309 static time_t modtime;
313 static uint32 rdev_major;
316 static char lastname[MAXPATHLEN];
317 char fname[MAXPATHLEN];
325 modtime = 0, mode = 0;
326 dev = 0, rdev = makedev(0, 0);
333 io_write_phase = "send_file_entry";
335 f_name_to(file, fname);
339 if (file->mode == mode)
340 flags |= XMIT_SAME_MODE;
343 if (preserve_devices) {
344 if (protocol_version < 28) {
345 if (IS_DEVICE(mode)) {
346 if (file->u.rdev == rdev)
347 flags |= XMIT_SAME_RDEV_pre28;
351 rdev = makedev(0, 0);
352 } else if (IS_DEVICE(mode)) {
354 if ((uint32)major(rdev) == rdev_major)
355 flags |= XMIT_SAME_RDEV_MAJOR;
357 rdev_major = major(rdev);
358 if ((uint32)minor(rdev) <= 0xFFu)
359 flags |= XMIT_RDEV_MINOR_IS_SMALL;
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 if (protocol_version < 28) {
439 if (!(flags & XMIT_SAME_RDEV_pre28))
440 write_int(f, (int)rdev);
442 if (!(flags & XMIT_SAME_RDEV_MAJOR))
443 write_int(f, major(rdev));
444 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
445 write_byte(f, minor(rdev));
447 write_int(f, minor(rdev));
452 if (preserve_links && S_ISLNK(mode)) {
453 int len = strlen(file->u.link);
455 write_buf(f, file->u.link, len);
459 #ifdef SUPPORT_HARD_LINKS
460 if (flags & XMIT_HAS_IDEV_DATA) {
461 if (protocol_version < 26) {
462 /* 32-bit dev_t and ino_t */
464 write_int(f, file->F_INODE);
466 /* 64-bit dev_t and ino_t */
467 if (!(flags & XMIT_SAME_DEV))
468 write_longint(f, dev);
469 write_longint(f, file->F_INODE);
474 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
476 int slen = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
480 /* Prior to 28, we sent a useless set of nulls. */
483 write_buf(f, sum, slen);
486 strlcpy(lastname, fname, MAXPATHLEN);
488 io_write_phase = "unknown";
491 static struct file_struct *receive_file_entry(struct file_list *flist,
492 unsigned short flags, int f)
494 static time_t modtime;
498 static uint32 rdev_major;
501 static char lastname[MAXPATHLEN], *lastdir;
502 static int lastdir_depth, lastdir_len = -1;
503 static unsigned int del_hier_name_len = 0;
504 static int in_del_hier = 0;
505 char thisname[MAXPATHLEN];
506 unsigned int l1 = 0, l2 = 0;
507 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
509 char *basename, *dirname, *bp;
510 struct file_struct *file;
513 modtime = 0, mode = 0;
514 dev = 0, rdev = makedev(0, 0);
523 if (flags & XMIT_SAME_NAME)
526 if (flags & XMIT_LONG_NAME)
531 if (l2 >= MAXPATHLEN - l1) {
533 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
534 flags, l1, l2, safe_fname(lastname));
535 overflow_exit("receive_file_entry");
538 strlcpy(thisname, lastname, l1 + 1);
539 read_sbuf(f, &thisname[l1], l2);
540 thisname[l1 + l2] = 0;
542 strlcpy(lastname, thisname, MAXPATHLEN);
544 clean_fname(thisname, 0);
547 sanitize_path(thisname, thisname, "", 0);
549 if ((basename = strrchr(thisname, '/')) != NULL) {
550 dirname_len = ++basename - thisname; /* counts future '\0' */
551 if (lastdir_len == dirname_len - 1
552 && strncmp(thisname, lastdir, lastdir_len) == 0) {
554 dirname_len = 0; /* indicates no copy is needed */
562 basename_len = strlen(basename) + 1; /* count the '\0' */
564 file_length = read_longint(f);
565 if (!(flags & XMIT_SAME_TIME))
566 modtime = (time_t)read_int(f);
567 if (!(flags & XMIT_SAME_MODE))
568 mode = from_wire_mode(read_int(f));
570 if (preserve_uid && !(flags & XMIT_SAME_UID))
571 uid = (uid_t)read_int(f);
572 if (preserve_gid && !(flags & XMIT_SAME_GID))
573 gid = (gid_t)read_int(f);
575 if (preserve_devices) {
576 if (protocol_version < 28) {
577 if (IS_DEVICE(mode)) {
578 if (!(flags & XMIT_SAME_RDEV_pre28))
579 rdev = (dev_t)read_int(f);
581 rdev = makedev(0, 0);
582 } else if (IS_DEVICE(mode)) {
584 if (!(flags & XMIT_SAME_RDEV_MAJOR))
585 rdev_major = read_int(f);
586 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
587 rdev_minor = read_byte(f);
589 rdev_minor = read_int(f);
590 rdev = makedev(rdev_major, rdev_minor);
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;
618 file->modtime = modtime;
619 file->length = file_length;
625 file->dirname = lastdir = bp;
626 lastdir_len = dirname_len - 1;
627 memcpy(bp, dirname, dirname_len - 1);
630 lastdir_depth = count_dir_elements(lastdir);
631 file->dir.depth = lastdir_depth + 1;
632 } else if (dirname) {
633 file->dirname = dirname; /* we're reusing lastname */
634 file->dir.depth = lastdir_depth + 1;
639 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
641 if (flags & XMIT_TOP_DIR) {
642 in_del_hier = recurse;
643 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
644 if (relative_paths && del_hier_name_len > 2
645 && basename_len == 1+1 && *basename == '.')
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 && thisname[del_hier_name_len] == '/'))
652 file->flags |= FLAG_DEL_HERE;
659 memcpy(bp, basename, basename_len);
662 if (preserve_devices && IS_DEVICE(mode))
668 read_sbuf(f, bp, linkname_len - 1);
670 sanitize_path(bp, bp, "", lastdir_depth);
675 #ifdef SUPPORT_HARD_LINKS
676 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
677 flags |= XMIT_HAS_IDEV_DATA;
678 if (flags & XMIT_HAS_IDEV_DATA) {
680 if (protocol_version < 26) {
684 if (!(flags & XMIT_SAME_DEV))
685 dev = read_longint(f);
686 inode = read_longint(f);
688 if (flist->hlink_pool) {
689 file->link_u.idev = pool_talloc(flist->hlink_pool,
690 struct idev, 1, "inode_table");
691 file->F_INODE = inode;
697 if (always_checksum && (sum_len || protocol_version < 28)) {
699 int slen = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
701 file->u.sum = sum = bp;
704 /* Prior to 28, we get a useless set of nulls. */
707 read_buf(f, sum, slen);
710 if (!preserve_perms) {
711 /* set an appropriate set of permissions based on original
712 * permissions and umask. This emulates what GNU cp does */
713 file->mode &= ~orig_umask;
720 * Create a file_struct for a named file by reading its stat()
721 * information and performing extensive checks against global
724 * @return the new file, or NULL if there was an error or this file
725 * should be excluded.
727 * @todo There is a small optimization opportunity here to avoid
728 * stat()ing the file in some circumstances, which has a certain cost.
729 * We are called immediately after doing readdir(), and so we may
730 * already know the d_type of the file. We could for example avoid
731 * statting directories if we're not recursing, but this is not a very
732 * important case. Some systems may not have d_type.
734 struct file_struct *make_file(char *fname, struct file_list *flist,
737 static char *lastdir;
738 static int lastdir_len = -1;
739 struct file_struct *file;
741 char sum[SUM_LENGTH];
742 char thisname[MAXPATHLEN];
743 char linkname[MAXPATHLEN];
744 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
745 char *basename, *dirname, *bp;
746 unsigned short flags = 0;
748 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
751 if (strlcpy(thisname, fname, sizeof thisname)
752 >= sizeof thisname - flist_dir_len) {
753 rprintf(FINFO, "skipping overly long name: %s\n",
757 clean_fname(thisname, 0);
759 sanitize_path(thisname, thisname, "", 0);
761 memset(sum, 0, SUM_LENGTH);
763 if (readlink_stat(thisname, &st, linkname) != 0) {
764 int save_errno = errno;
765 /* See if file is excluded before reporting an error. */
766 if (filter_level != NO_FILTERS
767 && is_excluded(thisname, 0, filter_level))
769 if (save_errno == ENOENT) {
771 /* Avoid "vanished" error if symlink points nowhere. */
772 if (copy_links && do_lstat(thisname, &st) == 0
773 && S_ISLNK(st.st_mode)) {
774 io_error |= IOERR_GENERAL;
775 rprintf(FERROR, "symlink has no referent: %s\n",
776 full_fname(thisname));
780 enum logcode c = am_daemon && protocol_version < 28
782 io_error |= IOERR_VANISHED;
783 rprintf(c, "file has vanished: %s\n",
784 full_fname(thisname));
787 io_error |= IOERR_GENERAL;
788 rsyserr(FERROR, save_errno, "readlink %s failed",
789 full_fname(thisname));
794 /* backup.c calls us with filter_level set to NO_FILTERS. */
795 if (filter_level == NO_FILTERS)
798 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
799 rprintf(FINFO, "skipping directory %s\n", safe_fname(thisname));
803 /* We only care about directories because we need to avoid recursing
804 * into a mount-point directory, not to avoid copying a symlinked
805 * file if -L (or similar) was specified. */
806 if (one_file_system && st.st_dev != filesystem_dev
807 && S_ISDIR(st.st_mode))
808 flags |= FLAG_MOUNT_POINT;
810 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
813 if (lp_ignore_nonreadable(module_id)) {
815 if (!S_ISLNK(st.st_mode))
817 if (access(thisname, R_OK) != 0)
824 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
825 who_am_i(), safe_fname(thisname), filter_level);
828 if ((basename = strrchr(thisname, '/')) != NULL) {
829 dirname_len = ++basename - thisname; /* counts future '\0' */
830 if (lastdir_len == dirname_len - 1
831 && strncmp(thisname, lastdir, lastdir_len) == 0) {
833 dirname_len = 0; /* indicates no copy is needed */
841 basename_len = strlen(basename) + 1; /* count the '\0' */
844 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
849 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
851 alloc_len = file_struct_len + dirname_len + basename_len
852 + linkname_len + sum_len;
854 bp = pool_alloc(flist->file_pool, alloc_len,
855 "receive_file_entry");
857 if (!(bp = new_array(char, alloc_len)))
858 out_of_memory("receive_file_entry");
861 file = (struct file_struct *)bp;
862 memset(bp, 0, file_struct_len);
863 bp += file_struct_len;
866 file->modtime = st.st_mtime;
867 file->length = st.st_size;
868 file->mode = st.st_mode;
869 file->uid = st.st_uid;
870 file->gid = st.st_gid;
872 #ifdef SUPPORT_HARD_LINKS
873 if (flist && flist->hlink_pool) {
874 if (protocol_version < 28) {
875 if (S_ISREG(st.st_mode))
876 file->link_u.idev = pool_talloc(
877 flist->hlink_pool, struct idev, 1,
880 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
881 file->link_u.idev = pool_talloc(
882 flist->hlink_pool, struct idev, 1,
886 if (file->link_u.idev) {
887 file->F_DEV = st.st_dev;
888 file->F_INODE = st.st_ino;
893 file->dirname = lastdir = bp;
894 lastdir_len = dirname_len - 1;
895 memcpy(bp, dirname, dirname_len - 1);
899 file->dirname = dirname;
902 memcpy(bp, basename, basename_len);
905 #ifdef HAVE_STRUCT_STAT_ST_RDEV
906 if (preserve_devices && IS_DEVICE(st.st_mode))
907 file->u.rdev = st.st_rdev;
913 memcpy(bp, linkname, linkname_len);
920 file_checksum(thisname, bp, st.st_size);
924 file->dir.root = flist_dir;
926 /* This code is only used by the receiver when it is building
927 * a list of files for a delete pass. */
928 if (keep_dirlinks && linkname_len && flist) {
930 int save_mode = file->mode;
931 file->mode = S_IFDIR; /* find a directory w/our name */
932 if (flist_find(the_file_list, file) >= 0
933 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
934 file->modtime = st2.st_mtime;
935 file->length = st2.st_size;
936 file->mode = st2.st_mode;
937 file->uid = st2.st_uid;
938 file->gid = st2.st_gid;
941 file->mode = save_mode;
944 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
945 stats.total_size += st.st_size;
950 static struct file_struct *send_file_name(int f, struct file_list *flist,
951 char *fname, unsigned short base_flags)
953 struct file_struct *file;
955 file = make_file(fname, flist, f == -2 ? SERVER_FILTERS : ALL_FILTERS);
959 maybe_emit_filelist_progress(flist->count + flist_count_offset);
963 if (file->basename[0]) {
964 flist->files[flist->count++] = file;
965 send_file_entry(file, f, base_flags);
970 static void send_if_directory(int f, struct file_list *flist,
971 struct file_struct *file)
973 char fbuf[MAXPATHLEN];
975 if (S_ISDIR(file->mode)
976 && !(file->flags & FLAG_MOUNT_POINT) && f_name_to(file, fbuf)) {
978 unsigned int len = strlen(fbuf);
979 if (len > 1 && fbuf[len-1] == '/')
981 if (len >= MAXPATHLEN - 1) {
982 io_error |= IOERR_GENERAL;
983 rprintf(FERROR, "skipping long-named directory: %s\n",
987 save_filters = push_local_filters(fbuf, len);
988 send_directory(f, flist, fbuf, len);
989 pop_local_filters(save_filters);
993 /* This function is normally called by the sender, but the receiving side also
994 * calls it from get_dirlist() with f set to -1 so that we just construct the
995 * file list in memory without sending it over the wire. Also, get_dirlist()
996 * might call this with f set to -2, which also indicates that local filter
997 * rules should be ignored. */
998 static void send_directory(int f, struct file_list *flist,
1005 int start = flist->count;
1007 if (!(d = opendir(fbuf))) {
1008 io_error |= IOERR_GENERAL;
1009 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1014 if (len != 1 || *fbuf != '/')
1017 remainder = MAXPATHLEN - (p - fbuf);
1019 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1020 char *dname = d_name(di);
1021 if (dname[0] == '.' && (dname[1] == '\0'
1022 || (dname[1] == '.' && dname[2] == '\0')))
1024 if (strlcpy(p, dname, remainder) < remainder)
1025 send_file_name(f, flist, fbuf, 0);
1027 io_error |= IOERR_GENERAL;
1029 "cannot send long-named file %s\n",
1037 io_error |= IOERR_GENERAL;
1038 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1044 int i, end = flist->count - 1;
1045 for (i = start; i <= end; i++)
1046 send_if_directory(f, flist, flist->files[i]);
1050 struct file_list *send_file_list(int f, int argc, char *argv[])
1054 char *p, *dir, olddir[sizeof curr_dir];
1055 char lastpath[MAXPATHLEN] = "";
1056 struct file_list *flist;
1057 struct timeval start_tv, end_tv;
1061 if (show_filelist_p())
1062 start_filelist_progress("building file list");
1064 start_write = stats.total_written;
1065 gettimeofday(&start_tv, NULL);
1067 flist = flist_new(WITH_HLINK, "send_file_list");
1069 io_start_buffering_out();
1070 if (filesfrom_fd >= 0) {
1071 if (argv[0] && !push_dir(argv[0])) {
1072 rsyserr(FERROR, errno, "push_dir %s failed",
1073 full_fname(argv[0]));
1074 exit_cleanup(RERR_FILESELECT);
1080 char fname2[MAXPATHLEN];
1081 char *fname = fname2;
1085 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1087 sanitize_path(fname, fname, "", 0);
1091 strlcpy(fname, *argv++, MAXPATHLEN);
1093 sanitize_path(fname, fname, "", 0);
1097 if (!l || fname[l - 1] == '/') {
1098 if (l == 2 && fname[0] == '.') {
1099 /* Turn "./" into just "." rather than "./." */
1102 if (l + 1 >= MAXPATHLEN)
1103 overflow_exit("send_file_list");
1108 } else if (l > 1 && fname[l-1] == '.' && fname[l-2] == '.'
1109 && (l == 2 || fname[l-3] == '/')) {
1110 if (l + 2 >= MAXPATHLEN)
1111 overflow_exit("send_file_list");
1117 is_dot_dir = fname[l-1] == '.'
1118 && (l == 1 || fname[l-2] == '/');
1121 if (link_stat(fname, &st, keep_dirlinks) != 0) {
1122 io_error |= IOERR_GENERAL;
1123 rsyserr(FERROR, errno, "link_stat %s failed",
1128 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1129 rprintf(FINFO, "skipping directory %s\n",
1137 if (!relative_paths) {
1138 p = strrchr(fname, '/');
1147 } else if ((p = strstr(fname, "/./")) != NULL) {
1160 static char *lastdir;
1161 static int lastdir_len;
1163 strlcpy(olddir, curr_dir, sizeof olddir);
1165 if (!push_dir(dir)) {
1166 io_error |= IOERR_GENERAL;
1167 rsyserr(FERROR, errno, "push_dir %s failed",
1172 if (lastdir && strcmp(lastdir, dir) == 0) {
1173 flist_dir = lastdir;
1174 flist_dir_len = lastdir_len;
1176 flist_dir = lastdir = strdup(dir);
1177 flist_dir_len = lastdir_len = strlen(dir);
1181 if (implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1182 /* Send the implied directories at the start of the
1183 * source spec, so we get their permissions right. */
1184 char *lp = lastpath, *fn = fname, *slash = fname;
1186 /* Skip any initial directories in our path that we
1187 * have in common with lastpath. */
1188 while (*fn && *lp == *fn) {
1194 if (fn != p || (*lp && *lp != '/')) {
1195 int save_copy_links = copy_links;
1196 int save_xfer_dirs = xfer_dirs;
1197 copy_links = copy_unsafe_links;
1199 while ((slash = strchr(slash+1, '/')) != 0) {
1201 send_file_name(f, flist, fname, 0);
1204 copy_links = save_copy_links;
1205 xfer_dirs = save_xfer_dirs;
1207 strlcpy(lastpath, fname, sizeof lastpath);
1212 if (one_file_system)
1213 filesystem_dev = st.st_dev;
1215 if (recurse || (xfer_dirs && is_dot_dir)) {
1216 struct file_struct *file;
1217 if ((file = send_file_name(f, flist, fname, XMIT_TOP_DIR)))
1218 send_if_directory(f, flist, file);
1220 send_file_name(f, flist, fname, 0);
1225 if (!pop_dir(olddir)) {
1226 rsyserr(FERROR, errno, "pop_dir %s failed",
1227 full_fname(olddir));
1228 exit_cleanup(RERR_FILESELECT);
1233 gettimeofday(&end_tv, NULL);
1234 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1235 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1236 if (stats.flist_buildtime == 0)
1237 stats.flist_buildtime = 1;
1240 send_file_entry(NULL, f, 0);
1242 if (show_filelist_p())
1243 finish_filelist_progress(flist);
1245 gettimeofday(&end_tv, NULL);
1246 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1247 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1249 if (flist->hlink_pool) {
1250 pool_destroy(flist->hlink_pool);
1251 flist->hlink_pool = NULL;
1254 /* Sort the list without removing any duplicates. This allows the
1255 * receiving side to ask for any name they like, which gives us the
1256 * flexibility to change the way we unduplicate names in the future
1257 * without causing a compatibility problem with older versions. */
1258 clean_flist(flist, 0, 0);
1260 /* Now send the uid/gid list. This was introduced in
1261 * protocol version 15 */
1264 /* send the io_error flag */
1265 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1268 stats.flist_size = stats.total_written - start_write;
1269 stats.num_files = flist->count;
1272 output_flist(flist);
1275 rprintf(FINFO, "send_file_list done\n");
1280 struct file_list *recv_file_list(int f)
1282 struct file_list *flist;
1283 unsigned short flags;
1286 if (show_filelist_p())
1287 start_filelist_progress("receiving file list");
1289 start_read = stats.total_read;
1291 flist = flist_new(WITH_HLINK, "recv_file_list");
1294 flist->malloced = 1000;
1295 flist->files = new_array(struct file_struct *, flist->malloced);
1300 while ((flags = read_byte(f)) != 0) {
1301 struct file_struct *file;
1303 flist_expand(flist);
1305 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1306 flags |= read_byte(f) << 8;
1307 file = receive_file_entry(flist, flags, f);
1309 if (S_ISREG(file->mode))
1310 stats.total_size += file->length;
1312 flist->files[flist->count++] = file;
1314 maybe_emit_filelist_progress(flist->count);
1317 rprintf(FINFO, "recv_file_name(%s)\n",
1318 safe_fname(f_name(file)));
1321 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1324 rprintf(FINFO, "received %d names\n", flist->count);
1326 if (show_filelist_p())
1327 finish_filelist_progress(flist);
1329 clean_flist(flist, relative_paths, 1);
1332 /* Now send the uid/gid list. This was introduced in
1333 * protocol version 15 */
1334 recv_uid_list(f, flist);
1336 /* Recv the io_error flag */
1337 if (lp_ignore_errors(module_id) || ignore_errors)
1340 io_error |= read_int(f);
1344 output_flist(flist);
1348 for (i = 0; i < flist->count; i++)
1349 list_file_entry(flist->files[i]);
1353 rprintf(FINFO, "recv_file_list done\n");
1355 stats.flist_size = stats.total_read - start_read;
1356 stats.num_files = flist->count;
1361 out_of_memory("recv_file_list");
1362 return NULL; /* not reached */
1365 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1367 return f_name_cmp(*file1, *file2);
1370 /* Search for an identically-named item in the file list. Note that the
1371 * items must agree in their directory-ness, or no match is returned. */
1372 int flist_find(struct file_list *flist, struct file_struct *f)
1374 int low = flist->low, high = flist->high;
1375 int ret, mid, mid_up;
1377 while (low <= high) {
1378 mid = (low + high) / 2;
1379 for (mid_up = mid; !flist->files[mid_up]->basename; mid_up++) {}
1381 ret = f_name_cmp(flist->files[mid_up], f);
1385 if (protocol_version < 29
1386 && S_ISDIR(flist->files[mid_up]->mode)
1387 != S_ISDIR(f->mode))
1400 * Free up any resources a file_struct has allocated
1401 * and clear the file.
1403 void clear_file(int i, struct file_list *flist)
1405 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1406 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1407 memset(flist->files[i], 0, file_struct_len);
1411 * allocate a new file list
1413 struct file_list *flist_new(int with_hlink, char *msg)
1415 struct file_list *flist;
1417 flist = new(struct file_list);
1421 memset(flist, 0, sizeof (struct file_list));
1423 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1424 out_of_memory, POOL_INTERN)))
1427 #ifdef SUPPORT_HARD_LINKS
1428 if (with_hlink && preserve_hard_links) {
1429 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1430 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1439 * free up all elements in a flist
1441 void flist_free(struct file_list *flist)
1443 pool_destroy(flist->file_pool);
1444 pool_destroy(flist->hlink_pool);
1450 * This routine ensures we don't have any duplicate names in our file list.
1451 * duplicate names can cause corruption because of the pipelining
1453 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1459 if (flist->count == 0) {
1464 sorting_flist = flist;
1465 qsort(flist->files, flist->count,
1466 sizeof flist->files[0], (int (*)())file_compare);
1467 sorting_flist = NULL;
1469 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1470 if (flist->files[i]->basename) {
1475 flist->low = prev_i;
1476 while (++i < flist->count) {
1478 struct file_struct *file = flist->files[i];
1480 if (!file->basename)
1482 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1484 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1485 int save_mode = file->mode;
1486 /* Make sure that this directory doesn't duplicate a
1487 * non-directory earlier in the list. */
1488 flist->high = prev_i;
1489 file->mode = S_IFREG;
1490 j = flist_find(flist, file);
1491 file->mode = save_mode;
1495 struct file_struct *fp = flist->files[j];
1497 /* If one is a dir and the other is not, we want to
1498 * keep the dir because it might have contents in the
1500 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1501 if (S_ISDIR(file->mode))
1507 if (verbose > 1 && !am_server) {
1509 "removing duplicate name %s from file list (%d)\n",
1510 safe_fname(f_name(file)), drop);
1512 /* Make sure that if we unduplicate '.', that we don't
1513 * lose track of a user-specified top directory. */
1514 flist->files[keep]->flags |= flist->files[drop]->flags
1515 & (FLAG_TOP_DIR|FLAG_DEL_HERE);
1517 clear_file(drop, flist);
1520 if (flist->low == drop) {
1522 j < i && !flist->files[j]->basename;
1531 flist->high = no_dups ? prev_i : flist->count - 1;
1534 /* We need to strip off the leading slashes for relative
1535 * paths, but this must be done _after_ the sorting phase. */
1536 for (i = flist->low; i <= flist->high; i++) {
1537 struct file_struct *file = flist->files[i];
1541 if (*file->dirname == '/') {
1542 char *s = file->dirname + 1;
1543 while (*s == '/') s++;
1544 memmove(file->dirname, s, strlen(s) + 1);
1547 if (!*file->dirname)
1548 file->dirname = NULL;
1553 static void output_flist(struct file_list *flist)
1555 char uidbuf[16], gidbuf[16], depthbuf[16];
1556 struct file_struct *file;
1557 const char *who = who_am_i();
1560 for (i = 0; i < flist->count; i++) {
1561 file = flist->files[i];
1562 if ((am_root || am_sender) && preserve_uid)
1563 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1566 if (preserve_gid && file->gid != GID_NONE)
1567 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1571 sprintf(depthbuf, "%d", file->dir.depth);
1572 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1573 who, i, am_sender ? NS(file->dir.root) : depthbuf,
1574 file->dirname ? safe_fname(file->dirname) : "",
1575 file->dirname ? "/" : "", NS(file->basename),
1576 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
1577 (double)file->length, uidbuf, gidbuf, file->flags);
1581 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1582 enum fnc_type { t_PATH, t_ITEM };
1584 /* Compare the names of two file_struct entities, similar to how strcmp()
1585 * would do if it were operating on the joined strings.
1587 * Some differences beginning with protocol_version 29: (1) directory names
1588 * are compared with an assumed trailing slash so that they compare in a
1589 * way that would cause them to sort immediately prior to any content they
1590 * may have; (2) a directory of any name compares after a non-directory of
1591 * any name at the same depth; (3) a directory with name "." compares prior
1592 * to anything else. These changes mean that a directory and a non-dir
1593 * with the same name will not compare as equal (protocol_version >= 29).
1595 * The dirname component can be an empty string, but the basename component
1596 * cannot (and never is in the current codebase). The basename component
1597 * may be NULL (for a removed item), in which case it is considered to be
1598 * after any existing item. */
1599 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1602 const uchar *c1, *c2;
1603 enum fnc_state state1, state2;
1604 enum fnc_type type1, type2;
1605 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1607 if (!f1 || !f1->basename) {
1608 if (!f2 || !f2->basename)
1612 if (!f2 || !f2->basename)
1615 c1 = (uchar*)f1->dirname;
1616 c2 = (uchar*)f2->dirname;
1620 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1621 c1 = (uchar*)f1->basename;
1622 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1624 state1 = s_TRAILING;
1637 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1638 c2 = (uchar*)f2->basename;
1639 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1641 state2 = s_TRAILING;
1655 return type1 == t_PATH ? 1 : -1;
1658 if ((dif = (int)*c1++ - (int)*c2++) != 0)
1667 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1668 c1 = (uchar*)f1->basename;
1669 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1671 state1 = s_TRAILING;
1677 state1 = s_TRAILING;
1678 if (type1 == t_PATH) {
1687 if (*c2 && type1 != type2)
1688 return type1 == t_PATH ? 1 : -1;
1697 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1698 c2 = (uchar*)f2->basename;
1699 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1701 state2 = s_TRAILING;
1707 state2 = s_TRAILING;
1708 if (type2 == t_PATH) {
1720 return type1 == t_PATH ? 1 : -1;
1727 /* Return a copy of the full filename of a flist entry, using the indicated
1728 * buffer. No size-checking is done because we checked the size when creating
1729 * the file_struct entry.
1731 char *f_name_to(struct file_struct *f, char *fbuf)
1733 if (!f || !f->basename)
1737 int len = strlen(f->dirname);
1738 memcpy(fbuf, f->dirname, len);
1740 strcpy(fbuf + len + 1, f->basename);
1742 strcpy(fbuf, f->basename);
1746 /* Like f_name_to(), but we rotate through 5 static buffers of our own. */
1747 char *f_name(struct file_struct *f)
1749 static char names[5][MAXPATHLEN];
1750 static unsigned int n;
1752 n = (n + 1) % (sizeof names / sizeof names[0]);
1754 return f_name_to(f, names[n]);
1757 /* Do a non-recursive scan of the named directory, possibly ignoring all
1758 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1759 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1760 * buffer (the functions we call will append names onto the end, but the old
1761 * dir value will be restored on exit). */
1762 struct file_list *get_dirlist(char *dirname, int dlen,
1763 int ignore_filter_rules)
1765 struct file_list *dirlist;
1766 char dirbuf[MAXPATHLEN];
1767 int save_recurse = recurse;
1770 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1771 if (dlen >= MAXPATHLEN)
1776 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1779 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
1780 recurse = save_recurse;
1782 flist_count_offset += dirlist->count;
1784 clean_flist(dirlist, 0, 0);
1787 output_flist(dirlist);