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;
46 extern char curr_dir[MAXPATHLEN];
47 extern unsigned int curr_dir_len;
48 extern char *backup_dir;
49 extern char *backup_suffix;
50 extern int filesfrom_fd;
52 extern int one_file_system;
53 extern int keep_dirlinks;
54 extern int preserve_links;
55 extern int preserve_hard_links;
56 extern int preserve_perms;
57 extern int preserve_devices;
58 extern int preserve_uid;
59 extern int preserve_gid;
60 extern int relative_paths;
61 extern int implied_dirs;
62 extern int make_backups;
63 extern int backup_suffix_len;
64 extern int copy_links;
65 extern int copy_unsafe_links;
66 extern int protocol_version;
67 extern int sanitize_paths;
68 extern int delete_excluded;
69 extern int max_delete;
70 extern int orig_umask;
73 extern struct filter_list_struct filter_list;
74 extern struct filter_list_struct server_filter_list;
78 static char empty_sum[MD4_SUM_LENGTH];
79 static unsigned int file_struct_len;
80 static struct file_list *received_flist;
81 static dev_t filesystem_dev; /* used to implement -x */
82 static int deletion_count = 0; /* used to implement --max-delete */
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);
225 /* This function is used to check if a file should be included/excluded
226 * from the list of files based on its name and type etc. The value of
227 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
228 static int is_excluded(char *fname, int is_dir, int filter_level)
230 #if 0 /* This currently never happens, so avoid a useless compare. */
231 if (filter_level == NO_FILTERS)
235 /* never exclude '.', even if somebody does --exclude '*' */
236 if (fname[0] == '.' && !fname[1])
238 /* Handle the -R version of the '.' dir. */
239 if (fname[0] == '/') {
240 int len = strlen(fname);
241 if (fname[len-1] == '.' && fname[len-2] == '/')
245 if (server_filter_list.head
246 && check_filter(&server_filter_list, fname, is_dir) < 0)
248 if (filter_level != ALL_FILTERS)
251 && check_filter(&filter_list, fname, is_dir) < 0)
256 static int to_wire_mode(mode_t mode)
259 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
260 return (mode & ~(_S_IFMT)) | 0120000;
265 static mode_t from_wire_mode(int mode)
267 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
268 return (mode & ~(_S_IFMT)) | _S_IFLNK;
273 static void send_directory(int f, struct file_list *flist, char *dir);
275 static char *flist_dir;
276 static int flist_dir_len;
280 * Make sure @p flist is big enough to hold at least @p flist->count
283 void flist_expand(struct file_list *flist)
285 struct file_struct **new_ptr;
287 if (flist->count < flist->malloced)
290 if (flist->malloced < FLIST_START)
291 flist->malloced = FLIST_START;
292 else if (flist->malloced >= FLIST_LINEAR)
293 flist->malloced += FLIST_LINEAR;
295 flist->malloced *= 2;
298 * In case count jumped or we are starting the list
299 * with a known size just set it.
301 if (flist->malloced < flist->count)
302 flist->malloced = flist->count;
304 new_ptr = realloc_array(flist->files, struct file_struct *,
307 if (verbose >= 2 && flist->malloced != FLIST_START) {
308 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
310 (double)sizeof flist->files[0] * flist->malloced,
311 (new_ptr == flist->files) ? " not" : "");
314 flist->files = new_ptr;
317 out_of_memory("flist_expand");
320 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
322 unsigned short flags;
323 static time_t modtime;
327 static uint32 rdev_major;
330 static char lastname[MAXPATHLEN];
331 char fname[MAXPATHLEN];
339 modtime = 0, mode = 0;
340 dev = 0, rdev = makedev(0, 0);
347 io_write_phase = "send_file_entry";
349 f_name_to(file, fname);
353 if (file->mode == mode)
354 flags |= XMIT_SAME_MODE;
357 if (preserve_devices) {
358 if (protocol_version < 28) {
359 if (IS_DEVICE(mode)) {
360 if (file->u.rdev == rdev)
361 flags |= XMIT_SAME_RDEV_pre28;
365 rdev = makedev(0, 0);
366 } else if (IS_DEVICE(mode)) {
368 if ((uint32)major(rdev) == rdev_major)
369 flags |= XMIT_SAME_RDEV_MAJOR;
371 rdev_major = major(rdev);
372 if ((uint32)minor(rdev) <= 0xFFu)
373 flags |= XMIT_RDEV_MINOR_IS_SMALL;
376 if (file->uid == uid)
377 flags |= XMIT_SAME_UID;
380 if (file->gid == gid)
381 flags |= XMIT_SAME_GID;
384 if (file->modtime == modtime)
385 flags |= XMIT_SAME_TIME;
387 modtime = file->modtime;
389 #if SUPPORT_HARD_LINKS
390 if (file->link_u.idev) {
391 if (file->F_DEV == dev) {
392 if (protocol_version >= 28)
393 flags |= XMIT_SAME_DEV;
396 flags |= XMIT_HAS_IDEV_DATA;
401 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
403 l2 = strlen(fname+l1);
406 flags |= XMIT_SAME_NAME;
408 flags |= XMIT_LONG_NAME;
410 /* We must make sure we don't send a zero flag byte or the
411 * other end will terminate the flist transfer. Note that
412 * the use of XMIT_DEL_START on a non-dir has no meaning, so
413 * it's harmless way to add a bit to the first flag byte. */
414 if (protocol_version >= 28) {
415 if (!flags && !S_ISDIR(mode))
416 flags |= XMIT_DEL_START;
417 if ((flags & 0xFF00) || !flags) {
418 flags |= XMIT_EXTENDED_FLAGS;
419 write_byte(f, flags);
420 write_byte(f, flags >> 8);
422 write_byte(f, flags);
424 if (!(flags & 0xFF) && !S_ISDIR(mode))
425 flags |= XMIT_DEL_START;
427 flags |= XMIT_LONG_NAME;
428 write_byte(f, flags);
430 if (flags & XMIT_SAME_NAME)
432 if (flags & XMIT_LONG_NAME)
436 write_buf(f, fname + l1, l2);
438 write_longint(f, file->length);
439 if (!(flags & XMIT_SAME_TIME))
440 write_int(f, modtime);
441 if (!(flags & XMIT_SAME_MODE))
442 write_int(f, to_wire_mode(mode));
443 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
448 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
453 if (preserve_devices && IS_DEVICE(mode)) {
454 if (protocol_version < 28) {
455 if (!(flags & XMIT_SAME_RDEV_pre28))
456 write_int(f, (int)rdev);
458 if (!(flags & XMIT_SAME_RDEV_MAJOR))
459 write_int(f, major(rdev));
460 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
461 write_byte(f, minor(rdev));
463 write_int(f, minor(rdev));
468 if (preserve_links && S_ISLNK(mode)) {
469 int len = strlen(file->u.link);
471 write_buf(f, file->u.link, len);
475 #if SUPPORT_HARD_LINKS
476 if (flags & XMIT_HAS_IDEV_DATA) {
477 if (protocol_version < 26) {
478 /* 32-bit dev_t and ino_t */
480 write_int(f, file->F_INODE);
482 /* 64-bit dev_t and ino_t */
483 if (!(flags & XMIT_SAME_DEV))
484 write_longint(f, dev);
485 write_longint(f, file->F_INODE);
490 if (always_checksum) {
494 else if (protocol_version < 28) {
495 /* Prior to 28, we sent a useless set of nulls. */
501 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
505 strlcpy(lastname, fname, MAXPATHLEN);
507 io_write_phase = "unknown";
512 void receive_file_entry(struct file_struct **fptr, unsigned short flags,
513 struct file_list *flist, int f)
515 static time_t modtime;
519 static uint32 rdev_major;
522 static char lastname[MAXPATHLEN], *lastdir;
523 static int lastdir_depth, lastdir_len = -1;
524 static unsigned int del_heir_name_len = 0;
525 static int in_del_hier = 0;
526 char thisname[MAXPATHLEN];
527 unsigned int l1 = 0, l2 = 0;
528 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
530 char *basename, *dirname, *bp;
531 struct file_struct *file;
534 modtime = 0, mode = 0;
535 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, 0);
568 sanitize_path(thisname, thisname, "", 0);
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;
639 file->modtime = modtime;
640 file->length = file_length;
646 if (flags & XMIT_DEL_START) {
648 del_heir_name_len = l1 + l2;
649 file->flags |= FLAG_DEL_START;
650 } else if (delete_during && in_del_hier) {
651 if (!relative_paths || (l1 >= del_heir_name_len
652 && thisname[del_heir_name_len] == '/'))
653 file->flags |= FLAG_DEL_START;
660 file->dirname = lastdir = bp;
661 lastdir_len = dirname_len - 1;
662 memcpy(bp, dirname, dirname_len - 1);
666 lastdir_depth = count_dir_elements(lastdir);
668 file->dirname = dirname;
671 memcpy(bp, basename, basename_len);
674 if (preserve_devices && IS_DEVICE(mode))
680 read_sbuf(f, bp, linkname_len - 1);
682 sanitize_path(bp, bp, "", lastdir_depth);
687 #if SUPPORT_HARD_LINKS
688 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
689 flags |= XMIT_HAS_IDEV_DATA;
690 if (flags & XMIT_HAS_IDEV_DATA) {
692 if (protocol_version < 26) {
696 if (!(flags & XMIT_SAME_DEV))
697 dev = read_longint(f);
698 inode = read_longint(f);
700 if (flist->hlink_pool) {
701 file->link_u.idev = pool_talloc(flist->hlink_pool,
702 struct idev, 1, "inode_table");
703 file->F_INODE = inode;
709 if (always_checksum) {
712 file->u.sum = sum = bp;
714 } else if (protocol_version < 28) {
715 /* Prior to 28, we get a useless set of nulls. */
721 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
725 if (!preserve_perms) {
726 /* set an appropriate set of permissions based on original
727 * permissions and umask. This emulates what GNU cp does */
728 file->mode &= ~orig_umask;
734 * Create a file_struct for a named file by reading its stat()
735 * information and performing extensive checks against global
738 * @return the new file, or NULL if there was an error or this file
739 * should be excluded.
741 * @todo There is a small optimization opportunity here to avoid
742 * stat()ing the file in some circumstances, which has a certain cost.
743 * We are called immediately after doing readdir(), and so we may
744 * already know the d_type of the file. We could for example avoid
745 * statting directories if we're not recursing, but this is not a very
746 * important case. Some systems may not have d_type.
748 struct file_struct *make_file(char *fname, struct file_list *flist,
751 static char *lastdir;
752 static int lastdir_len = -1;
753 struct file_struct *file;
755 char sum[SUM_LENGTH];
756 char thisname[MAXPATHLEN];
757 char linkname[MAXPATHLEN];
758 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
759 char *basename, *dirname, *bp;
760 unsigned short flags = 0;
762 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
765 if (strlcpy(thisname, fname, sizeof thisname)
766 >= sizeof thisname - flist_dir_len) {
767 rprintf(FINFO, "skipping overly long name: %s\n", fname);
770 clean_fname(thisname, 0);
772 sanitize_path(thisname, thisname, "", 0);
774 memset(sum, 0, SUM_LENGTH);
776 if (readlink_stat(thisname, &st, linkname) != 0) {
777 int save_errno = errno;
778 /* See if file is excluded before reporting an error. */
779 if (filter_level != NO_FILTERS
780 && is_excluded(thisname, 0, filter_level))
782 if (save_errno == ENOENT) {
784 /* Avoid "vanished" error if symlink points nowhere. */
785 if (copy_links && do_lstat(thisname, &st) == 0
786 && S_ISLNK(st.st_mode)) {
787 io_error |= IOERR_GENERAL;
788 rprintf(FERROR, "symlink has no referent: %s\n",
789 full_fname(thisname));
793 enum logcode c = am_daemon && protocol_version < 28
795 io_error |= IOERR_VANISHED;
796 rprintf(c, "file has vanished: %s\n",
797 full_fname(thisname));
800 io_error |= IOERR_GENERAL;
801 rsyserr(FERROR, save_errno, "readlink %s failed",
802 full_fname(thisname));
807 /* backup.c calls us with filter_level set to NO_FILTERS. */
808 if (filter_level == NO_FILTERS)
811 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
812 rprintf(FINFO, "skipping directory %s\n", thisname);
816 /* We only care about directories because we need to avoid recursing
817 * into a mount-point directory, not to avoid copying a symlinked
818 * file if -L (or similar) was specified. */
819 if (one_file_system && st.st_dev != filesystem_dev
820 && S_ISDIR(st.st_mode))
821 flags |= FLAG_MOUNT_POINT;
823 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
826 if (lp_ignore_nonreadable(module_id)) {
828 if (!S_ISLNK(st.st_mode))
830 if (access(thisname, R_OK) != 0)
837 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
838 who_am_i(), thisname, filter_level);
841 if ((basename = strrchr(thisname, '/')) != NULL) {
842 dirname_len = ++basename - thisname; /* counts future '\0' */
843 if (lastdir_len == dirname_len - 1
844 && strncmp(thisname, lastdir, lastdir_len) == 0) {
846 dirname_len = 0; /* indicates no copy is needed */
854 basename_len = strlen(basename) + 1; /* count the '\0' */
857 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
862 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
864 alloc_len = file_struct_len + dirname_len + basename_len
865 + linkname_len + sum_len;
867 bp = pool_alloc(flist->file_pool, alloc_len,
868 "receive_file_entry");
870 if (!(bp = new_array(char, alloc_len)))
871 out_of_memory("receive_file_entry");
874 file = (struct file_struct *)bp;
875 memset(bp, 0, file_struct_len);
876 bp += file_struct_len;
879 file->modtime = st.st_mtime;
880 file->length = st.st_size;
881 file->mode = st.st_mode;
882 file->uid = st.st_uid;
883 file->gid = st.st_gid;
885 #if SUPPORT_HARD_LINKS
886 if (flist && flist->hlink_pool) {
887 if (protocol_version < 28) {
888 if (S_ISREG(st.st_mode))
889 file->link_u.idev = pool_talloc(
890 flist->hlink_pool, struct idev, 1,
893 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
894 file->link_u.idev = pool_talloc(
895 flist->hlink_pool, struct idev, 1,
899 if (file->link_u.idev) {
900 file->F_DEV = st.st_dev;
901 file->F_INODE = st.st_ino;
906 file->dirname = lastdir = bp;
907 lastdir_len = dirname_len - 1;
908 memcpy(bp, dirname, dirname_len - 1);
912 file->dirname = dirname;
915 memcpy(bp, basename, basename_len);
918 #if HAVE_STRUCT_STAT_ST_RDEV
919 if (preserve_devices && IS_DEVICE(st.st_mode))
920 file->u.rdev = st.st_rdev;
926 memcpy(bp, linkname, linkname_len);
933 file_checksum(thisname, bp, st.st_size);
937 file->basedir = flist_dir;
939 /* This code is only used by the receiver when it is building
940 * a list of files for a delete pass. */
941 if (keep_dirlinks && linkname_len && flist) {
943 int i = flist_find(received_flist, file);
944 if (i >= 0 && S_ISDIR(received_flist->files[i]->mode)
945 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
946 file->modtime = st2.st_mtime;
947 file->length = st2.st_size;
948 file->mode = st2.st_mode;
949 file->uid = st2.st_uid;
950 file->gid = st2.st_gid;
952 if (file->link_u.idev) {
953 pool_free(flist->hlink_pool, 0, file->link_u.idev);
954 file->link_u.idev = NULL;
959 if (!S_ISDIR(st.st_mode))
960 stats.total_size += st.st_size;
966 void send_file_name(int f, struct file_list *flist, char *fname,
967 int recursive, unsigned short base_flags)
969 struct file_struct *file;
970 char fbuf[MAXPATHLEN];
972 /* f is set to -1 when calculating deletion file list */
973 file = make_file(fname, flist,
974 f == -1 && delete_excluded? SERVER_FILTERS : ALL_FILTERS);
979 maybe_emit_filelist_progress(flist);
983 if (file->basename[0]) {
984 flist->files[flist->count++] = file;
985 send_file_entry(file, f, base_flags);
988 if (recursive && S_ISDIR(file->mode)
989 && !(file->flags & FLAG_MOUNT_POINT)) {
990 send_directory(f, flist, f_name_to(file, fbuf));
995 /* Note that the "recurse" value either contains -1, for infinite recursion,
996 * or a number >= 0 indicating how many levels of recursion we will allow. */
997 static void send_directory(int f, struct file_list *flist, char *dir)
1001 char fname[MAXPATHLEN];
1002 unsigned int offset;
1008 io_error |= IOERR_GENERAL;
1009 rsyserr(FERROR, errno, "opendir %s failed", full_fname(dir));
1013 offset = strlcpy(fname, dir, MAXPATHLEN);
1015 if (offset >= MAXPATHLEN || p[-1] != '/') {
1016 if (offset >= MAXPATHLEN - 1) {
1017 io_error |= IOERR_GENERAL;
1018 rprintf(FERROR, "skipping long-named directory: %s\n",
1027 save_filters = push_local_filters(fname, offset);
1029 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1030 char *dname = d_name(di);
1031 if (dname[0] == '.' && (dname[1] == '\0'
1032 || (dname[1] == '.' && dname[2] == '\0')))
1034 if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset) {
1035 int do_subdirs = recurse >= 1 ? recurse-- : recurse;
1036 send_file_name(f, flist, fname, do_subdirs, 0);
1038 io_error |= IOERR_GENERAL;
1040 "cannot send long-named file %s\n",
1045 io_error |= IOERR_GENERAL;
1046 rsyserr(FERROR, errno, "readdir(%s)", dir);
1049 pop_local_filters(save_filters);
1055 /* This function is normally called by the sender, but the receiving side
1056 * also uses it to construct one or more file lists if one of the --delete
1057 * options have been specified. The delete_files() function sets f to -1
1058 * so that we just construct the file list in memory without sending it
1059 * over the wire. It also has the side-effect of ignoring user-excludes if
1060 * delete_excluded is set (so that the delete list includes user-excluded
1062 struct file_list *send_file_list(int f, int argc, char *argv[])
1066 char *p, *dir, olddir[sizeof curr_dir];
1067 char lastpath[MAXPATHLEN] = "";
1068 struct file_list *flist;
1069 BOOL need_first_push = True;
1070 struct timeval start_tv, end_tv;
1074 if (show_filelist_p() && f != -1)
1075 start_filelist_progress("building file list");
1077 start_write = stats.total_written;
1078 gettimeofday(&start_tv, NULL);
1080 flist = flist_new(f == -1 ? WITHOUT_HLINK : WITH_HLINK,
1084 io_start_buffering_out();
1085 if (filesfrom_fd >= 0) {
1086 if (argv[0] && !push_dir(argv[0])) {
1087 rsyserr(FERROR, errno, "push_dir %s failed",
1088 full_fname(argv[0]));
1089 exit_cleanup(RERR_FILESELECT);
1092 if (curr_dir_len < MAXPATHLEN - 1) {
1093 push_local_filters(curr_dir, curr_dir_len);
1094 need_first_push = False;
1100 char fname2[MAXPATHLEN];
1101 char *fname = fname2;
1105 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1107 sanitize_path(fname, fname, "", 0);
1111 strlcpy(fname, *argv++, MAXPATHLEN);
1113 sanitize_path(fname, fname, "", 0);
1117 if (!l || fname[l - 1] == '/') {
1118 if (l == 2 && fname[0] == '.') {
1119 /* Turn "./" into just "." rather than "./." */
1121 } else if (l < MAXPATHLEN) {
1126 if (fname[l-1] == '.' && (l == 1 || fname[l-2] == '/')) {
1127 if (!recurse && xfer_dirs)
1128 recurse = 1; /* allow one level */
1129 } else if (recurse > 0)
1132 if (need_first_push) {
1133 if ((p = strrchr(fname, '/')) != NULL) {
1134 if (*++p && strcmp(p, ".") != 0)
1135 push_local_filters(fname, p - fname);
1136 } else if (strcmp(fname, ".") != 0)
1137 push_local_filters(fname, 0);
1138 need_first_push = False;
1141 if (link_stat(fname, &st, keep_dirlinks) != 0) {
1143 io_error |= IOERR_GENERAL;
1144 rsyserr(FERROR, errno, "link_stat %s failed",
1150 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1151 rprintf(FINFO, "skipping directory %s\n", fname);
1158 if (!relative_paths) {
1159 p = strrchr(fname, '/');
1168 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1169 /* this ensures we send the intermediate directories,
1170 thus getting their permissions right */
1171 char *lp = lastpath, *fn = fname, *slash = fname;
1173 /* Skip any initial directories in our path that we
1174 * have in common with lastpath. */
1175 while (*fn && *lp == *fn) {
1181 if (fn != p || (*lp && *lp != '/')) {
1182 int save_copy_links = copy_links;
1183 int save_xfer_dirs = xfer_dirs;
1184 copy_links = copy_unsafe_links;
1186 while ((slash = strchr(slash+1, '/')) != 0) {
1188 send_file_name(f, flist, fname, 0, 0);
1191 copy_links = save_copy_links;
1192 xfer_dirs = save_xfer_dirs;
1194 strlcpy(lastpath, fname, sizeof lastpath);
1203 static char *lastdir;
1204 static int lastdir_len;
1206 strcpy(olddir, curr_dir); /* can't overflow */
1208 if (!push_dir(dir)) {
1209 io_error |= IOERR_GENERAL;
1210 rsyserr(FERROR, errno, "push_dir %s failed",
1215 if (lastdir && strcmp(lastdir, dir) == 0) {
1216 flist_dir = lastdir;
1217 flist_dir_len = lastdir_len;
1219 flist_dir = lastdir = strdup(dir);
1220 flist_dir_len = lastdir_len = strlen(dir);
1224 if (one_file_system)
1225 filesystem_dev = st.st_dev;
1227 do_subdirs = recurse >= 1 ? recurse-- : recurse;
1228 send_file_name(f, flist, fname, do_subdirs, XMIT_DEL_START);
1233 if (!pop_dir(olddir)) {
1234 rsyserr(FERROR, errno, "pop_dir %s failed",
1236 exit_cleanup(RERR_FILESELECT);
1242 gettimeofday(&end_tv, NULL);
1243 stats.flist_buildtime =
1244 (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1245 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1246 if (stats.flist_buildtime == 0)
1247 stats.flist_buildtime = 1;
1250 send_file_entry(NULL, f, 0);
1252 if (show_filelist_p())
1253 finish_filelist_progress(flist);
1255 gettimeofday(&end_tv, NULL);
1256 stats.flist_xfertime =
1257 (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1258 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1261 if (flist->hlink_pool) {
1262 pool_destroy(flist->hlink_pool);
1263 flist->hlink_pool = NULL;
1266 clean_flist(flist, 0, 0);
1269 /* Now send the uid/gid list. This was introduced in
1270 * protocol version 15 */
1273 /* send the io_error flag */
1274 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1277 stats.flist_size = stats.total_written - start_write;
1278 stats.num_files = flist->count;
1282 output_flist(flist, f < 0 ? "delete" : who_am_i());
1285 rprintf(FINFO, "send_file_list done\n");
1291 struct file_list *recv_file_list(int f)
1293 struct file_list *flist;
1294 unsigned short flags;
1297 if (show_filelist_p())
1298 start_filelist_progress("receiving file list");
1300 start_read = stats.total_read;
1302 flist = flist_new(WITH_HLINK, "recv_file_list");
1303 received_flist = flist;
1306 flist->malloced = 1000;
1307 flist->files = new_array(struct file_struct *, flist->malloced);
1312 while ((flags = read_byte(f)) != 0) {
1313 int i = flist->count;
1315 flist_expand(flist);
1317 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1318 flags |= read_byte(f) << 8;
1319 receive_file_entry(&flist->files[i], flags, flist, f);
1321 if (S_ISREG(flist->files[i]->mode))
1322 stats.total_size += flist->files[i]->length;
1326 maybe_emit_filelist_progress(flist);
1329 rprintf(FINFO, "recv_file_name(%s)\n",
1330 f_name(flist->files[i]));
1333 receive_file_entry(NULL, 0, NULL, 0); /* Signal that we're done. */
1336 rprintf(FINFO, "received %d names\n", flist->count);
1338 if (show_filelist_p())
1339 finish_filelist_progress(flist);
1341 clean_flist(flist, relative_paths, 1);
1344 /* Now send the uid/gid list. This was introduced in
1345 * protocol version 15 */
1346 recv_uid_list(f, flist);
1348 /* Recv the io_error flag */
1349 if (lp_ignore_errors(module_id) || ignore_errors)
1352 io_error |= read_int(f);
1356 output_flist(flist, who_am_i());
1360 for (i = 0; i < flist->count; i++)
1361 list_file_entry(flist->files[i]);
1365 rprintf(FINFO, "recv_file_list done\n");
1367 stats.flist_size = stats.total_read - start_read;
1368 stats.num_files = flist->count;
1373 out_of_memory("recv_file_list");
1374 return NULL; /* not reached */
1378 int file_compare(struct file_struct **file1, struct file_struct **file2)
1380 struct file_struct *f1 = *file1;
1381 struct file_struct *f2 = *file2;
1383 if (!f1->basename && !f2->basename)
1389 if (f1->dirname == f2->dirname)
1390 return u_strcmp(f1->basename, f2->basename);
1391 return f_name_cmp(f1, f2);
1395 int flist_find(struct file_list *flist, struct file_struct *f)
1397 int low = 0, high = flist->count - 1;
1399 while (high >= 0 && !flist->files[high]->basename) high--;
1404 while (low != high) {
1405 int mid = (low + high) / 2;
1406 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1408 return flist_up(flist, mid);
1415 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1416 return flist_up(flist, low);
1421 * Free up any resources a file_struct has allocated
1422 * and clear the file.
1424 void clear_file(int i, struct file_list *flist)
1426 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1427 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1428 memset(flist->files[i], 0, file_struct_len);
1433 * allocate a new file list
1435 struct file_list *flist_new(int with_hlink, char *msg)
1437 struct file_list *flist;
1439 flist = new(struct file_list);
1443 memset(flist, 0, sizeof (struct file_list));
1445 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1446 out_of_memory, POOL_INTERN)))
1449 #if SUPPORT_HARD_LINKS
1450 if (with_hlink && preserve_hard_links) {
1451 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1452 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1461 * free up all elements in a flist
1463 void flist_free(struct file_list *flist)
1465 pool_destroy(flist->file_pool);
1466 pool_destroy(flist->hlink_pool);
1473 * This routine ensures we don't have any duplicate names in our file list.
1474 * duplicate names can cause corruption because of the pipelining
1476 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1480 if (!flist || flist->count == 0)
1483 qsort(flist->files, flist->count,
1484 sizeof flist->files[0], (int (*)())file_compare);
1486 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1487 if (flist->files[i]->basename) {
1492 while (++i < flist->count) {
1493 if (!flist->files[i]->basename)
1495 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1496 if (verbose > 1 && !am_server) {
1498 "removing duplicate name %s from file list %d\n",
1499 f_name(flist->files[i]), i);
1501 /* Make sure that if we unduplicate '.', that we don't
1502 * lose track of a user-specified starting point (or
1503 * else deletions will mysteriously fail with -R). */
1504 if (flist->files[i]->flags & FLAG_DEL_START)
1505 flist->files[prev_i]->flags |= FLAG_DEL_START;
1507 clear_file(i, flist);
1513 /* we need to strip off the root directory in the case
1514 of relative paths, but this must be done _after_
1515 the sorting phase */
1516 for (i = 0; i < flist->count; i++) {
1517 if (flist->files[i]->dirname &&
1518 flist->files[i]->dirname[0] == '/') {
1519 memmove(&flist->files[i]->dirname[0],
1520 &flist->files[i]->dirname[1],
1521 strlen(flist->files[i]->dirname));
1524 if (flist->files[i]->dirname &&
1525 !flist->files[i]->dirname[0]) {
1526 flist->files[i]->dirname = NULL;
1532 static void output_flist(struct file_list *flist, const char *whose_list)
1534 char uidbuf[16], gidbuf[16];
1535 struct file_struct *file;
1538 for (i = 0; i < flist->count; i++) {
1539 file = flist->files[i];
1540 if ((am_root || am_sender) && preserve_uid)
1541 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1544 if (preserve_gid && file->gid != GID_NONE)
1545 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1548 rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f%s%s\n",
1549 whose_list, i, NS(file->basedir), NS(file->dirname),
1550 NS(file->basename), (int)file->mode,
1551 (double)file->length, uidbuf, gidbuf);
1556 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1558 /* Compare the names of two file_struct entities, just like strcmp()
1559 * would do if it were operating on the joined strings. We assume
1560 * that there are no 0-length strings.
1562 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1565 const uchar *c1, *c2;
1566 enum fnc_state state1, state2;
1568 if (!f1 || !f1->basename) {
1569 if (!f2 || !f2->basename)
1573 if (!f2 || !f2->basename)
1576 if (!(c1 = (uchar*)f1->dirname)) {
1578 c1 = (uchar*)f1->basename;
1584 if (!(c2 = (uchar*)f2->dirname)) {
1586 c2 = (uchar*)f2->basename;
1594 if ((dif = (int)*c1 - (int)*c2) != 0)
1604 c1 = (uchar*)f1->basename;
1618 c2 = (uchar*)f2->basename;
1632 /* Return a copy of the full filename of a flist entry, using the indicated
1633 * buffer. No size-checking is done because we checked the size when creating
1634 * the file_struct entry.
1636 char *f_name_to(struct file_struct *f, char *fbuf)
1638 if (!f || !f->basename)
1642 int len = strlen(f->dirname);
1643 memcpy(fbuf, f->dirname, len);
1645 strcpy(fbuf + len + 1, f->basename);
1647 strcpy(fbuf, f->basename);
1652 /* Like f_name_to(), but we rotate through 5 static buffers of our own. */
1653 char *f_name(struct file_struct *f)
1655 static char names[5][MAXPATHLEN];
1656 static unsigned int n;
1658 n = (n + 1) % (sizeof names / sizeof names[0]);
1660 return f_name_to(f, names[n]);
1664 static int is_backup_file(char *fn)
1666 int k = strlen(fn) - backup_suffix_len;
1667 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
1671 /* This function is used to implement --delete-during. */
1672 void delete_in_dir(struct file_list *flist, char *fname)
1674 struct file_list *dir_list;
1676 int save_recurse = recurse;
1678 if (max_delete && deletion_count >= max_delete)
1681 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
1682 rprintf(FINFO, "IO error encountered - skipping file deletion\n");
1683 max_delete = -1; /* avoid duplicating the above warning */
1687 if (link_stat(fname, &st, keep_dirlinks) < 0)
1690 if (one_file_system)
1691 filesystem_dev = st.st_dev;
1693 dir_list = flist_new(WITHOUT_HLINK, "delete_in_dir");
1694 recurse = 1; /* allow only one level */
1695 send_file_name(-1, dir_list, fname, 1, XMIT_DEL_START);
1696 recurse = save_recurse;
1697 clean_flist(dir_list, 0, 0);
1700 output_flist(dir_list, "delete");
1702 delete_missing(flist, dir_list, fname);
1704 flist_free(dir_list);
1708 /* If an item in dir_list is not found in full_list, delete it from the
1710 void delete_missing(struct file_list *full_list, struct file_list *dir_list,
1711 const char *dirname)
1715 if (max_delete && deletion_count >= max_delete)
1719 rprintf(FINFO, "deleting in %s\n", safe_fname(dirname));
1721 for (i = dir_list->count-1; i >= 0; i--) {
1722 if (!dir_list->files[i]->basename)
1724 mode = dir_list->files[i]->mode;
1725 if ((j = flist_find(full_list, dir_list->files[i])) < 0
1726 || (delete_during && S_ISDIR(mode)
1727 && !S_ISDIR(full_list->files[j]->mode))) {
1728 char *f = f_name(dir_list->files[i]);
1729 if (make_backups && (backup_dir || !is_backup_file(f))
1730 && !S_ISDIR(mode)) {
1733 rprintf(FINFO, "deleting %s\n",
1737 int dflags = delete_during
1738 ? DEL_DIR | DEL_FORCE_RECURSE
1739 : DEL_DIR | DEL_NO_RECURSE;
1740 delete_file(f, S_ISDIR(mode) ? dflags : 0);
1743 if (max_delete && deletion_count >= max_delete)