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;
36 extern int always_checksum;
38 extern int ignore_errors;
40 extern int cvs_exclude;
43 extern char curr_dir[MAXPATHLEN];
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 char empty_sum[MD4_SUM_LENGTH];
73 static unsigned int min_file_struct_len;
75 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
82 /* Figure out how big the file_struct is without trailing padding */
83 min_file_struct_len = ((char*)&f.flags - (char*)&f) + sizeof f.flags;
87 static int show_filelist_p(void)
89 return verbose && (recurse || files_from) && !am_server;
92 static void start_filelist_progress(char *kind)
94 rprintf(FINFO, "%s ... ", kind);
95 if ((verbose > 1) || do_progress)
101 static void emit_filelist_progress(const struct file_list *flist)
103 rprintf(FINFO, " %d files...\r", flist->count);
107 static void maybe_emit_filelist_progress(const struct file_list *flist)
109 if (do_progress && show_filelist_p() && ((flist->count % 100) == 0))
110 emit_filelist_progress(flist);
114 static void finish_filelist_progress(const struct file_list *flist)
117 /* This overwrites the progress line */
118 rprintf(FINFO, "%d file%sto consider\n",
119 flist->count, flist->count == 1 ? " " : "s ");
121 rprintf(FINFO, "done\n");
124 void show_flist_stats(void)
130 static void list_file_entry(struct file_struct *f)
135 /* this can happen if duplicate names were removed */
138 permstring(perms, f->mode);
141 if (preserve_links && S_ISLNK(f->mode)) {
142 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
144 (double) f->length, timestring(f->modtime),
145 f_name(f), f->u.link);
148 rprintf(FINFO, "%s %11.0f %s %s\n",
150 (double) f->length, timestring(f->modtime),
156 * Stat either a symlink or its referent, depending on the settings of
157 * copy_links, copy_unsafe_links, etc.
159 * @retval -1 on error
161 * @retval 0 for success
163 * @post If @p path is a symlink, then @p linkbuf (of size @c
164 * MAXPATHLEN) contains the symlink target.
166 * @post @p buffer contains information about the link or the
167 * referrent as appropriate, if they exist.
169 int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
173 return do_stat(path, buffer);
174 if (do_lstat(path, buffer) == -1)
176 if (S_ISLNK(buffer->st_mode)) {
177 int l = readlink((char *) path, linkbuf, MAXPATHLEN - 1);
181 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
183 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
186 return do_stat(path, buffer);
191 return do_stat(path, buffer);
195 int link_stat(const char *path, STRUCT_STAT * buffer)
199 return do_stat(path, buffer);
200 return do_lstat(path, buffer);
202 return do_stat(path, buffer);
207 * This function is used to check if a file should be included/excluded
208 * from the list of files based on its name and type etc. The value of
209 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
211 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
213 #if 0 /* This currently never happens, so avoid a useless compare. */
214 if (exclude_level == NO_EXCLUDES)
218 /* never exclude '.', even if somebody does --exclude '*' */
219 if (fname[0] == '.' && !fname[1])
221 /* Handle the -R version of the '.' dir. */
222 if (fname[0] == '/') {
223 int len = strlen(fname);
224 if (fname[len-1] == '.' && fname[len-2] == '/')
228 if (server_exclude_list
229 && check_exclude(server_exclude_list, fname, is_dir))
231 if (exclude_level != ALL_EXCLUDES)
233 if (exclude_list && check_exclude(exclude_list, fname, is_dir))
235 if (local_exclude_list
236 && check_exclude(local_exclude_list, fname, is_dir))
241 /* used by the one_file_system code */
242 static dev_t filesystem_dev;
244 static void set_filesystem(char *fname)
247 if (link_stat(fname, &st) != 0)
249 filesystem_dev = st.st_dev;
253 static int to_wire_mode(mode_t mode)
256 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
257 return (mode & ~(_S_IFMT)) | 0120000;
262 static mode_t from_wire_mode(int mode)
264 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
265 return (mode & ~(_S_IFMT)) | _S_IFLNK;
266 return (mode_t) mode;
270 static void send_directory(int f, struct file_list *flist, char *dir);
272 static char *flist_dir;
273 static int flist_dir_len;
277 * Make sure @p flist is big enough to hold at least @p flist->count
280 static void flist_expand(struct file_list *flist)
282 if (flist->count >= flist->malloced) {
285 if (flist->malloced < 1000)
286 flist->malloced += 1000;
288 flist->malloced *= 2;
291 new_ptr = realloc_array(flist->files,
292 struct file_struct *,
295 new_ptr = new_array(struct file_struct *,
300 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
302 (double) sizeof flist->files[0] * flist->malloced,
303 (new_ptr == flist->files) ? " not" : "");
306 flist->files = (struct file_struct **) new_ptr;
309 out_of_memory("flist_expand");
313 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
315 unsigned short flags;
316 static time_t modtime;
318 static DEV64_T rdev, rdev_high;
322 static char lastname[MAXPATHLEN];
323 char *fname, fbuf[MAXPATHLEN];
331 modtime = 0, mode = 0;
332 rdev = 0, rdev_high = 0, dev = 0;
338 io_write_phase = "send_file_entry";
340 fname = f_name_to(file, fbuf);
344 if (file->mode == mode)
345 flags |= XMIT_SAME_MODE;
348 if (preserve_devices) {
349 if (protocol_version < 28) {
350 if (IS_DEVICE(mode)) {
351 if (file->u.rdev == rdev) {
352 /* Set both flags to simplify the test
353 * when writing the data. */
354 flags |= XMIT_SAME_RDEV_pre28
355 | XMIT_SAME_HIGH_RDEV;
360 } else if (IS_DEVICE(mode)) {
361 if ((file->u.rdev & ~0xFF) == rdev_high)
362 flags |= XMIT_SAME_HIGH_RDEV;
365 rdev_high = rdev & ~0xFF;
369 if (file->uid == uid)
370 flags |= XMIT_SAME_UID;
373 if (file->gid == gid)
374 flags |= XMIT_SAME_GID;
377 if (file->modtime == modtime)
378 flags |= XMIT_SAME_TIME;
380 modtime = file->modtime;
382 #if SUPPORT_HARD_LINKS
383 if (file->link_u.idev) {
384 if (file->F_DEV == dev) {
385 if (protocol_version >= 28)
386 flags |= XMIT_SAME_DEV;
389 flags |= XMIT_HAS_IDEV_DATA;
394 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
396 l2 = strlen(fname+l1);
399 flags |= XMIT_SAME_NAME;
401 flags |= XMIT_LONG_NAME;
403 /* We must make sure we don't send a zero flag byte or the
404 * other end will terminate the flist transfer. Note that
405 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
406 * it's harmless way to add a bit to the first flag byte. */
407 if (protocol_version >= 28) {
408 if (!flags && !S_ISDIR(mode))
409 flags |= XMIT_TOP_DIR;
410 if ((flags & 0xFF00) || !flags) {
411 flags |= XMIT_EXTENDED_FLAGS;
412 write_byte(f, flags);
413 write_byte(f, flags >> 8);
415 write_byte(f, flags);
417 if (!(flags & 0xFF) && !S_ISDIR(mode))
418 flags |= XMIT_TOP_DIR;
420 flags |= XMIT_LONG_NAME;
421 write_byte(f, flags);
423 if (flags & XMIT_SAME_NAME)
425 if (flags & XMIT_LONG_NAME)
429 write_buf(f, fname + l1, l2);
431 write_longint(f, file->length);
432 if (!(flags & XMIT_SAME_TIME))
433 write_int(f, modtime);
434 if (!(flags & XMIT_SAME_MODE))
435 write_int(f, to_wire_mode(mode));
436 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
440 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
444 if (preserve_devices && IS_DEVICE(mode)) {
445 /* If XMIT_SAME_HIGH_RDEV is off, XMIT_SAME_RDEV_pre28 is
447 if (!(flags & XMIT_SAME_HIGH_RDEV))
449 else if (protocol_version >= 28)
454 if (preserve_links && S_ISLNK(mode)) {
455 int len = strlen(file->u.link);
457 write_buf(f, file->u.link, len);
461 #if SUPPORT_HARD_LINKS
462 if (flags & XMIT_HAS_IDEV_DATA) {
463 if (protocol_version < 26) {
464 /* 32-bit dev_t and ino_t */
466 write_int(f, file->F_INODE);
468 /* 64-bit dev_t and ino_t */
469 if (!(flags & XMIT_SAME_DEV))
470 write_longint(f, dev);
471 write_longint(f, file->F_INODE);
476 if (always_checksum) {
480 else if (protocol_version < 28) {
481 /* Prior to 28, we sent a useless set of nulls. */
486 write_buf(f, sum, protocol_version < 21? 2
491 strlcpy(lastname, fname, MAXPATHLEN);
493 io_write_phase = "unknown";
498 void receive_file_entry(struct file_struct **fptr, unsigned short flags, int f)
500 static time_t modtime;
502 static DEV64_T rdev, rdev_high;
506 static char lastname[MAXPATHLEN], *lastdir;
507 static int lastdir_len = -1;
508 char thisname[MAXPATHLEN];
509 unsigned int l1 = 0, l2 = 0;
510 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
511 int file_struct_len, idev_len;
513 char *basename, *dirname, *bp;
514 struct file_struct *file;
517 modtime = 0, mode = 0;
518 rdev = 0, rdev_high = 0, dev = 0;
524 if (flags & XMIT_SAME_NAME)
527 if (flags & XMIT_LONG_NAME)
532 if (l2 >= MAXPATHLEN - l1) {
534 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
535 flags, l1, l2, lastname);
536 overflow("receive_file_entry");
539 strlcpy(thisname, lastname, l1 + 1);
540 read_sbuf(f, &thisname[l1], l2);
541 thisname[l1 + l2] = 0;
543 strlcpy(lastname, thisname, MAXPATHLEN);
545 clean_fname(thisname);
548 sanitize_path(thisname, NULL);
550 if ((basename = strrchr(thisname, '/')) != NULL) {
551 dirname_len = ++basename - thisname; /* counts future '\0' */
552 if (lastdir_len == dirname_len - 1
553 && strncmp(thisname, lastdir, lastdir_len) == 0) {
555 dirname_len = 0; /* indicates no copy is needed */
563 basename_len = strlen(basename) + 1; /* count the '\0' */
565 file_length = read_longint(f);
566 if (!(flags & XMIT_SAME_TIME))
567 modtime = (time_t)read_int(f);
568 if (!(flags & XMIT_SAME_MODE))
569 mode = from_wire_mode(read_int(f));
571 if (preserve_uid && !(flags & XMIT_SAME_UID))
572 uid = (uid_t)read_int(f);
573 if (preserve_gid && !(flags & XMIT_SAME_GID))
574 gid = (gid_t)read_int(f);
576 if (preserve_devices) {
577 if (protocol_version < 28) {
578 if (IS_DEVICE(mode)) {
579 if (!(flags & XMIT_SAME_RDEV_pre28))
580 rdev = (DEV64_T)read_int(f);
583 } else if (IS_DEVICE(mode)) {
584 if (!(flags & XMIT_SAME_HIGH_RDEV)) {
585 rdev = (DEV64_T)read_int(f);
586 rdev_high = rdev & ~0xFF;
588 rdev = rdev_high | (DEV64_T)read_byte(f);
593 if (preserve_links && S_ISLNK(mode)) {
594 linkname_len = read_int(f) + 1; /* count the '\0' */
595 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
596 rprintf(FERROR, "overflow: linkname_len=%d\n",
598 overflow("receive_file_entry");
605 #if SUPPORT_HARD_LINKS
606 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
607 flags |= XMIT_HAS_IDEV_DATA;
608 if (flags & XMIT_HAS_IDEV_DATA)
609 idev_len = sizeof (struct idev);
614 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
615 file_struct_len = idev_len? sizeof file[0] : min_file_struct_len;
617 alloc_len = file_struct_len + dirname_len + basename_len
618 + linkname_len + sum_len + idev_len;
619 if (!(bp = new_array(char, alloc_len)))
620 out_of_memory("receive_file_entry");
621 file = *fptr = (struct file_struct *)bp;
622 memset(bp, 0, min_file_struct_len);
623 bp += file_struct_len;
625 file->flags = flags & XMIT_TOP_DIR ? FLAG_TOP_DIR : 0;
626 file->modtime = modtime;
627 file->length = file_length;
632 #if SUPPORT_HARD_LINKS
634 file->link_u.idev = (struct idev *)bp;
640 file->dirname = lastdir = bp;
641 lastdir_len = dirname_len - 1;
642 memcpy(bp, dirname, dirname_len - 1);
646 file->dirname = dirname;
649 memcpy(bp, basename, basename_len);
652 if (preserve_devices && IS_DEVICE(mode))
658 read_sbuf(f, bp, linkname_len - 1);
660 sanitize_path(bp, lastdir);
665 #if SUPPORT_HARD_LINKS
667 if (protocol_version < 26) {
669 file->F_INODE = read_int(f);
671 if (!(flags & XMIT_SAME_DEV))
672 dev = read_longint(f);
673 file->F_INODE = read_longint(f);
679 if (always_checksum) {
682 file->u.sum = sum = bp;
684 } else if (protocol_version < 28) {
685 /* Prior to 28, we get a useless set of nulls. */
690 read_buf(f, sum, protocol_version < 21? 2
695 if (!preserve_perms) {
696 extern int orig_umask;
697 /* set an appropriate set of permissions based on original
698 * permissions and umask. This emulates what GNU cp does */
699 file->mode &= ~orig_umask;
705 * Create a file_struct for a named file by reading its stat()
706 * information and performing extensive checks against global
709 * @return the new file, or NULL if there was an error or this file
710 * should be excluded.
712 * @todo There is a small optimization opportunity here to avoid
713 * stat()ing the file in some circumstances, which has a certain cost.
714 * We are called immediately after doing readdir(), and so we may
715 * already know the d_type of the file. We could for example avoid
716 * statting directories if we're not recursing, but this is not a very
717 * important case. Some systems may not have d_type.
719 struct file_struct *make_file(char *fname, int exclude_level)
721 static char *lastdir;
722 static int lastdir_len = -1;
723 struct file_struct *file;
725 char sum[SUM_LENGTH];
726 char thisname[MAXPATHLEN];
727 char linkname[MAXPATHLEN];
728 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
729 int file_struct_len, idev_len;
730 char *basename, *dirname, *bp;
731 unsigned short flags = 0;
733 if (strlcpy(thisname, fname, sizeof thisname)
734 >= sizeof thisname - flist_dir_len) {
735 rprintf(FINFO, "skipping overly long name: %s\n", fname);
738 clean_fname(thisname);
740 sanitize_path(thisname, NULL);
742 memset(sum, 0, SUM_LENGTH);
744 if (readlink_stat(thisname, &st, linkname) != 0) {
745 int save_errno = errno;
746 if (errno == ENOENT && exclude_level != NO_EXCLUDES) {
747 /* either symlink pointing nowhere or file that
748 * was removed during rsync run; see if excluded
749 * before reporting an error */
750 if (check_exclude_file(thisname, 0, exclude_level)) {
751 /* file is excluded anyway, ignore silently */
755 io_error |= IOERR_GENERAL;
756 rprintf(FERROR, "readlink %s failed: %s\n",
757 full_fname(thisname), strerror(save_errno));
761 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
762 if (exclude_level == NO_EXCLUDES)
765 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
766 rprintf(FINFO, "skipping directory %s\n", thisname);
770 /* We only care about directories because we need to avoid recursing
771 * into a mount-point directory, not to avoid copying a symlinked
772 * file if -L (or similar) was specified. */
773 if (one_file_system && st.st_dev != filesystem_dev
774 && S_ISDIR(st.st_mode))
775 flags |= FLAG_MOUNT_POINT;
777 if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level))
780 if (lp_ignore_nonreadable(module_id) && access(thisname, R_OK) != 0)
786 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
787 who_am_i(), thisname, exclude_level);
790 if ((basename = strrchr(thisname, '/')) != NULL) {
791 dirname_len = ++basename - thisname; /* counts future '\0' */
792 if (lastdir_len == dirname_len - 1
793 && strncmp(thisname, lastdir, lastdir_len) == 0) {
795 dirname_len = 0; /* indicates no copy is needed */
803 basename_len = strlen(basename) + 1; /* count the '\0' */
806 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
811 #if SUPPORT_HARD_LINKS
812 if (preserve_hard_links) {
813 if (protocol_version < 28) {
814 if (S_ISREG(st.st_mode))
815 idev_len = sizeof (struct idev);
819 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
820 idev_len = sizeof (struct idev);
828 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
829 file_struct_len = idev_len? sizeof file[0] : min_file_struct_len;
831 alloc_len = file_struct_len + dirname_len + basename_len
832 + linkname_len + sum_len + idev_len;
833 if (!(bp = new_array(char, alloc_len)))
834 out_of_memory("receive_file_entry");
835 file = (struct file_struct *)bp;
836 memset(bp, 0, min_file_struct_len);
837 bp += file_struct_len;
840 file->modtime = st.st_mtime;
841 file->length = st.st_size;
842 file->mode = st.st_mode;
843 file->uid = st.st_uid;
844 file->gid = st.st_gid;
846 #if SUPPORT_HARD_LINKS
848 file->link_u.idev = (struct idev *)bp;
850 file->F_DEV = st.st_dev;
851 file->F_INODE = st.st_ino;
856 file->dirname = lastdir = bp;
857 lastdir_len = dirname_len - 1;
858 memcpy(bp, dirname, dirname_len - 1);
862 file->dirname = dirname;
865 memcpy(bp, basename, basename_len);
868 #ifdef HAVE_STRUCT_STAT_ST_RDEV
869 if (preserve_devices && IS_DEVICE(st.st_mode))
870 file->u.rdev = st.st_rdev;
876 memcpy(bp, linkname, linkname_len);
883 file_checksum(thisname, bp, st.st_size);
887 file->basedir = flist_dir;
889 if (!S_ISDIR(st.st_mode))
890 stats.total_size += st.st_size;
896 void send_file_name(int f, struct file_list *flist, char *fname,
897 int recursive, unsigned short base_flags)
899 struct file_struct *file;
900 char fbuf[MAXPATHLEN];
901 extern int delete_excluded;
903 /* f is set to -1 when calculating deletion file list */
904 file = make_file(fname,
905 f == -1 && delete_excluded? SERVER_EXCLUDES
911 maybe_emit_filelist_progress(flist);
916 file->flags |= FLAG_TOP_DIR;
918 if (file->basename[0]) {
919 flist->files[flist->count++] = file;
920 send_file_entry(file, f, base_flags);
923 if (recursive && S_ISDIR(file->mode)
924 && !(file->flags & FLAG_MOUNT_POINT)) {
925 struct exclude_struct **last_exclude_list = local_exclude_list;
926 send_directory(f, flist, f_name_to(file, fbuf));
927 local_exclude_list = last_exclude_list;
933 static void send_directory(int f, struct file_list *flist, char *dir)
937 char fname[MAXPATHLEN];
943 io_error |= IOERR_GENERAL;
944 rprintf(FERROR, "opendir %s failed: %s\n",
945 full_fname(dir), strerror(errno));
949 offset = strlcpy(fname, dir, MAXPATHLEN);
951 if (offset >= MAXPATHLEN || p[-1] != '/') {
952 if (offset >= MAXPATHLEN - 1) {
953 io_error |= IOERR_GENERAL;
954 rprintf(FERROR, "skipping long-named directory: %s\n",
963 local_exclude_list = NULL;
966 if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
967 < MAXPATHLEN - offset)
968 add_exclude_file(&local_exclude_list,fname,MISSING_OK,ADD_EXCLUDE);
970 io_error |= IOERR_GENERAL;
972 "cannot cvs-exclude in long-named directory %s\n",
977 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
978 char *dname = d_name(di);
979 if (dname[0] == '.' && (dname[1] == '\0'
980 || (dname[1] == '.' && dname[2] == '\0')))
982 if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset)
983 send_file_name(f, flist, fname, recurse, 0);
985 io_error |= IOERR_GENERAL;
987 "cannot send long-named file %s\n",
992 io_error |= IOERR_GENERAL;
993 rprintf(FERROR, "readdir(%s): (%d) %s\n",
994 dir, errno, strerror(errno));
997 if (local_exclude_list)
998 free_exclude_list(&local_exclude_list); /* Zeros pointer too */
1005 * The delete_files() function in receiver.c sets f to -1 so that we just
1006 * construct the file list in memory without sending it over the wire. It
1007 * also has the side-effect of ignoring user-excludes if delete_excluded
1008 * is set (so that the delete list includes user-excluded files).
1010 struct file_list *send_file_list(int f, int argc, char *argv[])
1014 char *p, *dir, olddir[sizeof curr_dir];
1015 char lastpath[MAXPATHLEN] = "";
1016 struct file_list *flist;
1020 if (show_filelist_p() && f != -1)
1021 start_filelist_progress("building file list");
1023 start_write = stats.total_written;
1025 flist = flist_new();
1028 io_start_buffering_out(f);
1029 if (filesfrom_fd >= 0) {
1030 if (argv[0] && !push_dir(argv[0])) {
1031 rprintf(FERROR, "push_dir %s failed: %s\n",
1032 full_fname(argv[0]), strerror(errno));
1033 exit_cleanup(RERR_FILESELECT);
1040 char fname2[MAXPATHLEN];
1041 char *fname = fname2;
1044 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1046 sanitize_path(fname, NULL);
1050 strlcpy(fname, *argv++, MAXPATHLEN);
1052 sanitize_path(fname, NULL);
1056 if (fname[l - 1] == '/') {
1057 if (l == 2 && fname[0] == '.') {
1058 /* Turn "./" into just "." rather than "./." */
1060 } else if (l < MAXPATHLEN) {
1066 if (link_stat(fname, &st) != 0) {
1068 io_error |= IOERR_GENERAL;
1069 rprintf(FERROR, "link_stat %s failed: %s\n",
1070 full_fname(fname), strerror(errno));
1075 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
1076 rprintf(FINFO, "skipping directory %s\n", fname);
1083 if (!relative_paths) {
1084 p = strrchr(fname, '/');
1093 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1094 /* this ensures we send the intermediate directories,
1095 thus getting their permissions right */
1096 char *lp = lastpath, *fn = fname, *slash = fname;
1098 /* Skip any initial directories in our path that we
1099 * have in common with lastpath. */
1100 while (*fn && *lp == *fn) {
1106 if (fn != p || (*lp && *lp != '/')) {
1107 int copy_links_saved = copy_links;
1108 int recurse_saved = recurse;
1109 copy_links = copy_unsafe_links;
1110 /* set recurse to 1 to prevent make_file
1111 * from ignoring directory, but still
1112 * turn off the recursive parameter to
1115 while ((slash = strchr(slash+1, '/')) != 0) {
1117 send_file_name(f, flist, fname, 0, 0);
1120 copy_links = copy_links_saved;
1121 recurse = recurse_saved;
1123 strlcpy(lastpath, fname, sizeof lastpath);
1132 static char *lastdir;
1133 static int lastdir_len;
1135 strcpy(olddir, curr_dir); /* can't overflow */
1137 if (!push_dir(dir)) {
1138 io_error |= IOERR_GENERAL;
1139 rprintf(FERROR, "push_dir %s failed: %s\n",
1140 full_fname(dir), strerror(errno));
1144 if (lastdir && strcmp(lastdir, dir) == 0) {
1145 flist_dir = lastdir;
1146 flist_dir_len = lastdir_len;
1148 flist_dir = lastdir = strdup(dir);
1149 flist_dir_len = lastdir_len = strlen(dir);
1153 if (one_file_system)
1154 set_filesystem(fname);
1156 send_file_name(f, flist, fname, recurse, XMIT_TOP_DIR);
1161 if (!pop_dir(olddir)) {
1162 rprintf(FERROR, "pop_dir %s failed: %s\n",
1163 full_fname(dir), strerror(errno));
1164 exit_cleanup(RERR_FILESELECT);
1170 send_file_entry(NULL, f, 0);
1172 if (show_filelist_p())
1173 finish_filelist_progress(flist);
1176 clean_flist(flist, 0, 0);
1179 /* Now send the uid/gid list. This was introduced in
1180 * protocol version 15 */
1183 /* send the io_error flag */
1184 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1187 stats.flist_size = stats.total_written - start_write;
1188 stats.num_files = flist->count;
1190 write_batch_flist_info(flist->count, flist->files);
1194 rprintf(FINFO, "send_file_list done\n");
1200 struct file_list *recv_file_list(int f)
1202 struct file_list *flist;
1203 unsigned short flags;
1205 extern int list_only;
1207 if (show_filelist_p())
1208 start_filelist_progress("receiving file list");
1210 start_read = stats.total_read;
1212 flist = new(struct file_list);
1217 flist->malloced = 1000;
1218 flist->files = new_array(struct file_struct *, flist->malloced);
1223 while ((flags = read_byte(f)) != 0) {
1224 int i = flist->count;
1226 flist_expand(flist);
1228 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1229 flags |= read_byte(f) << 8;
1230 receive_file_entry(&flist->files[i], flags, f);
1232 if (S_ISREG(flist->files[i]->mode))
1233 stats.total_size += flist->files[i]->length;
1237 maybe_emit_filelist_progress(flist);
1240 rprintf(FINFO, "recv_file_name(%s)\n",
1241 f_name(flist->files[i]));
1244 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1247 rprintf(FINFO, "received %d names\n", flist->count);
1249 if (show_filelist_p())
1250 finish_filelist_progress(flist);
1252 clean_flist(flist, relative_paths, 1);
1255 /* Now send the uid/gid list. This was introduced in
1256 * protocol version 15 */
1257 recv_uid_list(f, flist);
1260 /* Recv the io_error flag */
1261 if (lp_ignore_errors(module_id) || ignore_errors)
1264 io_error |= read_int(f);
1270 for (i = 0; i < flist->count; i++)
1271 list_file_entry(flist->files[i]);
1275 rprintf(FINFO, "recv_file_list done\n");
1277 stats.flist_size = stats.total_read - start_read;
1278 stats.num_files = flist->count;
1283 out_of_memory("recv_file_list");
1284 return NULL; /* not reached */
1288 int file_compare(struct file_struct **file1, struct file_struct **file2)
1290 struct file_struct *f1 = *file1;
1291 struct file_struct *f2 = *file2;
1293 if (!f1->basename && !f2->basename)
1299 if (f1->dirname == f2->dirname)
1300 return u_strcmp(f1->basename, f2->basename);
1301 return f_name_cmp(f1, f2);
1305 int flist_find(struct file_list *flist, struct file_struct *f)
1307 int low = 0, high = flist->count - 1;
1309 while (high >= 0 && !flist->files[high]->basename) high--;
1314 while (low != high) {
1315 int mid = (low + high) / 2;
1316 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1318 return flist_up(flist, mid);
1325 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1326 return flist_up(flist, low);
1332 * Free up any resources a file_struct has allocated, and optionally free
1335 void free_file(struct file_struct *file, int free_the_struct)
1337 if (free_the_struct)
1340 memset(file, 0, min_file_struct_len);
1345 * allocate a new file list
1347 struct file_list *flist_new(void)
1349 struct file_list *flist;
1351 flist = new(struct file_list);
1353 out_of_memory("send_file_list");
1356 flist->malloced = 0;
1357 flist->files = NULL;
1363 * free up all elements in a flist
1365 void flist_free(struct file_list *flist)
1368 for (i = 1; i < flist->count; i++)
1369 free_file(flist->files[i], FREE_STRUCT);
1376 * This routine ensures we don't have any duplicate names in our file list.
1377 * duplicate names can cause corruption because of the pipelining
1379 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1383 if (!flist || flist->count == 0)
1386 qsort(flist->files, flist->count,
1387 sizeof flist->files[0], (int (*)()) file_compare);
1389 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1390 if (flist->files[i]->basename) {
1395 while (++i < flist->count) {
1396 if (!flist->files[i]->basename)
1398 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1399 if (verbose > 1 && !am_server) {
1401 "removing duplicate name %s from file list %d\n",
1402 f_name(flist->files[i]), i);
1404 /* Make sure that if we unduplicate '.', that we don't
1405 * lose track of a user-specified starting point (or
1406 * else deletions will mysteriously fail with -R). */
1407 if (flist->files[i]->flags & FLAG_TOP_DIR)
1408 flist->files[prev_i]->flags |= FLAG_TOP_DIR;
1409 free_file(flist->files[i], CLEAR_STRUCT);
1415 /* we need to strip off the root directory in the case
1416 of relative paths, but this must be done _after_
1417 the sorting phase */
1418 for (i = 0; i < flist->count; i++) {
1419 if (flist->files[i]->dirname &&
1420 flist->files[i]->dirname[0] == '/') {
1421 memmove(&flist->files[i]->dirname[0],
1422 &flist->files[i]->dirname[1],
1423 strlen(flist->files[i]->dirname));
1426 if (flist->files[i]->dirname &&
1427 !flist->files[i]->dirname[0]) {
1428 flist->files[i]->dirname = NULL;
1436 for (i = 0; i < flist->count; i++) {
1437 char uidbuf[16], gidbuf[16];
1438 struct file_struct *file = flist->files[i];
1439 if (am_root && preserve_uid)
1440 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1444 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1447 rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f%s%s\n",
1448 who_am_i(), i, NS(file->basedir), NS(file->dirname),
1449 NS(file->basename), (int) file->mode,
1450 (double) file->length, uidbuf, gidbuf);
1455 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1457 /* Compare the names of two file_struct entities, just like strcmp()
1458 * would do if it were operating on the joined strings. We assume
1459 * that there are no 0-length strings.
1461 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1464 const uchar *c1, *c2;
1465 enum fnc_state state1, state2;
1467 if (!f1 || !f1->basename) {
1468 if (!f2 || !f2->basename)
1472 if (!f2 || !f2->basename)
1475 if (!(c1 = (uchar*)f1->dirname)) {
1477 c1 = (uchar*)f1->basename;
1480 if (!(c2 = (uchar*)f2->dirname)) {
1482 c2 = (uchar*)f2->basename;
1487 if ((dif = (int)*c1 - (int)*c2) != 0)
1497 c1 = (uchar*)f1->basename;
1511 c2 = (uchar*)f2->basename;
1525 /* Return a copy of the full filename of a flist entry, using the indicated
1526 * buffer. No size-checking is done because we checked the size when creating
1527 * the file_struct entry.
1529 char *f_name_to(struct file_struct *f, char *fbuf)
1531 if (!f || !f->basename)
1535 int len = strlen(f->dirname);
1536 memcpy(fbuf, f->dirname, len);
1538 strcpy(fbuf + len + 1, f->basename);
1540 strcpy(fbuf, f->basename);
1545 /* Like f_name_to(), but we rotate through 5 static buffers of our own.
1547 char *f_name(struct file_struct *f)
1549 static char names[5][MAXPATHLEN];
1550 static unsigned int n;
1552 n = (n + 1) % (sizeof names / sizeof names[0]);
1554 return f_name_to(f, names[n]);