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 delete_during;
39 extern int always_checksum;
41 extern int ignore_errors;
42 extern int numeric_ids;
46 extern char curr_dir[MAXPATHLEN];
47 extern unsigned int curr_dir_len;
48 extern char *backup_dir;
49 extern char *backup_suffix;
50 extern int filesfrom_fd;
52 extern int one_file_system;
53 extern int keep_dirlinks;
54 extern int preserve_links;
55 extern int preserve_hard_links;
56 extern int preserve_perms;
57 extern int preserve_devices;
58 extern int preserve_uid;
59 extern int preserve_gid;
60 extern int relative_paths;
61 extern int implied_dirs;
62 extern int make_backups;
63 extern int backup_suffix_len;
64 extern int copy_links;
65 extern int copy_unsafe_links;
66 extern int protocol_version;
67 extern int sanitize_paths;
68 extern int delete_excluded;
69 extern int max_delete;
70 extern int orig_umask;
73 extern struct exclude_list_struct exclude_list;
74 extern struct exclude_list_struct server_exclude_list;
78 static char empty_sum[MD4_SUM_LENGTH];
79 static unsigned int file_struct_len;
80 static struct file_list *received_flist;
82 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
83 static void output_flist(struct file_list *flist, const char *whose_list);
89 /* Figure out how big the file_struct is without trailing padding */
90 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
94 static int show_filelist_p(void)
96 return verbose && xfer_dirs && !am_server;
99 static void start_filelist_progress(char *kind)
101 rprintf(FINFO, "%s ... ", kind);
102 if (verbose > 1 || do_progress)
103 rprintf(FINFO, "\n");
108 static void emit_filelist_progress(const struct file_list *flist)
110 rprintf(FINFO, " %d files...\r", flist->count);
114 static void maybe_emit_filelist_progress(const struct file_list *flist)
116 if (do_progress && show_filelist_p() && (flist->count % 100) == 0)
117 emit_filelist_progress(flist);
121 static void finish_filelist_progress(const struct file_list *flist)
124 /* This overwrites the progress line */
125 rprintf(FINFO, "%d file%sto consider\n",
126 flist->count, flist->count == 1 ? " " : "s ");
128 rprintf(FINFO, "done\n");
131 void show_flist_stats(void)
137 static void list_file_entry(struct file_struct *f)
142 /* this can happen if duplicate names were removed */
146 permstring(perms, f->mode);
149 if (preserve_links && S_ISLNK(f->mode)) {
150 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
152 (double)f->length, timestring(f->modtime),
153 f_name(f), f->u.link);
157 rprintf(FINFO, "%s %11.0f %s %s\n",
159 (double)f->length, timestring(f->modtime),
166 * Stat either a symlink or its referent, depending on the settings of
167 * copy_links, copy_unsafe_links, etc.
169 * @retval -1 on error
171 * @retval 0 for success
173 * @post If @p path is a symlink, then @p linkbuf (of size @c
174 * MAXPATHLEN) contains the symlink target.
176 * @post @p buffer contains information about the link or the
177 * referrent as appropriate, if they exist.
179 static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
183 return do_stat(path, buffer);
184 if (link_stat(path, buffer, 0) < 0)
186 if (S_ISLNK(buffer->st_mode)) {
187 int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1);
191 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
193 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
196 return do_stat(path, buffer);
201 return do_stat(path, buffer);
205 int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks)
209 return do_stat(path, buffer);
210 if (do_lstat(path, buffer) < 0)
212 if (follow_dirlinks && S_ISLNK(buffer->st_mode)) {
214 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
219 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 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
228 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
230 #if 0 /* This currently never happens, so avoid a useless compare. */
231 if (exclude_level == NO_EXCLUDES)
235 /* never exclude '.', even if somebody does --exclude '*' */
236 if (fname[0] == '.' && !fname[1])
238 /* Handle the -R version of the '.' dir. */
239 if (fname[0] == '/') {
240 int len = strlen(fname);
241 if (fname[len-1] == '.' && fname[len-2] == '/')
245 if (server_exclude_list.head
246 && check_exclude(&server_exclude_list, fname, is_dir) < 0)
248 if (exclude_level != ALL_EXCLUDES)
250 if (exclude_list.head
251 && check_exclude(&exclude_list, fname, is_dir) < 0)
256 /* used by the one_file_system code */
257 static dev_t filesystem_dev;
259 static void set_filesystem(char *fname)
262 if (do_stat(fname, &st) != 0)
264 filesystem_dev = st.st_dev;
268 static int to_wire_mode(mode_t mode)
271 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
272 return (mode & ~(_S_IFMT)) | 0120000;
277 static mode_t from_wire_mode(int mode)
279 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
280 return (mode & ~(_S_IFMT)) | _S_IFLNK;
285 static void send_directory(int f, struct file_list *flist, char *dir);
287 static char *flist_dir;
288 static int flist_dir_len;
292 * Make sure @p flist is big enough to hold at least @p flist->count
295 void flist_expand(struct file_list *flist)
297 struct file_struct **new_ptr;
299 if (flist->count < flist->malloced)
302 if (flist->malloced < FLIST_START)
303 flist->malloced = FLIST_START;
304 else if (flist->malloced >= FLIST_LINEAR)
305 flist->malloced += FLIST_LINEAR;
307 flist->malloced *= 2;
310 * In case count jumped or we are starting the list
311 * with a known size just set it.
313 if (flist->malloced < flist->count)
314 flist->malloced = flist->count;
316 new_ptr = realloc_array(flist->files, struct file_struct *,
319 if (verbose >= 2 && flist->malloced != FLIST_START) {
320 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
322 (double)sizeof flist->files[0] * flist->malloced,
323 (new_ptr == flist->files) ? " not" : "");
326 flist->files = new_ptr;
329 out_of_memory("flist_expand");
332 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
334 unsigned short flags;
335 static time_t modtime;
339 static uint32 rdev_major;
342 static char lastname[MAXPATHLEN];
343 char fname[MAXPATHLEN];
351 modtime = 0, mode = 0;
352 dev = 0, rdev = makedev(0, 0);
359 io_write_phase = "send_file_entry";
361 f_name_to(file, fname);
365 if (file->mode == mode)
366 flags |= XMIT_SAME_MODE;
369 if (preserve_devices) {
370 if (protocol_version < 28) {
371 if (IS_DEVICE(mode)) {
372 if (file->u.rdev == rdev)
373 flags |= XMIT_SAME_RDEV_pre28;
377 rdev = makedev(0, 0);
378 } else if (IS_DEVICE(mode)) {
380 if ((uint32)major(rdev) == rdev_major)
381 flags |= XMIT_SAME_RDEV_MAJOR;
383 rdev_major = major(rdev);
384 if ((uint32)minor(rdev) <= 0xFFu)
385 flags |= XMIT_RDEV_MINOR_IS_SMALL;
388 if (file->uid == uid)
389 flags |= XMIT_SAME_UID;
392 if (file->gid == gid)
393 flags |= XMIT_SAME_GID;
396 if (file->modtime == modtime)
397 flags |= XMIT_SAME_TIME;
399 modtime = file->modtime;
401 #if SUPPORT_HARD_LINKS
402 if (file->link_u.idev) {
403 if (file->F_DEV == dev) {
404 if (protocol_version >= 28)
405 flags |= XMIT_SAME_DEV;
408 flags |= XMIT_HAS_IDEV_DATA;
413 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
415 l2 = strlen(fname+l1);
418 flags |= XMIT_SAME_NAME;
420 flags |= XMIT_LONG_NAME;
422 /* We must make sure we don't send a zero flag byte or the
423 * other end will terminate the flist transfer. Note that
424 * the use of XMIT_DEL_START on a non-dir has no meaning, so
425 * it's harmless way to add a bit to the first flag byte. */
426 if (protocol_version >= 28) {
427 if (!flags && !S_ISDIR(mode))
428 flags |= XMIT_DEL_START;
429 if ((flags & 0xFF00) || !flags) {
430 flags |= XMIT_EXTENDED_FLAGS;
431 write_byte(f, flags);
432 write_byte(f, flags >> 8);
434 write_byte(f, flags);
436 if (!(flags & 0xFF) && !S_ISDIR(mode))
437 flags |= XMIT_DEL_START;
439 flags |= XMIT_LONG_NAME;
440 write_byte(f, flags);
442 if (flags & XMIT_SAME_NAME)
444 if (flags & XMIT_LONG_NAME)
448 write_buf(f, fname + l1, l2);
450 write_longint(f, file->length);
451 if (!(flags & XMIT_SAME_TIME))
452 write_int(f, modtime);
453 if (!(flags & XMIT_SAME_MODE))
454 write_int(f, to_wire_mode(mode));
455 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
460 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
465 if (preserve_devices && IS_DEVICE(mode)) {
466 if (protocol_version < 28) {
467 if (!(flags & XMIT_SAME_RDEV_pre28))
468 write_int(f, (int)rdev);
470 if (!(flags & XMIT_SAME_RDEV_MAJOR))
471 write_int(f, major(rdev));
472 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
473 write_byte(f, minor(rdev));
475 write_int(f, minor(rdev));
480 if (preserve_links && S_ISLNK(mode)) {
481 int len = strlen(file->u.link);
483 write_buf(f, file->u.link, len);
487 #if SUPPORT_HARD_LINKS
488 if (flags & XMIT_HAS_IDEV_DATA) {
489 if (protocol_version < 26) {
490 /* 32-bit dev_t and ino_t */
492 write_int(f, file->F_INODE);
494 /* 64-bit dev_t and ino_t */
495 if (!(flags & XMIT_SAME_DEV))
496 write_longint(f, dev);
497 write_longint(f, file->F_INODE);
502 if (always_checksum) {
506 else if (protocol_version < 28) {
507 /* Prior to 28, we sent a useless set of nulls. */
513 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
517 strlcpy(lastname, fname, MAXPATHLEN);
519 io_write_phase = "unknown";
524 void receive_file_entry(struct file_struct **fptr, unsigned short flags,
525 struct file_list *flist, int f)
527 static time_t modtime;
531 static uint32 rdev_major;
534 static char lastname[MAXPATHLEN], *lastdir;
535 static int lastdir_depth, lastdir_len = -1;
536 static unsigned int del_heir_name_len = -1;
537 static int in_del_hier = 0;
538 char thisname[MAXPATHLEN];
539 unsigned int l1 = 0, l2 = 0;
540 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
542 char *basename, *dirname, *bp;
543 struct file_struct *file;
546 modtime = 0, mode = 0;
547 dev = 0, rdev = makedev(0, 0);
551 del_heir_name_len = lastdir_len = -1;
556 if (flags & XMIT_SAME_NAME)
559 if (flags & XMIT_LONG_NAME)
564 if (l2 >= MAXPATHLEN - l1) {
566 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
567 flags, l1, l2, lastname);
568 overflow("receive_file_entry");
571 strlcpy(thisname, lastname, l1 + 1);
572 read_sbuf(f, &thisname[l1], l2);
573 thisname[l1 + l2] = 0;
575 strlcpy(lastname, thisname, MAXPATHLEN);
577 clean_fname(thisname, 0);
580 sanitize_path(thisname, thisname, "", 0);
582 if ((basename = strrchr(thisname, '/')) != NULL) {
583 dirname_len = ++basename - thisname; /* counts future '\0' */
584 if (lastdir_len == dirname_len - 1
585 && strncmp(thisname, lastdir, lastdir_len) == 0) {
587 dirname_len = 0; /* indicates no copy is needed */
595 basename_len = strlen(basename) + 1; /* count the '\0' */
597 file_length = read_longint(f);
598 if (!(flags & XMIT_SAME_TIME))
599 modtime = (time_t)read_int(f);
600 if (!(flags & XMIT_SAME_MODE))
601 mode = from_wire_mode(read_int(f));
603 if (preserve_uid && !(flags & XMIT_SAME_UID))
604 uid = (uid_t)read_int(f);
605 if (preserve_gid && !(flags & XMIT_SAME_GID))
606 gid = (gid_t)read_int(f);
608 if (preserve_devices) {
609 if (protocol_version < 28) {
610 if (IS_DEVICE(mode)) {
611 if (!(flags & XMIT_SAME_RDEV_pre28))
612 rdev = (dev_t)read_int(f);
614 rdev = makedev(0, 0);
615 } else if (IS_DEVICE(mode)) {
617 if (!(flags & XMIT_SAME_RDEV_MAJOR))
618 rdev_major = read_int(f);
619 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
620 rdev_minor = read_byte(f);
622 rdev_minor = read_int(f);
623 rdev = makedev(rdev_major, rdev_minor);
628 if (preserve_links && S_ISLNK(mode)) {
629 linkname_len = read_int(f) + 1; /* count the '\0' */
630 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
631 rprintf(FERROR, "overflow: linkname_len=%d\n",
633 overflow("receive_file_entry");
640 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
642 alloc_len = file_struct_len + dirname_len + basename_len
643 + linkname_len + sum_len;
644 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
646 file = *fptr = (struct file_struct *)bp;
647 memset(bp, 0, file_struct_len);
648 bp += file_struct_len;
651 file->modtime = modtime;
652 file->length = file_length;
658 if (flags & XMIT_DEL_START) {
660 del_heir_name_len = l1 + l2;
661 file->flags |= FLAG_DEL_START;
662 } else if (delete_during && in_del_hier) {
663 if (!relative_paths || (l1 >= del_heir_name_len
664 && thisname[del_heir_name_len] == '/'))
665 file->flags |= FLAG_DEL_START;
672 file->dirname = lastdir = bp;
673 lastdir_len = dirname_len - 1;
674 memcpy(bp, dirname, dirname_len - 1);
678 lastdir_depth = count_dir_elements(lastdir);
680 file->dirname = dirname;
683 memcpy(bp, basename, basename_len);
686 if (preserve_devices && IS_DEVICE(mode))
692 read_sbuf(f, bp, linkname_len - 1);
694 sanitize_path(bp, bp, "", lastdir_depth);
699 #if SUPPORT_HARD_LINKS
700 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
701 flags |= XMIT_HAS_IDEV_DATA;
702 if (flags & XMIT_HAS_IDEV_DATA) {
704 if (protocol_version < 26) {
708 if (!(flags & XMIT_SAME_DEV))
709 dev = read_longint(f);
710 inode = read_longint(f);
712 if (flist->hlink_pool) {
713 file->link_u.idev = pool_talloc(flist->hlink_pool,
714 struct idev, 1, "inode_table");
715 file->F_INODE = inode;
721 if (always_checksum) {
724 file->u.sum = sum = bp;
726 } else if (protocol_version < 28) {
727 /* Prior to 28, we get a useless set of nulls. */
733 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
737 if (!preserve_perms) {
738 /* set an appropriate set of permissions based on original
739 * permissions and umask. This emulates what GNU cp does */
740 file->mode &= ~orig_umask;
746 * Create a file_struct for a named file by reading its stat()
747 * information and performing extensive checks against global
750 * @return the new file, or NULL if there was an error or this file
751 * should be excluded.
753 * @todo There is a small optimization opportunity here to avoid
754 * stat()ing the file in some circumstances, which has a certain cost.
755 * We are called immediately after doing readdir(), and so we may
756 * already know the d_type of the file. We could for example avoid
757 * statting directories if we're not recursing, but this is not a very
758 * important case. Some systems may not have d_type.
760 struct file_struct *make_file(char *fname, struct file_list *flist,
763 static char *lastdir;
764 static int lastdir_len = -1;
765 struct file_struct *file;
767 char sum[SUM_LENGTH];
768 char thisname[MAXPATHLEN];
769 char linkname[MAXPATHLEN];
770 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
771 char *basename, *dirname, *bp;
772 unsigned short flags = 0;
774 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
777 if (strlcpy(thisname, fname, sizeof thisname)
778 >= sizeof thisname - flist_dir_len) {
779 rprintf(FINFO, "skipping overly long name: %s\n", fname);
782 clean_fname(thisname, 0);
784 sanitize_path(thisname, thisname, "", 0);
786 memset(sum, 0, SUM_LENGTH);
788 if (readlink_stat(thisname, &st, linkname) != 0) {
789 int save_errno = errno;
790 /* See if file is excluded before reporting an error. */
791 if (exclude_level != NO_EXCLUDES
792 && check_exclude_file(thisname, 0, exclude_level))
794 if (save_errno == ENOENT) {
796 /* Avoid "vanished" error if symlink points nowhere. */
797 if (copy_links && do_lstat(thisname, &st) == 0
798 && S_ISLNK(st.st_mode)) {
799 io_error |= IOERR_GENERAL;
800 rprintf(FERROR, "symlink has no referent: %s\n",
801 full_fname(thisname));
805 enum logcode c = am_daemon && protocol_version < 28
807 io_error |= IOERR_VANISHED;
808 rprintf(c, "file has vanished: %s\n",
809 full_fname(thisname));
812 io_error |= IOERR_GENERAL;
813 rsyserr(FERROR, save_errno, "readlink %s failed",
814 full_fname(thisname));
819 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
820 if (exclude_level == NO_EXCLUDES)
823 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
824 rprintf(FINFO, "skipping directory %s\n", thisname);
828 /* We only care about directories because we need to avoid recursing
829 * into a mount-point directory, not to avoid copying a symlinked
830 * file if -L (or similar) was specified. */
831 if (one_file_system && st.st_dev != filesystem_dev
832 && S_ISDIR(st.st_mode))
833 flags |= FLAG_MOUNT_POINT;
835 if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level))
838 if (lp_ignore_nonreadable(module_id)) {
840 if (!S_ISLNK(st.st_mode))
842 if (access(thisname, R_OK) != 0)
849 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
850 who_am_i(), thisname, exclude_level);
853 if ((basename = strrchr(thisname, '/')) != NULL) {
854 dirname_len = ++basename - thisname; /* counts future '\0' */
855 if (lastdir_len == dirname_len - 1
856 && strncmp(thisname, lastdir, lastdir_len) == 0) {
858 dirname_len = 0; /* indicates no copy is needed */
866 basename_len = strlen(basename) + 1; /* count the '\0' */
869 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
874 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
876 alloc_len = file_struct_len + dirname_len + basename_len
877 + linkname_len + sum_len;
879 bp = pool_alloc(flist->file_pool, alloc_len,
880 "receive_file_entry");
882 if (!(bp = new_array(char, alloc_len)))
883 out_of_memory("receive_file_entry");
886 file = (struct file_struct *)bp;
887 memset(bp, 0, file_struct_len);
888 bp += file_struct_len;
891 file->modtime = st.st_mtime;
892 file->length = st.st_size;
893 file->mode = st.st_mode;
894 file->uid = st.st_uid;
895 file->gid = st.st_gid;
897 #if SUPPORT_HARD_LINKS
898 if (flist && flist->hlink_pool) {
899 if (protocol_version < 28) {
900 if (S_ISREG(st.st_mode))
901 file->link_u.idev = pool_talloc(
902 flist->hlink_pool, struct idev, 1,
905 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
906 file->link_u.idev = pool_talloc(
907 flist->hlink_pool, struct idev, 1,
911 if (file->link_u.idev) {
912 file->F_DEV = st.st_dev;
913 file->F_INODE = st.st_ino;
918 file->dirname = lastdir = bp;
919 lastdir_len = dirname_len - 1;
920 memcpy(bp, dirname, dirname_len - 1);
924 file->dirname = dirname;
927 memcpy(bp, basename, basename_len);
930 #ifdef HAVE_STRUCT_STAT_ST_RDEV
931 if (preserve_devices && IS_DEVICE(st.st_mode))
932 file->u.rdev = st.st_rdev;
938 memcpy(bp, linkname, linkname_len);
945 file_checksum(thisname, bp, st.st_size);
949 file->basedir = flist_dir;
951 /* This code is only used by the receiver when it is building
952 * a list of files for a delete pass. */
953 if (keep_dirlinks && linkname_len && flist) {
955 int i = flist_find(received_flist, file);
956 if (i >= 0 && S_ISDIR(received_flist->files[i]->mode)
957 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
958 file->modtime = st2.st_mtime;
959 file->length = st2.st_size;
960 file->mode = st2.st_mode;
961 file->uid = st2.st_uid;
962 file->gid = st2.st_gid;
964 if (file->link_u.idev) {
965 pool_free(flist->hlink_pool, 0, file->link_u.idev);
966 file->link_u.idev = NULL;
971 if (!S_ISDIR(st.st_mode))
972 stats.total_size += st.st_size;
978 void send_file_name(int f, struct file_list *flist, char *fname,
979 int recursive, unsigned short base_flags)
981 struct file_struct *file;
982 char fbuf[MAXPATHLEN];
984 /* f is set to -1 when calculating deletion file list */
985 file = make_file(fname, flist,
986 f == -1 && delete_excluded? SERVER_EXCLUDES : ALL_EXCLUDES);
991 maybe_emit_filelist_progress(flist);
995 if (file->basename[0]) {
996 flist->files[flist->count++] = file;
997 send_file_entry(file, f, base_flags);
1000 if (recursive && S_ISDIR(file->mode)
1001 && !(file->flags & FLAG_MOUNT_POINT)) {
1002 send_directory(f, flist, f_name_to(file, fbuf));
1007 /* Note that the "recurse" value either contains -1, for infinite recursion,
1008 * or a number >= 0 indicating how many levels of recursion we will allow. */
1009 static void send_directory(int f, struct file_list *flist, char *dir)
1013 char fname[MAXPATHLEN];
1014 unsigned int offset;
1015 void *save_excludes;
1020 io_error |= IOERR_GENERAL;
1021 rsyserr(FERROR, errno, "opendir %s failed", full_fname(dir));
1025 offset = strlcpy(fname, dir, MAXPATHLEN);
1027 if (offset >= MAXPATHLEN || p[-1] != '/') {
1028 if (offset >= MAXPATHLEN - 1) {
1029 io_error |= IOERR_GENERAL;
1030 rprintf(FERROR, "skipping long-named directory: %s\n",
1039 save_excludes = push_local_excludes(fname, offset);
1041 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1042 char *dname = d_name(di);
1043 if (dname[0] == '.' && (dname[1] == '\0'
1044 || (dname[1] == '.' && dname[2] == '\0')))
1046 if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset) {
1047 int do_subdirs = recurse >= 1 ? recurse-- : recurse;
1048 send_file_name(f, flist, fname, do_subdirs, 0);
1050 io_error |= IOERR_GENERAL;
1052 "cannot send long-named file %s\n",
1057 io_error |= IOERR_GENERAL;
1058 rsyserr(FERROR, errno, "readdir(%s)", dir);
1061 pop_local_excludes(save_excludes);
1067 /* This function is normally called by the sender, but the receiving side
1068 * also uses it to construct one or more file lists if one of the --delete
1069 * options have been specified. The delete_in_dir() function sets f to -1
1070 * so that we just construct the file list in memory without sending it
1071 * over the wire. It also has the side-effect of ignoring user-excludes if
1072 * delete_excluded is set (so that the delete list includes user-excluded
1073 * files) and it avoids some per-arg init code for limited recursion (since
1074 * delete_in_dir() sets recurse before calling this function). */
1075 struct file_list *send_file_list(int f, int argc, char *argv[])
1079 char *p, *dir, olddir[sizeof curr_dir];
1080 char lastpath[MAXPATHLEN] = "";
1081 struct file_list *flist;
1082 BOOL need_first_push = True;
1086 if (show_filelist_p() && f != -1)
1087 start_filelist_progress("building file list");
1089 start_write = stats.total_written;
1091 flist = flist_new(f == -1 ? WITHOUT_HLINK : WITH_HLINK,
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);
1103 if (curr_dir_len < MAXPATHLEN - 1) {
1104 push_local_excludes(curr_dir, curr_dir_len);
1105 need_first_push = False;
1111 char fname2[MAXPATHLEN];
1112 char *fname = fname2;
1116 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1118 sanitize_path(fname, fname, "", 0);
1122 strlcpy(fname, *argv++, MAXPATHLEN);
1124 sanitize_path(fname, fname, "", 0);
1128 if (!l || fname[l - 1] == '/') {
1129 if (l == 2 && fname[0] == '.') {
1130 /* Turn "./" into just "." rather than "./." */
1132 } else if (l < MAXPATHLEN) {
1138 ; /* recurse is pre-set */
1139 else if (fname[l-1] == '.' && (l == 1 || fname[l-2] == '/')) {
1140 if (!recurse && xfer_dirs)
1141 recurse = 1; /* allow one level */
1142 } else if (recurse > 0)
1145 if (need_first_push) {
1146 if ((p = strrchr(fname, '/')) != NULL) {
1147 if (*++p && strcmp(p, ".") != 0)
1148 push_local_excludes(fname, p - fname);
1149 } else if (strcmp(fname, ".") != 0)
1150 push_local_excludes(fname, 0);
1151 need_first_push = False;
1154 if (link_stat(fname, &st, keep_dirlinks) != 0) {
1156 io_error |= IOERR_GENERAL;
1157 rsyserr(FERROR, errno, "link_stat %s failed",
1163 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1164 rprintf(FINFO, "skipping directory %s\n", fname);
1171 if (!relative_paths) {
1172 p = strrchr(fname, '/');
1181 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1182 /* this ensures we send the intermediate directories,
1183 thus getting 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, 0);
1204 copy_links = save_copy_links;
1205 xfer_dirs = save_xfer_dirs;
1207 strlcpy(lastpath, fname, sizeof lastpath);
1216 static char *lastdir;
1217 static int lastdir_len;
1219 strcpy(olddir, curr_dir); /* can't overflow */
1221 if (!push_dir(dir)) {
1222 io_error |= IOERR_GENERAL;
1223 rsyserr(FERROR, errno, "push_dir %s failed",
1228 if (lastdir && strcmp(lastdir, dir) == 0) {
1229 flist_dir = lastdir;
1230 flist_dir_len = lastdir_len;
1232 flist_dir = lastdir = strdup(dir);
1233 flist_dir_len = lastdir_len = strlen(dir);
1237 if (one_file_system)
1238 set_filesystem(fname);
1240 do_subdirs = recurse >= 1 ? recurse-- : recurse;
1241 send_file_name(f, flist, fname, do_subdirs, XMIT_DEL_START);
1246 if (!pop_dir(olddir)) {
1247 rsyserr(FERROR, errno, "pop_dir %s failed",
1249 exit_cleanup(RERR_FILESELECT);
1255 send_file_entry(NULL, f, 0);
1257 if (show_filelist_p())
1258 finish_filelist_progress(flist);
1261 if (flist->hlink_pool) {
1262 pool_destroy(flist->hlink_pool);
1263 flist->hlink_pool = NULL;
1266 clean_flist(flist, 0, 0);
1269 /* Now send the uid/gid list. This was introduced in
1270 * protocol version 15 */
1273 /* send the io_error flag */
1274 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1277 stats.flist_size = stats.total_written - start_write;
1278 stats.num_files = flist->count;
1282 output_flist(flist, f < 0 ? "delete" : who_am_i());
1285 rprintf(FINFO, "send_file_list done\n");
1291 struct file_list *recv_file_list(int f)
1293 struct file_list *flist;
1294 unsigned short flags;
1297 if (show_filelist_p())
1298 start_filelist_progress("receiving file list");
1300 start_read = stats.total_read;
1302 flist = flist_new(WITH_HLINK, "recv_file_list");
1303 received_flist = flist;
1306 flist->malloced = 1000;
1307 flist->files = new_array(struct file_struct *, flist->malloced);
1312 while ((flags = read_byte(f)) != 0) {
1313 int i = flist->count;
1315 flist_expand(flist);
1317 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1318 flags |= read_byte(f) << 8;
1319 receive_file_entry(&flist->files[i], flags, flist, f);
1321 if (S_ISREG(flist->files[i]->mode))
1322 stats.total_size += flist->files[i]->length;
1326 maybe_emit_filelist_progress(flist);
1329 rprintf(FINFO, "recv_file_name(%s)\n",
1330 f_name(flist->files[i]));
1333 receive_file_entry(NULL, 0, NULL, 0); /* Signal that we're done. */
1336 rprintf(FINFO, "received %d names\n", flist->count);
1338 if (show_filelist_p())
1339 finish_filelist_progress(flist);
1341 clean_flist(flist, relative_paths, 1);
1344 /* Now send the uid/gid list. This was introduced in
1345 * protocol version 15 */
1346 recv_uid_list(f, flist);
1348 /* Recv the io_error flag */
1349 if (lp_ignore_errors(module_id) || ignore_errors)
1352 io_error |= read_int(f);
1356 output_flist(flist, who_am_i());
1360 for (i = 0; i < flist->count; i++)
1361 list_file_entry(flist->files[i]);
1365 rprintf(FINFO, "recv_file_list done\n");
1367 stats.flist_size = stats.total_read - start_read;
1368 stats.num_files = flist->count;
1373 out_of_memory("recv_file_list");
1374 return NULL; /* not reached */
1378 int file_compare(struct file_struct **file1, struct file_struct **file2)
1380 struct file_struct *f1 = *file1;
1381 struct file_struct *f2 = *file2;
1383 if (!f1->basename && !f2->basename)
1389 if (f1->dirname == f2->dirname)
1390 return u_strcmp(f1->basename, f2->basename);
1391 return f_name_cmp(f1, f2);
1395 int flist_find(struct file_list *flist, struct file_struct *f)
1397 int low = 0, high = flist->count - 1;
1399 while (high >= 0 && !flist->files[high]->basename) high--;
1404 while (low != high) {
1405 int mid = (low + high) / 2;
1406 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1408 return flist_up(flist, mid);
1415 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1416 return flist_up(flist, low);
1421 * Free up any resources a file_struct has allocated
1422 * and clear the file.
1424 void clear_file(int i, struct file_list *flist)
1426 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1427 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1428 memset(flist->files[i], 0, file_struct_len);
1433 * allocate a new file list
1435 struct file_list *flist_new(int with_hlink, char *msg)
1437 struct file_list *flist;
1439 flist = new(struct file_list);
1443 memset(flist, 0, sizeof (struct file_list));
1445 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1446 out_of_memory, POOL_INTERN)))
1449 #if SUPPORT_HARD_LINKS
1450 if (with_hlink && preserve_hard_links) {
1451 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1452 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1461 * free up all elements in a flist
1463 void flist_free(struct file_list *flist)
1465 pool_destroy(flist->file_pool);
1466 pool_destroy(flist->hlink_pool);
1473 * This routine ensures we don't have any duplicate names in our file list.
1474 * duplicate names can cause corruption because of the pipelining
1476 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1480 if (!flist || flist->count == 0)
1483 qsort(flist->files, flist->count,
1484 sizeof flist->files[0], (int (*)())file_compare);
1486 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1487 if (flist->files[i]->basename) {
1492 while (++i < flist->count) {
1493 if (!flist->files[i]->basename)
1495 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1496 if (verbose > 1 && !am_server) {
1498 "removing duplicate name %s from file list %d\n",
1499 f_name(flist->files[i]), i);
1501 /* Make sure that if we unduplicate '.', that we don't
1502 * lose track of a user-specified starting point (or
1503 * else deletions will mysteriously fail with -R). */
1504 if (flist->files[i]->flags & FLAG_DEL_START)
1505 flist->files[prev_i]->flags |= FLAG_DEL_START;
1507 clear_file(i, flist);
1513 /* we need to strip off the root directory in the case
1514 of relative paths, but this must be done _after_
1515 the sorting phase */
1516 for (i = 0; i < flist->count; i++) {
1517 if (flist->files[i]->dirname &&
1518 flist->files[i]->dirname[0] == '/') {
1519 memmove(&flist->files[i]->dirname[0],
1520 &flist->files[i]->dirname[1],
1521 strlen(flist->files[i]->dirname));
1524 if (flist->files[i]->dirname &&
1525 !flist->files[i]->dirname[0]) {
1526 flist->files[i]->dirname = NULL;
1532 static void output_flist(struct file_list *flist, const char *whose_list)
1534 char uidbuf[16], gidbuf[16];
1535 struct file_struct *file;
1538 for (i = 0; i < flist->count; i++) {
1539 file = flist->files[i];
1540 if ((am_root || am_sender) && preserve_uid)
1541 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1544 if (preserve_gid && file->gid != GID_NONE)
1545 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1548 rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f%s%s\n",
1549 whose_list, i, NS(file->basedir), NS(file->dirname),
1550 NS(file->basename), (int)file->mode,
1551 (double)file->length, uidbuf, gidbuf);
1556 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1558 /* Compare the names of two file_struct entities, just like strcmp()
1559 * would do if it were operating on the joined strings. We assume
1560 * that there are no 0-length strings.
1562 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1565 const uchar *c1, *c2;
1566 enum fnc_state state1, state2;
1568 if (!f1 || !f1->basename) {
1569 if (!f2 || !f2->basename)
1573 if (!f2 || !f2->basename)
1576 if (!(c1 = (uchar*)f1->dirname)) {
1578 c1 = (uchar*)f1->basename;
1584 if (!(c2 = (uchar*)f2->dirname)) {
1586 c2 = (uchar*)f2->basename;
1594 if ((dif = (int)*c1 - (int)*c2) != 0)
1604 c1 = (uchar*)f1->basename;
1618 c2 = (uchar*)f2->basename;
1632 /* Return a copy of the full filename of a flist entry, using the indicated
1633 * buffer. No size-checking is done because we checked the size when creating
1634 * the file_struct entry.
1636 char *f_name_to(struct file_struct *f, char *fbuf)
1638 if (!f || !f->basename)
1642 int len = strlen(f->dirname);
1643 memcpy(fbuf, f->dirname, len);
1645 strcpy(fbuf + len + 1, f->basename);
1647 strcpy(fbuf, f->basename);
1652 /* Like f_name_to(), but we rotate through 5 static buffers of our own.
1654 char *f_name(struct file_struct *f)
1656 static char names[5][MAXPATHLEN];
1657 static unsigned int n;
1659 n = (n + 1) % (sizeof names / sizeof names[0]);
1661 return f_name_to(f, names[n]);
1664 static int is_backup_file(char *fn)
1666 int k = strlen(fn) - backup_suffix_len;
1667 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
1670 void delete_in_dir(struct file_list *flist, char *fname)
1672 static int deletion_count = 0;
1673 struct file_list *del_flist;
1674 int save_recurse = recurse;
1675 int save_xfer_dirs = xfer_dirs;
1676 int save_implied_dirs = implied_dirs;
1677 int save_relative_paths = relative_paths;
1681 if (max_delete && deletion_count >= max_delete)
1684 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
1685 rprintf(FINFO, "IO error encountered - skipping file deletion\n");
1686 max_delete = -1; /* avoid duplicating the above warning */
1690 if (delete_during) {
1691 recurse = 1; /* allow one level only */
1698 del_flist = send_file_list(-1, 1, argv);
1700 relative_paths = save_relative_paths;
1701 implied_dirs = save_implied_dirs;
1702 xfer_dirs = save_xfer_dirs;
1703 recurse = save_recurse;
1709 rprintf(FINFO, "deleting in %s\n", safe_fname(fname));
1711 for (i = del_flist->count-1; i >= 0; i--) {
1712 if (max_delete && deletion_count >= max_delete)
1714 if (!del_flist->files[i]->basename)
1716 mode = del_flist->files[i]->mode;
1717 if ((j = flist_find(flist, del_flist->files[i])) < 0
1718 || (delete_during && S_ISDIR(mode)
1719 && !S_ISDIR(flist->files[j]->mode))) {
1720 char *f = f_name(del_flist->files[i]);
1721 if (make_backups && (backup_dir || !is_backup_file(f))
1722 && !S_ISDIR(mode)) {
1725 rprintf(FINFO, "deleting %s\n",
1729 delete_file(f, S_ISDIR(mode)
1730 ? DEL_DIR | DEL_RECURSE : 0);
1735 flist_free(del_flist);