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 * @todo Get rid of the string_area optimization. Efficiently
25 * allocating blocks is the responsibility of the system's malloc
26 * library, not of rsync.
28 * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html
34 extern struct stats stats;
37 extern int do_progress;
39 extern int always_checksum;
41 extern int cvs_exclude;
44 extern char *files_from;
45 extern int filesfrom_fd;
47 extern int one_file_system;
48 extern int make_backups;
49 extern int preserve_links;
50 extern int preserve_hard_links;
51 extern int preserve_perms;
52 extern int preserve_devices;
53 extern int preserve_uid;
54 extern int preserve_gid;
55 extern int preserve_times;
56 extern int relative_paths;
57 extern int implied_dirs;
58 extern int copy_links;
59 extern int copy_unsafe_links;
60 extern int protocol_version;
61 extern int sanitize_paths;
63 extern int read_batch;
64 extern int write_batch;
66 extern struct exclude_struct **exclude_list;
67 extern struct exclude_struct **server_exclude_list;
68 extern struct exclude_struct **local_exclude_list;
72 static struct file_struct null_file;
73 static char empty_sum[MD4_SUM_LENGTH];
75 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
78 static int show_filelist_p(void)
80 return verbose && (recurse || files_from) && !am_server;
83 static void start_filelist_progress(char *kind)
85 rprintf(FINFO, "%s ... ", kind);
86 if ((verbose > 1) || do_progress)
92 static void emit_filelist_progress(const struct file_list *flist)
94 rprintf(FINFO, " %d files...\r", flist->count);
98 static void maybe_emit_filelist_progress(const struct file_list *flist)
100 if (do_progress && show_filelist_p() && ((flist->count % 100) == 0))
101 emit_filelist_progress(flist);
105 static void finish_filelist_progress(const struct file_list *flist)
108 /* This overwrites the progress line */
109 rprintf(FINFO, "%d file%sto consider\n",
110 flist->count, flist->count == 1 ? " " : "s ");
112 rprintf(FINFO, "done\n");
115 void show_flist_stats(void)
121 static struct string_area *string_area_new(int size)
123 struct string_area *a;
127 a = new(struct string_area);
129 out_of_memory("string_area_new");
130 a->current = a->base = new_array(char, size);
132 out_of_memory("string_area_new buffer");
133 a->end = a->base + size;
139 static void string_area_free(struct string_area *a)
141 struct string_area *next;
143 for (; a; a = next) {
149 static char *string_area_malloc(struct string_area **ap, int size)
152 struct string_area *a;
154 /* does the request fit into the current space? */
156 if (a->current + size >= a->end) {
157 /* no; get space, move new string_area to front of the list */
158 a = string_area_new(size > ARENA_SIZE ? size : ARENA_SIZE);
163 /* have space; do the "allocation." */
169 static char *string_area_strdup(struct string_area **ap, const char *src)
171 char *dest = string_area_malloc(ap, strlen(src) + 1);
172 return strcpy(dest, src);
175 static void list_file_entry(struct file_struct *f)
180 /* this can happen if duplicate names were removed */
183 permstring(perms, f->mode);
185 if (preserve_links && S_ISLNK(f->mode)) {
186 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
188 (double) f->length, timestring(f->modtime),
191 rprintf(FINFO, "%s %11.0f %s %s\n",
193 (double) f->length, timestring(f->modtime),
200 * Stat either a symlink or its referent, depending on the settings of
201 * copy_links, copy_unsafe_links, etc.
203 * @retval -1 on error
205 * @retval 0 for success
207 * @post If @p path is a symlink, then @p linkbuf (of size @c
208 * MAXPATHLEN) contains the symlink target.
210 * @post @p buffer contains information about the link or the
211 * referrent as appropriate, if they exist.
213 int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
217 return do_stat(path, buffer);
218 if (do_lstat(path, buffer) == -1)
220 if (S_ISLNK(buffer->st_mode)) {
221 int l = readlink((char *) path, linkbuf, MAXPATHLEN - 1);
225 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
227 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
230 return do_stat(path, buffer);
235 return do_stat(path, buffer);
239 int link_stat(const char *path, STRUCT_STAT * buffer)
243 return do_stat(path, buffer);
244 return do_lstat(path, buffer);
246 return do_stat(path, buffer);
251 * This function is used to check if a file should be included/excluded
252 * from the list of files based on its name and type etc. The value of
253 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
255 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
257 #if 0 /* This currently never happens, so avoid a useless compare. */
258 if (exclude_level == NO_EXCLUDES)
262 /* never exclude '.', even if somebody does --exclude '*' */
263 if (fname[0] == '.' && !fname[1])
265 /* Handle the -R version of the '.' dir. */
266 if (fname[0] == '/') {
267 int len = strlen(fname);
268 if (fname[len-1] == '.' && fname[len-2] == '/')
272 if (server_exclude_list
273 && check_exclude(server_exclude_list, fname, is_dir))
275 if (exclude_level != ALL_EXCLUDES)
277 if (exclude_list && check_exclude(exclude_list, fname, is_dir))
279 if (local_exclude_list
280 && check_exclude(local_exclude_list, fname, is_dir))
285 /* used by the one_file_system code */
286 static dev_t filesystem_dev;
288 static void set_filesystem(char *fname)
291 if (link_stat(fname, &st) != 0)
293 filesystem_dev = st.st_dev;
297 static int to_wire_mode(mode_t mode)
299 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
300 return (mode & ~(_S_IFMT)) | 0120000;
304 static mode_t from_wire_mode(int mode)
306 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
307 return (mode & ~(_S_IFMT)) | _S_IFLNK;
308 return (mode_t) mode;
312 static void send_directory(int f, struct file_list *flist, char *dir);
314 static char *flist_dir;
318 * Make sure @p flist is big enough to hold at least @p flist->count
321 static void flist_expand(struct file_list *flist)
323 if (flist->count >= flist->malloced) {
326 if (flist->malloced < 1000)
327 flist->malloced += 1000;
329 flist->malloced *= 2;
332 new_ptr = realloc_array(flist->files,
333 struct file_struct *,
336 new_ptr = new_array(struct file_struct *,
341 rprintf(FINFO, "expand file_list to %.0f bytes, did%s move\n",
342 (double)sizeof(flist->files[0])
344 (new_ptr == flist->files) ? " not" : "");
347 flist->files = (struct file_struct **) new_ptr;
350 out_of_memory("flist_expand");
354 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
356 unsigned short flags;
357 static time_t modtime;
359 static DEV64_T rdev; /* just high bytes in p28 onward */
363 static char lastname[MAXPATHLEN];
364 char *fname, fbuf[MAXPATHLEN];
372 modtime = 0, mode = 0;
379 io_write_phase = "send_file_entry";
381 fname = f_name_to(file, fbuf, sizeof fbuf);
385 if (file->mode == mode)
389 if (preserve_devices) {
390 if (protocol_version < 28) {
391 if (IS_DEVICE(mode) && file->rdev == rdev) {
392 /* Set both flags so that the test when
393 * writing the data is simpler. */
394 flags |= SAME_RDEV_pre28|SAME_HIGH_RDEV;
399 else if (IS_DEVICE(mode)) {
400 if ((file->rdev & ~0xFF) == rdev)
401 flags |= SAME_HIGH_RDEV;
403 rdev = file->rdev & ~0xFF;
406 if (file->uid == uid)
410 if (file->gid == gid)
414 if (file->modtime == modtime)
417 modtime = file->modtime;
418 if (file->flags & HAS_INODE_DATA) {
419 if (file->dev == dev) {
420 if (protocol_version >= 28)
425 flags |= HAS_INODE_DATA;
429 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
431 l2 = strlen(fname) - l1;
438 /* We must make sure we don't send a zero flags byte or
439 * the other end will terminate the flist transfer. */
440 if (flags == 0 && !S_ISDIR(mode))
441 flags |= FLAG_DELETE; /* NOTE: no meaning for non-dir */
442 if (protocol_version >= 28) {
443 if ((flags & 0xFF00) || flags == 0) {
444 flags |= EXTENDED_FLAGS;
445 write_byte(f, flags);
446 write_byte(f, flags >> 8);
448 write_byte(f, flags);
452 write_byte(f, flags);
454 if (flags & SAME_NAME)
456 if (flags & LONG_NAME)
460 write_buf(f, fname + l1, l2);
462 write_longint(f, file->length);
463 if (!(flags & SAME_TIME))
464 write_int(f, modtime);
465 if (!(flags & SAME_MODE))
466 write_int(f, to_wire_mode(mode));
467 if (preserve_uid && !(flags & SAME_UID)) {
471 if (preserve_gid && !(flags & SAME_GID)) {
475 if (preserve_devices && IS_DEVICE(mode)) {
476 /* If SAME_HIGH_RDEV is off, SAME_RDEV_pre28 is also off.
477 * Also, avoid using "rdev" because it may be incomplete. */
478 if (!(flags & SAME_HIGH_RDEV))
479 write_int(f, file->rdev);
480 else if (protocol_version >= 28)
481 write_byte(f, file->rdev);
485 if (preserve_links && S_ISLNK(mode)) {
486 write_int(f, strlen(file->link));
487 write_buf(f, file->link, strlen(file->link));
491 #if SUPPORT_HARD_LINKS
492 if (flags & HAS_INODE_DATA) {
493 if (protocol_version < 26) {
494 /* 32-bit dev_t and ino_t */
496 write_int(f, file->inode);
498 /* 64-bit dev_t and ino_t */
499 if (!(flags & SAME_DEV))
500 write_longint(f, dev);
501 write_longint(f, file->inode);
506 if (always_checksum && (protocol_version < 28 || S_ISREG(mode))) {
507 char *sum = file->sum? file->sum : empty_sum;
508 write_buf(f, sum, protocol_version < 21? 2
512 strlcpy(lastname, fname, MAXPATHLEN);
513 lastname[MAXPATHLEN - 1] = 0;
515 io_write_phase = "unknown";
520 void receive_file_entry(struct file_struct **fptr, unsigned short flags, int f)
522 static time_t modtime;
524 static DEV64_T rdev; /* just high bytes in p28 onward */
528 static char lastname[MAXPATHLEN];
529 char thisname[MAXPATHLEN];
530 unsigned int l1 = 0, l2 = 0;
532 struct file_struct *file;
535 modtime = 0, mode = 0;
542 if (flags & SAME_NAME)
545 if (flags & LONG_NAME)
550 file = new(struct file_struct);
552 out_of_memory("receive_file_entry");
553 memset((char *) file, 0, sizeof(*file));
556 if (l2 >= MAXPATHLEN - l1) {
558 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
559 flags, l1, l2, lastname);
560 overflow("receive_file_entry");
563 strlcpy(thisname, lastname, l1 + 1);
564 read_sbuf(f, &thisname[l1], l2);
565 thisname[l1 + l2] = 0;
567 strlcpy(lastname, thisname, MAXPATHLEN);
568 lastname[MAXPATHLEN - 1] = 0;
570 clean_fname(thisname);
572 if (sanitize_paths) {
573 sanitize_path(thisname, NULL);
576 if ((p = strrchr(thisname, '/'))) {
577 static char *lastdir;
579 if (lastdir && strcmp(thisname, lastdir) == 0)
580 file->dirname = lastdir;
582 file->dirname = strdup(thisname);
583 lastdir = file->dirname;
585 file->basename = strdup(p + 1);
587 file->dirname = NULL;
588 file->basename = strdup(thisname);
592 out_of_memory("receive_file_entry 1");
595 file->length = read_longint(f);
596 if (!(flags & SAME_TIME))
597 modtime = (time_t)read_int(f);
598 file->modtime = modtime;
599 if (!(flags & SAME_MODE))
600 mode = from_wire_mode(read_int(f));
604 if (!(flags & SAME_UID))
605 uid = (uid_t)read_int(f);
609 if (!(flags & SAME_GID))
610 gid = (gid_t)read_int(f);
613 if (preserve_devices) {
614 if (protocol_version < 28) {
615 if (IS_DEVICE(mode)) {
616 if (!(flags & SAME_RDEV_pre28))
617 rdev = (DEV64_T)read_int(f);
621 } else if (IS_DEVICE(mode)) {
622 if (!(flags & SAME_HIGH_RDEV)) {
623 file->rdev = (DEV64_T)read_int(f);
624 rdev = file->rdev & ~0xFF;
626 file->rdev = rdev | (DEV64_T)read_byte(f);
630 if (preserve_links && S_ISLNK(mode)) {
633 rprintf(FERROR, "overflow: l=%d\n", l);
634 overflow("receive_file_entry");
636 file->link = new_array(char, l + 1);
638 out_of_memory("receive_file_entry 2");
639 read_sbuf(f, file->link, l);
641 sanitize_path(file->link, file->dirname);
643 #if SUPPORT_HARD_LINKS
644 if (preserve_hard_links && protocol_version < 28
646 file->flags |= HAS_INODE_DATA;
647 if (file->flags & HAS_INODE_DATA) {
648 if (protocol_version < 26) {
650 file->inode = read_int(f);
652 if (!(flags & SAME_DEV))
653 dev = read_longint(f);
654 file->inode = read_longint(f);
660 if (always_checksum) {
663 sum = file->sum = new_array(char, MD4_SUM_LENGTH);
665 out_of_memory("md4 sum");
666 } else if (protocol_version < 28) {
667 /* Prior to 28, we get a useless set of nulls. */
672 read_buf(f, sum, protocol_version < 21? 2
677 if (!preserve_perms) {
678 extern int orig_umask;
679 /* set an appropriate set of permissions based on original
680 * permissions and umask. This emulates what GNU cp does */
681 file->mode &= ~orig_umask;
686 /* determine if a file in a different filesstem should be skipped
687 when one_file_system is set. We bascally only want to include
688 the mount points - but they can be hard to find! */
689 static int skip_filesystem(char *fname, STRUCT_STAT * st)
692 char *p = strrchr(fname, '/');
694 /* skip all but directories */
695 if (!S_ISDIR(st->st_mode))
698 /* if its not a subdirectory then allow */
703 if (link_stat(fname, &st2)) {
709 return (st2.st_dev != filesystem_dev);
712 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
713 /* IRIX cc cares that the operands to the ternary have the same type. */
714 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
717 * Create a file_struct for a named file by reading its stat()
718 * information and performing extensive checks against global
721 * @return the new file, or NULL if there was an error or this file
722 * should be excluded.
724 * @todo There is a small optimization opportunity here to avoid
725 * stat()ing the file in some circumstances, which has a certain cost.
726 * We are called immediately after doing readdir(), and so we may
727 * already know the d_type of the file. We could for example avoid
728 * statting directories if we're not recursing, but this is not a very
729 * important case. Some systems may not have d_type.
731 struct file_struct *make_file(char *fname, struct string_area **ap,
734 struct file_struct *file;
736 char sum[SUM_LENGTH];
738 char cleaned_name[MAXPATHLEN];
739 char linkbuf[MAXPATHLEN];
740 extern int module_id;
742 strlcpy(cleaned_name, fname, MAXPATHLEN);
743 cleaned_name[MAXPATHLEN - 1] = 0;
744 clean_fname(cleaned_name);
746 sanitize_path(cleaned_name, NULL);
747 fname = cleaned_name;
749 memset(sum, 0, SUM_LENGTH);
751 if (readlink_stat(fname, &st, linkbuf) != 0) {
752 int save_errno = errno;
753 if (errno == ENOENT && exclude_level != NO_EXCLUDES) {
754 /* either symlink pointing nowhere or file that
755 * was removed during rsync run; see if excluded
756 * before reporting an error */
757 if (check_exclude_file(fname, 0, exclude_level)) {
758 /* file is excluded anyway, ignore silently */
762 io_error |= IOERR_GENERAL;
763 rprintf(FERROR, "readlink %s failed: %s\n",
764 full_fname(fname), strerror(save_errno));
768 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
769 if (exclude_level == NO_EXCLUDES)
772 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
773 rprintf(FINFO, "skipping directory %s\n", fname);
777 if (one_file_system && st.st_dev != filesystem_dev) {
778 if (skip_filesystem(fname, &st))
782 if (check_exclude_file(fname, S_ISDIR(st.st_mode) != 0, exclude_level))
785 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
791 rprintf(FINFO, "make_file(%s,*,%d)\n", fname, exclude_level);
793 file = new(struct file_struct);
795 out_of_memory("make_file");
796 memset((char *) file, 0, sizeof(*file));
798 if ((p = strrchr(fname, '/'))) {
799 static char *lastdir;
801 if (lastdir && strcmp(fname, lastdir) == 0)
802 file->dirname = lastdir;
804 file->dirname = strdup(fname);
805 lastdir = file->dirname;
807 file->basename = STRDUP(ap, p + 1);
810 file->dirname = NULL;
811 file->basename = STRDUP(ap, fname);
814 file->modtime = st.st_mtime;
815 file->length = st.st_size;
816 file->mode = st.st_mode;
817 file->uid = st.st_uid;
818 file->gid = st.st_gid;
819 if (preserve_hard_links) {
820 if (protocol_version < 28? S_ISREG(st.st_mode)
821 : !S_ISDIR(st.st_mode) && st.st_nlink > 1) {
822 file->dev = st.st_dev;
823 file->inode = st.st_ino;
824 file->flags |= HAS_INODE_DATA;
827 #ifdef HAVE_STRUCT_STAT_ST_RDEV
828 file->rdev = st.st_rdev;
832 if (S_ISLNK(st.st_mode))
833 file->link = STRDUP(ap, linkbuf);
836 if (always_checksum && S_ISREG(st.st_mode)) {
837 file->sum = (char*)MALLOC(ap, MD4_SUM_LENGTH);
839 out_of_memory("md4 sum");
840 file_checksum(fname, file->sum, st.st_size);
844 static char *lastdir;
845 if (lastdir && strcmp(lastdir, flist_dir) == 0)
846 file->basedir = lastdir;
848 file->basedir = strdup(flist_dir);
849 lastdir = file->basedir;
852 file->basedir = NULL;
854 if (!S_ISDIR(st.st_mode))
855 stats.total_size += st.st_size;
861 void send_file_name(int f, struct file_list *flist, char *fname,
862 int recursive, unsigned short base_flags)
864 struct file_struct *file;
865 char fbuf[MAXPATHLEN];
866 extern int delete_excluded;
868 /* f is set to -1 when calculating deletion file list */
869 file = make_file(fname, &flist->string_area,
870 f == -1 && delete_excluded? SERVER_EXCLUDES
876 maybe_emit_filelist_progress(flist);
881 file->flags |= FLAG_DELETE;
883 if (file->basename[0]) {
884 flist->files[flist->count++] = file;
885 send_file_entry(file, f, base_flags);
888 if (S_ISDIR(file->mode) && recursive) {
889 struct exclude_struct **last_exclude_list =
891 send_directory(f, flist, f_name_to(file, fbuf, sizeof fbuf));
892 local_exclude_list = last_exclude_list;
898 static void send_directory(int f, struct file_list *flist, char *dir)
902 char fname[MAXPATHLEN];
908 io_error |= IOERR_GENERAL;
909 rprintf(FERROR, "opendir %s failed: %s\n",
910 full_fname(dir), strerror(errno));
914 strlcpy(fname, dir, MAXPATHLEN);
916 if (fname[l - 1] != '/') {
917 if (l == MAXPATHLEN - 1) {
918 io_error |= IOERR_GENERAL;
919 rprintf(FERROR, "skipping long-named directory: %s\n",
924 strlcat(fname, "/", MAXPATHLEN);
927 p = fname + strlen(fname);
929 local_exclude_list = NULL;
932 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN - 1) {
933 strcpy(p, ".cvsignore");
934 add_exclude_file(&exclude_list,fname,MISSING_OK,ADD_EXCLUDE);
936 io_error |= IOERR_GENERAL;
938 "cannot cvs-exclude in long-named directory %s\n",
943 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
944 char *dname = d_name(di);
945 if (dname[0] == '.' && (dname[1] == '\0'
946 || (dname[1] == '.' && dname[2] == '\0')))
948 strlcpy(p, dname, MAXPATHLEN - l);
949 send_file_name(f, flist, fname, recurse, 0);
952 io_error |= IOERR_GENERAL;
953 rprintf(FERROR, "readdir(%s): (%d) %s\n",
954 dir, errno, strerror(errno));
957 if (local_exclude_list)
958 free_exclude_list(&local_exclude_list); /* Zeros pointer too */
965 * The delete_files() function in receiver.c sets f to -1 so that we just
966 * construct the file list in memory without sending it over the wire. It
967 * also has the side-effect of ignoring user-excludes if delete_excluded
968 * is set (so that the delete list includes user-excluded files).
970 struct file_list *send_file_list(int f, int argc, char *argv[])
974 char *p, *dir, *olddir;
975 char lastpath[MAXPATHLEN] = "";
976 struct file_list *flist;
980 if (show_filelist_p() && f != -1)
981 start_filelist_progress("building file list");
983 start_write = stats.total_written;
988 io_start_buffering_out(f);
989 if (filesfrom_fd >= 0) {
990 if (argv[0] && !push_dir(argv[0], 0)) {
991 rprintf(FERROR, "push_dir %s failed: %s\n",
992 full_fname(argv[0]), strerror(errno));
993 exit_cleanup(RERR_FILESELECT);
1000 char fname2[MAXPATHLEN];
1001 char *fname = fname2;
1004 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1006 sanitize_path(fname, NULL);
1010 strlcpy(fname, *argv++, MAXPATHLEN);
1012 sanitize_path(fname, NULL);
1016 if (fname[l - 1] == '/') {
1017 if (l == 2 && fname[0] == '.') {
1018 /* Turn "./" into just "." rather than "./." */
1021 strlcat(fname, ".", MAXPATHLEN);
1025 if (link_stat(fname, &st) != 0) {
1027 io_error |= IOERR_GENERAL;
1028 rprintf(FERROR, "link_stat %s failed: %s\n",
1029 full_fname(fname), strerror(errno));
1034 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
1035 rprintf(FINFO, "skipping directory %s\n", fname);
1042 if (!relative_paths) {
1043 p = strrchr(fname, '/');
1052 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1053 /* this ensures we send the intermediate directories,
1054 thus getting their permissions right */
1055 char *lp = lastpath, *fn = fname, *slash = fname;
1057 /* Skip any initial directories in our path that we
1058 * have in common with lastpath. */
1059 while (*fn && *lp == *fn) {
1065 if (fn != p || (*lp && *lp != '/')) {
1066 int copy_links_saved = copy_links;
1067 int recurse_saved = recurse;
1068 copy_links = copy_unsafe_links;
1069 /* set recurse to 1 to prevent make_file
1070 * from ignoring directory, but still
1071 * turn off the recursive parameter to
1074 while ((slash = strchr(slash+1, '/')) != 0) {
1076 send_file_name(f, flist, fname, 0, 0);
1079 copy_links = copy_links_saved;
1080 recurse = recurse_saved;
1082 strlcpy(lastpath, fname, sizeof lastpath);
1091 olddir = push_dir(dir, 1);
1094 io_error |= IOERR_GENERAL;
1095 rprintf(FERROR, "push_dir %s failed: %s\n",
1096 full_fname(dir), strerror(errno));
1103 if (one_file_system)
1104 set_filesystem(fname);
1106 send_file_name(f, flist, fname, recurse, FLAG_DELETE);
1108 if (olddir != NULL) {
1110 if (pop_dir(olddir) != 0) {
1111 rprintf(FERROR, "pop_dir %s failed: %s\n",
1112 full_fname(dir), strerror(errno));
1113 exit_cleanup(RERR_FILESELECT);
1119 send_file_entry(NULL, f, 0);
1121 if (show_filelist_p() && f != -1)
1122 finish_filelist_progress(flist);
1124 clean_flist(flist, 0, 0);
1126 /* now send the uid/gid list. This was introduced in protocol
1131 /* send the io_error flag */
1133 extern int module_id;
1134 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1139 stats.flist_size = stats.total_written - start_write;
1140 stats.num_files = flist->count;
1142 write_batch_flist_info(flist->count, flist->files);
1146 rprintf(FINFO, "send_file_list done\n");
1152 struct file_list *recv_file_list(int f)
1154 struct file_list *flist;
1155 unsigned short flags;
1157 extern int list_only;
1159 if (show_filelist_p())
1160 start_filelist_progress("receiving file list");
1162 start_read = stats.total_read;
1164 flist = new(struct file_list);
1169 flist->malloced = 1000;
1170 flist->files = new_array(struct file_struct *, flist->malloced);
1175 while ((flags = read_byte(f)) != 0) {
1176 int i = flist->count;
1178 flist_expand(flist);
1180 if (protocol_version >= 28 && (flags & EXTENDED_FLAGS))
1181 flags |= read_byte(f) << 8;
1182 receive_file_entry(&flist->files[i], flags, f);
1184 if (S_ISREG(flist->files[i]->mode))
1185 stats.total_size += flist->files[i]->length;
1189 maybe_emit_filelist_progress(flist);
1192 rprintf(FINFO, "recv_file_name(%s)\n",
1193 f_name(flist->files[i]));
1196 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1199 rprintf(FINFO, "received %d names\n", flist->count);
1201 clean_flist(flist, relative_paths, 1);
1203 if (show_filelist_p())
1204 finish_filelist_progress(flist);
1206 /* now recv the uid/gid list. This was introduced in protocol version 15 */
1208 recv_uid_list(f, flist);
1210 /* recv the io_error flag */
1211 if (f != -1 && !read_batch) { /* dw-added readbatch */
1212 extern int module_id;
1213 extern int ignore_errors;
1214 if (lp_ignore_errors(module_id) || ignore_errors)
1217 io_error |= read_int(f);
1222 for (i = 0; i < flist->count; i++)
1223 list_file_entry(flist->files[i]);
1228 rprintf(FINFO, "recv_file_list done\n");
1230 stats.flist_size = stats.total_read - start_read;
1231 stats.num_files = flist->count;
1236 out_of_memory("recv_file_list");
1237 return NULL; /* not reached */
1241 int file_compare(struct file_struct **file1, struct file_struct **file2)
1243 struct file_struct *f1 = *file1;
1244 struct file_struct *f2 = *file2;
1246 if (!f1->basename && !f2->basename)
1252 if (f1->dirname == f2->dirname)
1253 return u_strcmp(f1->basename, f2->basename);
1254 return f_name_cmp(f1, f2);
1258 int flist_find(struct file_list *flist, struct file_struct *f)
1260 int low = 0, high = flist->count - 1;
1262 while (high >= 0 && !flist->files[high]->basename) high--;
1267 while (low != high) {
1268 int mid = (low + high) / 2;
1269 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1271 return flist_up(flist, mid);
1278 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1279 return flist_up(flist, low);
1287 void free_file(struct file_struct *file)
1292 free(file->basename);
1302 * allocate a new file list
1304 struct file_list *flist_new(void)
1306 struct file_list *flist;
1308 flist = new(struct file_list);
1310 out_of_memory("send_file_list");
1313 flist->malloced = 0;
1314 flist->files = NULL;
1317 flist->string_area = string_area_new(0);
1319 flist->string_area = NULL;
1325 * free up all elements in a flist
1327 void flist_free(struct file_list *flist)
1330 for (i = 1; i < flist->count; i++) {
1331 if (!flist->string_area)
1332 free_file(flist->files[i]);
1333 free(flist->files[i]);
1335 /* FIXME: I don't think we generally need to blank the flist
1336 * since it's about to be freed. This will just cause more
1337 * memory traffic. If you want a freed-memory debugger, you
1338 * know where to get it. */
1339 memset((char *) flist->files, 0,
1340 sizeof(flist->files[0]) * flist->count);
1342 if (flist->string_area)
1343 string_area_free(flist->string_area);
1344 memset((char *) flist, 0, sizeof(*flist));
1350 * This routine ensures we don't have any duplicate names in our file list.
1351 * duplicate names can cause corruption because of the pipelining
1353 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1357 if (!flist || flist->count == 0)
1360 qsort(flist->files, flist->count,
1361 sizeof(flist->files[0]), (int (*)()) file_compare);
1363 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1364 if (flist->files[i]->basename) {
1369 while (++i < flist->count) {
1370 if (!flist->files[i]->basename)
1372 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1373 if (verbose > 1 && !am_server) {
1375 "removing duplicate name %s from file list %d\n",
1376 f_name(flist->files[i]), i);
1378 /* Make sure that if we unduplicate '.', that we don't
1379 * lose track of a user-specified starting point (or
1380 * else deletions will mysteriously fail with -R). */
1381 if (flist->files[i]->flags & FLAG_DELETE)
1382 flist->files[prev_i]->flags |= FLAG_DELETE;
1383 /* it's not great that the flist knows the semantics of
1384 * the file memory usage, but i'd rather not add a flag
1385 * byte to that struct.
1386 * XXX can i use a bit in the flags field? */
1387 if (flist->string_area)
1388 flist->files[i][0] = null_file;
1390 free_file(flist->files[i]);
1397 /* we need to strip off the root directory in the case
1398 of relative paths, but this must be done _after_
1399 the sorting phase */
1400 for (i = 0; i < flist->count; i++) {
1401 if (flist->files[i]->dirname &&
1402 flist->files[i]->dirname[0] == '/') {
1403 memmove(&flist->files[i]->dirname[0],
1404 &flist->files[i]->dirname[1],
1405 strlen(flist->files[i]->dirname));
1408 if (flist->files[i]->dirname &&
1409 !flist->files[i]->dirname[0]) {
1410 flist->files[i]->dirname = NULL;
1418 for (i = 0; i < flist->count; i++) {
1419 rprintf(FINFO, "[%ld] i=%d %s %s mode=0%o len=%.0f\n",
1421 NS(flist->files[i]->dirname),
1422 NS(flist->files[i]->basename),
1423 (int) flist->files[i]->mode,
1424 (double) flist->files[i]->length);
1429 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1431 /* Compare the names of two file_struct entities, just like strcmp()
1432 * would do if it were operating on the joined strings. We assume
1433 * that there are no 0-length strings.
1435 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1438 const uchar *c1, *c2;
1439 enum fnc_state state1, state2;
1441 if (!f1 || !f1->basename) {
1442 if (!f2 || !f2->basename)
1446 if (!f2 || !f2->basename)
1449 if (!(c1 = (uchar*)f1->dirname)) {
1451 c1 = (uchar*)f1->basename;
1455 if (!(c2 = (uchar*)f2->dirname)) {
1457 c2 = (uchar*)f2->basename;
1463 if ((dif = (int)*c1 - (int)*c2) != 0)
1473 c1 = (uchar*)f1->basename;
1487 c2 = (uchar*)f2->basename;
1501 /* Return a copy of the full filename of a flist entry, using the indicated
1504 char *f_name_to(struct file_struct *f, char *fbuf, int bsize)
1506 if (!f || !f->basename)
1510 int off = strlcpy(fbuf, f->dirname, bsize);
1511 off += strlcpy(fbuf + off, "/", bsize - off);
1512 strlcpy(fbuf + off, f->basename, bsize - off);
1514 strlcpy(fbuf, f->basename, bsize);
1519 /* Like f_name_to(), but we rotate through 5 static buffers of our own.
1521 char *f_name(struct file_struct *f)
1523 static char names[5][MAXPATHLEN];
1524 static unsigned int n;
1526 n = (n + 1) % (sizeof names / sizeof names[0]);
1528 return f_name_to(f, names[n], sizeof names[0]);