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;
45 extern char curr_dir[MAXPATHLEN];
46 extern unsigned int curr_dir_len;
47 extern char *backup_dir;
48 extern char *backup_suffix;
49 extern int filesfrom_fd;
51 extern int one_file_system;
52 extern int keep_dirlinks;
53 extern int preserve_links;
54 extern int preserve_hard_links;
55 extern int preserve_perms;
56 extern int preserve_devices;
57 extern int preserve_uid;
58 extern int preserve_gid;
59 extern int relative_paths;
60 extern int implied_dirs;
61 extern int make_backups;
62 extern int backup_suffix_len;
63 extern int copy_links;
64 extern int copy_unsafe_links;
65 extern int protocol_version;
66 extern int sanitize_paths;
67 extern int max_delete;
68 extern int orig_umask;
71 extern struct filter_list_struct filter_list;
72 extern struct filter_list_struct server_filter_list;
76 static char empty_sum[MD4_SUM_LENGTH];
77 static unsigned int file_struct_len;
78 static struct file_list *received_flist, *sorting_flist;
79 static dev_t filesystem_dev; /* used to implement -x */
80 static int deletion_count = 0; /* used to implement --max-delete */
82 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
83 static void output_flist(struct file_list *flist, const char *whose_list);
89 /* Figure out how big the file_struct is without trailing padding */
90 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
94 static int show_filelist_p(void)
96 return verbose && xfer_dirs && !am_server;
99 static void start_filelist_progress(char *kind)
101 rprintf(FINFO, "%s ... ", kind);
102 if (verbose > 1 || do_progress)
103 rprintf(FINFO, "\n");
108 static void emit_filelist_progress(const struct file_list *flist)
110 rprintf(FINFO, " %d files...\r", flist->count);
114 static void maybe_emit_filelist_progress(const struct file_list *flist)
116 if (do_progress && show_filelist_p() && (flist->count % 100) == 0)
117 emit_filelist_progress(flist);
121 static void finish_filelist_progress(const struct file_list *flist)
124 /* This overwrites the progress line */
125 rprintf(FINFO, "%d file%sto consider\n",
126 flist->count, flist->count == 1 ? " " : "s ");
128 rprintf(FINFO, "done\n");
131 void show_flist_stats(void)
137 static void list_file_entry(struct file_struct *f)
142 /* this can happen if duplicate names were removed */
146 permstring(perms, f->mode);
149 if (preserve_links && S_ISLNK(f->mode)) {
150 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
152 (double)f->length, timestring(f->modtime),
153 safe_fname(f_name(f)), safe_fname(f->u.link));
157 rprintf(FINFO, "%s %11.0f %s %s\n",
159 (double)f->length, timestring(f->modtime),
160 safe_fname(f_name(f)));
166 * Stat either a symlink or its referent, depending on the settings of
167 * copy_links, copy_unsafe_links, etc.
169 * @retval -1 on error
171 * @retval 0 for success
173 * @post If @p path is a symlink, then @p linkbuf (of size @c
174 * MAXPATHLEN) contains the symlink target.
176 * @post @p buffer contains information about the link or the
177 * referrent as appropriate, if they exist.
179 static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
183 return do_stat(path, buffer);
184 if (link_stat(path, buffer, 0) < 0)
186 if (S_ISLNK(buffer->st_mode)) {
187 int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1);
191 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
193 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
194 safe_fname(path), safe_fname(linkbuf));
196 return do_stat(path, buffer);
201 return do_stat(path, buffer);
205 int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks)
209 return do_stat(path, buffer);
210 if (do_lstat(path, buffer) < 0)
212 if (follow_dirlinks && S_ISLNK(buffer->st_mode)) {
214 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
219 return do_stat(path, buffer);
223 /* This function is used to check if a file should be included/excluded
224 * from the list of files based on its name and type etc. The value of
225 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
226 static int is_excluded(char *fname, int is_dir, int filter_level)
228 #if 0 /* This currently never happens, so avoid a useless compare. */
229 if (filter_level == NO_FILTERS)
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_filter_list.head
244 && check_filter(&server_filter_list, fname, is_dir) < 0)
246 if (filter_level != ALL_FILTERS)
249 && check_filter(&filter_list, fname, is_dir) < 0)
254 static int to_wire_mode(mode_t mode)
257 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
258 return (mode & ~(_S_IFMT)) | 0120000;
263 static mode_t from_wire_mode(int mode)
265 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
266 return (mode & ~(_S_IFMT)) | _S_IFLNK;
271 static void send_directory(int f, struct file_list *flist,
272 char *fbuf, unsigned int offset);
274 static char *flist_dir;
275 static int flist_dir_len;
279 * Make sure @p flist is big enough to hold at least @p flist->count
282 void flist_expand(struct file_list *flist)
284 struct file_struct **new_ptr;
286 if (flist->count < flist->malloced)
289 if (flist->malloced < FLIST_START)
290 flist->malloced = FLIST_START;
291 else if (flist->malloced >= FLIST_LINEAR)
292 flist->malloced += FLIST_LINEAR;
294 flist->malloced *= 2;
297 * In case count jumped or we are starting the list
298 * with a known size just set it.
300 if (flist->malloced < flist->count)
301 flist->malloced = flist->count;
303 new_ptr = realloc_array(flist->files, struct file_struct *,
306 if (verbose >= 2 && flist->malloced != FLIST_START) {
307 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
309 (double)sizeof flist->files[0] * flist->malloced,
310 (new_ptr == flist->files) ? " not" : "");
313 flist->files = new_ptr;
316 out_of_memory("flist_expand");
319 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
321 unsigned short flags;
322 static time_t modtime;
326 static uint32 rdev_major;
329 static char lastname[MAXPATHLEN];
330 char fname[MAXPATHLEN];
338 modtime = 0, mode = 0;
339 dev = 0, rdev = makedev(0, 0);
346 io_write_phase = "send_file_entry";
348 f_name_to(file, fname);
352 if (file->mode == mode)
353 flags |= XMIT_SAME_MODE;
356 if (preserve_devices) {
357 if (protocol_version < 28) {
358 if (IS_DEVICE(mode)) {
359 if (file->u.rdev == rdev)
360 flags |= XMIT_SAME_RDEV_pre28;
364 rdev = makedev(0, 0);
365 } else if (IS_DEVICE(mode)) {
367 if ((uint32)major(rdev) == rdev_major)
368 flags |= XMIT_SAME_RDEV_MAJOR;
370 rdev_major = major(rdev);
371 if ((uint32)minor(rdev) <= 0xFFu)
372 flags |= XMIT_RDEV_MINOR_IS_SMALL;
375 if (file->uid == uid)
376 flags |= XMIT_SAME_UID;
379 if (file->gid == gid)
380 flags |= XMIT_SAME_GID;
383 if (file->modtime == modtime)
384 flags |= XMIT_SAME_TIME;
386 modtime = file->modtime;
388 #ifdef SUPPORT_HARD_LINKS
389 if (file->link_u.idev) {
390 if (file->F_DEV == dev) {
391 if (protocol_version >= 28)
392 flags |= XMIT_SAME_DEV;
395 flags |= XMIT_HAS_IDEV_DATA;
400 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
402 l2 = strlen(fname+l1);
405 flags |= XMIT_SAME_NAME;
407 flags |= XMIT_LONG_NAME;
409 /* We must make sure we don't send a zero flag byte or the
410 * other end will terminate the flist transfer. Note that
411 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
412 * it's harmless way to add a bit to the first flag byte. */
413 if (protocol_version >= 28) {
414 if (!flags && !S_ISDIR(mode))
415 flags |= XMIT_TOP_DIR;
416 if ((flags & 0xFF00) || !flags) {
417 flags |= XMIT_EXTENDED_FLAGS;
418 write_byte(f, flags);
419 write_byte(f, flags >> 8);
421 write_byte(f, flags);
423 if (!(flags & 0xFF) && !S_ISDIR(mode))
424 flags |= XMIT_TOP_DIR;
426 flags |= XMIT_LONG_NAME;
427 write_byte(f, flags);
429 if (flags & XMIT_SAME_NAME)
431 if (flags & XMIT_LONG_NAME)
435 write_buf(f, fname + l1, l2);
437 write_longint(f, file->length);
438 if (!(flags & XMIT_SAME_TIME))
439 write_int(f, modtime);
440 if (!(flags & XMIT_SAME_MODE))
441 write_int(f, to_wire_mode(mode));
442 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
447 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
452 if (preserve_devices && IS_DEVICE(mode)) {
453 if (protocol_version < 28) {
454 if (!(flags & XMIT_SAME_RDEV_pre28))
455 write_int(f, (int)rdev);
457 if (!(flags & XMIT_SAME_RDEV_MAJOR))
458 write_int(f, major(rdev));
459 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
460 write_byte(f, minor(rdev));
462 write_int(f, minor(rdev));
467 if (preserve_links && S_ISLNK(mode)) {
468 int len = strlen(file->u.link);
470 write_buf(f, file->u.link, len);
474 #ifdef SUPPORT_HARD_LINKS
475 if (flags & XMIT_HAS_IDEV_DATA) {
476 if (protocol_version < 26) {
477 /* 32-bit dev_t and ino_t */
479 write_int(f, file->F_INODE);
481 /* 64-bit dev_t and ino_t */
482 if (!(flags & XMIT_SAME_DEV))
483 write_longint(f, dev);
484 write_longint(f, file->F_INODE);
489 if (always_checksum) {
493 else if (protocol_version < 28) {
494 /* Prior to 28, we sent a useless set of nulls. */
500 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
504 strlcpy(lastname, fname, MAXPATHLEN);
506 io_write_phase = "unknown";
511 static struct file_struct *receive_file_entry(struct file_list *flist,
512 unsigned short flags, int f)
514 static time_t modtime;
518 static uint32 rdev_major;
521 static char lastname[MAXPATHLEN], *lastdir;
522 static int lastdir_depth, lastdir_len = -1;
523 static unsigned int del_hier_name_len = 0;
524 static int in_del_hier = 0;
525 char thisname[MAXPATHLEN];
526 unsigned int l1 = 0, l2 = 0;
527 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
529 char *basename, *dirname, *bp;
530 struct file_struct *file;
533 modtime = 0, mode = 0;
534 dev = 0, rdev = makedev(0, 0);
543 if (flags & XMIT_SAME_NAME)
546 if (flags & XMIT_LONG_NAME)
551 if (l2 >= MAXPATHLEN - l1) {
553 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
554 flags, l1, l2, safe_fname(lastname));
555 overflow("receive_file_entry");
558 strlcpy(thisname, lastname, l1 + 1);
559 read_sbuf(f, &thisname[l1], l2);
560 thisname[l1 + l2] = 0;
562 strlcpy(lastname, thisname, MAXPATHLEN);
564 clean_fname(thisname, 0);
567 sanitize_path(thisname, thisname, "", 0);
569 if ((basename = strrchr(thisname, '/')) != NULL) {
570 dirname_len = ++basename - thisname; /* counts future '\0' */
571 if (lastdir_len == dirname_len - 1
572 && strncmp(thisname, lastdir, lastdir_len) == 0) {
574 dirname_len = 0; /* indicates no copy is needed */
582 basename_len = strlen(basename) + 1; /* count the '\0' */
584 file_length = read_longint(f);
585 if (!(flags & XMIT_SAME_TIME))
586 modtime = (time_t)read_int(f);
587 if (!(flags & XMIT_SAME_MODE))
588 mode = from_wire_mode(read_int(f));
590 if (preserve_uid && !(flags & XMIT_SAME_UID))
591 uid = (uid_t)read_int(f);
592 if (preserve_gid && !(flags & XMIT_SAME_GID))
593 gid = (gid_t)read_int(f);
595 if (preserve_devices) {
596 if (protocol_version < 28) {
597 if (IS_DEVICE(mode)) {
598 if (!(flags & XMIT_SAME_RDEV_pre28))
599 rdev = (dev_t)read_int(f);
601 rdev = makedev(0, 0);
602 } else if (IS_DEVICE(mode)) {
604 if (!(flags & XMIT_SAME_RDEV_MAJOR))
605 rdev_major = read_int(f);
606 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
607 rdev_minor = read_byte(f);
609 rdev_minor = read_int(f);
610 rdev = makedev(rdev_major, rdev_minor);
615 if (preserve_links && S_ISLNK(mode)) {
616 linkname_len = read_int(f) + 1; /* count the '\0' */
617 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
618 rprintf(FERROR, "overflow: linkname_len=%d\n",
620 overflow("receive_file_entry");
627 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
629 alloc_len = file_struct_len + dirname_len + basename_len
630 + linkname_len + sum_len;
631 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
633 file = (struct file_struct *)bp;
634 memset(bp, 0, file_struct_len);
635 bp += file_struct_len;
638 file->modtime = modtime;
639 file->length = file_length;
645 file->dirname = lastdir = bp;
646 lastdir_len = dirname_len - 1;
647 memcpy(bp, dirname, dirname_len - 1);
650 lastdir_depth = count_dir_elements(lastdir);
651 file->dir.depth = lastdir_depth + 1;
652 } else if (dirname) {
653 file->dirname = dirname; /* we're reusing lastname */
654 file->dir.depth = lastdir_depth + 1;
659 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
661 if (flags & XMIT_TOP_DIR) {
663 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
664 file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
665 } else if (in_del_hier) {
666 if (!relative_paths || !del_hier_name_len
667 || (l1 >= del_hier_name_len
668 && thisname[del_hier_name_len] == '/'))
669 file->flags |= FLAG_DEL_HERE;
676 memcpy(bp, basename, basename_len);
679 if (preserve_devices && IS_DEVICE(mode))
685 read_sbuf(f, bp, linkname_len - 1);
687 sanitize_path(bp, bp, "", lastdir_depth);
692 #ifdef SUPPORT_HARD_LINKS
693 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
694 flags |= XMIT_HAS_IDEV_DATA;
695 if (flags & XMIT_HAS_IDEV_DATA) {
697 if (protocol_version < 26) {
701 if (!(flags & XMIT_SAME_DEV))
702 dev = read_longint(f);
703 inode = read_longint(f);
705 if (flist->hlink_pool) {
706 file->link_u.idev = pool_talloc(flist->hlink_pool,
707 struct idev, 1, "inode_table");
708 file->F_INODE = inode;
714 if (always_checksum) {
717 file->u.sum = sum = bp;
719 } else if (protocol_version < 28) {
720 /* Prior to 28, we get a useless set of nulls. */
726 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
730 if (!preserve_perms) {
731 /* set an appropriate set of permissions based on original
732 * permissions and umask. This emulates what GNU cp does */
733 file->mode &= ~orig_umask;
741 * Create a file_struct for a named file by reading its stat()
742 * information and performing extensive checks against global
745 * @return the new file, or NULL if there was an error or this file
746 * should be excluded.
748 * @todo There is a small optimization opportunity here to avoid
749 * stat()ing the file in some circumstances, which has a certain cost.
750 * We are called immediately after doing readdir(), and so we may
751 * already know the d_type of the file. We could for example avoid
752 * statting directories if we're not recursing, but this is not a very
753 * important case. Some systems may not have d_type.
755 struct file_struct *make_file(char *fname, struct file_list *flist,
758 static char *lastdir;
759 static int lastdir_len = -1;
760 struct file_struct *file;
762 char sum[SUM_LENGTH];
763 char thisname[MAXPATHLEN];
764 char linkname[MAXPATHLEN];
765 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
766 char *basename, *dirname, *bp;
767 unsigned short flags = 0;
769 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
772 if (strlcpy(thisname, fname, sizeof thisname)
773 >= sizeof thisname - flist_dir_len) {
774 rprintf(FINFO, "skipping overly long name: %s\n",
778 clean_fname(thisname, 0);
780 sanitize_path(thisname, thisname, "", 0);
782 memset(sum, 0, SUM_LENGTH);
784 if (readlink_stat(thisname, &st, linkname) != 0) {
785 int save_errno = errno;
786 /* See if file is excluded before reporting an error. */
787 if (filter_level != NO_FILTERS
788 && is_excluded(thisname, 0, filter_level))
790 if (save_errno == ENOENT) {
792 /* Avoid "vanished" error if symlink points nowhere. */
793 if (copy_links && do_lstat(thisname, &st) == 0
794 && S_ISLNK(st.st_mode)) {
795 io_error |= IOERR_GENERAL;
796 rprintf(FERROR, "symlink has no referent: %s\n",
797 full_fname(thisname));
801 enum logcode c = am_daemon && protocol_version < 28
803 io_error |= IOERR_VANISHED;
804 rprintf(c, "file has vanished: %s\n",
805 full_fname(thisname));
808 io_error |= IOERR_GENERAL;
809 rsyserr(FERROR, save_errno, "readlink %s failed",
810 full_fname(thisname));
815 /* backup.c calls us with filter_level set to NO_FILTERS. */
816 if (filter_level == NO_FILTERS)
819 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
820 rprintf(FINFO, "skipping directory %s\n", safe_fname(thisname));
824 /* We only care about directories because we need to avoid recursing
825 * into a mount-point directory, not to avoid copying a symlinked
826 * file if -L (or similar) was specified. */
827 if (one_file_system && st.st_dev != filesystem_dev
828 && S_ISDIR(st.st_mode))
829 flags |= FLAG_MOUNT_POINT;
831 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
834 if (lp_ignore_nonreadable(module_id)) {
836 if (!S_ISLNK(st.st_mode))
838 if (access(thisname, R_OK) != 0)
845 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
846 who_am_i(), safe_fname(thisname), filter_level);
849 if ((basename = strrchr(thisname, '/')) != NULL) {
850 dirname_len = ++basename - thisname; /* counts future '\0' */
851 if (lastdir_len == dirname_len - 1
852 && strncmp(thisname, lastdir, lastdir_len) == 0) {
854 dirname_len = 0; /* indicates no copy is needed */
862 basename_len = strlen(basename) + 1; /* count the '\0' */
865 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
870 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
872 alloc_len = file_struct_len + dirname_len + basename_len
873 + linkname_len + sum_len;
875 bp = pool_alloc(flist->file_pool, alloc_len,
876 "receive_file_entry");
878 if (!(bp = new_array(char, alloc_len)))
879 out_of_memory("receive_file_entry");
882 file = (struct file_struct *)bp;
883 memset(bp, 0, file_struct_len);
884 bp += file_struct_len;
887 file->modtime = st.st_mtime;
888 file->length = st.st_size;
889 file->mode = st.st_mode;
890 file->uid = st.st_uid;
891 file->gid = st.st_gid;
893 #ifdef SUPPORT_HARD_LINKS
894 if (flist && flist->hlink_pool) {
895 if (protocol_version < 28) {
896 if (S_ISREG(st.st_mode))
897 file->link_u.idev = pool_talloc(
898 flist->hlink_pool, struct idev, 1,
901 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
902 file->link_u.idev = pool_talloc(
903 flist->hlink_pool, struct idev, 1,
907 if (file->link_u.idev) {
908 file->F_DEV = st.st_dev;
909 file->F_INODE = st.st_ino;
914 file->dirname = lastdir = bp;
915 lastdir_len = dirname_len - 1;
916 memcpy(bp, dirname, dirname_len - 1);
920 file->dirname = dirname;
923 memcpy(bp, basename, basename_len);
926 #ifdef HAVE_STRUCT_STAT_ST_RDEV
927 if (preserve_devices && IS_DEVICE(st.st_mode))
928 file->u.rdev = st.st_rdev;
934 memcpy(bp, linkname, linkname_len);
941 file_checksum(thisname, bp, st.st_size);
945 file->dir.root = flist_dir;
947 /* This code is only used by the receiver when it is building
948 * a list of files for a delete pass. */
949 if (keep_dirlinks && linkname_len && flist) {
951 int save_mode = file->mode;
952 file->mode = S_IFDIR; /* find a directory w/our name */
953 if (flist_find(received_flist, file) >= 0
954 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
955 file->modtime = st2.st_mtime;
956 file->length = st2.st_size;
957 file->mode = st2.st_mode;
958 file->uid = st2.st_uid;
959 file->gid = st2.st_gid;
962 file->mode = save_mode;
965 if (!S_ISDIR(st.st_mode))
966 stats.total_size += st.st_size;
972 void send_file_name(int f, struct file_list *flist, char *fname,
973 int recursive, unsigned short base_flags)
975 struct file_struct *file;
976 char fbuf[MAXPATHLEN];
978 file = make_file(fname, flist, f == -2 ? SERVER_FILTERS : ALL_FILTERS);
982 maybe_emit_filelist_progress(flist);
986 if (file->basename[0]) {
987 flist->files[flist->count++] = file;
988 send_file_entry(file, f, base_flags);
991 if (recursive && S_ISDIR(file->mode)
992 && !(file->flags & FLAG_MOUNT_POINT) && f_name_to(file, fbuf)) {
994 unsigned int len = strlen(fbuf);
995 if (len > 1 && fbuf[len-1] == '/')
997 if (len >= MAXPATHLEN - 1) {
998 io_error |= IOERR_GENERAL;
999 rprintf(FERROR, "skipping long-named directory: %s\n",
1003 save_filters = push_local_filters(fbuf, len);
1004 send_directory(f, flist, fbuf, len);
1005 pop_local_filters(save_filters);
1010 /* Note that the "recurse" value either contains -1, for infinite recursion,
1011 * or a number >= 0 indicating how many levels of recursion we will allow.
1012 * This function is normally called by the sender, but the receiving side
1013 * also calls it from delete_in_dir() with f set to -1 so that we just
1014 * construct the file list in memory without sending it over the wire. Also,
1015 * get_dirlist() calls this with f set to -2, which indicates that local
1016 * filter rules should be ignored. */
1017 static void send_directory(int f, struct file_list *flist,
1018 char *fbuf, unsigned int len)
1024 if (!(d = opendir(fbuf))) {
1025 io_error |= IOERR_GENERAL;
1026 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
1031 if (len != 1 || *fbuf != '/')
1035 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1036 char *dname = d_name(di);
1037 if (dname[0] == '.' && (dname[1] == '\0'
1038 || (dname[1] == '.' && dname[2] == '\0')))
1040 if (strlcpy(p, dname, MAXPATHLEN - len) < MAXPATHLEN - len) {
1041 int do_subdirs = recurse >= 1 ? recurse-- : recurse;
1042 send_file_name(f, flist, fbuf, do_subdirs, 0);
1044 io_error |= IOERR_GENERAL;
1046 "cannot send long-named file %s\n",
1051 io_error |= IOERR_GENERAL;
1053 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
1060 struct file_list *send_file_list(int f, int argc, char *argv[])
1064 char *p, *dir, olddir[sizeof curr_dir];
1065 char lastpath[MAXPATHLEN] = "";
1066 struct file_list *flist;
1067 struct timeval start_tv, end_tv;
1071 if (show_filelist_p())
1072 start_filelist_progress("building file list");
1074 start_write = stats.total_written;
1075 gettimeofday(&start_tv, NULL);
1077 flist = flist_new(WITH_HLINK, "send_file_list");
1079 io_start_buffering_out();
1080 if (filesfrom_fd >= 0) {
1081 if (argv[0] && !push_dir(argv[0])) {
1082 rsyserr(FERROR, errno, "push_dir %s failed",
1083 full_fname(argv[0]));
1084 exit_cleanup(RERR_FILESELECT);
1090 char fname2[MAXPATHLEN];
1091 char *fname = fname2;
1095 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1097 sanitize_path(fname, fname, "", 0);
1101 strlcpy(fname, *argv++, MAXPATHLEN);
1103 sanitize_path(fname, fname, "", 0);
1107 if (!l || fname[l - 1] == '/') {
1108 if (l == 2 && fname[0] == '.') {
1109 /* Turn "./" into just "." rather than "./." */
1111 } else if (l < MAXPATHLEN) {
1116 if (fname[l-1] == '.' && (l == 1 || fname[l-2] == '/')) {
1117 if (!recurse && xfer_dirs)
1118 recurse = 1; /* allow one level */
1119 } else if (recurse > 0)
1122 if (link_stat(fname, &st, keep_dirlinks) != 0) {
1123 io_error |= IOERR_GENERAL;
1124 rsyserr(FERROR, errno, "link_stat %s failed",
1129 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1130 rprintf(FINFO, "skipping directory %s\n",
1138 if (!relative_paths) {
1139 p = strrchr(fname, '/');
1148 } else if (implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1149 /* this ensures we send the intermediate directories,
1150 thus getting their permissions right */
1151 char *lp = lastpath, *fn = fname, *slash = fname;
1153 /* Skip any initial directories in our path that we
1154 * have in common with lastpath. */
1155 while (*fn && *lp == *fn) {
1161 if (fn != p || (*lp && *lp != '/')) {
1162 int save_copy_links = copy_links;
1163 int save_xfer_dirs = xfer_dirs;
1164 copy_links = copy_unsafe_links;
1166 while ((slash = strchr(slash+1, '/')) != 0) {
1168 send_file_name(f, flist, fname, 0, 0);
1171 copy_links = save_copy_links;
1172 xfer_dirs = save_xfer_dirs;
1174 strlcpy(lastpath, fname, sizeof lastpath);
1183 static char *lastdir;
1184 static int lastdir_len;
1186 strcpy(olddir, curr_dir); /* can't overflow */
1188 if (!push_dir(dir)) {
1189 io_error |= IOERR_GENERAL;
1190 rsyserr(FERROR, errno, "push_dir %s failed",
1195 if (lastdir && strcmp(lastdir, dir) == 0) {
1196 flist_dir = lastdir;
1197 flist_dir_len = lastdir_len;
1199 flist_dir = lastdir = strdup(dir);
1200 flist_dir_len = lastdir_len = strlen(dir);
1204 if (one_file_system)
1205 filesystem_dev = st.st_dev;
1207 do_subdirs = recurse >= 1 ? recurse-- : recurse;
1208 send_file_name(f, flist, fname, do_subdirs, XMIT_TOP_DIR);
1213 if (!pop_dir(olddir)) {
1214 rsyserr(FERROR, errno, "pop_dir %s failed",
1216 exit_cleanup(RERR_FILESELECT);
1221 gettimeofday(&end_tv, NULL);
1222 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1223 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1224 if (stats.flist_buildtime == 0)
1225 stats.flist_buildtime = 1;
1228 send_file_entry(NULL, f, 0);
1230 if (show_filelist_p())
1231 finish_filelist_progress(flist);
1233 gettimeofday(&end_tv, NULL);
1234 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1235 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1237 if (flist->hlink_pool) {
1238 pool_destroy(flist->hlink_pool);
1239 flist->hlink_pool = NULL;
1242 /* Sort the list without removing any duplicates. This allows the
1243 * receiving side to ask for any name they like, which gives us the
1244 * flexibility to change the way we unduplicate names in the future
1245 * without causing a compatibility problem with older versions. */
1246 clean_flist(flist, 0, 0);
1248 /* Now send the uid/gid list. This was introduced in
1249 * protocol version 15 */
1252 /* send the io_error flag */
1253 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1256 stats.flist_size = stats.total_written - start_write;
1257 stats.num_files = flist->count;
1260 output_flist(flist, who_am_i());
1263 rprintf(FINFO, "send_file_list done\n");
1269 struct file_list *recv_file_list(int f)
1271 struct file_list *flist;
1272 unsigned short flags;
1275 if (show_filelist_p())
1276 start_filelist_progress("receiving file list");
1278 start_read = stats.total_read;
1280 flist = flist_new(WITH_HLINK, "recv_file_list");
1281 received_flist = flist;
1284 flist->malloced = 1000;
1285 flist->files = new_array(struct file_struct *, flist->malloced);
1290 while ((flags = read_byte(f)) != 0) {
1291 struct file_struct *file;
1293 flist_expand(flist);
1295 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1296 flags |= read_byte(f) << 8;
1297 file = receive_file_entry(flist, flags, f);
1299 if (S_ISREG(file->mode))
1300 stats.total_size += file->length;
1302 flist->files[flist->count++] = file;
1304 maybe_emit_filelist_progress(flist);
1307 rprintf(FINFO, "recv_file_name(%s)\n",
1308 safe_fname(f_name(file)));
1311 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1314 rprintf(FINFO, "received %d names\n", flist->count);
1316 if (show_filelist_p())
1317 finish_filelist_progress(flist);
1319 clean_flist(flist, relative_paths, 1);
1322 /* Now send the uid/gid list. This was introduced in
1323 * protocol version 15 */
1324 recv_uid_list(f, flist);
1326 /* Recv the io_error flag */
1327 if (lp_ignore_errors(module_id) || ignore_errors)
1330 io_error |= read_int(f);
1334 output_flist(flist, who_am_i());
1338 for (i = 0; i < flist->count; i++)
1339 list_file_entry(flist->files[i]);
1343 rprintf(FINFO, "recv_file_list done\n");
1345 stats.flist_size = stats.total_read - start_read;
1346 stats.num_files = flist->count;
1351 out_of_memory("recv_file_list");
1352 return NULL; /* not reached */
1356 static int file_compare(struct file_struct **file1, struct file_struct **file2)
1358 return f_name_cmp(*file1, *file2);
1362 /* Search for an identically-named item in the file list. Note that the
1363 * items must agree in their directory-ness, or no match is returned. */
1364 int flist_find(struct file_list *flist, struct file_struct *f)
1366 int low = flist->low, high = flist->high;
1367 int ret, mid, mid_up;
1369 while (low <= high) {
1370 mid = (low + high) / 2;
1371 for (mid_up = mid; !flist->files[mid_up]->basename; mid_up++) {}
1373 ret = f_name_cmp(flist->files[mid_up], f);
1377 if (protocol_version < 29
1378 && S_ISDIR(flist->files[mid_up]->mode)
1379 != S_ISDIR(f->mode))
1393 * Free up any resources a file_struct has allocated
1394 * and clear the file.
1396 void clear_file(int i, struct file_list *flist)
1398 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1399 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1400 memset(flist->files[i], 0, file_struct_len);
1405 * allocate a new file list
1407 struct file_list *flist_new(int with_hlink, char *msg)
1409 struct file_list *flist;
1411 flist = new(struct file_list);
1415 memset(flist, 0, sizeof (struct file_list));
1417 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1418 out_of_memory, POOL_INTERN)))
1421 #ifdef SUPPORT_HARD_LINKS
1422 if (with_hlink && preserve_hard_links) {
1423 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
1424 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1433 * free up all elements in a flist
1435 void flist_free(struct file_list *flist)
1437 pool_destroy(flist->file_pool);
1438 pool_destroy(flist->hlink_pool);
1445 * This routine ensures we don't have any duplicate names in our file list.
1446 * duplicate names can cause corruption because of the pipelining
1448 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1452 if (!flist || flist->count == 0)
1455 sorting_flist = flist;
1456 qsort(flist->files, flist->count,
1457 sizeof flist->files[0], (int (*)())file_compare);
1458 sorting_flist = NULL;
1460 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1461 if (flist->files[i]->basename) {
1466 flist->low = prev_i;
1467 while (++i < flist->count) {
1469 struct file_struct *file = flist->files[i];
1471 if (!file->basename)
1473 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1475 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
1476 int save_mode = file->mode;
1477 /* Make sure that this directory doesn't duplicate a
1478 * non-directory earlier in the list. */
1479 flist->high = prev_i;
1480 file->mode = S_IFREG;
1481 j = flist_find(flist, file);
1482 file->mode = save_mode;
1486 struct file_struct *fp = flist->files[j];
1488 /* If one is a dir and the other is not, we want to
1489 * keep the dir because it might have contents in the
1491 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1492 if (S_ISDIR(file->mode))
1498 if (verbose > 1 && !am_server) {
1500 "removing duplicate name %s from file list (%d)\n",
1501 safe_fname(f_name(file)), drop);
1503 /* Make sure that if we unduplicate '.', that we don't
1504 * lose track of a user-specified top directory. */
1505 if (flist->files[drop]->flags & FLAG_TOP_DIR)
1506 flist->files[keep]->flags |= FLAG_TOP_DIR;
1508 clear_file(drop, flist);
1511 if (flist->low == drop) {
1513 j < i && !flist->files[j]->basename;
1522 flist->high = no_dups ? prev_i : flist->count - 1;
1525 /* We need to strip off the leading slashes for relative
1526 * paths, but this must be done _after_ the sorting phase. */
1527 for (i = flist->low; i <= flist->high; i++) {
1528 struct file_struct *file = flist->files[i];
1532 if (*file->dirname == '/') {
1533 char *s = file->dirname + 1;
1534 while (*s == '/') s++;
1535 memmove(file->dirname, s, strlen(s) + 1);
1538 if (!*file->dirname)
1539 file->dirname = NULL;
1544 static void output_flist(struct file_list *flist, const char *whose_list)
1546 char uidbuf[16], gidbuf[16], depthbuf[16];
1547 struct file_struct *file;
1550 for (i = 0; i < flist->count; i++) {
1551 file = flist->files[i];
1552 if ((am_root || am_sender) && preserve_uid)
1553 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1556 if (preserve_gid && file->gid != GID_NONE)
1557 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1561 sprintf(depthbuf, "%d", file->dir.depth);
1562 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
1563 whose_list, i, am_sender ? NS(file->dir.root) : depthbuf,
1564 file->dirname ? safe_fname(file->dirname) : "",
1565 file->dirname ? "/" : "", NS(file->basename),
1566 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
1567 (double)file->length, uidbuf, gidbuf, file->flags);
1572 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
1573 enum fnc_type { t_PATH, t_ITEM };
1575 /* Compare the names of two file_struct entities, similar to how strcmp()
1576 * would do if it were operating on the joined strings.
1578 * Some differences beginning with protocol_version 29: (1) directory names
1579 * are compared with an assumed trailing slash so that they compare in a
1580 * way that would cause them to sort immediately prior to any content they
1581 * may have; (2) a directory of any name compares after a non-directory of
1582 * any name at the same depth; (3) a directory with name "." compares prior
1583 * to anything else. These changes mean that a directory and a non-dir
1584 * with the same name will not compare as equal (protocol_version >= 29).
1586 * The dirname component can be an empty string, but the basename component
1587 * cannot (and never is in the current codebase). The basename component
1588 * may be NULL (for a removed item), in which case it is considered to be
1589 * after any existing item. */
1590 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1593 const uchar *c1, *c2;
1594 enum fnc_state state1, state2;
1595 enum fnc_type type1, type2;
1596 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
1598 if (!f1 || !f1->basename) {
1599 if (!f2 || !f2->basename)
1603 if (!f2 || !f2->basename)
1606 c1 = (uchar*)f1->dirname;
1607 c2 = (uchar*)f2->dirname;
1611 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1612 c1 = (uchar*)f1->basename;
1613 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1615 state1 = s_TRAILING;
1628 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1629 c2 = (uchar*)f2->basename;
1630 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1632 state2 = s_TRAILING;
1646 return type1 == t_PATH ? 1 : -1;
1649 if ((dif = (int)*c1++ - (int)*c2++) != 0)
1658 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
1660 c1 = (uchar*)f1->basename;
1663 state1 = s_TRAILING;
1664 if (type1 == t_PATH) {
1673 if (*c2 && type1 != type2)
1674 return type1 == t_PATH ? 1 : -1;
1679 if (state1 == s_SLASH && sorting_flist) {
1681 /* Optimize for future comparisons. */
1683 j < sorting_flist->count;
1685 struct file_struct *fp
1686 = sorting_flist->files[j];
1687 if (fp->dirname == f2->dirname)
1688 fp->dirname = f1->dirname;
1695 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
1697 c2 = (uchar*)f2->basename;
1700 state2 = s_TRAILING;
1701 if (type2 == t_PATH) {
1713 return type1 == t_PATH ? 1 : -1;
1721 /* Return a copy of the full filename of a flist entry, using the indicated
1722 * buffer. No size-checking is done because we checked the size when creating
1723 * the file_struct entry.
1725 char *f_name_to(struct file_struct *f, char *fbuf)
1727 if (!f || !f->basename)
1731 int len = strlen(f->dirname);
1732 memcpy(fbuf, f->dirname, len);
1734 strcpy(fbuf + len + 1, f->basename);
1736 strcpy(fbuf, f->basename);
1741 /* Like f_name_to(), but we rotate through 5 static buffers of our own. */
1742 char *f_name(struct file_struct *f)
1744 static char names[5][MAXPATHLEN];
1745 static unsigned int n;
1747 n = (n + 1) % (sizeof names / sizeof names[0]);
1749 return f_name_to(f, names[n]);
1753 static int is_backup_file(char *fn)
1755 int k = strlen(fn) - backup_suffix_len;
1756 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
1759 struct file_list *get_dirlist(const char *dirname, int ignore_filter_rules)
1761 struct file_list *dirlist;
1762 char dirbuf[MAXPATHLEN];
1764 int save_recurse = recurse;
1766 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1767 if (dlen >= MAXPATHLEN)
1770 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1772 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirbuf, dlen);
1773 recurse = save_recurse;
1779 /* This function is used to implement per-directory deletion, and
1780 * is used by all the --delete-WHEN options. Note that the fbuf
1781 * pointer must point to a MAXPATHLEN buffer with the name of the
1782 * directory in it (the functions we call will append names onto
1783 * the end, but the old dir value will be restored on exit). */
1784 void delete_in_dir(struct file_list *flist, char *fbuf,
1785 struct file_struct *file)
1787 static int min_depth = MAXPATHLEN, cur_depth = -1;
1788 static void *filt_array[MAXPATHLEN/2+1];
1789 struct file_list *dir_list;
1794 while (cur_depth >= min_depth)
1795 pop_local_filters(filt_array[cur_depth--]);
1796 min_depth = MAXPATHLEN;
1800 if (file->dir.depth >= MAXPATHLEN/2+1)
1801 return; /* Impossible... */
1803 if (max_delete && deletion_count >= max_delete)
1806 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
1808 "IO error encountered -- skipping file deletion\n");
1809 max_delete = -1; /* avoid duplicating the above warning */
1813 while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
1814 pop_local_filters(filt_array[cur_depth--]);
1815 cur_depth = file->dir.depth;
1816 if (min_depth > cur_depth)
1817 min_depth = cur_depth;
1818 dlen = strlen(fbuf);
1819 filt_array[cur_depth] = push_local_filters(fbuf, dlen);
1821 if (link_stat(fbuf, &st, keep_dirlinks) < 0)
1824 if (one_file_system && file->flags & FLAG_TOP_DIR)
1825 filesystem_dev = st.st_dev;
1827 dir_list = flist_new(WITHOUT_HLINK, "delete_in_dir");
1830 send_directory(-1, dir_list, fbuf, dlen);
1835 output_flist(dir_list, "delete");
1837 delete_missing(flist, dir_list, fbuf);
1839 flist_free(dir_list);
1843 /* If an item in dir_list is not found in full_list, delete it from the
1845 void delete_missing(struct file_list *full_list, struct file_list *dir_list,
1846 const char *dirname)
1850 if (max_delete && deletion_count >= max_delete)
1854 rprintf(FINFO, "delete_missing(%s)\n", safe_fname(dirname));
1856 for (i = dir_list->count; i--; ) {
1857 if (!dir_list->files[i]->basename)
1859 mode = dir_list->files[i]->mode;
1860 if (flist_find(full_list, dir_list->files[i]) < 0) {
1861 char *f = f_name(dir_list->files[i]);
1862 if (make_backups && (backup_dir || !is_backup_file(f))
1863 && !S_ISDIR(mode)) {
1866 rprintf(FINFO, "deleting %s\n",
1869 } else if (S_ISDIR(mode))
1870 delete_file(f, DEL_DIR | DEL_FORCE_RECURSE);
1874 if (max_delete && deletion_count >= max_delete)