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;
43 extern int cvs_exclude;
46 extern char curr_dir[MAXPATHLEN];
47 extern char *files_from;
48 extern int filesfrom_fd;
50 extern int one_file_system;
51 extern int keep_dirlinks;
52 extern int preserve_links;
53 extern int preserve_hard_links;
54 extern int preserve_perms;
55 extern int preserve_devices;
56 extern int preserve_uid;
57 extern int preserve_gid;
58 extern int relative_paths;
59 extern int implied_dirs;
60 extern int copy_links;
61 extern int copy_unsafe_links;
62 extern int protocol_version;
63 extern int sanitize_paths;
64 extern int delete_excluded;
65 extern int orig_umask;
68 extern struct exclude_list_struct exclude_list;
69 extern struct exclude_list_struct server_exclude_list;
70 extern struct exclude_list_struct local_exclude_list;
74 static char empty_sum[MD4_SUM_LENGTH];
75 static unsigned int file_struct_len;
77 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
78 static void output_flist(struct file_list *flist);
84 /* Figure out how big the file_struct is without trailing padding */
85 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
89 static int show_filelist_p(void)
91 return verbose && (recurse || files_from) && !am_server;
94 static void start_filelist_progress(char *kind)
96 rprintf(FINFO, "%s ... ", kind);
97 if (verbose > 1 || do_progress)
103 static void emit_filelist_progress(const struct file_list *flist)
105 rprintf(FINFO, " %d files...\r", flist->count);
109 static void maybe_emit_filelist_progress(const struct file_list *flist)
111 if (do_progress && show_filelist_p() && (flist->count % 100) == 0)
112 emit_filelist_progress(flist);
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)
132 static void list_file_entry(struct file_struct *f)
137 /* this can happen if duplicate names were removed */
141 permstring(perms, f->mode);
144 if (preserve_links && S_ISLNK(f->mode)) {
145 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
147 (double)f->length, timestring(f->modtime),
148 f_name(f), f->u.link);
152 rprintf(FINFO, "%s %11.0f %s %s\n",
154 (double)f->length, timestring(f->modtime),
161 * Stat either a symlink or its referent, depending on the settings of
162 * copy_links, copy_unsafe_links, etc.
164 * @retval -1 on error
166 * @retval 0 for success
168 * @post If @p path is a symlink, then @p linkbuf (of size @c
169 * MAXPATHLEN) contains the symlink target.
171 * @post @p buffer contains information about the link or the
172 * referrent as appropriate, if they exist.
174 int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
178 return do_stat(path, buffer);
179 if (link_stat(path, buffer, keep_dirlinks) < 0)
181 if (S_ISLNK(buffer->st_mode)) {
182 int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1);
186 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
188 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
191 return do_stat(path, buffer);
196 return do_stat(path, buffer);
200 int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks)
204 return do_stat(path, buffer);
205 if (do_lstat(path, buffer) < 0)
207 if (follow_dirlinks && S_ISLNK(buffer->st_mode)) {
209 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
214 return do_stat(path, buffer);
219 * This function is used to check if a file should be included/excluded
220 * from the list of files based on its name and type etc. The value of
221 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
223 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
227 #if 0 /* This currently never happens, so avoid a useless compare. */
228 if (exclude_level == NO_EXCLUDES)
232 /* never exclude '.', even if somebody does --exclude '*' */
233 if (fname[0] == '.' && !fname[1])
235 /* Handle the -R version of the '.' dir. */
236 if (fname[0] == '/') {
237 int len = strlen(fname);
238 if (fname[len-1] == '.' && fname[len-2] == '/')
242 if (server_exclude_list.head
243 && check_exclude(&server_exclude_list, fname, is_dir) < 0)
245 if (exclude_level != ALL_EXCLUDES)
247 if (exclude_list.head
248 && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
250 if (local_exclude_list.head
251 && check_exclude(&local_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 *,
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_TOP_DIR 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_TOP_DIR;
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_TOP_DIR;
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 char thisname[MAXPATHLEN];
537 unsigned int l1 = 0, l2 = 0;
538 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
540 char *basename, *dirname, *bp;
541 struct file_struct *file;
544 modtime = 0, mode = 0;
545 dev = 0, rdev = makedev(0, 0);
553 if (flags & XMIT_SAME_NAME)
556 if (flags & XMIT_LONG_NAME)
561 if (l2 >= MAXPATHLEN - l1) {
563 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
564 flags, l1, l2, lastname);
565 overflow("receive_file_entry");
568 strlcpy(thisname, lastname, l1 + 1);
569 read_sbuf(f, &thisname[l1], l2);
570 thisname[l1 + l2] = 0;
572 strlcpy(lastname, thisname, MAXPATHLEN);
574 clean_fname(thisname, 0);
577 sanitize_path(thisname, thisname, "", 0);
579 if ((basename = strrchr(thisname, '/')) != NULL) {
580 dirname_len = ++basename - thisname; /* counts future '\0' */
581 if (lastdir_len == dirname_len - 1
582 && strncmp(thisname, lastdir, lastdir_len) == 0) {
584 dirname_len = 0; /* indicates no copy is needed */
592 basename_len = strlen(basename) + 1; /* count the '\0' */
594 file_length = read_longint(f);
595 if (!(flags & XMIT_SAME_TIME))
596 modtime = (time_t)read_int(f);
597 if (!(flags & XMIT_SAME_MODE))
598 mode = from_wire_mode(read_int(f));
600 if (preserve_uid && !(flags & XMIT_SAME_UID))
601 uid = (uid_t)read_int(f);
602 if (preserve_gid && !(flags & XMIT_SAME_GID))
603 gid = (gid_t)read_int(f);
605 if (preserve_devices) {
606 if (protocol_version < 28) {
607 if (IS_DEVICE(mode)) {
608 if (!(flags & XMIT_SAME_RDEV_pre28))
609 rdev = (dev_t)read_int(f);
611 rdev = makedev(0, 0);
612 } else if (IS_DEVICE(mode)) {
614 if (!(flags & XMIT_SAME_RDEV_MAJOR))
615 rdev_major = read_int(f);
616 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
617 rdev_minor = read_byte(f);
619 rdev_minor = read_int(f);
620 rdev = makedev(rdev_major, rdev_minor);
625 if (preserve_links && S_ISLNK(mode)) {
626 linkname_len = read_int(f) + 1; /* count the '\0' */
627 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
628 rprintf(FERROR, "overflow: linkname_len=%d\n",
630 overflow("receive_file_entry");
637 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
639 alloc_len = file_struct_len + dirname_len + basename_len
640 + linkname_len + sum_len;
641 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
643 file = *fptr = (struct file_struct *)bp;
644 memset(bp, 0, file_struct_len);
645 bp += file_struct_len;
647 file->flags = flags & XMIT_TOP_DIR ? FLAG_TOP_DIR : 0;
648 file->modtime = modtime;
649 file->length = file_length;
655 file->dirname = lastdir = bp;
656 lastdir_len = dirname_len - 1;
657 memcpy(bp, dirname, dirname_len - 1);
661 lastdir_depth = count_dir_elements(lastdir);
663 file->dirname = dirname;
666 memcpy(bp, basename, basename_len);
669 if (preserve_devices && IS_DEVICE(mode))
675 read_sbuf(f, bp, linkname_len - 1);
677 sanitize_path(bp, bp, "", lastdir_depth);
682 #if 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) {
707 file->u.sum = sum = bp;
709 } else if (protocol_version < 28) {
710 /* Prior to 28, we get a useless set of nulls. */
716 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
720 if (!preserve_perms) {
721 /* set an appropriate set of permissions based on original
722 * permissions and umask. This emulates what GNU cp does */
723 file->mode &= ~orig_umask;
729 * Create a file_struct for a named file by reading its stat()
730 * information and performing extensive checks against global
733 * @return the new file, or NULL if there was an error or this file
734 * should be excluded.
736 * @todo There is a small optimization opportunity here to avoid
737 * stat()ing the file in some circumstances, which has a certain cost.
738 * We are called immediately after doing readdir(), and so we may
739 * already know the d_type of the file. We could for example avoid
740 * statting directories if we're not recursing, but this is not a very
741 * important case. Some systems may not have d_type.
743 struct file_struct *make_file(char *fname, struct file_list *flist,
746 static char *lastdir;
747 static int lastdir_len = -1;
748 struct file_struct *file;
750 char sum[SUM_LENGTH];
751 char thisname[MAXPATHLEN];
752 char linkname[MAXPATHLEN];
753 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
754 char *basename, *dirname, *bp;
755 unsigned short flags = 0;
757 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
760 if (strlcpy(thisname, fname, sizeof thisname)
761 >= sizeof thisname - flist_dir_len) {
762 rprintf(FINFO, "skipping overly long name: %s\n", fname);
765 clean_fname(thisname, 0);
767 sanitize_path(thisname, thisname, "", 0);
769 memset(sum, 0, SUM_LENGTH);
771 if (readlink_stat(thisname, &st, linkname) != 0) {
772 int save_errno = errno;
773 /* See if file is excluded before reporting an error. */
774 if (exclude_level != NO_EXCLUDES
775 && check_exclude_file(thisname, 0, exclude_level))
777 if (save_errno == ENOENT) {
779 /* Avoid "vanished" error if symlink points nowhere. */
780 if (copy_links && do_lstat(thisname, &st) == 0
781 && S_ISLNK(st.st_mode)) {
782 io_error |= IOERR_GENERAL;
783 rprintf(FERROR, "symlink has no referent: %s\n",
784 full_fname(thisname));
788 enum logcode c = am_daemon && protocol_version < 28
790 io_error |= IOERR_VANISHED;
791 rprintf(c, "file has vanished: %s\n",
792 full_fname(thisname));
795 io_error |= IOERR_GENERAL;
796 rsyserr(FERROR, save_errno, "readlink %s failed",
797 full_fname(thisname));
802 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
803 if (exclude_level == NO_EXCLUDES)
806 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
807 rprintf(FINFO, "skipping directory %s\n", thisname);
811 /* We only care about directories because we need to avoid recursing
812 * into a mount-point directory, not to avoid copying a symlinked
813 * file if -L (or similar) was specified. */
814 if (one_file_system && st.st_dev != filesystem_dev
815 && S_ISDIR(st.st_mode))
816 flags |= FLAG_MOUNT_POINT;
818 if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level))
821 if (lp_ignore_nonreadable(module_id) && access(thisname, R_OK) != 0)
827 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
828 who_am_i(), thisname, exclude_level);
831 if ((basename = strrchr(thisname, '/')) != NULL) {
832 dirname_len = ++basename - thisname; /* counts future '\0' */
833 if (lastdir_len == dirname_len - 1
834 && strncmp(thisname, lastdir, lastdir_len) == 0) {
836 dirname_len = 0; /* indicates no copy is needed */
844 basename_len = strlen(basename) + 1; /* count the '\0' */
847 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
852 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
854 alloc_len = file_struct_len + dirname_len + basename_len
855 + linkname_len + sum_len;
857 bp = pool_alloc(flist->file_pool, alloc_len,
858 "receive_file_entry");
860 if (!(bp = new_array(char, alloc_len)))
861 out_of_memory("receive_file_entry");
864 file = (struct file_struct *)bp;
865 memset(bp, 0, file_struct_len);
866 bp += file_struct_len;
869 file->modtime = st.st_mtime;
870 file->length = st.st_size;
871 file->mode = st.st_mode;
872 file->uid = st.st_uid;
873 file->gid = st.st_gid;
875 #if SUPPORT_HARD_LINKS
876 if (flist && flist->hlink_pool) {
877 if (protocol_version < 28) {
878 if (S_ISREG(st.st_mode))
879 file->link_u.idev = pool_talloc(
880 flist->hlink_pool, struct idev, 1,
883 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
884 file->link_u.idev = pool_talloc(
885 flist->hlink_pool, struct idev, 1,
889 if (file->link_u.idev) {
890 file->F_DEV = st.st_dev;
891 file->F_INODE = st.st_ino;
896 file->dirname = lastdir = bp;
897 lastdir_len = dirname_len - 1;
898 memcpy(bp, dirname, dirname_len - 1);
902 file->dirname = dirname;
905 memcpy(bp, basename, basename_len);
908 #ifdef HAVE_STRUCT_STAT_ST_RDEV
909 if (preserve_devices && IS_DEVICE(st.st_mode))
910 file->u.rdev = st.st_rdev;
916 memcpy(bp, linkname, linkname_len);
923 file_checksum(thisname, bp, st.st_size);
927 file->basedir = flist_dir;
929 if (!S_ISDIR(st.st_mode))
930 stats.total_size += st.st_size;
936 void send_file_name(int f, struct file_list *flist, char *fname,
937 int recursive, unsigned short base_flags)
939 struct file_struct *file;
940 char fbuf[MAXPATHLEN];
942 /* f is set to -1 when calculating deletion file list */
943 file = make_file(fname, flist,
944 f == -1 && delete_excluded? SERVER_EXCLUDES : ALL_EXCLUDES);
949 maybe_emit_filelist_progress(flist);
953 if (file->basename[0]) {
954 flist->files[flist->count++] = file;
955 send_file_entry(file, f, base_flags);
958 if (recursive && S_ISDIR(file->mode)
959 && !(file->flags & FLAG_MOUNT_POINT)) {
960 struct exclude_list_struct last_list = local_exclude_list;
961 local_exclude_list.head = local_exclude_list.tail = NULL;
962 send_directory(f, flist, f_name_to(file, fbuf));
964 rprintf(FINFO, "[%s] popping %sexclude list\n",
965 who_am_i(), local_exclude_list.debug_type);
967 clear_exclude_list(&local_exclude_list);
968 local_exclude_list = last_list;
973 static void send_directory(int f, struct file_list *flist, char *dir)
977 char fname[MAXPATHLEN];
983 io_error |= IOERR_GENERAL;
984 rsyserr(FERROR, errno, "opendir %s failed", full_fname(dir));
988 offset = strlcpy(fname, dir, MAXPATHLEN);
990 if (offset >= MAXPATHLEN || p[-1] != '/') {
991 if (offset >= MAXPATHLEN - 1) {
992 io_error |= IOERR_GENERAL;
993 rprintf(FERROR, "skipping long-named directory: %s\n",
1003 if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
1004 < MAXPATHLEN - offset) {
1005 add_exclude_file(&local_exclude_list, fname,
1006 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
1008 io_error |= IOERR_GENERAL;
1010 "cannot cvs-exclude in long-named directory %s\n",
1015 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1016 char *dname = d_name(di);
1017 if (dname[0] == '.' && (dname[1] == '\0'
1018 || (dname[1] == '.' && dname[2] == '\0')))
1020 if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset)
1021 send_file_name(f, flist, fname, recurse, 0);
1023 io_error |= IOERR_GENERAL;
1025 "cannot send long-named file %s\n",
1030 io_error |= IOERR_GENERAL;
1031 rsyserr(FERROR, errno, "readdir(%s)", dir);
1039 * This function is normally called by the sender, but the receiver also
1040 * uses it to construct its own file list if --delete has been specified.
1041 * The delete_files() function in receiver.c sets f to -1 so that we just
1042 * construct the file list in memory without sending it over the wire. It
1043 * also has the side-effect of ignoring user-excludes if delete_excluded
1044 * is set (so that the delete list includes user-excluded files).
1046 struct file_list *send_file_list(int f, int argc, char *argv[])
1050 char *p, *dir, olddir[sizeof curr_dir];
1051 char lastpath[MAXPATHLEN] = "";
1052 struct file_list *flist;
1056 if (show_filelist_p() && f != -1)
1057 start_filelist_progress("building file list");
1059 start_write = stats.total_written;
1061 flist = flist_new(f == -1 ? WITHOUT_HLINK : WITH_HLINK,
1065 io_start_buffering_out();
1066 if (filesfrom_fd >= 0) {
1067 if (argv[0] && !push_dir(argv[0])) {
1068 rsyserr(FERROR, errno, "push_dir %s failed",
1069 full_fname(argv[0]));
1070 exit_cleanup(RERR_FILESELECT);
1077 char fname2[MAXPATHLEN];
1078 char *fname = fname2;
1081 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1083 sanitize_path(fname, fname, "", 0);
1087 strlcpy(fname, *argv++, MAXPATHLEN);
1089 sanitize_path(fname, fname, "", 0);
1093 if (fname[l - 1] == '/') {
1094 if (l == 2 && fname[0] == '.') {
1095 /* Turn "./" into just "." rather than "./." */
1097 } else if (l < MAXPATHLEN) {
1103 if (link_stat(fname, &st, keep_dirlinks) != 0) {
1105 io_error |= IOERR_GENERAL;
1106 rsyserr(FERROR, errno, "link_stat %s failed",
1112 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
1113 rprintf(FINFO, "skipping directory %s\n", fname);
1120 if (!relative_paths) {
1121 p = strrchr(fname, '/');
1130 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1131 /* this ensures we send the intermediate directories,
1132 thus getting their permissions right */
1133 char *lp = lastpath, *fn = fname, *slash = fname;
1135 /* Skip any initial directories in our path that we
1136 * have in common with lastpath. */
1137 while (*fn && *lp == *fn) {
1143 if (fn != p || (*lp && *lp != '/')) {
1144 int copy_links_saved = copy_links;
1145 int recurse_saved = recurse;
1146 copy_links = copy_unsafe_links;
1147 /* set recurse to 1 to prevent make_file
1148 * from ignoring directory, but still
1149 * turn off the recursive parameter to
1152 while ((slash = strchr(slash+1, '/')) != 0) {
1154 send_file_name(f, flist, fname, 0, 0);
1157 copy_links = copy_links_saved;
1158 recurse = recurse_saved;
1160 strlcpy(lastpath, fname, sizeof lastpath);
1169 static char *lastdir;
1170 static int lastdir_len;
1172 strcpy(olddir, curr_dir); /* can't overflow */
1174 if (!push_dir(dir)) {
1175 io_error |= IOERR_GENERAL;
1176 rsyserr(FERROR, errno, "push_dir %s failed",
1181 if (lastdir && strcmp(lastdir, dir) == 0) {
1182 flist_dir = lastdir;
1183 flist_dir_len = lastdir_len;
1185 flist_dir = lastdir = strdup(dir);
1186 flist_dir_len = lastdir_len = strlen(dir);
1190 if (one_file_system)
1191 set_filesystem(fname);
1193 send_file_name(f, flist, fname, recurse, XMIT_TOP_DIR);
1198 if (!pop_dir(olddir)) {
1199 rsyserr(FERROR, errno, "pop_dir %s failed",
1201 exit_cleanup(RERR_FILESELECT);
1207 send_file_entry(NULL, f, 0);
1209 if (show_filelist_p())
1210 finish_filelist_progress(flist);
1213 if (flist->hlink_pool) {
1214 pool_destroy(flist->hlink_pool);
1215 flist->hlink_pool = NULL;
1218 clean_flist(flist, 0, 0);
1221 /* Now send the uid/gid list. This was introduced in
1222 * protocol version 15 */
1225 /* send the io_error flag */
1226 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1229 stats.flist_size = stats.total_written - start_write;
1230 stats.num_files = flist->count;
1234 output_flist(flist);
1237 rprintf(FINFO, "send_file_list done\n");
1243 struct file_list *recv_file_list(int f)
1245 struct file_list *flist;
1246 unsigned short flags;
1249 if (show_filelist_p())
1250 start_filelist_progress("receiving file list");
1252 start_read = stats.total_read;
1254 flist = flist_new(WITH_HLINK, "recv_file_list");
1257 flist->malloced = 1000;
1258 flist->files = new_array(struct file_struct *, flist->malloced);
1263 while ((flags = read_byte(f)) != 0) {
1264 int i = flist->count;
1266 flist_expand(flist);
1268 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1269 flags |= read_byte(f) << 8;
1270 receive_file_entry(&flist->files[i], flags, flist, f);
1272 if (S_ISREG(flist->files[i]->mode))
1273 stats.total_size += flist->files[i]->length;
1277 maybe_emit_filelist_progress(flist);
1280 rprintf(FINFO, "recv_file_name(%s)\n",
1281 f_name(flist->files[i]));
1284 receive_file_entry(NULL, 0, NULL, 0); /* Signal that we're done. */
1287 rprintf(FINFO, "received %d names\n", flist->count);
1289 if (show_filelist_p())
1290 finish_filelist_progress(flist);
1292 clean_flist(flist, relative_paths, 1);
1295 /* Now send the uid/gid list. This was introduced in
1296 * protocol version 15 */
1297 recv_uid_list(f, flist);
1299 /* Recv the io_error flag */
1300 if (lp_ignore_errors(module_id) || ignore_errors)
1303 io_error |= read_int(f);
1307 output_flist(flist);
1311 for (i = 0; i < flist->count; i++)
1312 list_file_entry(flist->files[i]);
1316 rprintf(FINFO, "recv_file_list done\n");
1318 stats.flist_size = stats.total_read - start_read;
1319 stats.num_files = flist->count;
1324 out_of_memory("recv_file_list");
1325 return NULL; /* not reached */
1329 int file_compare(struct file_struct **file1, struct file_struct **file2)
1331 struct file_struct *f1 = *file1;
1332 struct file_struct *f2 = *file2;
1334 if (!f1->basename && !f2->basename)
1340 if (f1->dirname == f2->dirname)
1341 return u_strcmp(f1->basename, f2->basename);
1342 return f_name_cmp(f1, f2);
1346 int flist_find(struct file_list *flist, struct file_struct *f)
1348 int low = 0, high = flist->count - 1;
1350 while (high >= 0 && !flist->files[high]->basename) high--;
1355 while (low != high) {
1356 int mid = (low + high) / 2;
1357 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1359 return flist_up(flist, mid);
1366 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1367 return flist_up(flist, low);
1372 * Free up any resources a file_struct has allocated
1373 * and clear the file.
1375 void clear_file(int i, struct file_list *flist)
1377 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1378 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1379 memset(flist->files[i], 0, file_struct_len);
1384 * allocate a new file list
1386 struct file_list *flist_new(int with_hlink, char *msg)
1388 struct file_list *flist;
1390 flist = new(struct file_list);
1394 memset(flist, 0, sizeof (struct file_list));
1396 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1397 out_of_memory, POOL_INTERN)))
1400 #if SUPPORT_HARD_LINKS
1401 if (with_hlink && preserve_hard_links) {
1402 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1403 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1412 * free up all elements in a flist
1414 void flist_free(struct file_list *flist)
1416 pool_destroy(flist->file_pool);
1417 pool_destroy(flist->hlink_pool);
1424 * This routine ensures we don't have any duplicate names in our file list.
1425 * duplicate names can cause corruption because of the pipelining
1427 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1431 if (!flist || flist->count == 0)
1434 qsort(flist->files, flist->count,
1435 sizeof flist->files[0], (int (*)())file_compare);
1437 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1438 if (flist->files[i]->basename) {
1443 while (++i < flist->count) {
1444 if (!flist->files[i]->basename)
1446 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1447 if (verbose > 1 && !am_server) {
1449 "removing duplicate name %s from file list %d\n",
1450 f_name(flist->files[i]), i);
1452 /* Make sure that if we unduplicate '.', that we don't
1453 * lose track of a user-specified starting point (or
1454 * else deletions will mysteriously fail with -R). */
1455 if (flist->files[i]->flags & FLAG_TOP_DIR)
1456 flist->files[prev_i]->flags |= FLAG_TOP_DIR;
1458 clear_file(i, flist);
1464 /* we need to strip off the root directory in the case
1465 of relative paths, but this must be done _after_
1466 the sorting phase */
1467 for (i = 0; i < flist->count; i++) {
1468 if (flist->files[i]->dirname &&
1469 flist->files[i]->dirname[0] == '/') {
1470 memmove(&flist->files[i]->dirname[0],
1471 &flist->files[i]->dirname[1],
1472 strlen(flist->files[i]->dirname));
1475 if (flist->files[i]->dirname &&
1476 !flist->files[i]->dirname[0]) {
1477 flist->files[i]->dirname = NULL;
1483 static void output_flist(struct file_list *flist)
1485 char uidbuf[16], gidbuf[16];
1486 struct file_struct *file;
1489 for (i = 0; i < flist->count; i++) {
1490 file = flist->files[i];
1491 if ((am_root || am_sender) && preserve_uid)
1492 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1495 if (preserve_gid && file->gid != GID_NONE)
1496 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1499 rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f%s%s\n",
1500 who_am_i(), i, NS(file->basedir), NS(file->dirname),
1501 NS(file->basename), (int)file->mode,
1502 (double)file->length, uidbuf, gidbuf);
1507 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1509 /* Compare the names of two file_struct entities, just like strcmp()
1510 * would do if it were operating on the joined strings. We assume
1511 * that there are no 0-length strings.
1513 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1516 const uchar *c1, *c2;
1517 enum fnc_state state1, state2;
1519 if (!f1 || !f1->basename) {
1520 if (!f2 || !f2->basename)
1524 if (!f2 || !f2->basename)
1527 if (!(c1 = (uchar*)f1->dirname)) {
1529 c1 = (uchar*)f1->basename;
1535 if (!(c2 = (uchar*)f2->dirname)) {
1537 c2 = (uchar*)f2->basename;
1545 if ((dif = (int)*c1 - (int)*c2) != 0)
1555 c1 = (uchar*)f1->basename;
1569 c2 = (uchar*)f2->basename;
1583 /* Return a copy of the full filename of a flist entry, using the indicated
1584 * buffer. No size-checking is done because we checked the size when creating
1585 * the file_struct entry.
1587 char *f_name_to(struct file_struct *f, char *fbuf)
1589 if (!f || !f->basename)
1593 int len = strlen(f->dirname);
1594 memcpy(fbuf, f->dirname, len);
1596 strcpy(fbuf + len + 1, f->basename);
1598 strcpy(fbuf, f->basename);
1603 /* Like f_name_to(), but we rotate through 5 static buffers of our own.
1605 char *f_name(struct file_struct *f)
1607 static char names[5][MAXPATHLEN];
1608 static unsigned int n;
1610 n = (n + 1) % (sizeof names / sizeof names[0]);
1612 return f_name_to(f, names[n]);