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 delete_during;
39 extern int always_checksum;
41 extern int ignore_errors;
42 extern int numeric_ids;
44 extern int cvs_exclude;
48 extern char curr_dir[MAXPATHLEN];
49 extern char *backup_dir;
50 extern char *backup_suffix;
51 extern int filesfrom_fd;
53 extern int one_file_system;
54 extern int keep_dirlinks;
55 extern int preserve_links;
56 extern int preserve_hard_links;
57 extern int preserve_perms;
58 extern int preserve_devices;
59 extern int preserve_uid;
60 extern int preserve_gid;
61 extern int relative_paths;
62 extern int implied_dirs;
63 extern int make_backups;
64 extern int backup_suffix_len;
65 extern int copy_links;
66 extern int copy_unsafe_links;
67 extern int protocol_version;
68 extern int sanitize_paths;
69 extern int delete_excluded;
70 extern int max_delete;
71 extern int orig_umask;
74 extern struct exclude_list_struct exclude_list;
75 extern struct exclude_list_struct server_exclude_list;
76 extern struct exclude_list_struct local_exclude_list;
80 static char empty_sum[MD4_SUM_LENGTH];
81 static unsigned int file_struct_len;
82 static struct file_list *received_flist;
84 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
85 static void output_flist(struct file_list *flist, const char *whose_list);
91 /* Figure out how big the file_struct is without trailing padding */
92 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
96 static int show_filelist_p(void)
98 return verbose && xfer_dirs && !am_server;
101 static void start_filelist_progress(char *kind)
103 rprintf(FINFO, "%s ... ", kind);
104 if (verbose > 1 || do_progress)
105 rprintf(FINFO, "\n");
110 static void emit_filelist_progress(const struct file_list *flist)
112 rprintf(FINFO, " %d files...\r", flist->count);
116 static void maybe_emit_filelist_progress(const struct file_list *flist)
118 if (do_progress && show_filelist_p() && (flist->count % 100) == 0)
119 emit_filelist_progress(flist);
123 static void finish_filelist_progress(const struct file_list *flist)
126 /* This overwrites the progress line */
127 rprintf(FINFO, "%d file%sto consider\n",
128 flist->count, flist->count == 1 ? " " : "s ");
130 rprintf(FINFO, "done\n");
133 void show_flist_stats(void)
139 static void list_file_entry(struct file_struct *f)
144 /* this can happen if duplicate names were removed */
148 permstring(perms, f->mode);
151 if (preserve_links && S_ISLNK(f->mode)) {
152 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
154 (double)f->length, timestring(f->modtime),
155 f_name(f), f->u.link);
159 rprintf(FINFO, "%s %11.0f %s %s\n",
161 (double)f->length, timestring(f->modtime),
168 * Stat either a symlink or its referent, depending on the settings of
169 * copy_links, copy_unsafe_links, etc.
171 * @retval -1 on error
173 * @retval 0 for success
175 * @post If @p path is a symlink, then @p linkbuf (of size @c
176 * MAXPATHLEN) contains the symlink target.
178 * @post @p buffer contains information about the link or the
179 * referrent as appropriate, if they exist.
181 static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
185 return do_stat(path, buffer);
186 if (link_stat(path, buffer, 0) < 0)
188 if (S_ISLNK(buffer->st_mode)) {
189 int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1);
193 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
195 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
198 return do_stat(path, buffer);
203 return do_stat(path, buffer);
207 int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks)
211 return do_stat(path, buffer);
212 if (do_lstat(path, buffer) < 0)
214 if (follow_dirlinks && S_ISLNK(buffer->st_mode)) {
216 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
221 return do_stat(path, buffer);
226 * This function is used to check if a file should be included/excluded
227 * from the list of files based on its name and type etc. The value of
228 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
230 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
234 #if 0 /* This currently never happens, so avoid a useless compare. */
235 if (exclude_level == NO_EXCLUDES)
239 /* never exclude '.', even if somebody does --exclude '*' */
240 if (fname[0] == '.' && !fname[1])
242 /* Handle the -R version of the '.' dir. */
243 if (fname[0] == '/') {
244 int len = strlen(fname);
245 if (fname[len-1] == '.' && fname[len-2] == '/')
249 if (server_exclude_list.head
250 && check_exclude(&server_exclude_list, fname, is_dir) < 0)
252 if (exclude_level != ALL_EXCLUDES)
254 if (exclude_list.head
255 && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
257 if (local_exclude_list.head
258 && check_exclude(&local_exclude_list, fname, is_dir) < 0)
263 /* used by the one_file_system code */
264 static dev_t filesystem_dev;
266 static void set_filesystem(char *fname)
269 if (do_stat(fname, &st) != 0)
271 filesystem_dev = st.st_dev;
275 static int to_wire_mode(mode_t mode)
278 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
279 return (mode & ~(_S_IFMT)) | 0120000;
284 static mode_t from_wire_mode(int mode)
286 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
287 return (mode & ~(_S_IFMT)) | _S_IFLNK;
292 static void send_directory(int f, struct file_list *flist, char *dir);
294 static char *flist_dir;
295 static int flist_dir_len;
299 * Make sure @p flist is big enough to hold at least @p flist->count
302 void flist_expand(struct file_list *flist)
304 struct file_struct **new_ptr;
306 if (flist->count < flist->malloced)
309 if (flist->malloced < FLIST_START)
310 flist->malloced = FLIST_START;
311 else if (flist->malloced >= FLIST_LINEAR)
312 flist->malloced += FLIST_LINEAR;
314 flist->malloced *= 2;
317 * In case count jumped or we are starting the list
318 * with a known size just set it.
320 if (flist->malloced < flist->count)
321 flist->malloced = flist->count;
323 new_ptr = realloc_array(flist->files, struct file_struct *,
326 if (verbose >= 2 && flist->malloced != FLIST_START) {
327 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
329 (double)sizeof flist->files[0] * flist->malloced,
330 (new_ptr == flist->files) ? " not" : "");
333 flist->files = new_ptr;
336 out_of_memory("flist_expand");
339 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
341 unsigned short flags;
342 static time_t modtime;
346 static uint32 rdev_major;
349 static char lastname[MAXPATHLEN];
350 char fname[MAXPATHLEN];
358 modtime = 0, mode = 0;
359 dev = 0, rdev = makedev(0, 0);
366 io_write_phase = "send_file_entry";
368 f_name_to(file, fname);
372 if (file->mode == mode)
373 flags |= XMIT_SAME_MODE;
376 if (preserve_devices) {
377 if (protocol_version < 28) {
378 if (IS_DEVICE(mode)) {
379 if (file->u.rdev == rdev)
380 flags |= XMIT_SAME_RDEV_pre28;
384 rdev = makedev(0, 0);
385 } else if (IS_DEVICE(mode)) {
387 if ((uint32)major(rdev) == rdev_major)
388 flags |= XMIT_SAME_RDEV_MAJOR;
390 rdev_major = major(rdev);
391 if ((uint32)minor(rdev) <= 0xFFu)
392 flags |= XMIT_RDEV_MINOR_IS_SMALL;
395 if (file->uid == uid)
396 flags |= XMIT_SAME_UID;
399 if (file->gid == gid)
400 flags |= XMIT_SAME_GID;
403 if (file->modtime == modtime)
404 flags |= XMIT_SAME_TIME;
406 modtime = file->modtime;
408 #if SUPPORT_HARD_LINKS
409 if (file->link_u.idev) {
410 if (file->F_DEV == dev) {
411 if (protocol_version >= 28)
412 flags |= XMIT_SAME_DEV;
415 flags |= XMIT_HAS_IDEV_DATA;
420 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
422 l2 = strlen(fname+l1);
425 flags |= XMIT_SAME_NAME;
427 flags |= XMIT_LONG_NAME;
429 /* We must make sure we don't send a zero flag byte or the
430 * other end will terminate the flist transfer. Note that
431 * the use of XMIT_DEL_START on a non-dir has no meaning, so
432 * it's harmless way to add a bit to the first flag byte. */
433 if (protocol_version >= 28) {
434 if (!flags && !S_ISDIR(mode))
435 flags |= XMIT_DEL_START;
436 if ((flags & 0xFF00) || !flags) {
437 flags |= XMIT_EXTENDED_FLAGS;
438 write_byte(f, flags);
439 write_byte(f, flags >> 8);
441 write_byte(f, flags);
443 if (!(flags & 0xFF) && !S_ISDIR(mode))
444 flags |= XMIT_DEL_START;
446 flags |= XMIT_LONG_NAME;
447 write_byte(f, flags);
449 if (flags & XMIT_SAME_NAME)
451 if (flags & XMIT_LONG_NAME)
455 write_buf(f, fname + l1, l2);
457 write_longint(f, file->length);
458 if (!(flags & XMIT_SAME_TIME))
459 write_int(f, modtime);
460 if (!(flags & XMIT_SAME_MODE))
461 write_int(f, to_wire_mode(mode));
462 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
467 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
472 if (preserve_devices && IS_DEVICE(mode)) {
473 if (protocol_version < 28) {
474 if (!(flags & XMIT_SAME_RDEV_pre28))
475 write_int(f, (int)rdev);
477 if (!(flags & XMIT_SAME_RDEV_MAJOR))
478 write_int(f, major(rdev));
479 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
480 write_byte(f, minor(rdev));
482 write_int(f, minor(rdev));
487 if (preserve_links && S_ISLNK(mode)) {
488 int len = strlen(file->u.link);
490 write_buf(f, file->u.link, len);
494 #if SUPPORT_HARD_LINKS
495 if (flags & XMIT_HAS_IDEV_DATA) {
496 if (protocol_version < 26) {
497 /* 32-bit dev_t and ino_t */
499 write_int(f, file->F_INODE);
501 /* 64-bit dev_t and ino_t */
502 if (!(flags & XMIT_SAME_DEV))
503 write_longint(f, dev);
504 write_longint(f, file->F_INODE);
509 if (always_checksum) {
513 else if (protocol_version < 28) {
514 /* Prior to 28, we sent a useless set of nulls. */
520 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
524 strlcpy(lastname, fname, MAXPATHLEN);
526 io_write_phase = "unknown";
531 void receive_file_entry(struct file_struct **fptr, unsigned short flags,
532 struct file_list *flist, int f)
534 static time_t modtime;
538 static uint32 rdev_major;
541 static char lastname[MAXPATHLEN], *lastdir;
542 static int lastdir_depth, lastdir_len = -1;
543 static unsigned int del_heir_name_len = -1;
544 static int in_del_hier = 0;
545 char thisname[MAXPATHLEN];
546 unsigned int l1 = 0, l2 = 0;
547 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
549 char *basename, *dirname, *bp;
550 struct file_struct *file;
553 modtime = 0, mode = 0;
554 dev = 0, rdev = makedev(0, 0);
558 del_heir_name_len = lastdir_len = -1;
563 if (flags & XMIT_SAME_NAME)
566 if (flags & XMIT_LONG_NAME)
571 if (l2 >= MAXPATHLEN - l1) {
573 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
574 flags, l1, l2, lastname);
575 overflow("receive_file_entry");
578 strlcpy(thisname, lastname, l1 + 1);
579 read_sbuf(f, &thisname[l1], l2);
580 thisname[l1 + l2] = 0;
582 strlcpy(lastname, thisname, MAXPATHLEN);
584 clean_fname(thisname, 0);
587 sanitize_path(thisname, thisname, "", 0);
589 if ((basename = strrchr(thisname, '/')) != NULL) {
590 dirname_len = ++basename - thisname; /* counts future '\0' */
591 if (lastdir_len == dirname_len - 1
592 && strncmp(thisname, lastdir, lastdir_len) == 0) {
594 dirname_len = 0; /* indicates no copy is needed */
602 basename_len = strlen(basename) + 1; /* count the '\0' */
604 file_length = read_longint(f);
605 if (!(flags & XMIT_SAME_TIME))
606 modtime = (time_t)read_int(f);
607 if (!(flags & XMIT_SAME_MODE))
608 mode = from_wire_mode(read_int(f));
610 if (preserve_uid && !(flags & XMIT_SAME_UID))
611 uid = (uid_t)read_int(f);
612 if (preserve_gid && !(flags & XMIT_SAME_GID))
613 gid = (gid_t)read_int(f);
615 if (preserve_devices) {
616 if (protocol_version < 28) {
617 if (IS_DEVICE(mode)) {
618 if (!(flags & XMIT_SAME_RDEV_pre28))
619 rdev = (dev_t)read_int(f);
621 rdev = makedev(0, 0);
622 } else if (IS_DEVICE(mode)) {
624 if (!(flags & XMIT_SAME_RDEV_MAJOR))
625 rdev_major = read_int(f);
626 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
627 rdev_minor = read_byte(f);
629 rdev_minor = read_int(f);
630 rdev = makedev(rdev_major, rdev_minor);
635 if (preserve_links && S_ISLNK(mode)) {
636 linkname_len = read_int(f) + 1; /* count the '\0' */
637 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
638 rprintf(FERROR, "overflow: linkname_len=%d\n",
640 overflow("receive_file_entry");
647 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
649 alloc_len = file_struct_len + dirname_len + basename_len
650 + linkname_len + sum_len;
651 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
653 file = *fptr = (struct file_struct *)bp;
654 memset(bp, 0, file_struct_len);
655 bp += file_struct_len;
658 file->modtime = modtime;
659 file->length = file_length;
665 if (flags & XMIT_DEL_START) {
667 del_heir_name_len = l1 + l2;
668 file->flags |= FLAG_DEL_START;
669 } else if (delete_during && in_del_hier) {
670 if (!relative_paths || (l1 >= del_heir_name_len
671 && thisname[del_heir_name_len] == '/'))
672 file->flags |= FLAG_DEL_START;
679 file->dirname = lastdir = bp;
680 lastdir_len = dirname_len - 1;
681 memcpy(bp, dirname, dirname_len - 1);
685 lastdir_depth = count_dir_elements(lastdir);
687 file->dirname = dirname;
690 memcpy(bp, basename, basename_len);
693 if (preserve_devices && IS_DEVICE(mode))
699 read_sbuf(f, bp, linkname_len - 1);
701 sanitize_path(bp, bp, "", lastdir_depth);
706 #if SUPPORT_HARD_LINKS
707 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
708 flags |= XMIT_HAS_IDEV_DATA;
709 if (flags & XMIT_HAS_IDEV_DATA) {
711 if (protocol_version < 26) {
715 if (!(flags & XMIT_SAME_DEV))
716 dev = read_longint(f);
717 inode = read_longint(f);
719 if (flist->hlink_pool) {
720 file->link_u.idev = pool_talloc(flist->hlink_pool,
721 struct idev, 1, "inode_table");
722 file->F_INODE = inode;
728 if (always_checksum) {
731 file->u.sum = sum = bp;
733 } else if (protocol_version < 28) {
734 /* Prior to 28, we get a useless set of nulls. */
740 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
744 if (!preserve_perms) {
745 /* set an appropriate set of permissions based on original
746 * permissions and umask. This emulates what GNU cp does */
747 file->mode &= ~orig_umask;
753 * Create a file_struct for a named file by reading its stat()
754 * information and performing extensive checks against global
757 * @return the new file, or NULL if there was an error or this file
758 * should be excluded.
760 * @todo There is a small optimization opportunity here to avoid
761 * stat()ing the file in some circumstances, which has a certain cost.
762 * We are called immediately after doing readdir(), and so we may
763 * already know the d_type of the file. We could for example avoid
764 * statting directories if we're not recursing, but this is not a very
765 * important case. Some systems may not have d_type.
767 struct file_struct *make_file(char *fname, struct file_list *flist,
770 static char *lastdir;
771 static int lastdir_len = -1;
772 struct file_struct *file;
774 char sum[SUM_LENGTH];
775 char thisname[MAXPATHLEN];
776 char linkname[MAXPATHLEN];
777 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
778 char *basename, *dirname, *bp;
779 unsigned short flags = 0;
781 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
784 if (strlcpy(thisname, fname, sizeof thisname)
785 >= sizeof thisname - flist_dir_len) {
786 rprintf(FINFO, "skipping overly long name: %s\n", fname);
789 clean_fname(thisname, 0);
791 sanitize_path(thisname, thisname, "", 0);
793 memset(sum, 0, SUM_LENGTH);
795 if (readlink_stat(thisname, &st, linkname) != 0) {
796 int save_errno = errno;
797 /* See if file is excluded before reporting an error. */
798 if (exclude_level != NO_EXCLUDES
799 && check_exclude_file(thisname, 0, exclude_level))
801 if (save_errno == ENOENT) {
803 /* Avoid "vanished" error if symlink points nowhere. */
804 if (copy_links && do_lstat(thisname, &st) == 0
805 && S_ISLNK(st.st_mode)) {
806 io_error |= IOERR_GENERAL;
807 rprintf(FERROR, "symlink has no referent: %s\n",
808 full_fname(thisname));
812 enum logcode c = am_daemon && protocol_version < 28
814 io_error |= IOERR_VANISHED;
815 rprintf(c, "file has vanished: %s\n",
816 full_fname(thisname));
819 io_error |= IOERR_GENERAL;
820 rsyserr(FERROR, save_errno, "readlink %s failed",
821 full_fname(thisname));
826 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
827 if (exclude_level == NO_EXCLUDES)
830 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
831 rprintf(FINFO, "skipping directory %s\n", thisname);
835 /* We only care about directories because we need to avoid recursing
836 * into a mount-point directory, not to avoid copying a symlinked
837 * file if -L (or similar) was specified. */
838 if (one_file_system && st.st_dev != filesystem_dev
839 && S_ISDIR(st.st_mode))
840 flags |= FLAG_MOUNT_POINT;
842 if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level))
845 if (lp_ignore_nonreadable(module_id)) {
847 if (!S_ISLNK(st.st_mode))
849 if (access(thisname, R_OK) != 0)
856 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
857 who_am_i(), thisname, exclude_level);
860 if ((basename = strrchr(thisname, '/')) != NULL) {
861 dirname_len = ++basename - thisname; /* counts future '\0' */
862 if (lastdir_len == dirname_len - 1
863 && strncmp(thisname, lastdir, lastdir_len) == 0) {
865 dirname_len = 0; /* indicates no copy is needed */
873 basename_len = strlen(basename) + 1; /* count the '\0' */
876 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
881 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
883 alloc_len = file_struct_len + dirname_len + basename_len
884 + linkname_len + sum_len;
886 bp = pool_alloc(flist->file_pool, alloc_len,
887 "receive_file_entry");
889 if (!(bp = new_array(char, alloc_len)))
890 out_of_memory("receive_file_entry");
893 file = (struct file_struct *)bp;
894 memset(bp, 0, file_struct_len);
895 bp += file_struct_len;
898 file->modtime = st.st_mtime;
899 file->length = st.st_size;
900 file->mode = st.st_mode;
901 file->uid = st.st_uid;
902 file->gid = st.st_gid;
904 #if SUPPORT_HARD_LINKS
905 if (flist && flist->hlink_pool) {
906 if (protocol_version < 28) {
907 if (S_ISREG(st.st_mode))
908 file->link_u.idev = pool_talloc(
909 flist->hlink_pool, struct idev, 1,
912 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
913 file->link_u.idev = pool_talloc(
914 flist->hlink_pool, struct idev, 1,
918 if (file->link_u.idev) {
919 file->F_DEV = st.st_dev;
920 file->F_INODE = st.st_ino;
925 file->dirname = lastdir = bp;
926 lastdir_len = dirname_len - 1;
927 memcpy(bp, dirname, dirname_len - 1);
931 file->dirname = dirname;
934 memcpy(bp, basename, basename_len);
937 #ifdef HAVE_STRUCT_STAT_ST_RDEV
938 if (preserve_devices && IS_DEVICE(st.st_mode))
939 file->u.rdev = st.st_rdev;
945 memcpy(bp, linkname, linkname_len);
952 file_checksum(thisname, bp, st.st_size);
956 file->basedir = flist_dir;
958 /* This code is only used by the receiver when it is building
959 * a list of files for a delete pass. */
960 if (keep_dirlinks && linkname_len && flist) {
962 int i = flist_find(received_flist, file);
963 if (i >= 0 && S_ISDIR(received_flist->files[i]->mode)
964 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
965 file->modtime = st2.st_mtime;
966 file->length = st2.st_size;
967 file->mode = st2.st_mode;
968 file->uid = st2.st_uid;
969 file->gid = st2.st_gid;
971 if (file->link_u.idev) {
972 pool_free(flist->hlink_pool, 0, file->link_u.idev);
973 file->link_u.idev = NULL;
978 if (!S_ISDIR(st.st_mode))
979 stats.total_size += st.st_size;
985 void send_file_name(int f, struct file_list *flist, char *fname,
986 int recursive, unsigned short base_flags)
988 struct file_struct *file;
989 char fbuf[MAXPATHLEN];
991 /* f is set to -1 when calculating deletion file list */
992 file = make_file(fname, flist,
993 f == -1 && delete_excluded? SERVER_EXCLUDES : ALL_EXCLUDES);
998 maybe_emit_filelist_progress(flist);
1000 flist_expand(flist);
1002 if (file->basename[0]) {
1003 flist->files[flist->count++] = file;
1004 send_file_entry(file, f, base_flags);
1007 if (recursive && S_ISDIR(file->mode)
1008 && !(file->flags & FLAG_MOUNT_POINT)) {
1009 struct exclude_list_struct last_list = local_exclude_list;
1010 local_exclude_list.head = local_exclude_list.tail = NULL;
1011 send_directory(f, flist, f_name_to(file, fbuf));
1013 rprintf(FINFO, "[%s] popping %sexclude list\n",
1014 who_am_i(), local_exclude_list.debug_type);
1016 clear_exclude_list(&local_exclude_list);
1017 local_exclude_list = last_list;
1022 /* Note that the "recurse" value either contains -1, for infinite recursion,
1023 * or a number >= 0 indicating how many levels of recursion we will allow. */
1024 static void send_directory(int f, struct file_list *flist, char *dir)
1028 char fname[MAXPATHLEN];
1029 unsigned int offset;
1034 io_error |= IOERR_GENERAL;
1035 rsyserr(FERROR, errno, "opendir %s failed", full_fname(dir));
1039 offset = strlcpy(fname, dir, MAXPATHLEN);
1041 if (offset >= MAXPATHLEN || p[-1] != '/') {
1042 if (offset >= MAXPATHLEN - 1) {
1043 io_error |= IOERR_GENERAL;
1044 rprintf(FERROR, "skipping long-named directory: %s\n",
1054 if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
1055 < MAXPATHLEN - offset) {
1056 add_exclude_file(&local_exclude_list, fname,
1057 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
1059 io_error |= IOERR_GENERAL;
1061 "cannot cvs-exclude in long-named directory %s\n",
1066 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1067 char *dname = d_name(di);
1068 if (dname[0] == '.' && (dname[1] == '\0'
1069 || (dname[1] == '.' && dname[2] == '\0')))
1071 if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset) {
1072 int do_subdirs = recurse >= 1 ? recurse-- : recurse;
1073 send_file_name(f, flist, fname, do_subdirs, 0);
1075 io_error |= IOERR_GENERAL;
1077 "cannot send long-named file %s\n",
1082 io_error |= IOERR_GENERAL;
1083 rsyserr(FERROR, errno, "readdir(%s)", dir);
1090 /* This function is normally called by the sender, but the receiving side
1091 * also uses it to construct one or more file lists if one of the --delete
1092 * options have been specified. The delete_in_dir() function sets f to -1
1093 * so that we just construct the file list in memory without sending it
1094 * over the wire. It also has the side-effect of ignoring user-excludes if
1095 * delete_excluded is set (so that the delete list includes user-excluded
1096 * files) and it avoids some per-arg init code for limited recursion (since
1097 * delete_in_dir() sets recurse before calling this function). */
1098 struct file_list *send_file_list(int f, int argc, char *argv[])
1102 char *p, *dir, olddir[sizeof curr_dir];
1103 char lastpath[MAXPATHLEN] = "";
1104 struct file_list *flist;
1108 if (show_filelist_p() && f != -1)
1109 start_filelist_progress("building file list");
1111 start_write = stats.total_written;
1113 flist = flist_new(f == -1 ? WITHOUT_HLINK : WITH_HLINK,
1117 io_start_buffering_out();
1118 if (filesfrom_fd >= 0) {
1119 if (argv[0] && !push_dir(argv[0])) {
1120 rsyserr(FERROR, errno, "push_dir %s failed",
1121 full_fname(argv[0]));
1122 exit_cleanup(RERR_FILESELECT);
1129 char fname2[MAXPATHLEN];
1130 char *fname = fname2;
1134 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1136 sanitize_path(fname, fname, "", 0);
1140 strlcpy(fname, *argv++, MAXPATHLEN);
1142 sanitize_path(fname, fname, "", 0);
1146 if (!l || fname[l - 1] == '/') {
1147 if (l == 2 && fname[0] == '.') {
1148 /* Turn "./" into just "." rather than "./." */
1150 } else if (l < MAXPATHLEN) {
1156 ; /* recurse is pre-set */
1157 else if (fname[l-1] == '.' && (l == 1 || fname[l-2] == '/')) {
1158 if (!recurse && xfer_dirs)
1159 recurse = 1; /* allow one level */
1160 } else if (recurse > 0)
1163 if (link_stat(fname, &st, keep_dirlinks) != 0) {
1165 io_error |= IOERR_GENERAL;
1166 rsyserr(FERROR, errno, "link_stat %s failed",
1172 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1173 rprintf(FINFO, "skipping directory %s\n", fname);
1180 if (!relative_paths) {
1181 p = strrchr(fname, '/');
1190 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1191 /* this ensures we send the intermediate directories,
1192 thus getting their permissions right */
1193 char *lp = lastpath, *fn = fname, *slash = fname;
1195 /* Skip any initial directories in our path that we
1196 * have in common with lastpath. */
1197 while (*fn && *lp == *fn) {
1203 if (fn != p || (*lp && *lp != '/')) {
1204 int save_copy_links = copy_links;
1205 int save_xfer_dirs = xfer_dirs;
1206 copy_links = copy_unsafe_links;
1208 while ((slash = strchr(slash+1, '/')) != 0) {
1210 send_file_name(f, flist, fname, 0, 0);
1213 copy_links = save_copy_links;
1214 xfer_dirs = save_xfer_dirs;
1216 strlcpy(lastpath, fname, sizeof lastpath);
1225 static char *lastdir;
1226 static int lastdir_len;
1228 strcpy(olddir, curr_dir); /* can't overflow */
1230 if (!push_dir(dir)) {
1231 io_error |= IOERR_GENERAL;
1232 rsyserr(FERROR, errno, "push_dir %s failed",
1237 if (lastdir && strcmp(lastdir, dir) == 0) {
1238 flist_dir = lastdir;
1239 flist_dir_len = lastdir_len;
1241 flist_dir = lastdir = strdup(dir);
1242 flist_dir_len = lastdir_len = strlen(dir);
1246 if (one_file_system)
1247 set_filesystem(fname);
1249 do_subdirs = recurse >= 1 ? recurse-- : recurse;
1250 send_file_name(f, flist, fname, do_subdirs, XMIT_DEL_START);
1255 if (!pop_dir(olddir)) {
1256 rsyserr(FERROR, errno, "pop_dir %s failed",
1258 exit_cleanup(RERR_FILESELECT);
1264 send_file_entry(NULL, f, 0);
1266 if (show_filelist_p())
1267 finish_filelist_progress(flist);
1270 if (flist->hlink_pool) {
1271 pool_destroy(flist->hlink_pool);
1272 flist->hlink_pool = NULL;
1275 clean_flist(flist, 0, 0);
1278 /* Now send the uid/gid list. This was introduced in
1279 * protocol version 15 */
1282 /* send the io_error flag */
1283 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1286 stats.flist_size = stats.total_written - start_write;
1287 stats.num_files = flist->count;
1291 output_flist(flist, f < 0 ? "delete" : who_am_i());
1294 rprintf(FINFO, "send_file_list done\n");
1300 struct file_list *recv_file_list(int f)
1302 struct file_list *flist;
1303 unsigned short flags;
1306 if (show_filelist_p())
1307 start_filelist_progress("receiving file list");
1309 start_read = stats.total_read;
1311 flist = flist_new(WITH_HLINK, "recv_file_list");
1312 received_flist = flist;
1315 flist->malloced = 1000;
1316 flist->files = new_array(struct file_struct *, flist->malloced);
1321 while ((flags = read_byte(f)) != 0) {
1322 int i = flist->count;
1324 flist_expand(flist);
1326 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1327 flags |= read_byte(f) << 8;
1328 receive_file_entry(&flist->files[i], flags, flist, f);
1330 if (S_ISREG(flist->files[i]->mode))
1331 stats.total_size += flist->files[i]->length;
1335 maybe_emit_filelist_progress(flist);
1338 rprintf(FINFO, "recv_file_name(%s)\n",
1339 f_name(flist->files[i]));
1342 receive_file_entry(NULL, 0, NULL, 0); /* Signal that we're done. */
1345 rprintf(FINFO, "received %d names\n", flist->count);
1347 if (show_filelist_p())
1348 finish_filelist_progress(flist);
1350 clean_flist(flist, relative_paths, 1);
1353 /* Now send the uid/gid list. This was introduced in
1354 * protocol version 15 */
1355 recv_uid_list(f, flist);
1357 /* Recv the io_error flag */
1358 if (lp_ignore_errors(module_id) || ignore_errors)
1361 io_error |= read_int(f);
1365 output_flist(flist, who_am_i());
1369 for (i = 0; i < flist->count; i++)
1370 list_file_entry(flist->files[i]);
1374 rprintf(FINFO, "recv_file_list done\n");
1376 stats.flist_size = stats.total_read - start_read;
1377 stats.num_files = flist->count;
1382 out_of_memory("recv_file_list");
1383 return NULL; /* not reached */
1387 int file_compare(struct file_struct **file1, struct file_struct **file2)
1389 struct file_struct *f1 = *file1;
1390 struct file_struct *f2 = *file2;
1392 if (!f1->basename && !f2->basename)
1398 if (f1->dirname == f2->dirname)
1399 return u_strcmp(f1->basename, f2->basename);
1400 return f_name_cmp(f1, f2);
1404 int flist_find(struct file_list *flist, struct file_struct *f)
1406 int low = 0, high = flist->count - 1;
1408 while (high >= 0 && !flist->files[high]->basename) high--;
1413 while (low != high) {
1414 int mid = (low + high) / 2;
1415 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1417 return flist_up(flist, mid);
1424 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1425 return flist_up(flist, low);
1430 * Free up any resources a file_struct has allocated
1431 * and clear the file.
1433 void clear_file(int i, struct file_list *flist)
1435 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1436 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1437 memset(flist->files[i], 0, file_struct_len);
1442 * allocate a new file list
1444 struct file_list *flist_new(int with_hlink, char *msg)
1446 struct file_list *flist;
1448 flist = new(struct file_list);
1452 memset(flist, 0, sizeof (struct file_list));
1454 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1455 out_of_memory, POOL_INTERN)))
1458 #if SUPPORT_HARD_LINKS
1459 if (with_hlink && preserve_hard_links) {
1460 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1461 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1470 * free up all elements in a flist
1472 void flist_free(struct file_list *flist)
1474 pool_destroy(flist->file_pool);
1475 pool_destroy(flist->hlink_pool);
1482 * This routine ensures we don't have any duplicate names in our file list.
1483 * duplicate names can cause corruption because of the pipelining
1485 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1489 if (!flist || flist->count == 0)
1492 qsort(flist->files, flist->count,
1493 sizeof flist->files[0], (int (*)())file_compare);
1495 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1496 if (flist->files[i]->basename) {
1501 while (++i < flist->count) {
1502 if (!flist->files[i]->basename)
1504 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1505 if (verbose > 1 && !am_server) {
1507 "removing duplicate name %s from file list %d\n",
1508 f_name(flist->files[i]), i);
1510 /* Make sure that if we unduplicate '.', that we don't
1511 * lose track of a user-specified starting point (or
1512 * else deletions will mysteriously fail with -R). */
1513 if (flist->files[i]->flags & FLAG_DEL_START)
1514 flist->files[prev_i]->flags |= FLAG_DEL_START;
1516 clear_file(i, flist);
1522 /* we need to strip off the root directory in the case
1523 of relative paths, but this must be done _after_
1524 the sorting phase */
1525 for (i = 0; i < flist->count; i++) {
1526 if (flist->files[i]->dirname &&
1527 flist->files[i]->dirname[0] == '/') {
1528 memmove(&flist->files[i]->dirname[0],
1529 &flist->files[i]->dirname[1],
1530 strlen(flist->files[i]->dirname));
1533 if (flist->files[i]->dirname &&
1534 !flist->files[i]->dirname[0]) {
1535 flist->files[i]->dirname = NULL;
1541 static void output_flist(struct file_list *flist, const char *whose_list)
1543 char uidbuf[16], gidbuf[16];
1544 struct file_struct *file;
1547 for (i = 0; i < flist->count; i++) {
1548 file = flist->files[i];
1549 if ((am_root || am_sender) && preserve_uid)
1550 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1553 if (preserve_gid && file->gid != GID_NONE)
1554 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1557 rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f%s%s\n",
1558 whose_list, i, NS(file->basedir), NS(file->dirname),
1559 NS(file->basename), (int)file->mode,
1560 (double)file->length, uidbuf, gidbuf);
1565 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1567 /* Compare the names of two file_struct entities, just like strcmp()
1568 * would do if it were operating on the joined strings. We assume
1569 * that there are no 0-length strings.
1571 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1574 const uchar *c1, *c2;
1575 enum fnc_state state1, state2;
1577 if (!f1 || !f1->basename) {
1578 if (!f2 || !f2->basename)
1582 if (!f2 || !f2->basename)
1585 if (!(c1 = (uchar*)f1->dirname)) {
1587 c1 = (uchar*)f1->basename;
1593 if (!(c2 = (uchar*)f2->dirname)) {
1595 c2 = (uchar*)f2->basename;
1603 if ((dif = (int)*c1 - (int)*c2) != 0)
1613 c1 = (uchar*)f1->basename;
1627 c2 = (uchar*)f2->basename;
1641 /* Return a copy of the full filename of a flist entry, using the indicated
1642 * buffer. No size-checking is done because we checked the size when creating
1643 * the file_struct entry.
1645 char *f_name_to(struct file_struct *f, char *fbuf)
1647 if (!f || !f->basename)
1651 int len = strlen(f->dirname);
1652 memcpy(fbuf, f->dirname, len);
1654 strcpy(fbuf + len + 1, f->basename);
1656 strcpy(fbuf, f->basename);
1661 /* Like f_name_to(), but we rotate through 5 static buffers of our own.
1663 char *f_name(struct file_struct *f)
1665 static char names[5][MAXPATHLEN];
1666 static unsigned int n;
1668 n = (n + 1) % (sizeof names / sizeof names[0]);
1670 return f_name_to(f, names[n]);
1673 static int is_backup_file(char *fn)
1675 int k = strlen(fn) - backup_suffix_len;
1676 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
1679 void delete_in_dir(struct file_list *flist, char *fname)
1681 static int deletion_count = 0;
1682 struct file_list *del_flist;
1683 int save_recurse = recurse;
1684 int save_xfer_dirs = xfer_dirs;
1685 int save_implied_dirs = implied_dirs;
1686 int save_relative_paths = relative_paths;
1690 if (max_delete && deletion_count >= max_delete)
1693 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
1694 rprintf(FINFO, "IO error encountered - skipping file deletion\n");
1695 max_delete = -1; /* avoid duplicating the above warning */
1699 if (delete_during) {
1700 recurse = 1; /* allow one level only */
1707 del_flist = send_file_list(-1, 1, argv);
1709 relative_paths = save_relative_paths;
1710 implied_dirs = save_implied_dirs;
1711 xfer_dirs = save_xfer_dirs;
1712 recurse = save_recurse;
1718 rprintf(FINFO, "deleting in %s\n", safe_fname(fname));
1720 for (i = del_flist->count-1; i >= 0; i--) {
1721 if (max_delete && deletion_count >= max_delete)
1723 if (!del_flist->files[i]->basename)
1725 mode = del_flist->files[i]->mode;
1726 if ((j = flist_find(flist, del_flist->files[i])) < 0
1727 || (delete_during && S_ISDIR(mode)
1728 && !S_ISDIR(flist->files[j]->mode))) {
1729 char *f = f_name(del_flist->files[i]);
1730 if (make_backups && (backup_dir || !is_backup_file(f))
1731 && !S_ISDIR(mode)) {
1734 rprintf(FINFO, "deleting %s\n",
1738 delete_file(f, S_ISDIR(mode)
1739 ? DEL_DIR | DEL_RECURSE : 0);
1744 flist_free(del_flist);