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);
189 if (preserve_links && S_ISLNK(f->mode)) {
190 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
192 (double) f->length, timestring(f->modtime),
193 f_name(f), f->u.link);
196 rprintf(FINFO, "%s %11.0f %s %s\n",
198 (double) f->length, timestring(f->modtime),
204 * Stat either a symlink or its referent, depending on the settings of
205 * copy_links, copy_unsafe_links, etc.
207 * @retval -1 on error
209 * @retval 0 for success
211 * @post If @p path is a symlink, then @p linkbuf (of size @c
212 * MAXPATHLEN) contains the symlink target.
214 * @post @p buffer contains information about the link or the
215 * referrent as appropriate, if they exist.
217 int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
221 return do_stat(path, buffer);
222 if (do_lstat(path, buffer) == -1)
224 if (S_ISLNK(buffer->st_mode)) {
225 int l = readlink((char *) path, linkbuf, MAXPATHLEN - 1);
229 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
231 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
234 return do_stat(path, buffer);
239 return do_stat(path, buffer);
243 int link_stat(const char *path, STRUCT_STAT * buffer)
247 return do_stat(path, buffer);
248 return do_lstat(path, buffer);
250 return do_stat(path, buffer);
255 * This function is used to check if a file should be included/excluded
256 * from the list of files based on its name and type etc. The value of
257 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
259 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
261 #if 0 /* This currently never happens, so avoid a useless compare. */
262 if (exclude_level == NO_EXCLUDES)
266 /* never exclude '.', even if somebody does --exclude '*' */
267 if (fname[0] == '.' && !fname[1])
269 /* Handle the -R version of the '.' dir. */
270 if (fname[0] == '/') {
271 int len = strlen(fname);
272 if (fname[len-1] == '.' && fname[len-2] == '/')
276 if (server_exclude_list
277 && check_exclude(server_exclude_list, fname, is_dir))
279 if (exclude_level != ALL_EXCLUDES)
281 if (exclude_list && check_exclude(exclude_list, fname, is_dir))
283 if (local_exclude_list
284 && check_exclude(local_exclude_list, fname, is_dir))
289 /* used by the one_file_system code */
290 static dev_t filesystem_dev;
292 static void set_filesystem(char *fname)
295 if (link_stat(fname, &st) != 0)
297 filesystem_dev = st.st_dev;
301 static int to_wire_mode(mode_t mode)
304 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
305 return (mode & ~(_S_IFMT)) | 0120000;
310 static mode_t from_wire_mode(int mode)
312 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
313 return (mode & ~(_S_IFMT)) | _S_IFLNK;
314 return (mode_t) mode;
318 static void send_directory(int f, struct file_list *flist, char *dir);
320 static char *flist_dir;
321 static int flist_dir_len;
325 * Make sure @p flist is big enough to hold at least @p flist->count
328 static void flist_expand(struct file_list *flist)
330 if (flist->count >= flist->malloced) {
333 if (flist->malloced < 1000)
334 flist->malloced += 1000;
336 flist->malloced *= 2;
339 new_ptr = realloc_array(flist->files,
340 struct file_struct *,
343 new_ptr = new_array(struct file_struct *,
348 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
350 (double)sizeof(flist->files[0])
352 (new_ptr == flist->files) ? " not" : "");
355 flist->files = (struct file_struct **) new_ptr;
358 out_of_memory("flist_expand");
362 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
364 unsigned short flags;
365 static time_t modtime;
367 static DEV64_T rdev, rdev_high;
371 static char lastname[MAXPATHLEN];
372 char *fname, fbuf[MAXPATHLEN];
380 modtime = 0, mode = 0;
381 rdev = 0, rdev_high = 0, dev = 0;
387 io_write_phase = "send_file_entry";
389 fname = f_name_to(file, fbuf);
393 if (file->mode == mode)
394 flags |= XMIT_SAME_MODE;
397 if (preserve_devices) {
398 if (protocol_version < 28) {
399 if (IS_DEVICE(mode)) {
400 if (file->u.rdev == rdev) {
401 /* Set both flags to simplify the test
402 * when writing the data. */
403 flags |= XMIT_SAME_RDEV_pre28
404 | XMIT_SAME_HIGH_RDEV;
409 } else if (IS_DEVICE(mode)) {
410 if ((file->u.rdev & ~0xFF) == rdev_high)
411 flags |= XMIT_SAME_HIGH_RDEV;
414 rdev_high = rdev & ~0xFF;
418 if (file->uid == uid)
419 flags |= XMIT_SAME_UID;
422 if (file->gid == gid)
423 flags |= XMIT_SAME_GID;
426 if (file->modtime == modtime)
427 flags |= XMIT_SAME_TIME;
429 modtime = file->modtime;
430 #if SUPPORT_HARD_LINKS
431 if (file->link_u.idev) {
432 if (file->F_DEV == dev) {
433 if (protocol_version >= 28)
434 flags |= XMIT_SAME_DEV;
437 flags |= XMIT_HAS_IDEV_DATA;
442 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
444 l2 = strlen(fname+l1);
447 flags |= XMIT_SAME_NAME;
449 flags |= XMIT_LONG_NAME;
451 /* We must make sure we don't send a zero flag byte or the
452 * other end will terminate the flist transfer. Note that
453 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
454 * it's harmless way to add a bit to the first flag byte. */
455 if (protocol_version >= 28) {
456 if (!flags && !S_ISDIR(mode))
457 flags |= XMIT_TOP_DIR;
458 if ((flags & 0xFF00) || !flags) {
459 flags |= XMIT_EXTENDED_FLAGS;
460 write_byte(f, flags);
461 write_byte(f, flags >> 8);
463 write_byte(f, flags);
465 if (!(flags & 0xFF) && !S_ISDIR(mode))
466 flags |= XMIT_TOP_DIR;
468 flags |= XMIT_LONG_NAME;
469 write_byte(f, flags);
471 if (flags & XMIT_SAME_NAME)
473 if (flags & XMIT_LONG_NAME)
477 write_buf(f, fname + l1, l2);
479 write_longint(f, file->length);
480 if (!(flags & XMIT_SAME_TIME))
481 write_int(f, modtime);
482 if (!(flags & XMIT_SAME_MODE))
483 write_int(f, to_wire_mode(mode));
484 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
488 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
492 if (preserve_devices && IS_DEVICE(mode)) {
493 /* If XMIT_SAME_HIGH_RDEV is off, XMIT_SAME_RDEV_pre28 is
495 if (!(flags & XMIT_SAME_HIGH_RDEV))
497 else if (protocol_version >= 28)
502 if (preserve_links && S_ISLNK(mode)) {
503 int len = strlen(file->u.link);
505 write_buf(f, file->u.link, len);
509 #if SUPPORT_HARD_LINKS
510 if (flags & XMIT_HAS_IDEV_DATA) {
511 if (protocol_version < 26) {
512 /* 32-bit dev_t and ino_t */
514 write_int(f, file->F_INODE);
516 /* 64-bit dev_t and ino_t */
517 if (!(flags & XMIT_SAME_DEV))
518 write_longint(f, dev);
519 write_longint(f, file->F_INODE);
524 if (always_checksum) {
528 else if (protocol_version < 28) {
529 /* Prior to 28, we sent a useless set of nulls. */
534 write_buf(f, sum, protocol_version < 21? 2
539 strlcpy(lastname, fname, MAXPATHLEN);
541 io_write_phase = "unknown";
546 void receive_file_entry(struct file_struct **fptr, unsigned short flags, int f)
548 static time_t modtime;
550 static DEV64_T rdev, rdev_high;
554 static char lastname[MAXPATHLEN];
555 char thisname[MAXPATHLEN];
556 unsigned int l1 = 0, l2 = 0;
558 struct file_struct *file;
561 modtime = 0, mode = 0;
562 rdev = 0, rdev_high = 0, dev = 0;
568 if (flags & XMIT_SAME_NAME)
571 if (flags & XMIT_LONG_NAME)
576 file = new(struct file_struct);
578 out_of_memory("receive_file_entry");
579 memset((char *) file, 0, sizeof(*file));
582 if (l2 >= MAXPATHLEN - l1) {
584 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
585 flags, l1, l2, lastname);
586 overflow("receive_file_entry");
589 strlcpy(thisname, lastname, l1 + 1);
590 read_sbuf(f, &thisname[l1], l2);
591 thisname[l1 + l2] = 0;
593 strlcpy(lastname, thisname, MAXPATHLEN);
595 clean_fname(thisname);
598 sanitize_path(thisname, NULL);
600 if ((p = strrchr(thisname, '/'))) {
601 static char *lastdir;
603 if (lastdir && strcmp(thisname, lastdir) == 0)
604 file->dirname = lastdir;
606 file->dirname = strdup(thisname);
607 lastdir = file->dirname;
609 file->basename = strdup(p + 1);
611 file->dirname = NULL;
612 file->basename = strdup(thisname);
616 out_of_memory("receive_file_entry 1");
618 file->flags = flags & XMIT_TOP_DIR ? FLAG_TOP_DIR : 0;
619 file->length = read_longint(f);
620 if (!(flags & XMIT_SAME_TIME))
621 modtime = (time_t)read_int(f);
622 file->modtime = modtime;
623 if (!(flags & XMIT_SAME_MODE))
624 mode = from_wire_mode(read_int(f));
628 if (!(flags & XMIT_SAME_UID))
629 uid = (uid_t)read_int(f);
633 if (!(flags & XMIT_SAME_GID))
634 gid = (gid_t)read_int(f);
637 if (preserve_devices) {
638 if (protocol_version < 28) {
639 if (IS_DEVICE(mode)) {
640 if (!(flags & XMIT_SAME_RDEV_pre28))
641 rdev = (DEV64_T)read_int(f);
645 } else if (IS_DEVICE(mode)) {
646 if (!(flags & XMIT_SAME_HIGH_RDEV)) {
647 rdev = (DEV64_T)read_int(f);
648 rdev_high = rdev & ~0xFF;
650 rdev = rdev_high | (DEV64_T)read_byte(f);
656 if (preserve_links && S_ISLNK(mode)) {
657 int len = read_int(f);
658 if (len < 0 || len >= MAXPATHLEN) {
659 rprintf(FERROR, "overflow: len=%d\n", len);
660 overflow("receive_file_entry");
662 if (!(file->u.link = new_array(char, len + 1)))
663 out_of_memory("receive_file_entry 2");
664 read_sbuf(f, file->u.link, len);
666 sanitize_path(file->u.link, file->dirname);
670 #if SUPPORT_HARD_LINKS
671 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
672 flags |= XMIT_HAS_IDEV_DATA;
673 if (flags & XMIT_HAS_IDEV_DATA) {
674 if (!(file->link_u.idev = new(struct idev)))
675 out_of_memory("file inode data");
676 if (protocol_version < 26) {
678 file->F_INODE = read_int(f);
680 if (!(flags & XMIT_SAME_DEV))
681 dev = read_longint(f);
682 file->F_INODE = read_longint(f);
688 if (always_checksum) {
691 sum = file->u.sum = new_array(char, MD4_SUM_LENGTH);
693 out_of_memory("md4 sum");
694 } else if (protocol_version < 28) {
695 /* Prior to 28, we get a useless set of nulls. */
700 read_buf(f, sum, protocol_version < 21? 2
705 if (!preserve_perms) {
706 extern int orig_umask;
707 /* set an appropriate set of permissions based on original
708 * permissions and umask. This emulates what GNU cp does */
709 file->mode &= ~orig_umask;
714 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
715 /* IRIX cc cares that the operands to the ternary have the same type. */
716 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
719 * Create a file_struct for a named file by reading its stat()
720 * information and performing extensive checks against global
723 * @return the new file, or NULL if there was an error or this file
724 * should be excluded.
726 * @todo There is a small optimization opportunity here to avoid
727 * stat()ing the file in some circumstances, which has a certain cost.
728 * We are called immediately after doing readdir(), and so we may
729 * already know the d_type of the file. We could for example avoid
730 * statting directories if we're not recursing, but this is not a very
731 * important case. Some systems may not have d_type.
733 struct file_struct *make_file(char *fname, struct string_area **ap,
736 struct file_struct *file;
738 char sum[SUM_LENGTH];
740 char thisname[MAXPATHLEN];
741 char linkname[MAXPATHLEN];
742 unsigned short flags = 0;
744 if (strlcpy(thisname, fname, sizeof thisname)
745 >= sizeof thisname - flist_dir_len) {
746 rprintf(FINFO, "skipping overly long name: %s\n", fname);
749 clean_fname(thisname);
751 sanitize_path(thisname, NULL);
753 memset(sum, 0, SUM_LENGTH);
755 if (readlink_stat(thisname, &st, linkname) != 0) {
756 int save_errno = errno;
757 if (errno == ENOENT && exclude_level != NO_EXCLUDES) {
758 /* either symlink pointing nowhere or file that
759 * was removed during rsync run; see if excluded
760 * before reporting an error */
761 if (check_exclude_file(thisname, 0, exclude_level)) {
762 /* file is excluded anyway, ignore silently */
766 io_error |= IOERR_GENERAL;
767 rprintf(FERROR, "readlink %s failed: %s\n",
768 full_fname(thisname), strerror(save_errno));
772 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
773 if (exclude_level == NO_EXCLUDES)
776 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
777 rprintf(FINFO, "skipping directory %s\n", thisname);
781 if (one_file_system && st.st_dev != filesystem_dev) {
782 /* We allow a directory though to preserve the mount point.
783 * However, flag it so that we don't recurse. */
784 if (!S_ISDIR(st.st_mode))
786 flags |= FLAG_MOUNT_POINT;
789 if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level))
792 if (lp_ignore_nonreadable(module_id) && access(thisname, R_OK) != 0)
798 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
799 who_am_i(), thisname, exclude_level);
802 file = new(struct file_struct);
804 out_of_memory("make_file");
805 memset((char *) file, 0, sizeof(*file));
808 if ((p = strrchr(thisname, '/'))) {
809 static char *lastdir;
811 if (lastdir && strcmp(thisname, lastdir) == 0)
812 file->dirname = lastdir;
814 file->dirname = strdup(thisname);
815 lastdir = file->dirname;
817 file->basename = STRDUP(ap, p + 1);
820 file->dirname = NULL;
821 file->basename = STRDUP(ap, thisname);
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;
830 #ifdef HAVE_STRUCT_STAT_ST_RDEV
831 if (preserve_devices && IS_DEVICE(st.st_mode))
832 file->u.rdev = st.st_rdev;
836 if (S_ISLNK(st.st_mode))
837 file->u.link = STRDUP(ap, linkname);
840 #if SUPPORT_HARD_LINKS
841 if (preserve_hard_links) {
842 if (protocol_version < 28 ? S_ISREG(st.st_mode)
843 : !S_ISDIR(st.st_mode) && st.st_nlink > 1) {
844 if (!(file->link_u.idev = new(struct idev)))
845 out_of_memory("file inode data");
846 file->F_DEV = st.st_dev;
847 file->F_INODE = st.st_ino;
852 if (always_checksum && S_ISREG(st.st_mode)) {
853 if (!(file->u.sum = (char*)MALLOC(ap, MD4_SUM_LENGTH)))
854 out_of_memory("md4 sum");
855 file_checksum(thisname, file->u.sum, st.st_size);
858 file->basedir = flist_dir;
860 if (!S_ISDIR(st.st_mode))
861 stats.total_size += st.st_size;
867 void send_file_name(int f, struct file_list *flist, char *fname,
868 int recursive, unsigned short base_flags)
870 struct file_struct *file;
871 char fbuf[MAXPATHLEN];
872 extern int delete_excluded;
874 /* f is set to -1 when calculating deletion file list */
875 file = make_file(fname, &flist->string_area,
876 f == -1 && delete_excluded? SERVER_EXCLUDES
882 maybe_emit_filelist_progress(flist);
887 file->flags |= FLAG_TOP_DIR;
889 if (file->basename[0]) {
890 flist->files[flist->count++] = file;
891 send_file_entry(file, f, base_flags);
894 if (recursive && S_ISDIR(file->mode)
895 && !(file->flags & FLAG_MOUNT_POINT)) {
896 struct exclude_struct **last_exclude_list = local_exclude_list;
897 send_directory(f, flist, f_name_to(file, fbuf));
898 local_exclude_list = last_exclude_list;
904 static void send_directory(int f, struct file_list *flist, char *dir)
908 char fname[MAXPATHLEN];
914 io_error |= IOERR_GENERAL;
915 rprintf(FERROR, "opendir %s failed: %s\n",
916 full_fname(dir), strerror(errno));
920 offset = strlcpy(fname, dir, MAXPATHLEN);
922 if (offset >= MAXPATHLEN || p[-1] != '/') {
923 if (offset >= MAXPATHLEN - 1) {
924 io_error |= IOERR_GENERAL;
925 rprintf(FERROR, "skipping long-named directory: %s\n",
934 local_exclude_list = NULL;
937 if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
938 < MAXPATHLEN - offset)
939 add_exclude_file(&local_exclude_list,fname,MISSING_OK,ADD_EXCLUDE);
941 io_error |= IOERR_GENERAL;
943 "cannot cvs-exclude in long-named directory %s\n",
948 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
949 char *dname = d_name(di);
950 if (dname[0] == '.' && (dname[1] == '\0'
951 || (dname[1] == '.' && dname[2] == '\0')))
953 if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset)
954 send_file_name(f, flist, fname, recurse, 0);
956 io_error |= IOERR_GENERAL;
958 "cannot send long-named file %s\n",
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[sizeof curr_dir];
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])) {
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 "./." */
1031 } else if (l < MAXPATHLEN) {
1037 if (link_stat(fname, &st) != 0) {
1039 io_error |= IOERR_GENERAL;
1040 rprintf(FERROR, "link_stat %s failed: %s\n",
1041 full_fname(fname), strerror(errno));
1046 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
1047 rprintf(FINFO, "skipping directory %s\n", fname);
1054 if (!relative_paths) {
1055 p = strrchr(fname, '/');
1064 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1065 /* this ensures we send the intermediate directories,
1066 thus getting their permissions right */
1067 char *lp = lastpath, *fn = fname, *slash = fname;
1069 /* Skip any initial directories in our path that we
1070 * have in common with lastpath. */
1071 while (*fn && *lp == *fn) {
1077 if (fn != p || (*lp && *lp != '/')) {
1078 int copy_links_saved = copy_links;
1079 int recurse_saved = recurse;
1080 copy_links = copy_unsafe_links;
1081 /* set recurse to 1 to prevent make_file
1082 * from ignoring directory, but still
1083 * turn off the recursive parameter to
1086 while ((slash = strchr(slash+1, '/')) != 0) {
1088 send_file_name(f, flist, fname, 0, 0);
1091 copy_links = copy_links_saved;
1092 recurse = recurse_saved;
1094 strlcpy(lastpath, fname, sizeof lastpath);
1103 static char *lastdir;
1104 static int lastdir_len;
1106 strcpy(olddir, curr_dir); /* can't overflow */
1108 if (!push_dir(dir)) {
1109 io_error |= IOERR_GENERAL;
1110 rprintf(FERROR, "push_dir %s failed: %s\n",
1111 full_fname(dir), strerror(errno));
1115 if (lastdir && strcmp(lastdir, dir) == 0) {
1116 flist_dir = lastdir;
1117 flist_dir_len = lastdir_len;
1119 flist_dir = lastdir = strdup(dir);
1120 flist_dir_len = lastdir_len = strlen(dir);
1124 if (one_file_system)
1125 set_filesystem(fname);
1127 send_file_name(f, flist, fname, recurse, XMIT_TOP_DIR);
1132 if (!pop_dir(olddir)) {
1133 rprintf(FERROR, "pop_dir %s failed: %s\n",
1134 full_fname(dir), strerror(errno));
1135 exit_cleanup(RERR_FILESELECT);
1141 send_file_entry(NULL, f, 0);
1143 if (show_filelist_p())
1144 finish_filelist_progress(flist);
1147 clean_flist(flist, 0, 0);
1150 /* Now send the uid/gid list. This was introduced in
1151 * protocol version 15 */
1154 /* send the io_error flag */
1155 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1158 stats.flist_size = stats.total_written - start_write;
1159 stats.num_files = flist->count;
1161 write_batch_flist_info(flist->count, flist->files);
1165 rprintf(FINFO, "send_file_list done\n");
1171 struct file_list *recv_file_list(int f)
1173 struct file_list *flist;
1174 unsigned short flags;
1176 extern int list_only;
1178 if (show_filelist_p())
1179 start_filelist_progress("receiving file list");
1181 start_read = stats.total_read;
1183 flist = new(struct file_list);
1188 flist->malloced = 1000;
1189 flist->files = new_array(struct file_struct *, flist->malloced);
1194 while ((flags = read_byte(f)) != 0) {
1195 int i = flist->count;
1197 flist_expand(flist);
1199 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1200 flags |= read_byte(f) << 8;
1201 receive_file_entry(&flist->files[i], flags, f);
1203 if (S_ISREG(flist->files[i]->mode))
1204 stats.total_size += flist->files[i]->length;
1208 maybe_emit_filelist_progress(flist);
1211 rprintf(FINFO, "recv_file_name(%s)\n",
1212 f_name(flist->files[i]));
1215 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1218 rprintf(FINFO, "received %d names\n", flist->count);
1220 if (show_filelist_p())
1221 finish_filelist_progress(flist);
1223 clean_flist(flist, relative_paths, 1);
1226 /* Now send the uid/gid list. This was introduced in
1227 * protocol version 15 */
1228 recv_uid_list(f, flist);
1231 /* Recv the io_error flag */
1232 if (lp_ignore_errors(module_id) || ignore_errors)
1235 io_error |= read_int(f);
1241 for (i = 0; i < flist->count; i++)
1242 list_file_entry(flist->files[i]);
1246 rprintf(FINFO, "recv_file_list done\n");
1248 stats.flist_size = stats.total_read - start_read;
1249 stats.num_files = flist->count;
1254 out_of_memory("recv_file_list");
1255 return NULL; /* not reached */
1259 int file_compare(struct file_struct **file1, struct file_struct **file2)
1261 struct file_struct *f1 = *file1;
1262 struct file_struct *f2 = *file2;
1264 if (!f1->basename && !f2->basename)
1270 if (f1->dirname == f2->dirname)
1271 return u_strcmp(f1->basename, f2->basename);
1272 return f_name_cmp(f1, f2);
1276 int flist_find(struct file_list *flist, struct file_struct *f)
1278 int low = 0, high = flist->count - 1;
1280 while (high >= 0 && !flist->files[high]->basename) high--;
1285 while (low != high) {
1286 int mid = (low + high) / 2;
1287 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1289 return flist_up(flist, mid);
1296 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1297 return flist_up(flist, low);
1305 void free_file(struct file_struct *file)
1310 free(file->basename);
1311 if (!IS_DEVICE(file->mode) && file->u.sum)
1312 free(file->u.sum); /* Handles u.link too. */
1313 #if SUPPORT_HARD_LINKS
1314 if (file->link_u.idev)
1315 free((char*)file->link_u.idev); /* Handles link_u.links too. */
1322 * allocate a new file list
1324 struct file_list *flist_new(void)
1326 struct file_list *flist;
1328 flist = new(struct file_list);
1330 out_of_memory("send_file_list");
1333 flist->malloced = 0;
1334 flist->files = NULL;
1337 flist->string_area = string_area_new(0);
1339 flist->string_area = NULL;
1345 * free up all elements in a flist
1347 void flist_free(struct file_list *flist)
1350 for (i = 1; i < flist->count; i++) {
1351 if (!flist->string_area)
1352 free_file(flist->files[i]);
1353 free(flist->files[i]);
1355 /* FIXME: I don't think we generally need to blank the flist
1356 * since it's about to be freed. This will just cause more
1357 * memory traffic. If you want a freed-memory debugger, you
1358 * know where to get it. */
1359 memset((char *) flist->files, 0,
1360 sizeof(flist->files[0]) * flist->count);
1362 if (flist->string_area)
1363 string_area_free(flist->string_area);
1364 memset((char *) flist, 0, sizeof(*flist));
1370 * This routine ensures we don't have any duplicate names in our file list.
1371 * duplicate names can cause corruption because of the pipelining
1373 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1377 if (!flist || flist->count == 0)
1380 qsort(flist->files, flist->count,
1381 sizeof(flist->files[0]), (int (*)()) file_compare);
1383 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1384 if (flist->files[i]->basename) {
1389 while (++i < flist->count) {
1390 if (!flist->files[i]->basename)
1392 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1393 if (verbose > 1 && !am_server) {
1395 "removing duplicate name %s from file list %d\n",
1396 f_name(flist->files[i]), i);
1398 /* Make sure that if we unduplicate '.', that we don't
1399 * lose track of a user-specified starting point (or
1400 * else deletions will mysteriously fail with -R). */
1401 if (flist->files[i]->flags & FLAG_TOP_DIR)
1402 flist->files[prev_i]->flags |= FLAG_TOP_DIR;
1403 /* it's not great that the flist knows the semantics of
1404 * the file memory usage, but i'd rather not add a flag
1405 * byte to that struct.
1406 * XXX can i use a bit in the flags field? */
1407 if (flist->string_area)
1408 flist->files[i][0] = null_file;
1410 free_file(flist->files[i]);
1416 /* we need to strip off the root directory in the case
1417 of relative paths, but this must be done _after_
1418 the sorting phase */
1419 for (i = 0; i < flist->count; i++) {
1420 if (flist->files[i]->dirname &&
1421 flist->files[i]->dirname[0] == '/') {
1422 memmove(&flist->files[i]->dirname[0],
1423 &flist->files[i]->dirname[1],
1424 strlen(flist->files[i]->dirname));
1427 if (flist->files[i]->dirname &&
1428 !flist->files[i]->dirname[0]) {
1429 flist->files[i]->dirname = NULL;
1437 for (i = 0; i < flist->count; i++) {
1438 rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f\n",
1440 NS(flist->files[i]->basedir),
1441 NS(flist->files[i]->dirname),
1442 NS(flist->files[i]->basename),
1443 (int) flist->files[i]->mode,
1444 (double) flist->files[i]->length);
1449 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1451 /* Compare the names of two file_struct entities, just like strcmp()
1452 * would do if it were operating on the joined strings. We assume
1453 * that there are no 0-length strings.
1455 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1458 const uchar *c1, *c2;
1459 enum fnc_state state1, state2;
1461 if (!f1 || !f1->basename) {
1462 if (!f2 || !f2->basename)
1466 if (!f2 || !f2->basename)
1469 if (!(c1 = (uchar*)f1->dirname)) {
1471 c1 = (uchar*)f1->basename;
1474 if (!(c2 = (uchar*)f2->dirname)) {
1476 c2 = (uchar*)f2->basename;
1481 if ((dif = (int)*c1 - (int)*c2) != 0)
1491 c1 = (uchar*)f1->basename;
1505 c2 = (uchar*)f2->basename;
1519 /* Return a copy of the full filename of a flist entry, using the indicated
1520 * buffer. No size-checking is done because we checked the size when creating
1521 * the file_struct entry.
1523 char *f_name_to(struct file_struct *f, char *fbuf)
1525 if (!f || !f->basename)
1529 int len = strlen(f->dirname);
1530 memcpy(fbuf, f->dirname, len);
1532 strcpy(fbuf + len + 1, f->basename);
1534 strcpy(fbuf, f->basename);
1539 /* Like f_name_to(), but we rotate through 5 static buffers of our own.
1541 char *f_name(struct file_struct *f)
1543 static char names[5][MAXPATHLEN];
1544 static unsigned int n;
1546 n = (n + 1) % (sizeof names / sizeof names[0]);
1548 return f_name_to(f, names[n]);