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;
37 extern int always_checksum;
39 extern int ignore_errors;
40 extern int numeric_ids;
42 extern int cvs_exclude;
45 extern char curr_dir[MAXPATHLEN];
46 extern char *files_from;
47 extern int filesfrom_fd;
49 extern int one_file_system;
50 extern int keep_dirlinks;
51 extern int preserve_links;
52 extern int preserve_hard_links;
53 extern int preserve_perms;
54 extern int preserve_devices;
55 extern int preserve_uid;
56 extern int preserve_gid;
57 extern int relative_paths;
58 extern int implied_dirs;
59 extern int copy_links;
60 extern int copy_unsafe_links;
61 extern int protocol_version;
62 extern int sanitize_paths;
63 extern int delete_excluded;
64 extern int orig_umask;
67 extern struct exclude_list_struct exclude_list;
68 extern struct exclude_list_struct server_exclude_list;
69 extern struct exclude_list_struct local_exclude_list;
73 static char empty_sum[MD4_SUM_LENGTH];
74 static unsigned int file_struct_len;
76 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
77 static void output_flist(struct file_list *flist);
83 /* Figure out how big the file_struct is without trailing padding */
84 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
88 static int show_filelist_p(void)
90 return verbose && (recurse || files_from) && !am_server;
93 static void start_filelist_progress(char *kind)
95 rprintf(FINFO, "%s ... ", kind);
96 if (verbose > 1 || do_progress)
102 static void emit_filelist_progress(const struct file_list *flist)
104 rprintf(FINFO, " %d files...\r", flist->count);
108 static void maybe_emit_filelist_progress(const struct file_list *flist)
110 if (do_progress && show_filelist_p() && (flist->count % 100) == 0)
111 emit_filelist_progress(flist);
115 static void finish_filelist_progress(const struct file_list *flist)
118 /* This overwrites the progress line */
119 rprintf(FINFO, "%d file%sto consider\n",
120 flist->count, flist->count == 1 ? " " : "s ");
122 rprintf(FINFO, "done\n");
125 void show_flist_stats(void)
131 static void list_file_entry(struct file_struct *f)
136 /* this can happen if duplicate names were removed */
140 permstring(perms, f->mode);
143 if (preserve_links && S_ISLNK(f->mode)) {
144 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
146 (double)f->length, timestring(f->modtime),
147 f_name(f), f->u.link);
151 rprintf(FINFO, "%s %11.0f %s %s\n",
153 (double)f->length, timestring(f->modtime),
160 * Stat either a symlink or its referent, depending on the settings of
161 * copy_links, copy_unsafe_links, etc.
163 * @retval -1 on error
165 * @retval 0 for success
167 * @post If @p path is a symlink, then @p linkbuf (of size @c
168 * MAXPATHLEN) contains the symlink target.
170 * @post @p buffer contains information about the link or the
171 * referrent as appropriate, if they exist.
173 int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
177 return do_stat(path, buffer);
178 if (link_stat(path, buffer, keep_dirlinks) < 0)
180 if (S_ISLNK(buffer->st_mode)) {
181 int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1);
185 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
187 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
190 return do_stat(path, buffer);
195 return do_stat(path, buffer);
199 int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks)
203 return do_stat(path, buffer);
204 if (do_lstat(path, buffer) < 0)
206 if (follow_dirlinks && S_ISLNK(buffer->st_mode)) {
208 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
213 return do_stat(path, buffer);
218 * This function is used to check if a file should be included/excluded
219 * from the list of files based on its name and type etc. The value of
220 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
222 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
226 #if 0 /* This currently never happens, so avoid a useless compare. */
227 if (exclude_level == NO_EXCLUDES)
231 /* never exclude '.', even if somebody does --exclude '*' */
232 if (fname[0] == '.' && !fname[1])
234 /* Handle the -R version of the '.' dir. */
235 if (fname[0] == '/') {
236 int len = strlen(fname);
237 if (fname[len-1] == '.' && fname[len-2] == '/')
241 if (server_exclude_list.head
242 && check_exclude(&server_exclude_list, fname, is_dir) < 0)
244 if (exclude_level != ALL_EXCLUDES)
246 if (exclude_list.head
247 && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
249 if (local_exclude_list.head
250 && check_exclude(&local_exclude_list, fname, is_dir) < 0)
255 /* used by the one_file_system code */
256 static dev_t filesystem_dev;
258 static void set_filesystem(char *fname)
261 if (do_stat(fname, &st) != 0)
263 filesystem_dev = st.st_dev;
267 static int to_wire_mode(mode_t mode)
270 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
271 return (mode & ~(_S_IFMT)) | 0120000;
276 static mode_t from_wire_mode(int mode)
278 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
279 return (mode & ~(_S_IFMT)) | _S_IFLNK;
284 static void send_directory(int f, struct file_list *flist, char *dir);
286 static char *flist_dir;
287 static int flist_dir_len;
291 * Make sure @p flist is big enough to hold at least @p flist->count
294 void flist_expand(struct file_list *flist)
296 struct file_struct **new_ptr;
298 if (flist->count < flist->malloced)
301 if (flist->malloced < FLIST_START)
302 flist->malloced = FLIST_START;
303 else if (flist->malloced >= FLIST_LINEAR)
304 flist->malloced += FLIST_LINEAR;
306 flist->malloced *= 2;
309 * In case count jumped or we are starting the list
310 * with a known size just set it.
312 if (flist->malloced < flist->count)
313 flist->malloced = flist->count;
315 new_ptr = realloc_array(flist->files, struct file_struct *,
319 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
321 (double)sizeof flist->files[0] * flist->malloced,
322 (new_ptr == flist->files) ? " not" : "");
325 flist->files = new_ptr;
328 out_of_memory("flist_expand");
331 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
333 unsigned short flags;
334 static time_t modtime;
338 static uint32 rdev_major;
341 static char lastname[MAXPATHLEN];
342 char fname[MAXPATHLEN];
350 modtime = 0, mode = 0;
351 dev = 0, rdev = makedev(0, 0);
358 io_write_phase = "send_file_entry";
360 f_name_to(file, fname);
364 if (file->mode == mode)
365 flags |= XMIT_SAME_MODE;
368 if (preserve_devices) {
369 if (protocol_version < 28) {
370 if (IS_DEVICE(mode)) {
371 if (file->u.rdev == rdev)
372 flags |= XMIT_SAME_RDEV_pre28;
376 rdev = makedev(0, 0);
377 } else if (IS_DEVICE(mode)) {
379 if ((uint32)major(rdev) == rdev_major)
380 flags |= XMIT_SAME_RDEV_MAJOR;
382 rdev_major = major(rdev);
383 if ((uint32)minor(rdev) <= 0xFFu)
384 flags |= XMIT_RDEV_MINOR_IS_SMALL;
387 if (file->uid == uid)
388 flags |= XMIT_SAME_UID;
391 if (file->gid == gid)
392 flags |= XMIT_SAME_GID;
395 if (file->modtime == modtime)
396 flags |= XMIT_SAME_TIME;
398 modtime = file->modtime;
400 #if SUPPORT_HARD_LINKS
401 if (file->link_u.idev) {
402 if (file->F_DEV == dev) {
403 if (protocol_version >= 28)
404 flags |= XMIT_SAME_DEV;
407 flags |= XMIT_HAS_IDEV_DATA;
412 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
414 l2 = strlen(fname+l1);
417 flags |= XMIT_SAME_NAME;
419 flags |= XMIT_LONG_NAME;
421 /* We must make sure we don't send a zero flag byte or the
422 * other end will terminate the flist transfer. Note that
423 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
424 * it's harmless way to add a bit to the first flag byte. */
425 if (protocol_version >= 28) {
426 if (!flags && !S_ISDIR(mode))
427 flags |= XMIT_TOP_DIR;
428 if ((flags & 0xFF00) || !flags) {
429 flags |= XMIT_EXTENDED_FLAGS;
430 write_byte(f, flags);
431 write_byte(f, flags >> 8);
433 write_byte(f, flags);
435 if (!(flags & 0xFF) && !S_ISDIR(mode))
436 flags |= XMIT_TOP_DIR;
438 flags |= XMIT_LONG_NAME;
439 write_byte(f, flags);
441 if (flags & XMIT_SAME_NAME)
443 if (flags & XMIT_LONG_NAME)
447 write_buf(f, fname + l1, l2);
449 write_longint(f, file->length);
450 if (!(flags & XMIT_SAME_TIME))
451 write_int(f, modtime);
452 if (!(flags & XMIT_SAME_MODE))
453 write_int(f, to_wire_mode(mode));
454 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
459 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
464 if (preserve_devices && IS_DEVICE(mode)) {
465 if (protocol_version < 28) {
466 if (!(flags & XMIT_SAME_RDEV_pre28))
467 write_int(f, (int)rdev);
469 if (!(flags & XMIT_SAME_RDEV_MAJOR))
470 write_int(f, major(rdev));
471 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
472 write_byte(f, minor(rdev));
474 write_int(f, minor(rdev));
479 if (preserve_links && S_ISLNK(mode)) {
480 int len = strlen(file->u.link);
482 write_buf(f, file->u.link, len);
486 #if SUPPORT_HARD_LINKS
487 if (flags & XMIT_HAS_IDEV_DATA) {
488 if (protocol_version < 26) {
489 /* 32-bit dev_t and ino_t */
491 write_int(f, file->F_INODE);
493 /* 64-bit dev_t and ino_t */
494 if (!(flags & XMIT_SAME_DEV))
495 write_longint(f, dev);
496 write_longint(f, file->F_INODE);
501 if (always_checksum) {
505 else if (protocol_version < 28) {
506 /* Prior to 28, we sent a useless set of nulls. */
512 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
516 strlcpy(lastname, fname, MAXPATHLEN);
518 io_write_phase = "unknown";
523 void receive_file_entry(struct file_struct **fptr, unsigned short flags,
524 struct file_list *flist, int f)
526 static time_t modtime;
530 static uint32 rdev_major;
533 static char lastname[MAXPATHLEN], *lastdir;
534 static int lastdir_depth, lastdir_len = -1;
535 char thisname[MAXPATHLEN];
536 unsigned int l1 = 0, l2 = 0;
537 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
539 char *basename, *dirname, *bp;
540 struct file_struct *file;
543 modtime = 0, mode = 0;
544 dev = 0, rdev = makedev(0, 0);
552 if (flags & XMIT_SAME_NAME)
555 if (flags & XMIT_LONG_NAME)
560 if (l2 >= MAXPATHLEN - l1) {
562 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
563 flags, l1, l2, lastname);
564 overflow("receive_file_entry");
567 strlcpy(thisname, lastname, l1 + 1);
568 read_sbuf(f, &thisname[l1], l2);
569 thisname[l1 + l2] = 0;
571 strlcpy(lastname, thisname, MAXPATHLEN);
573 clean_fname(thisname, 0);
576 sanitize_path(thisname, thisname, "", 0);
578 if ((basename = strrchr(thisname, '/')) != NULL) {
579 dirname_len = ++basename - thisname; /* counts future '\0' */
580 if (lastdir_len == dirname_len - 1
581 && strncmp(thisname, lastdir, lastdir_len) == 0) {
583 dirname_len = 0; /* indicates no copy is needed */
591 basename_len = strlen(basename) + 1; /* count the '\0' */
593 file_length = read_longint(f);
594 if (!(flags & XMIT_SAME_TIME))
595 modtime = (time_t)read_int(f);
596 if (!(flags & XMIT_SAME_MODE))
597 mode = from_wire_mode(read_int(f));
599 if (preserve_uid && !(flags & XMIT_SAME_UID))
600 uid = (uid_t)read_int(f);
601 if (preserve_gid && !(flags & XMIT_SAME_GID))
602 gid = (gid_t)read_int(f);
604 if (preserve_devices) {
605 if (protocol_version < 28) {
606 if (IS_DEVICE(mode)) {
607 if (!(flags & XMIT_SAME_RDEV_pre28))
608 rdev = (dev_t)read_int(f);
610 rdev = makedev(0, 0);
611 } else if (IS_DEVICE(mode)) {
613 if (!(flags & XMIT_SAME_RDEV_MAJOR))
614 rdev_major = read_int(f);
615 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
616 rdev_minor = read_byte(f);
618 rdev_minor = read_int(f);
619 rdev = makedev(rdev_major, rdev_minor);
624 if (preserve_links && S_ISLNK(mode)) {
625 linkname_len = read_int(f) + 1; /* count the '\0' */
626 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
627 rprintf(FERROR, "overflow: linkname_len=%d\n",
629 overflow("receive_file_entry");
636 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
638 alloc_len = file_struct_len + dirname_len + basename_len
639 + linkname_len + sum_len;
640 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
642 file = *fptr = (struct file_struct *)bp;
643 memset(bp, 0, file_struct_len);
644 bp += file_struct_len;
646 file->flags = flags & XMIT_TOP_DIR ? FLAG_TOP_DIR : 0;
647 file->modtime = modtime;
648 file->length = file_length;
654 file->dirname = lastdir = bp;
655 lastdir_len = dirname_len - 1;
656 memcpy(bp, dirname, dirname_len - 1);
660 lastdir_depth = count_dir_elements(lastdir);
662 file->dirname = dirname;
665 memcpy(bp, basename, basename_len);
668 if (preserve_devices && IS_DEVICE(mode))
674 read_sbuf(f, bp, linkname_len - 1);
676 sanitize_path(bp, bp, "", lastdir_depth);
681 #if SUPPORT_HARD_LINKS
682 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
683 flags |= XMIT_HAS_IDEV_DATA;
684 if (flags & XMIT_HAS_IDEV_DATA) {
686 if (protocol_version < 26) {
690 if (!(flags & XMIT_SAME_DEV))
691 dev = read_longint(f);
692 inode = read_longint(f);
694 if (flist->hlink_pool) {
695 file->link_u.idev = pool_talloc(flist->hlink_pool,
696 struct idev, 1, "inode_table");
697 file->F_INODE = inode;
703 if (always_checksum) {
706 file->u.sum = sum = bp;
708 } else if (protocol_version < 28) {
709 /* Prior to 28, we get a useless set of nulls. */
715 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
719 if (!preserve_perms) {
720 /* set an appropriate set of permissions based on original
721 * permissions and umask. This emulates what GNU cp does */
722 file->mode &= ~orig_umask;
728 * Create a file_struct for a named file by reading its stat()
729 * information and performing extensive checks against global
732 * @return the new file, or NULL if there was an error or this file
733 * should be excluded.
735 * @todo There is a small optimization opportunity here to avoid
736 * stat()ing the file in some circumstances, which has a certain cost.
737 * We are called immediately after doing readdir(), and so we may
738 * already know the d_type of the file. We could for example avoid
739 * statting directories if we're not recursing, but this is not a very
740 * important case. Some systems may not have d_type.
742 struct file_struct *make_file(char *fname, struct file_list *flist,
745 static char *lastdir;
746 static int lastdir_len = -1;
747 struct file_struct *file;
749 char sum[SUM_LENGTH];
750 char thisname[MAXPATHLEN];
751 char linkname[MAXPATHLEN];
752 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
753 char *basename, *dirname, *bp;
754 unsigned short flags = 0;
756 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
759 if (strlcpy(thisname, fname, sizeof thisname)
760 >= sizeof thisname - flist_dir_len) {
761 rprintf(FINFO, "skipping overly long name: %s\n", fname);
764 clean_fname(thisname, 0);
766 sanitize_path(thisname, thisname, "", 0);
768 memset(sum, 0, SUM_LENGTH);
770 if (readlink_stat(thisname, &st, linkname) != 0) {
771 int save_errno = errno;
772 /* See if file is excluded before reporting an error. */
773 if (exclude_level != NO_EXCLUDES
774 && check_exclude_file(thisname, 0, exclude_level))
776 if (save_errno == ENOENT) {
778 /* Avoid "vanished" error if symlink points nowhere. */
779 if (copy_links && do_lstat(thisname, &st) == 0
780 && S_ISLNK(st.st_mode)) {
781 io_error |= IOERR_GENERAL;
782 rprintf(FERROR, "symlink has no referent: %s\n",
783 full_fname(thisname));
787 enum logcode c = am_daemon && protocol_version < 28
789 io_error |= IOERR_VANISHED;
790 rprintf(c, "file has vanished: %s\n",
791 full_fname(thisname));
794 io_error |= IOERR_GENERAL;
795 rsyserr(FERROR, save_errno, "readlink %s failed",
796 full_fname(thisname));
801 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
802 if (exclude_level == NO_EXCLUDES)
805 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
806 rprintf(FINFO, "skipping directory %s\n", thisname);
810 /* We only care about directories because we need to avoid recursing
811 * into a mount-point directory, not to avoid copying a symlinked
812 * file if -L (or similar) was specified. */
813 if (one_file_system && st.st_dev != filesystem_dev
814 && S_ISDIR(st.st_mode))
815 flags |= FLAG_MOUNT_POINT;
817 if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level))
820 if (lp_ignore_nonreadable(module_id) && access(thisname, R_OK) != 0)
826 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
827 who_am_i(), thisname, exclude_level);
830 if ((basename = strrchr(thisname, '/')) != NULL) {
831 dirname_len = ++basename - thisname; /* counts future '\0' */
832 if (lastdir_len == dirname_len - 1
833 && strncmp(thisname, lastdir, lastdir_len) == 0) {
835 dirname_len = 0; /* indicates no copy is needed */
843 basename_len = strlen(basename) + 1; /* count the '\0' */
846 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
851 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
853 alloc_len = file_struct_len + dirname_len + basename_len
854 + linkname_len + sum_len;
856 bp = pool_alloc(flist->file_pool, alloc_len,
857 "receive_file_entry");
859 if (!(bp = new_array(char, alloc_len)))
860 out_of_memory("receive_file_entry");
863 file = (struct file_struct *)bp;
864 memset(bp, 0, file_struct_len);
865 bp += file_struct_len;
868 file->modtime = st.st_mtime;
869 file->length = st.st_size;
870 file->mode = st.st_mode;
871 file->uid = st.st_uid;
872 file->gid = st.st_gid;
874 #if SUPPORT_HARD_LINKS
875 if (flist && flist->hlink_pool) {
876 if (protocol_version < 28) {
877 if (S_ISREG(st.st_mode))
878 file->link_u.idev = pool_talloc(
879 flist->hlink_pool, struct idev, 1,
882 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
883 file->link_u.idev = pool_talloc(
884 flist->hlink_pool, struct idev, 1,
888 if (file->link_u.idev) {
889 file->F_DEV = st.st_dev;
890 file->F_INODE = st.st_ino;
895 file->dirname = lastdir = bp;
896 lastdir_len = dirname_len - 1;
897 memcpy(bp, dirname, dirname_len - 1);
901 file->dirname = dirname;
904 memcpy(bp, basename, basename_len);
907 #ifdef HAVE_STRUCT_STAT_ST_RDEV
908 if (preserve_devices && IS_DEVICE(st.st_mode))
909 file->u.rdev = st.st_rdev;
915 memcpy(bp, linkname, linkname_len);
922 file_checksum(thisname, bp, st.st_size);
926 file->basedir = flist_dir;
928 if (!S_ISDIR(st.st_mode))
929 stats.total_size += st.st_size;
935 void send_file_name(int f, struct file_list *flist, char *fname,
936 int recursive, unsigned short base_flags)
938 struct file_struct *file;
939 char fbuf[MAXPATHLEN];
941 /* f is set to -1 when calculating deletion file list */
942 file = make_file(fname, flist,
943 f == -1 && delete_excluded? SERVER_EXCLUDES : ALL_EXCLUDES);
948 maybe_emit_filelist_progress(flist);
952 if (file->basename[0]) {
953 flist->files[flist->count++] = file;
954 send_file_entry(file, f, base_flags);
957 if (recursive && S_ISDIR(file->mode)
958 && !(file->flags & FLAG_MOUNT_POINT)) {
959 struct exclude_list_struct last_list = local_exclude_list;
960 local_exclude_list.head = local_exclude_list.tail = NULL;
961 send_directory(f, flist, f_name_to(file, fbuf));
963 rprintf(FINFO, "[%s] popping %sexclude list\n",
964 who_am_i(), local_exclude_list.debug_type);
966 clear_exclude_list(&local_exclude_list);
967 local_exclude_list = last_list;
972 static void send_directory(int f, struct file_list *flist, char *dir)
976 char fname[MAXPATHLEN];
982 io_error |= IOERR_GENERAL;
983 rsyserr(FERROR, errno, "opendir %s failed", full_fname(dir));
987 offset = strlcpy(fname, dir, MAXPATHLEN);
989 if (offset >= MAXPATHLEN || p[-1] != '/') {
990 if (offset >= MAXPATHLEN - 1) {
991 io_error |= IOERR_GENERAL;
992 rprintf(FERROR, "skipping long-named directory: %s\n",
1002 if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
1003 < MAXPATHLEN - offset) {
1004 add_exclude_file(&local_exclude_list, fname,
1005 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
1007 io_error |= IOERR_GENERAL;
1009 "cannot cvs-exclude in long-named directory %s\n",
1014 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1015 char *dname = d_name(di);
1016 if (dname[0] == '.' && (dname[1] == '\0'
1017 || (dname[1] == '.' && dname[2] == '\0')))
1019 if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset)
1020 send_file_name(f, flist, fname, recurse, 0);
1022 io_error |= IOERR_GENERAL;
1024 "cannot send long-named file %s\n",
1029 io_error |= IOERR_GENERAL;
1030 rsyserr(FERROR, errno, "readdir(%s)", dir);
1038 * This function is normally called by the sender, but the receiver also
1039 * uses it to construct its own file list if --delete has been specified.
1040 * The delete_files() function in receiver.c sets f to -1 so that we just
1041 * construct the file list in memory without sending it over the wire. It
1042 * also has the side-effect of ignoring user-excludes if delete_excluded
1043 * is set (so that the delete list includes user-excluded files).
1045 struct file_list *send_file_list(int f, int argc, char *argv[])
1049 char *p, *dir, olddir[sizeof curr_dir];
1050 char lastpath[MAXPATHLEN] = "";
1051 struct file_list *flist;
1055 if (show_filelist_p() && f != -1)
1056 start_filelist_progress("building file list");
1058 start_write = stats.total_written;
1060 flist = flist_new(f == -1 ? WITHOUT_HLINK : WITH_HLINK,
1064 io_start_buffering_out();
1065 if (filesfrom_fd >= 0) {
1066 if (argv[0] && !push_dir(argv[0])) {
1067 rsyserr(FERROR, errno, "push_dir %s failed",
1068 full_fname(argv[0]));
1069 exit_cleanup(RERR_FILESELECT);
1076 char fname2[MAXPATHLEN];
1077 char *fname = fname2;
1080 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1082 sanitize_path(fname, fname, "", 0);
1086 strlcpy(fname, *argv++, MAXPATHLEN);
1088 sanitize_path(fname, fname, "", 0);
1092 if (fname[l - 1] == '/') {
1093 if (l == 2 && fname[0] == '.') {
1094 /* Turn "./" into just "." rather than "./." */
1096 } else if (l < MAXPATHLEN) {
1102 if (link_stat(fname, &st, keep_dirlinks) != 0) {
1104 io_error |= IOERR_GENERAL;
1105 rsyserr(FERROR, errno, "link_stat %s failed",
1111 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
1112 rprintf(FINFO, "skipping directory %s\n", fname);
1119 if (!relative_paths) {
1120 p = strrchr(fname, '/');
1129 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1130 /* this ensures we send the intermediate directories,
1131 thus getting their permissions right */
1132 char *lp = lastpath, *fn = fname, *slash = fname;
1134 /* Skip any initial directories in our path that we
1135 * have in common with lastpath. */
1136 while (*fn && *lp == *fn) {
1142 if (fn != p || (*lp && *lp != '/')) {
1143 int copy_links_saved = copy_links;
1144 int recurse_saved = recurse;
1145 copy_links = copy_unsafe_links;
1146 /* set recurse to 1 to prevent make_file
1147 * from ignoring directory, but still
1148 * turn off the recursive parameter to
1151 while ((slash = strchr(slash+1, '/')) != 0) {
1153 send_file_name(f, flist, fname, 0, 0);
1156 copy_links = copy_links_saved;
1157 recurse = recurse_saved;
1159 strlcpy(lastpath, fname, sizeof lastpath);
1168 static char *lastdir;
1169 static int lastdir_len;
1171 strcpy(olddir, curr_dir); /* can't overflow */
1173 if (!push_dir(dir)) {
1174 io_error |= IOERR_GENERAL;
1175 rsyserr(FERROR, errno, "push_dir %s failed",
1180 if (lastdir && strcmp(lastdir, dir) == 0) {
1181 flist_dir = lastdir;
1182 flist_dir_len = lastdir_len;
1184 flist_dir = lastdir = strdup(dir);
1185 flist_dir_len = lastdir_len = strlen(dir);
1189 if (one_file_system)
1190 set_filesystem(fname);
1192 send_file_name(f, flist, fname, recurse, XMIT_TOP_DIR);
1197 if (!pop_dir(olddir)) {
1198 rsyserr(FERROR, errno, "pop_dir %s failed",
1200 exit_cleanup(RERR_FILESELECT);
1206 send_file_entry(NULL, f, 0);
1208 if (show_filelist_p())
1209 finish_filelist_progress(flist);
1212 if (flist->hlink_pool) {
1213 pool_destroy(flist->hlink_pool);
1214 flist->hlink_pool = NULL;
1217 clean_flist(flist, 0, 0);
1220 /* Now send the uid/gid list. This was introduced in
1221 * protocol version 15 */
1224 /* send the io_error flag */
1225 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1228 stats.flist_size = stats.total_written - start_write;
1229 stats.num_files = flist->count;
1233 output_flist(flist);
1236 rprintf(FINFO, "send_file_list done\n");
1242 struct file_list *recv_file_list(int f)
1244 struct file_list *flist;
1245 unsigned short flags;
1248 if (show_filelist_p())
1249 start_filelist_progress("receiving file list");
1251 start_read = stats.total_read;
1253 flist = flist_new(WITH_HLINK, "recv_file_list");
1256 flist->malloced = 1000;
1257 flist->files = new_array(struct file_struct *, flist->malloced);
1262 while ((flags = read_byte(f)) != 0) {
1263 int i = flist->count;
1265 flist_expand(flist);
1267 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1268 flags |= read_byte(f) << 8;
1269 receive_file_entry(&flist->files[i], flags, flist, f);
1271 if (S_ISREG(flist->files[i]->mode))
1272 stats.total_size += flist->files[i]->length;
1276 maybe_emit_filelist_progress(flist);
1279 rprintf(FINFO, "recv_file_name(%s)\n",
1280 f_name(flist->files[i]));
1283 receive_file_entry(NULL, 0, NULL, 0); /* Signal that we're done. */
1286 rprintf(FINFO, "received %d names\n", flist->count);
1288 if (show_filelist_p())
1289 finish_filelist_progress(flist);
1291 clean_flist(flist, relative_paths, 1);
1294 /* Now send the uid/gid list. This was introduced in
1295 * protocol version 15 */
1296 recv_uid_list(f, flist);
1298 /* Recv the io_error flag */
1299 if (lp_ignore_errors(module_id) || ignore_errors)
1302 io_error |= read_int(f);
1306 output_flist(flist);
1310 for (i = 0; i < flist->count; i++)
1311 list_file_entry(flist->files[i]);
1315 rprintf(FINFO, "recv_file_list done\n");
1317 stats.flist_size = stats.total_read - start_read;
1318 stats.num_files = flist->count;
1323 out_of_memory("recv_file_list");
1324 return NULL; /* not reached */
1328 int file_compare(struct file_struct **file1, struct file_struct **file2)
1330 struct file_struct *f1 = *file1;
1331 struct file_struct *f2 = *file2;
1333 if (!f1->basename && !f2->basename)
1339 if (f1->dirname == f2->dirname)
1340 return u_strcmp(f1->basename, f2->basename);
1341 return f_name_cmp(f1, f2);
1345 int flist_find(struct file_list *flist, struct file_struct *f)
1347 int low = 0, high = flist->count - 1;
1349 while (high >= 0 && !flist->files[high]->basename) high--;
1354 while (low != high) {
1355 int mid = (low + high) / 2;
1356 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1358 return flist_up(flist, mid);
1365 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1366 return flist_up(flist, low);
1371 * Free up any resources a file_struct has allocated
1372 * and clear the file.
1374 void clear_file(int i, struct file_list *flist)
1376 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1377 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1378 memset(flist->files[i], 0, file_struct_len);
1383 * allocate a new file list
1385 struct file_list *flist_new(int with_hlink, char *msg)
1387 struct file_list *flist;
1389 flist = new(struct file_list);
1393 memset(flist, 0, sizeof (struct file_list));
1395 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1396 out_of_memory, POOL_INTERN)))
1399 #if SUPPORT_HARD_LINKS
1400 if (with_hlink && preserve_hard_links) {
1401 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1402 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1411 * free up all elements in a flist
1413 void flist_free(struct file_list *flist)
1415 pool_destroy(flist->file_pool);
1416 pool_destroy(flist->hlink_pool);
1423 * This routine ensures we don't have any duplicate names in our file list.
1424 * duplicate names can cause corruption because of the pipelining
1426 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1430 if (!flist || flist->count == 0)
1433 qsort(flist->files, flist->count,
1434 sizeof flist->files[0], (int (*)())file_compare);
1436 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1437 if (flist->files[i]->basename) {
1442 while (++i < flist->count) {
1443 if (!flist->files[i]->basename)
1445 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1446 if (verbose > 1 && !am_server) {
1448 "removing duplicate name %s from file list %d\n",
1449 f_name(flist->files[i]), i);
1451 /* Make sure that if we unduplicate '.', that we don't
1452 * lose track of a user-specified starting point (or
1453 * else deletions will mysteriously fail with -R). */
1454 if (flist->files[i]->flags & FLAG_TOP_DIR)
1455 flist->files[prev_i]->flags |= FLAG_TOP_DIR;
1457 clear_file(i, flist);
1463 /* we need to strip off the root directory in the case
1464 of relative paths, but this must be done _after_
1465 the sorting phase */
1466 for (i = 0; i < flist->count; i++) {
1467 if (flist->files[i]->dirname &&
1468 flist->files[i]->dirname[0] == '/') {
1469 memmove(&flist->files[i]->dirname[0],
1470 &flist->files[i]->dirname[1],
1471 strlen(flist->files[i]->dirname));
1474 if (flist->files[i]->dirname &&
1475 !flist->files[i]->dirname[0]) {
1476 flist->files[i]->dirname = NULL;
1482 static void output_flist(struct file_list *flist)
1484 char uidbuf[16], gidbuf[16];
1485 struct file_struct *file;
1488 for (i = 0; i < flist->count; i++) {
1489 file = flist->files[i];
1490 if (am_root && preserve_uid)
1491 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1494 if (preserve_gid && file->gid != GID_NONE)
1495 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1498 rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f%s%s\n",
1499 who_am_i(), i, NS(file->basedir), NS(file->dirname),
1500 NS(file->basename), (int)file->mode,
1501 (double)file->length, uidbuf, gidbuf);
1506 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1508 /* Compare the names of two file_struct entities, just like strcmp()
1509 * would do if it were operating on the joined strings. We assume
1510 * that there are no 0-length strings.
1512 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1515 const uchar *c1, *c2;
1516 enum fnc_state state1, state2;
1518 if (!f1 || !f1->basename) {
1519 if (!f2 || !f2->basename)
1523 if (!f2 || !f2->basename)
1526 if (!(c1 = (uchar*)f1->dirname)) {
1528 c1 = (uchar*)f1->basename;
1534 if (!(c2 = (uchar*)f2->dirname)) {
1536 c2 = (uchar*)f2->basename;
1544 if ((dif = (int)*c1 - (int)*c2) != 0)
1554 c1 = (uchar*)f1->basename;
1568 c2 = (uchar*)f2->basename;
1582 /* Return a copy of the full filename of a flist entry, using the indicated
1583 * buffer. No size-checking is done because we checked the size when creating
1584 * the file_struct entry.
1586 char *f_name_to(struct file_struct *f, char *fbuf)
1588 if (!f || !f->basename)
1592 int len = strlen(f->dirname);
1593 memcpy(fbuf, f->dirname, len);
1595 strcpy(fbuf + len + 1, f->basename);
1597 strcpy(fbuf, f->basename);
1602 /* Like f_name_to(), but we rotate through 5 static buffers of our own.
1604 char *f_name(struct file_struct *f)
1606 static char names[5][MAXPATHLEN];
1607 static unsigned int n;
1609 n = (n + 1) % (sizeof names / sizeof names[0]);
1611 return f_name_to(f, names[n]);