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 * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html
30 extern struct stats stats;
33 extern int do_progress;
35 extern int always_checksum;
37 extern int ignore_errors;
39 extern int cvs_exclude;
42 extern char curr_dir[MAXPATHLEN];
43 extern char *files_from;
44 extern int filesfrom_fd;
46 extern int one_file_system;
47 extern int make_backups;
48 extern int preserve_links;
49 extern int preserve_hard_links;
50 extern int preserve_perms;
51 extern int preserve_devices;
52 extern int preserve_uid;
53 extern int preserve_gid;
54 extern int preserve_times;
55 extern int relative_paths;
56 extern int implied_dirs;
57 extern int copy_links;
58 extern int copy_unsafe_links;
59 extern int protocol_version;
60 extern int sanitize_paths;
62 extern int read_batch;
63 extern int write_batch;
65 extern struct exclude_struct **exclude_list;
66 extern struct exclude_struct **server_exclude_list;
67 extern struct exclude_struct **local_exclude_list;
71 static char empty_sum[MD4_SUM_LENGTH];
73 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
76 static int show_filelist_p(void)
78 return verbose && (recurse || files_from) && !am_server;
81 static void start_filelist_progress(char *kind)
83 rprintf(FINFO, "%s ... ", kind);
84 if ((verbose > 1) || do_progress)
90 static void emit_filelist_progress(const struct file_list *flist)
92 rprintf(FINFO, " %d files...\r", flist->count);
96 static void maybe_emit_filelist_progress(const struct file_list *flist)
98 if (do_progress && show_filelist_p() && ((flist->count % 100) == 0))
99 emit_filelist_progress(flist);
103 static void finish_filelist_progress(const struct file_list *flist)
106 /* This overwrites the progress line */
107 rprintf(FINFO, "%d file%sto consider\n",
108 flist->count, flist->count == 1 ? " " : "s ");
110 rprintf(FINFO, "done\n");
113 void show_flist_stats(void)
119 static void list_file_entry(struct file_struct *f)
124 /* this can happen if duplicate names were removed */
127 permstring(perms, f->mode);
130 if (preserve_links && S_ISLNK(f->mode)) {
131 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
133 (double) f->length, timestring(f->modtime),
134 f_name(f), f->u.link);
137 rprintf(FINFO, "%s %11.0f %s %s\n",
139 (double) f->length, timestring(f->modtime),
145 * Stat either a symlink or its referent, depending on the settings of
146 * copy_links, copy_unsafe_links, etc.
148 * @retval -1 on error
150 * @retval 0 for success
152 * @post If @p path is a symlink, then @p linkbuf (of size @c
153 * MAXPATHLEN) contains the symlink target.
155 * @post @p buffer contains information about the link or the
156 * referrent as appropriate, if they exist.
158 int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
162 return do_stat(path, buffer);
163 if (do_lstat(path, buffer) == -1)
165 if (S_ISLNK(buffer->st_mode)) {
166 int l = readlink((char *) path, linkbuf, MAXPATHLEN - 1);
170 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
172 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
175 return do_stat(path, buffer);
180 return do_stat(path, buffer);
184 int link_stat(const char *path, STRUCT_STAT * buffer)
188 return do_stat(path, buffer);
189 return do_lstat(path, buffer);
191 return do_stat(path, buffer);
196 * This function is used to check if a file should be included/excluded
197 * from the list of files based on its name and type etc. The value of
198 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
200 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
202 #if 0 /* This currently never happens, so avoid a useless compare. */
203 if (exclude_level == NO_EXCLUDES)
207 /* never exclude '.', even if somebody does --exclude '*' */
208 if (fname[0] == '.' && !fname[1])
210 /* Handle the -R version of the '.' dir. */
211 if (fname[0] == '/') {
212 int len = strlen(fname);
213 if (fname[len-1] == '.' && fname[len-2] == '/')
217 if (server_exclude_list
218 && check_exclude(server_exclude_list, fname, is_dir))
220 if (exclude_level != ALL_EXCLUDES)
222 if (exclude_list && check_exclude(exclude_list, fname, is_dir))
224 if (local_exclude_list
225 && check_exclude(local_exclude_list, fname, is_dir))
230 /* used by the one_file_system code */
231 static dev_t filesystem_dev;
233 static void set_filesystem(char *fname)
236 if (link_stat(fname, &st) != 0)
238 filesystem_dev = st.st_dev;
242 static int to_wire_mode(mode_t mode)
245 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
246 return (mode & ~(_S_IFMT)) | 0120000;
251 static mode_t from_wire_mode(int mode)
253 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
254 return (mode & ~(_S_IFMT)) | _S_IFLNK;
255 return (mode_t) mode;
259 static void send_directory(int f, struct file_list *flist, char *dir);
261 static char *flist_dir;
262 static int flist_dir_len;
266 * Make sure @p flist is big enough to hold at least @p flist->count
269 static void flist_expand(struct file_list *flist)
271 if (flist->count >= flist->malloced) {
274 if (flist->malloced < 1000)
275 flist->malloced += 1000;
277 flist->malloced *= 2;
280 new_ptr = realloc_array(flist->files,
281 struct file_struct *,
284 new_ptr = new_array(struct file_struct *,
289 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
291 (double)sizeof(flist->files[0])
293 (new_ptr == flist->files) ? " not" : "");
296 flist->files = (struct file_struct **) new_ptr;
299 out_of_memory("flist_expand");
303 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
305 unsigned short flags;
306 static time_t modtime;
308 static DEV64_T rdev, rdev_high;
312 static char lastname[MAXPATHLEN];
313 char *fname, fbuf[MAXPATHLEN];
321 modtime = 0, mode = 0;
322 rdev = 0, rdev_high = 0, dev = 0;
328 io_write_phase = "send_file_entry";
330 fname = f_name_to(file, fbuf);
334 if (file->mode == mode)
335 flags |= XMIT_SAME_MODE;
338 if (preserve_devices) {
339 if (protocol_version < 28) {
340 if (IS_DEVICE(mode)) {
341 if (file->u.rdev == rdev) {
342 /* Set both flags to simplify the test
343 * when writing the data. */
344 flags |= XMIT_SAME_RDEV_pre28
345 | XMIT_SAME_HIGH_RDEV;
350 } else if (IS_DEVICE(mode)) {
351 if ((file->u.rdev & ~0xFF) == rdev_high)
352 flags |= XMIT_SAME_HIGH_RDEV;
355 rdev_high = rdev & ~0xFF;
359 if (file->uid == uid)
360 flags |= XMIT_SAME_UID;
363 if (file->gid == gid)
364 flags |= XMIT_SAME_GID;
367 if (file->modtime == modtime)
368 flags |= XMIT_SAME_TIME;
370 modtime = file->modtime;
372 #if SUPPORT_HARD_LINKS
373 if (file->link_u.idev) {
374 if (file->F_DEV == dev) {
375 if (protocol_version >= 28)
376 flags |= XMIT_SAME_DEV;
379 flags |= XMIT_HAS_IDEV_DATA;
384 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
386 l2 = strlen(fname+l1);
389 flags |= XMIT_SAME_NAME;
391 flags |= XMIT_LONG_NAME;
393 /* We must make sure we don't send a zero flag byte or the
394 * other end will terminate the flist transfer. Note that
395 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
396 * it's harmless way to add a bit to the first flag byte. */
397 if (protocol_version >= 28) {
398 if (!flags && !S_ISDIR(mode))
399 flags |= XMIT_TOP_DIR;
400 if ((flags & 0xFF00) || !flags) {
401 flags |= XMIT_EXTENDED_FLAGS;
402 write_byte(f, flags);
403 write_byte(f, flags >> 8);
405 write_byte(f, flags);
407 if (!(flags & 0xFF) && !S_ISDIR(mode))
408 flags |= XMIT_TOP_DIR;
410 flags |= XMIT_LONG_NAME;
411 write_byte(f, flags);
413 if (flags & XMIT_SAME_NAME)
415 if (flags & XMIT_LONG_NAME)
419 write_buf(f, fname + l1, l2);
421 write_longint(f, file->length);
422 if (!(flags & XMIT_SAME_TIME))
423 write_int(f, modtime);
424 if (!(flags & XMIT_SAME_MODE))
425 write_int(f, to_wire_mode(mode));
426 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
430 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
434 if (preserve_devices && IS_DEVICE(mode)) {
435 /* If XMIT_SAME_HIGH_RDEV is off, XMIT_SAME_RDEV_pre28 is
437 if (!(flags & XMIT_SAME_HIGH_RDEV))
439 else if (protocol_version >= 28)
444 if (preserve_links && S_ISLNK(mode)) {
445 int len = strlen(file->u.link);
447 write_buf(f, file->u.link, len);
451 #if SUPPORT_HARD_LINKS
452 if (flags & XMIT_HAS_IDEV_DATA) {
453 if (protocol_version < 26) {
454 /* 32-bit dev_t and ino_t */
456 write_int(f, file->F_INODE);
458 /* 64-bit dev_t and ino_t */
459 if (!(flags & XMIT_SAME_DEV))
460 write_longint(f, dev);
461 write_longint(f, file->F_INODE);
466 if (always_checksum) {
470 else if (protocol_version < 28) {
471 /* Prior to 28, we sent a useless set of nulls. */
476 write_buf(f, sum, protocol_version < 21? 2
481 strlcpy(lastname, fname, MAXPATHLEN);
483 io_write_phase = "unknown";
488 void receive_file_entry(struct file_struct **fptr, unsigned short flags, int f)
490 static time_t modtime;
492 static DEV64_T rdev, rdev_high;
496 static char lastname[MAXPATHLEN], *lastdir;
497 static int lastdir_len = -1;
498 char thisname[MAXPATHLEN];
499 unsigned int l1 = 0, l2 = 0;
500 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
502 char *basename, *dirname, *bp;
503 struct file_struct *file;
506 modtime = 0, mode = 0;
507 rdev = 0, rdev_high = 0, dev = 0;
513 if (flags & XMIT_SAME_NAME)
516 if (flags & XMIT_LONG_NAME)
521 if (l2 >= MAXPATHLEN - l1) {
523 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
524 flags, l1, l2, lastname);
525 overflow("receive_file_entry");
528 strlcpy(thisname, lastname, l1 + 1);
529 read_sbuf(f, &thisname[l1], l2);
530 thisname[l1 + l2] = 0;
532 strlcpy(lastname, thisname, MAXPATHLEN);
534 clean_fname(thisname);
537 sanitize_path(thisname, NULL);
539 if ((basename = strrchr(thisname, '/')) != NULL) {
540 dirname_len = ++basename - thisname; /* counts future '\0' */
541 if (lastdir_len == dirname_len - 1
542 && strncmp(thisname, lastdir, lastdir_len) == 0) {
544 dirname_len = 0; /* indicates no copy is needed */
552 basename_len = strlen(basename) + 1; /* count the '\0' */
554 file_length = read_longint(f);
555 if (!(flags & XMIT_SAME_TIME))
556 modtime = (time_t)read_int(f);
557 if (!(flags & XMIT_SAME_MODE))
558 mode = from_wire_mode(read_int(f));
560 if (preserve_uid && !(flags & XMIT_SAME_UID))
561 uid = (uid_t)read_int(f);
562 if (preserve_gid && !(flags & XMIT_SAME_GID))
563 gid = (gid_t)read_int(f);
565 if (preserve_devices) {
566 if (protocol_version < 28) {
567 if (IS_DEVICE(mode)) {
568 if (!(flags & XMIT_SAME_RDEV_pre28))
569 rdev = (DEV64_T)read_int(f);
572 } else if (IS_DEVICE(mode)) {
573 if (!(flags & XMIT_SAME_HIGH_RDEV)) {
574 rdev = (DEV64_T)read_int(f);
575 rdev_high = rdev & ~0xFF;
577 rdev = rdev_high | (DEV64_T)read_byte(f);
582 if (preserve_links && S_ISLNK(mode)) {
583 linkname_len = read_int(f) + 1; /* count the '\0' */
584 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
585 rprintf(FERROR, "overflow: linkname_len=%d\n",
587 overflow("receive_file_entry");
594 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
596 alloc_len = sizeof file[0] + dirname_len + basename_len
597 + linkname_len + sum_len;
598 if (!(bp = new_array(char, alloc_len)))
599 out_of_memory("receive_file_entry");
600 file = *fptr = (struct file_struct *)bp;
601 memset(bp, 0, sizeof file[0]);
602 bp += sizeof file[0];
604 file->flags = flags & XMIT_TOP_DIR ? FLAG_TOP_DIR : 0;
605 file->modtime = modtime;
606 file->length = file_length;
612 file->dirname = lastdir = bp;
613 lastdir_len = dirname_len - 1;
614 memcpy(bp, dirname, dirname_len - 1);
618 file->dirname = dirname;
621 memcpy(bp, basename, basename_len);
624 if (preserve_devices && IS_DEVICE(mode))
630 read_sbuf(f, bp, linkname_len - 1);
632 sanitize_path(bp, lastdir);
637 #if SUPPORT_HARD_LINKS
638 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
639 flags |= XMIT_HAS_IDEV_DATA;
640 if (flags & XMIT_HAS_IDEV_DATA) {
641 if (!(file->link_u.idev = new(struct idev)))
642 out_of_memory("file inode data");
643 if (protocol_version < 26) {
645 file->F_INODE = read_int(f);
647 if (!(flags & XMIT_SAME_DEV))
648 dev = read_longint(f);
649 file->F_INODE = read_longint(f);
655 if (always_checksum) {
658 file->u.sum = sum = bp;
660 } else if (protocol_version < 28) {
661 /* Prior to 28, we get a useless set of nulls. */
666 read_buf(f, sum, protocol_version < 21? 2
671 if (!preserve_perms) {
672 extern int orig_umask;
673 /* set an appropriate set of permissions based on original
674 * permissions and umask. This emulates what GNU cp does */
675 file->mode &= ~orig_umask;
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, int exclude_level)
697 static char *lastdir;
698 static int lastdir_len = -1;
699 struct file_struct *file;
701 char sum[SUM_LENGTH];
702 char thisname[MAXPATHLEN];
703 char linkname[MAXPATHLEN];
704 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
705 char *basename, *dirname, *bp;
706 unsigned short flags = 0;
708 if (strlcpy(thisname, fname, sizeof thisname)
709 >= sizeof thisname - flist_dir_len) {
710 rprintf(FINFO, "skipping overly long name: %s\n", fname);
713 clean_fname(thisname);
715 sanitize_path(thisname, NULL);
717 memset(sum, 0, SUM_LENGTH);
719 if (readlink_stat(thisname, &st, linkname) != 0) {
720 int save_errno = errno;
721 if (errno == ENOENT && exclude_level != NO_EXCLUDES) {
722 /* either symlink pointing nowhere or file that
723 * was removed during rsync run; see if excluded
724 * before reporting an error */
725 if (check_exclude_file(thisname, 0, exclude_level)) {
726 /* file is excluded anyway, ignore silently */
730 io_error |= IOERR_GENERAL;
731 rprintf(FERROR, "readlink %s failed: %s\n",
732 full_fname(thisname), strerror(save_errno));
736 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
737 if (exclude_level == NO_EXCLUDES)
740 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
741 rprintf(FINFO, "skipping directory %s\n", thisname);
745 if (one_file_system && st.st_dev != filesystem_dev) {
746 /* We allow a directory though to preserve the mount point.
747 * However, flag it so that we don't recurse. */
748 if (!S_ISDIR(st.st_mode))
750 flags |= FLAG_MOUNT_POINT;
753 if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level))
756 if (lp_ignore_nonreadable(module_id) && access(thisname, R_OK) != 0)
762 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
763 who_am_i(), thisname, exclude_level);
766 if ((basename = strrchr(thisname, '/')) != NULL) {
767 dirname_len = ++basename - thisname; /* counts future '\0' */
768 if (lastdir_len == dirname_len - 1
769 && strncmp(thisname, lastdir, lastdir_len) == 0) {
771 dirname_len = 0; /* indicates no copy is needed */
779 basename_len = strlen(basename) + 1; /* count the '\0' */
782 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
787 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
789 alloc_len = sizeof file[0] + dirname_len + basename_len
790 + linkname_len + sum_len;
791 if (!(bp = new_array(char, alloc_len)))
792 out_of_memory("receive_file_entry");
793 file = (struct file_struct *)bp;
794 memset(bp, 0, sizeof file[0]);
795 bp += sizeof file[0];
798 file->modtime = st.st_mtime;
799 file->length = st.st_size;
800 file->mode = st.st_mode;
801 file->uid = st.st_uid;
802 file->gid = st.st_gid;
805 file->dirname = lastdir = bp;
806 lastdir_len = dirname_len - 1;
807 memcpy(bp, dirname, dirname_len - 1);
811 file->dirname = dirname;
814 memcpy(bp, basename, basename_len);
817 #ifdef HAVE_STRUCT_STAT_ST_RDEV
818 if (preserve_devices && IS_DEVICE(st.st_mode))
819 file->u.rdev = st.st_rdev;
825 memcpy(bp, linkname, linkname_len);
830 #if SUPPORT_HARD_LINKS
831 if (preserve_hard_links) {
832 if (protocol_version < 28 ? S_ISREG(st.st_mode)
833 : !S_ISDIR(st.st_mode) && st.st_nlink > 1) {
834 if (!(file->link_u.idev = new(struct idev)))
835 out_of_memory("file inode data");
836 file->F_DEV = st.st_dev;
837 file->F_INODE = st.st_ino;
844 file_checksum(thisname, bp, 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,
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);
1293 * Free up any resources a file_struct has allocated, and optionally free
1296 void free_file(struct file_struct *file, int free_the_struct)
1298 #if SUPPORT_HARD_LINKS
1299 if (file->link_u.idev)
1300 free((char*)file->link_u.idev); /* Handles link_u.links too. */
1302 if (free_the_struct)
1305 memset(file, 0, sizeof file[0]);
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;
1328 * free up all elements in a flist
1330 void flist_free(struct file_list *flist)
1333 for (i = 1; i < flist->count; i++)
1334 free_file(flist->files[i], FREE_STRUCT);
1341 * This routine ensures we don't have any duplicate names in our file list.
1342 * duplicate names can cause corruption because of the pipelining
1344 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1348 if (!flist || flist->count == 0)
1351 qsort(flist->files, flist->count,
1352 sizeof(flist->files[0]), (int (*)()) file_compare);
1354 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1355 if (flist->files[i]->basename) {
1360 while (++i < flist->count) {
1361 if (!flist->files[i]->basename)
1363 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1364 if (verbose > 1 && !am_server) {
1366 "removing duplicate name %s from file list %d\n",
1367 f_name(flist->files[i]), i);
1369 /* Make sure that if we unduplicate '.', that we don't
1370 * lose track of a user-specified starting point (or
1371 * else deletions will mysteriously fail with -R). */
1372 if (flist->files[i]->flags & FLAG_TOP_DIR)
1373 flist->files[prev_i]->flags |= FLAG_TOP_DIR;
1374 free_file(flist->files[i], CLEAR_STRUCT);
1380 /* we need to strip off the root directory in the case
1381 of relative paths, but this must be done _after_
1382 the sorting phase */
1383 for (i = 0; i < flist->count; i++) {
1384 if (flist->files[i]->dirname &&
1385 flist->files[i]->dirname[0] == '/') {
1386 memmove(&flist->files[i]->dirname[0],
1387 &flist->files[i]->dirname[1],
1388 strlen(flist->files[i]->dirname));
1391 if (flist->files[i]->dirname &&
1392 !flist->files[i]->dirname[0]) {
1393 flist->files[i]->dirname = NULL;
1401 for (i = 0; i < flist->count; i++) {
1402 rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f\n",
1404 NS(flist->files[i]->basedir),
1405 NS(flist->files[i]->dirname),
1406 NS(flist->files[i]->basename),
1407 (int) flist->files[i]->mode,
1408 (double) flist->files[i]->length);
1413 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1415 /* Compare the names of two file_struct entities, just like strcmp()
1416 * would do if it were operating on the joined strings. We assume
1417 * that there are no 0-length strings.
1419 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1422 const uchar *c1, *c2;
1423 enum fnc_state state1, state2;
1425 if (!f1 || !f1->basename) {
1426 if (!f2 || !f2->basename)
1430 if (!f2 || !f2->basename)
1433 if (!(c1 = (uchar*)f1->dirname)) {
1435 c1 = (uchar*)f1->basename;
1438 if (!(c2 = (uchar*)f2->dirname)) {
1440 c2 = (uchar*)f2->basename;
1445 if ((dif = (int)*c1 - (int)*c2) != 0)
1455 c1 = (uchar*)f1->basename;
1469 c2 = (uchar*)f2->basename;
1483 /* Return a copy of the full filename of a flist entry, using the indicated
1484 * buffer. No size-checking is done because we checked the size when creating
1485 * the file_struct entry.
1487 char *f_name_to(struct file_struct *f, char *fbuf)
1489 if (!f || !f->basename)
1493 int len = strlen(f->dirname);
1494 memcpy(fbuf, f->dirname, len);
1496 strcpy(fbuf + len + 1, f->basename);
1498 strcpy(fbuf, f->basename);
1503 /* Like f_name_to(), but we rotate through 5 static buffers of our own.
1505 char *f_name(struct file_struct *f)
1507 static char names[5][MAXPATHLEN];
1508 static unsigned int n;
1510 n = (n + 1) % (sizeof names / sizeof names[0]);
1512 return f_name_to(f, names[n]);