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),
189 f_name(f), f->u.link);
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)) {
392 if (file->u.rdev == rdev) {
393 /* Set both flags so that the test when
394 * writing the data is simpler. */
395 flags |= SAME_RDEV_pre28|SAME_HIGH_RDEV;
400 } else if (IS_DEVICE(mode)) {
401 if ((file->u.rdev & ~0xFF) == rdev)
402 flags |= SAME_HIGH_RDEV;
404 rdev = file->u.rdev & ~0xFF;
407 if (file->uid == uid)
411 if (file->gid == gid)
415 if (file->modtime == modtime)
418 modtime = file->modtime;
419 if (file->link_u.idev) {
420 if (file->F_DEV == dev) {
421 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->u.rdev);
480 else if (protocol_version >= 28)
481 write_byte(f, file->u.rdev);
485 if (preserve_links && S_ISLNK(mode)) {
486 write_int(f, strlen(file->u.link));
487 write_buf(f, file->u.link, strlen(file->u.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->F_INODE);
498 /* 64-bit dev_t and ino_t */
499 if (!(flags & SAME_DEV))
500 write_longint(f, dev);
501 write_longint(f, file->F_INODE);
506 if (always_checksum) {
510 else if (protocol_version < 28) {
511 /* Prior to 28, we sent a useless set of nulls. */
516 write_buf(f, sum, protocol_version < 21? 2
521 strlcpy(lastname, fname, MAXPATHLEN);
522 lastname[MAXPATHLEN - 1] = 0;
524 io_write_phase = "unknown";
529 void receive_file_entry(struct file_struct **fptr, unsigned short flags, int f)
531 static time_t modtime;
533 static DEV64_T rdev; /* just high bytes in p28 onward */
537 static char lastname[MAXPATHLEN];
538 char thisname[MAXPATHLEN];
539 unsigned int l1 = 0, l2 = 0;
541 struct file_struct *file;
544 modtime = 0, mode = 0;
551 if (flags & SAME_NAME)
554 if (flags & LONG_NAME)
559 file = new(struct file_struct);
561 out_of_memory("receive_file_entry");
562 memset((char *) file, 0, sizeof(*file));
565 if (l2 >= MAXPATHLEN - l1) {
567 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
568 flags, l1, l2, lastname);
569 overflow("receive_file_entry");
572 strlcpy(thisname, lastname, l1 + 1);
573 read_sbuf(f, &thisname[l1], l2);
574 thisname[l1 + l2] = 0;
576 strlcpy(lastname, thisname, MAXPATHLEN);
577 lastname[MAXPATHLEN - 1] = 0;
579 clean_fname(thisname);
581 if (sanitize_paths) {
582 sanitize_path(thisname, NULL);
585 if ((p = strrchr(thisname, '/'))) {
586 static char *lastdir;
588 if (lastdir && strcmp(thisname, lastdir) == 0)
589 file->dirname = lastdir;
591 file->dirname = strdup(thisname);
592 lastdir = file->dirname;
594 file->basename = strdup(p + 1);
596 file->dirname = NULL;
597 file->basename = strdup(thisname);
601 out_of_memory("receive_file_entry 1");
604 file->length = read_longint(f);
605 if (!(flags & SAME_TIME))
606 modtime = (time_t)read_int(f);
607 file->modtime = modtime;
608 if (!(flags & SAME_MODE))
609 mode = from_wire_mode(read_int(f));
613 if (!(flags & SAME_UID))
614 uid = (uid_t)read_int(f);
618 if (!(flags & SAME_GID))
619 gid = (gid_t)read_int(f);
622 if (preserve_devices) {
623 if (protocol_version < 28) {
624 if (IS_DEVICE(mode)) {
625 if (!(flags & SAME_RDEV_pre28))
626 rdev = (DEV64_T)read_int(f);
630 } else if (IS_DEVICE(mode)) {
631 if (!(flags & SAME_HIGH_RDEV)) {
632 file->u.rdev = (DEV64_T)read_int(f);
633 rdev = file->u.rdev & ~0xFF;
635 file->u.rdev = rdev | (DEV64_T)read_byte(f);
639 if (preserve_links && S_ISLNK(mode)) {
642 rprintf(FERROR, "overflow: l=%d\n", l);
643 overflow("receive_file_entry");
645 if (!(file->u.link = new_array(char, l + 1)))
646 out_of_memory("receive_file_entry 2");
647 read_sbuf(f, file->u.link, l);
649 sanitize_path(file->u.link, file->dirname);
651 #if SUPPORT_HARD_LINKS
652 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
653 flags |= HAS_INODE_DATA;
654 if (flags & HAS_INODE_DATA) {
655 if (!(file->link_u.idev = new(struct idev)))
656 out_of_memory("file inode data");
657 if (protocol_version < 26) {
659 file->F_INODE = read_int(f);
661 if (!(flags & SAME_DEV))
662 dev = read_longint(f);
663 file->F_INODE = read_longint(f);
669 if (always_checksum) {
672 sum = file->u.sum = new_array(char, MD4_SUM_LENGTH);
674 out_of_memory("md4 sum");
675 } else if (protocol_version < 28) {
676 /* Prior to 28, we get a useless set of nulls. */
681 read_buf(f, sum, protocol_version < 21? 2
686 if (!preserve_perms) {
687 extern int orig_umask;
688 /* set an appropriate set of permissions based on original
689 * permissions and umask. This emulates what GNU cp does */
690 file->mode &= ~orig_umask;
695 /* determine if a file in a different filesstem should be skipped
696 when one_file_system is set. We bascally only want to include
697 the mount points - but they can be hard to find! */
698 static int skip_filesystem(char *fname, STRUCT_STAT * st)
701 char *p = strrchr(fname, '/');
703 /* skip all but directories */
704 if (!S_ISDIR(st->st_mode))
707 /* if its not a subdirectory then allow */
712 if (link_stat(fname, &st2)) {
718 return (st2.st_dev != filesystem_dev);
721 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
722 /* IRIX cc cares that the operands to the ternary have the same type. */
723 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
726 * Create a file_struct for a named file by reading its stat()
727 * information and performing extensive checks against global
730 * @return the new file, or NULL if there was an error or this file
731 * should be excluded.
733 * @todo There is a small optimization opportunity here to avoid
734 * stat()ing the file in some circumstances, which has a certain cost.
735 * We are called immediately after doing readdir(), and so we may
736 * already know the d_type of the file. We could for example avoid
737 * statting directories if we're not recursing, but this is not a very
738 * important case. Some systems may not have d_type.
740 struct file_struct *make_file(char *fname, struct string_area **ap,
743 struct file_struct *file;
745 char sum[SUM_LENGTH];
747 char cleaned_name[MAXPATHLEN];
748 char linkbuf[MAXPATHLEN];
749 extern int module_id;
751 strlcpy(cleaned_name, fname, MAXPATHLEN);
752 cleaned_name[MAXPATHLEN - 1] = 0;
753 clean_fname(cleaned_name);
755 sanitize_path(cleaned_name, NULL);
756 fname = cleaned_name;
758 memset(sum, 0, SUM_LENGTH);
760 if (readlink_stat(fname, &st, linkbuf) != 0) {
761 int save_errno = errno;
762 if (errno == ENOENT && exclude_level != NO_EXCLUDES) {
763 /* either symlink pointing nowhere or file that
764 * was removed during rsync run; see if excluded
765 * before reporting an error */
766 if (check_exclude_file(fname, 0, exclude_level)) {
767 /* file is excluded anyway, ignore silently */
771 io_error |= IOERR_GENERAL;
772 rprintf(FERROR, "readlink %s failed: %s\n",
773 full_fname(fname), strerror(save_errno));
777 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
778 if (exclude_level == NO_EXCLUDES)
781 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
782 rprintf(FINFO, "skipping directory %s\n", fname);
786 if (one_file_system && st.st_dev != filesystem_dev) {
787 if (skip_filesystem(fname, &st))
791 if (check_exclude_file(fname, S_ISDIR(st.st_mode) != 0, exclude_level))
794 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
800 rprintf(FINFO, "make_file(%s,*,%d)\n", fname, exclude_level);
802 file = new(struct file_struct);
804 out_of_memory("make_file");
805 memset((char *) file, 0, sizeof(*file));
807 if ((p = strrchr(fname, '/'))) {
808 static char *lastdir;
810 if (lastdir && strcmp(fname, lastdir) == 0)
811 file->dirname = lastdir;
813 file->dirname = strdup(fname);
814 lastdir = file->dirname;
816 file->basename = STRDUP(ap, p + 1);
819 file->dirname = NULL;
820 file->basename = STRDUP(ap, fname);
823 file->modtime = st.st_mtime;
824 file->length = st.st_size;
825 file->mode = st.st_mode;
826 file->uid = st.st_uid;
827 file->gid = st.st_gid;
828 if (preserve_hard_links) {
829 if (protocol_version < 28 ? S_ISREG(st.st_mode)
830 : !S_ISDIR(st.st_mode) && st.st_nlink > 1) {
831 if (!(file->link_u.idev = new(struct idev)))
832 out_of_memory("file inode data");
833 file->F_DEV = st.st_dev;
834 file->F_INODE = st.st_ino;
837 #ifdef HAVE_STRUCT_STAT_ST_RDEV
838 if (IS_DEVICE(st.st_mode))
839 file->u.rdev = st.st_rdev;
843 if (S_ISLNK(st.st_mode))
844 file->u.link = STRDUP(ap, linkbuf);
847 if (always_checksum && S_ISREG(st.st_mode)) {
848 if (!(file->u.sum = (char*)MALLOC(ap, MD4_SUM_LENGTH)))
849 out_of_memory("md4 sum");
850 file_checksum(fname, file->u.sum, st.st_size);
854 static char *lastdir;
855 if (lastdir && strcmp(lastdir, flist_dir) == 0)
856 file->basedir = lastdir;
858 file->basedir = strdup(flist_dir);
859 lastdir = file->basedir;
862 file->basedir = NULL;
864 if (!S_ISDIR(st.st_mode))
865 stats.total_size += st.st_size;
871 void send_file_name(int f, struct file_list *flist, char *fname,
872 int recursive, unsigned short base_flags)
874 struct file_struct *file;
875 char fbuf[MAXPATHLEN];
876 extern int delete_excluded;
878 /* f is set to -1 when calculating deletion file list */
879 file = make_file(fname, &flist->string_area,
880 f == -1 && delete_excluded? SERVER_EXCLUDES
886 maybe_emit_filelist_progress(flist);
891 file->flags |= FLAG_DELETE;
893 if (file->basename[0]) {
894 flist->files[flist->count++] = file;
895 send_file_entry(file, f, base_flags);
898 if (S_ISDIR(file->mode) && recursive) {
899 struct exclude_struct **last_exclude_list =
901 send_directory(f, flist, f_name_to(file, fbuf, sizeof fbuf));
902 local_exclude_list = last_exclude_list;
908 static void send_directory(int f, struct file_list *flist, char *dir)
912 char fname[MAXPATHLEN];
918 io_error |= IOERR_GENERAL;
919 rprintf(FERROR, "opendir %s failed: %s\n",
920 full_fname(dir), strerror(errno));
924 strlcpy(fname, dir, MAXPATHLEN);
926 if (fname[l - 1] != '/') {
927 if (l == MAXPATHLEN - 1) {
928 io_error |= IOERR_GENERAL;
929 rprintf(FERROR, "skipping long-named directory: %s\n",
934 strlcat(fname, "/", MAXPATHLEN);
937 p = fname + strlen(fname);
939 local_exclude_list = NULL;
942 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN - 1) {
943 strcpy(p, ".cvsignore");
944 add_exclude_file(&exclude_list,fname,MISSING_OK,ADD_EXCLUDE);
946 io_error |= IOERR_GENERAL;
948 "cannot cvs-exclude in long-named directory %s\n",
953 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
954 char *dname = d_name(di);
955 if (dname[0] == '.' && (dname[1] == '\0'
956 || (dname[1] == '.' && dname[2] == '\0')))
958 strlcpy(p, dname, MAXPATHLEN - l);
959 send_file_name(f, flist, fname, recurse, 0);
962 io_error |= IOERR_GENERAL;
963 rprintf(FERROR, "readdir(%s): (%d) %s\n",
964 dir, errno, strerror(errno));
967 if (local_exclude_list)
968 free_exclude_list(&local_exclude_list); /* Zeros pointer too */
975 * The delete_files() function in receiver.c sets f to -1 so that we just
976 * construct the file list in memory without sending it over the wire. It
977 * also has the side-effect of ignoring user-excludes if delete_excluded
978 * is set (so that the delete list includes user-excluded files).
980 struct file_list *send_file_list(int f, int argc, char *argv[])
984 char *p, *dir, *olddir;
985 char lastpath[MAXPATHLEN] = "";
986 struct file_list *flist;
990 if (show_filelist_p() && f != -1)
991 start_filelist_progress("building file list");
993 start_write = stats.total_written;
998 io_start_buffering_out(f);
999 if (filesfrom_fd >= 0) {
1000 if (argv[0] && !push_dir(argv[0], 0)) {
1001 rprintf(FERROR, "push_dir %s failed: %s\n",
1002 full_fname(argv[0]), strerror(errno));
1003 exit_cleanup(RERR_FILESELECT);
1010 char fname2[MAXPATHLEN];
1011 char *fname = fname2;
1014 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1016 sanitize_path(fname, NULL);
1020 strlcpy(fname, *argv++, MAXPATHLEN);
1022 sanitize_path(fname, NULL);
1026 if (fname[l - 1] == '/') {
1027 if (l == 2 && fname[0] == '.') {
1028 /* Turn "./" into just "." rather than "./." */
1031 strlcat(fname, ".", MAXPATHLEN);
1035 if (link_stat(fname, &st) != 0) {
1037 io_error |= IOERR_GENERAL;
1038 rprintf(FERROR, "link_stat %s failed: %s\n",
1039 full_fname(fname), strerror(errno));
1044 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
1045 rprintf(FINFO, "skipping directory %s\n", fname);
1052 if (!relative_paths) {
1053 p = strrchr(fname, '/');
1062 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1063 /* this ensures we send the intermediate directories,
1064 thus getting their permissions right */
1065 char *lp = lastpath, *fn = fname, *slash = fname;
1067 /* Skip any initial directories in our path that we
1068 * have in common with lastpath. */
1069 while (*fn && *lp == *fn) {
1075 if (fn != p || (*lp && *lp != '/')) {
1076 int copy_links_saved = copy_links;
1077 int recurse_saved = recurse;
1078 copy_links = copy_unsafe_links;
1079 /* set recurse to 1 to prevent make_file
1080 * from ignoring directory, but still
1081 * turn off the recursive parameter to
1084 while ((slash = strchr(slash+1, '/')) != 0) {
1086 send_file_name(f, flist, fname, 0, 0);
1089 copy_links = copy_links_saved;
1090 recurse = recurse_saved;
1092 strlcpy(lastpath, fname, sizeof lastpath);
1101 olddir = push_dir(dir, 1);
1104 io_error |= IOERR_GENERAL;
1105 rprintf(FERROR, "push_dir %s failed: %s\n",
1106 full_fname(dir), strerror(errno));
1113 if (one_file_system)
1114 set_filesystem(fname);
1116 send_file_name(f, flist, fname, recurse, FLAG_DELETE);
1118 if (olddir != NULL) {
1120 if (pop_dir(olddir) != 0) {
1121 rprintf(FERROR, "pop_dir %s failed: %s\n",
1122 full_fname(dir), strerror(errno));
1123 exit_cleanup(RERR_FILESELECT);
1129 send_file_entry(NULL, f, 0);
1131 if (show_filelist_p() && f != -1)
1132 finish_filelist_progress(flist);
1134 clean_flist(flist, 0, 0);
1136 /* now send the uid/gid list. This was introduced in protocol
1141 /* send the io_error flag */
1143 extern int module_id;
1144 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1149 stats.flist_size = stats.total_written - start_write;
1150 stats.num_files = flist->count;
1152 write_batch_flist_info(flist->count, flist->files);
1156 rprintf(FINFO, "send_file_list done\n");
1162 struct file_list *recv_file_list(int f)
1164 struct file_list *flist;
1165 unsigned short flags;
1167 extern int list_only;
1169 if (show_filelist_p())
1170 start_filelist_progress("receiving file list");
1172 start_read = stats.total_read;
1174 flist = new(struct file_list);
1179 flist->malloced = 1000;
1180 flist->files = new_array(struct file_struct *, flist->malloced);
1185 while ((flags = read_byte(f)) != 0) {
1186 int i = flist->count;
1188 flist_expand(flist);
1190 if (protocol_version >= 28 && (flags & EXTENDED_FLAGS))
1191 flags |= read_byte(f) << 8;
1192 receive_file_entry(&flist->files[i], flags, f);
1194 if (S_ISREG(flist->files[i]->mode))
1195 stats.total_size += flist->files[i]->length;
1199 maybe_emit_filelist_progress(flist);
1202 rprintf(FINFO, "recv_file_name(%s)\n",
1203 f_name(flist->files[i]));
1206 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1209 rprintf(FINFO, "received %d names\n", flist->count);
1211 clean_flist(flist, relative_paths, 1);
1213 if (show_filelist_p())
1214 finish_filelist_progress(flist);
1216 /* now recv the uid/gid list. This was introduced in protocol version 15 */
1218 recv_uid_list(f, flist);
1220 /* recv the io_error flag */
1221 if (f != -1 && !read_batch) { /* dw-added readbatch */
1222 extern int module_id;
1223 extern int ignore_errors;
1224 if (lp_ignore_errors(module_id) || ignore_errors)
1227 io_error |= read_int(f);
1232 for (i = 0; i < flist->count; i++)
1233 list_file_entry(flist->files[i]);
1238 rprintf(FINFO, "recv_file_list done\n");
1240 stats.flist_size = stats.total_read - start_read;
1241 stats.num_files = flist->count;
1246 out_of_memory("recv_file_list");
1247 return NULL; /* not reached */
1251 int file_compare(struct file_struct **file1, struct file_struct **file2)
1253 struct file_struct *f1 = *file1;
1254 struct file_struct *f2 = *file2;
1256 if (!f1->basename && !f2->basename)
1262 if (f1->dirname == f2->dirname)
1263 return u_strcmp(f1->basename, f2->basename);
1264 return f_name_cmp(f1, f2);
1268 int flist_find(struct file_list *flist, struct file_struct *f)
1270 int low = 0, high = flist->count - 1;
1272 while (high >= 0 && !flist->files[high]->basename) high--;
1277 while (low != high) {
1278 int mid = (low + high) / 2;
1279 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1281 return flist_up(flist, mid);
1288 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1289 return flist_up(flist, low);
1297 void free_file(struct file_struct *file)
1302 free(file->basename);
1303 if (!IS_DEVICE(file->mode) && file->u.link)
1304 free(file->u.link); /* Handles u.sum too. */
1305 if (file->link_u.idev)
1306 free((char*)file->link_u.idev); /* Handles link_u.links too. */
1312 * allocate a new file list
1314 struct file_list *flist_new(void)
1316 struct file_list *flist;
1318 flist = new(struct file_list);
1320 out_of_memory("send_file_list");
1323 flist->malloced = 0;
1324 flist->files = NULL;
1327 flist->string_area = string_area_new(0);
1329 flist->string_area = NULL;
1335 * free up all elements in a flist
1337 void flist_free(struct file_list *flist)
1340 for (i = 1; i < flist->count; i++) {
1341 if (!flist->string_area)
1342 free_file(flist->files[i]);
1343 free(flist->files[i]);
1345 /* FIXME: I don't think we generally need to blank the flist
1346 * since it's about to be freed. This will just cause more
1347 * memory traffic. If you want a freed-memory debugger, you
1348 * know where to get it. */
1349 memset((char *) flist->files, 0,
1350 sizeof(flist->files[0]) * flist->count);
1352 if (flist->string_area)
1353 string_area_free(flist->string_area);
1354 memset((char *) flist, 0, sizeof(*flist));
1360 * This routine ensures we don't have any duplicate names in our file list.
1361 * duplicate names can cause corruption because of the pipelining
1363 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1367 if (!flist || flist->count == 0)
1370 qsort(flist->files, flist->count,
1371 sizeof(flist->files[0]), (int (*)()) file_compare);
1373 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1374 if (flist->files[i]->basename) {
1379 while (++i < flist->count) {
1380 if (!flist->files[i]->basename)
1382 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1383 if (verbose > 1 && !am_server) {
1385 "removing duplicate name %s from file list %d\n",
1386 f_name(flist->files[i]), i);
1388 /* Make sure that if we unduplicate '.', that we don't
1389 * lose track of a user-specified starting point (or
1390 * else deletions will mysteriously fail with -R). */
1391 if (flist->files[i]->flags & FLAG_DELETE)
1392 flist->files[prev_i]->flags |= FLAG_DELETE;
1393 /* it's not great that the flist knows the semantics of
1394 * the file memory usage, but i'd rather not add a flag
1395 * byte to that struct.
1396 * XXX can i use a bit in the flags field? */
1397 if (flist->string_area)
1398 flist->files[i][0] = null_file;
1400 free_file(flist->files[i]);
1406 /* we need to strip off the root directory in the case
1407 of relative paths, but this must be done _after_
1408 the sorting phase */
1409 for (i = 0; i < flist->count; i++) {
1410 if (flist->files[i]->dirname &&
1411 flist->files[i]->dirname[0] == '/') {
1412 memmove(&flist->files[i]->dirname[0],
1413 &flist->files[i]->dirname[1],
1414 strlen(flist->files[i]->dirname));
1417 if (flist->files[i]->dirname &&
1418 !flist->files[i]->dirname[0]) {
1419 flist->files[i]->dirname = NULL;
1427 for (i = 0; i < flist->count; i++) {
1428 rprintf(FINFO, "[%ld] i=%d %s %s mode=0%o len=%.0f\n",
1430 NS(flist->files[i]->dirname),
1431 NS(flist->files[i]->basename),
1432 (int) flist->files[i]->mode,
1433 (double) flist->files[i]->length);
1438 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1440 /* Compare the names of two file_struct entities, just like strcmp()
1441 * would do if it were operating on the joined strings. We assume
1442 * that there are no 0-length strings.
1444 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1447 const uchar *c1, *c2;
1448 enum fnc_state state1, state2;
1450 if (!f1 || !f1->basename) {
1451 if (!f2 || !f2->basename)
1455 if (!f2 || !f2->basename)
1458 if (!(c1 = (uchar*)f1->dirname)) {
1460 c1 = (uchar*)f1->basename;
1463 if (!(c2 = (uchar*)f2->dirname)) {
1465 c2 = (uchar*)f2->basename;
1470 if ((dif = (int)*c1 - (int)*c2) != 0)
1480 c1 = (uchar*)f1->basename;
1494 c2 = (uchar*)f2->basename;
1508 /* Return a copy of the full filename of a flist entry, using the indicated
1511 char *f_name_to(struct file_struct *f, char *fbuf, int bsize)
1513 if (!f || !f->basename)
1517 int off = strlcpy(fbuf, f->dirname, bsize);
1518 off += strlcpy(fbuf + off, "/", bsize - off);
1519 strlcpy(fbuf + off, f->basename, bsize - off);
1521 strlcpy(fbuf, f->basename, bsize);
1526 /* Like f_name_to(), but we rotate through 5 static buffers of our own.
1528 char *f_name(struct file_struct *f)
1530 static char names[5][MAXPATHLEN];
1531 static unsigned int n;
1533 n = (n + 1) % (sizeof names / sizeof names[0]);
1535 return f_name_to(f, names[n], sizeof names[0]);