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 prune_empty_dirs;
57 extern int copy_links;
58 extern int copy_unsafe_links;
59 extern int protocol_version;
60 extern int sanitize_paths;
61 extern int orig_umask;
62 extern struct stats stats;
63 extern struct file_list *the_file_list;
65 extern char curr_dir[MAXPATHLEN];
67 extern struct chmod_mode_struct *chmod_modes;
69 extern struct filter_list_struct filter_list;
70 extern struct filter_list_struct server_filter_list;
74 dev_t filesystem_dev; /* used to implement -x */
75 unsigned int file_struct_len;
77 static char empty_sum[MD4_SUM_LENGTH];
78 static int flist_count_offset;
80 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
81 static void output_flist(struct file_list *flist);
87 /* Figure out how big the file_struct is without trailing padding */
88 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
89 checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
92 static int show_filelist_p(void)
94 return verbose && xfer_dirs && !am_server;
97 static void start_filelist_progress(char *kind)
99 rprintf(FINFO, "%s ... ", kind);
100 if (verbose > 1 || do_progress)
101 rprintf(FINFO, "\n");
105 static void emit_filelist_progress(int count)
107 rprintf(FINFO, " %d files...\r", count);
110 static void maybe_emit_filelist_progress(int count)
112 if (do_progress && show_filelist_p() && (count % 100) == 0)
113 emit_filelist_progress(count);
116 static void finish_filelist_progress(const struct file_list *flist)
119 /* This overwrites the progress line */
120 rprintf(FINFO, "%d file%sto consider\n",
121 flist->count, flist->count == 1 ? " " : "s ");
123 rprintf(FINFO, "done\n");
126 void show_flist_stats(void)
131 static void list_file_entry(struct file_struct *f)
133 char permbuf[PERMSTRING_SIZE];
136 /* this can happen if duplicate names were removed */
140 permstring(permbuf, f->mode);
143 if (preserve_links && S_ISLNK(f->mode)) {
144 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
146 (double)f->length, timestring(f->modtime),
147 f_name(f, NULL), f->u.link);
151 rprintf(FINFO, "%s %11.0f %s %s\n",
153 (double)f->length, timestring(f->modtime),
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",
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;
263 static void send_directory(int f, struct file_list *flist,
264 char *fbuf, int len);
266 static char *flist_dir;
267 static int flist_dir_len;
271 * Make sure @p flist is big enough to hold at least @p flist->count
274 void flist_expand(struct file_list *flist)
276 struct file_struct **new_ptr;
278 if (flist->count < flist->malloced)
281 if (flist->malloced < FLIST_START)
282 flist->malloced = FLIST_START;
283 else if (flist->malloced >= FLIST_LINEAR)
284 flist->malloced += FLIST_LINEAR;
286 flist->malloced *= 2;
289 * In case count jumped or we are starting the list
290 * with a known size just set it.
292 if (flist->malloced < flist->count)
293 flist->malloced = flist->count;
295 new_ptr = realloc_array(flist->files, struct file_struct *,
298 if (verbose >= 2 && flist->malloced != FLIST_START) {
299 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
301 (double)sizeof flist->files[0] * flist->malloced,
302 (new_ptr == flist->files) ? " not" : "");
305 flist->files = new_ptr;
308 out_of_memory("flist_expand");
311 static void send_file_entry(struct file_struct *file, int f)
313 unsigned short flags;
314 static time_t modtime;
318 static uint32 rdev_major;
321 static char lastname[MAXPATHLEN];
322 char fname[MAXPATHLEN];
330 modtime = 0, mode = 0;
331 dev = 0, rdev = makedev(0, 0);
338 io_write_phase = "send_file_entry";
342 flags = file->flags & XMIT_TOP_DIR;
344 if (file->mode == mode)
345 flags |= XMIT_SAME_MODE;
348 if ((preserve_devices && IS_DEVICE(mode))
349 || (preserve_specials && IS_SPECIAL(mode))) {
350 if (protocol_version < 28) {
351 if (file->u.rdev == rdev)
352 flags |= XMIT_SAME_RDEV_pre28;
357 if ((uint32)major(rdev) == rdev_major)
358 flags |= XMIT_SAME_RDEV_MAJOR;
360 rdev_major = major(rdev);
361 if ((uint32)minor(rdev) <= 0xFFu)
362 flags |= XMIT_RDEV_MINOR_IS_SMALL;
364 } else if (protocol_version < 28)
365 rdev = makedev(0, 0);
366 if (file->uid == uid)
367 flags |= XMIT_SAME_UID;
370 if (file->gid == gid)
371 flags |= XMIT_SAME_GID;
374 if (file->modtime == modtime)
375 flags |= XMIT_SAME_TIME;
377 modtime = file->modtime;
379 #ifdef SUPPORT_HARD_LINKS
380 if (file->link_u.idev) {
381 if (file->F_DEV == dev) {
382 if (protocol_version >= 28)
383 flags |= XMIT_SAME_DEV;
386 flags |= XMIT_HAS_IDEV_DATA;
391 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
393 l2 = strlen(fname+l1);
396 flags |= XMIT_SAME_NAME;
398 flags |= XMIT_LONG_NAME;
400 /* We must make sure we don't send a zero flag byte or the
401 * other end will terminate the flist transfer. Note that
402 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
403 * it's harmless way to add a bit to the first flag byte. */
404 if (protocol_version >= 28) {
405 if (!flags && !S_ISDIR(mode))
406 flags |= XMIT_TOP_DIR;
407 if ((flags & 0xFF00) || !flags) {
408 flags |= XMIT_EXTENDED_FLAGS;
409 write_byte(f, flags);
410 write_byte(f, flags >> 8);
412 write_byte(f, flags);
415 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
416 write_byte(f, flags);
418 if (flags & XMIT_SAME_NAME)
420 if (flags & XMIT_LONG_NAME)
424 write_buf(f, fname + l1, l2);
426 write_longint(f, file->length);
427 if (!(flags & XMIT_SAME_TIME))
428 write_int(f, modtime);
429 if (!(flags & XMIT_SAME_MODE))
430 write_int(f, to_wire_mode(mode));
431 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
436 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
441 if ((preserve_devices && IS_DEVICE(mode))
442 || (preserve_specials && IS_SPECIAL(mode))) {
443 if (protocol_version < 28) {
444 if (!(flags & XMIT_SAME_RDEV_pre28))
445 write_int(f, (int)rdev);
447 if (!(flags & XMIT_SAME_RDEV_MAJOR))
448 write_int(f, major(rdev));
449 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
450 write_byte(f, minor(rdev));
452 write_int(f, minor(rdev));
457 if (preserve_links && S_ISLNK(mode)) {
458 int len = strlen(file->u.link);
460 write_buf(f, file->u.link, len);
464 #ifdef SUPPORT_HARD_LINKS
465 if (flags & XMIT_HAS_IDEV_DATA) {
466 if (protocol_version < 26) {
467 /* 32-bit dev_t and ino_t */
469 write_int(f, file->F_INODE);
471 /* 64-bit dev_t and ino_t */
472 if (!(flags & XMIT_SAME_DEV))
473 write_longint(f, dev);
474 write_longint(f, file->F_INODE);
479 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
484 /* Prior to 28, we sent a useless set of nulls. */
487 write_buf(f, sum, checksum_len);
490 strlcpy(lastname, fname, MAXPATHLEN);
492 io_write_phase = "unknown";
495 static struct file_struct *receive_file_entry(struct file_list *flist,
496 unsigned short flags, int f)
498 static time_t modtime;
502 static uint32 rdev_major;
505 static char lastname[MAXPATHLEN], *lastdir;
506 static int lastdir_depth, lastdir_len = -1;
507 static unsigned int del_hier_name_len = 0;
508 static int in_del_hier = 0;
509 char thisname[MAXPATHLEN];
510 unsigned int l1 = 0, l2 = 0;
511 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
513 char *basename, *dirname, *bp;
514 struct file_struct *file;
517 modtime = 0, mode = 0;
518 dev = 0, rdev = makedev(0, 0);
527 if (flags & XMIT_SAME_NAME)
530 if (flags & XMIT_LONG_NAME)
535 if (l2 >= MAXPATHLEN - l1) {
537 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
538 flags, l1, l2, lastname);
539 overflow_exit("receive_file_entry");
542 strlcpy(thisname, lastname, l1 + 1);
543 read_sbuf(f, &thisname[l1], l2);
544 thisname[l1 + l2] = 0;
546 strlcpy(lastname, thisname, MAXPATHLEN);
548 clean_fname(thisname, 0);
551 sanitize_path(thisname, thisname, "", 0);
553 if ((basename = strrchr(thisname, '/')) != NULL) {
554 dirname_len = ++basename - thisname; /* counts future '\0' */
555 if (lastdir_len == dirname_len - 1
556 && strncmp(thisname, lastdir, lastdir_len) == 0) {
558 dirname_len = 0; /* indicates no copy is needed */
566 basename_len = strlen(basename) + 1; /* count the '\0' */
568 file_length = read_longint(f);
569 if (!(flags & XMIT_SAME_TIME))
570 modtime = (time_t)read_int(f);
571 if (!(flags & XMIT_SAME_MODE))
572 mode = from_wire_mode(read_int(f));
574 if (chmod_modes && !S_ISLNK(mode))
575 mode = tweak_mode(mode, chmod_modes);
577 if (preserve_uid && !(flags & XMIT_SAME_UID))
578 uid = (uid_t)read_int(f);
579 if (preserve_gid && !(flags & XMIT_SAME_GID))
580 gid = (gid_t)read_int(f);
582 if ((preserve_devices && IS_DEVICE(mode))
583 || (preserve_specials && IS_SPECIAL(mode))) {
584 if (protocol_version < 28) {
585 if (!(flags & XMIT_SAME_RDEV_pre28))
586 rdev = (dev_t)read_int(f);
589 if (!(flags & XMIT_SAME_RDEV_MAJOR))
590 rdev_major = read_int(f);
591 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
592 rdev_minor = read_byte(f);
594 rdev_minor = read_int(f);
595 rdev = makedev(rdev_major, rdev_minor);
597 } else if (protocol_version < 28)
598 rdev = makedev(0, 0);
601 if (preserve_links && S_ISLNK(mode)) {
602 linkname_len = read_int(f) + 1; /* count the '\0' */
603 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
604 rprintf(FERROR, "overflow: linkname_len=%d\n",
606 overflow_exit("receive_file_entry");
613 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
615 alloc_len = file_struct_len + dirname_len + basename_len
616 + linkname_len + sum_len;
617 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
619 file = (struct file_struct *)bp;
620 memset(bp, 0, file_struct_len);
621 bp += file_struct_len;
623 file->modtime = modtime;
624 file->length = file_length;
630 file->dirname = lastdir = bp;
631 lastdir_len = dirname_len - 1;
632 memcpy(bp, dirname, dirname_len - 1);
635 lastdir_depth = count_dir_elements(lastdir);
636 file->dir.depth = lastdir_depth + 1;
637 } else if (dirname) {
638 file->dirname = dirname; /* we're reusing lastname */
639 file->dir.depth = lastdir_depth + 1;
644 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
646 if (flags & XMIT_TOP_DIR) {
647 in_del_hier = recurse;
648 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
649 if (relative_paths && del_hier_name_len > 2
650 && lastname[del_hier_name_len-1] == '.'
651 && lastname[del_hier_name_len-2] == '/')
652 del_hier_name_len -= 2;
653 file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
654 } else if (in_del_hier) {
655 if (!relative_paths || !del_hier_name_len
656 || (l1 >= del_hier_name_len
657 && lastname[del_hier_name_len] == '/'))
658 file->flags |= FLAG_DEL_HERE;
665 memcpy(bp, basename, basename_len);
668 if ((preserve_devices && IS_DEVICE(mode))
669 || (preserve_specials && IS_SPECIAL(mode)))
675 read_sbuf(f, bp, linkname_len - 1);
677 sanitize_path(bp, bp, "", lastdir_depth);
682 #ifdef SUPPORT_HARD_LINKS
683 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
684 flags |= XMIT_HAS_IDEV_DATA;
685 if (flags & XMIT_HAS_IDEV_DATA) {
687 if (protocol_version < 26) {
691 if (!(flags & XMIT_SAME_DEV))
692 dev = read_longint(f);
693 inode = read_longint(f);
695 if (flist->hlink_pool) {
696 file->link_u.idev = pool_talloc(flist->hlink_pool,
697 struct idev, 1, "inode_table");
698 file->F_INODE = inode;
704 if (always_checksum && (sum_len || protocol_version < 28)) {
707 file->u.sum = sum = bp;
710 /* Prior to 28, we get a useless set of nulls. */
713 read_buf(f, sum, checksum_len);
716 if (!preserve_perms) {
717 /* set an appropriate set of permissions based on original
718 * permissions and umask. This emulates what GNU cp does */
719 file->mode &= ~orig_umask;
726 * Create a file_struct for a named file by reading its stat()
727 * information and performing extensive checks against global
730 * @return the new file, or NULL if there was an error or this file
731 * should be excluded.
733 * @todo There is a small optimization opportunity here to avoid
734 * stat()ing the file in some circumstances, which has a certain cost.
735 * We are called immediately after doing readdir(), and so we may
736 * already know the d_type of the file. We could for example avoid
737 * statting directories if we're not recursing, but this is not a very
738 * important case. Some systems may not have d_type.
740 struct file_struct *make_file(char *fname, struct file_list *flist,
741 STRUCT_STAT *stp, unsigned short flags,
744 static char *lastdir;
745 static int lastdir_len = -1;
746 struct file_struct *file;
748 char sum[SUM_LENGTH];
749 char thisname[MAXPATHLEN];
750 char linkname[MAXPATHLEN];
751 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
752 char *basename, *dirname, *bp;
754 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
757 if (strlcpy(thisname, fname, sizeof thisname)
758 >= sizeof thisname - flist_dir_len) {
759 rprintf(FINFO, "skipping overly long name: %s\n", fname);
762 clean_fname(thisname, 0);
764 sanitize_path(thisname, thisname, "", 0);
766 memset(sum, 0, SUM_LENGTH);
768 if (stp && S_ISDIR(stp->st_mode))
769 st = *stp; /* Needed for "symlink/." with --relative. */
770 else if (readlink_stat(thisname, &st, linkname) != 0) {
771 int save_errno = errno;
772 /* See if file is excluded before reporting an error. */
773 if (filter_level != NO_FILTERS
774 && is_excluded(thisname, 0, filter_level))
776 if (save_errno == ENOENT) {
778 /* Avoid "vanished" error if symlink points nowhere. */
779 if (copy_links && do_lstat(thisname, &st) == 0
780 && S_ISLNK(st.st_mode)) {
781 io_error |= IOERR_GENERAL;
782 rprintf(FERROR, "symlink has no referent: %s\n",
783 full_fname(thisname));
787 enum logcode c = am_daemon && protocol_version < 28
789 io_error |= IOERR_VANISHED;
790 rprintf(c, "file has vanished: %s\n",
791 full_fname(thisname));
794 io_error |= IOERR_GENERAL;
795 rsyserr(FERROR, save_errno, "readlink %s failed",
796 full_fname(thisname));
801 /* backup.c calls us with filter_level set to NO_FILTERS. */
802 if (filter_level == NO_FILTERS)
805 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
806 rprintf(FINFO, "skipping directory %s\n", thisname);
810 /* We only care about directories because we need to avoid recursing
811 * into a mount-point directory, not to avoid copying a symlinked
812 * file if -L (or similar) was specified. */
813 if (one_file_system && st.st_dev != filesystem_dev
814 && S_ISDIR(st.st_mode)) {
815 if (one_file_system > 1) {
817 rprintf(FINFO, "skipping mount-point dir %s\n",
822 flags |= FLAG_MOUNT_POINT;
825 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
828 if (lp_ignore_nonreadable(module_id)) {
830 if (!S_ISLNK(st.st_mode))
832 if (access(thisname, R_OK) != 0)
839 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
840 who_am_i(), thisname, filter_level);
843 if ((basename = strrchr(thisname, '/')) != NULL) {
844 dirname_len = ++basename - thisname; /* counts future '\0' */
845 if (lastdir_len == dirname_len - 1
846 && strncmp(thisname, lastdir, lastdir_len) == 0) {
848 dirname_len = 0; /* indicates no copy is needed */
856 basename_len = strlen(basename) + 1; /* count the '\0' */
859 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
864 sum_len = always_checksum && am_sender && S_ISREG(st.st_mode)
865 ? MD4_SUM_LENGTH : 0;
867 alloc_len = file_struct_len + dirname_len + basename_len
868 + linkname_len + sum_len;
870 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
872 if (!(bp = new_array(char, alloc_len)))
873 out_of_memory("make_file");
876 file = (struct file_struct *)bp;
877 memset(bp, 0, file_struct_len);
878 bp += file_struct_len;
881 file->modtime = st.st_mtime;
882 file->length = st.st_size;
883 file->mode = st.st_mode;
884 file->uid = st.st_uid;
885 file->gid = st.st_gid;
887 #ifdef SUPPORT_HARD_LINKS
888 if (flist && flist->hlink_pool) {
889 if (protocol_version < 28) {
890 if (S_ISREG(st.st_mode))
891 file->link_u.idev = pool_talloc(
892 flist->hlink_pool, struct idev, 1,
895 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
896 file->link_u.idev = pool_talloc(
897 flist->hlink_pool, struct idev, 1,
901 if (file->link_u.idev) {
902 file->F_DEV = st.st_dev;
903 file->F_INODE = st.st_ino;
908 file->dirname = lastdir = bp;
909 lastdir_len = dirname_len - 1;
910 memcpy(bp, dirname, dirname_len - 1);
914 file->dirname = dirname;
917 memcpy(bp, basename, basename_len);
920 #ifdef HAVE_STRUCT_STAT_ST_RDEV
921 if ((preserve_devices && IS_DEVICE(st.st_mode))
922 || (preserve_specials && IS_SPECIAL(st.st_mode)))
923 file->u.rdev = st.st_rdev;
929 memcpy(bp, linkname, linkname_len);
936 file_checksum(thisname, bp, st.st_size);
940 file->dir.root = flist_dir;
942 /* This code is only used by the receiver when it is building
943 * a list of files for a delete pass. */
944 if (keep_dirlinks && linkname_len && flist) {
946 int save_mode = file->mode;
947 file->mode = S_IFDIR; /* Find a directory with our name. */
948 if (flist_find(the_file_list, file) >= 0
949 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
950 file->modtime = st2.st_mtime;
951 file->length = st2.st_size;
952 file->mode = st2.st_mode;
953 file->uid = st2.st_uid;
954 file->gid = st2.st_gid;
957 file->mode = save_mode;
960 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
961 stats.total_size += st.st_size;
966 static struct file_struct *send_file_name(int f, struct file_list *flist,
967 char *fname, STRUCT_STAT *stp,
968 unsigned short flags)
970 struct file_struct *file;
972 file = make_file(fname, flist, stp, flags,
973 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
977 if (chmod_modes && !S_ISLNK(file->mode))
978 file->mode = tweak_mode(file->mode, chmod_modes);
980 maybe_emit_filelist_progress(flist->count + flist_count_offset);
984 if (file->basename[0]) {
985 flist->files[flist->count++] = file;
986 send_file_entry(file, f);
991 static void send_if_directory(int f, struct file_list *flist,
992 struct file_struct *file,
993 char *fbuf, unsigned int ol)
995 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
997 if (S_ISDIR(file->mode)
998 && !(file->flags & FLAG_MOUNT_POINT) && f_name(file, fbuf)) {
1000 unsigned int len = strlen(fbuf);
1001 if (len > 1 && fbuf[len-1] == '/')
1003 if (len >= MAXPATHLEN - 1) {
1004 io_error |= IOERR_GENERAL;
1005 rprintf(FERROR, "skipping long-named directory: %s\n",
1009 save_filters = push_local_filters(fbuf, len);
1010 send_directory(f, flist, fbuf, len);
1011 pop_local_filters(save_filters);
1018 /* This function is normally called by the sender, but the receiving side also
1019 * calls it from get_dirlist() with f set to -1 so that we just construct the
1020 * file list in memory without sending it over the wire. Also, get_dirlist()
1021 * might call this with f set to -2, which also indicates that local filter
1022 * rules should be ignored. */
1023 static void send_directory(int f, struct file_list *flist,
1024 char *fbuf, int len)
1030 int start = flist->count;
1032 if (!(d = opendir(fbuf))) {
1033 io_error |= IOERR_GENERAL;
1034 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1039 if (len != 1 || *fbuf != '/')
1042 remainder = MAXPATHLEN - (p - fbuf);
1044 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1045 char *dname = d_name(di);
1046 if (dname[0] == '.' && (dname[1] == '\0'
1047 || (dname[1] == '.' && dname[2] == '\0')))
1049 if (strlcpy(p, dname, remainder) >= remainder) {
1050 io_error |= IOERR_GENERAL;
1052 "cannot send long-named file %s\n",
1057 send_file_name(f, flist, fbuf, NULL, 0);
1063 io_error |= IOERR_GENERAL;
1064 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1070 int i, end = flist->count - 1;
1071 for (i = start; i <= end; i++)
1072 send_if_directory(f, flist, flist->files[i], fbuf, len);
1076 struct file_list *send_file_list(int f, int argc, char *argv[])
1080 char *p, *dir, olddir[sizeof curr_dir];
1081 char lastpath[MAXPATHLEN] = "";
1082 struct file_list *flist;
1083 struct timeval start_tv, end_tv;
1087 if (show_filelist_p())
1088 start_filelist_progress("building file list");
1090 start_write = stats.total_written;
1091 gettimeofday(&start_tv, NULL);
1093 flist = flist_new(WITH_HLINK, "send_file_list");
1095 io_start_buffering_out();
1096 if (filesfrom_fd >= 0) {
1097 if (argv[0] && !push_dir(argv[0])) {
1098 rsyserr(FERROR, errno, "push_dir %s failed",
1099 full_fname(argv[0]));
1100 exit_cleanup(RERR_FILESELECT);
1106 char fbuf[MAXPATHLEN];
1111 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
1113 sanitize_path(fbuf, fbuf, "", 0);
1117 strlcpy(fbuf, *argv++, MAXPATHLEN);
1119 sanitize_path(fbuf, fbuf, "", 0);
1123 if (relative_paths) {
1124 /* We clean up fbuf below. */
1126 } else if (!len || fbuf[len - 1] == '/') {
1127 if (len == 2 && fbuf[0] == '.') {
1128 /* Turn "./" into just "." rather than "./." */
1131 if (len + 1 >= MAXPATHLEN)
1132 overflow_exit("send_file_list");
1137 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1138 && (len == 2 || fbuf[len-3] == '/')) {
1139 if (len + 2 >= MAXPATHLEN)
1140 overflow_exit("send_file_list");
1146 is_dot_dir = fbuf[len-1] == '.'
1147 && (len == 1 || fbuf[len-2] == '/');
1150 if (link_stat(fbuf, &st, keep_dirlinks) != 0) {
1151 io_error |= IOERR_GENERAL;
1152 rsyserr(FERROR, errno, "link_stat %s failed",
1157 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1158 rprintf(FINFO, "skipping directory %s\n", fbuf);
1165 if (!relative_paths) {
1166 p = strrchr(fbuf, '/');
1173 len -= p - fbuf + 1;
1178 if ((p = strstr(fbuf, "/./")) != NULL) {
1184 len -= p - fbuf + 3;
1188 /* Get rid of trailing "/" and "/.". */
1190 if (fn[len - 1] == '/') {
1192 if (!--len && !dir) {
1197 else if (len >= 2 && fn[len - 1] == '.'
1198 && fn[len - 2] == '/') {
1200 if (!(len -= 2) && !dir) {
1208 /* Reject a ".." dir in the active part of the path. */
1209 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1210 if ((p[2] == '/' || p[2] == '\0')
1211 && (p == fn || p[-1] == '/')) {
1213 "found \"..\" dir in relative path: %s\n",
1215 exit_cleanup(RERR_SYNTAX);
1226 static char *lastdir;
1227 static int lastdir_len;
1229 strlcpy(olddir, curr_dir, sizeof olddir);
1231 if (!push_dir(dir)) {
1232 io_error |= IOERR_GENERAL;
1233 rsyserr(FERROR, errno, "push_dir %s failed",
1238 if (lastdir && strcmp(lastdir, dir) == 0) {
1239 flist_dir = lastdir;
1240 flist_dir_len = lastdir_len;
1242 flist_dir = lastdir = strdup(dir);
1243 flist_dir_len = lastdir_len = strlen(dir);
1248 memmove(fbuf, fn, len + 1);
1250 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
1251 /* Send the implied directories at the start of the
1252 * source spec, so we get their permissions right. */
1253 char *lp = lastpath, *slash = fbuf;
1255 /* Skip any initial directories in our path that we
1256 * have in common with lastpath. */
1257 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
1262 if (fn != p || (*lp && *lp != '/')) {
1263 int save_copy_links = copy_links;
1264 int save_xfer_dirs = xfer_dirs;
1265 copy_links = copy_unsafe_links;
1267 while ((slash = strchr(slash+1, '/')) != 0) {
1269 send_file_name(f, flist, fbuf, NULL, 0);
1272 copy_links = save_copy_links;
1273 xfer_dirs = save_xfer_dirs;
1275 strlcpy(lastpath, fbuf, sizeof lastpath);
1280 if (one_file_system)
1281 filesystem_dev = st.st_dev;
1283 if (recurse || (xfer_dirs && is_dot_dir)) {
1284 struct file_struct *file;
1285 file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
1287 send_if_directory(f, flist, file, fbuf, len);
1289 send_file_name(f, flist, fbuf, &st, 0);
1294 if (!pop_dir(olddir)) {
1295 rsyserr(FERROR, errno, "pop_dir %s failed",
1296 full_fname(olddir));
1297 exit_cleanup(RERR_FILESELECT);
1302 gettimeofday(&end_tv, NULL);
1303 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1304 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1305 if (stats.flist_buildtime == 0)
1306 stats.flist_buildtime = 1;
1309 send_file_entry(NULL, f);
1311 if (show_filelist_p())
1312 finish_filelist_progress(flist);
1314 gettimeofday(&end_tv, NULL);
1315 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1316 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1318 if (flist->hlink_pool) {
1319 pool_destroy(flist->hlink_pool);
1320 flist->hlink_pool = NULL;
1323 /* Sort the list without removing any duplicates. This allows the
1324 * receiving side to ask for any name they like, which gives us the
1325 * flexibility to change the way we unduplicate names in the future
1326 * without causing a compatibility problem with older versions. */
1327 clean_flist(flist, 0, 0);
1331 /* send the io_error flag */
1332 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1335 stats.flist_size = stats.total_written - start_write;
1336 stats.num_files = flist->count;
1339 output_flist(flist);
1342 rprintf(FINFO, "send_file_list done\n");
1347 struct file_list *recv_file_list(int f)
1349 struct file_list *flist;
1350 unsigned short flags;
1353 if (show_filelist_p())
1354 start_filelist_progress("receiving file list");
1356 start_read = stats.total_read;
1358 flist = flist_new(WITH_HLINK, "recv_file_list");
1361 flist->malloced = 1000;
1362 flist->files = new_array(struct file_struct *, flist->malloced);
1366 while ((flags = read_byte(f)) != 0) {
1367 struct file_struct *file;
1369 flist_expand(flist);
1371 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1372 flags |= read_byte(f) << 8;
1373 file = receive_file_entry(flist, flags, f);
1375 if (S_ISREG(file->mode))
1376 stats.total_size += file->length;
1378 flist->files[flist->count++] = file;
1380 maybe_emit_filelist_progress(flist->count);
1383 rprintf(FINFO, "recv_file_name(%s)\n",
1384 f_name(file, NULL));
1387 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1390 rprintf(FINFO, "received %d names\n", flist->count);
1392 if (show_filelist_p())
1393 finish_filelist_progress(flist);
1395 clean_flist(flist, relative_paths, 1);
1398 recv_uid_list(f, flist);
1400 /* Recv the io_error flag */
1401 if (lp_ignore_errors(module_id) || ignore_errors)
1404 io_error |= read_int(f);
1408 output_flist(flist);
1412 for (i = 0; i < flist->count; i++)
1413 list_file_entry(flist->files[i]);
1417 rprintf(FINFO, "recv_file_list done\n");
1419 stats.flist_size = stats.total_read - start_read;
1420 stats.num_files = flist->count;
1425 out_of_memory("recv_file_list");
1426 return NULL; /* not reached */
1429 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1431 return f_name_cmp(*file1, *file2);
1434 /* Search for an identically-named item in the file list. Note that the
1435 * items must agree in their directory-ness, or no match is returned. */
1436 int flist_find(struct file_list *flist, struct file_struct *f)
1438 int low = flist->low, high = flist->high;
1439 int diff, mid, mid_up;
1441 while (low <= high) {
1442 mid = (low + high) / 2;
1443 if (flist->files[mid]->basename)
1446 /* Scan for the next non-empty entry using the cached
1447 * distance values. If the value isn't fully up-to-
1448 * date, update it. */
1449 mid_up = mid + flist->files[mid]->dir.depth;
1450 if (!flist->files[mid_up]->basename) {
1452 mid_up += flist->files[mid_up]->dir.depth;
1453 } while (!flist->files[mid_up]->basename);
1454 flist->files[mid]->dir.depth = mid_up - mid;
1456 if (mid_up > high) {
1457 /* If there's nothing left above us, set high to
1458 * a non-empty entry below us and continue. */
1459 high = mid - flist->files[mid]->length;
1460 if (!flist->files[high]->basename) {
1462 high -= flist->files[high]->length;
1463 } while (!flist->files[high]->basename);
1464 flist->files[mid]->length = mid - high;
1469 diff = f_name_cmp(flist->files[mid_up], f);
1471 if (protocol_version < 29
1472 && S_ISDIR(flist->files[mid_up]->mode)
1473 != S_ISDIR(f->mode))
1486 * Free up any resources a file_struct has allocated
1487 * and clear the file.
1489 void clear_file(struct file_struct *file, struct file_list *flist)
1491 if (flist->hlink_pool && file->link_u.idev)
1492 pool_free(flist->hlink_pool, 0, file->link_u.idev);
1493 memset(file, 0, file_struct_len);
1494 /* In an empty entry, dir.depth is an offset to the next non-empty
1495 * entry. Likewise for length in the opposite direction. We assume
1496 * that we're alone for now since flist_find() will collate adjacent
1497 * items for any entries that are encountered during the find. */
1498 file->length = file->dir.depth = 1;
1502 * allocate a new file list
1504 struct file_list *flist_new(int with_hlink, char *msg)
1506 struct file_list *flist;
1508 flist = new(struct file_list);
1512 memset(flist, 0, sizeof (struct file_list));
1514 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1515 out_of_memory, POOL_INTERN)))
1518 #ifdef SUPPORT_HARD_LINKS
1519 if (with_hlink && preserve_hard_links) {
1520 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1521 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1530 * free up all elements in a flist
1532 void flist_free(struct file_list *flist)
1534 pool_destroy(flist->file_pool);
1535 pool_destroy(flist->hlink_pool);
1541 * This routine ensures we don't have any duplicate names in our file list.
1542 * duplicate names can cause corruption because of the pipelining
1544 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1546 char fbuf[MAXPATHLEN];
1551 if (flist->count == 0) {
1556 qsort(flist->files, flist->count,
1557 sizeof flist->files[0], (int (*)())file_compare);
1559 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1560 if (flist->files[i]->basename) {
1565 flist->low = prev_i;
1566 while (++i < flist->count) {
1568 struct file_struct *file = flist->files[i];
1570 if (!file->basename)
1572 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1574 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1575 int save_mode = file->mode;
1576 /* Make sure that this directory doesn't duplicate a
1577 * non-directory earlier in the list. */
1578 flist->high = prev_i;
1579 file->mode = S_IFREG;
1580 j = flist_find(flist, file);
1581 file->mode = save_mode;
1585 struct file_struct *fp = flist->files[j];
1587 /* If one is a dir and the other is not, we want to
1588 * keep the dir because it might have contents in the
1590 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1591 if (S_ISDIR(file->mode))
1597 if (verbose > 1 && !am_server) {
1599 "removing duplicate name %s from file list (%d)\n",
1600 f_name(file, fbuf), drop);
1602 /* Make sure we don't lose track of a user-specified
1604 flist->files[keep]->flags |= flist->files[drop]->flags
1605 & (FLAG_TOP_DIR|FLAG_DEL_HERE);
1607 clear_file(flist->files[drop], flist);
1610 if (flist->low == drop) {
1612 j < i && !flist->files[j]->basename;
1621 flist->high = no_dups ? prev_i : flist->count - 1;
1624 /* We need to strip off the leading slashes for relative
1625 * paths, but this must be done _after_ the sorting phase. */
1626 for (i = flist->low; i <= flist->high; i++) {
1627 struct file_struct *file = flist->files[i];
1631 while (*file->dirname == '/')
1633 if (!*file->dirname)
1634 file->dirname = NULL;
1638 if (prune_empty_dirs && no_dups) {
1639 int j, prev_depth = 0;
1641 prev_i = 0; /* It's OK that this isn't really true. */
1643 for (i = flist->low; i <= flist->high; i++) {
1644 struct file_struct *fp, *file = flist->files[i];
1646 /* This temporarily abuses the dir.depth value for a
1647 * directory that is in a chain that might get pruned.
1648 * We restore the old value if it gets a reprieve. */
1649 if (S_ISDIR(file->mode) && file->dir.depth) {
1650 /* Dump empty dirs when coming back down. */
1651 for (j = prev_depth; j >= file->dir.depth; j--) {
1652 fp = flist->files[prev_i];
1653 if (fp->dir.depth >= 0)
1655 prev_i = -fp->dir.depth-1;
1656 clear_file(fp, flist);
1658 prev_depth = file->dir.depth;
1659 if (is_excluded(f_name(file, fbuf), 1,
1661 /* Keep dirs through this dir. */
1662 for (j = prev_depth-1; ; j--) {
1663 fp = flist->files[prev_i];
1664 if (fp->dir.depth >= 0)
1666 prev_i = -fp->dir.depth-1;
1670 file->dir.depth = -prev_i-1;
1673 /* Keep dirs through this non-dir. */
1674 for (j = prev_depth; ; j--) {
1675 fp = flist->files[prev_i];
1676 if (fp->dir.depth >= 0)
1678 prev_i = -fp->dir.depth-1;
1683 /* Dump empty all remaining empty dirs. */
1685 struct file_struct *fp = flist->files[prev_i];
1686 if (fp->dir.depth >= 0)
1688 prev_i = -fp->dir.depth-1;
1689 clear_file(fp, flist);
1692 for (i = flist->low; i <= flist->high; i++) {
1693 if (flist->files[i]->basename)
1697 for (i = flist->high; i >= flist->low; i--) {
1698 if (flist->files[i]->basename)
1705 static void output_flist(struct file_list *flist)
1707 char uidbuf[16], gidbuf[16], depthbuf[16];
1708 struct file_struct *file;
1709 const char *who = who_am_i();
1712 for (i = 0; i < flist->count; i++) {
1713 file = flist->files[i];
1714 if ((am_root || am_sender) && preserve_uid)
1715 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1718 if (preserve_gid && file->gid != GID_NONE)
1719 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1723 sprintf(depthbuf, "%d", file->dir.depth);
1724 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1725 who, i, am_sender ? NS(file->dir.root) : depthbuf,
1726 file->dirname ? file->dirname : "",
1727 file->dirname ? "/" : "", NS(file->basename),
1728 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
1729 (double)file->length, uidbuf, gidbuf, file->flags);
1733 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1734 enum fnc_type { t_PATH, t_ITEM };
1736 /* Compare the names of two file_struct entities, similar to how strcmp()
1737 * would do if it were operating on the joined strings.
1739 * Some differences beginning with protocol_version 29: (1) directory names
1740 * are compared with an assumed trailing slash so that they compare in a
1741 * way that would cause them to sort immediately prior to any content they
1742 * may have; (2) a directory of any name compares after a non-directory of
1743 * any name at the same depth; (3) a directory with name "." compares prior
1744 * to anything else. These changes mean that a directory and a non-dir
1745 * with the same name will not compare as equal (protocol_version >= 29).
1747 * The dirname component can be an empty string, but the basename component
1748 * cannot (and never is in the current codebase). The basename component
1749 * may be NULL (for a removed item), in which case it is considered to be
1750 * after any existing item. */
1751 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1754 const uchar *c1, *c2;
1755 enum fnc_state state1, state2;
1756 enum fnc_type type1, type2;
1757 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1759 if (!f1 || !f1->basename) {
1760 if (!f2 || !f2->basename)
1764 if (!f2 || !f2->basename)
1767 c1 = (uchar*)f1->dirname;
1768 c2 = (uchar*)f2->dirname;
1772 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1773 c1 = (uchar*)f1->basename;
1774 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1776 state1 = s_TRAILING;
1789 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1790 c2 = (uchar*)f2->basename;
1791 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1793 state2 = s_TRAILING;
1807 return type1 == t_PATH ? 1 : -1;
1810 if ((dif = (int)*c1++ - (int)*c2++) != 0)
1819 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1820 c1 = (uchar*)f1->basename;
1821 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1823 state1 = s_TRAILING;
1829 state1 = s_TRAILING;
1830 if (type1 == t_PATH) {
1839 if (*c2 && type1 != type2)
1840 return type1 == t_PATH ? 1 : -1;
1849 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1850 c2 = (uchar*)f2->basename;
1851 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1853 state2 = s_TRAILING;
1859 state2 = s_TRAILING;
1860 if (type2 == t_PATH) {
1872 return type1 == t_PATH ? 1 : -1;
1879 /* Return a copy of the full filename of a flist entry, using the indicated
1880 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
1881 * done because we checked the size when creating the file_struct entry.
1883 char *f_name(struct file_struct *f, char *fbuf)
1885 if (!f || !f->basename)
1889 static char names[5][MAXPATHLEN];
1890 static unsigned int n;
1892 n = (n + 1) % (sizeof names / sizeof names[0]);
1898 int len = strlen(f->dirname);
1899 memcpy(fbuf, f->dirname, len);
1901 strcpy(fbuf + len + 1, f->basename);
1903 strcpy(fbuf, f->basename);
1908 /* Do a non-recursive scan of the named directory, possibly ignoring all
1909 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1910 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1911 * buffer (the functions we call will append names onto the end, but the old
1912 * dir value will be restored on exit). */
1913 struct file_list *get_dirlist(char *dirname, int dlen,
1914 int ignore_filter_rules)
1916 struct file_list *dirlist;
1917 char dirbuf[MAXPATHLEN];
1918 int save_recurse = recurse;
1921 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1922 if (dlen >= MAXPATHLEN)
1927 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1930 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
1931 recurse = save_recurse;
1933 flist_count_offset += dirlist->count;
1935 clean_flist(dirlist, 0, 0);
1938 output_flist(dirlist);