X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/blobdiff_plain/7402d58369a8307aa52d334a17072deb010f450f..f0fbf1d670ee13bb461d44ac089ef9ecea267c7f:/flist.c diff --git a/flist.c b/flist.c index 851e3061..2fab393a 100644 --- a/flist.c +++ b/flist.c @@ -1,34 +1,29 @@ /* - Copyright (C) Andrew Tridgell 1996 - Copyright (C) Paul Mackerras 1996 - Copyright (C) 2001, 2002 by Martin Pool - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -/** @file flist.c - * Generate and receive file lists + * Generate and receive file lists. * - * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html + * Copyright (C) 1996 Andrew Tridgell + * Copyright (C) 1996 Paul Mackerras + * Copyright (C) 2001, 2002 Martin Pool + * Copyright (C) 2002, 2003, 2004, 2005, 2006 Wayne Davison * - **/ + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ #include "rsync.h" extern int verbose; -extern int dry_run; extern int list_only; extern int am_root; extern int am_server; @@ -43,22 +38,24 @@ extern int recurse; extern int xfer_dirs; extern int filesfrom_fd; extern int one_file_system; +extern int copy_dirlinks; extern int keep_dirlinks; extern int preserve_links; extern int preserve_hard_links; -extern int preserve_perms; extern int preserve_devices; extern int preserve_specials; extern int preserve_uid; extern int preserve_gid; extern int relative_paths; extern int implied_dirs; -extern int skip_empty_dirs; +extern int flist_extra_ndx; +extern int ignore_perishable; +extern int non_perishable_cnt; +extern int prune_empty_dirs; extern int copy_links; extern int copy_unsafe_links; extern int protocol_version; extern int sanitize_paths; -extern int orig_umask; extern struct stats stats; extern struct file_list *the_file_list; @@ -72,11 +69,17 @@ extern struct filter_list_struct server_filter_list; int io_error; int checksum_len; dev_t filesystem_dev; /* used to implement -x */ -unsigned int file_struct_len; +int file_struct_len; + +/* The tmp_* vars are used as a cache area by make_file() to store data + * that the sender doesn't need to remember in its file list. The data + * will survive just long enough to be used by send_file_entry(). */ +static dev_t tmp_rdev; +static struct idev tmp_idev; +static char tmp_sum[MD4_SUM_LENGTH]; static char empty_sum[MD4_SUM_LENGTH]; static int flist_count_offset; -static int max_dir_depth = 0; static void clean_flist(struct file_list *flist, int strip_root, int no_dups); static void output_flist(struct file_list *flist); @@ -97,15 +100,15 @@ static int show_filelist_p(void) static void start_filelist_progress(char *kind) { - rprintf(FINFO, "%s ... ", kind); + rprintf(FCLIENT, "%s ... ", kind); if (verbose > 1 || do_progress) - rprintf(FINFO, "\n"); + rprintf(FCLIENT, "\n"); rflush(FINFO); } static void emit_filelist_progress(int count) { - rprintf(FINFO, " %d files...\r", count); + rprintf(FCLIENT, " %d files...\r", count); } static void maybe_emit_filelist_progress(int count) @@ -131,86 +134,78 @@ void show_flist_stats(void) static void list_file_entry(struct file_struct *f) { - char perms[11]; + char permbuf[PERMSTRING_SIZE]; if (!f->basename) { /* this can happen if duplicate names were removed */ return; } - permstring(perms, f->mode); + permstring(permbuf, f->mode); #ifdef SUPPORT_LINKS if (preserve_links && S_ISLNK(f->mode)) { rprintf(FINFO, "%s %11.0f %s %s -> %s\n", - perms, + permbuf, (double)f->length, timestring(f->modtime), - f_name(f, NULL), f->u.link); + f_name(f, NULL), F_SYMLINK(f)); } else #endif { rprintf(FINFO, "%s %11.0f %s %s\n", - perms, + permbuf, (double)f->length, timestring(f->modtime), f_name(f, NULL)); } } -/** - * Stat either a symlink or its referent, depending on the settings of - * copy_links, copy_unsafe_links, etc. - * - * @retval -1 on error +/* Stat either a symlink or its referent, depending on the settings of + * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success. * - * @retval 0 for success + * If path is the name of a symlink, then the linkbuf buffer (which must hold + * MAXPATHLEN chars) will be set to the symlink's target string. * - * @post If @p path is a symlink, then @p linkbuf (of size @c - * MAXPATHLEN) contains the symlink target. - * - * @post @p buffer contains information about the link or the - * referrent as appropriate, if they exist. - **/ -static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf) + * The stat structure pointed to by stp will contain information about the + * link or the referent as appropriate, if they exist. */ +static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf) { #ifdef SUPPORT_LINKS - if (copy_links) - return do_stat(path, buffer); - if (link_stat(path, buffer, 0) < 0) + if (link_stat(path, stp, copy_dirlinks) < 0) return -1; - if (S_ISLNK(buffer->st_mode)) { - int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1); - if (l == -1) + if (S_ISLNK(stp->st_mode)) { + int llen = readlink(path, linkbuf, MAXPATHLEN - 1); + if (llen < 0) return -1; - linkbuf[l] = 0; + linkbuf[llen] = '\0'; if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) { if (verbose > 1) { rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n", path, linkbuf); } - return do_stat(path, buffer); + return do_stat(path, stp); } } return 0; #else - return do_stat(path, buffer); + return do_stat(path, stp); #endif } -int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks) +int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks) { #ifdef SUPPORT_LINKS if (copy_links) - return do_stat(path, buffer); - if (do_lstat(path, buffer) < 0) + return do_stat(path, stp); + if (do_lstat(path, stp) < 0) return -1; - if (follow_dirlinks && S_ISLNK(buffer->st_mode)) { + if (follow_dirlinks && S_ISLNK(stp->st_mode)) { STRUCT_STAT st; if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode)) - *buffer = st; + *stp = st; } return 0; #else - return do_stat(path, buffer); + return do_stat(path, stp); #endif } @@ -248,23 +243,27 @@ static int is_excluded(char *fname, int is_dir, int filter_level) static int to_wire_mode(mode_t mode) { #ifdef SUPPORT_LINKS - if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) +#if _S_IFLNK != 0120000 + if (S_ISLNK(mode)) return (mode & ~(_S_IFMT)) | 0120000; #endif - return (int)mode; +#endif + return mode; } static mode_t from_wire_mode(int mode) { - if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) +#if _S_IFLNK != 0120000 + if ((mode & (_S_IFMT)) == 0120000) return (mode & ~(_S_IFMT)) | _S_IFLNK; - return (mode_t)mode; +#endif + return mode; } static void send_directory(int f, struct file_list *flist, char *fbuf, int len); -static char *flist_dir; +static const char *flist_dir; static int flist_dir_len; @@ -297,7 +296,7 @@ void flist_expand(struct file_list *flist) flist->malloced); if (verbose >= 2 && flist->malloced != FLIST_START) { - rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n", + rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n", who_am_i(), (double)sizeof flist->files[0] * flist->malloced, (new_ptr == flist->files) ? " not" : ""); @@ -329,18 +328,16 @@ static void send_file_entry(struct file_struct *file, int f) if (!file) { write_byte(f, 0); modtime = 0, mode = 0; - dev = 0, rdev = makedev(0, 0); + dev = 0, rdev = MAKEDEV(0, 0); rdev_major = 0; uid = 0, gid = 0; *lastname = '\0'; return; } - io_write_phase = "send_file_entry"; - f_name(file, fname); - flags = file->flags & XMIT_TOP_DIR; + flags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */ if (file->mode == mode) flags |= XMIT_SAME_MODE; @@ -349,12 +346,12 @@ static void send_file_entry(struct file_struct *file, int f) if ((preserve_devices && IS_DEVICE(mode)) || (preserve_specials && IS_SPECIAL(mode))) { if (protocol_version < 28) { - if (file->u.rdev == rdev) + if (tmp_rdev == rdev) flags |= XMIT_SAME_RDEV_pre28; else - rdev = file->u.rdev; + rdev = tmp_rdev; } else { - rdev = file->u.rdev; + rdev = tmp_rdev; if ((uint32)major(rdev) == rdev_major) flags |= XMIT_SAME_RDEV_MAJOR; else @@ -363,27 +360,31 @@ static void send_file_entry(struct file_struct *file, int f) flags |= XMIT_RDEV_MINOR_IS_SMALL; } } else if (protocol_version < 28) - rdev = makedev(0, 0); - if (file->uid == uid) - flags |= XMIT_SAME_UID; - else - uid = file->uid; - if (file->gid == gid) - flags |= XMIT_SAME_GID; - else - gid = file->gid; + rdev = MAKEDEV(0, 0); + if (preserve_uid) { + if (F_UID(file) == uid) + flags |= XMIT_SAME_UID; + else + uid = F_UID(file); + } + if (preserve_gid) { + if (F_GID(file) == gid) + flags |= XMIT_SAME_GID; + else + gid = F_GID(file); + } if (file->modtime == modtime) flags |= XMIT_SAME_TIME; else modtime = file->modtime; #ifdef SUPPORT_HARD_LINKS - if (file->link_u.idev) { - if (file->F_DEV == dev) { + if (tmp_idev.dev != 0) { + if (tmp_idev.dev == dev) { if (protocol_version >= 28) flags |= XMIT_SAME_DEV; } else - dev = file->F_DEV; + dev = tmp_idev.dev; flags |= XMIT_HAS_IDEV_DATA; } #endif @@ -407,8 +408,7 @@ static void send_file_entry(struct file_struct *file, int f) flags |= XMIT_TOP_DIR; if ((flags & 0xFF00) || !flags) { flags |= XMIT_EXTENDED_FLAGS; - write_byte(f, flags); - write_byte(f, flags >> 8); + write_shortint(f, flags); } else write_byte(f, flags); } else { @@ -456,31 +456,32 @@ static void send_file_entry(struct file_struct *file, int f) #ifdef SUPPORT_LINKS if (preserve_links && S_ISLNK(mode)) { - int len = strlen(file->u.link); + const char *sl = F_SYMLINK(file); + int len = strlen(sl); write_int(f, len); - write_buf(f, file->u.link, len); + write_buf(f, sl, len); } #endif #ifdef SUPPORT_HARD_LINKS - if (flags & XMIT_HAS_IDEV_DATA) { + if (tmp_idev.dev != 0) { if (protocol_version < 26) { /* 32-bit dev_t and ino_t */ - write_int(f, dev); - write_int(f, file->F_INODE); + write_int(f, (int32)dev); + write_int(f, (int32)tmp_idev.ino); } else { /* 64-bit dev_t and ino_t */ if (!(flags & XMIT_SAME_DEV)) write_longint(f, dev); - write_longint(f, file->F_INODE); + write_longint(f, tmp_idev.ino); } } #endif if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) { - char *sum; + const char *sum; if (S_ISREG(mode)) - sum = file->u.sum; + sum = tmp_sum; else { /* Prior to 28, we sent a useless set of nulls. */ sum = empty_sum; @@ -489,12 +490,10 @@ static void send_file_entry(struct file_struct *file, int f) } strlcpy(lastname, fname, MAXPATHLEN); - - io_write_phase = "unknown"; } -static struct file_struct *receive_file_entry(struct file_list *flist, - unsigned short flags, int f) +static struct file_struct *recv_file_entry(struct file_list *flist, + unsigned short flags, int f) { static time_t modtime; static mode_t mode; @@ -510,13 +509,14 @@ static struct file_struct *receive_file_entry(struct file_list *flist, char thisname[MAXPATHLEN]; unsigned int l1 = 0, l2 = 0; int alloc_len, basename_len, dirname_len, linkname_len, sum_len; + int extra_len = (flist_extra_ndx-1) * sizeof (union flist_extras); OFF_T file_length; char *basename, *dirname, *bp; struct file_struct *file; if (!flist) { modtime = 0, mode = 0; - dev = 0, rdev = makedev(0, 0); + dev = 0, rdev = MAKEDEV(0, 0); rdev_major = 0; uid = 0, gid = 0; *lastname = '\0'; @@ -535,9 +535,9 @@ static struct file_struct *receive_file_entry(struct file_list *flist, if (l2 >= MAXPATHLEN - l1) { rprintf(FERROR, - "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n", - flags, l1, l2, lastname); - overflow_exit("receive_file_entry"); + "overflow: flags=0x%x l1=%d l2=%d lastname=%s [%s]\n", + flags, l1, l2, lastname, who_am_i()); + overflow_exit("recv_file_entry"); } strlcpy(thisname, lastname, l1 + 1); @@ -549,7 +549,7 @@ static struct file_struct *receive_file_entry(struct file_list *flist, clean_fname(thisname, 0); if (sanitize_paths) - sanitize_path(thisname, thisname, "", 0); + sanitize_path(thisname, thisname, "", 0, NULL); if ((basename = strrchr(thisname, '/')) != NULL) { dirname_len = ++basename - thisname; /* counts future '\0' */ @@ -593,10 +593,11 @@ static struct file_struct *receive_file_entry(struct file_list *flist, rdev_minor = read_byte(f); else rdev_minor = read_int(f); - rdev = makedev(rdev_major, rdev_minor); + rdev = MAKEDEV(rdev_major, rdev_minor); } + extra_len += 2 * sizeof (union flist_extras); } else if (protocol_version < 28) - rdev = makedev(0, 0); + rdev = MAKEDEV(0, 0); #ifdef SUPPORT_LINKS if (preserve_links && S_ISLNK(mode)) { @@ -604,28 +605,44 @@ static struct file_struct *receive_file_entry(struct file_list *flist, if (linkname_len <= 0 || linkname_len > MAXPATHLEN) { rprintf(FERROR, "overflow: linkname_len=%d\n", linkname_len - 1); - overflow_exit("receive_file_entry"); + overflow_exit("recv_file_entry"); } } else #endif linkname_len = 0; +#ifdef SUPPORT_HARD_LINKS + if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode)) + flags |= XMIT_HAS_IDEV_DATA; + if (flags & XMIT_HAS_IDEV_DATA) { + extra_len += sizeof (union flist_extras); + assert(flist->hlink_pool != NULL); + } +#endif + sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0; alloc_len = file_struct_len + dirname_len + basename_len - + linkname_len + sum_len; - bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry"); + + linkname_len + sum_len + extra_len; + bp = pool_alloc(flist->file_pool, alloc_len, "recv_file_entry"); + memset(bp, 0, file_struct_len + extra_len); + bp += extra_len; file = (struct file_struct *)bp; - memset(bp, 0, file_struct_len); - bp += file_struct_len; + bp += file_struct_len + linkname_len + sum_len; +#ifdef SUPPORT_HARD_LINKS + if (flags & XMIT_HAS_IDEV_DATA) + file->flags |= FLAG_HLINK_INFO; +#endif file->modtime = modtime; file->length = file_length; file->mode = mode; - file->uid = uid; - file->gid = gid; + if (preserve_uid) + F_UID(file) = uid; + if (preserve_gid) + F_GID(file) = gid; if (dirname_len) { file->dirname = lastdir = bp; @@ -635,8 +652,6 @@ static struct file_struct *receive_file_entry(struct file_list *flist, bp[-1] = '\0'; lastdir_depth = count_dir_elements(lastdir); file->dir.depth = lastdir_depth + 1; - if (lastdir_depth >= max_dir_depth) - max_dir_depth = lastdir_depth + 1; } else if (dirname) { file->dirname = dirname; /* we're reusing lastname */ file->dir.depth = lastdir_depth + 1; @@ -653,12 +668,12 @@ static struct file_struct *receive_file_entry(struct file_list *flist, && lastname[del_hier_name_len-1] == '.' && lastname[del_hier_name_len-2] == '/') del_hier_name_len -= 2; - file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE; + file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR; } else if (in_del_hier) { if (!relative_paths || !del_hier_name_len || (l1 >= del_hier_name_len && lastname[del_hier_name_len] == '/')) - file->flags |= FLAG_DEL_HERE; + file->flags |= FLAG_XFER_DIR; else in_del_hier = 0; } @@ -669,57 +684,45 @@ static struct file_struct *receive_file_entry(struct file_list *flist, bp += basename_len; if ((preserve_devices && IS_DEVICE(mode)) - || (preserve_specials && IS_SPECIAL(mode))) - file->u.rdev = rdev; + || (preserve_specials && IS_SPECIAL(mode))) { + F_DMAJOR(file) = major(rdev); + F_DMINOR(file) = minor(rdev); + } #ifdef SUPPORT_LINKS if (linkname_len) { - file->u.link = bp; + bp = F_SYMLINK(file); read_sbuf(f, bp, linkname_len - 1); if (sanitize_paths) - sanitize_path(bp, bp, "", lastdir_depth); - bp += linkname_len; + sanitize_path(bp, bp, "", lastdir_depth, NULL); } #endif #ifdef SUPPORT_HARD_LINKS - if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode)) - flags |= XMIT_HAS_IDEV_DATA; if (flags & XMIT_HAS_IDEV_DATA) { - int64 inode; + struct idev *idevp = pool_talloc(flist->hlink_pool, struct idev, + 1, "inode_table"); + F_IDEV(file) = idevp; if (protocol_version < 26) { - dev = read_int(f); - inode = read_int(f); + idevp->dev = read_int(f); + idevp->ino = read_int(f); } else { if (!(flags & XMIT_SAME_DEV)) dev = read_longint(f); - inode = read_longint(f); - } - if (flist->hlink_pool) { - file->link_u.idev = pool_talloc(flist->hlink_pool, - struct idev, 1, "inode_table"); - file->F_INODE = inode; - file->F_DEV = dev; + idevp->dev = dev; + idevp->ino = read_longint(f); } } #endif if (always_checksum && (sum_len || protocol_version < 28)) { - char *sum; - if (sum_len) { - file->u.sum = sum = bp; - /*bp += sum_len;*/ - } else { + if (sum_len) + bp = F_SUM(file); + else { /* Prior to 28, we get a useless set of nulls. */ - sum = empty_sum; + bp = tmp_sum; } - read_buf(f, sum, checksum_len); - } - - if (!preserve_perms) { - /* set an appropriate set of permissions based on original - * permissions and umask. This emulates what GNU cp does */ - file->mode &= ~orig_umask; + read_buf(f, bp, checksum_len); } return file; @@ -740,18 +743,17 @@ static struct file_struct *receive_file_entry(struct file_list *flist, * statting directories if we're not recursing, but this is not a very * important case. Some systems may not have d_type. **/ -struct file_struct *make_file(char *fname, struct file_list *flist, - STRUCT_STAT *stp, unsigned short flags, - int filter_level) +struct file_struct *make_file(const char *fname, struct file_list *flist, + STRUCT_STAT *stp, int flags, int filter_level) { static char *lastdir; static int lastdir_len = -1; struct file_struct *file; STRUCT_STAT st; - char sum[SUM_LENGTH]; char thisname[MAXPATHLEN]; char linkname[MAXPATHLEN]; - int alloc_len, basename_len, dirname_len, linkname_len, sum_len; + int alloc_len, basename_len, dirname_len, linkname_len; + int extra_len = (flist_extra_ndx-1) * sizeof (union flist_extras); char *basename, *dirname, *bp; if (!flist || !flist->count) /* Ignore lastdir when invalid. */ @@ -764,18 +766,21 @@ struct file_struct *make_file(char *fname, struct file_list *flist, } clean_fname(thisname, 0); if (sanitize_paths) - sanitize_path(thisname, thisname, "", 0); - - memset(sum, 0, SUM_LENGTH); + sanitize_path(thisname, thisname, "", 0, NULL); - if (stp && S_ISDIR(stp->st_mode)) + if (stp && S_ISDIR(stp->st_mode)) { st = *stp; /* Needed for "symlink/." with --relative. */ - else if (readlink_stat(thisname, &st, linkname) != 0) { + *linkname = '\0'; /* make IBM code checker happy */ + } else if (readlink_stat(thisname, &st, linkname) != 0) { int save_errno = errno; /* See if file is excluded before reporting an error. */ if (filter_level != NO_FILTERS - && is_excluded(thisname, 0, filter_level)) + && (is_excluded(thisname, 0, filter_level) + || is_excluded(thisname, 1, filter_level))) { + if (ignore_perishable && save_errno != ENOENT) + non_perishable_cnt++; return NULL; + } if (save_errno == ENOENT) { #ifdef SUPPORT_LINKS /* Avoid "vanished" error if symlink points nowhere. */ @@ -810,7 +815,7 @@ struct file_struct *make_file(char *fname, struct file_list *flist, return NULL; } - /* We only care about directories because we need to avoid recursing + /* -x only affects directories because we need to avoid recursing * into a mount-point directory, not to avoid copying a symlinked * file if -L (or similar) was specified. */ if (one_file_system && st.st_dev != filesystem_dev @@ -822,11 +827,14 @@ struct file_struct *make_file(char *fname, struct file_list *flist, } return NULL; } - flags |= FLAG_MOUNT_POINT; + flags |= FLAG_MOUNT_DIR; } - if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) + if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) { + if (ignore_perishable) + non_perishable_cnt++; return NULL; + } if (lp_ignore_nonreadable(module_id)) { #ifdef SUPPORT_LINKS @@ -864,11 +872,8 @@ struct file_struct *make_file(char *fname, struct file_list *flist, linkname_len = 0; #endif - sum_len = always_checksum && am_sender && S_ISREG(st.st_mode) - ? MD4_SUM_LENGTH : 0; - alloc_len = file_struct_len + dirname_len + basename_len - + linkname_len + sum_len; + + linkname_len + extra_len; if (flist) bp = pool_alloc(flist->file_pool, alloc_len, "make_file"); else { @@ -876,36 +881,31 @@ struct file_struct *make_file(char *fname, struct file_list *flist, out_of_memory("make_file"); } + memset(bp, 0, file_struct_len + extra_len); + bp += extra_len; file = (struct file_struct *)bp; - memset(bp, 0, file_struct_len); - bp += file_struct_len; + bp += file_struct_len + linkname_len; + +#ifdef SUPPORT_HARD_LINKS + if (preserve_hard_links && flist) { + if (protocol_version >= 28 + ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1) + : S_ISREG(st.st_mode)) { + tmp_idev.dev = st.st_dev; + tmp_idev.ino = st.st_ino; + } else + tmp_idev.dev = tmp_idev.ino = 0; + } +#endif file->flags = flags; file->modtime = st.st_mtime; file->length = st.st_size; file->mode = st.st_mode; - file->uid = st.st_uid; - file->gid = st.st_gid; - -#ifdef SUPPORT_HARD_LINKS - if (flist && flist->hlink_pool) { - if (protocol_version < 28) { - if (S_ISREG(st.st_mode)) - file->link_u.idev = pool_talloc( - flist->hlink_pool, struct idev, 1, - "inode_table"); - } else { - if (!S_ISDIR(st.st_mode) && st.st_nlink > 1) - file->link_u.idev = pool_talloc( - flist->hlink_pool, struct idev, 1, - "inode_table"); - } - } - if (file->link_u.idev) { - file->F_DEV = st.st_dev; - file->F_INODE = st.st_ino; - } -#endif + if (preserve_uid) + F_UID(file) = st.st_uid; + if (preserve_gid) + F_GID(file) = st.st_gid; if (dirname_len) { file->dirname = lastdir = bp; @@ -923,22 +923,16 @@ struct file_struct *make_file(char *fname, struct file_list *flist, #ifdef HAVE_STRUCT_STAT_ST_RDEV if ((preserve_devices && IS_DEVICE(st.st_mode)) || (preserve_specials && IS_SPECIAL(st.st_mode))) - file->u.rdev = st.st_rdev; + tmp_rdev = st.st_rdev; #endif #ifdef SUPPORT_LINKS - if (linkname_len) { - file->u.link = bp; - memcpy(bp, linkname, linkname_len); - bp += linkname_len; - } + if (linkname_len) + memcpy(F_SYMLINK(file), linkname, linkname_len); #endif - if (sum_len) { - file->u.sum = bp; - file_checksum(thisname, bp, st.st_size); - /*bp += sum_len;*/ - } + if (always_checksum && am_sender && S_ISREG(st.st_mode)) + file_checksum(thisname, tmp_sum, st.st_size); file->dir.root = flist_dir; @@ -953,9 +947,10 @@ struct file_struct *make_file(char *fname, struct file_list *flist, file->modtime = st2.st_mtime; file->length = st2.st_size; file->mode = st2.st_mode; - file->uid = st2.st_uid; - file->gid = st2.st_gid; - file->u.link = NULL; + if (preserve_uid) + F_UID(file) = st2.st_uid; + if (preserve_gid) + F_GID(file) = st2.st_gid; } else file->mode = save_mode; } @@ -963,9 +958,20 @@ struct file_struct *make_file(char *fname, struct file_list *flist, if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) stats.total_size += st.st_size; + if (basename_len == 0+1) + return NULL; + return file; } +/* Only called for temporary file_struct entries. */ +void unmake_file(struct file_struct *file) +{ + union flist_extras *start = (union flist_extras *)file + - (flist_extra_ndx - 1); + free(start); +} + static struct file_struct *send_file_name(int f, struct file_list *flist, char *fname, STRUCT_STAT *stp, unsigned short flags) @@ -983,11 +989,8 @@ static struct file_struct *send_file_name(int f, struct file_list *flist, maybe_emit_filelist_progress(flist->count + flist_count_offset); flist_expand(flist); - - if (file->basename[0]) { - flist->files[flist->count++] = file; - send_file_entry(file, f); - } + flist->files[flist->count++] = file; + send_file_entry(file, f); return file; } @@ -998,7 +1001,7 @@ static void send_if_directory(int f, struct file_list *flist, char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/'); if (S_ISDIR(file->mode) - && !(file->flags & FLAG_MOUNT_POINT) && f_name(file, fbuf)) { + && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) { void *save_filters; unsigned int len = strlen(fbuf); if (len > 1 && fbuf[len-1] == '/') @@ -1087,17 +1090,18 @@ struct file_list *send_file_list(int f, int argc, char *argv[]) int64 start_write; int use_ff_fd = 0; + rprintf(FLOG, "building file list\n"); if (show_filelist_p()) start_filelist_progress("building file list"); start_write = stats.total_written; gettimeofday(&start_tv, NULL); - flist = flist_new(WITH_HLINK, "send_file_list"); + flist = flist_new(0, "send_file_list"); io_start_buffering_out(); if (filesfrom_fd >= 0) { - if (argv[0] && !push_dir(argv[0])) { + if (argv[0] && !push_dir(argv[0], 0)) { rsyserr(FERROR, errno, "push_dir %s failed", full_fname(argv[0])); exit_cleanup(RERR_FILESELECT); @@ -1113,13 +1117,13 @@ struct file_list *send_file_list(int f, int argc, char *argv[]) if (use_ff_fd) { if (read_filesfrom_line(filesfrom_fd, fbuf) == 0) break; - sanitize_path(fbuf, fbuf, "", 0); + sanitize_path(fbuf, fbuf, "", 0, NULL); } else { if (argc-- == 0) break; strlcpy(fbuf, *argv++, MAXPATHLEN); if (sanitize_paths) - sanitize_path(fbuf, fbuf, "", 0); + sanitize_path(fbuf, fbuf, "", 0, NULL); } len = strlen(fbuf); @@ -1150,7 +1154,7 @@ struct file_list *send_file_list(int f, int argc, char *argv[]) && (len == 1 || fbuf[len-2] == '/'); } - if (link_stat(fbuf, &st, keep_dirlinks) != 0) { + if (link_stat(fbuf, &st, copy_dirlinks) != 0) { io_error |= IOERR_GENERAL; rsyserr(FERROR, errno, "link_stat %s failed", full_fname(fbuf)); @@ -1207,6 +1211,8 @@ struct file_list *send_file_list(int f, int argc, char *argv[]) } else break; } + if (len == 1 && fn[0] == '/') + fn[len++] = '.'; fn[len] = '\0'; /* Reject a ".." dir in the active part of the path. */ for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) { @@ -1226,12 +1232,12 @@ struct file_list *send_file_list(int f, int argc, char *argv[]) } if (dir && *dir) { - static char *lastdir; + static const char *lastdir; static int lastdir_len; strlcpy(olddir, curr_dir, sizeof olddir); - if (!push_dir(dir)) { + if (!push_dir(dir, 0)) { io_error |= IOERR_GENERAL; rsyserr(FERROR, errno, "push_dir %s failed", full_fname(dir)); @@ -1265,7 +1271,7 @@ struct file_list *send_file_list(int f, int argc, char *argv[]) if (fn != p || (*lp && *lp != '/')) { int save_copy_links = copy_links; int save_xfer_dirs = xfer_dirs; - copy_links = copy_unsafe_links; + copy_links |= copy_unsafe_links; xfer_dirs = 1; while ((slash = strchr(slash+1, '/')) != 0) { *slash = '\0'; @@ -1318,18 +1324,14 @@ struct file_list *send_file_list(int f, int argc, char *argv[]) stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000 + (end_tv.tv_usec - start_tv.tv_usec) / 1000; - if (flist->hlink_pool) { - pool_destroy(flist->hlink_pool); - flist->hlink_pool = NULL; - } - /* Sort the list without removing any duplicates. This allows the * receiving side to ask for any name they like, which gives us the * flexibility to change the way we unduplicate names in the future * without causing a compatibility problem with older versions. */ clean_flist(flist, 0, 0); - send_uid_list(f); + if (!numeric_ids) + send_uid_list(f); /* send the io_error flag */ write_int(f, lp_ignore_errors(module_id) ? 0 : io_error); @@ -1353,6 +1355,7 @@ struct file_list *recv_file_list(int f) unsigned short flags; int64 start_read; + rprintf(FLOG, "receiving file list\n"); if (show_filelist_p()) start_filelist_progress("receiving file list"); @@ -1360,11 +1363,6 @@ struct file_list *recv_file_list(int f) flist = flist_new(WITH_HLINK, "recv_file_list"); - flist->count = 0; - flist->malloced = 1000; - flist->files = new_array(struct file_struct *, flist->malloced); - if (!flist->files) - goto oom; while ((flags = read_byte(f)) != 0) { struct file_struct *file; @@ -1373,9 +1371,9 @@ struct file_list *recv_file_list(int f) if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS)) flags |= read_byte(f) << 8; - file = receive_file_entry(flist, flags, f); + file = recv_file_entry(flist, flags, f); - if (S_ISREG(file->mode)) + if (S_ISREG(file->mode) || S_ISLNK(file->mode)) stats.total_size += file->length; flist->files[flist->count++] = file; @@ -1387,7 +1385,7 @@ struct file_list *recv_file_list(int f) f_name(file, NULL)); } } - receive_file_entry(NULL, 0, 0); /* Signal that we're done. */ + recv_file_entry(NULL, 0, 0); /* Signal that we're done. */ if (verbose > 2) rprintf(FINFO, "received %d names\n", flist->count); @@ -1423,10 +1421,6 @@ struct file_list *recv_file_list(int f) stats.num_files = flist->count; return flist; - - oom: - out_of_memory("recv_file_list"); - return NULL; /* not reached */ } static int file_compare(struct file_struct **file1, struct file_struct **file2) @@ -1439,7 +1433,7 @@ static int file_compare(struct file_struct **file1, struct file_struct **file2) int flist_find(struct file_list *flist, struct file_struct *f) { int low = flist->low, high = flist->high; - int ret, mid, mid_up; + int diff, mid, mid_up; while (low <= high) { mid = (low + high) / 2; @@ -1449,38 +1443,38 @@ int flist_find(struct file_list *flist, struct file_struct *f) /* Scan for the next non-empty entry using the cached * distance values. If the value isn't fully up-to- * date, update it. */ - while (1) { - mid_up = mid + flist->files[mid]->dir.depth; - if (flist->files[mid_up]->basename) - break; - flist->files[mid]->dir.depth - += flist->files[mid_up]->dir.depth; + mid_up = mid + flist->files[mid]->dir.depth; + if (!flist->files[mid_up]->basename) { + do { + mid_up += flist->files[mid_up]->dir.depth; + } while (!flist->files[mid_up]->basename); + flist->files[mid]->dir.depth = mid_up - mid; } if (mid_up > high) { /* If there's nothing left above us, set high to * a non-empty entry below us and continue. */ - while (1) { - high = mid - flist->files[mid]->length; - if (flist->files[high]->basename) - break; - flist->files[mid]->length - += flist->files[high]->length; + high = mid - (int)flist->files[mid]->length; + if (!flist->files[high]->basename) { + do { + high -= (int)flist->files[high]->length; + } while (!flist->files[high]->basename); + flist->files[mid]->length = mid - high; } continue; } } - ret = f_name_cmp(flist->files[mid_up], f); - if (ret == 0) { + diff = f_name_cmp(flist->files[mid_up], f); + if (diff == 0) { if (protocol_version < 29 && S_ISDIR(flist->files[mid_up]->mode) != S_ISDIR(f->mode)) return -1; return mid_up; } - if (ret > 0) - high = mid - 1; - else + if (diff < 0) low = mid_up + 1; + else + high = mid - 1; } return -1; } @@ -1489,16 +1483,13 @@ int flist_find(struct file_list *flist, struct file_struct *f) * Free up any resources a file_struct has allocated * and clear the file. */ -void clear_file(int i, struct file_list *flist) +void clear_file(struct file_struct *file) { - struct file_struct *file = flist->files[i]; - if (flist->hlink_pool && file->link_u.idev) - pool_free(flist->hlink_pool, 0, file->link_u.idev); memset(file, 0, file_struct_len); /* In an empty entry, dir.depth is an offset to the next non-empty * entry. Likewise for length in the opposite direction. We assume - * that we're alone for now since flist_find() will collate adjacent - * items for any entries that are encountered during the find. */ + * that we're alone for now since flist_find() will adjust the counts + * it runs into that aren't up-to-date. */ file->length = file->dir.depth = 1; } @@ -1513,10 +1504,9 @@ struct file_list *flist_new(int with_hlink, char *msg) if (!flist) out_of_memory(msg); - memset(flist, 0, sizeof (struct file_list)); + memset(flist, 0, sizeof flist[0]); - if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, - out_of_memory, POOL_INTERN))) + if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, out_of_memory, POOL_INTERN))) out_of_memory(msg); #ifdef SUPPORT_HARD_LINKS @@ -1547,6 +1537,7 @@ void flist_free(struct file_list *flist) */ static void clean_flist(struct file_list *flist, int strip_root, int no_dups) { + char fbuf[MAXPATHLEN]; int i, prev_i = 0; if (!flist) @@ -1600,14 +1591,14 @@ static void clean_flist(struct file_list *flist, int strip_root, int no_dups) if (verbose > 1 && !am_server) { rprintf(FINFO, "removing duplicate name %s from file list (%d)\n", - f_name(file, NULL), drop); + f_name(file, fbuf), drop); } /* Make sure we don't lose track of a user-specified * top directory. */ flist->files[keep]->flags |= flist->files[drop]->flags - & (FLAG_TOP_DIR|FLAG_DEL_HERE); + & (FLAG_TOP_DIR|FLAG_XFER_DIR); - clear_file(drop, flist); + clear_file(flist->files[drop]); if (keep == i) { if (flist->low == drop) { @@ -1638,33 +1629,58 @@ static void clean_flist(struct file_list *flist, int strip_root, int no_dups) } } - if (skip_empty_dirs && no_dups && max_dir_depth) { - int j, cur_depth = 0; - int *maybe_dirs = new_array(int, max_dir_depth); + if (prune_empty_dirs && no_dups) { + int j, prev_depth = 0; - maybe_dirs[0] = -1; + prev_i = 0; /* It's OK that this isn't really true. */ for (i = flist->low; i <= flist->high; i++) { - struct file_struct *file = flist->files[i]; + struct file_struct *fp, *file = flist->files[i]; + /* This temporarily abuses the dir.depth value for a + * directory that is in a chain that might get pruned. + * We restore the old value if it gets a reprieve. */ if (S_ISDIR(file->mode) && file->dir.depth) { - j = cur_depth; - cur_depth = file->dir.depth - 1; - for ( ; j >= cur_depth; j--) { - if (maybe_dirs[j] < 0) - continue; - clear_file(maybe_dirs[j], flist); + /* Dump empty dirs when coming back down. */ + for (j = prev_depth; j >= file->dir.depth; j--) { + fp = flist->files[prev_i]; + if (fp->dir.depth >= 0) + break; + prev_i = -fp->dir.depth-1; + clear_file(fp); + } + prev_depth = file->dir.depth; + if (is_excluded(f_name(file, fbuf), 1, + ALL_FILTERS)) { + /* Keep dirs through this dir. */ + for (j = prev_depth-1; ; j--) { + fp = flist->files[prev_i]; + if (fp->dir.depth >= 0) + break; + prev_i = -fp->dir.depth-1; + fp->dir.depth = j; + } + } else + file->dir.depth = -prev_i-1; + prev_i = i; + } else { + /* Keep dirs through this non-dir. */ + for (j = prev_depth; ; j--) { + fp = flist->files[prev_i]; + if (fp->dir.depth >= 0) + break; + prev_i = -fp->dir.depth-1; + fp->dir.depth = j; } - maybe_dirs[cur_depth] = i; - } else if (maybe_dirs[cur_depth] >= 0) { - for (j = 0; j <= cur_depth; j++) - maybe_dirs[j] = -1; } } - for (j = cur_depth; j >= 0; j--) { - if (maybe_dirs[j] < 0) - continue; - clear_file(maybe_dirs[j], flist); + /* Dump empty all remaining empty dirs. */ + while (1) { + struct file_struct *fp = flist->files[prev_i]; + if (fp->dir.depth >= 0) + break; + prev_i = -fp->dir.depth-1; + clear_file(fp); } for (i = flist->low; i <= flist->high; i++) { @@ -1689,16 +1705,18 @@ static void output_flist(struct file_list *flist) for (i = 0; i < flist->count; i++) { file = flist->files[i]; - if ((am_root || am_sender) && preserve_uid) - sprintf(uidbuf, " uid=%ld", (long)file->uid); - else + if ((am_root || am_sender) && preserve_uid) { + snprintf(uidbuf, sizeof uidbuf, " uid=%ld", + (long)F_UID(file)); + } else *uidbuf = '\0'; - if (preserve_gid && file->gid != GID_NONE) - sprintf(gidbuf, " gid=%ld", (long)file->gid); - else + if (preserve_gid && F_GID(file) != GID_NONE) { + snprintf(gidbuf, sizeof gidbuf, " gid=%ld", + (long)F_GID(file)); + } else *gidbuf = '\0'; if (!am_sender) - sprintf(depthbuf, "%d", file->dir.depth); + snprintf(depthbuf, sizeof depthbuf, "%d", file->dir.depth); rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n", who, i, am_sender ? NS(file->dir.root) : depthbuf, file->dirname ? file->dirname : "", @@ -1755,10 +1773,6 @@ int f_name_cmp(struct file_struct *f1, struct file_struct *f2) c1 = (uchar*)""; } else state1 = s_BASE; - } else if (!*c1) { - type1 = t_path; - state1 = s_SLASH; - c1 = (uchar*)"/"; } else { type1 = t_path; state1 = s_DIR; @@ -1772,10 +1786,6 @@ int f_name_cmp(struct file_struct *f1, struct file_struct *f2) c2 = (uchar*)""; } else state2 = s_BASE; - } else if (!*c2) { - type2 = t_path; - state2 = s_SLASH; - c2 = (uchar*)"/"; } else { type2 = t_path; state2 = s_DIR; @@ -1784,9 +1794,7 @@ int f_name_cmp(struct file_struct *f1, struct file_struct *f2) if (type1 != type2) return type1 == t_PATH ? 1 : -1; - while (1) { - if ((dif = (int)*c1++ - (int)*c2++) != 0) - break; + do { if (!*c1) { switch (state1) { case s_DIR: @@ -1849,7 +1857,7 @@ int f_name_cmp(struct file_struct *f1, struct file_struct *f2) if (type1 != type2) return type1 == t_PATH ? 1 : -1; } - } + } while ((dif = (int)*c1++ - (int)*c2++) == 0); return dif; } @@ -1876,9 +1884,9 @@ char *f_name(struct file_struct *f, char *fbuf) int len = strlen(f->dirname); memcpy(fbuf, f->dirname, len); fbuf[len] = '/'; - strcpy(fbuf + len + 1, f->basename); + strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1)); } else - strcpy(fbuf, f->basename); + strlcpy(fbuf, f->basename, MAXPATHLEN); return fbuf; } @@ -1894,6 +1902,7 @@ struct file_list *get_dirlist(char *dirname, int dlen, struct file_list *dirlist; char dirbuf[MAXPATHLEN]; int save_recurse = recurse; + int save_xfer_dirs = xfer_dirs; if (dlen < 0) { dlen = strlcpy(dirbuf, dirname, MAXPATHLEN); @@ -1905,7 +1914,9 @@ struct file_list *get_dirlist(char *dirname, int dlen, dirlist = flist_new(WITHOUT_HLINK, "get_dirlist"); recurse = 0; + xfer_dirs = 1; send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen); + xfer_dirs = save_xfer_dirs; recurse = save_recurse; if (do_progress) flist_count_offset += dirlist->count;