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;
35 extern int always_checksum;
37 extern int ignore_errors;
39 extern int cvs_exclude;
42 extern char curr_dir[MAXPATHLEN];
43 extern char *files_from;
44 extern int filesfrom_fd;
46 extern int one_file_system;
47 extern int make_backups;
48 extern int preserve_links;
49 extern int preserve_hard_links;
50 extern int preserve_perms;
51 extern int preserve_devices;
52 extern int preserve_uid;
53 extern int preserve_gid;
54 extern int preserve_times;
55 extern int relative_paths;
56 extern int implied_dirs;
57 extern int copy_links;
58 extern int copy_unsafe_links;
59 extern int protocol_version;
60 extern int sanitize_paths;
62 extern int read_batch;
63 extern int write_batch;
65 extern struct exclude_struct **exclude_list;
66 extern struct exclude_struct **server_exclude_list;
67 extern struct exclude_struct **local_exclude_list;
71 static char empty_sum[MD4_SUM_LENGTH];
73 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
76 static int show_filelist_p(void)
78 return verbose && (recurse || files_from) && !am_server;
81 static void start_filelist_progress(char *kind)
83 rprintf(FINFO, "%s ... ", kind);
84 if ((verbose > 1) || do_progress)
90 static void emit_filelist_progress(const struct file_list *flist)
92 rprintf(FINFO, " %d files...\r", flist->count);
96 static void maybe_emit_filelist_progress(const struct file_list *flist)
98 if (do_progress && show_filelist_p() && ((flist->count % 100) == 0))
99 emit_filelist_progress(flist);
103 static void finish_filelist_progress(const struct file_list *flist)
106 /* This overwrites the progress line */
107 rprintf(FINFO, "%d file%sto consider\n",
108 flist->count, flist->count == 1 ? " " : "s ");
110 rprintf(FINFO, "done\n");
113 void show_flist_stats(void)
119 static void list_file_entry(struct file_struct *f)
124 /* this can happen if duplicate names were removed */
127 permstring(perms, f->mode);
130 if (preserve_links && S_ISLNK(f->mode)) {
131 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
133 (double) f->length, timestring(f->modtime),
134 f_name(f), f->u.link);
137 rprintf(FINFO, "%s %11.0f %s %s\n",
139 (double) f->length, timestring(f->modtime),
145 * Stat either a symlink or its referent, depending on the settings of
146 * copy_links, copy_unsafe_links, etc.
148 * @retval -1 on error
150 * @retval 0 for success
152 * @post If @p path is a symlink, then @p linkbuf (of size @c
153 * MAXPATHLEN) contains the symlink target.
155 * @post @p buffer contains information about the link or the
156 * referrent as appropriate, if they exist.
158 int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
162 return do_stat(path, buffer);
163 if (do_lstat(path, buffer) == -1)
165 if (S_ISLNK(buffer->st_mode)) {
166 int l = readlink((char *) path, linkbuf, MAXPATHLEN - 1);
170 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
172 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
175 return do_stat(path, buffer);
180 return do_stat(path, buffer);
184 int link_stat(const char *path, STRUCT_STAT * buffer)
188 return do_stat(path, buffer);
189 return do_lstat(path, buffer);
191 return do_stat(path, buffer);
196 * This function is used to check if a file should be included/excluded
197 * from the list of files based on its name and type etc. The value of
198 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
200 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
202 #if 0 /* This currently never happens, so avoid a useless compare. */
203 if (exclude_level == NO_EXCLUDES)
207 /* never exclude '.', even if somebody does --exclude '*' */
208 if (fname[0] == '.' && !fname[1])
210 /* Handle the -R version of the '.' dir. */
211 if (fname[0] == '/') {
212 int len = strlen(fname);
213 if (fname[len-1] == '.' && fname[len-2] == '/')
217 if (server_exclude_list
218 && check_exclude(server_exclude_list, fname, is_dir))
220 if (exclude_level != ALL_EXCLUDES)
222 if (exclude_list && check_exclude(exclude_list, fname, is_dir))
224 if (local_exclude_list
225 && check_exclude(local_exclude_list, fname, is_dir))
230 /* used by the one_file_system code */
231 static dev_t filesystem_dev;
233 static void set_filesystem(char *fname)
236 if (link_stat(fname, &st) != 0)
238 filesystem_dev = st.st_dev;
242 static int to_wire_mode(mode_t mode)
245 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
246 return (mode & ~(_S_IFMT)) | 0120000;
251 static mode_t from_wire_mode(int mode)
253 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
254 return (mode & ~(_S_IFMT)) | _S_IFLNK;
255 return (mode_t) mode;
259 static void send_directory(int f, struct file_list *flist, char *dir);
261 static char *flist_dir;
262 static int flist_dir_len;
266 * Make sure @p flist is big enough to hold at least @p flist->count
269 static void flist_expand(struct file_list *flist)
271 if (flist->count >= flist->malloced) {
274 if (flist->malloced < 1000)
275 flist->malloced += 1000;
277 flist->malloced *= 2;
280 new_ptr = realloc_array(flist->files,
281 struct file_struct *,
284 new_ptr = new_array(struct file_struct *,
289 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
291 (double)sizeof(flist->files[0])
293 (new_ptr == flist->files) ? " not" : "");
296 flist->files = (struct file_struct **) new_ptr;
299 out_of_memory("flist_expand");
303 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
305 unsigned short flags;
306 static time_t modtime;
308 static DEV64_T rdev, rdev_high;
312 static char lastname[MAXPATHLEN];
313 char *fname, fbuf[MAXPATHLEN];
321 modtime = 0, mode = 0;
322 rdev = 0, rdev_high = 0, dev = 0;
328 io_write_phase = "send_file_entry";
330 fname = f_name_to(file, fbuf);
334 if (file->mode == mode)
335 flags |= XMIT_SAME_MODE;
338 if (preserve_devices) {
339 if (protocol_version < 28) {
340 if (IS_DEVICE(mode)) {
341 if (file->u.rdev == rdev) {
342 /* Set both flags to simplify the test
343 * when writing the data. */
344 flags |= XMIT_SAME_RDEV_pre28
345 | XMIT_SAME_HIGH_RDEV;
350 } else if (IS_DEVICE(mode)) {
351 if ((file->u.rdev & ~0xFF) == rdev_high)
352 flags |= XMIT_SAME_HIGH_RDEV;
355 rdev_high = rdev & ~0xFF;
359 if (file->uid == uid)
360 flags |= XMIT_SAME_UID;
363 if (file->gid == gid)
364 flags |= XMIT_SAME_GID;
367 if (file->modtime == modtime)
368 flags |= XMIT_SAME_TIME;
370 modtime = file->modtime;
372 #if SUPPORT_HARD_LINKS
373 if (file->link_u.idev) {
374 if (file->F_DEV == dev) {
375 if (protocol_version >= 28)
376 flags |= XMIT_SAME_DEV;
379 flags |= XMIT_HAS_IDEV_DATA;
384 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
386 l2 = strlen(fname+l1);
389 flags |= XMIT_SAME_NAME;
391 flags |= XMIT_LONG_NAME;
393 /* We must make sure we don't send a zero flag byte or the
394 * other end will terminate the flist transfer. Note that
395 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
396 * it's harmless way to add a bit to the first flag byte. */
397 if (protocol_version >= 28) {
398 if (!flags && !S_ISDIR(mode))
399 flags |= XMIT_TOP_DIR;
400 if ((flags & 0xFF00) || !flags) {
401 flags |= XMIT_EXTENDED_FLAGS;
402 write_byte(f, flags);
403 write_byte(f, flags >> 8);
405 write_byte(f, flags);
407 if (!(flags & 0xFF) && !S_ISDIR(mode))
408 flags |= XMIT_TOP_DIR;
410 flags |= XMIT_LONG_NAME;
411 write_byte(f, flags);
413 if (flags & XMIT_SAME_NAME)
415 if (flags & XMIT_LONG_NAME)
419 write_buf(f, fname + l1, l2);
421 write_longint(f, file->length);
422 if (!(flags & XMIT_SAME_TIME))
423 write_int(f, modtime);
424 if (!(flags & XMIT_SAME_MODE))
425 write_int(f, to_wire_mode(mode));
426 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
430 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
434 if (preserve_devices && IS_DEVICE(mode)) {
435 /* If XMIT_SAME_HIGH_RDEV is off, XMIT_SAME_RDEV_pre28 is
437 if (!(flags & XMIT_SAME_HIGH_RDEV))
439 else if (protocol_version >= 28)
444 if (preserve_links && S_ISLNK(mode)) {
445 int len = strlen(file->u.link);
447 write_buf(f, file->u.link, len);
451 #if SUPPORT_HARD_LINKS
452 if (flags & XMIT_HAS_IDEV_DATA) {
453 if (protocol_version < 26) {
454 /* 32-bit dev_t and ino_t */
456 write_int(f, file->F_INODE);
458 /* 64-bit dev_t and ino_t */
459 if (!(flags & XMIT_SAME_DEV))
460 write_longint(f, dev);
461 write_longint(f, file->F_INODE);
466 if (always_checksum) {
470 else if (protocol_version < 28) {
471 /* Prior to 28, we sent a useless set of nulls. */
476 write_buf(f, sum, protocol_version < 21? 2
481 strlcpy(lastname, fname, MAXPATHLEN);
483 io_write_phase = "unknown";
488 void receive_file_entry(struct file_struct **fptr, unsigned short flags, int f)
490 static time_t modtime;
492 static DEV64_T rdev, rdev_high;
496 static char lastname[MAXPATHLEN], *lastdir;
497 static int lastdir_len = -1;
498 char thisname[MAXPATHLEN];
499 unsigned int l1 = 0, l2 = 0;
500 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
501 int idev_len, idev_pad;
503 char *basename, *dirname, *bp;
504 struct file_struct *file;
507 modtime = 0, mode = 0;
508 rdev = 0, rdev_high = 0, dev = 0;
514 if (flags & XMIT_SAME_NAME)
517 if (flags & XMIT_LONG_NAME)
522 if (l2 >= MAXPATHLEN - l1) {
524 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
525 flags, l1, l2, lastname);
526 overflow("receive_file_entry");
529 strlcpy(thisname, lastname, l1 + 1);
530 read_sbuf(f, &thisname[l1], l2);
531 thisname[l1 + l2] = 0;
533 strlcpy(lastname, thisname, MAXPATHLEN);
535 clean_fname(thisname);
538 sanitize_path(thisname, NULL);
540 if ((basename = strrchr(thisname, '/')) != NULL) {
541 dirname_len = ++basename - thisname; /* counts future '\0' */
542 if (lastdir_len == dirname_len - 1
543 && strncmp(thisname, lastdir, lastdir_len) == 0) {
545 dirname_len = 0; /* indicates no copy is needed */
553 basename_len = strlen(basename) + 1; /* count the '\0' */
555 file_length = read_longint(f);
556 if (!(flags & XMIT_SAME_TIME))
557 modtime = (time_t)read_int(f);
558 if (!(flags & XMIT_SAME_MODE))
559 mode = from_wire_mode(read_int(f));
561 if (preserve_uid && !(flags & XMIT_SAME_UID))
562 uid = (uid_t)read_int(f);
563 if (preserve_gid && !(flags & XMIT_SAME_GID))
564 gid = (gid_t)read_int(f);
566 if (preserve_devices) {
567 if (protocol_version < 28) {
568 if (IS_DEVICE(mode)) {
569 if (!(flags & XMIT_SAME_RDEV_pre28))
570 rdev = (DEV64_T)read_int(f);
573 } else if (IS_DEVICE(mode)) {
574 if (!(flags & XMIT_SAME_HIGH_RDEV)) {
575 rdev = (DEV64_T)read_int(f);
576 rdev_high = rdev & ~0xFF;
578 rdev = rdev_high | (DEV64_T)read_byte(f);
583 if (preserve_links && S_ISLNK(mode)) {
584 linkname_len = read_int(f) + 1; /* count the '\0' */
585 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
586 rprintf(FERROR, "overflow: linkname_len=%d\n",
588 overflow("receive_file_entry");
595 #if SUPPORT_HARD_LINKS
596 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
597 flags |= XMIT_HAS_IDEV_DATA;
598 if (flags & XMIT_HAS_IDEV_DATA)
599 idev_len = sizeof (struct idev);
604 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
606 alloc_len = sizeof file[0] + dirname_len + basename_len
607 + linkname_len + sum_len;
609 idev_pad = (4 - (alloc_len % 4)) % 4;
610 alloc_len += idev_pad + idev_len;
613 if (!(bp = new_array(char, alloc_len)))
614 out_of_memory("receive_file_entry");
615 file = *fptr = (struct file_struct *)bp;
616 memset(bp, 0, sizeof file[0]);
617 bp += sizeof file[0];
619 file->flags = flags & XMIT_TOP_DIR ? FLAG_TOP_DIR : 0;
620 file->modtime = modtime;
621 file->length = file_length;
627 file->dirname = lastdir = bp;
628 lastdir_len = dirname_len - 1;
629 memcpy(bp, dirname, dirname_len - 1);
633 file->dirname = dirname;
636 memcpy(bp, basename, basename_len);
639 if (preserve_devices && IS_DEVICE(mode))
645 read_sbuf(f, bp, linkname_len - 1);
647 sanitize_path(bp, lastdir);
652 #if SUPPORT_HARD_LINKS
654 file->link_u.idev = (struct idev *)(bp + idev_pad);
655 bp += idev_pad + idev_len;
656 if (protocol_version < 26) {
658 file->F_INODE = read_int(f);
660 if (!(flags & XMIT_SAME_DEV))
661 dev = read_longint(f);
662 file->F_INODE = read_longint(f);
668 if (always_checksum) {
671 file->u.sum = sum = bp;
673 } else if (protocol_version < 28) {
674 /* Prior to 28, we get a useless set of nulls. */
679 read_buf(f, sum, protocol_version < 21? 2
684 if (!preserve_perms) {
685 extern int orig_umask;
686 /* set an appropriate set of permissions based on original
687 * permissions and umask. This emulates what GNU cp does */
688 file->mode &= ~orig_umask;
694 * Create a file_struct for a named file by reading its stat()
695 * information and performing extensive checks against global
698 * @return the new file, or NULL if there was an error or this file
699 * should be excluded.
701 * @todo There is a small optimization opportunity here to avoid
702 * stat()ing the file in some circumstances, which has a certain cost.
703 * We are called immediately after doing readdir(), and so we may
704 * already know the d_type of the file. We could for example avoid
705 * statting directories if we're not recursing, but this is not a very
706 * important case. Some systems may not have d_type.
708 struct file_struct *make_file(char *fname, int exclude_level)
710 static char *lastdir;
711 static int lastdir_len = -1;
712 struct file_struct *file;
714 char sum[SUM_LENGTH];
715 char thisname[MAXPATHLEN];
716 char linkname[MAXPATHLEN];
717 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
718 int idev_len, idev_pad;
719 char *basename, *dirname, *bp;
720 unsigned short flags = 0;
722 if (strlcpy(thisname, fname, sizeof thisname)
723 >= sizeof thisname - flist_dir_len) {
724 rprintf(FINFO, "skipping overly long name: %s\n", fname);
727 clean_fname(thisname);
729 sanitize_path(thisname, NULL);
731 memset(sum, 0, SUM_LENGTH);
733 if (readlink_stat(thisname, &st, linkname) != 0) {
734 int save_errno = errno;
735 if (errno == ENOENT && exclude_level != NO_EXCLUDES) {
736 /* either symlink pointing nowhere or file that
737 * was removed during rsync run; see if excluded
738 * before reporting an error */
739 if (check_exclude_file(thisname, 0, exclude_level)) {
740 /* file is excluded anyway, ignore silently */
744 io_error |= IOERR_GENERAL;
745 rprintf(FERROR, "readlink %s failed: %s\n",
746 full_fname(thisname), strerror(save_errno));
750 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
751 if (exclude_level == NO_EXCLUDES)
754 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
755 rprintf(FINFO, "skipping directory %s\n", thisname);
759 if (one_file_system && st.st_dev != filesystem_dev) {
760 /* We allow a directory though to preserve the mount point.
761 * However, flag it so that we don't recurse. */
762 if (!S_ISDIR(st.st_mode))
764 flags |= FLAG_MOUNT_POINT;
767 if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level))
770 if (lp_ignore_nonreadable(module_id) && access(thisname, R_OK) != 0)
776 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
777 who_am_i(), thisname, exclude_level);
780 if ((basename = strrchr(thisname, '/')) != NULL) {
781 dirname_len = ++basename - thisname; /* counts future '\0' */
782 if (lastdir_len == dirname_len - 1
783 && strncmp(thisname, lastdir, lastdir_len) == 0) {
785 dirname_len = 0; /* indicates no copy is needed */
793 basename_len = strlen(basename) + 1; /* count the '\0' */
796 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
801 #if SUPPORT_HARD_LINKS
802 if (preserve_hard_links) {
803 idev_len = (protocol_version < 28 ? S_ISREG(st.st_mode)
804 : !S_ISDIR(st.st_mode) && st.st_nlink > 1)
805 ? sizeof (struct idev) : 0;
810 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
812 alloc_len = sizeof file[0] + dirname_len + basename_len
813 + linkname_len + sum_len;
815 idev_pad = (4 - (alloc_len % 4)) % 4;
816 alloc_len += idev_pad + idev_len;
819 if (!(bp = new_array(char, alloc_len)))
820 out_of_memory("receive_file_entry");
821 file = (struct file_struct *)bp;
822 memset(bp, 0, sizeof file[0]);
823 bp += sizeof file[0];
826 file->modtime = st.st_mtime;
827 file->length = st.st_size;
828 file->mode = st.st_mode;
829 file->uid = st.st_uid;
830 file->gid = st.st_gid;
833 file->dirname = lastdir = bp;
834 lastdir_len = dirname_len - 1;
835 memcpy(bp, dirname, dirname_len - 1);
839 file->dirname = dirname;
842 memcpy(bp, basename, basename_len);
845 #ifdef HAVE_STRUCT_STAT_ST_RDEV
846 if (preserve_devices && IS_DEVICE(st.st_mode))
847 file->u.rdev = st.st_rdev;
853 memcpy(bp, linkname, linkname_len);
858 #if SUPPORT_HARD_LINKS
860 file->link_u.idev = (struct idev *)(bp + idev_pad);
861 bp += idev_pad + idev_len;
862 file->F_DEV = st.st_dev;
863 file->F_INODE = st.st_ino;
869 file_checksum(thisname, bp, st.st_size);
873 file->basedir = flist_dir;
875 if (!S_ISDIR(st.st_mode))
876 stats.total_size += st.st_size;
882 void send_file_name(int f, struct file_list *flist, char *fname,
883 int recursive, unsigned short base_flags)
885 struct file_struct *file;
886 char fbuf[MAXPATHLEN];
887 extern int delete_excluded;
889 /* f is set to -1 when calculating deletion file list */
890 file = make_file(fname,
891 f == -1 && delete_excluded? SERVER_EXCLUDES
897 maybe_emit_filelist_progress(flist);
902 file->flags |= FLAG_TOP_DIR;
904 if (file->basename[0]) {
905 flist->files[flist->count++] = file;
906 send_file_entry(file, f, base_flags);
909 if (recursive && S_ISDIR(file->mode)
910 && !(file->flags & FLAG_MOUNT_POINT)) {
911 struct exclude_struct **last_exclude_list = local_exclude_list;
912 send_directory(f, flist, f_name_to(file, fbuf));
913 local_exclude_list = last_exclude_list;
919 static void send_directory(int f, struct file_list *flist, char *dir)
923 char fname[MAXPATHLEN];
929 io_error |= IOERR_GENERAL;
930 rprintf(FERROR, "opendir %s failed: %s\n",
931 full_fname(dir), strerror(errno));
935 offset = strlcpy(fname, dir, MAXPATHLEN);
937 if (offset >= MAXPATHLEN || p[-1] != '/') {
938 if (offset >= MAXPATHLEN - 1) {
939 io_error |= IOERR_GENERAL;
940 rprintf(FERROR, "skipping long-named directory: %s\n",
949 local_exclude_list = NULL;
952 if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
953 < MAXPATHLEN - offset)
954 add_exclude_file(&local_exclude_list,fname,MISSING_OK,ADD_EXCLUDE);
956 io_error |= IOERR_GENERAL;
958 "cannot cvs-exclude in long-named directory %s\n",
963 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
964 char *dname = d_name(di);
965 if (dname[0] == '.' && (dname[1] == '\0'
966 || (dname[1] == '.' && dname[2] == '\0')))
968 if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset)
969 send_file_name(f, flist, fname, recurse, 0);
971 io_error |= IOERR_GENERAL;
973 "cannot send long-named file %s\n",
978 io_error |= IOERR_GENERAL;
979 rprintf(FERROR, "readdir(%s): (%d) %s\n",
980 dir, errno, strerror(errno));
983 if (local_exclude_list)
984 free_exclude_list(&local_exclude_list); /* Zeros pointer too */
991 * The delete_files() function in receiver.c sets f to -1 so that we just
992 * construct the file list in memory without sending it over the wire. It
993 * also has the side-effect of ignoring user-excludes if delete_excluded
994 * is set (so that the delete list includes user-excluded files).
996 struct file_list *send_file_list(int f, int argc, char *argv[])
1000 char *p, *dir, olddir[sizeof curr_dir];
1001 char lastpath[MAXPATHLEN] = "";
1002 struct file_list *flist;
1006 if (show_filelist_p() && f != -1)
1007 start_filelist_progress("building file list");
1009 start_write = stats.total_written;
1011 flist = flist_new();
1014 io_start_buffering_out(f);
1015 if (filesfrom_fd >= 0) {
1016 if (argv[0] && !push_dir(argv[0])) {
1017 rprintf(FERROR, "push_dir %s failed: %s\n",
1018 full_fname(argv[0]), strerror(errno));
1019 exit_cleanup(RERR_FILESELECT);
1026 char fname2[MAXPATHLEN];
1027 char *fname = fname2;
1030 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1032 sanitize_path(fname, NULL);
1036 strlcpy(fname, *argv++, MAXPATHLEN);
1038 sanitize_path(fname, NULL);
1042 if (fname[l - 1] == '/') {
1043 if (l == 2 && fname[0] == '.') {
1044 /* Turn "./" into just "." rather than "./." */
1046 } else if (l < MAXPATHLEN) {
1052 if (link_stat(fname, &st) != 0) {
1054 io_error |= IOERR_GENERAL;
1055 rprintf(FERROR, "link_stat %s failed: %s\n",
1056 full_fname(fname), strerror(errno));
1061 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
1062 rprintf(FINFO, "skipping directory %s\n", fname);
1069 if (!relative_paths) {
1070 p = strrchr(fname, '/');
1079 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1080 /* this ensures we send the intermediate directories,
1081 thus getting their permissions right */
1082 char *lp = lastpath, *fn = fname, *slash = fname;
1084 /* Skip any initial directories in our path that we
1085 * have in common with lastpath. */
1086 while (*fn && *lp == *fn) {
1092 if (fn != p || (*lp && *lp != '/')) {
1093 int copy_links_saved = copy_links;
1094 int recurse_saved = recurse;
1095 copy_links = copy_unsafe_links;
1096 /* set recurse to 1 to prevent make_file
1097 * from ignoring directory, but still
1098 * turn off the recursive parameter to
1101 while ((slash = strchr(slash+1, '/')) != 0) {
1103 send_file_name(f, flist, fname, 0, 0);
1106 copy_links = copy_links_saved;
1107 recurse = recurse_saved;
1109 strlcpy(lastpath, fname, sizeof lastpath);
1118 static char *lastdir;
1119 static int lastdir_len;
1121 strcpy(olddir, curr_dir); /* can't overflow */
1123 if (!push_dir(dir)) {
1124 io_error |= IOERR_GENERAL;
1125 rprintf(FERROR, "push_dir %s failed: %s\n",
1126 full_fname(dir), strerror(errno));
1130 if (lastdir && strcmp(lastdir, dir) == 0) {
1131 flist_dir = lastdir;
1132 flist_dir_len = lastdir_len;
1134 flist_dir = lastdir = strdup(dir);
1135 flist_dir_len = lastdir_len = strlen(dir);
1139 if (one_file_system)
1140 set_filesystem(fname);
1142 send_file_name(f, flist, fname, recurse, XMIT_TOP_DIR);
1147 if (!pop_dir(olddir)) {
1148 rprintf(FERROR, "pop_dir %s failed: %s\n",
1149 full_fname(dir), strerror(errno));
1150 exit_cleanup(RERR_FILESELECT);
1156 send_file_entry(NULL, f, 0);
1158 if (show_filelist_p())
1159 finish_filelist_progress(flist);
1162 clean_flist(flist, 0, 0);
1165 /* Now send the uid/gid list. This was introduced in
1166 * protocol version 15 */
1169 /* send the io_error flag */
1170 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1173 stats.flist_size = stats.total_written - start_write;
1174 stats.num_files = flist->count;
1176 write_batch_flist_info(flist->count, flist->files);
1180 rprintf(FINFO, "send_file_list done\n");
1186 struct file_list *recv_file_list(int f)
1188 struct file_list *flist;
1189 unsigned short flags;
1191 extern int list_only;
1193 if (show_filelist_p())
1194 start_filelist_progress("receiving file list");
1196 start_read = stats.total_read;
1198 flist = new(struct file_list);
1203 flist->malloced = 1000;
1204 flist->files = new_array(struct file_struct *, flist->malloced);
1209 while ((flags = read_byte(f)) != 0) {
1210 int i = flist->count;
1212 flist_expand(flist);
1214 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1215 flags |= read_byte(f) << 8;
1216 receive_file_entry(&flist->files[i], flags, f);
1218 if (S_ISREG(flist->files[i]->mode))
1219 stats.total_size += flist->files[i]->length;
1223 maybe_emit_filelist_progress(flist);
1226 rprintf(FINFO, "recv_file_name(%s)\n",
1227 f_name(flist->files[i]));
1230 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1233 rprintf(FINFO, "received %d names\n", flist->count);
1235 if (show_filelist_p())
1236 finish_filelist_progress(flist);
1238 clean_flist(flist, relative_paths, 1);
1241 /* Now send the uid/gid list. This was introduced in
1242 * protocol version 15 */
1243 recv_uid_list(f, flist);
1246 /* Recv the io_error flag */
1247 if (lp_ignore_errors(module_id) || ignore_errors)
1250 io_error |= read_int(f);
1256 for (i = 0; i < flist->count; i++)
1257 list_file_entry(flist->files[i]);
1261 rprintf(FINFO, "recv_file_list done\n");
1263 stats.flist_size = stats.total_read - start_read;
1264 stats.num_files = flist->count;
1269 out_of_memory("recv_file_list");
1270 return NULL; /* not reached */
1274 int file_compare(struct file_struct **file1, struct file_struct **file2)
1276 struct file_struct *f1 = *file1;
1277 struct file_struct *f2 = *file2;
1279 if (!f1->basename && !f2->basename)
1285 if (f1->dirname == f2->dirname)
1286 return u_strcmp(f1->basename, f2->basename);
1287 return f_name_cmp(f1, f2);
1291 int flist_find(struct file_list *flist, struct file_struct *f)
1293 int low = 0, high = flist->count - 1;
1295 while (high >= 0 && !flist->files[high]->basename) high--;
1300 while (low != high) {
1301 int mid = (low + high) / 2;
1302 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1304 return flist_up(flist, mid);
1311 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1312 return flist_up(flist, low);
1318 * Free up any resources a file_struct has allocated, and optionally free
1321 void free_file(struct file_struct *file, int free_the_struct)
1323 if (free_the_struct)
1326 memset(file, 0, sizeof file[0]);
1331 * allocate a new file list
1333 struct file_list *flist_new(void)
1335 struct file_list *flist;
1337 flist = new(struct file_list);
1339 out_of_memory("send_file_list");
1342 flist->malloced = 0;
1343 flist->files = NULL;
1349 * free up all elements in a flist
1351 void flist_free(struct file_list *flist)
1354 for (i = 1; i < flist->count; i++)
1355 free_file(flist->files[i], FREE_STRUCT);
1362 * This routine ensures we don't have any duplicate names in our file list.
1363 * duplicate names can cause corruption because of the pipelining
1365 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1369 if (!flist || flist->count == 0)
1372 qsort(flist->files, flist->count,
1373 sizeof(flist->files[0]), (int (*)()) file_compare);
1375 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1376 if (flist->files[i]->basename) {
1381 while (++i < flist->count) {
1382 if (!flist->files[i]->basename)
1384 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1385 if (verbose > 1 && !am_server) {
1387 "removing duplicate name %s from file list %d\n",
1388 f_name(flist->files[i]), i);
1390 /* Make sure that if we unduplicate '.', that we don't
1391 * lose track of a user-specified starting point (or
1392 * else deletions will mysteriously fail with -R). */
1393 if (flist->files[i]->flags & FLAG_TOP_DIR)
1394 flist->files[prev_i]->flags |= FLAG_TOP_DIR;
1395 free_file(flist->files[i], CLEAR_STRUCT);
1401 /* we need to strip off the root directory in the case
1402 of relative paths, but this must be done _after_
1403 the sorting phase */
1404 for (i = 0; i < flist->count; i++) {
1405 if (flist->files[i]->dirname &&
1406 flist->files[i]->dirname[0] == '/') {
1407 memmove(&flist->files[i]->dirname[0],
1408 &flist->files[i]->dirname[1],
1409 strlen(flist->files[i]->dirname));
1412 if (flist->files[i]->dirname &&
1413 !flist->files[i]->dirname[0]) {
1414 flist->files[i]->dirname = NULL;
1422 for (i = 0; i < flist->count; i++) {
1423 rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f\n",
1425 NS(flist->files[i]->basedir),
1426 NS(flist->files[i]->dirname),
1427 NS(flist->files[i]->basename),
1428 (int) flist->files[i]->mode,
1429 (double) flist->files[i]->length);
1434 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1436 /* Compare the names of two file_struct entities, just like strcmp()
1437 * would do if it were operating on the joined strings. We assume
1438 * that there are no 0-length strings.
1440 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1443 const uchar *c1, *c2;
1444 enum fnc_state state1, state2;
1446 if (!f1 || !f1->basename) {
1447 if (!f2 || !f2->basename)
1451 if (!f2 || !f2->basename)
1454 if (!(c1 = (uchar*)f1->dirname)) {
1456 c1 = (uchar*)f1->basename;
1459 if (!(c2 = (uchar*)f2->dirname)) {
1461 c2 = (uchar*)f2->basename;
1466 if ((dif = (int)*c1 - (int)*c2) != 0)
1476 c1 = (uchar*)f1->basename;
1490 c2 = (uchar*)f2->basename;
1504 /* Return a copy of the full filename of a flist entry, using the indicated
1505 * buffer. No size-checking is done because we checked the size when creating
1506 * the file_struct entry.
1508 char *f_name_to(struct file_struct *f, char *fbuf)
1510 if (!f || !f->basename)
1514 int len = strlen(f->dirname);
1515 memcpy(fbuf, f->dirname, len);
1517 strcpy(fbuf + len + 1, f->basename);
1519 strcpy(fbuf, f->basename);
1524 /* Like f_name_to(), but we rotate through 5 static buffers of our own.
1526 char *f_name(struct file_struct *f)
1528 static char names[5][MAXPATHLEN];
1529 static unsigned int n;
1531 n = (n + 1) % (sizeof names / sizeof names[0]);
1533 return f_name_to(f, names[n]);