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
30 extern struct stats stats;
33 extern int do_progress;
38 extern int always_checksum;
40 extern int ignore_errors;
41 extern int numeric_ids;
45 extern char curr_dir[MAXPATHLEN];
46 extern unsigned int curr_dir_len;
47 extern char *backup_dir;
48 extern char *backup_suffix;
49 extern int filesfrom_fd;
51 extern int one_file_system;
52 extern int keep_dirlinks;
53 extern int preserve_links;
54 extern int preserve_hard_links;
55 extern int preserve_perms;
56 extern int preserve_devices;
57 extern int preserve_uid;
58 extern int preserve_gid;
59 extern int relative_paths;
60 extern int implied_dirs;
61 extern int make_backups;
62 extern int backup_suffix_len;
63 extern int copy_links;
64 extern int copy_unsafe_links;
65 extern int protocol_version;
66 extern int sanitize_paths;
67 extern int max_delete;
68 extern int orig_umask;
70 extern char *log_format;
72 extern struct filter_list_struct filter_list;
73 extern struct filter_list_struct server_filter_list;
77 static char empty_sum[MD4_SUM_LENGTH];
78 static unsigned int file_struct_len;
79 static struct file_list *received_flist, *sorting_flist;
80 static dev_t filesystem_dev; /* used to implement -x */
81 static int deletion_count = 0; /* used to implement --max-delete */
83 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
84 static void output_flist(struct file_list *flist, const char *whose_list);
90 /* Figure out how big the file_struct is without trailing padding */
91 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
95 static int show_filelist_p(void)
97 return verbose && xfer_dirs && !am_server;
100 static void start_filelist_progress(char *kind)
102 rprintf(FINFO, "%s ... ", kind);
103 if (verbose > 1 || do_progress)
104 rprintf(FINFO, "\n");
109 static void emit_filelist_progress(const struct file_list *flist)
111 rprintf(FINFO, " %d files...\r", flist->count);
115 static void maybe_emit_filelist_progress(const struct file_list *flist)
117 if (do_progress && show_filelist_p() && (flist->count % 100) == 0)
118 emit_filelist_progress(flist);
122 static void finish_filelist_progress(const struct file_list *flist)
125 /* This overwrites the progress line */
126 rprintf(FINFO, "%d file%sto consider\n",
127 flist->count, flist->count == 1 ? " " : "s ");
129 rprintf(FINFO, "done\n");
132 void show_flist_stats(void)
138 static void list_file_entry(struct file_struct *f)
143 /* this can happen if duplicate names were removed */
147 permstring(perms, f->mode);
150 if (preserve_links && S_ISLNK(f->mode)) {
151 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
153 (double)f->length, timestring(f->modtime),
154 safe_fname(f_name(f)), safe_fname(f->u.link));
158 rprintf(FINFO, "%s %11.0f %s %s\n",
160 (double)f->length, timestring(f->modtime),
161 safe_fname(f_name(f)));
167 * Stat either a symlink or its referent, depending on the settings of
168 * copy_links, copy_unsafe_links, etc.
170 * @retval -1 on error
172 * @retval 0 for success
174 * @post If @p path is a symlink, then @p linkbuf (of size @c
175 * MAXPATHLEN) contains the symlink target.
177 * @post @p buffer contains information about the link or the
178 * referrent as appropriate, if they exist.
180 static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
184 return do_stat(path, buffer);
185 if (link_stat(path, buffer, 0) < 0)
187 if (S_ISLNK(buffer->st_mode)) {
188 int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1);
192 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
194 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
195 safe_fname(path), safe_fname(linkbuf));
197 return do_stat(path, buffer);
202 return do_stat(path, buffer);
206 int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks)
210 return do_stat(path, buffer);
211 if (do_lstat(path, buffer) < 0)
213 if (follow_dirlinks && S_ISLNK(buffer->st_mode)) {
215 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
220 return do_stat(path, buffer);
224 /* This function is used to check if a file should be included/excluded
225 * from the list of files based on its name and type etc. The value of
226 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
227 static int is_excluded(char *fname, int is_dir, int filter_level)
229 #if 0 /* This currently never happens, so avoid a useless compare. */
230 if (filter_level == NO_FILTERS)
234 /* never exclude '.', even if somebody does --exclude '*' */
235 if (fname[0] == '.' && !fname[1])
237 /* Handle the -R version of the '.' dir. */
238 if (fname[0] == '/') {
239 int len = strlen(fname);
240 if (fname[len-1] == '.' && fname[len-2] == '/')
244 if (server_filter_list.head
245 && check_filter(&server_filter_list, fname, is_dir) < 0)
247 if (filter_level != ALL_FILTERS)
250 && check_filter(&filter_list, fname, is_dir) < 0)
255 static int to_wire_mode(mode_t mode)
258 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
259 return (mode & ~(_S_IFMT)) | 0120000;
264 static mode_t from_wire_mode(int mode)
266 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
267 return (mode & ~(_S_IFMT)) | _S_IFLNK;
272 static void send_directory(int f, struct file_list *flist,
273 char *fbuf, unsigned int offset);
275 static char *flist_dir;
276 static int flist_dir_len;
280 * Make sure @p flist is big enough to hold at least @p flist->count
283 void flist_expand(struct file_list *flist)
285 struct file_struct **new_ptr;
287 if (flist->count < flist->malloced)
290 if (flist->malloced < FLIST_START)
291 flist->malloced = FLIST_START;
292 else if (flist->malloced >= FLIST_LINEAR)
293 flist->malloced += FLIST_LINEAR;
295 flist->malloced *= 2;
298 * In case count jumped or we are starting the list
299 * with a known size just set it.
301 if (flist->malloced < flist->count)
302 flist->malloced = flist->count;
304 new_ptr = realloc_array(flist->files, struct file_struct *,
307 if (verbose >= 2 && flist->malloced != FLIST_START) {
308 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
310 (double)sizeof flist->files[0] * flist->malloced,
311 (new_ptr == flist->files) ? " not" : "");
314 flist->files = new_ptr;
317 out_of_memory("flist_expand");
320 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
322 unsigned short flags;
323 static time_t modtime;
327 static uint32 rdev_major;
330 static char lastname[MAXPATHLEN];
331 char fname[MAXPATHLEN];
339 modtime = 0, mode = 0;
340 dev = 0, rdev = makedev(0, 0);
347 io_write_phase = "send_file_entry";
349 f_name_to(file, fname);
353 if (file->mode == mode)
354 flags |= XMIT_SAME_MODE;
357 if (preserve_devices) {
358 if (protocol_version < 28) {
359 if (IS_DEVICE(mode)) {
360 if (file->u.rdev == rdev)
361 flags |= XMIT_SAME_RDEV_pre28;
365 rdev = makedev(0, 0);
366 } else if (IS_DEVICE(mode)) {
368 if ((uint32)major(rdev) == rdev_major)
369 flags |= XMIT_SAME_RDEV_MAJOR;
371 rdev_major = major(rdev);
372 if ((uint32)minor(rdev) <= 0xFFu)
373 flags |= XMIT_RDEV_MINOR_IS_SMALL;
376 if (file->uid == uid)
377 flags |= XMIT_SAME_UID;
380 if (file->gid == gid)
381 flags |= XMIT_SAME_GID;
384 if (file->modtime == modtime)
385 flags |= XMIT_SAME_TIME;
387 modtime = file->modtime;
389 #ifdef SUPPORT_HARD_LINKS
390 if (file->link_u.idev) {
391 if (file->F_DEV == dev) {
392 if (protocol_version >= 28)
393 flags |= XMIT_SAME_DEV;
396 flags |= XMIT_HAS_IDEV_DATA;
401 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
403 l2 = strlen(fname+l1);
406 flags |= XMIT_SAME_NAME;
408 flags |= XMIT_LONG_NAME;
410 /* We must make sure we don't send a zero flag byte or the
411 * other end will terminate the flist transfer. Note that
412 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
413 * it's harmless way to add a bit to the first flag byte. */
414 if (protocol_version >= 28) {
415 if (!flags && !S_ISDIR(mode))
416 flags |= XMIT_TOP_DIR;
417 if ((flags & 0xFF00) || !flags) {
418 flags |= XMIT_EXTENDED_FLAGS;
419 write_byte(f, flags);
420 write_byte(f, flags >> 8);
422 write_byte(f, flags);
424 if (!(flags & 0xFF) && !S_ISDIR(mode))
425 flags |= XMIT_TOP_DIR;
427 flags |= XMIT_LONG_NAME;
428 write_byte(f, flags);
430 if (flags & XMIT_SAME_NAME)
432 if (flags & XMIT_LONG_NAME)
436 write_buf(f, fname + l1, l2);
438 write_longint(f, file->length);
439 if (!(flags & XMIT_SAME_TIME))
440 write_int(f, modtime);
441 if (!(flags & XMIT_SAME_MODE))
442 write_int(f, to_wire_mode(mode));
443 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
448 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
453 if (preserve_devices && IS_DEVICE(mode)) {
454 if (protocol_version < 28) {
455 if (!(flags & XMIT_SAME_RDEV_pre28))
456 write_int(f, (int)rdev);
458 if (!(flags & XMIT_SAME_RDEV_MAJOR))
459 write_int(f, major(rdev));
460 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
461 write_byte(f, minor(rdev));
463 write_int(f, minor(rdev));
468 if (preserve_links && S_ISLNK(mode)) {
469 int len = strlen(file->u.link);
471 write_buf(f, file->u.link, len);
475 #ifdef SUPPORT_HARD_LINKS
476 if (flags & XMIT_HAS_IDEV_DATA) {
477 if (protocol_version < 26) {
478 /* 32-bit dev_t and ino_t */
480 write_int(f, file->F_INODE);
482 /* 64-bit dev_t and ino_t */
483 if (!(flags & XMIT_SAME_DEV))
484 write_longint(f, dev);
485 write_longint(f, file->F_INODE);
490 if (always_checksum) {
494 else if (protocol_version < 28) {
495 /* Prior to 28, we sent a useless set of nulls. */
501 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
505 strlcpy(lastname, fname, MAXPATHLEN);
507 io_write_phase = "unknown";
512 static struct file_struct *receive_file_entry(struct file_list *flist,
513 unsigned short flags, int f)
515 static time_t modtime;
519 static uint32 rdev_major;
522 static char lastname[MAXPATHLEN], *lastdir;
523 static int lastdir_depth, lastdir_len = -1;
524 static unsigned int del_hier_name_len = 0;
525 static int in_del_hier = 0;
526 char thisname[MAXPATHLEN];
527 unsigned int l1 = 0, l2 = 0;
528 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
530 char *basename, *dirname, *bp;
531 struct file_struct *file;
534 modtime = 0, mode = 0;
535 dev = 0, rdev = makedev(0, 0);
544 if (flags & XMIT_SAME_NAME)
547 if (flags & XMIT_LONG_NAME)
552 if (l2 >= MAXPATHLEN - l1) {
554 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
555 flags, l1, l2, safe_fname(lastname));
556 overflow("receive_file_entry");
559 strlcpy(thisname, lastname, l1 + 1);
560 read_sbuf(f, &thisname[l1], l2);
561 thisname[l1 + l2] = 0;
563 strlcpy(lastname, thisname, MAXPATHLEN);
565 clean_fname(thisname, 0);
568 sanitize_path(thisname, thisname, "", 0);
570 if ((basename = strrchr(thisname, '/')) != NULL) {
571 dirname_len = ++basename - thisname; /* counts future '\0' */
572 if (lastdir_len == dirname_len - 1
573 && strncmp(thisname, lastdir, lastdir_len) == 0) {
575 dirname_len = 0; /* indicates no copy is needed */
583 basename_len = strlen(basename) + 1; /* count the '\0' */
585 file_length = read_longint(f);
586 if (!(flags & XMIT_SAME_TIME))
587 modtime = (time_t)read_int(f);
588 if (!(flags & XMIT_SAME_MODE))
589 mode = from_wire_mode(read_int(f));
591 if (preserve_uid && !(flags & XMIT_SAME_UID))
592 uid = (uid_t)read_int(f);
593 if (preserve_gid && !(flags & XMIT_SAME_GID))
594 gid = (gid_t)read_int(f);
596 if (preserve_devices) {
597 if (protocol_version < 28) {
598 if (IS_DEVICE(mode)) {
599 if (!(flags & XMIT_SAME_RDEV_pre28))
600 rdev = (dev_t)read_int(f);
602 rdev = makedev(0, 0);
603 } else if (IS_DEVICE(mode)) {
605 if (!(flags & XMIT_SAME_RDEV_MAJOR))
606 rdev_major = read_int(f);
607 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
608 rdev_minor = read_byte(f);
610 rdev_minor = read_int(f);
611 rdev = makedev(rdev_major, rdev_minor);
616 if (preserve_links && S_ISLNK(mode)) {
617 linkname_len = read_int(f) + 1; /* count the '\0' */
618 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
619 rprintf(FERROR, "overflow: linkname_len=%d\n",
621 overflow("receive_file_entry");
628 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
630 alloc_len = file_struct_len + dirname_len + basename_len
631 + linkname_len + sum_len;
632 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
634 file = (struct file_struct *)bp;
635 memset(bp, 0, file_struct_len);
636 bp += file_struct_len;
639 file->modtime = modtime;
640 file->length = file_length;
646 file->dirname = lastdir = bp;
647 lastdir_len = dirname_len - 1;
648 memcpy(bp, dirname, dirname_len - 1);
651 lastdir_depth = count_dir_elements(lastdir);
652 file->dir.depth = lastdir_depth + 1;
653 } else if (dirname) {
654 file->dirname = dirname; /* we're reusing lastname */
655 file->dir.depth = lastdir_depth + 1;
660 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
662 if (flags & XMIT_TOP_DIR) {
664 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
665 file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
666 } else if (in_del_hier) {
667 if (!relative_paths || !del_hier_name_len
668 || (l1 >= del_hier_name_len
669 && thisname[del_hier_name_len] == '/'))
670 file->flags |= FLAG_DEL_HERE;
677 memcpy(bp, basename, basename_len);
680 if (preserve_devices && IS_DEVICE(mode))
686 read_sbuf(f, bp, linkname_len - 1);
688 sanitize_path(bp, bp, "", lastdir_depth);
693 #ifdef SUPPORT_HARD_LINKS
694 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
695 flags |= XMIT_HAS_IDEV_DATA;
696 if (flags & XMIT_HAS_IDEV_DATA) {
698 if (protocol_version < 26) {
702 if (!(flags & XMIT_SAME_DEV))
703 dev = read_longint(f);
704 inode = read_longint(f);
706 if (flist->hlink_pool) {
707 file->link_u.idev = pool_talloc(flist->hlink_pool,
708 struct idev, 1, "inode_table");
709 file->F_INODE = inode;
715 if (always_checksum) {
718 file->u.sum = sum = bp;
720 } else if (protocol_version < 28) {
721 /* Prior to 28, we get a useless set of nulls. */
727 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
731 if (!preserve_perms) {
732 /* set an appropriate set of permissions based on original
733 * permissions and umask. This emulates what GNU cp does */
734 file->mode &= ~orig_umask;
742 * Create a file_struct for a named file by reading its stat()
743 * information and performing extensive checks against global
746 * @return the new file, or NULL if there was an error or this file
747 * should be excluded.
749 * @todo There is a small optimization opportunity here to avoid
750 * stat()ing the file in some circumstances, which has a certain cost.
751 * We are called immediately after doing readdir(), and so we may
752 * already know the d_type of the file. We could for example avoid
753 * statting directories if we're not recursing, but this is not a very
754 * important case. Some systems may not have d_type.
756 struct file_struct *make_file(char *fname, struct file_list *flist,
759 static char *lastdir;
760 static int lastdir_len = -1;
761 struct file_struct *file;
763 char sum[SUM_LENGTH];
764 char thisname[MAXPATHLEN];
765 char linkname[MAXPATHLEN];
766 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
767 char *basename, *dirname, *bp;
768 unsigned short flags = 0;
770 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
773 if (strlcpy(thisname, fname, sizeof thisname)
774 >= sizeof thisname - flist_dir_len) {
775 rprintf(FINFO, "skipping overly long name: %s\n",
779 clean_fname(thisname, 0);
781 sanitize_path(thisname, thisname, "", 0);
783 memset(sum, 0, SUM_LENGTH);
785 if (readlink_stat(thisname, &st, linkname) != 0) {
786 int save_errno = errno;
787 /* See if file is excluded before reporting an error. */
788 if (filter_level != NO_FILTERS
789 && is_excluded(thisname, 0, filter_level))
791 if (save_errno == ENOENT) {
793 /* Avoid "vanished" error if symlink points nowhere. */
794 if (copy_links && do_lstat(thisname, &st) == 0
795 && S_ISLNK(st.st_mode)) {
796 io_error |= IOERR_GENERAL;
797 rprintf(FERROR, "symlink has no referent: %s\n",
798 full_fname(thisname));
802 enum logcode c = am_daemon && protocol_version < 28
804 io_error |= IOERR_VANISHED;
805 rprintf(c, "file has vanished: %s\n",
806 full_fname(thisname));
809 io_error |= IOERR_GENERAL;
810 rsyserr(FERROR, save_errno, "readlink %s failed",
811 full_fname(thisname));
816 /* backup.c calls us with filter_level set to NO_FILTERS. */
817 if (filter_level == NO_FILTERS)
820 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
821 rprintf(FINFO, "skipping directory %s\n", safe_fname(thisname));
825 /* We only care about directories because we need to avoid recursing
826 * into a mount-point directory, not to avoid copying a symlinked
827 * file if -L (or similar) was specified. */
828 if (one_file_system && st.st_dev != filesystem_dev
829 && S_ISDIR(st.st_mode))
830 flags |= FLAG_MOUNT_POINT;
832 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
835 if (lp_ignore_nonreadable(module_id)) {
837 if (!S_ISLNK(st.st_mode))
839 if (access(thisname, R_OK) != 0)
846 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
847 who_am_i(), safe_fname(thisname), filter_level);
850 if ((basename = strrchr(thisname, '/')) != NULL) {
851 dirname_len = ++basename - thisname; /* counts future '\0' */
852 if (lastdir_len == dirname_len - 1
853 && strncmp(thisname, lastdir, lastdir_len) == 0) {
855 dirname_len = 0; /* indicates no copy is needed */
863 basename_len = strlen(basename) + 1; /* count the '\0' */
866 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
871 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
873 alloc_len = file_struct_len + dirname_len + basename_len
874 + linkname_len + sum_len;
876 bp = pool_alloc(flist->file_pool, alloc_len,
877 "receive_file_entry");
879 if (!(bp = new_array(char, alloc_len)))
880 out_of_memory("receive_file_entry");
883 file = (struct file_struct *)bp;
884 memset(bp, 0, file_struct_len);
885 bp += file_struct_len;
888 file->modtime = st.st_mtime;
889 file->length = st.st_size;
890 file->mode = st.st_mode;
891 file->uid = st.st_uid;
892 file->gid = st.st_gid;
894 #ifdef SUPPORT_HARD_LINKS
895 if (flist && flist->hlink_pool) {
896 if (protocol_version < 28) {
897 if (S_ISREG(st.st_mode))
898 file->link_u.idev = pool_talloc(
899 flist->hlink_pool, struct idev, 1,
902 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
903 file->link_u.idev = pool_talloc(
904 flist->hlink_pool, struct idev, 1,
908 if (file->link_u.idev) {
909 file->F_DEV = st.st_dev;
910 file->F_INODE = st.st_ino;
915 file->dirname = lastdir = bp;
916 lastdir_len = dirname_len - 1;
917 memcpy(bp, dirname, dirname_len - 1);
921 file->dirname = dirname;
924 memcpy(bp, basename, basename_len);
927 #ifdef HAVE_STRUCT_STAT_ST_RDEV
928 if (preserve_devices && IS_DEVICE(st.st_mode))
929 file->u.rdev = st.st_rdev;
935 memcpy(bp, linkname, linkname_len);
942 file_checksum(thisname, bp, st.st_size);
946 file->dir.root = flist_dir;
948 /* This code is only used by the receiver when it is building
949 * a list of files for a delete pass. */
950 if (keep_dirlinks && linkname_len && flist) {
952 int save_mode = file->mode;
953 file->mode = S_IFDIR; /* find a directory w/our name */
954 if (flist_find(received_flist, file) >= 0
955 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
956 file->modtime = st2.st_mtime;
957 file->length = st2.st_size;
958 file->mode = st2.st_mode;
959 file->uid = st2.st_uid;
960 file->gid = st2.st_gid;
963 file->mode = save_mode;
966 if (!S_ISDIR(st.st_mode))
967 stats.total_size += st.st_size;
973 void send_file_name(int f, struct file_list *flist, char *fname,
974 int recursive, unsigned short base_flags)
976 struct file_struct *file;
977 char fbuf[MAXPATHLEN];
979 file = make_file(fname, flist, f == -2 ? SERVER_FILTERS : ALL_FILTERS);
983 maybe_emit_filelist_progress(flist);
987 if (file->basename[0]) {
988 flist->files[flist->count++] = file;
989 send_file_entry(file, f, base_flags);
992 if (recursive && S_ISDIR(file->mode)
993 && !(file->flags & FLAG_MOUNT_POINT) && f_name_to(file, fbuf)) {
995 unsigned int len = strlen(fbuf);
996 if (len > 1 && fbuf[len-1] == '/')
998 if (len >= MAXPATHLEN - 1) {
999 io_error |= IOERR_GENERAL;
1000 rprintf(FERROR, "skipping long-named directory: %s\n",
1004 save_filters = push_local_filters(fbuf, len);
1005 send_directory(f, flist, fbuf, len);
1006 pop_local_filters(save_filters);
1011 /* Note that the "recurse" value either contains -1, for infinite recursion,
1012 * or a number >= 0 indicating how many levels of recursion we will allow.
1013 * This function is normally called by the sender, but the receiving side
1014 * also calls it from delete_in_dir() with f set to -1 so that we just
1015 * construct the file list in memory without sending it over the wire. Also,
1016 * get_dirlist() calls this with f set to -2, which indicates that local
1017 * filter rules should be ignored. */
1018 static void send_directory(int f, struct file_list *flist,
1019 char *fbuf, unsigned int len)
1025 if (!(d = opendir(fbuf))) {
1026 io_error |= IOERR_GENERAL;
1027 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1032 if (len != 1 || *fbuf != '/')
1036 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1037 char *dname = d_name(di);
1038 if (dname[0] == '.' && (dname[1] == '\0'
1039 || (dname[1] == '.' && dname[2] == '\0')))
1041 if (strlcpy(p, dname, MAXPATHLEN - len) < MAXPATHLEN - len) {
1042 int do_subdirs = recurse >= 1 ? recurse-- : recurse;
1043 send_file_name(f, flist, fbuf, do_subdirs, 0);
1045 io_error |= IOERR_GENERAL;
1047 "cannot send long-named file %s\n",
1052 io_error |= IOERR_GENERAL;
1054 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1061 struct file_list *send_file_list(int f, int argc, char *argv[])
1065 char *p, *dir, olddir[sizeof curr_dir];
1066 char lastpath[MAXPATHLEN] = "";
1067 struct file_list *flist;
1068 struct timeval start_tv, end_tv;
1072 if (show_filelist_p())
1073 start_filelist_progress("building file list");
1075 start_write = stats.total_written;
1076 gettimeofday(&start_tv, NULL);
1078 flist = flist_new(WITH_HLINK, "send_file_list");
1080 io_start_buffering_out();
1081 if (filesfrom_fd >= 0) {
1082 if (argv[0] && !push_dir(argv[0])) {
1083 rsyserr(FERROR, errno, "push_dir %s failed",
1084 full_fname(argv[0]));
1085 exit_cleanup(RERR_FILESELECT);
1091 char fname2[MAXPATHLEN];
1092 char *fname = fname2;
1096 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1098 sanitize_path(fname, fname, "", 0);
1102 strlcpy(fname, *argv++, MAXPATHLEN);
1104 sanitize_path(fname, fname, "", 0);
1108 if (!l || fname[l - 1] == '/') {
1109 if (l == 2 && fname[0] == '.') {
1110 /* Turn "./" into just "." rather than "./." */
1112 } else if (l < MAXPATHLEN) {
1117 if (fname[l-1] == '.' && (l == 1 || fname[l-2] == '/')) {
1118 if (!recurse && xfer_dirs)
1119 recurse = 1; /* allow one level */
1120 } else if (recurse > 0)
1123 if (link_stat(fname, &st, keep_dirlinks) != 0) {
1124 io_error |= IOERR_GENERAL;
1125 rsyserr(FERROR, errno, "link_stat %s failed",
1130 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1131 rprintf(FINFO, "skipping directory %s\n",
1139 if (!relative_paths) {
1140 p = strrchr(fname, '/');
1149 } else if (implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1150 /* this ensures we send the intermediate directories,
1151 thus getting their permissions right */
1152 char *lp = lastpath, *fn = fname, *slash = fname;
1154 /* Skip any initial directories in our path that we
1155 * have in common with lastpath. */
1156 while (*fn && *lp == *fn) {
1162 if (fn != p || (*lp && *lp != '/')) {
1163 int save_copy_links = copy_links;
1164 int save_xfer_dirs = xfer_dirs;
1165 copy_links = copy_unsafe_links;
1167 while ((slash = strchr(slash+1, '/')) != 0) {
1169 send_file_name(f, flist, fname, 0, 0);
1172 copy_links = save_copy_links;
1173 xfer_dirs = save_xfer_dirs;
1175 strlcpy(lastpath, fname, sizeof lastpath);
1184 static char *lastdir;
1185 static int lastdir_len;
1187 strcpy(olddir, curr_dir); /* can't overflow */
1189 if (!push_dir(dir)) {
1190 io_error |= IOERR_GENERAL;
1191 rsyserr(FERROR, errno, "push_dir %s failed",
1196 if (lastdir && strcmp(lastdir, dir) == 0) {
1197 flist_dir = lastdir;
1198 flist_dir_len = lastdir_len;
1200 flist_dir = lastdir = strdup(dir);
1201 flist_dir_len = lastdir_len = strlen(dir);
1205 if (one_file_system)
1206 filesystem_dev = st.st_dev;
1208 do_subdirs = recurse >= 1 ? recurse-- : recurse;
1209 send_file_name(f, flist, fname, do_subdirs, XMIT_TOP_DIR);
1214 if (!pop_dir(olddir)) {
1215 rsyserr(FERROR, errno, "pop_dir %s failed",
1217 exit_cleanup(RERR_FILESELECT);
1222 gettimeofday(&end_tv, NULL);
1223 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1224 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1225 if (stats.flist_buildtime == 0)
1226 stats.flist_buildtime = 1;
1229 send_file_entry(NULL, f, 0);
1231 if (show_filelist_p())
1232 finish_filelist_progress(flist);
1234 gettimeofday(&end_tv, NULL);
1235 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1236 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1238 if (flist->hlink_pool) {
1239 pool_destroy(flist->hlink_pool);
1240 flist->hlink_pool = NULL;
1243 /* Sort the list without removing any duplicates. This allows the
1244 * receiving side to ask for any name they like, which gives us the
1245 * flexibility to change the way we unduplicate names in the future
1246 * without causing a compatibility problem with older versions. */
1247 clean_flist(flist, 0, 0);
1249 /* Now send the uid/gid list. This was introduced in
1250 * protocol version 15 */
1253 /* send the io_error flag */
1254 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1257 stats.flist_size = stats.total_written - start_write;
1258 stats.num_files = flist->count;
1261 output_flist(flist, who_am_i());
1264 rprintf(FINFO, "send_file_list done\n");
1270 struct file_list *recv_file_list(int f)
1272 struct file_list *flist;
1273 unsigned short flags;
1276 if (show_filelist_p())
1277 start_filelist_progress("receiving file list");
1279 start_read = stats.total_read;
1281 flist = flist_new(WITH_HLINK, "recv_file_list");
1282 received_flist = flist;
1285 flist->malloced = 1000;
1286 flist->files = new_array(struct file_struct *, flist->malloced);
1291 while ((flags = read_byte(f)) != 0) {
1292 struct file_struct *file;
1294 flist_expand(flist);
1296 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1297 flags |= read_byte(f) << 8;
1298 file = receive_file_entry(flist, flags, f);
1300 if (S_ISREG(file->mode))
1301 stats.total_size += file->length;
1303 flist->files[flist->count++] = file;
1305 maybe_emit_filelist_progress(flist);
1308 rprintf(FINFO, "recv_file_name(%s)\n",
1309 safe_fname(f_name(file)));
1312 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1315 rprintf(FINFO, "received %d names\n", flist->count);
1317 if (show_filelist_p())
1318 finish_filelist_progress(flist);
1320 clean_flist(flist, relative_paths, 1);
1323 /* Now send the uid/gid list. This was introduced in
1324 * protocol version 15 */
1325 recv_uid_list(f, flist);
1327 /* Recv the io_error flag */
1328 if (lp_ignore_errors(module_id) || ignore_errors)
1331 io_error |= read_int(f);
1335 output_flist(flist, who_am_i());
1339 for (i = 0; i < flist->count; i++)
1340 list_file_entry(flist->files[i]);
1344 rprintf(FINFO, "recv_file_list done\n");
1346 stats.flist_size = stats.total_read - start_read;
1347 stats.num_files = flist->count;
1352 out_of_memory("recv_file_list");
1353 return NULL; /* not reached */
1357 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1359 return f_name_cmp(*file1, *file2);
1363 /* Search for an identically-named item in the file list. Note that the
1364 * items must agree in their directory-ness, or no match is returned. */
1365 int flist_find(struct file_list *flist, struct file_struct *f)
1367 int low = flist->low, high = flist->high;
1368 int ret, mid, mid_up;
1370 while (low <= high) {
1371 mid = (low + high) / 2;
1372 for (mid_up = mid; !flist->files[mid_up]->basename; mid_up++) {}
1374 ret = f_name_cmp(flist->files[mid_up], f);
1378 if (protocol_version < 29
1379 && S_ISDIR(flist->files[mid_up]->mode)
1380 != S_ISDIR(f->mode))
1394 * Free up any resources a file_struct has allocated
1395 * and clear the file.
1397 void clear_file(int i, struct file_list *flist)
1399 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1400 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1401 memset(flist->files[i], 0, file_struct_len);
1406 * allocate a new file list
1408 struct file_list *flist_new(int with_hlink, char *msg)
1410 struct file_list *flist;
1412 flist = new(struct file_list);
1416 memset(flist, 0, sizeof (struct file_list));
1418 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1419 out_of_memory, POOL_INTERN)))
1422 #ifdef SUPPORT_HARD_LINKS
1423 if (with_hlink && preserve_hard_links) {
1424 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1425 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1434 * free up all elements in a flist
1436 void flist_free(struct file_list *flist)
1438 pool_destroy(flist->file_pool);
1439 pool_destroy(flist->hlink_pool);
1446 * This routine ensures we don't have any duplicate names in our file list.
1447 * duplicate names can cause corruption because of the pipelining
1449 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1453 if (!flist || flist->count == 0)
1456 sorting_flist = flist;
1457 qsort(flist->files, flist->count,
1458 sizeof flist->files[0], (int (*)())file_compare);
1459 sorting_flist = NULL;
1461 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1462 if (flist->files[i]->basename) {
1467 flist->low = prev_i;
1468 while (++i < flist->count) {
1470 struct file_struct *file = flist->files[i];
1472 if (!file->basename)
1474 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1476 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1477 int save_mode = file->mode;
1478 /* Make sure that this directory doesn't duplicate a
1479 * non-directory earlier in the list. */
1480 flist->high = prev_i;
1481 file->mode = S_IFREG;
1482 j = flist_find(flist, file);
1483 file->mode = save_mode;
1487 struct file_struct *fp = flist->files[j];
1489 /* If one is a dir and the other is not, we want to
1490 * keep the dir because it might have contents in the
1492 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1493 if (S_ISDIR(file->mode))
1499 if (verbose > 1 && !am_server) {
1501 "removing duplicate name %s from file list (%d)\n",
1502 safe_fname(f_name(file)), drop);
1504 /* Make sure that if we unduplicate '.', that we don't
1505 * lose track of a user-specified top directory. */
1506 if (flist->files[drop]->flags & FLAG_TOP_DIR)
1507 flist->files[keep]->flags |= FLAG_TOP_DIR;
1509 clear_file(drop, flist);
1512 if (flist->low == drop) {
1514 j < i && !flist->files[j]->basename;
1523 flist->high = no_dups ? prev_i : flist->count - 1;
1526 /* We need to strip off the leading slashes for relative
1527 * paths, but this must be done _after_ the sorting phase. */
1528 for (i = flist->low; i <= flist->high; i++) {
1529 struct file_struct *file = flist->files[i];
1533 if (*file->dirname == '/') {
1534 char *s = file->dirname + 1;
1535 while (*s == '/') s++;
1536 memmove(file->dirname, s, strlen(s) + 1);
1539 if (!*file->dirname)
1540 file->dirname = NULL;
1545 static void output_flist(struct file_list *flist, const char *whose_list)
1547 char uidbuf[16], gidbuf[16], depthbuf[16];
1548 struct file_struct *file;
1551 for (i = 0; i < flist->count; i++) {
1552 file = flist->files[i];
1553 if ((am_root || am_sender) && preserve_uid)
1554 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1557 if (preserve_gid && file->gid != GID_NONE)
1558 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1562 sprintf(depthbuf, "%d", file->dir.depth);
1563 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1564 whose_list, i, am_sender ? NS(file->dir.root) : depthbuf,
1565 file->dirname ? safe_fname(file->dirname) : "",
1566 file->dirname ? "/" : "", NS(file->basename),
1567 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
1568 (double)file->length, uidbuf, gidbuf, file->flags);
1573 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1574 enum fnc_type { t_PATH, t_ITEM };
1576 /* Compare the names of two file_struct entities, similar to how strcmp()
1577 * would do if it were operating on the joined strings.
1579 * Some differences beginning with protocol_version 29: (1) directory names
1580 * are compared with an assumed trailing slash so that they compare in a
1581 * way that would cause them to sort immediately prior to any content they
1582 * may have; (2) a directory of any name compares after a non-directory of
1583 * any name at the same depth; (3) a directory with name "." compares prior
1584 * to anything else. These changes mean that a directory and a non-dir
1585 * with the same name will not compare as equal (protocol_version >= 29).
1587 * The dirname component can be an empty string, but the basename component
1588 * cannot (and never is in the current codebase). The basename component
1589 * may be NULL (for a removed item), in which case it is considered to be
1590 * after any existing item. */
1591 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1594 const uchar *c1, *c2;
1595 enum fnc_state state1, state2;
1596 enum fnc_type type1, type2;
1597 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1599 if (!f1 || !f1->basename) {
1600 if (!f2 || !f2->basename)
1604 if (!f2 || !f2->basename)
1607 c1 = (uchar*)f1->dirname;
1608 c2 = (uchar*)f2->dirname;
1612 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1613 c1 = (uchar*)f1->basename;
1614 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1616 state1 = s_TRAILING;
1629 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1630 c2 = (uchar*)f2->basename;
1631 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1633 state2 = s_TRAILING;
1647 return type1 == t_PATH ? 1 : -1;
1650 if ((dif = (int)*c1++ - (int)*c2++) != 0)
1659 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1661 c1 = (uchar*)f1->basename;
1664 state1 = s_TRAILING;
1665 if (type1 == t_PATH) {
1674 if (*c2 && type1 != type2)
1675 return type1 == t_PATH ? 1 : -1;
1680 if (state1 == s_SLASH && sorting_flist) {
1682 /* Optimize for future comparisons. */
1684 j < sorting_flist->count;
1686 struct file_struct *fp
1687 = sorting_flist->files[j];
1688 if (fp->dirname == f2->dirname)
1689 fp->dirname = f1->dirname;
1696 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1698 c2 = (uchar*)f2->basename;
1701 state2 = s_TRAILING;
1702 if (type2 == t_PATH) {
1714 return type1 == t_PATH ? 1 : -1;
1722 /* Return a copy of the full filename of a flist entry, using the indicated
1723 * buffer. No size-checking is done because we checked the size when creating
1724 * the file_struct entry.
1726 char *f_name_to(struct file_struct *f, char *fbuf)
1728 if (!f || !f->basename)
1732 int len = strlen(f->dirname);
1733 memcpy(fbuf, f->dirname, len);
1735 strcpy(fbuf + len + 1, f->basename);
1737 strcpy(fbuf, f->basename);
1742 /* Like f_name_to(), but we rotate through 5 static buffers of our own. */
1743 char *f_name(struct file_struct *f)
1745 static char names[5][MAXPATHLEN];
1746 static unsigned int n;
1748 n = (n + 1) % (sizeof names / sizeof names[0]);
1750 return f_name_to(f, names[n]);
1754 static int is_backup_file(char *fn)
1756 int k = strlen(fn) - backup_suffix_len;
1757 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
1760 struct file_list *get_dirlist(const char *dirname, int ignore_filter_rules)
1762 struct file_list *dirlist;
1763 char dirbuf[MAXPATHLEN];
1765 int save_recurse = recurse;
1767 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1768 if (dlen >= MAXPATHLEN)
1771 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1773 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirbuf, dlen);
1774 recurse = save_recurse;
1780 /* This function is used to implement per-directory deletion, and
1781 * is used by all the --delete-WHEN options. Note that the fbuf
1782 * pointer must point to a MAXPATHLEN buffer with the name of the
1783 * directory in it (the functions we call will append names onto
1784 * the end, but the old dir value will be restored on exit). */
1785 void delete_in_dir(struct file_list *flist, char *fbuf,
1786 struct file_struct *file)
1788 static int min_depth = MAXPATHLEN, cur_depth = -1;
1789 static void *filt_array[MAXPATHLEN/2+1];
1790 struct file_list *dir_list;
1795 while (cur_depth >= min_depth)
1796 pop_local_filters(filt_array[cur_depth--]);
1797 min_depth = MAXPATHLEN;
1801 if (file->dir.depth >= MAXPATHLEN/2+1)
1802 return; /* Impossible... */
1804 if (max_delete && deletion_count >= max_delete)
1807 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
1809 "IO error encountered -- skipping file deletion\n");
1810 max_delete = -1; /* avoid duplicating the above warning */
1814 while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
1815 pop_local_filters(filt_array[cur_depth--]);
1816 cur_depth = file->dir.depth;
1817 if (min_depth > cur_depth)
1818 min_depth = cur_depth;
1819 dlen = strlen(fbuf);
1820 filt_array[cur_depth] = push_local_filters(fbuf, dlen);
1822 if (link_stat(fbuf, &st, keep_dirlinks) < 0)
1825 if (one_file_system && file->flags & FLAG_TOP_DIR)
1826 filesystem_dev = st.st_dev;
1828 dir_list = flist_new(WITHOUT_HLINK, "delete_in_dir");
1831 send_directory(-1, dir_list, fbuf, dlen);
1836 output_flist(dir_list, "delete");
1838 delete_missing(flist, dir_list, fbuf);
1840 flist_free(dir_list);
1844 /* If an item in dir_list is not found in full_list, delete it from the
1846 void delete_missing(struct file_list *full_list, struct file_list *dir_list,
1847 const char *dirname)
1851 if (max_delete && deletion_count >= max_delete)
1855 rprintf(FINFO, "delete_missing(%s)\n", safe_fname(dirname));
1857 for (i = dir_list->count; i--; ) {
1858 if (!dir_list->files[i]->basename)
1860 mode = dir_list->files[i]->mode;
1861 if (flist_find(full_list, dir_list->files[i]) < 0) {
1862 char *f = f_name(dir_list->files[i]);
1863 if (make_backups && (backup_dir || !is_backup_file(f))
1864 && !S_ISDIR(mode)) {
1866 if (verbose || log_format)
1867 log_delete(f, mode);
1868 } else if (S_ISDIR(mode))
1869 delete_file(f, mode, DEL_FORCE_RECURSE);
1871 delete_file(f, mode, 0);
1873 if (max_delete && deletion_count >= max_delete)