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 ignore_errors;
43 extern int cvs_exclude;
46 extern char *files_from;
47 extern int filesfrom_fd;
49 extern int one_file_system;
50 extern int make_backups;
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 preserve_times;
58 extern int relative_paths;
59 extern int implied_dirs;
60 extern int copy_links;
61 extern int copy_unsafe_links;
62 extern int protocol_version;
63 extern int sanitize_paths;
65 extern int read_batch;
66 extern int write_batch;
68 extern struct exclude_struct **exclude_list;
69 extern struct exclude_struct **server_exclude_list;
70 extern struct exclude_struct **local_exclude_list;
74 static struct file_struct null_file;
75 static char empty_sum[MD4_SUM_LENGTH];
77 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
80 static int show_filelist_p(void)
82 return verbose && (recurse || files_from) && !am_server;
85 static void start_filelist_progress(char *kind)
87 rprintf(FINFO, "%s ... ", kind);
88 if ((verbose > 1) || do_progress)
94 static void emit_filelist_progress(const struct file_list *flist)
96 rprintf(FINFO, " %d files...\r", flist->count);
100 static void maybe_emit_filelist_progress(const struct file_list *flist)
102 if (do_progress && show_filelist_p() && ((flist->count % 100) == 0))
103 emit_filelist_progress(flist);
107 static void finish_filelist_progress(const struct file_list *flist)
110 /* This overwrites the progress line */
111 rprintf(FINFO, "%d file%sto consider\n",
112 flist->count, flist->count == 1 ? " " : "s ");
114 rprintf(FINFO, "done\n");
117 void show_flist_stats(void)
123 static struct string_area *string_area_new(int size)
125 struct string_area *a;
129 a = new(struct string_area);
131 out_of_memory("string_area_new");
132 a->current = a->base = new_array(char, size);
134 out_of_memory("string_area_new buffer");
135 a->end = a->base + size;
141 static void string_area_free(struct string_area *a)
143 struct string_area *next;
145 for (; a; a = next) {
151 static char *string_area_malloc(struct string_area **ap, int size)
154 struct string_area *a;
156 /* does the request fit into the current space? */
158 if (a->current + size >= a->end) {
159 /* no; get space, move new string_area to front of the list */
160 a = string_area_new(size > ARENA_SIZE ? size : ARENA_SIZE);
165 /* have space; do the "allocation." */
171 static char *string_area_strdup(struct string_area **ap, const char *src)
173 char *dest = string_area_malloc(ap, strlen(src) + 1);
174 return strcpy(dest, src);
177 static void list_file_entry(struct file_struct *f)
182 /* this can happen if duplicate names were removed */
185 permstring(perms, f->mode);
187 if (preserve_links && S_ISLNK(f->mode)) {
188 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
190 (double) f->length, timestring(f->modtime),
191 f_name(f), f->u.link);
193 rprintf(FINFO, "%s %11.0f %s %s\n",
195 (double) f->length, timestring(f->modtime),
202 * Stat either a symlink or its referent, depending on the settings of
203 * copy_links, copy_unsafe_links, etc.
205 * @retval -1 on error
207 * @retval 0 for success
209 * @post If @p path is a symlink, then @p linkbuf (of size @c
210 * MAXPATHLEN) contains the symlink target.
212 * @post @p buffer contains information about the link or the
213 * referrent as appropriate, if they exist.
215 int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
219 return do_stat(path, buffer);
220 if (do_lstat(path, buffer) == -1)
222 if (S_ISLNK(buffer->st_mode)) {
223 int l = readlink((char *) path, linkbuf, MAXPATHLEN - 1);
227 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
229 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
232 return do_stat(path, buffer);
237 return do_stat(path, buffer);
241 int link_stat(const char *path, STRUCT_STAT * buffer)
245 return do_stat(path, buffer);
246 return do_lstat(path, buffer);
248 return do_stat(path, buffer);
253 * This function is used to check if a file should be included/excluded
254 * from the list of files based on its name and type etc. The value of
255 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
257 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
259 #if 0 /* This currently never happens, so avoid a useless compare. */
260 if (exclude_level == NO_EXCLUDES)
264 /* never exclude '.', even if somebody does --exclude '*' */
265 if (fname[0] == '.' && !fname[1])
267 /* Handle the -R version of the '.' dir. */
268 if (fname[0] == '/') {
269 int len = strlen(fname);
270 if (fname[len-1] == '.' && fname[len-2] == '/')
274 if (server_exclude_list
275 && check_exclude(server_exclude_list, fname, is_dir))
277 if (exclude_level != ALL_EXCLUDES)
279 if (exclude_list && check_exclude(exclude_list, fname, is_dir))
281 if (local_exclude_list
282 && check_exclude(local_exclude_list, fname, is_dir))
287 /* used by the one_file_system code */
288 static dev_t filesystem_dev;
290 static void set_filesystem(char *fname)
293 if (link_stat(fname, &st) != 0)
295 filesystem_dev = st.st_dev;
299 static int to_wire_mode(mode_t mode)
301 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
302 return (mode & ~(_S_IFMT)) | 0120000;
306 static mode_t from_wire_mode(int mode)
308 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
309 return (mode & ~(_S_IFMT)) | _S_IFLNK;
310 return (mode_t) mode;
314 static void send_directory(int f, struct file_list *flist, char *dir);
316 static char *flist_dir;
320 * Make sure @p flist is big enough to hold at least @p flist->count
323 static void flist_expand(struct file_list *flist)
325 if (flist->count >= flist->malloced) {
328 if (flist->malloced < 1000)
329 flist->malloced += 1000;
331 flist->malloced *= 2;
334 new_ptr = realloc_array(flist->files,
335 struct file_struct *,
338 new_ptr = new_array(struct file_struct *,
343 rprintf(FINFO, "expand file_list to %.0f bytes, did%s move\n",
344 (double)sizeof(flist->files[0])
346 (new_ptr == flist->files) ? " not" : "");
349 flist->files = (struct file_struct **) new_ptr;
352 out_of_memory("flist_expand");
356 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
358 unsigned short flags;
359 static time_t modtime;
361 static DEV64_T rdev; /* just high bytes in p28 onward */
365 static char lastname[MAXPATHLEN];
366 char *fname, fbuf[MAXPATHLEN];
374 modtime = 0, mode = 0;
381 io_write_phase = "send_file_entry";
383 fname = f_name_to(file, fbuf, sizeof fbuf);
387 if (file->mode == mode)
391 if (preserve_devices) {
392 if (protocol_version < 28) {
393 if (IS_DEVICE(mode)) {
394 if (file->u.rdev == rdev) {
395 /* Set both flags so that the test when
396 * writing the data is simpler. */
397 flags |= SAME_RDEV_pre28|SAME_HIGH_RDEV;
402 } else if (IS_DEVICE(mode)) {
403 if ((file->u.rdev & ~0xFF) == rdev)
404 flags |= SAME_HIGH_RDEV;
406 rdev = file->u.rdev & ~0xFF;
409 if (file->uid == uid)
413 if (file->gid == gid)
417 if (file->modtime == modtime)
420 modtime = file->modtime;
421 if (file->link_u.idev) {
422 if (file->F_DEV == dev) {
423 if (protocol_version >= 28)
427 flags |= HAS_INODE_DATA;
431 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
433 l2 = strlen(fname) - l1;
440 /* We must make sure we don't send a zero flags byte or
441 * the other end will terminate the flist transfer. */
442 if (flags == 0 && !S_ISDIR(mode))
443 flags |= FLAG_DELETE; /* NOTE: no meaning for non-dir */
444 if (protocol_version >= 28) {
445 if ((flags & 0xFF00) || flags == 0) {
446 flags |= EXTENDED_FLAGS;
447 write_byte(f, flags);
448 write_byte(f, flags >> 8);
450 write_byte(f, flags);
454 write_byte(f, flags);
456 if (flags & SAME_NAME)
458 if (flags & LONG_NAME)
462 write_buf(f, fname + l1, l2);
464 write_longint(f, file->length);
465 if (!(flags & SAME_TIME))
466 write_int(f, modtime);
467 if (!(flags & SAME_MODE))
468 write_int(f, to_wire_mode(mode));
469 if (preserve_uid && !(flags & SAME_UID)) {
473 if (preserve_gid && !(flags & SAME_GID)) {
477 if (preserve_devices && IS_DEVICE(mode)) {
478 /* If SAME_HIGH_RDEV is off, SAME_RDEV_pre28 is also off.
479 * Also, avoid using "rdev" because it may be incomplete. */
480 if (!(flags & SAME_HIGH_RDEV))
481 write_int(f, file->u.rdev);
482 else if (protocol_version >= 28)
483 write_byte(f, file->u.rdev);
487 if (preserve_links && S_ISLNK(mode)) {
488 write_int(f, strlen(file->u.link));
489 write_buf(f, file->u.link, strlen(file->u.link));
493 #if SUPPORT_HARD_LINKS
494 if (flags & HAS_INODE_DATA) {
495 if (protocol_version < 26) {
496 /* 32-bit dev_t and ino_t */
498 write_int(f, file->F_INODE);
500 /* 64-bit dev_t and ino_t */
501 if (!(flags & SAME_DEV))
502 write_longint(f, dev);
503 write_longint(f, file->F_INODE);
508 if (always_checksum) {
512 else if (protocol_version < 28) {
513 /* Prior to 28, we sent a useless set of nulls. */
518 write_buf(f, sum, protocol_version < 21? 2
523 strlcpy(lastname, fname, MAXPATHLEN);
524 lastname[MAXPATHLEN - 1] = 0;
526 io_write_phase = "unknown";
531 void receive_file_entry(struct file_struct **fptr, unsigned short flags, int f)
533 static time_t modtime;
535 static DEV64_T rdev; /* just high bytes in p28 onward */
539 static char lastname[MAXPATHLEN];
540 char thisname[MAXPATHLEN];
541 unsigned int l1 = 0, l2 = 0;
543 struct file_struct *file;
546 modtime = 0, mode = 0;
553 if (flags & SAME_NAME)
556 if (flags & LONG_NAME)
561 file = new(struct file_struct);
563 out_of_memory("receive_file_entry");
564 memset((char *) file, 0, sizeof(*file));
567 if (l2 >= MAXPATHLEN - l1) {
569 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
570 flags, l1, l2, lastname);
571 overflow("receive_file_entry");
574 strlcpy(thisname, lastname, l1 + 1);
575 read_sbuf(f, &thisname[l1], l2);
576 thisname[l1 + l2] = 0;
578 strlcpy(lastname, thisname, MAXPATHLEN);
579 lastname[MAXPATHLEN - 1] = 0;
581 clean_fname(thisname);
583 if (sanitize_paths) {
584 sanitize_path(thisname, NULL);
587 if ((p = strrchr(thisname, '/'))) {
588 static char *lastdir;
590 if (lastdir && strcmp(thisname, lastdir) == 0)
591 file->dirname = lastdir;
593 file->dirname = strdup(thisname);
594 lastdir = file->dirname;
596 file->basename = strdup(p + 1);
598 file->dirname = NULL;
599 file->basename = strdup(thisname);
603 out_of_memory("receive_file_entry 1");
606 file->length = read_longint(f);
607 if (!(flags & SAME_TIME))
608 modtime = (time_t)read_int(f);
609 file->modtime = modtime;
610 if (!(flags & SAME_MODE))
611 mode = from_wire_mode(read_int(f));
615 if (!(flags & SAME_UID))
616 uid = (uid_t)read_int(f);
620 if (!(flags & SAME_GID))
621 gid = (gid_t)read_int(f);
624 if (preserve_devices) {
625 if (protocol_version < 28) {
626 if (IS_DEVICE(mode)) {
627 if (!(flags & SAME_RDEV_pre28))
628 rdev = (DEV64_T)read_int(f);
632 } else if (IS_DEVICE(mode)) {
633 if (!(flags & SAME_HIGH_RDEV)) {
634 file->u.rdev = (DEV64_T)read_int(f);
635 rdev = file->u.rdev & ~0xFF;
637 file->u.rdev = rdev | (DEV64_T)read_byte(f);
641 if (preserve_links && S_ISLNK(mode)) {
644 rprintf(FERROR, "overflow: l=%d\n", l);
645 overflow("receive_file_entry");
647 if (!(file->u.link = new_array(char, l + 1)))
648 out_of_memory("receive_file_entry 2");
649 read_sbuf(f, file->u.link, l);
651 sanitize_path(file->u.link, file->dirname);
653 #if SUPPORT_HARD_LINKS
654 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
655 flags |= HAS_INODE_DATA;
656 if (flags & HAS_INODE_DATA) {
657 if (!(file->link_u.idev = new(struct idev)))
658 out_of_memory("file inode data");
659 if (protocol_version < 26) {
661 file->F_INODE = read_int(f);
663 if (!(flags & SAME_DEV))
664 dev = read_longint(f);
665 file->F_INODE = read_longint(f);
671 if (always_checksum) {
674 sum = file->u.sum = new_array(char, MD4_SUM_LENGTH);
676 out_of_memory("md4 sum");
677 } else if (protocol_version < 28) {
678 /* Prior to 28, we get a useless set of nulls. */
683 read_buf(f, sum, protocol_version < 21? 2
688 if (!preserve_perms) {
689 extern int orig_umask;
690 /* set an appropriate set of permissions based on original
691 * permissions and umask. This emulates what GNU cp does */
692 file->mode &= ~orig_umask;
697 /* determine if a file in a different filesstem should be skipped
698 when one_file_system is set. We bascally only want to include
699 the mount points - but they can be hard to find! */
700 static int skip_filesystem(char *fname, STRUCT_STAT * st)
703 char *p = strrchr(fname, '/');
705 /* skip all but directories */
706 if (!S_ISDIR(st->st_mode))
709 /* if its not a subdirectory then allow */
714 if (link_stat(fname, &st2)) {
720 return (st2.st_dev != filesystem_dev);
723 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
724 /* IRIX cc cares that the operands to the ternary have the same type. */
725 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
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 string_area **ap,
745 struct file_struct *file;
747 char sum[SUM_LENGTH];
749 char cleaned_name[MAXPATHLEN];
750 char linkbuf[MAXPATHLEN];
752 strlcpy(cleaned_name, fname, MAXPATHLEN);
753 cleaned_name[MAXPATHLEN - 1] = 0;
754 clean_fname(cleaned_name);
756 sanitize_path(cleaned_name, NULL);
757 fname = cleaned_name;
759 memset(sum, 0, SUM_LENGTH);
761 if (readlink_stat(fname, &st, linkbuf) != 0) {
762 int save_errno = errno;
763 if (errno == ENOENT && exclude_level != NO_EXCLUDES) {
764 /* either symlink pointing nowhere or file that
765 * was removed during rsync run; see if excluded
766 * before reporting an error */
767 if (check_exclude_file(fname, 0, exclude_level)) {
768 /* file is excluded anyway, ignore silently */
772 io_error |= IOERR_GENERAL;
773 rprintf(FERROR, "readlink %s failed: %s\n",
774 full_fname(fname), strerror(save_errno));
778 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
779 if (exclude_level == NO_EXCLUDES)
782 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
783 rprintf(FINFO, "skipping directory %s\n", fname);
787 if (one_file_system && st.st_dev != filesystem_dev) {
788 if (skip_filesystem(fname, &st))
792 if (check_exclude_file(fname, S_ISDIR(st.st_mode) != 0, exclude_level))
795 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
801 rprintf(FINFO, "make_file(%s,*,%d)\n", fname, exclude_level);
803 file = new(struct file_struct);
805 out_of_memory("make_file");
806 memset((char *) file, 0, sizeof(*file));
808 if ((p = strrchr(fname, '/'))) {
809 static char *lastdir;
811 if (lastdir && strcmp(fname, lastdir) == 0)
812 file->dirname = lastdir;
814 file->dirname = strdup(fname);
815 lastdir = file->dirname;
817 file->basename = STRDUP(ap, p + 1);
820 file->dirname = NULL;
821 file->basename = STRDUP(ap, fname);
824 file->modtime = st.st_mtime;
825 file->length = st.st_size;
826 file->mode = st.st_mode;
827 file->uid = st.st_uid;
828 file->gid = st.st_gid;
829 if (preserve_hard_links) {
830 if (protocol_version < 28 ? S_ISREG(st.st_mode)
831 : !S_ISDIR(st.st_mode) && st.st_nlink > 1) {
832 if (!(file->link_u.idev = new(struct idev)))
833 out_of_memory("file inode data");
834 file->F_DEV = st.st_dev;
835 file->F_INODE = st.st_ino;
838 #ifdef HAVE_STRUCT_STAT_ST_RDEV
839 if (IS_DEVICE(st.st_mode))
840 file->u.rdev = st.st_rdev;
844 if (S_ISLNK(st.st_mode))
845 file->u.link = STRDUP(ap, linkbuf);
848 if (always_checksum && S_ISREG(st.st_mode)) {
849 if (!(file->u.sum = (char*)MALLOC(ap, MD4_SUM_LENGTH)))
850 out_of_memory("md4 sum");
851 file_checksum(fname, file->u.sum, st.st_size);
855 static char *lastdir;
856 if (lastdir && strcmp(lastdir, flist_dir) == 0)
857 file->basedir = lastdir;
859 file->basedir = strdup(flist_dir);
860 lastdir = file->basedir;
863 file->basedir = NULL;
865 if (!S_ISDIR(st.st_mode))
866 stats.total_size += st.st_size;
872 void send_file_name(int f, struct file_list *flist, char *fname,
873 int recursive, unsigned short base_flags)
875 struct file_struct *file;
876 char fbuf[MAXPATHLEN];
877 extern int delete_excluded;
879 /* f is set to -1 when calculating deletion file list */
880 file = make_file(fname, &flist->string_area,
881 f == -1 && delete_excluded? SERVER_EXCLUDES
887 maybe_emit_filelist_progress(flist);
892 file->flags |= FLAG_DELETE;
894 if (file->basename[0]) {
895 flist->files[flist->count++] = file;
896 send_file_entry(file, f, base_flags);
899 if (S_ISDIR(file->mode) && recursive) {
900 struct exclude_struct **last_exclude_list =
902 send_directory(f, flist, f_name_to(file, fbuf, sizeof fbuf));
903 local_exclude_list = last_exclude_list;
909 static void send_directory(int f, struct file_list *flist, char *dir)
913 char fname[MAXPATHLEN];
919 io_error |= IOERR_GENERAL;
920 rprintf(FERROR, "opendir %s failed: %s\n",
921 full_fname(dir), strerror(errno));
925 strlcpy(fname, dir, MAXPATHLEN);
927 if (fname[l - 1] != '/') {
928 if (l == MAXPATHLEN - 1) {
929 io_error |= IOERR_GENERAL;
930 rprintf(FERROR, "skipping long-named directory: %s\n",
935 strlcat(fname, "/", MAXPATHLEN);
938 p = fname + strlen(fname);
940 local_exclude_list = NULL;
943 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN - 1) {
944 strcpy(p, ".cvsignore");
945 add_exclude_file(&local_exclude_list,fname,MISSING_OK,ADD_EXCLUDE);
947 io_error |= IOERR_GENERAL;
949 "cannot cvs-exclude in long-named directory %s\n",
954 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
955 char *dname = d_name(di);
956 if (dname[0] == '.' && (dname[1] == '\0'
957 || (dname[1] == '.' && dname[2] == '\0')))
959 strlcpy(p, dname, MAXPATHLEN - l);
960 send_file_name(f, flist, fname, recurse, 0);
963 io_error |= IOERR_GENERAL;
964 rprintf(FERROR, "readdir(%s): (%d) %s\n",
965 dir, errno, strerror(errno));
968 if (local_exclude_list)
969 free_exclude_list(&local_exclude_list); /* Zeros pointer too */
976 * The delete_files() function in receiver.c sets f to -1 so that we just
977 * construct the file list in memory without sending it over the wire. It
978 * also has the side-effect of ignoring user-excludes if delete_excluded
979 * is set (so that the delete list includes user-excluded files).
981 struct file_list *send_file_list(int f, int argc, char *argv[])
985 char *p, *dir, *olddir;
986 char lastpath[MAXPATHLEN] = "";
987 struct file_list *flist;
991 if (show_filelist_p() && f != -1)
992 start_filelist_progress("building file list");
994 start_write = stats.total_written;
999 io_start_buffering_out(f);
1000 if (filesfrom_fd >= 0) {
1001 if (argv[0] && !push_dir(argv[0], 0)) {
1002 rprintf(FERROR, "push_dir %s failed: %s\n",
1003 full_fname(argv[0]), strerror(errno));
1004 exit_cleanup(RERR_FILESELECT);
1011 char fname2[MAXPATHLEN];
1012 char *fname = fname2;
1015 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1017 sanitize_path(fname, NULL);
1021 strlcpy(fname, *argv++, MAXPATHLEN);
1023 sanitize_path(fname, NULL);
1027 if (fname[l - 1] == '/') {
1028 if (l == 2 && fname[0] == '.') {
1029 /* Turn "./" into just "." rather than "./." */
1032 strlcat(fname, ".", MAXPATHLEN);
1036 if (link_stat(fname, &st) != 0) {
1038 io_error |= IOERR_GENERAL;
1039 rprintf(FERROR, "link_stat %s failed: %s\n",
1040 full_fname(fname), strerror(errno));
1045 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
1046 rprintf(FINFO, "skipping directory %s\n", fname);
1053 if (!relative_paths) {
1054 p = strrchr(fname, '/');
1063 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1064 /* this ensures we send the intermediate directories,
1065 thus getting their permissions right */
1066 char *lp = lastpath, *fn = fname, *slash = fname;
1068 /* Skip any initial directories in our path that we
1069 * have in common with lastpath. */
1070 while (*fn && *lp == *fn) {
1076 if (fn != p || (*lp && *lp != '/')) {
1077 int copy_links_saved = copy_links;
1078 int recurse_saved = recurse;
1079 copy_links = copy_unsafe_links;
1080 /* set recurse to 1 to prevent make_file
1081 * from ignoring directory, but still
1082 * turn off the recursive parameter to
1085 while ((slash = strchr(slash+1, '/')) != 0) {
1087 send_file_name(f, flist, fname, 0, 0);
1090 copy_links = copy_links_saved;
1091 recurse = recurse_saved;
1093 strlcpy(lastpath, fname, sizeof lastpath);
1102 olddir = push_dir(dir, 1);
1105 io_error |= IOERR_GENERAL;
1106 rprintf(FERROR, "push_dir %s failed: %s\n",
1107 full_fname(dir), strerror(errno));
1114 if (one_file_system)
1115 set_filesystem(fname);
1117 send_file_name(f, flist, fname, recurse, FLAG_DELETE);
1119 if (olddir != NULL) {
1121 if (pop_dir(olddir) != 0) {
1122 rprintf(FERROR, "pop_dir %s failed: %s\n",
1123 full_fname(dir), strerror(errno));
1124 exit_cleanup(RERR_FILESELECT);
1130 send_file_entry(NULL, f, 0);
1132 if (show_filelist_p())
1133 finish_filelist_progress(flist);
1136 clean_flist(flist, 0, 0);
1139 /* Now send the uid/gid list. This was introduced in
1140 * protocol version 15 */
1143 /* send the io_error flag */
1144 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1147 stats.flist_size = stats.total_written - start_write;
1148 stats.num_files = flist->count;
1150 write_batch_flist_info(flist->count, flist->files);
1154 rprintf(FINFO, "send_file_list done\n");
1160 struct file_list *recv_file_list(int f)
1162 struct file_list *flist;
1163 unsigned short flags;
1165 extern int list_only;
1167 if (show_filelist_p())
1168 start_filelist_progress("receiving file list");
1170 start_read = stats.total_read;
1172 flist = new(struct file_list);
1177 flist->malloced = 1000;
1178 flist->files = new_array(struct file_struct *, flist->malloced);
1183 while ((flags = read_byte(f)) != 0) {
1184 int i = flist->count;
1186 flist_expand(flist);
1188 if (protocol_version >= 28 && (flags & EXTENDED_FLAGS))
1189 flags |= read_byte(f) << 8;
1190 receive_file_entry(&flist->files[i], flags, f);
1192 if (S_ISREG(flist->files[i]->mode))
1193 stats.total_size += flist->files[i]->length;
1197 maybe_emit_filelist_progress(flist);
1200 rprintf(FINFO, "recv_file_name(%s)\n",
1201 f_name(flist->files[i]));
1204 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1207 rprintf(FINFO, "received %d names\n", flist->count);
1209 if (show_filelist_p())
1210 finish_filelist_progress(flist);
1212 clean_flist(flist, relative_paths, 1);
1215 /* Now send the uid/gid list. This was introduced in
1216 * protocol version 15 */
1217 recv_uid_list(f, flist);
1220 /* Recv the io_error flag */
1221 if (lp_ignore_errors(module_id) || ignore_errors)
1224 io_error |= read_int(f);
1230 for (i = 0; i < flist->count; i++)
1231 list_file_entry(flist->files[i]);
1235 rprintf(FINFO, "recv_file_list done\n");
1237 stats.flist_size = stats.total_read - start_read;
1238 stats.num_files = flist->count;
1243 out_of_memory("recv_file_list");
1244 return NULL; /* not reached */
1248 int file_compare(struct file_struct **file1, struct file_struct **file2)
1250 struct file_struct *f1 = *file1;
1251 struct file_struct *f2 = *file2;
1253 if (!f1->basename && !f2->basename)
1259 if (f1->dirname == f2->dirname)
1260 return u_strcmp(f1->basename, f2->basename);
1261 return f_name_cmp(f1, f2);
1265 int flist_find(struct file_list *flist, struct file_struct *f)
1267 int low = 0, high = flist->count - 1;
1269 while (high >= 0 && !flist->files[high]->basename) high--;
1274 while (low != high) {
1275 int mid = (low + high) / 2;
1276 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1278 return flist_up(flist, mid);
1285 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1286 return flist_up(flist, low);
1294 void free_file(struct file_struct *file)
1299 free(file->basename);
1300 if (!IS_DEVICE(file->mode) && file->u.link)
1301 free(file->u.link); /* Handles u.sum too. */
1302 if (file->link_u.idev)
1303 free((char*)file->link_u.idev); /* Handles link_u.links too. */
1309 * allocate a new file list
1311 struct file_list *flist_new(void)
1313 struct file_list *flist;
1315 flist = new(struct file_list);
1317 out_of_memory("send_file_list");
1320 flist->malloced = 0;
1321 flist->files = NULL;
1324 flist->string_area = string_area_new(0);
1326 flist->string_area = NULL;
1332 * free up all elements in a flist
1334 void flist_free(struct file_list *flist)
1337 for (i = 1; i < flist->count; i++) {
1338 if (!flist->string_area)
1339 free_file(flist->files[i]);
1340 free(flist->files[i]);
1342 /* FIXME: I don't think we generally need to blank the flist
1343 * since it's about to be freed. This will just cause more
1344 * memory traffic. If you want a freed-memory debugger, you
1345 * know where to get it. */
1346 memset((char *) flist->files, 0,
1347 sizeof(flist->files[0]) * flist->count);
1349 if (flist->string_area)
1350 string_area_free(flist->string_area);
1351 memset((char *) flist, 0, sizeof(*flist));
1357 * This routine ensures we don't have any duplicate names in our file list.
1358 * duplicate names can cause corruption because of the pipelining
1360 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1364 if (!flist || flist->count == 0)
1367 qsort(flist->files, flist->count,
1368 sizeof(flist->files[0]), (int (*)()) file_compare);
1370 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1371 if (flist->files[i]->basename) {
1376 while (++i < flist->count) {
1377 if (!flist->files[i]->basename)
1379 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1380 if (verbose > 1 && !am_server) {
1382 "removing duplicate name %s from file list %d\n",
1383 f_name(flist->files[i]), i);
1385 /* Make sure that if we unduplicate '.', that we don't
1386 * lose track of a user-specified starting point (or
1387 * else deletions will mysteriously fail with -R). */
1388 if (flist->files[i]->flags & FLAG_DELETE)
1389 flist->files[prev_i]->flags |= FLAG_DELETE;
1390 /* it's not great that the flist knows the semantics of
1391 * the file memory usage, but i'd rather not add a flag
1392 * byte to that struct.
1393 * XXX can i use a bit in the flags field? */
1394 if (flist->string_area)
1395 flist->files[i][0] = null_file;
1397 free_file(flist->files[i]);
1403 /* we need to strip off the root directory in the case
1404 of relative paths, but this must be done _after_
1405 the sorting phase */
1406 for (i = 0; i < flist->count; i++) {
1407 if (flist->files[i]->dirname &&
1408 flist->files[i]->dirname[0] == '/') {
1409 memmove(&flist->files[i]->dirname[0],
1410 &flist->files[i]->dirname[1],
1411 strlen(flist->files[i]->dirname));
1414 if (flist->files[i]->dirname &&
1415 !flist->files[i]->dirname[0]) {
1416 flist->files[i]->dirname = NULL;
1424 for (i = 0; i < flist->count; i++) {
1425 rprintf(FINFO, "[%ld] i=%d %s %s mode=0%o len=%.0f\n",
1427 NS(flist->files[i]->dirname),
1428 NS(flist->files[i]->basename),
1429 (int) flist->files[i]->mode,
1430 (double) flist->files[i]->length);
1435 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1437 /* Compare the names of two file_struct entities, just like strcmp()
1438 * would do if it were operating on the joined strings. We assume
1439 * that there are no 0-length strings.
1441 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1444 const uchar *c1, *c2;
1445 enum fnc_state state1, state2;
1447 if (!f1 || !f1->basename) {
1448 if (!f2 || !f2->basename)
1452 if (!f2 || !f2->basename)
1455 if (!(c1 = (uchar*)f1->dirname)) {
1457 c1 = (uchar*)f1->basename;
1460 if (!(c2 = (uchar*)f2->dirname)) {
1462 c2 = (uchar*)f2->basename;
1467 if ((dif = (int)*c1 - (int)*c2) != 0)
1477 c1 = (uchar*)f1->basename;
1491 c2 = (uchar*)f2->basename;
1505 /* Return a copy of the full filename of a flist entry, using the indicated
1508 char *f_name_to(struct file_struct *f, char *fbuf, int bsize)
1510 if (!f || !f->basename)
1514 int off = strlcpy(fbuf, f->dirname, bsize);
1515 off += strlcpy(fbuf + off, "/", bsize - off);
1516 strlcpy(fbuf + off, f->basename, bsize - off);
1518 strlcpy(fbuf, f->basename, bsize);
1523 /* Like f_name_to(), but we rotate through 5 static buffers of our own.
1525 char *f_name(struct file_struct *f)
1527 static char names[5][MAXPATHLEN];
1528 static unsigned int n;
1530 n = (n + 1) % (sizeof names / sizeof names[0]);
1532 return f_name_to(f, names[n], sizeof names[0]);