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;
37 extern int always_checksum;
39 extern int ignore_errors;
40 extern int numeric_ids;
42 extern int cvs_exclude;
45 extern char curr_dir[MAXPATHLEN];
46 extern char *files_from;
47 extern int filesfrom_fd;
49 extern int one_file_system;
50 extern int preserve_links;
51 extern int preserve_hard_links;
52 extern int preserve_perms;
53 extern int preserve_devices;
54 extern int preserve_uid;
55 extern int preserve_gid;
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_list_struct exclude_list;
67 extern struct exclude_list_struct server_exclude_list;
68 extern struct exclude_list_struct local_exclude_list;
72 static char empty_sum[MD4_SUM_LENGTH];
73 static unsigned int file_struct_len;
75 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
76 static void output_flist(struct file_list *flist);
82 /* Figure out how big the file_struct is without trailing padding */
83 file_struct_len = offsetof(struct file_struct, flags) + 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 */
139 permstring(perms, f->mode);
142 if (preserve_links && S_ISLNK(f->mode)) {
143 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
145 (double)f->length, timestring(f->modtime),
146 f_name(f), f->u.link);
150 rprintf(FINFO, "%s %11.0f %s %s\n",
152 (double)f->length, timestring(f->modtime),
159 * Stat either a symlink or its referent, depending on the settings of
160 * copy_links, copy_unsafe_links, etc.
162 * @retval -1 on error
164 * @retval 0 for success
166 * @post If @p path is a symlink, then @p linkbuf (of size @c
167 * MAXPATHLEN) contains the symlink target.
169 * @post @p buffer contains information about the link or the
170 * referrent as appropriate, if they exist.
172 int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
176 return do_stat(path, buffer);
177 if (do_lstat(path, buffer) == -1)
179 if (S_ISLNK(buffer->st_mode)) {
180 int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1);
184 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
186 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
189 return do_stat(path, buffer);
194 return do_stat(path, buffer);
198 int link_stat(const char *path, STRUCT_STAT *buffer)
202 return do_stat(path, buffer);
203 return do_lstat(path, buffer);
205 return do_stat(path, buffer);
210 * This function is used to check if a file should be included/excluded
211 * from the list of files based on its name and type etc. The value of
212 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
214 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
218 #if 0 /* This currently never happens, so avoid a useless compare. */
219 if (exclude_level == NO_EXCLUDES)
223 /* never exclude '.', even if somebody does --exclude '*' */
224 if (fname[0] == '.' && !fname[1])
226 /* Handle the -R version of the '.' dir. */
227 if (fname[0] == '/') {
228 int len = strlen(fname);
229 if (fname[len-1] == '.' && fname[len-2] == '/')
233 if (server_exclude_list.head
234 && check_exclude(&server_exclude_list, fname, is_dir) < 0)
236 if (exclude_level != ALL_EXCLUDES)
238 if (exclude_list.head
239 && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
241 if (local_exclude_list.head
242 && check_exclude(&local_exclude_list, fname, is_dir) < 0)
247 /* used by the one_file_system code */
248 static dev_t filesystem_dev;
250 static void set_filesystem(char *fname)
253 if (do_stat(fname, &st) != 0)
255 filesystem_dev = st.st_dev;
259 static int to_wire_mode(mode_t mode)
262 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
263 return (mode & ~(_S_IFMT)) | 0120000;
268 static mode_t from_wire_mode(int mode)
270 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
271 return (mode & ~(_S_IFMT)) | _S_IFLNK;
276 static void send_directory(int f, struct file_list *flist, char *dir);
278 static char *flist_dir;
279 static int flist_dir_len;
283 * Make sure @p flist is big enough to hold at least @p flist->count
286 void flist_expand(struct file_list *flist)
288 struct file_struct **new_ptr;
290 if (flist->count < flist->malloced)
293 if (flist->malloced < FLIST_START)
294 flist->malloced = FLIST_START;
295 else if (flist->malloced >= FLIST_LINEAR)
296 flist->malloced += FLIST_LINEAR;
298 flist->malloced *= 2;
301 * In case count jumped or we are starting the list
302 * with a known size just set it.
304 if (flist->malloced < flist->count)
305 flist->malloced = flist->count;
307 new_ptr = realloc_array(flist->files, struct file_struct *,
311 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
313 (double)sizeof flist->files[0] * flist->malloced,
314 (new_ptr == flist->files) ? " not" : "");
317 flist->files = new_ptr;
320 out_of_memory("flist_expand");
323 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
325 unsigned short flags;
326 static time_t modtime;
330 static uint32 rdev_major;
333 static char lastname[MAXPATHLEN];
334 char fname[MAXPATHLEN];
342 modtime = 0, mode = 0;
343 dev = 0, rdev = makedev(0, 0);
350 io_write_phase = "send_file_entry";
352 f_name_to(file, fname);
356 if (file->mode == mode)
357 flags |= XMIT_SAME_MODE;
360 if (preserve_devices) {
361 if (protocol_version < 28) {
362 if (IS_DEVICE(mode)) {
363 if (file->u.rdev == rdev)
364 flags |= XMIT_SAME_RDEV_pre28;
368 rdev = makedev(0, 0);
369 } else if (IS_DEVICE(mode)) {
371 if ((uint32)major(rdev) == rdev_major)
372 flags |= XMIT_SAME_RDEV_MAJOR;
374 rdev_major = major(rdev);
375 if ((uint32)minor(rdev) <= 0xFFu)
376 flags |= XMIT_RDEV_MINOR_IS_SMALL;
379 if (file->uid == uid)
380 flags |= XMIT_SAME_UID;
383 if (file->gid == gid)
384 flags |= XMIT_SAME_GID;
387 if (file->modtime == modtime)
388 flags |= XMIT_SAME_TIME;
390 modtime = file->modtime;
392 #if SUPPORT_HARD_LINKS
393 if (file->link_u.idev) {
394 if (file->F_DEV == dev) {
395 if (protocol_version >= 28)
396 flags |= XMIT_SAME_DEV;
399 flags |= XMIT_HAS_IDEV_DATA;
404 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
406 l2 = strlen(fname+l1);
409 flags |= XMIT_SAME_NAME;
411 flags |= XMIT_LONG_NAME;
413 /* We must make sure we don't send a zero flag byte or the
414 * other end will terminate the flist transfer. Note that
415 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
416 * it's harmless way to add a bit to the first flag byte. */
417 if (protocol_version >= 28) {
418 if (!flags && !S_ISDIR(mode))
419 flags |= XMIT_TOP_DIR;
420 if ((flags & 0xFF00) || !flags) {
421 flags |= XMIT_EXTENDED_FLAGS;
422 write_byte(f, flags);
423 write_byte(f, flags >> 8);
425 write_byte(f, flags);
427 if (!(flags & 0xFF) && !S_ISDIR(mode))
428 flags |= XMIT_TOP_DIR;
430 flags |= XMIT_LONG_NAME;
431 write_byte(f, flags);
433 if (flags & XMIT_SAME_NAME)
435 if (flags & XMIT_LONG_NAME)
439 write_buf(f, fname + l1, l2);
441 write_longint(f, file->length);
442 if (!(flags & XMIT_SAME_TIME))
443 write_int(f, modtime);
444 if (!(flags & XMIT_SAME_MODE))
445 write_int(f, to_wire_mode(mode));
446 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
451 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
456 if (preserve_devices && IS_DEVICE(mode)) {
457 if (protocol_version < 28) {
458 if (!(flags & XMIT_SAME_RDEV_pre28))
459 write_int(f, (int)rdev);
461 if (!(flags & XMIT_SAME_RDEV_MAJOR))
462 write_int(f, major(rdev));
463 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
464 write_byte(f, minor(rdev));
466 write_int(f, minor(rdev));
471 if (preserve_links && S_ISLNK(mode)) {
472 int len = strlen(file->u.link);
474 write_buf(f, file->u.link, len);
478 #if SUPPORT_HARD_LINKS
479 if (flags & XMIT_HAS_IDEV_DATA) {
480 if (protocol_version < 26) {
481 /* 32-bit dev_t and ino_t */
483 write_int(f, file->F_INODE);
485 /* 64-bit dev_t and ino_t */
486 if (!(flags & XMIT_SAME_DEV))
487 write_longint(f, dev);
488 write_longint(f, file->F_INODE);
493 if (always_checksum) {
497 else if (protocol_version < 28) {
498 /* Prior to 28, we sent a useless set of nulls. */
504 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
508 strlcpy(lastname, fname, MAXPATHLEN);
510 io_write_phase = "unknown";
515 void receive_file_entry(struct file_struct **fptr, unsigned short flags,
516 struct file_list *flist, int f)
518 static time_t modtime;
522 static uint32 rdev_major;
525 static char lastname[MAXPATHLEN], *lastdir;
526 static int lastdir_len = -1;
527 char thisname[MAXPATHLEN];
528 unsigned int l1 = 0, l2 = 0;
529 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
531 char *basename, *dirname, *bp;
532 struct file_struct *file;
535 modtime = 0, mode = 0;
536 dev = 0, rdev = makedev(0, 0);
544 if (flags & XMIT_SAME_NAME)
547 if (flags & XMIT_LONG_NAME)
552 if (l2 >= MAXPATHLEN - l1) {
554 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
555 flags, l1, l2, lastname);
556 overflow("receive_file_entry");
559 strlcpy(thisname, lastname, l1 + 1);
560 read_sbuf(f, &thisname[l1], l2);
561 thisname[l1 + l2] = 0;
563 strlcpy(lastname, thisname, MAXPATHLEN);
565 clean_fname(thisname);
568 sanitize_path(thisname, NULL);
570 if ((basename = strrchr(thisname, '/')) != NULL) {
571 dirname_len = ++basename - thisname; /* counts future '\0' */
572 if (lastdir_len == dirname_len - 1
573 && strncmp(thisname, lastdir, lastdir_len) == 0) {
575 dirname_len = 0; /* indicates no copy is needed */
583 basename_len = strlen(basename) + 1; /* count the '\0' */
585 file_length = read_longint(f);
586 if (!(flags & XMIT_SAME_TIME))
587 modtime = (time_t)read_int(f);
588 if (!(flags & XMIT_SAME_MODE))
589 mode = from_wire_mode(read_int(f));
591 if (preserve_uid && !(flags & XMIT_SAME_UID))
592 uid = (uid_t)read_int(f);
593 if (preserve_gid && !(flags & XMIT_SAME_GID))
594 gid = (gid_t)read_int(f);
596 if (preserve_devices) {
597 if (protocol_version < 28) {
598 if (IS_DEVICE(mode)) {
599 if (!(flags & XMIT_SAME_RDEV_pre28))
600 rdev = (dev_t)read_int(f);
602 rdev = makedev(0, 0);
603 } else if (IS_DEVICE(mode)) {
605 if (!(flags & XMIT_SAME_RDEV_MAJOR))
606 rdev_major = read_int(f);
607 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
608 rdev_minor = read_byte(f);
610 rdev_minor = read_int(f);
611 rdev = makedev(rdev_major, rdev_minor);
616 if (preserve_links && S_ISLNK(mode)) {
617 linkname_len = read_int(f) + 1; /* count the '\0' */
618 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
619 rprintf(FERROR, "overflow: linkname_len=%d\n",
621 overflow("receive_file_entry");
628 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
630 alloc_len = file_struct_len + dirname_len + basename_len
631 + linkname_len + sum_len;
632 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
634 file = *fptr = (struct file_struct *)bp;
635 memset(bp, 0, file_struct_len);
636 bp += file_struct_len;
638 file->flags = flags & XMIT_TOP_DIR ? FLAG_TOP_DIR : 0;
639 file->modtime = modtime;
640 file->length = file_length;
646 file->dirname = lastdir = bp;
647 lastdir_len = dirname_len - 1;
648 memcpy(bp, dirname, dirname_len - 1);
652 file->dirname = dirname;
655 memcpy(bp, basename, basename_len);
658 if (preserve_devices && IS_DEVICE(mode))
664 read_sbuf(f, bp, linkname_len - 1);
666 sanitize_path(bp, lastdir);
671 #if SUPPORT_HARD_LINKS
672 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
673 flags |= XMIT_HAS_IDEV_DATA;
674 if (flags & XMIT_HAS_IDEV_DATA) {
676 if (protocol_version < 26) {
680 if (!(flags & XMIT_SAME_DEV))
681 dev = read_longint(f);
682 inode = read_longint(f);
684 if (flist->hlink_pool) {
685 file->link_u.idev = pool_talloc(flist->hlink_pool,
686 struct idev, 1, "inode_table");
687 file->F_INODE = inode;
693 if (always_checksum) {
696 file->u.sum = sum = bp;
698 } else if (protocol_version < 28) {
699 /* Prior to 28, we get a useless set of nulls. */
705 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
709 if (!preserve_perms) {
710 extern int orig_umask;
711 /* set an appropriate set of permissions based on original
712 * permissions and umask. This emulates what GNU cp does */
713 file->mode &= ~orig_umask;
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,
734 struct file_list *flist, int exclude_level)
736 static char *lastdir;
737 static int lastdir_len = -1;
738 struct file_struct *file;
740 char sum[SUM_LENGTH];
741 char thisname[MAXPATHLEN];
742 char linkname[MAXPATHLEN];
743 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
744 char *basename, *dirname, *bp;
745 unsigned short flags = 0;
747 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
750 if (strlcpy(thisname, fname, sizeof thisname)
751 >= sizeof thisname - flist_dir_len) {
752 rprintf(FINFO, "skipping overly long name: %s\n", fname);
755 clean_fname(thisname);
757 sanitize_path(thisname, NULL);
759 memset(sum, 0, SUM_LENGTH);
761 if (readlink_stat(thisname, &st, linkname) != 0) {
762 int save_errno = errno;
763 /* See if file is excluded before reporting an error. */
764 if (exclude_level != NO_EXCLUDES
765 && check_exclude_file(thisname, 0, exclude_level))
767 if (save_errno == ENOENT) {
769 /* Avoid "vanished" error if symlink points nowhere. */
770 if (copy_links && do_lstat(thisname, &st) == 0
771 && S_ISLNK(st.st_mode)) {
772 io_error |= IOERR_GENERAL;
773 rprintf(FERROR, "symlink has no referent: %s\n",
774 full_fname(thisname));
778 enum logcode c = am_daemon && protocol_version < 28
780 io_error |= IOERR_VANISHED;
781 rprintf(c, "file has vanished: %s\n",
782 full_fname(thisname));
785 io_error |= IOERR_GENERAL;
786 rsyserr(FERROR, save_errno, "readlink %s failed",
787 full_fname(thisname));
792 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
793 if (exclude_level == NO_EXCLUDES)
796 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
797 rprintf(FINFO, "skipping directory %s\n", thisname);
801 /* We only care about directories because we need to avoid recursing
802 * into a mount-point directory, not to avoid copying a symlinked
803 * file if -L (or similar) was specified. */
804 if (one_file_system && st.st_dev != filesystem_dev
805 && S_ISDIR(st.st_mode))
806 flags |= FLAG_MOUNT_POINT;
808 if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level))
811 if (lp_ignore_nonreadable(module_id) && access(thisname, R_OK) != 0)
817 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
818 who_am_i(), thisname, exclude_level);
821 if ((basename = strrchr(thisname, '/')) != NULL) {
822 dirname_len = ++basename - thisname; /* counts future '\0' */
823 if (lastdir_len == dirname_len - 1
824 && strncmp(thisname, lastdir, lastdir_len) == 0) {
826 dirname_len = 0; /* indicates no copy is needed */
834 basename_len = strlen(basename) + 1; /* count the '\0' */
837 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
842 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
844 alloc_len = file_struct_len + dirname_len + basename_len
845 + linkname_len + sum_len;
847 bp = pool_alloc(flist->file_pool, alloc_len,
848 "receive_file_entry");
850 if (!(bp = new_array(char, alloc_len)))
851 out_of_memory("receive_file_entry");
854 file = (struct file_struct *)bp;
855 memset(bp, 0, file_struct_len);
856 bp += file_struct_len;
859 file->modtime = st.st_mtime;
860 file->length = st.st_size;
861 file->mode = st.st_mode;
862 file->uid = st.st_uid;
863 file->gid = st.st_gid;
865 #if SUPPORT_HARD_LINKS
866 if (flist && flist->hlink_pool) {
867 if (protocol_version < 28) {
868 if (S_ISREG(st.st_mode))
869 file->link_u.idev = pool_talloc(
870 flist->hlink_pool, struct idev, 1,
873 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
874 file->link_u.idev = pool_talloc(
875 flist->hlink_pool, struct idev, 1,
879 if (file->link_u.idev) {
880 file->F_DEV = st.st_dev;
881 file->F_INODE = st.st_ino;
886 file->dirname = lastdir = bp;
887 lastdir_len = dirname_len - 1;
888 memcpy(bp, dirname, dirname_len - 1);
892 file->dirname = dirname;
895 memcpy(bp, basename, basename_len);
898 #ifdef HAVE_STRUCT_STAT_ST_RDEV
899 if (preserve_devices && IS_DEVICE(st.st_mode))
900 file->u.rdev = st.st_rdev;
906 memcpy(bp, linkname, linkname_len);
913 file_checksum(thisname, bp, st.st_size);
917 file->basedir = flist_dir;
919 if (!S_ISDIR(st.st_mode))
920 stats.total_size += st.st_size;
926 void send_file_name(int f, struct file_list *flist, char *fname,
927 int recursive, unsigned short base_flags)
929 struct file_struct *file;
930 char fbuf[MAXPATHLEN];
931 extern int delete_excluded;
933 /* f is set to -1 when calculating deletion file list */
934 file = make_file(fname, flist,
935 f == -1 && delete_excluded? SERVER_EXCLUDES : ALL_EXCLUDES);
940 maybe_emit_filelist_progress(flist);
945 file->flags |= FLAG_TOP_DIR;
947 if (file->basename[0]) {
948 flist->files[flist->count++] = file;
949 send_file_entry(file, f, base_flags);
952 if (recursive && S_ISDIR(file->mode)
953 && !(file->flags & FLAG_MOUNT_POINT)) {
954 struct exclude_list_struct last_list = local_exclude_list;
955 local_exclude_list.head = local_exclude_list.tail = NULL;
956 send_directory(f, flist, f_name_to(file, fbuf));
958 rprintf(FINFO, "[%s] popping %sexclude list\n",
959 who_am_i(), local_exclude_list.debug_type);
961 clear_exclude_list(&local_exclude_list);
962 local_exclude_list = last_list;
967 static void send_directory(int f, struct file_list *flist, char *dir)
971 char fname[MAXPATHLEN];
977 io_error |= IOERR_GENERAL;
978 rsyserr(FERROR, errno, "opendir %s failed", full_fname(dir));
982 offset = strlcpy(fname, dir, MAXPATHLEN);
984 if (offset >= MAXPATHLEN || p[-1] != '/') {
985 if (offset >= MAXPATHLEN - 1) {
986 io_error |= IOERR_GENERAL;
987 rprintf(FERROR, "skipping long-named directory: %s\n",
997 if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
998 < MAXPATHLEN - offset) {
999 add_exclude_file(&local_exclude_list, fname,
1000 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
1002 io_error |= IOERR_GENERAL;
1004 "cannot cvs-exclude in long-named directory %s\n",
1009 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1010 char *dname = d_name(di);
1011 if (dname[0] == '.' && (dname[1] == '\0'
1012 || (dname[1] == '.' && dname[2] == '\0')))
1014 if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset)
1015 send_file_name(f, flist, fname, recurse, 0);
1017 io_error |= IOERR_GENERAL;
1019 "cannot send long-named file %s\n",
1024 io_error |= IOERR_GENERAL;
1025 rsyserr(FERROR, errno, "readdir(%s)", dir);
1033 * The delete_files() function in receiver.c sets f to -1 so that we just
1034 * construct the file list in memory without sending it over the wire. It
1035 * also has the side-effect of ignoring user-excludes if delete_excluded
1036 * is set (so that the delete list includes user-excluded files).
1038 struct file_list *send_file_list(int f, int argc, char *argv[])
1042 char *p, *dir, olddir[sizeof curr_dir];
1043 char lastpath[MAXPATHLEN] = "";
1044 struct file_list *flist;
1048 if (show_filelist_p() && f != -1)
1049 start_filelist_progress("building file list");
1051 start_write = stats.total_written;
1053 flist = flist_new(f == -1 ? WITHOUT_HLINK : WITH_HLINK,
1057 io_start_buffering_out(f);
1058 if (filesfrom_fd >= 0) {
1059 if (argv[0] && !push_dir(argv[0])) {
1060 rsyserr(FERROR, errno, "push_dir %s failed",
1061 full_fname(argv[0]));
1062 exit_cleanup(RERR_FILESELECT);
1069 char fname2[MAXPATHLEN];
1070 char *fname = fname2;
1073 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1075 sanitize_path(fname, NULL);
1079 strlcpy(fname, *argv++, MAXPATHLEN);
1081 sanitize_path(fname, NULL);
1085 if (fname[l - 1] == '/') {
1086 if (l == 2 && fname[0] == '.') {
1087 /* Turn "./" into just "." rather than "./." */
1089 } else if (l < MAXPATHLEN) {
1095 if (link_stat(fname, &st) != 0) {
1097 io_error |= IOERR_GENERAL;
1098 rsyserr(FERROR, errno, "link_stat %s failed",
1104 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
1105 rprintf(FINFO, "skipping directory %s\n", fname);
1112 if (!relative_paths) {
1113 p = strrchr(fname, '/');
1122 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1123 /* this ensures we send the intermediate directories,
1124 thus getting their permissions right */
1125 char *lp = lastpath, *fn = fname, *slash = fname;
1127 /* Skip any initial directories in our path that we
1128 * have in common with lastpath. */
1129 while (*fn && *lp == *fn) {
1135 if (fn != p || (*lp && *lp != '/')) {
1136 int copy_links_saved = copy_links;
1137 int recurse_saved = recurse;
1138 copy_links = copy_unsafe_links;
1139 /* set recurse to 1 to prevent make_file
1140 * from ignoring directory, but still
1141 * turn off the recursive parameter to
1144 while ((slash = strchr(slash+1, '/')) != 0) {
1146 send_file_name(f, flist, fname, 0, 0);
1149 copy_links = copy_links_saved;
1150 recurse = recurse_saved;
1152 strlcpy(lastpath, fname, sizeof lastpath);
1161 static char *lastdir;
1162 static int lastdir_len;
1164 strcpy(olddir, curr_dir); /* can't overflow */
1166 if (!push_dir(dir)) {
1167 io_error |= IOERR_GENERAL;
1168 rsyserr(FERROR, errno, "push_dir %s failed",
1173 if (lastdir && strcmp(lastdir, dir) == 0) {
1174 flist_dir = lastdir;
1175 flist_dir_len = lastdir_len;
1177 flist_dir = lastdir = strdup(dir);
1178 flist_dir_len = lastdir_len = strlen(dir);
1182 if (one_file_system)
1183 set_filesystem(fname);
1185 send_file_name(f, flist, fname, recurse, XMIT_TOP_DIR);
1190 if (!pop_dir(olddir)) {
1191 rsyserr(FERROR, errno, "pop_dir %s failed",
1193 exit_cleanup(RERR_FILESELECT);
1199 send_file_entry(NULL, f, 0);
1201 if (show_filelist_p())
1202 finish_filelist_progress(flist);
1205 if (flist->hlink_pool) {
1206 pool_destroy(flist->hlink_pool);
1207 flist->hlink_pool = NULL;
1210 clean_flist(flist, 0, 0);
1213 /* Now send the uid/gid list. This was introduced in
1214 * protocol version 15 */
1217 /* send the io_error flag */
1218 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1221 stats.flist_size = stats.total_written - start_write;
1222 stats.num_files = flist->count;
1224 write_batch_flist_info(flist->count, flist->files);
1228 output_flist(flist);
1231 rprintf(FINFO, "send_file_list done\n");
1237 struct file_list *recv_file_list(int f)
1239 struct file_list *flist;
1240 unsigned short flags;
1242 extern int list_only;
1244 if (show_filelist_p())
1245 start_filelist_progress("receiving file list");
1247 start_read = stats.total_read;
1249 flist = flist_new(WITH_HLINK, "recv_file_list");
1252 flist->malloced = 1000;
1253 flist->files = new_array(struct file_struct *, flist->malloced);
1258 while ((flags = read_byte(f)) != 0) {
1259 int i = flist->count;
1261 flist_expand(flist);
1263 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1264 flags |= read_byte(f) << 8;
1265 receive_file_entry(&flist->files[i], flags, flist, f);
1267 if (S_ISREG(flist->files[i]->mode))
1268 stats.total_size += flist->files[i]->length;
1272 maybe_emit_filelist_progress(flist);
1275 rprintf(FINFO, "recv_file_name(%s)\n",
1276 f_name(flist->files[i]));
1279 receive_file_entry(NULL, 0, NULL, 0); /* Signal that we're done. */
1282 rprintf(FINFO, "received %d names\n", flist->count);
1284 if (show_filelist_p())
1285 finish_filelist_progress(flist);
1287 clean_flist(flist, relative_paths, 1);
1290 /* Now send the uid/gid list. This was introduced in
1291 * protocol version 15 */
1292 recv_uid_list(f, flist);
1295 /* Recv the io_error flag */
1296 if (lp_ignore_errors(module_id) || ignore_errors)
1299 io_error |= read_int(f);
1304 output_flist(flist);
1308 for (i = 0; i < flist->count; i++)
1309 list_file_entry(flist->files[i]);
1313 rprintf(FINFO, "recv_file_list done\n");
1315 stats.flist_size = stats.total_read - start_read;
1316 stats.num_files = flist->count;
1321 out_of_memory("recv_file_list");
1322 return NULL; /* not reached */
1326 int file_compare(struct file_struct **file1, struct file_struct **file2)
1328 struct file_struct *f1 = *file1;
1329 struct file_struct *f2 = *file2;
1331 if (!f1->basename && !f2->basename)
1337 if (f1->dirname == f2->dirname)
1338 return u_strcmp(f1->basename, f2->basename);
1339 return f_name_cmp(f1, f2);
1343 int flist_find(struct file_list *flist, struct file_struct *f)
1345 int low = 0, high = flist->count - 1;
1347 while (high >= 0 && !flist->files[high]->basename) high--;
1352 while (low != high) {
1353 int mid = (low + high) / 2;
1354 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1356 return flist_up(flist, mid);
1363 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1364 return flist_up(flist, low);
1369 * Free up any resources a file_struct has allocated
1370 * and clear the file.
1372 void clear_file(int i, struct file_list *flist)
1374 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1375 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1376 memset(flist->files[i], 0, file_struct_len);
1381 * allocate a new file list
1383 struct file_list *flist_new(int with_hlink, char *msg)
1385 struct file_list *flist;
1387 flist = new(struct file_list);
1391 memset(flist, 0, sizeof (struct file_list));
1393 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1394 out_of_memory, POOL_INTERN)))
1397 #if SUPPORT_HARD_LINKS
1398 if (with_hlink && preserve_hard_links) {
1399 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1400 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1409 * free up all elements in a flist
1411 void flist_free(struct file_list *flist)
1413 pool_destroy(flist->file_pool);
1414 pool_destroy(flist->hlink_pool);
1421 * This routine ensures we don't have any duplicate names in our file list.
1422 * duplicate names can cause corruption because of the pipelining
1424 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1428 if (!flist || flist->count == 0)
1431 qsort(flist->files, flist->count,
1432 sizeof flist->files[0], (int (*)())file_compare);
1434 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1435 if (flist->files[i]->basename) {
1440 while (++i < flist->count) {
1441 if (!flist->files[i]->basename)
1443 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1444 if (verbose > 1 && !am_server) {
1446 "removing duplicate name %s from file list %d\n",
1447 f_name(flist->files[i]), i);
1449 /* Make sure that if we unduplicate '.', that we don't
1450 * lose track of a user-specified starting point (or
1451 * else deletions will mysteriously fail with -R). */
1452 if (flist->files[i]->flags & FLAG_TOP_DIR)
1453 flist->files[prev_i]->flags |= FLAG_TOP_DIR;
1455 clear_file(i, flist);
1461 /* we need to strip off the root directory in the case
1462 of relative paths, but this must be done _after_
1463 the sorting phase */
1464 for (i = 0; i < flist->count; i++) {
1465 if (flist->files[i]->dirname &&
1466 flist->files[i]->dirname[0] == '/') {
1467 memmove(&flist->files[i]->dirname[0],
1468 &flist->files[i]->dirname[1],
1469 strlen(flist->files[i]->dirname));
1472 if (flist->files[i]->dirname &&
1473 !flist->files[i]->dirname[0]) {
1474 flist->files[i]->dirname = NULL;
1480 static void output_flist(struct file_list *flist)
1482 char uidbuf[16], gidbuf[16];
1483 struct file_struct *file;
1486 for (i = 0; i < flist->count; i++) {
1487 file = flist->files[i];
1488 if (am_root && preserve_uid)
1489 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1492 if (preserve_gid && file->gid != GID_NONE)
1493 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1496 rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f%s%s\n",
1497 who_am_i(), i, NS(file->basedir), NS(file->dirname),
1498 NS(file->basename), (int)file->mode,
1499 (double)file->length, uidbuf, gidbuf);
1504 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1506 /* Compare the names of two file_struct entities, just like strcmp()
1507 * would do if it were operating on the joined strings. We assume
1508 * that there are no 0-length strings.
1510 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1513 const uchar *c1, *c2;
1514 enum fnc_state state1, state2;
1516 if (!f1 || !f1->basename) {
1517 if (!f2 || !f2->basename)
1521 if (!f2 || !f2->basename)
1524 if (!(c1 = (uchar*)f1->dirname)) {
1526 c1 = (uchar*)f1->basename;
1532 if (!(c2 = (uchar*)f2->dirname)) {
1534 c2 = (uchar*)f2->basename;
1542 if ((dif = (int)*c1 - (int)*c2) != 0)
1552 c1 = (uchar*)f1->basename;
1566 c2 = (uchar*)f2->basename;
1580 /* Return a copy of the full filename of a flist entry, using the indicated
1581 * buffer. No size-checking is done because we checked the size when creating
1582 * the file_struct entry.
1584 char *f_name_to(struct file_struct *f, char *fbuf)
1586 if (!f || !f->basename)
1590 int len = strlen(f->dirname);
1591 memcpy(fbuf, f->dirname, len);
1593 strcpy(fbuf + len + 1, f->basename);
1595 strcpy(fbuf, f->basename);
1600 /* Like f_name_to(), but we rotate through 5 static buffers of our own.
1602 char *f_name(struct file_struct *f)
1604 static char names[5][MAXPATHLEN];
1605 static unsigned int n;
1607 n = (n + 1) % (sizeof names / sizeof names[0]);
1609 return f_name_to(f, names[n]);