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;
38 extern int always_checksum;
40 extern int ignore_errors;
41 extern int numeric_ids;
43 extern int cvs_exclude;
47 extern char curr_dir[MAXPATHLEN];
48 extern int filesfrom_fd;
50 extern int one_file_system;
51 extern int keep_dirlinks;
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 relative_paths;
59 extern int implied_dirs;
60 extern int copy_links;
61 extern int copy_unsafe_links;
62 extern int protocol_version;
63 extern int sanitize_paths;
64 extern int delete_excluded;
65 extern int orig_umask;
68 extern struct exclude_list_struct exclude_list;
69 extern struct exclude_list_struct server_exclude_list;
70 extern struct exclude_list_struct local_exclude_list;
74 static char empty_sum[MD4_SUM_LENGTH];
75 static unsigned int file_struct_len;
76 static struct file_list *received_flist;
78 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
79 static void output_flist(struct file_list *flist, const char *whose_list);
85 /* Figure out how big the file_struct is without trailing padding */
86 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
90 static int show_filelist_p(void)
92 return verbose && keep_dirs && !am_server;
95 static void start_filelist_progress(char *kind)
97 rprintf(FINFO, "%s ... ", kind);
98 if (verbose > 1 || do_progress)
104 static void emit_filelist_progress(const struct file_list *flist)
106 rprintf(FINFO, " %d files...\r", flist->count);
110 static void maybe_emit_filelist_progress(const struct file_list *flist)
112 if (do_progress && show_filelist_p() && (flist->count % 100) == 0)
113 emit_filelist_progress(flist);
117 static void finish_filelist_progress(const struct file_list *flist)
120 /* This overwrites the progress line */
121 rprintf(FINFO, "%d file%sto consider\n",
122 flist->count, flist->count == 1 ? " " : "s ");
124 rprintf(FINFO, "done\n");
127 void show_flist_stats(void)
133 static void list_file_entry(struct file_struct *f)
138 /* this can happen if duplicate names were removed */
142 permstring(perms, f->mode);
145 if (preserve_links && S_ISLNK(f->mode)) {
146 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
148 (double)f->length, timestring(f->modtime),
149 f_name(f), f->u.link);
153 rprintf(FINFO, "%s %11.0f %s %s\n",
155 (double)f->length, timestring(f->modtime),
162 * Stat either a symlink or its referent, depending on the settings of
163 * copy_links, copy_unsafe_links, etc.
165 * @retval -1 on error
167 * @retval 0 for success
169 * @post If @p path is a symlink, then @p linkbuf (of size @c
170 * MAXPATHLEN) contains the symlink target.
172 * @post @p buffer contains information about the link or the
173 * referrent as appropriate, if they exist.
175 static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
179 return do_stat(path, buffer);
180 if (link_stat(path, buffer, 0) < 0)
182 if (S_ISLNK(buffer->st_mode)) {
183 int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1);
187 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
189 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
192 return do_stat(path, buffer);
197 return do_stat(path, buffer);
201 int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks)
205 return do_stat(path, buffer);
206 if (do_lstat(path, buffer) < 0)
208 if (follow_dirlinks && S_ISLNK(buffer->st_mode)) {
210 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
215 return do_stat(path, buffer);
220 * This function is used to check if a file should be included/excluded
221 * from the list of files based on its name and type etc. The value of
222 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
224 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
228 #if 0 /* This currently never happens, so avoid a useless compare. */
229 if (exclude_level == NO_EXCLUDES)
233 /* never exclude '.', even if somebody does --exclude '*' */
234 if (fname[0] == '.' && !fname[1])
236 /* Handle the -R version of the '.' dir. */
237 if (fname[0] == '/') {
238 int len = strlen(fname);
239 if (fname[len-1] == '.' && fname[len-2] == '/')
243 if (server_exclude_list.head
244 && check_exclude(&server_exclude_list, fname, is_dir) < 0)
246 if (exclude_level != ALL_EXCLUDES)
248 if (exclude_list.head
249 && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
251 if (local_exclude_list.head
252 && check_exclude(&local_exclude_list, fname, is_dir) < 0)
257 /* used by the one_file_system code */
258 static dev_t filesystem_dev;
260 static void set_filesystem(char *fname)
263 if (do_stat(fname, &st) != 0)
265 filesystem_dev = st.st_dev;
269 static int to_wire_mode(mode_t mode)
272 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
273 return (mode & ~(_S_IFMT)) | 0120000;
278 static mode_t from_wire_mode(int mode)
280 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
281 return (mode & ~(_S_IFMT)) | _S_IFLNK;
286 static void send_directory(int f, struct file_list *flist, char *dir);
288 static char *flist_dir;
289 static int flist_dir_len;
293 * Make sure @p flist is big enough to hold at least @p flist->count
296 void flist_expand(struct file_list *flist)
298 struct file_struct **new_ptr;
300 if (flist->count < flist->malloced)
303 if (flist->malloced < FLIST_START)
304 flist->malloced = FLIST_START;
305 else if (flist->malloced >= FLIST_LINEAR)
306 flist->malloced += FLIST_LINEAR;
308 flist->malloced *= 2;
311 * In case count jumped or we are starting the list
312 * with a known size just set it.
314 if (flist->malloced < flist->count)
315 flist->malloced = flist->count;
317 new_ptr = realloc_array(flist->files, struct file_struct *,
320 if (verbose >= 2 && flist->malloced != FLIST_START) {
321 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
323 (double)sizeof flist->files[0] * flist->malloced,
324 (new_ptr == flist->files) ? " not" : "");
327 flist->files = new_ptr;
330 out_of_memory("flist_expand");
333 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
335 unsigned short flags;
336 static time_t modtime;
340 static uint32 rdev_major;
343 static char lastname[MAXPATHLEN];
344 char fname[MAXPATHLEN];
352 modtime = 0, mode = 0;
353 dev = 0, rdev = makedev(0, 0);
360 io_write_phase = "send_file_entry";
362 f_name_to(file, fname);
366 if (file->mode == mode)
367 flags |= XMIT_SAME_MODE;
370 if (preserve_devices) {
371 if (protocol_version < 28) {
372 if (IS_DEVICE(mode)) {
373 if (file->u.rdev == rdev)
374 flags |= XMIT_SAME_RDEV_pre28;
378 rdev = makedev(0, 0);
379 } else if (IS_DEVICE(mode)) {
381 if ((uint32)major(rdev) == rdev_major)
382 flags |= XMIT_SAME_RDEV_MAJOR;
384 rdev_major = major(rdev);
385 if ((uint32)minor(rdev) <= 0xFFu)
386 flags |= XMIT_RDEV_MINOR_IS_SMALL;
389 if (file->uid == uid)
390 flags |= XMIT_SAME_UID;
393 if (file->gid == gid)
394 flags |= XMIT_SAME_GID;
397 if (file->modtime == modtime)
398 flags |= XMIT_SAME_TIME;
400 modtime = file->modtime;
402 #if SUPPORT_HARD_LINKS
403 if (file->link_u.idev) {
404 if (file->F_DEV == dev) {
405 if (protocol_version >= 28)
406 flags |= XMIT_SAME_DEV;
409 flags |= XMIT_HAS_IDEV_DATA;
414 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
416 l2 = strlen(fname+l1);
419 flags |= XMIT_SAME_NAME;
421 flags |= XMIT_LONG_NAME;
423 /* We must make sure we don't send a zero flag byte or the
424 * other end will terminate the flist transfer. Note that
425 * the use of XMIT_DEL_START on a non-dir has no meaning, so
426 * it's harmless way to add a bit to the first flag byte. */
427 if (protocol_version >= 28) {
428 if (!flags && !S_ISDIR(mode))
429 flags |= XMIT_DEL_START;
430 if ((flags & 0xFF00) || !flags) {
431 flags |= XMIT_EXTENDED_FLAGS;
432 write_byte(f, flags);
433 write_byte(f, flags >> 8);
435 write_byte(f, flags);
437 if (!(flags & 0xFF) && !S_ISDIR(mode))
438 flags |= XMIT_DEL_START;
440 flags |= XMIT_LONG_NAME;
441 write_byte(f, flags);
443 if (flags & XMIT_SAME_NAME)
445 if (flags & XMIT_LONG_NAME)
449 write_buf(f, fname + l1, l2);
451 write_longint(f, file->length);
452 if (!(flags & XMIT_SAME_TIME))
453 write_int(f, modtime);
454 if (!(flags & XMIT_SAME_MODE))
455 write_int(f, to_wire_mode(mode));
456 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
461 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
466 if (preserve_devices && IS_DEVICE(mode)) {
467 if (protocol_version < 28) {
468 if (!(flags & XMIT_SAME_RDEV_pre28))
469 write_int(f, (int)rdev);
471 if (!(flags & XMIT_SAME_RDEV_MAJOR))
472 write_int(f, major(rdev));
473 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
474 write_byte(f, minor(rdev));
476 write_int(f, minor(rdev));
481 if (preserve_links && S_ISLNK(mode)) {
482 int len = strlen(file->u.link);
484 write_buf(f, file->u.link, len);
488 #if SUPPORT_HARD_LINKS
489 if (flags & XMIT_HAS_IDEV_DATA) {
490 if (protocol_version < 26) {
491 /* 32-bit dev_t and ino_t */
493 write_int(f, file->F_INODE);
495 /* 64-bit dev_t and ino_t */
496 if (!(flags & XMIT_SAME_DEV))
497 write_longint(f, dev);
498 write_longint(f, file->F_INODE);
503 if (always_checksum) {
507 else if (protocol_version < 28) {
508 /* Prior to 28, we sent a useless set of nulls. */
514 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
518 strlcpy(lastname, fname, MAXPATHLEN);
520 io_write_phase = "unknown";
525 void receive_file_entry(struct file_struct **fptr, unsigned short flags,
526 struct file_list *flist, int f)
528 static time_t modtime;
532 static uint32 rdev_major;
535 static char lastname[MAXPATHLEN], *lastdir;
536 static int lastdir_depth, lastdir_len = -1;
537 char thisname[MAXPATHLEN];
538 unsigned int l1 = 0, l2 = 0;
539 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
541 char *basename, *dirname, *bp;
542 struct file_struct *file;
545 modtime = 0, mode = 0;
546 dev = 0, rdev = makedev(0, 0);
554 if (flags & XMIT_SAME_NAME)
557 if (flags & XMIT_LONG_NAME)
562 if (l2 >= MAXPATHLEN - l1) {
564 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
565 flags, l1, l2, lastname);
566 overflow("receive_file_entry");
569 strlcpy(thisname, lastname, l1 + 1);
570 read_sbuf(f, &thisname[l1], l2);
571 thisname[l1 + l2] = 0;
573 strlcpy(lastname, thisname, MAXPATHLEN);
575 clean_fname(thisname, 0);
578 sanitize_path(thisname, thisname, "", 0);
580 if ((basename = strrchr(thisname, '/')) != NULL) {
581 dirname_len = ++basename - thisname; /* counts future '\0' */
582 if (lastdir_len == dirname_len - 1
583 && strncmp(thisname, lastdir, lastdir_len) == 0) {
585 dirname_len = 0; /* indicates no copy is needed */
593 basename_len = strlen(basename) + 1; /* count the '\0' */
595 file_length = read_longint(f);
596 if (!(flags & XMIT_SAME_TIME))
597 modtime = (time_t)read_int(f);
598 if (!(flags & XMIT_SAME_MODE))
599 mode = from_wire_mode(read_int(f));
601 if (preserve_uid && !(flags & XMIT_SAME_UID))
602 uid = (uid_t)read_int(f);
603 if (preserve_gid && !(flags & XMIT_SAME_GID))
604 gid = (gid_t)read_int(f);
606 if (preserve_devices) {
607 if (protocol_version < 28) {
608 if (IS_DEVICE(mode)) {
609 if (!(flags & XMIT_SAME_RDEV_pre28))
610 rdev = (dev_t)read_int(f);
612 rdev = makedev(0, 0);
613 } else if (IS_DEVICE(mode)) {
615 if (!(flags & XMIT_SAME_RDEV_MAJOR))
616 rdev_major = read_int(f);
617 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
618 rdev_minor = read_byte(f);
620 rdev_minor = read_int(f);
621 rdev = makedev(rdev_major, rdev_minor);
626 if (preserve_links && S_ISLNK(mode)) {
627 linkname_len = read_int(f) + 1; /* count the '\0' */
628 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
629 rprintf(FERROR, "overflow: linkname_len=%d\n",
631 overflow("receive_file_entry");
638 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
640 alloc_len = file_struct_len + dirname_len + basename_len
641 + linkname_len + sum_len;
642 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
644 file = *fptr = (struct file_struct *)bp;
645 memset(bp, 0, file_struct_len);
646 bp += file_struct_len;
648 file->flags = flags & XMIT_DEL_START ? FLAG_DEL_START : 0;
649 file->modtime = modtime;
650 file->length = file_length;
656 file->dirname = lastdir = bp;
657 lastdir_len = dirname_len - 1;
658 memcpy(bp, dirname, dirname_len - 1);
662 lastdir_depth = count_dir_elements(lastdir);
664 file->dirname = dirname;
667 memcpy(bp, basename, basename_len);
670 if (preserve_devices && IS_DEVICE(mode))
676 read_sbuf(f, bp, linkname_len - 1);
678 sanitize_path(bp, bp, "", lastdir_depth);
683 #if SUPPORT_HARD_LINKS
684 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
685 flags |= XMIT_HAS_IDEV_DATA;
686 if (flags & XMIT_HAS_IDEV_DATA) {
688 if (protocol_version < 26) {
692 if (!(flags & XMIT_SAME_DEV))
693 dev = read_longint(f);
694 inode = read_longint(f);
696 if (flist->hlink_pool) {
697 file->link_u.idev = pool_talloc(flist->hlink_pool,
698 struct idev, 1, "inode_table");
699 file->F_INODE = inode;
705 if (always_checksum) {
708 file->u.sum = sum = bp;
710 } else if (protocol_version < 28) {
711 /* Prior to 28, we get a useless set of nulls. */
717 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
721 if (!preserve_perms) {
722 /* set an appropriate set of permissions based on original
723 * permissions and umask. This emulates what GNU cp does */
724 file->mode &= ~orig_umask;
730 * Create a file_struct for a named file by reading its stat()
731 * information and performing extensive checks against global
734 * @return the new file, or NULL if there was an error or this file
735 * should be excluded.
737 * @todo There is a small optimization opportunity here to avoid
738 * stat()ing the file in some circumstances, which has a certain cost.
739 * We are called immediately after doing readdir(), and so we may
740 * already know the d_type of the file. We could for example avoid
741 * statting directories if we're not recursing, but this is not a very
742 * important case. Some systems may not have d_type.
744 struct file_struct *make_file(char *fname, struct file_list *flist,
747 static char *lastdir;
748 static int lastdir_len = -1;
749 struct file_struct *file;
751 char sum[SUM_LENGTH];
752 char thisname[MAXPATHLEN];
753 char linkname[MAXPATHLEN];
754 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
755 char *basename, *dirname, *bp;
756 unsigned short flags = 0;
758 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
761 if (strlcpy(thisname, fname, sizeof thisname)
762 >= sizeof thisname - flist_dir_len) {
763 rprintf(FINFO, "skipping overly long name: %s\n", fname);
766 clean_fname(thisname, 0);
768 sanitize_path(thisname, thisname, "", 0);
770 memset(sum, 0, SUM_LENGTH);
772 if (readlink_stat(thisname, &st, linkname) != 0) {
773 int save_errno = errno;
774 /* See if file is excluded before reporting an error. */
775 if (exclude_level != NO_EXCLUDES
776 && check_exclude_file(thisname, 0, exclude_level))
778 if (save_errno == ENOENT) {
780 /* Avoid "vanished" error if symlink points nowhere. */
781 if (copy_links && do_lstat(thisname, &st) == 0
782 && S_ISLNK(st.st_mode)) {
783 io_error |= IOERR_GENERAL;
784 rprintf(FERROR, "symlink has no referent: %s\n",
785 full_fname(thisname));
789 enum logcode c = am_daemon && protocol_version < 28
791 io_error |= IOERR_VANISHED;
792 rprintf(c, "file has vanished: %s\n",
793 full_fname(thisname));
796 io_error |= IOERR_GENERAL;
797 rsyserr(FERROR, save_errno, "readlink %s failed",
798 full_fname(thisname));
803 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
804 if (exclude_level == NO_EXCLUDES)
807 if (S_ISDIR(st.st_mode) && !keep_dirs) {
808 rprintf(FINFO, "skipping directory %s\n", thisname);
812 /* We only care about directories because we need to avoid recursing
813 * into a mount-point directory, not to avoid copying a symlinked
814 * file if -L (or similar) was specified. */
815 if (one_file_system && st.st_dev != filesystem_dev
816 && S_ISDIR(st.st_mode))
817 flags |= FLAG_MOUNT_POINT;
819 if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level))
822 if (lp_ignore_nonreadable(module_id)) {
824 if (!S_ISLNK(st.st_mode))
826 if (access(thisname, R_OK) != 0)
833 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
834 who_am_i(), thisname, exclude_level);
837 if ((basename = strrchr(thisname, '/')) != NULL) {
838 dirname_len = ++basename - thisname; /* counts future '\0' */
839 if (lastdir_len == dirname_len - 1
840 && strncmp(thisname, lastdir, lastdir_len) == 0) {
842 dirname_len = 0; /* indicates no copy is needed */
850 basename_len = strlen(basename) + 1; /* count the '\0' */
853 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
858 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
860 alloc_len = file_struct_len + dirname_len + basename_len
861 + linkname_len + sum_len;
863 bp = pool_alloc(flist->file_pool, alloc_len,
864 "receive_file_entry");
866 if (!(bp = new_array(char, alloc_len)))
867 out_of_memory("receive_file_entry");
870 file = (struct file_struct *)bp;
871 memset(bp, 0, file_struct_len);
872 bp += file_struct_len;
875 file->modtime = st.st_mtime;
876 file->length = st.st_size;
877 file->mode = st.st_mode;
878 file->uid = st.st_uid;
879 file->gid = st.st_gid;
881 #if SUPPORT_HARD_LINKS
882 if (flist && flist->hlink_pool) {
883 if (protocol_version < 28) {
884 if (S_ISREG(st.st_mode))
885 file->link_u.idev = pool_talloc(
886 flist->hlink_pool, struct idev, 1,
889 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
890 file->link_u.idev = pool_talloc(
891 flist->hlink_pool, struct idev, 1,
895 if (file->link_u.idev) {
896 file->F_DEV = st.st_dev;
897 file->F_INODE = st.st_ino;
902 file->dirname = lastdir = bp;
903 lastdir_len = dirname_len - 1;
904 memcpy(bp, dirname, dirname_len - 1);
908 file->dirname = dirname;
911 memcpy(bp, basename, basename_len);
914 #ifdef HAVE_STRUCT_STAT_ST_RDEV
915 if (preserve_devices && IS_DEVICE(st.st_mode))
916 file->u.rdev = st.st_rdev;
922 memcpy(bp, linkname, linkname_len);
929 file_checksum(thisname, bp, st.st_size);
933 file->basedir = flist_dir;
935 /* This code is only used by the receiver when it is building
936 * a list of files for a delete pass. */
937 if (keep_dirlinks && linkname_len && flist) {
939 int i = flist_find(received_flist, file);
940 if (i >= 0 && S_ISDIR(received_flist->files[i]->mode)
941 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
942 file->modtime = st2.st_mtime;
943 file->length = st2.st_size;
944 file->mode = st2.st_mode;
945 file->uid = st2.st_uid;
946 file->gid = st2.st_gid;
948 if (file->link_u.idev) {
949 pool_free(flist->hlink_pool, 0, file->link_u.idev);
950 file->link_u.idev = NULL;
955 if (!S_ISDIR(st.st_mode))
956 stats.total_size += st.st_size;
962 void send_file_name(int f, struct file_list *flist, char *fname,
963 int recursive, unsigned short base_flags)
965 struct file_struct *file;
966 char fbuf[MAXPATHLEN];
968 /* f is set to -1 when calculating deletion file list */
969 file = make_file(fname, flist,
970 f == -1 && delete_excluded? SERVER_EXCLUDES : ALL_EXCLUDES);
975 maybe_emit_filelist_progress(flist);
979 if (file->basename[0]) {
980 flist->files[flist->count++] = file;
981 send_file_entry(file, f, base_flags);
984 if (recursive && S_ISDIR(file->mode)
985 && !(file->flags & FLAG_MOUNT_POINT)) {
986 struct exclude_list_struct last_list = local_exclude_list;
987 local_exclude_list.head = local_exclude_list.tail = NULL;
988 send_directory(f, flist, f_name_to(file, fbuf));
990 rprintf(FINFO, "[%s] popping %sexclude list\n",
991 who_am_i(), local_exclude_list.debug_type);
993 clear_exclude_list(&local_exclude_list);
994 local_exclude_list = last_list;
999 /* Note that the "recurse" value either contains -1, for infinite recursion,
1000 * or a number >= 0 indicating how many levels of recursion we will allow. */
1001 static void send_directory(int f, struct file_list *flist, char *dir)
1005 char fname[MAXPATHLEN];
1006 unsigned int offset;
1011 io_error |= IOERR_GENERAL;
1012 rsyserr(FERROR, errno, "opendir %s failed", full_fname(dir));
1016 offset = strlcpy(fname, dir, MAXPATHLEN);
1018 if (offset >= MAXPATHLEN || p[-1] != '/') {
1019 if (offset >= MAXPATHLEN - 1) {
1020 io_error |= IOERR_GENERAL;
1021 rprintf(FERROR, "skipping long-named directory: %s\n",
1031 if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
1032 < MAXPATHLEN - offset) {
1033 add_exclude_file(&local_exclude_list, fname,
1034 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
1036 io_error |= IOERR_GENERAL;
1038 "cannot cvs-exclude in long-named directory %s\n",
1043 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1044 char *dname = d_name(di);
1045 if (dname[0] == '.' && (dname[1] == '\0'
1046 || (dname[1] == '.' && dname[2] == '\0')))
1048 if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset) {
1049 int do_subdirs = recurse >= 1 ? recurse-- : recurse;
1050 send_file_name(f, flist, fname, do_subdirs, 0);
1052 io_error |= IOERR_GENERAL;
1054 "cannot send long-named file %s\n",
1059 io_error |= IOERR_GENERAL;
1060 rsyserr(FERROR, errno, "readdir(%s)", dir);
1068 * This function is normally called by the sender, but the receiver also
1069 * uses it to construct its own file list if --delete has been specified.
1070 * The delete_files() function in receiver.c sets f to -1 so that we just
1071 * construct the file list in memory without sending it over the wire. It
1072 * also has the side-effect of ignoring user-excludes if delete_excluded
1073 * is set (so that the delete list includes user-excluded files).
1075 struct file_list *send_file_list(int f, int argc, char *argv[])
1079 char *p, *dir, olddir[sizeof curr_dir];
1080 char lastpath[MAXPATHLEN] = "";
1081 struct file_list *flist;
1085 if (show_filelist_p() && f != -1)
1086 start_filelist_progress("building file list");
1088 start_write = stats.total_written;
1090 flist = flist_new(f == -1 ? WITHOUT_HLINK : WITH_HLINK,
1094 io_start_buffering_out();
1095 if (filesfrom_fd >= 0) {
1096 if (argv[0] && !push_dir(argv[0])) {
1097 rsyserr(FERROR, errno, "push_dir %s failed",
1098 full_fname(argv[0]));
1099 exit_cleanup(RERR_FILESELECT);
1106 char fname2[MAXPATHLEN];
1107 char *fname = fname2;
1111 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1113 sanitize_path(fname, fname, "", 0);
1117 strlcpy(fname, *argv++, MAXPATHLEN);
1119 sanitize_path(fname, fname, "", 0);
1123 if (!l || fname[l - 1] == '/') {
1124 if (l == 2 && fname[0] == '.') {
1125 /* Turn "./" into just "." rather than "./." */
1127 } else if (l < MAXPATHLEN) {
1132 if (fname[l-1] == '.' && (l == 1 || fname[l-2] == '/')) {
1133 if (!recurse && keep_dirs)
1134 recurse = 1; /* allow one level */
1135 } else if (recurse > 0)
1138 if (link_stat(fname, &st, keep_dirlinks) != 0) {
1140 io_error |= IOERR_GENERAL;
1141 rsyserr(FERROR, errno, "link_stat %s failed",
1147 if (S_ISDIR(st.st_mode) && !keep_dirs) {
1148 rprintf(FINFO, "skipping directory %s\n", fname);
1155 if (!relative_paths) {
1156 p = strrchr(fname, '/');
1165 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1166 /* this ensures we send the intermediate directories,
1167 thus getting their permissions right */
1168 char *lp = lastpath, *fn = fname, *slash = fname;
1170 /* Skip any initial directories in our path that we
1171 * have in common with lastpath. */
1172 while (*fn && *lp == *fn) {
1178 if (fn != p || (*lp && *lp != '/')) {
1179 int save_copy_links = copy_links;
1180 int save_keep_dirs = keep_dirs;
1181 copy_links = copy_unsafe_links;
1183 while ((slash = strchr(slash+1, '/')) != 0) {
1185 send_file_name(f, flist, fname, 0, 0);
1188 copy_links = save_copy_links;
1189 keep_dirs = save_keep_dirs;
1191 strlcpy(lastpath, fname, sizeof lastpath);
1200 static char *lastdir;
1201 static int lastdir_len;
1203 strcpy(olddir, curr_dir); /* can't overflow */
1205 if (!push_dir(dir)) {
1206 io_error |= IOERR_GENERAL;
1207 rsyserr(FERROR, errno, "push_dir %s failed",
1212 if (lastdir && strcmp(lastdir, dir) == 0) {
1213 flist_dir = lastdir;
1214 flist_dir_len = lastdir_len;
1216 flist_dir = lastdir = strdup(dir);
1217 flist_dir_len = lastdir_len = strlen(dir);
1221 if (one_file_system)
1222 set_filesystem(fname);
1224 do_subdirs = recurse >= 1 ? recurse-- : recurse;
1225 send_file_name(f, flist, fname, do_subdirs, XMIT_DEL_START);
1230 if (!pop_dir(olddir)) {
1231 rsyserr(FERROR, errno, "pop_dir %s failed",
1233 exit_cleanup(RERR_FILESELECT);
1239 send_file_entry(NULL, f, 0);
1241 if (show_filelist_p())
1242 finish_filelist_progress(flist);
1245 if (flist->hlink_pool) {
1246 pool_destroy(flist->hlink_pool);
1247 flist->hlink_pool = NULL;
1250 clean_flist(flist, 0, 0);
1253 /* Now send the uid/gid list. This was introduced in
1254 * protocol version 15 */
1257 /* send the io_error flag */
1258 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1261 stats.flist_size = stats.total_written - start_write;
1262 stats.num_files = flist->count;
1266 output_flist(flist, f < 0 ? "delete" : who_am_i());
1269 rprintf(FINFO, "send_file_list done\n");
1275 struct file_list *recv_file_list(int f)
1277 struct file_list *flist;
1278 unsigned short flags;
1281 if (show_filelist_p())
1282 start_filelist_progress("receiving file list");
1284 start_read = stats.total_read;
1286 flist = flist_new(WITH_HLINK, "recv_file_list");
1287 received_flist = flist;
1290 flist->malloced = 1000;
1291 flist->files = new_array(struct file_struct *, flist->malloced);
1296 while ((flags = read_byte(f)) != 0) {
1297 int i = flist->count;
1299 flist_expand(flist);
1301 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1302 flags |= read_byte(f) << 8;
1303 receive_file_entry(&flist->files[i], flags, flist, f);
1305 if (S_ISREG(flist->files[i]->mode))
1306 stats.total_size += flist->files[i]->length;
1310 maybe_emit_filelist_progress(flist);
1313 rprintf(FINFO, "recv_file_name(%s)\n",
1314 f_name(flist->files[i]));
1317 receive_file_entry(NULL, 0, NULL, 0); /* Signal that we're done. */
1320 rprintf(FINFO, "received %d names\n", flist->count);
1322 if (show_filelist_p())
1323 finish_filelist_progress(flist);
1325 clean_flist(flist, relative_paths, 1);
1328 /* Now send the uid/gid list. This was introduced in
1329 * protocol version 15 */
1330 recv_uid_list(f, flist);
1332 /* Recv the io_error flag */
1333 if (lp_ignore_errors(module_id) || ignore_errors)
1336 io_error |= read_int(f);
1340 output_flist(flist, who_am_i());
1344 for (i = 0; i < flist->count; i++)
1345 list_file_entry(flist->files[i]);
1349 rprintf(FINFO, "recv_file_list done\n");
1351 stats.flist_size = stats.total_read - start_read;
1352 stats.num_files = flist->count;
1357 out_of_memory("recv_file_list");
1358 return NULL; /* not reached */
1362 int file_compare(struct file_struct **file1, struct file_struct **file2)
1364 struct file_struct *f1 = *file1;
1365 struct file_struct *f2 = *file2;
1367 if (!f1->basename && !f2->basename)
1373 if (f1->dirname == f2->dirname)
1374 return u_strcmp(f1->basename, f2->basename);
1375 return f_name_cmp(f1, f2);
1379 int flist_find(struct file_list *flist, struct file_struct *f)
1381 int low = 0, high = flist->count - 1;
1383 while (high >= 0 && !flist->files[high]->basename) high--;
1388 while (low != high) {
1389 int mid = (low + high) / 2;
1390 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1392 return flist_up(flist, mid);
1399 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1400 return flist_up(flist, low);
1405 * Free up any resources a file_struct has allocated
1406 * and clear the file.
1408 void clear_file(int i, struct file_list *flist)
1410 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1411 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1412 memset(flist->files[i], 0, file_struct_len);
1417 * allocate a new file list
1419 struct file_list *flist_new(int with_hlink, char *msg)
1421 struct file_list *flist;
1423 flist = new(struct file_list);
1427 memset(flist, 0, sizeof (struct file_list));
1429 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1430 out_of_memory, POOL_INTERN)))
1433 #if SUPPORT_HARD_LINKS
1434 if (with_hlink && preserve_hard_links) {
1435 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1436 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1445 * free up all elements in a flist
1447 void flist_free(struct file_list *flist)
1449 pool_destroy(flist->file_pool);
1450 pool_destroy(flist->hlink_pool);
1457 * This routine ensures we don't have any duplicate names in our file list.
1458 * duplicate names can cause corruption because of the pipelining
1460 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1464 if (!flist || flist->count == 0)
1467 qsort(flist->files, flist->count,
1468 sizeof flist->files[0], (int (*)())file_compare);
1470 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1471 if (flist->files[i]->basename) {
1476 while (++i < flist->count) {
1477 if (!flist->files[i]->basename)
1479 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1480 if (verbose > 1 && !am_server) {
1482 "removing duplicate name %s from file list %d\n",
1483 f_name(flist->files[i]), i);
1485 /* Make sure that if we unduplicate '.', that we don't
1486 * lose track of a user-specified starting point (or
1487 * else deletions will mysteriously fail with -R). */
1488 if (flist->files[i]->flags & FLAG_DEL_START)
1489 flist->files[prev_i]->flags |= FLAG_DEL_START;
1491 clear_file(i, flist);
1497 /* we need to strip off the root directory in the case
1498 of relative paths, but this must be done _after_
1499 the sorting phase */
1500 for (i = 0; i < flist->count; i++) {
1501 if (flist->files[i]->dirname &&
1502 flist->files[i]->dirname[0] == '/') {
1503 memmove(&flist->files[i]->dirname[0],
1504 &flist->files[i]->dirname[1],
1505 strlen(flist->files[i]->dirname));
1508 if (flist->files[i]->dirname &&
1509 !flist->files[i]->dirname[0]) {
1510 flist->files[i]->dirname = NULL;
1516 static void output_flist(struct file_list *flist, const char *whose_list)
1518 char uidbuf[16], gidbuf[16];
1519 struct file_struct *file;
1522 for (i = 0; i < flist->count; i++) {
1523 file = flist->files[i];
1524 if ((am_root || am_sender) && preserve_uid)
1525 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1528 if (preserve_gid && file->gid != GID_NONE)
1529 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1532 rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f%s%s\n",
1533 whose_list, i, NS(file->basedir), NS(file->dirname),
1534 NS(file->basename), (int)file->mode,
1535 (double)file->length, uidbuf, gidbuf);
1540 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1542 /* Compare the names of two file_struct entities, just like strcmp()
1543 * would do if it were operating on the joined strings. We assume
1544 * that there are no 0-length strings.
1546 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1549 const uchar *c1, *c2;
1550 enum fnc_state state1, state2;
1552 if (!f1 || !f1->basename) {
1553 if (!f2 || !f2->basename)
1557 if (!f2 || !f2->basename)
1560 if (!(c1 = (uchar*)f1->dirname)) {
1562 c1 = (uchar*)f1->basename;
1568 if (!(c2 = (uchar*)f2->dirname)) {
1570 c2 = (uchar*)f2->basename;
1578 if ((dif = (int)*c1 - (int)*c2) != 0)
1588 c1 = (uchar*)f1->basename;
1602 c2 = (uchar*)f2->basename;
1616 /* Return a copy of the full filename of a flist entry, using the indicated
1617 * buffer. No size-checking is done because we checked the size when creating
1618 * the file_struct entry.
1620 char *f_name_to(struct file_struct *f, char *fbuf)
1622 if (!f || !f->basename)
1626 int len = strlen(f->dirname);
1627 memcpy(fbuf, f->dirname, len);
1629 strcpy(fbuf + len + 1, f->basename);
1631 strcpy(fbuf, f->basename);
1636 /* Like f_name_to(), but we rotate through 5 static buffers of our own.
1638 char *f_name(struct file_struct *f)
1640 static char names[5][MAXPATHLEN];
1641 static unsigned int n;
1643 n = (n + 1) % (sizeof names / sizeof names[0]);
1645 return f_name_to(f, names[n]);