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;
74 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
77 static int show_filelist_p(void)
79 return verbose && (recurse || files_from) && !am_server;
82 static void start_filelist_progress(char *kind)
84 rprintf(FINFO, "%s ... ", kind);
85 if ((verbose > 1) || do_progress)
91 static void emit_filelist_progress(const struct file_list *flist)
93 rprintf(FINFO, " %d files...\r", flist->count);
97 static void maybe_emit_filelist_progress(const struct file_list *flist)
99 if (do_progress && show_filelist_p() && ((flist->count % 100) == 0))
100 emit_filelist_progress(flist);
104 static void finish_filelist_progress(const struct file_list *flist)
107 /* This overwrites the progress line */
108 rprintf(FINFO, "%d file%sto consider\n",
109 flist->count, flist->count == 1 ? " " : "s ");
111 rprintf(FINFO, "done\n");
114 void show_flist_stats(void)
120 static struct string_area *string_area_new(int size)
122 struct string_area *a;
126 a = new(struct string_area);
128 out_of_memory("string_area_new");
129 a->current = a->base = new_array(char, size);
131 out_of_memory("string_area_new buffer");
132 a->end = a->base + size;
138 static void string_area_free(struct string_area *a)
140 struct string_area *next;
142 for (; a; a = next) {
148 static char *string_area_malloc(struct string_area **ap, int size)
151 struct string_area *a;
153 /* does the request fit into the current space? */
155 if (a->current + size >= a->end) {
156 /* no; get space, move new string_area to front of the list */
157 a = string_area_new(size > ARENA_SIZE ? size : ARENA_SIZE);
162 /* have space; do the "allocation." */
168 static char *string_area_strdup(struct string_area **ap, const char *src)
170 char *dest = string_area_malloc(ap, strlen(src) + 1);
171 return strcpy(dest, src);
174 static void list_file_entry(struct file_struct *f)
179 /* this can happen if duplicate names were removed */
182 permstring(perms, f->mode);
184 if (preserve_links && S_ISLNK(f->mode)) {
185 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
187 (double) f->length, timestring(f->modtime),
190 rprintf(FINFO, "%s %11.0f %s %s\n",
192 (double) f->length, timestring(f->modtime),
199 * Stat either a symlink or its referent, depending on the settings of
200 * copy_links, copy_unsafe_links, etc.
202 * @retval -1 on error
204 * @retval 0 for success
206 * @post If @p path is a symlink, then @p linkbuf (of size @c
207 * MAXPATHLEN) contains the symlink target.
209 * @post @p buffer contains information about the link or the
210 * referrent as appropriate, if they exist.
212 int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
216 return do_stat(path, buffer);
217 if (do_lstat(path, buffer) == -1)
219 if (S_ISLNK(buffer->st_mode)) {
220 int l = readlink((char *) path, linkbuf, MAXPATHLEN - 1);
224 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
226 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
229 return do_stat(path, buffer);
234 return do_stat(path, buffer);
238 int link_stat(const char *path, STRUCT_STAT * buffer)
242 return do_stat(path, buffer);
243 return do_lstat(path, buffer);
245 return do_stat(path, buffer);
250 * This function is used to check if a file should be included/excluded
251 * from the list of files based on its name and type etc. The value of
252 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
254 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
256 #if 0 /* This currently never happens, so avoid a useless compare. */
257 if (exclude_level == NO_EXCLUDES)
261 /* never exclude '.', even if somebody does --exclude '*' */
262 if (fname[0] == '.' && !fname[1])
264 /* Handle the -R version of the '.' dir. */
265 if (fname[0] == '/') {
266 int len = strlen(fname);
267 if (fname[len-1] == '.' && fname[len-2] == '/')
271 if (server_exclude_list
272 && check_exclude(server_exclude_list, fname, is_dir))
274 if (exclude_level != ALL_EXCLUDES)
276 if (exclude_list && check_exclude(exclude_list, fname, is_dir))
278 if (local_exclude_list
279 && check_exclude(local_exclude_list, fname, is_dir))
284 /* used by the one_file_system code */
285 static dev_t filesystem_dev;
287 static void set_filesystem(char *fname)
290 if (link_stat(fname, &st) != 0)
292 filesystem_dev = st.st_dev;
296 static int to_wire_mode(mode_t mode)
298 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
299 return (mode & ~(_S_IFMT)) | 0120000;
303 static mode_t from_wire_mode(int mode)
305 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
306 return (mode & ~(_S_IFMT)) | _S_IFLNK;
307 return (mode_t) mode;
311 static void send_directory(int f, struct file_list *flist, char *dir);
313 static char *flist_dir;
317 * Make sure @p flist is big enough to hold at least @p flist->count
320 static void flist_expand(struct file_list *flist)
322 if (flist->count >= flist->malloced) {
325 if (flist->malloced < 1000)
326 flist->malloced += 1000;
328 flist->malloced *= 2;
331 new_ptr = realloc_array(flist->files,
332 struct file_struct *,
335 new_ptr = new_array(struct file_struct *,
340 rprintf(FINFO, "expand file_list to %.0f bytes, did%s move\n",
341 (double)sizeof(flist->files[0])
343 (new_ptr == flist->files) ? " not" : "");
346 flist->files = (struct file_struct **) new_ptr;
349 out_of_memory("flist_expand");
354 static void send_file_entry(struct file_struct *file, int f,
355 unsigned short base_flags)
357 unsigned short flags;
358 static time_t last_time;
359 static mode_t last_mode;
360 static DEV64_T last_rdev;
361 static uid_t last_uid;
362 static gid_t last_gid;
363 static char lastname[MAXPATHLEN];
364 char *fname, fbuf[MAXPATHLEN];
375 io_write_phase = "send_file_entry";
377 fname = f_name_to(file, fbuf, sizeof fbuf);
381 if (file->mode == last_mode)
384 last_mode = file->mode;
385 if (preserve_devices) {
386 if (protocol_version < 28) {
387 if (IS_DEVICE(last_mode) && file->rdev == last_rdev)
388 flags |= OLD_SAME_RDEV|SAME_HIGH_RDEV;
390 last_rdev = file->rdev;
392 else if (IS_DEVICE(last_mode)) {
393 if ((file->rdev & ~0xFF) == (last_rdev & ~0xFF))
394 flags |= SAME_HIGH_RDEV;
395 last_rdev = file->rdev;
398 if (file->uid == last_uid)
401 last_uid = file->uid;
402 if (file->gid == last_gid)
405 last_gid = file->gid;
406 if (file->modtime == last_time)
409 last_time = file->modtime;
412 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
414 l2 = strlen(fname) - l1;
421 /* We must make sure we don't send a zero flags byte or
422 * the other end will terminate the flist transfer. */
423 if (flags == 0 && !S_ISDIR(last_mode))
424 flags |= FLAG_DELETE; /* NOTE: no meaning for non-dir */
425 if (protocol_version >= 28) {
426 if ((flags & 0xFF00) || flags == 0) {
427 flags |= EXTENDED_FLAGS;
428 write_byte(f, flags);
429 write_byte(f, flags >> 8);
431 write_byte(f, flags);
435 write_byte(f, flags);
437 if (flags & SAME_NAME)
439 if (flags & LONG_NAME)
443 write_buf(f, fname + l1, l2);
445 write_longint(f, file->length);
446 if (!(flags & SAME_TIME))
447 write_int(f, last_time);
448 if (!(flags & SAME_MODE))
449 write_int(f, to_wire_mode(last_mode));
450 if (preserve_uid && !(flags & SAME_UID)) {
452 write_int(f, last_uid);
454 if (preserve_gid && !(flags & SAME_GID)) {
456 write_int(f, last_gid);
458 if (preserve_devices && IS_DEVICE(last_mode)) {
459 /* If SAME_HIGH_RDEV is off, OLD_SAME_RDEV is also off. */
460 if (!(flags & SAME_HIGH_RDEV))
461 write_int(f, last_rdev);
462 else if (protocol_version >= 28)
463 write_byte(f, last_rdev);
467 if (preserve_links && S_ISLNK(last_mode)) {
468 write_int(f, strlen(file->link));
469 write_buf(f, file->link, strlen(file->link));
473 #if SUPPORT_HARD_LINKS
474 if (preserve_hard_links && S_ISREG(file->mode)) {
475 if (protocol_version < 26) {
476 /* 32-bit dev_t and ino_t */
477 write_int(f, (int) file->dev);
478 write_int(f, (int) file->inode);
480 /* 64-bit dev_t and ino_t */
481 write_longint(f, file->dev);
482 write_longint(f, file->inode);
487 if (always_checksum) {
488 if (protocol_version < 21)
489 write_buf(f, file->sum, 2);
491 write_buf(f, file->sum, MD4_SUM_LENGTH);
494 strlcpy(lastname, fname, MAXPATHLEN);
495 lastname[MAXPATHLEN - 1] = 0;
497 io_write_phase = "unknown";
502 static void receive_file_entry(struct file_struct **fptr,
503 unsigned short flags, int f)
505 static time_t last_time;
506 static mode_t last_mode;
507 static DEV64_T last_rdev;
508 static uid_t last_uid;
509 static gid_t last_gid;
510 static char lastname[MAXPATHLEN];
511 char thisname[MAXPATHLEN];
512 unsigned int l1 = 0, l2 = 0;
514 struct file_struct *file;
516 if (flags & SAME_NAME)
519 if (flags & LONG_NAME)
524 file = new(struct file_struct);
526 out_of_memory("receive_file_entry");
527 memset((char *) file, 0, sizeof(*file));
530 if (l2 >= MAXPATHLEN - l1) {
532 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
533 flags, l1, l2, lastname);
534 overflow("receive_file_entry");
537 strlcpy(thisname, lastname, l1 + 1);
538 read_sbuf(f, &thisname[l1], l2);
539 thisname[l1 + l2] = 0;
541 strlcpy(lastname, thisname, MAXPATHLEN);
542 lastname[MAXPATHLEN - 1] = 0;
544 clean_fname(thisname);
546 if (sanitize_paths) {
547 sanitize_path(thisname, NULL);
550 if ((p = strrchr(thisname, '/'))) {
551 static char *lastdir;
553 if (lastdir && strcmp(thisname, lastdir) == 0)
554 file->dirname = lastdir;
556 file->dirname = strdup(thisname);
557 lastdir = file->dirname;
559 file->basename = strdup(p + 1);
561 file->dirname = NULL;
562 file->basename = strdup(thisname);
566 out_of_memory("receive_file_entry 1");
569 file->length = read_longint(f);
570 if (!(flags & SAME_TIME))
571 last_time = (time_t)read_int(f);
572 file->modtime = last_time;
573 if (!(flags & SAME_MODE))
574 last_mode = from_wire_mode(read_int(f));
575 file->mode = last_mode;
578 if (!(flags & SAME_UID))
579 last_uid = (uid_t)read_int(f);
580 file->uid = last_uid;
583 if (!(flags & SAME_GID))
584 last_gid = (gid_t)read_int(f);
585 file->gid = last_gid;
587 if (preserve_devices) {
588 if (protocol_version < 28) {
589 if (IS_DEVICE(last_mode)) {
590 if (!(flags & OLD_SAME_RDEV))
591 last_rdev = (DEV64_T)read_int(f);
592 file->rdev = last_rdev;
595 } else if (IS_DEVICE(last_mode)) {
596 if (!(flags & SAME_HIGH_RDEV))
597 last_rdev = (DEV64_T)read_int(f);
599 last_rdev = (DEV64_T)((last_rdev & ~0xFF)
602 file->rdev = last_rdev;
606 if (preserve_links && S_ISLNK(last_mode)) {
609 rprintf(FERROR, "overflow: l=%d\n", l);
610 overflow("receive_file_entry");
612 file->link = new_array(char, l + 1);
614 out_of_memory("receive_file_entry 2");
615 read_sbuf(f, file->link, l);
617 sanitize_path(file->link, file->dirname);
619 #if SUPPORT_HARD_LINKS
620 if (preserve_hard_links && S_ISREG(file->mode)) {
621 if (protocol_version < 26) {
622 file->dev = read_int(f);
623 file->inode = read_int(f);
625 file->dev = read_longint(f);
626 file->inode = read_longint(f);
631 if (always_checksum) {
632 file->sum = new_array(char, MD4_SUM_LENGTH);
634 out_of_memory("md4 sum");
635 if (protocol_version < 21)
636 read_buf(f, file->sum, 2);
638 read_buf(f, file->sum, MD4_SUM_LENGTH);
641 if (!preserve_perms) {
642 extern int orig_umask;
643 /* set an appropriate set of permissions based on original
644 permissions and umask. This emulates what GNU cp does */
645 file->mode &= ~orig_umask;
650 /* determine if a file in a different filesstem should be skipped
651 when one_file_system is set. We bascally only want to include
652 the mount points - but they can be hard to find! */
653 static int skip_filesystem(char *fname, STRUCT_STAT * st)
656 char *p = strrchr(fname, '/');
658 /* skip all but directories */
659 if (!S_ISDIR(st->st_mode))
662 /* if its not a subdirectory then allow */
667 if (link_stat(fname, &st2)) {
673 return (st2.st_dev != filesystem_dev);
676 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
677 /* IRIX cc cares that the operands to the ternary have the same type. */
678 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
681 * Create a file_struct for a named file by reading its stat()
682 * information and performing extensive checks against global
685 * @return the new file, or NULL if there was an error or this file
686 * should be excluded.
688 * @todo There is a small optimization opportunity here to avoid
689 * stat()ing the file in some circumstances, which has a certain cost.
690 * We are called immediately after doing readdir(), and so we may
691 * already know the d_type of the file. We could for example avoid
692 * statting directories if we're not recursing, but this is not a very
693 * important case. Some systems may not have d_type.
695 struct file_struct *make_file(char *fname, struct string_area **ap,
698 struct file_struct *file;
700 char sum[SUM_LENGTH];
702 char cleaned_name[MAXPATHLEN];
703 char linkbuf[MAXPATHLEN];
704 extern int module_id;
706 strlcpy(cleaned_name, fname, MAXPATHLEN);
707 cleaned_name[MAXPATHLEN - 1] = 0;
708 clean_fname(cleaned_name);
710 sanitize_path(cleaned_name, NULL);
711 fname = cleaned_name;
713 memset(sum, 0, SUM_LENGTH);
715 if (readlink_stat(fname, &st, linkbuf) != 0) {
716 int save_errno = errno;
717 if (errno == ENOENT && exclude_level != NO_EXCLUDES) {
718 /* either symlink pointing nowhere or file that
719 * was removed during rsync run; see if excluded
720 * before reporting an error */
721 if (check_exclude_file(fname, 0, exclude_level)) {
722 /* file is excluded anyway, ignore silently */
726 io_error |= IOERR_GENERAL;
727 rprintf(FERROR, "readlink %s failed: %s\n",
728 full_fname(fname), strerror(save_errno));
732 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
733 if (exclude_level == NO_EXCLUDES)
736 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
737 rprintf(FINFO, "skipping directory %s\n", fname);
741 if (one_file_system && st.st_dev != filesystem_dev) {
742 if (skip_filesystem(fname, &st))
746 if (check_exclude_file(fname, S_ISDIR(st.st_mode) != 0, exclude_level))
749 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
755 rprintf(FINFO, "make_file(%s,*,%d)\n", fname, exclude_level);
757 file = new(struct file_struct);
759 out_of_memory("make_file");
760 memset((char *) file, 0, sizeof(*file));
762 if ((p = strrchr(fname, '/'))) {
763 static char *lastdir;
765 if (lastdir && strcmp(fname, lastdir) == 0)
766 file->dirname = lastdir;
768 file->dirname = strdup(fname);
769 lastdir = file->dirname;
771 file->basename = STRDUP(ap, p + 1);
774 file->dirname = NULL;
775 file->basename = STRDUP(ap, fname);
778 file->modtime = st.st_mtime;
779 file->length = st.st_size;
780 file->mode = st.st_mode;
781 file->uid = st.st_uid;
782 file->gid = st.st_gid;
783 file->dev = st.st_dev;
784 file->inode = st.st_ino;
785 #ifdef HAVE_STRUCT_STAT_ST_RDEV
786 file->rdev = st.st_rdev;
790 if (S_ISLNK(st.st_mode))
791 file->link = STRDUP(ap, linkbuf);
794 if (always_checksum) {
795 file->sum = (char *) MALLOC(ap, MD4_SUM_LENGTH);
797 out_of_memory("md4 sum");
798 /* drat. we have to provide a null checksum for non-regular
799 files in order to be compatible with earlier versions
801 if (S_ISREG(st.st_mode)) {
802 file_checksum(fname, file->sum, st.st_size);
804 memset(file->sum, 0, MD4_SUM_LENGTH);
809 static char *lastdir;
810 if (lastdir && strcmp(lastdir, flist_dir) == 0)
811 file->basedir = lastdir;
813 file->basedir = strdup(flist_dir);
814 lastdir = file->basedir;
817 file->basedir = NULL;
819 if (!S_ISDIR(st.st_mode))
820 stats.total_size += st.st_size;
826 void send_file_name(int f, struct file_list *flist, char *fname,
827 int recursive, unsigned short base_flags)
829 struct file_struct *file;
830 char fbuf[MAXPATHLEN];
831 extern int delete_excluded;
833 /* f is set to -1 when calculating deletion file list */
834 file = make_file(fname, &flist->string_area,
835 f == -1 && delete_excluded? SERVER_EXCLUDES
841 maybe_emit_filelist_progress(flist);
846 file->flags |= FLAG_DELETE;
848 if (file->basename[0]) {
849 flist->files[flist->count++] = file;
850 send_file_entry(file, f, base_flags);
853 if (S_ISDIR(file->mode) && recursive) {
854 struct exclude_struct **last_exclude_list =
856 send_directory(f, flist, f_name_to(file, fbuf, sizeof fbuf));
857 local_exclude_list = last_exclude_list;
863 static void send_directory(int f, struct file_list *flist, char *dir)
867 char fname[MAXPATHLEN];
873 io_error |= IOERR_GENERAL;
874 rprintf(FERROR, "opendir %s failed: %s\n",
875 full_fname(dir), strerror(errno));
879 strlcpy(fname, dir, MAXPATHLEN);
881 if (fname[l - 1] != '/') {
882 if (l == MAXPATHLEN - 1) {
883 io_error |= IOERR_GENERAL;
884 rprintf(FERROR, "skipping long-named directory: %s\n",
889 strlcat(fname, "/", MAXPATHLEN);
892 p = fname + strlen(fname);
894 local_exclude_list = NULL;
897 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN - 1) {
898 strcpy(p, ".cvsignore");
899 add_exclude_file(&exclude_list,fname,MISSING_OK,ADD_EXCLUDE);
901 io_error |= IOERR_GENERAL;
903 "cannot cvs-exclude in long-named directory %s\n",
908 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
909 char *dname = d_name(di);
910 if (dname[0] == '.' && (dname[1] == '\0'
911 || (dname[1] == '.' && dname[2] == '\0')))
913 strlcpy(p, dname, MAXPATHLEN - l);
914 send_file_name(f, flist, fname, recurse, 0);
917 io_error |= IOERR_GENERAL;
918 rprintf(FERROR, "readdir(%s): (%d) %s\n",
919 dir, errno, strerror(errno));
922 if (local_exclude_list)
923 free_exclude_list(&local_exclude_list); /* Zeros pointer too */
930 * The delete_files() function in receiver.c sets f to -1 so that we just
931 * construct the file list in memory without sending it over the wire. It
932 * also has the side-effect of ignoring user-excludes if delete_excluded
933 * is set (so that the delete list includes user-excluded files).
935 struct file_list *send_file_list(int f, int argc, char *argv[])
939 char *p, *dir, *olddir;
940 char lastpath[MAXPATHLEN] = "";
941 struct file_list *flist;
945 if (show_filelist_p() && f != -1)
946 start_filelist_progress("building file list");
948 start_write = stats.total_written;
953 io_start_buffering_out(f);
954 if (filesfrom_fd >= 0) {
955 if (argv[0] && !push_dir(argv[0], 0)) {
956 rprintf(FERROR, "push_dir %s failed: %s\n",
957 full_fname(argv[0]), strerror(errno));
958 exit_cleanup(RERR_FILESELECT);
965 char fname2[MAXPATHLEN];
966 char *fname = fname2;
969 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
971 sanitize_path(fname, NULL);
975 strlcpy(fname, *argv++, MAXPATHLEN);
977 sanitize_path(fname, NULL);
981 if (fname[l - 1] == '/') {
982 if (l == 2 && fname[0] == '.') {
983 /* Turn "./" into just "." rather than "./." */
986 strlcat(fname, ".", MAXPATHLEN);
990 if (link_stat(fname, &st) != 0) {
992 io_error |= IOERR_GENERAL;
993 rprintf(FERROR, "link_stat %s failed: %s\n",
994 full_fname(fname), strerror(errno));
999 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
1000 rprintf(FINFO, "skipping directory %s\n", fname);
1007 if (!relative_paths) {
1008 p = strrchr(fname, '/');
1017 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1018 /* this ensures we send the intermediate directories,
1019 thus getting their permissions right */
1020 char *lp = lastpath, *fn = fname, *slash = fname;
1022 /* Skip any initial directories in our path that we
1023 * have in common with lastpath. */
1024 while (*fn && *lp == *fn) {
1030 if (fn != p || (*lp && *lp != '/')) {
1031 int copy_links_saved = copy_links;
1032 int recurse_saved = recurse;
1033 copy_links = copy_unsafe_links;
1034 /* set recurse to 1 to prevent make_file
1035 * from ignoring directory, but still
1036 * turn off the recursive parameter to
1039 while ((slash = strchr(slash+1, '/')) != 0) {
1041 send_file_name(f, flist, fname, 0, 0);
1044 copy_links = copy_links_saved;
1045 recurse = recurse_saved;
1047 strlcpy(lastpath, fname, sizeof lastpath);
1056 olddir = push_dir(dir, 1);
1059 io_error |= IOERR_GENERAL;
1060 rprintf(FERROR, "push_dir %s failed: %s\n",
1061 full_fname(dir), strerror(errno));
1068 if (one_file_system)
1069 set_filesystem(fname);
1071 send_file_name(f, flist, fname, recurse, FLAG_DELETE);
1073 if (olddir != NULL) {
1075 if (pop_dir(olddir) != 0) {
1076 rprintf(FERROR, "pop_dir %s failed: %s\n",
1077 full_fname(dir), strerror(errno));
1078 exit_cleanup(RERR_FILESELECT);
1084 send_file_entry(NULL, f, 0);
1086 if (show_filelist_p() && f != -1)
1087 finish_filelist_progress(flist);
1089 clean_flist(flist, 0, 0);
1091 /* now send the uid/gid list. This was introduced in protocol
1096 /* send the io_error flag */
1098 extern int module_id;
1099 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1104 stats.flist_size = stats.total_written - start_write;
1105 stats.num_files = flist->count;
1107 write_batch_flist_info(flist->count, flist->files);
1111 rprintf(FINFO, "send_file_list done\n");
1117 struct file_list *recv_file_list(int f)
1119 struct file_list *flist;
1120 unsigned short flags;
1122 extern int list_only;
1124 if (show_filelist_p())
1125 start_filelist_progress("receiving file list");
1127 start_read = stats.total_read;
1129 flist = new(struct file_list);
1134 flist->malloced = 1000;
1135 flist->files = new_array(struct file_struct *, flist->malloced);
1140 while ((flags = read_byte(f)) != 0) {
1141 int i = flist->count;
1143 flist_expand(flist);
1145 if (protocol_version >= 28 && (flags & EXTENDED_FLAGS))
1146 flags |= read_byte(f) << 8;
1147 receive_file_entry(&flist->files[i], flags, f);
1149 if (S_ISREG(flist->files[i]->mode))
1150 stats.total_size += flist->files[i]->length;
1154 maybe_emit_filelist_progress(flist);
1157 rprintf(FINFO, "recv_file_name(%s)\n",
1158 f_name(flist->files[i]));
1164 rprintf(FINFO, "received %d names\n", flist->count);
1166 clean_flist(flist, relative_paths, 1);
1168 if (show_filelist_p())
1169 finish_filelist_progress(flist);
1171 /* now recv the uid/gid list. This was introduced in protocol version 15 */
1173 recv_uid_list(f, flist);
1175 /* recv the io_error flag */
1176 if (f != -1 && !read_batch) { /* dw-added readbatch */
1177 extern int module_id;
1178 extern int ignore_errors;
1179 if (lp_ignore_errors(module_id) || ignore_errors)
1182 io_error |= read_int(f);
1187 for (i = 0; i < flist->count; i++)
1188 list_file_entry(flist->files[i]);
1193 rprintf(FINFO, "recv_file_list done\n");
1195 stats.flist_size = stats.total_read - start_read;
1196 stats.num_files = flist->count;
1201 out_of_memory("recv_file_list");
1202 return NULL; /* not reached */
1206 int file_compare(struct file_struct **file1, struct file_struct **file2)
1208 struct file_struct *f1 = *file1;
1209 struct file_struct *f2 = *file2;
1211 if (!f1->basename && !f2->basename)
1217 if (f1->dirname == f2->dirname)
1218 return u_strcmp(f1->basename, f2->basename);
1219 return f_name_cmp(f1, f2);
1223 int flist_find(struct file_list *flist, struct file_struct *f)
1225 int low = 0, high = flist->count - 1;
1227 while (high >= 0 && !flist->files[high]->basename) high--;
1232 while (low != high) {
1233 int mid = (low + high) / 2;
1234 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1236 return flist_up(flist, mid);
1243 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1244 return flist_up(flist, low);
1252 void free_file(struct file_struct *file)
1257 free(file->basename);
1267 * allocate a new file list
1269 struct file_list *flist_new(void)
1271 struct file_list *flist;
1273 flist = new(struct file_list);
1275 out_of_memory("send_file_list");
1278 flist->malloced = 0;
1279 flist->files = NULL;
1282 flist->string_area = string_area_new(0);
1284 flist->string_area = NULL;
1290 * free up all elements in a flist
1292 void flist_free(struct file_list *flist)
1295 for (i = 1; i < flist->count; i++) {
1296 if (!flist->string_area)
1297 free_file(flist->files[i]);
1298 free(flist->files[i]);
1300 /* FIXME: I don't think we generally need to blank the flist
1301 * since it's about to be freed. This will just cause more
1302 * memory traffic. If you want a freed-memory debugger, you
1303 * know where to get it. */
1304 memset((char *) flist->files, 0,
1305 sizeof(flist->files[0]) * flist->count);
1307 if (flist->string_area)
1308 string_area_free(flist->string_area);
1309 memset((char *) flist, 0, sizeof(*flist));
1315 * This routine ensures we don't have any duplicate names in our file list.
1316 * duplicate names can cause corruption because of the pipelining
1318 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1322 if (!flist || flist->count == 0)
1325 qsort(flist->files, flist->count,
1326 sizeof(flist->files[0]), (int (*)()) file_compare);
1328 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1329 if (flist->files[i]->basename) {
1334 while (++i < flist->count) {
1335 if (!flist->files[i]->basename)
1337 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1338 if (verbose > 1 && !am_server) {
1340 "removing duplicate name %s from file list %d\n",
1341 f_name(flist->files[i]), i);
1343 /* Make sure that if we unduplicate '.', that we don't
1344 * lose track of a user-specified starting point (or
1345 * else deletions will mysteriously fail with -R). */
1346 if (flist->files[i]->flags & FLAG_DELETE)
1347 flist->files[prev_i]->flags |= FLAG_DELETE;
1348 /* it's not great that the flist knows the semantics of
1349 * the file memory usage, but i'd rather not add a flag
1350 * byte to that struct.
1351 * XXX can i use a bit in the flags field? */
1352 if (flist->string_area)
1353 flist->files[i][0] = null_file;
1355 free_file(flist->files[i]);
1362 /* we need to strip off the root directory in the case
1363 of relative paths, but this must be done _after_
1364 the sorting phase */
1365 for (i = 0; i < flist->count; i++) {
1366 if (flist->files[i]->dirname &&
1367 flist->files[i]->dirname[0] == '/') {
1368 memmove(&flist->files[i]->dirname[0],
1369 &flist->files[i]->dirname[1],
1370 strlen(flist->files[i]->dirname));
1373 if (flist->files[i]->dirname &&
1374 !flist->files[i]->dirname[0]) {
1375 flist->files[i]->dirname = NULL;
1383 for (i = 0; i < flist->count; i++) {
1384 rprintf(FINFO, "[%ld] i=%d %s %s mode=0%o len=%.0f\n",
1386 NS(flist->files[i]->dirname),
1387 NS(flist->files[i]->basename),
1388 (int) flist->files[i]->mode,
1389 (double) flist->files[i]->length);
1394 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1396 /* Compare the names of two file_struct entities, just like strcmp()
1397 * would do if it were operating on the joined strings. We assume
1398 * that there are no 0-length strings.
1400 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1403 const uchar *c1, *c2;
1404 enum fnc_state state1, state2;
1406 if (!f1 || !f1->basename) {
1407 if (!f2 || !f2->basename)
1411 if (!f2 || !f2->basename)
1414 if (!(c1 = (uchar*)f1->dirname)) {
1416 c1 = (uchar*)f1->basename;
1420 if (!(c2 = (uchar*)f2->dirname)) {
1422 c2 = (uchar*)f2->basename;
1428 if ((dif = (int)*c1 - (int)*c2) != 0)
1438 c1 = (uchar*)f1->basename;
1452 c2 = (uchar*)f2->basename;
1466 /* Return a copy of the full filename of a flist entry, using the indicated
1469 char *f_name_to(struct file_struct *f, char *fbuf, int bsize)
1471 if (!f || !f->basename)
1475 int off = strlcpy(fbuf, f->dirname, bsize);
1476 off += strlcpy(fbuf + off, "/", bsize - off);
1477 strlcpy(fbuf + off, f->basename, bsize - off);
1479 strlcpy(fbuf, f->basename, bsize);
1484 /* Like f_name_to(), but we rotate through 5 static buffers of our own.
1486 char *f_name(struct file_struct *f)
1488 static char names[5][MAXPATHLEN];
1489 static unsigned int n;
1491 n = (n + 1) % (sizeof names / sizeof names[0]);
1493 return f_name_to(f, names[n], sizeof names[0]);