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 curr_dir[MAXPATHLEN];
47 extern char *files_from;
48 extern int filesfrom_fd;
50 extern int one_file_system;
51 extern int make_backups;
52 extern int preserve_links;
53 extern int preserve_hard_links;
54 extern int preserve_perms;
55 extern int preserve_devices;
56 extern int preserve_uid;
57 extern int preserve_gid;
58 extern int preserve_times;
59 extern int relative_paths;
60 extern int implied_dirs;
61 extern int copy_links;
62 extern int copy_unsafe_links;
63 extern int protocol_version;
64 extern int sanitize_paths;
66 extern int read_batch;
67 extern int write_batch;
69 extern struct exclude_struct **exclude_list;
70 extern struct exclude_struct **server_exclude_list;
71 extern struct exclude_struct **local_exclude_list;
75 static struct file_struct null_file;
76 static char empty_sum[MD4_SUM_LENGTH];
78 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
81 static int show_filelist_p(void)
83 return verbose && (recurse || files_from) && !am_server;
86 static void start_filelist_progress(char *kind)
88 rprintf(FINFO, "%s ... ", kind);
89 if ((verbose > 1) || do_progress)
95 static void emit_filelist_progress(const struct file_list *flist)
97 rprintf(FINFO, " %d files...\r", flist->count);
101 static void maybe_emit_filelist_progress(const struct file_list *flist)
103 if (do_progress && show_filelist_p() && ((flist->count % 100) == 0))
104 emit_filelist_progress(flist);
108 static void finish_filelist_progress(const struct file_list *flist)
111 /* This overwrites the progress line */
112 rprintf(FINFO, "%d file%sto consider\n",
113 flist->count, flist->count == 1 ? " " : "s ");
115 rprintf(FINFO, "done\n");
118 void show_flist_stats(void)
124 static struct string_area *string_area_new(int size)
126 struct string_area *a;
130 a = new(struct string_area);
132 out_of_memory("string_area_new");
133 a->current = a->base = new_array(char, size);
135 out_of_memory("string_area_new buffer");
136 a->end = a->base + size;
142 static void string_area_free(struct string_area *a)
144 struct string_area *next;
146 for (; a; a = next) {
152 static char *string_area_malloc(struct string_area **ap, int size)
155 struct string_area *a;
157 /* does the request fit into the current space? */
159 if (a->current + size >= a->end) {
160 /* no; get space, move new string_area to front of the list */
161 a = string_area_new(size > ARENA_SIZE ? size : ARENA_SIZE);
166 /* have space; do the "allocation." */
172 static char *string_area_strdup(struct string_area **ap, const char *src)
174 char *dest = string_area_malloc(ap, strlen(src) + 1);
175 return strcpy(dest, src);
178 static void list_file_entry(struct file_struct *f)
183 /* this can happen if duplicate names were removed */
186 permstring(perms, f->mode);
188 if (preserve_links && S_ISLNK(f->mode)) {
189 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
191 (double) f->length, timestring(f->modtime),
192 f_name(f), f->u.link);
194 rprintf(FINFO, "%s %11.0f %s %s\n",
196 (double) f->length, timestring(f->modtime),
203 * Stat either a symlink or its referent, depending on the settings of
204 * copy_links, copy_unsafe_links, etc.
206 * @retval -1 on error
208 * @retval 0 for success
210 * @post If @p path is a symlink, then @p linkbuf (of size @c
211 * MAXPATHLEN) contains the symlink target.
213 * @post @p buffer contains information about the link or the
214 * referrent as appropriate, if they exist.
216 int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
220 return do_stat(path, buffer);
221 if (do_lstat(path, buffer) == -1)
223 if (S_ISLNK(buffer->st_mode)) {
224 int l = readlink((char *) path, linkbuf, MAXPATHLEN - 1);
228 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
230 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
233 return do_stat(path, buffer);
238 return do_stat(path, buffer);
242 int link_stat(const char *path, STRUCT_STAT * buffer)
246 return do_stat(path, buffer);
247 return do_lstat(path, buffer);
249 return do_stat(path, buffer);
254 * This function is used to check if a file should be included/excluded
255 * from the list of files based on its name and type etc. The value of
256 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
258 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
260 #if 0 /* This currently never happens, so avoid a useless compare. */
261 if (exclude_level == NO_EXCLUDES)
265 /* never exclude '.', even if somebody does --exclude '*' */
266 if (fname[0] == '.' && !fname[1])
268 /* Handle the -R version of the '.' dir. */
269 if (fname[0] == '/') {
270 int len = strlen(fname);
271 if (fname[len-1] == '.' && fname[len-2] == '/')
275 if (server_exclude_list
276 && check_exclude(server_exclude_list, fname, is_dir))
278 if (exclude_level != ALL_EXCLUDES)
280 if (exclude_list && check_exclude(exclude_list, fname, is_dir))
282 if (local_exclude_list
283 && check_exclude(local_exclude_list, fname, is_dir))
288 /* used by the one_file_system code */
289 static dev_t filesystem_dev;
291 static void set_filesystem(char *fname)
294 if (link_stat(fname, &st) != 0)
296 filesystem_dev = st.st_dev;
300 static int to_wire_mode(mode_t mode)
302 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
303 return (mode & ~(_S_IFMT)) | 0120000;
307 static mode_t from_wire_mode(int mode)
309 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
310 return (mode & ~(_S_IFMT)) | _S_IFLNK;
311 return (mode_t) mode;
315 static void send_directory(int f, struct file_list *flist, char *dir);
317 static char *flist_dir;
318 static int flist_dir_len;
322 * Make sure @p flist is big enough to hold at least @p flist->count
325 static void flist_expand(struct file_list *flist)
327 if (flist->count >= flist->malloced) {
330 if (flist->malloced < 1000)
331 flist->malloced += 1000;
333 flist->malloced *= 2;
336 new_ptr = realloc_array(flist->files,
337 struct file_struct *,
340 new_ptr = new_array(struct file_struct *,
345 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
347 (double)sizeof(flist->files[0])
349 (new_ptr == flist->files) ? " not" : "");
352 flist->files = (struct file_struct **) new_ptr;
355 out_of_memory("flist_expand");
359 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
361 unsigned short flags;
362 static time_t modtime;
364 static DEV64_T rdev, rdev_high;
368 static char lastname[MAXPATHLEN];
369 char *fname, fbuf[MAXPATHLEN];
377 modtime = 0, mode = 0;
378 rdev = 0, rdev_high = 0, dev = 0;
384 io_write_phase = "send_file_entry";
386 fname = f_name_to(file, fbuf);
390 if (file->mode == mode)
391 flags |= XMIT_SAME_MODE;
394 if (preserve_devices) {
395 if (protocol_version < 28) {
396 if (IS_DEVICE(mode)) {
397 if (file->u.rdev == rdev) {
398 /* Set both flags to simplify the test
399 * when writing the data. */
400 flags |= XMIT_SAME_RDEV_pre28
401 | XMIT_SAME_HIGH_RDEV;
406 } else if (IS_DEVICE(mode)) {
407 if ((file->u.rdev & ~0xFF) == rdev_high)
408 flags |= XMIT_SAME_HIGH_RDEV;
411 rdev_high = rdev & ~0xFF;
415 if (file->uid == uid)
416 flags |= XMIT_SAME_UID;
419 if (file->gid == gid)
420 flags |= XMIT_SAME_GID;
423 if (file->modtime == modtime)
424 flags |= XMIT_SAME_TIME;
426 modtime = file->modtime;
427 if (file->link_u.idev) {
428 if (file->F_DEV == dev) {
429 if (protocol_version >= 28)
430 flags |= XMIT_SAME_DEV;
433 flags |= XMIT_HAS_IDEV_DATA;
437 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
439 l2 = strlen(fname+l1);
442 flags |= XMIT_SAME_NAME;
444 flags |= XMIT_LONG_NAME;
446 /* We must make sure we don't send a zero flag byte or the
447 * other end will terminate the flist transfer. Note that
448 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
449 * it's harmless way to add a bit to the first flag byte. */
450 if (protocol_version >= 28) {
451 if (!flags && !S_ISDIR(mode))
452 flags |= XMIT_TOP_DIR;
453 if ((flags & 0xFF00) || !flags) {
454 flags |= XMIT_EXTENDED_FLAGS;
455 write_byte(f, flags);
456 write_byte(f, flags >> 8);
458 write_byte(f, flags);
460 if (!(flags & 0xFF) && !S_ISDIR(mode))
461 flags |= XMIT_TOP_DIR;
463 flags |= XMIT_LONG_NAME;
464 write_byte(f, flags);
466 if (flags & XMIT_SAME_NAME)
468 if (flags & XMIT_LONG_NAME)
472 write_buf(f, fname + l1, l2);
474 write_longint(f, file->length);
475 if (!(flags & XMIT_SAME_TIME))
476 write_int(f, modtime);
477 if (!(flags & XMIT_SAME_MODE))
478 write_int(f, to_wire_mode(mode));
479 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
483 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
487 if (preserve_devices && IS_DEVICE(mode)) {
488 /* If XMIT_SAME_HIGH_RDEV is off, XMIT_SAME_RDEV_pre28 is
490 if (!(flags & XMIT_SAME_HIGH_RDEV))
492 else if (protocol_version >= 28)
497 if (preserve_links && S_ISLNK(mode)) {
498 int len = strlen(file->u.link);
500 write_buf(f, file->u.link, len);
504 #if SUPPORT_HARD_LINKS
505 if (flags & XMIT_HAS_IDEV_DATA) {
506 if (protocol_version < 26) {
507 /* 32-bit dev_t and ino_t */
509 write_int(f, file->F_INODE);
511 /* 64-bit dev_t and ino_t */
512 if (!(flags & XMIT_SAME_DEV))
513 write_longint(f, dev);
514 write_longint(f, file->F_INODE);
519 if (always_checksum) {
523 else if (protocol_version < 28) {
524 /* Prior to 28, we sent a useless set of nulls. */
529 write_buf(f, sum, protocol_version < 21? 2
534 strlcpy(lastname, fname, MAXPATHLEN);
536 io_write_phase = "unknown";
541 void receive_file_entry(struct file_struct **fptr, unsigned short flags, int f)
543 static time_t modtime;
545 static DEV64_T rdev, rdev_high;
549 static char lastname[MAXPATHLEN];
550 char thisname[MAXPATHLEN];
551 unsigned int l1 = 0, l2 = 0;
553 struct file_struct *file;
556 modtime = 0, mode = 0;
557 rdev = 0, rdev_high = 0, dev = 0;
563 if (flags & XMIT_SAME_NAME)
566 if (flags & XMIT_LONG_NAME)
571 file = new(struct file_struct);
573 out_of_memory("receive_file_entry");
574 memset((char *) file, 0, sizeof(*file));
577 if (l2 >= MAXPATHLEN - l1) {
579 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
580 flags, l1, l2, lastname);
581 overflow("receive_file_entry");
584 strlcpy(thisname, lastname, l1 + 1);
585 read_sbuf(f, &thisname[l1], l2);
586 thisname[l1 + l2] = 0;
588 strlcpy(lastname, thisname, MAXPATHLEN);
589 lastname[MAXPATHLEN - 1] = 0;
591 clean_fname(thisname);
593 if (sanitize_paths) {
594 sanitize_path(thisname, NULL);
597 if ((p = strrchr(thisname, '/'))) {
598 static char *lastdir;
600 if (lastdir && strcmp(thisname, lastdir) == 0)
601 file->dirname = lastdir;
603 file->dirname = strdup(thisname);
604 lastdir = file->dirname;
606 file->basename = strdup(p + 1);
608 file->dirname = NULL;
609 file->basename = strdup(thisname);
613 out_of_memory("receive_file_entry 1");
615 file->flags = flags & XMIT_TOP_DIR ? FLAG_TOP_DIR : 0;
616 file->length = read_longint(f);
617 if (!(flags & XMIT_SAME_TIME))
618 modtime = (time_t)read_int(f);
619 file->modtime = modtime;
620 if (!(flags & XMIT_SAME_MODE))
621 mode = from_wire_mode(read_int(f));
625 if (!(flags & XMIT_SAME_UID))
626 uid = (uid_t)read_int(f);
630 if (!(flags & XMIT_SAME_GID))
631 gid = (gid_t)read_int(f);
634 if (preserve_devices) {
635 if (protocol_version < 28) {
636 if (IS_DEVICE(mode)) {
637 if (!(flags & XMIT_SAME_RDEV_pre28))
638 rdev = (DEV64_T)read_int(f);
642 } else if (IS_DEVICE(mode)) {
643 if (!(flags & XMIT_SAME_HIGH_RDEV)) {
644 rdev = (DEV64_T)read_int(f);
645 rdev_high = rdev & ~0xFF;
647 rdev = rdev_high | (DEV64_T)read_byte(f);
652 if (preserve_links && S_ISLNK(mode)) {
653 int len = read_int(f);
654 if (len < 0 || len >= MAXPATHLEN) {
655 rprintf(FERROR, "overflow: len=%d\n", len);
656 overflow("receive_file_entry");
658 if (!(file->u.link = new_array(char, len + 1)))
659 out_of_memory("receive_file_entry 2");
660 read_sbuf(f, file->u.link, len);
662 sanitize_path(file->u.link, file->dirname);
664 #if SUPPORT_HARD_LINKS
665 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
666 flags |= XMIT_HAS_IDEV_DATA;
667 if (flags & XMIT_HAS_IDEV_DATA) {
668 if (!(file->link_u.idev = new(struct idev)))
669 out_of_memory("file inode data");
670 if (protocol_version < 26) {
672 file->F_INODE = read_int(f);
674 if (!(flags & XMIT_SAME_DEV))
675 dev = read_longint(f);
676 file->F_INODE = read_longint(f);
682 if (always_checksum) {
685 sum = file->u.sum = new_array(char, MD4_SUM_LENGTH);
687 out_of_memory("md4 sum");
688 } else if (protocol_version < 28) {
689 /* Prior to 28, we get a useless set of nulls. */
694 read_buf(f, sum, protocol_version < 21? 2
699 if (!preserve_perms) {
700 extern int orig_umask;
701 /* set an appropriate set of permissions based on original
702 * permissions and umask. This emulates what GNU cp does */
703 file->mode &= ~orig_umask;
708 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
709 /* IRIX cc cares that the operands to the ternary have the same type. */
710 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
713 * Create a file_struct for a named file by reading its stat()
714 * information and performing extensive checks against global
717 * @return the new file, or NULL if there was an error or this file
718 * should be excluded.
720 * @todo There is a small optimization opportunity here to avoid
721 * stat()ing the file in some circumstances, which has a certain cost.
722 * We are called immediately after doing readdir(), and so we may
723 * already know the d_type of the file. We could for example avoid
724 * statting directories if we're not recursing, but this is not a very
725 * important case. Some systems may not have d_type.
727 struct file_struct *make_file(char *fname, struct string_area **ap,
730 struct file_struct *file;
732 char sum[SUM_LENGTH];
734 char thisname[MAXPATHLEN];
735 char linkname[MAXPATHLEN];
736 unsigned short flags = 0;
738 if (strlcpy(thisname, fname, sizeof thisname)
739 >= sizeof thisname - flist_dir_len) {
740 rprintf(FINFO, "skipping overly long name: %s\n", fname);
743 clean_fname(thisname);
745 sanitize_path(thisname, NULL);
747 memset(sum, 0, SUM_LENGTH);
749 if (readlink_stat(thisname, &st, linkname) != 0) {
750 int save_errno = errno;
751 if (errno == ENOENT && exclude_level != NO_EXCLUDES) {
752 /* either symlink pointing nowhere or file that
753 * was removed during rsync run; see if excluded
754 * before reporting an error */
755 if (check_exclude_file(thisname, 0, exclude_level)) {
756 /* file is excluded anyway, ignore silently */
760 io_error |= IOERR_GENERAL;
761 rprintf(FERROR, "readlink %s failed: %s\n",
762 full_fname(thisname), strerror(save_errno));
766 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
767 if (exclude_level == NO_EXCLUDES)
770 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
771 rprintf(FINFO, "skipping directory %s\n", thisname);
775 if (one_file_system && st.st_dev != filesystem_dev) {
776 /* We allow a directory though to preserve the mount point.
777 * However, flag it so that we don't recurse. */
778 if (!S_ISDIR(st.st_mode))
780 flags |= FLAG_MOUNT_POINT;
783 if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level))
786 if (lp_ignore_nonreadable(module_id) && access(thisname, R_OK) != 0)
792 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
793 who_am_i(), thisname, exclude_level);
796 file = new(struct file_struct);
798 out_of_memory("make_file");
799 memset((char *) file, 0, sizeof(*file));
802 if ((p = strrchr(thisname, '/'))) {
803 static char *lastdir;
805 if (lastdir && strcmp(thisname, lastdir) == 0)
806 file->dirname = lastdir;
808 file->dirname = strdup(thisname);
809 lastdir = file->dirname;
811 file->basename = STRDUP(ap, p + 1);
814 file->dirname = NULL;
815 file->basename = STRDUP(ap, thisname);
818 file->modtime = st.st_mtime;
819 file->length = st.st_size;
820 file->mode = st.st_mode;
821 file->uid = st.st_uid;
822 file->gid = st.st_gid;
823 if (preserve_hard_links) {
824 if (protocol_version < 28 ? S_ISREG(st.st_mode)
825 : !S_ISDIR(st.st_mode) && st.st_nlink > 1) {
826 if (!(file->link_u.idev = new(struct idev)))
827 out_of_memory("file inode data");
828 file->F_DEV = st.st_dev;
829 file->F_INODE = st.st_ino;
832 #ifdef HAVE_STRUCT_STAT_ST_RDEV
833 if (preserve_devices && IS_DEVICE(st.st_mode))
834 file->u.rdev = st.st_rdev;
838 if (S_ISLNK(st.st_mode))
839 file->u.link = STRDUP(ap, linkname);
842 if (always_checksum && S_ISREG(st.st_mode)) {
843 if (!(file->u.sum = (char*)MALLOC(ap, MD4_SUM_LENGTH)))
844 out_of_memory("md4 sum");
845 file_checksum(thisname, file->u.sum, st.st_size);
848 file->basedir = flist_dir;
850 if (!S_ISDIR(st.st_mode))
851 stats.total_size += st.st_size;
857 void send_file_name(int f, struct file_list *flist, char *fname,
858 int recursive, unsigned short base_flags)
860 struct file_struct *file;
861 char fbuf[MAXPATHLEN];
862 extern int delete_excluded;
864 /* f is set to -1 when calculating deletion file list */
865 file = make_file(fname, &flist->string_area,
866 f == -1 && delete_excluded? SERVER_EXCLUDES
872 maybe_emit_filelist_progress(flist);
877 file->flags |= FLAG_TOP_DIR;
879 if (file->basename[0]) {
880 flist->files[flist->count++] = file;
881 send_file_entry(file, f, base_flags);
884 if (recursive && S_ISDIR(file->mode)
885 && !(file->flags & FLAG_MOUNT_POINT)) {
886 struct exclude_struct **last_exclude_list = local_exclude_list;
887 send_directory(f, flist, f_name_to(file, fbuf));
888 local_exclude_list = last_exclude_list;
894 static void send_directory(int f, struct file_list *flist, char *dir)
898 char fname[MAXPATHLEN];
904 io_error |= IOERR_GENERAL;
905 rprintf(FERROR, "opendir %s failed: %s\n",
906 full_fname(dir), strerror(errno));
910 offset = strlcpy(fname, dir, MAXPATHLEN);
912 if (offset >= MAXPATHLEN || p[-1] != '/') {
913 if (offset >= MAXPATHLEN - 1) {
914 io_error |= IOERR_GENERAL;
915 rprintf(FERROR, "skipping long-named directory: %s\n",
924 local_exclude_list = NULL;
927 if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
928 < MAXPATHLEN - offset)
929 add_exclude_file(&local_exclude_list,fname,MISSING_OK,ADD_EXCLUDE);
931 io_error |= IOERR_GENERAL;
933 "cannot cvs-exclude in long-named directory %s\n",
938 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
939 char *dname = d_name(di);
940 if (dname[0] == '.' && (dname[1] == '\0'
941 || (dname[1] == '.' && dname[2] == '\0')))
943 if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset)
944 send_file_name(f, flist, fname, recurse, 0);
946 io_error |= IOERR_GENERAL;
948 "cannot send long-named file %s\n",
953 io_error |= IOERR_GENERAL;
954 rprintf(FERROR, "readdir(%s): (%d) %s\n",
955 dir, errno, strerror(errno));
958 if (local_exclude_list)
959 free_exclude_list(&local_exclude_list); /* Zeros pointer too */
966 * The delete_files() function in receiver.c sets f to -1 so that we just
967 * construct the file list in memory without sending it over the wire. It
968 * also has the side-effect of ignoring user-excludes if delete_excluded
969 * is set (so that the delete list includes user-excluded files).
971 struct file_list *send_file_list(int f, int argc, char *argv[])
975 char *p, *dir, olddir[sizeof curr_dir];
976 char lastpath[MAXPATHLEN] = "";
977 struct file_list *flist;
981 if (show_filelist_p() && f != -1)
982 start_filelist_progress("building file list");
984 start_write = stats.total_written;
989 io_start_buffering_out(f);
990 if (filesfrom_fd >= 0) {
991 if (argv[0] && !push_dir(argv[0])) {
992 rprintf(FERROR, "push_dir %s failed: %s\n",
993 full_fname(argv[0]), strerror(errno));
994 exit_cleanup(RERR_FILESELECT);
1001 char fname2[MAXPATHLEN];
1002 char *fname = fname2;
1005 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1007 sanitize_path(fname, NULL);
1011 strlcpy(fname, *argv++, MAXPATHLEN);
1013 sanitize_path(fname, NULL);
1017 if (fname[l - 1] == '/') {
1018 if (l == 2 && fname[0] == '.') {
1019 /* Turn "./" into just "." rather than "./." */
1021 } else if (l < MAXPATHLEN) {
1027 if (link_stat(fname, &st) != 0) {
1029 io_error |= IOERR_GENERAL;
1030 rprintf(FERROR, "link_stat %s failed: %s\n",
1031 full_fname(fname), strerror(errno));
1036 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
1037 rprintf(FINFO, "skipping directory %s\n", fname);
1044 if (!relative_paths) {
1045 p = strrchr(fname, '/');
1054 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1055 /* this ensures we send the intermediate directories,
1056 thus getting their permissions right */
1057 char *lp = lastpath, *fn = fname, *slash = fname;
1059 /* Skip any initial directories in our path that we
1060 * have in common with lastpath. */
1061 while (*fn && *lp == *fn) {
1067 if (fn != p || (*lp && *lp != '/')) {
1068 int copy_links_saved = copy_links;
1069 int recurse_saved = recurse;
1070 copy_links = copy_unsafe_links;
1071 /* set recurse to 1 to prevent make_file
1072 * from ignoring directory, but still
1073 * turn off the recursive parameter to
1076 while ((slash = strchr(slash+1, '/')) != 0) {
1078 send_file_name(f, flist, fname, 0, 0);
1081 copy_links = copy_links_saved;
1082 recurse = recurse_saved;
1084 strlcpy(lastpath, fname, sizeof lastpath);
1093 static char *lastdir;
1094 static int lastdir_len;
1096 strcpy(olddir, curr_dir); /* can't overflow */
1098 if (!push_dir(dir)) {
1099 io_error |= IOERR_GENERAL;
1100 rprintf(FERROR, "push_dir %s failed: %s\n",
1101 full_fname(dir), strerror(errno));
1105 if (lastdir && strcmp(lastdir, dir) == 0) {
1106 flist_dir = lastdir;
1107 flist_dir_len = lastdir_len;
1109 flist_dir = lastdir = strdup(dir);
1110 flist_dir_len = lastdir_len = strlen(dir);
1114 if (one_file_system)
1115 set_filesystem(fname);
1117 send_file_name(f, flist, fname, recurse, XMIT_TOP_DIR);
1122 if (!pop_dir(olddir)) {
1123 rprintf(FERROR, "pop_dir %s failed: %s\n",
1124 full_fname(dir), strerror(errno));
1125 exit_cleanup(RERR_FILESELECT);
1131 send_file_entry(NULL, f, 0);
1133 if (show_filelist_p())
1134 finish_filelist_progress(flist);
1137 clean_flist(flist, 0, 0);
1140 /* Now send the uid/gid list. This was introduced in
1141 * protocol version 15 */
1144 /* send the io_error flag */
1145 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1148 stats.flist_size = stats.total_written - start_write;
1149 stats.num_files = flist->count;
1151 write_batch_flist_info(flist->count, flist->files);
1155 rprintf(FINFO, "send_file_list done\n");
1161 struct file_list *recv_file_list(int f)
1163 struct file_list *flist;
1164 unsigned short flags;
1166 extern int list_only;
1168 if (show_filelist_p())
1169 start_filelist_progress("receiving file list");
1171 start_read = stats.total_read;
1173 flist = new(struct file_list);
1178 flist->malloced = 1000;
1179 flist->files = new_array(struct file_struct *, flist->malloced);
1184 while ((flags = read_byte(f)) != 0) {
1185 int i = flist->count;
1187 flist_expand(flist);
1189 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1190 flags |= read_byte(f) << 8;
1191 receive_file_entry(&flist->files[i], flags, f);
1193 if (S_ISREG(flist->files[i]->mode))
1194 stats.total_size += flist->files[i]->length;
1198 maybe_emit_filelist_progress(flist);
1201 rprintf(FINFO, "recv_file_name(%s)\n",
1202 f_name(flist->files[i]));
1205 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1208 rprintf(FINFO, "received %d names\n", flist->count);
1210 if (show_filelist_p())
1211 finish_filelist_progress(flist);
1213 clean_flist(flist, relative_paths, 1);
1216 /* Now send the uid/gid list. This was introduced in
1217 * protocol version 15 */
1218 recv_uid_list(f, flist);
1221 /* Recv the io_error flag */
1222 if (lp_ignore_errors(module_id) || ignore_errors)
1225 io_error |= read_int(f);
1231 for (i = 0; i < flist->count; i++)
1232 list_file_entry(flist->files[i]);
1236 rprintf(FINFO, "recv_file_list done\n");
1238 stats.flist_size = stats.total_read - start_read;
1239 stats.num_files = flist->count;
1244 out_of_memory("recv_file_list");
1245 return NULL; /* not reached */
1249 int file_compare(struct file_struct **file1, struct file_struct **file2)
1251 struct file_struct *f1 = *file1;
1252 struct file_struct *f2 = *file2;
1254 if (!f1->basename && !f2->basename)
1260 if (f1->dirname == f2->dirname)
1261 return u_strcmp(f1->basename, f2->basename);
1262 return f_name_cmp(f1, f2);
1266 int flist_find(struct file_list *flist, struct file_struct *f)
1268 int low = 0, high = flist->count - 1;
1270 while (high >= 0 && !flist->files[high]->basename) high--;
1275 while (low != high) {
1276 int mid = (low + high) / 2;
1277 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1279 return flist_up(flist, mid);
1286 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1287 return flist_up(flist, low);
1295 void free_file(struct file_struct *file)
1300 free(file->basename);
1301 if (!IS_DEVICE(file->mode) && file->u.link)
1302 free(file->u.link); /* Handles u.sum too. */
1303 if (file->link_u.idev)
1304 free((char*)file->link_u.idev); /* Handles link_u.links too. */
1310 * allocate a new file list
1312 struct file_list *flist_new(void)
1314 struct file_list *flist;
1316 flist = new(struct file_list);
1318 out_of_memory("send_file_list");
1321 flist->malloced = 0;
1322 flist->files = NULL;
1325 flist->string_area = string_area_new(0);
1327 flist->string_area = NULL;
1333 * free up all elements in a flist
1335 void flist_free(struct file_list *flist)
1338 for (i = 1; i < flist->count; i++) {
1339 if (!flist->string_area)
1340 free_file(flist->files[i]);
1341 free(flist->files[i]);
1343 /* FIXME: I don't think we generally need to blank the flist
1344 * since it's about to be freed. This will just cause more
1345 * memory traffic. If you want a freed-memory debugger, you
1346 * know where to get it. */
1347 memset((char *) flist->files, 0,
1348 sizeof(flist->files[0]) * flist->count);
1350 if (flist->string_area)
1351 string_area_free(flist->string_area);
1352 memset((char *) flist, 0, sizeof(*flist));
1358 * This routine ensures we don't have any duplicate names in our file list.
1359 * duplicate names can cause corruption because of the pipelining
1361 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1365 if (!flist || flist->count == 0)
1368 qsort(flist->files, flist->count,
1369 sizeof(flist->files[0]), (int (*)()) file_compare);
1371 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1372 if (flist->files[i]->basename) {
1377 while (++i < flist->count) {
1378 if (!flist->files[i]->basename)
1380 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1381 if (verbose > 1 && !am_server) {
1383 "removing duplicate name %s from file list %d\n",
1384 f_name(flist->files[i]), i);
1386 /* Make sure that if we unduplicate '.', that we don't
1387 * lose track of a user-specified starting point (or
1388 * else deletions will mysteriously fail with -R). */
1389 if (flist->files[i]->flags & FLAG_TOP_DIR)
1390 flist->files[prev_i]->flags |= FLAG_TOP_DIR;
1391 /* it's not great that the flist knows the semantics of
1392 * the file memory usage, but i'd rather not add a flag
1393 * byte to that struct.
1394 * XXX can i use a bit in the flags field? */
1395 if (flist->string_area)
1396 flist->files[i][0] = null_file;
1398 free_file(flist->files[i]);
1404 /* we need to strip off the root directory in the case
1405 of relative paths, but this must be done _after_
1406 the sorting phase */
1407 for (i = 0; i < flist->count; i++) {
1408 if (flist->files[i]->dirname &&
1409 flist->files[i]->dirname[0] == '/') {
1410 memmove(&flist->files[i]->dirname[0],
1411 &flist->files[i]->dirname[1],
1412 strlen(flist->files[i]->dirname));
1415 if (flist->files[i]->dirname &&
1416 !flist->files[i]->dirname[0]) {
1417 flist->files[i]->dirname = NULL;
1425 for (i = 0; i < flist->count; i++) {
1426 rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f\n",
1428 NS(flist->files[i]->basedir),
1429 NS(flist->files[i]->dirname),
1430 NS(flist->files[i]->basename),
1431 (int) flist->files[i]->mode,
1432 (double) flist->files[i]->length);
1437 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1439 /* Compare the names of two file_struct entities, just like strcmp()
1440 * would do if it were operating on the joined strings. We assume
1441 * that there are no 0-length strings.
1443 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1446 const uchar *c1, *c2;
1447 enum fnc_state state1, state2;
1449 if (!f1 || !f1->basename) {
1450 if (!f2 || !f2->basename)
1454 if (!f2 || !f2->basename)
1457 if (!(c1 = (uchar*)f1->dirname)) {
1459 c1 = (uchar*)f1->basename;
1462 if (!(c2 = (uchar*)f2->dirname)) {
1464 c2 = (uchar*)f2->basename;
1469 if ((dif = (int)*c1 - (int)*c2) != 0)
1479 c1 = (uchar*)f1->basename;
1493 c2 = (uchar*)f2->basename;
1507 /* Return a copy of the full filename of a flist entry, using the indicated
1508 * buffer. No size-checking is done because we checked the size when creating
1509 * the file_struct entry.
1511 char *f_name_to(struct file_struct *f, char *fbuf)
1513 if (!f || !f->basename)
1517 int len = strlen(f->dirname);
1518 memcpy(fbuf, f->dirname, len);
1520 strcpy(fbuf + len + 1, f->basename);
1522 strcpy(fbuf, f->basename);
1527 /* Like f_name_to(), but we rotate through 5 static buffers of our own.
1529 char *f_name(struct file_struct *f)
1531 static char names[5][MAXPATHLEN];
1532 static unsigned int n;
1534 n = (n + 1) % (sizeof names / sizeof names[0]);
1536 return f_name_to(f, names[n]);