From 82ad07c4182f744c07b96a15df4572e559ed7dc8 Mon Sep 17 00:00:00 2001 From: Wayne Davison Date: Sun, 3 Dec 2006 06:44:16 +0000 Subject: [PATCH] - Typical tranfers now save 12-20 bytes per file because several vars were moved out of file_struct into an optional member-var setup. - Renamed and reorganized the FLAG_* defines. - Use NDX_DONE instead of a literal -1 when sending/checking the end-of-phase index value. --- backup.c | 37 ++++--- batch.c | 19 +++- flist.c | 298 ++++++++++++++++++++++++++-------------------------- generator.c | 123 +++++++++++----------- hlink.c | 109 +++++++++++-------- log.c | 14 ++- main.c | 4 +- options.c | 7 +- receiver.c | 8 +- rsync.c | 14 +-- rsync.h | 79 ++++++++------ sender.c | 6 +- uidlist.c | 7 +- 13 files changed, 391 insertions(+), 334 deletions(-) diff --git a/backup.c b/backup.c index 2ca53fa2..2ae30b4d 100644 --- a/backup.c +++ b/backup.c @@ -22,17 +22,18 @@ #include "rsync.h" extern int verbose; -extern int backup_dir_len; -extern unsigned int backup_dir_remainder; -extern char backup_dir_buf[MAXPATHLEN]; -extern char *backup_suffix; -extern char *backup_dir; - extern int am_root; extern int preserve_devices; extern int preserve_specials; extern int preserve_links; extern int safe_symlinks; +extern int backup_dir_len; +extern int flist_extra_ndx; +extern int file_struct_len; +extern unsigned int backup_dir_remainder; +extern char backup_dir_buf[MAXPATHLEN]; +extern char *backup_suffix; +extern char *backup_dir; /* make a complete pathname for backup file */ char *get_backup_name(const char *fname) @@ -185,16 +186,19 @@ static int keep_backup(const char *fname) if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) return 1; /* the file could have disappeared */ - if (!(buf = get_backup_name(fname))) + if (!(buf = get_backup_name(fname))) { + unmake_file(file); return 0; + } /* Check to see if this is a device file, or link */ if ((am_root && preserve_devices && IS_DEVICE(file->mode)) || (preserve_specials && IS_SPECIAL(file->mode))) { + dev_t rdev = MAKEDEV(F_DMAJOR(file), F_DMINOR(file)); do_unlink(buf); - if (do_mknod(buf, file->mode, file->u.rdev) < 0 + if (do_mknod(buf, file->mode, rdev) < 0 && (errno != ENOENT || make_bak_dir(buf) < 0 - || do_mknod(buf, file->mode, file->u.rdev) < 0)) { + || do_mknod(buf, file->mode, rdev) < 0)) { rsyserr(FERROR, errno, "mknod %s failed", full_fname(buf)); } else if (verbose > 2) { @@ -224,20 +228,20 @@ static int keep_backup(const char *fname) #ifdef SUPPORT_LINKS if (!kept && preserve_links && S_ISLNK(file->mode)) { - if (safe_symlinks && unsafe_symlink(file->u.link, buf)) { + const char *sl = F_SYMLINK(file); + if (safe_symlinks && unsafe_symlink(sl, buf)) { if (verbose) { rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n", - full_fname(buf), file->u.link); + full_fname(buf), sl); } kept = 1; } else { do_unlink(buf); - if (do_symlink(file->u.link, buf) < 0 + if (do_symlink(sl, buf) < 0 && (errno != ENOENT || make_bak_dir(buf) < 0 - || do_symlink(file->u.link, buf) < 0)) { + || do_symlink(sl, buf) < 0)) { rsyserr(FERROR, errno, "link %s -> \"%s\"", - full_fname(buf), - file->u.link); + full_fname(buf), sl); } do_unlink(fname); kept = 1; @@ -248,6 +252,7 @@ static int keep_backup(const char *fname) if (!kept && !S_ISREG(file->mode)) { rprintf(FINFO, "make_bak: skipping non-regular file %s\n", fname); + unmake_file(file); return 1; } @@ -263,7 +268,7 @@ static int keep_backup(const char *fname) } } set_file_attrs(buf, file, NULL, 0); - free(file); + unmake_file(file); if (verbose > 1) { rprintf(FINFO, "backed up %s to %s\n", diff --git a/batch.c b/batch.c index 121e34ff..e0eaa39b 100644 --- a/batch.c +++ b/batch.c @@ -36,16 +36,19 @@ extern int always_checksum; extern int do_compression; extern int def_compress_level; extern int protocol_version; +extern int flist_extra_ndx; extern char *batch_name; extern struct filter_list_struct filter_list; +static int tweaked_preserve_uid; +static int tweaked_preserve_gid; static int tweaked_compress_level; static int *flag_ptr[] = { &recurse, /* 0 */ - &preserve_uid, /* 1 */ - &preserve_gid, /* 2 */ + &tweaked_preserve_uid, /* 1 */ + &tweaked_preserve_gid, /* 2 */ &preserve_links, /* 3 */ &preserve_devices, /* 4 */ &preserve_hard_links, /* 5 */ @@ -72,6 +75,8 @@ void write_stream_flags(int fd) { int i, flags; + tweaked_preserve_uid = preserve_uid != 0; + tweaked_preserve_gid = preserve_gid != 0; #if Z_DEFAULT_COMPRESSION == -1 tweaked_compress_level = do_compression ? def_compress_level + 2 : 0; #else @@ -113,6 +118,16 @@ void read_stream_flags(int fd) xfer_dirs = 0; } + if (tweaked_preserve_uid) { + if (!preserve_uid) + preserve_uid = flist_extra_ndx++; + } else + preserve_uid = 0; + if (tweaked_preserve_gid) { + if (!preserve_gid) + preserve_gid = flist_extra_ndx++; + } else + preserve_gid = 0; if (tweaked_compress_level == 0 || tweaked_compress_level == 2) do_compression = 0; else { diff --git a/flist.c b/flist.c index 325c101f..2fab393a 100644 --- a/flist.c +++ b/flist.c @@ -48,6 +48,7 @@ extern int preserve_uid; extern int preserve_gid; extern int relative_paths; extern int implied_dirs; +extern int flist_extra_ndx; extern int ignore_perishable; extern int non_perishable_cnt; extern int prune_empty_dirs; @@ -68,7 +69,14 @@ 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; @@ -140,7 +148,7 @@ static void list_file_entry(struct file_struct *f) rprintf(FINFO, "%s %11.0f %s %s -> %s\n", permbuf, (double)f->length, timestring(f->modtime), - f_name(f, NULL), f->u.link); + f_name(f, NULL), F_SYMLINK(f)); } else #endif { @@ -329,7 +337,7 @@ static void send_file_entry(struct file_struct *file, int f) 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; @@ -338,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 @@ -353,26 +361,30 @@ static void send_file_entry(struct file_struct *file, int f) } } 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; + 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 @@ -444,23 +456,24 @@ 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 (file->link_u.idev) { + if (tmp_idev.dev != 0) { if (protocol_version < 26) { /* 32-bit dev_t and ino_t */ write_int(f, (int32)dev); - write_int(f, (int32)file->F_INODE); + 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 @@ -468,7 +481,7 @@ static void send_file_entry(struct file_struct *file, int f) if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) { 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; @@ -479,8 +492,8 @@ static void send_file_entry(struct file_struct *file, int f) strlcpy(lastname, fname, MAXPATHLEN); } -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; @@ -496,6 +509,7 @@ 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; @@ -521,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); @@ -581,6 +595,7 @@ static struct file_struct *receive_file_entry(struct file_list *flist, rdev_minor = read_int(f); rdev = MAKEDEV(rdev_major, rdev_minor); } + extra_len += 2 * sizeof (union flist_extras); } else if (protocol_version < 28) rdev = MAKEDEV(0, 0); @@ -590,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; @@ -637,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; } @@ -653,51 +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, NULL); - bp += linkname_len; } #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); + read_buf(f, bp, checksum_len); } return file; @@ -719,17 +744,16 @@ static struct file_struct *receive_file_entry(struct file_list *flist, * important case. Some systems may not have d_type. **/ struct file_struct *make_file(const char *fname, struct file_list *flist, - STRUCT_STAT *stp, unsigned short flags, - int filter_level) + 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. */ @@ -744,8 +768,6 @@ struct file_struct *make_file(const char *fname, struct file_list *flist, if (sanitize_paths) sanitize_path(thisname, thisname, "", 0, NULL); - memset(sum, 0, SUM_LENGTH); - if (stp && S_ISDIR(stp->st_mode)) { st = *stp; /* Needed for "symlink/." with --relative. */ *linkname = '\0'; /* make IBM code checker happy */ @@ -805,7 +827,7 @@ struct file_struct *make_file(const 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)) { @@ -850,11 +872,8 @@ struct file_struct *make_file(const 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 { @@ -862,36 +881,31 @@ struct file_struct *make_file(const 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; @@ -909,22 +923,16 @@ struct file_struct *make_file(const 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; @@ -939,9 +947,10 @@ struct file_struct *make_file(const 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; } @@ -949,9 +958,20 @@ struct file_struct *make_file(const 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) @@ -969,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; } @@ -984,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] == '/') @@ -1080,7 +1097,7 @@ struct file_list *send_file_list(int f, int argc, char *argv[]) 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) { @@ -1307,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); @@ -1350,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; @@ -1363,7 +1371,7 @@ 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) || S_ISLNK(file->mode)) stats.total_size += file->length; @@ -1377,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); @@ -1413,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) @@ -1479,10 +1483,8 @@ 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(struct file_struct *file, struct file_list *flist) +void clear_file(struct file_struct *file) { - 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 @@ -1502,7 +1504,7 @@ 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))) out_of_memory(msg); @@ -1594,9 +1596,9 @@ static void clean_flist(struct file_list *flist, int strip_root, int no_dups) /* 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(flist->files[drop], flist); + clear_file(flist->files[drop]); if (keep == i) { if (flist->low == drop) { @@ -1645,7 +1647,7 @@ static void clean_flist(struct file_list *flist, int strip_root, int no_dups) if (fp->dir.depth >= 0) break; prev_i = -fp->dir.depth-1; - clear_file(fp, flist); + clear_file(fp); } prev_depth = file->dir.depth; if (is_excluded(f_name(file, fbuf), 1, @@ -1678,7 +1680,7 @@ static void clean_flist(struct file_list *flist, int strip_root, int no_dups) if (fp->dir.depth >= 0) break; prev_i = -fp->dir.depth-1; - clear_file(fp, flist); + clear_file(fp); } for (i = flist->low; i <= flist->high; i++) { @@ -1703,13 +1705,15 @@ 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) - snprintf(uidbuf, sizeof 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) - snprintf(gidbuf, sizeof 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) snprintf(depthbuf, sizeof depthbuf, "%d", file->dir.depth); diff --git a/generator.c b/generator.c index dacb6c78..536302f1 100644 --- a/generator.c +++ b/generator.c @@ -50,6 +50,7 @@ extern int delete_during; extern int delete_after; extern int module_id; extern int ignore_errors; +extern int flist_extra_ndx; extern int remove_source_files; extern int delay_updates; extern int update_only; @@ -85,6 +86,7 @@ extern long block_size; /* "long" because popt can't set an int32. */ extern int max_delete; extern int force_delete; extern int one_file_system; +extern int file_struct_len; extern struct stats stats; extern dev_t filesystem_dev; extern char *backup_dir; @@ -234,7 +236,7 @@ static enum delret delete_dir_contents(char *fname, int flags) for (j = dirlist->count; j--; ) { struct file_struct *fp = dirlist->files[j]; - if (fp->flags & FLAG_MOUNT_POINT) { + if (fp->flags & FLAG_MOUNT_DIR) { if (verbose > 1) { rprintf(FINFO, "mount point, %s, pins parent directory\n", @@ -393,7 +395,7 @@ static void delete_in_dir(struct file_list *flist, char *fbuf, struct file_struct *fp = dirlist->files[i]; if (!fp->basename) continue; - if (fp->flags & FLAG_MOUNT_POINT) { + if (fp->flags & FLAG_MOUNT_DIR) { if (verbose > 1) rprintf(FINFO, "cannot delete mount point: %s\n", f_name(fp, NULL)); @@ -426,7 +428,7 @@ static void do_delete_pass(struct file_list *flist) for (j = 0; j < flist->count; j++) { struct file_struct *file = flist->files[j]; - if (!(file->flags & FLAG_DEL_HERE)) + if (!(file->flags & FLAG_XFER_DIR)) continue; f_name(file, fbuf); @@ -451,10 +453,10 @@ int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st) && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS)) return 0; - if (am_root && preserve_uid && st->st_uid != file->uid) + if (am_root && preserve_uid && st->st_uid != F_UID(file)) return 0; - if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid) + if (preserve_gid && F_GID(file) != GID_NONE && st->st_gid != F_GID(file)) return 0; return 1; @@ -477,10 +479,10 @@ void itemize(struct file_struct *file, int ndx, int statret, STRUCT_STAT *st, iflags |= ITEM_REPORT_TIME; if ((file->mode & CHMOD_BITS) != (st->st_mode & CHMOD_BITS)) iflags |= ITEM_REPORT_PERMS; - if (preserve_uid && am_root && file->uid != st->st_uid) + if (preserve_uid && am_root && F_UID(file) != st->st_uid) iflags |= ITEM_REPORT_OWNER; - if (preserve_gid && file->gid != GID_NONE - && st->st_gid != file->gid) + if (preserve_gid && F_GID(file) != GID_NONE + && st->st_gid != F_GID(file)) iflags |= ITEM_REPORT_GROUP; } else iflags |= ITEM_IS_NEW; @@ -515,7 +517,7 @@ int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st) if (always_checksum && S_ISREG(st->st_mode)) { char sum[MD4_SUM_LENGTH]; file_checksum(fn, sum, st->st_size); - return memcmp(sum, file->u.sum, checksum_len) == 0; + return memcmp(sum, F_SUM(file), checksum_len) == 0; } if (size_only) @@ -677,8 +679,7 @@ static int find_fuzzy(struct file_struct *file, struct file_list *dirlist) int len, suf_len; uint32 dist; - if (!S_ISREG(fp->mode) || !fp->length - || fp->flags & FLAG_NO_FUZZY) + if (!S_ISREG(fp->mode) || !fp->length || fp->flags & FLAG_SENT) continue; name = fp->basename; @@ -723,10 +724,10 @@ void check_for_finished_hlinks(int itemizing, enum logcode code) continue; file = the_file_list->files[ndx]; - if (!file->link_u.links) + if (!IS_HLINKED(file)) continue; - hard_link_cluster(file, ndx, itemizing, code); + hard_link_cluster(file, ndx, itemizing, code, -1); } } @@ -786,11 +787,8 @@ static int try_dests_reg(struct file_struct *file, char *fname, int ndx, if (hard_link_one(file, ndx, fname, 0, stp, cmpbuf, 1, i, code) < 0) goto try_a_copy; - if (preserve_hard_links && file->link_u.links) { - if (dry_run) - file->link_u.links->link_dest_used = j + 1; - hard_link_cluster(file, ndx, itemizing, code); - } + if (preserve_hard_links && IS_HLINKED(file)) + hard_link_cluster(file, ndx, itemizing, code, j); } else #endif if (itemizing) @@ -820,8 +818,8 @@ static int try_dests_reg(struct file_struct *file, char *fname, int ndx, rprintf(code, "%s%s\n", fname, match_level == 3 ? " is uptodate" : ""); } - if (preserve_hard_links && file->link_u.links) - hard_link_cluster(file, ndx, itemizing, code); + if (preserve_hard_links && IS_HLINKED(file)) + hard_link_cluster(file, ndx, itemizing, code, j); return -2; } @@ -895,7 +893,7 @@ static int try_dests_non(struct file_struct *file, char *fname, int ndx, break; case TYPE_SPECIAL: case TYPE_DEVICE: - if (stp->st_rdev != file->u.rdev) + if (stp->st_rdev != MAKEDEV(F_DMAJOR(file), F_DMINOR(file))) continue; break; #ifdef SUPPORT_LINKS @@ -903,7 +901,7 @@ static int try_dests_non(struct file_struct *file, char *fname, int ndx, if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0) continue; lnk[len] = '\0'; - if (strcmp(lnk, file->u.link) != 0) + if (strcmp(lnk, F_SYMLINK(file)) != 0) continue; break; #endif @@ -945,8 +943,8 @@ static int try_dests_non(struct file_struct *file, char *fname, int ndx, cmpbuf, fname); return j; } - if (preserve_hard_links && file->link_u.links) - hard_link_cluster(file, ndx, itemizing, code); + if (preserve_hard_links && IS_HLINKED(file)) + hard_link_cluster(file, ndx, itemizing, code, -1); } else #endif match_level = 2; @@ -1138,7 +1136,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, rsyserr(FERROR, errno, "recv_generator: mkdir %s failed", full_fname(fname)); - file->flags |= FLAG_MISSING; + file->flags |= FLAG_MISSING_DIR; if (ndx+1 < the_file_list->count && the_file_list->files[ndx+1]->dir.depth > file->dir.depth) { rprintf(FERROR, @@ -1154,25 +1152,26 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, if (real_ret != 0 && one_file_system) real_st.st_dev = filesystem_dev; if (delete_during && f_out != -1 && !phase && dry_run < 2 - && (file->flags & FLAG_DEL_HERE)) + && (file->flags & FLAG_XFER_DIR)) delete_in_dir(the_file_list, fname, file, &real_st); return; } - if (preserve_hard_links && file->link_u.links + if (preserve_hard_links && IS_HLINKED(file) && hard_link_check(file, ndx, fname, statret, &st, itemizing, code, HL_CHECK_MASTER)) return; if (preserve_links && S_ISLNK(file->mode)) { #ifdef SUPPORT_LINKS - if (safe_symlinks && unsafe_symlink(file->u.link, fname)) { + const char *sl = F_SYMLINK(file); + if (safe_symlinks && unsafe_symlink(sl, fname)) { if (verbose) { if (the_file_list->count == 1) fname = f_name(file, NULL); rprintf(FINFO, "ignoring unsafe symlink %s -> \"%s\"\n", - full_fname(fname), file->u.link); + full_fname(fname), sl); } return; } @@ -1183,14 +1182,13 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, if (!S_ISLNK(st.st_mode)) statret = -1; else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0 - && strncmp(lnk, file->u.link, len) == 0 - && file->u.link[len] == '\0') { + && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') { /* The link is pointing to the right place. */ if (itemizing) itemize(file, ndx, 0, &st, 0, 0, NULL); set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT); - if (preserve_hard_links && file->link_u.links) - hard_link_cluster(file, ndx, itemizing, code); + if (preserve_hard_links && IS_HLINKED(file)) + hard_link_cluster(file, ndx, itemizing, code, -1); if (remove_source_files == 1) goto return_with_success; return; @@ -1215,13 +1213,13 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, } else if (j >= 0) statret = 1; } - if (preserve_hard_links && file->link_u.links + if (preserve_hard_links && IS_HLINKED(file) && hard_link_check(file, ndx, fname, -1, &st, itemizing, code, HL_SKIP)) return; - if (do_symlink(file->u.link, fname) != 0) { + if (do_symlink(sl, fname) != 0) { rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed", - full_fname(fname), file->u.link); + full_fname(fname), sl); } else { set_file_attrs(fname, file, NULL, 0); if (itemizing) { @@ -1229,9 +1227,9 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, ITEM_LOCAL_CHANGE, 0, NULL); } if (code != FNONE && verbose) - rprintf(code, "%s -> %s\n", fname, file->u.link); - if (preserve_hard_links && file->link_u.links) - hard_link_cluster(file, ndx, itemizing, code); + rprintf(code, "%s -> %s\n", fname, sl); + if (preserve_hard_links && IS_HLINKED(file)) + hard_link_cluster(file, ndx, itemizing, code, -1); /* This does not check remove_source_files == 1 * because this is one of the items that the old * --remove-sent-files option would remove. */ @@ -1244,6 +1242,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, if ((am_root && preserve_devices && IS_DEVICE(file->mode)) || (preserve_specials && IS_SPECIAL(file->mode))) { + dev_t rdev = MAKEDEV(F_DMAJOR(file), F_DMINOR(file)); if (statret == 0) { char *t; if (IS_DEVICE(file->mode)) { @@ -1257,13 +1256,13 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, } if (statret == 0 && (st.st_mode & ~CHMOD_BITS) == (file->mode & ~CHMOD_BITS) - && st.st_rdev == file->u.rdev) { + && st.st_rdev == rdev) { /* The device or special file is identical. */ if (itemizing) itemize(file, ndx, 0, &st, 0, 0, NULL); set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT); - if (preserve_hard_links && file->link_u.links) - hard_link_cluster(file, ndx, itemizing, code); + if (preserve_hard_links && IS_HLINKED(file)) + hard_link_cluster(file, ndx, itemizing, code, -1); if (remove_source_files == 1) goto return_with_success; return; @@ -1286,15 +1285,15 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, } else if (j >= 0) statret = 1; } - if (preserve_hard_links && file->link_u.links + if (preserve_hard_links && IS_HLINKED(file) && hard_link_check(file, ndx, fname, -1, &st, itemizing, code, HL_SKIP)) return; if (verbose > 2) { rprintf(FINFO,"mknod(%s,0%o,0x%x)\n", - fname, (int)file->mode, (int)file->u.rdev); + fname, (int)file->mode, (int)rdev); } - if (do_mknod(fname, file->mode, file->u.rdev) < 0) { + if (do_mknod(fname, file->mode, rdev) < 0) { rsyserr(FERROR, errno, "mknod %s failed", full_fname(fname)); } else { @@ -1305,8 +1304,8 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, } if (code != FNONE && verbose) rprintf(code, "%s\n", fname); - if (preserve_hard_links && file->link_u.links) - hard_link_cluster(file, ndx, itemizing, code); + if (preserve_hard_links && IS_HLINKED(file)) + hard_link_cluster(file, ndx, itemizing, code, -1); if (remove_source_files == 1) goto return_with_success; } @@ -1403,7 +1402,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, } if (statret != 0) { - if (preserve_hard_links && file->link_u.links + if (preserve_hard_links && IS_HLINKED(file) && hard_link_check(file, ndx, fname, statret, &st, itemizing, code, HL_SKIP)) return; @@ -1431,8 +1430,8 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, 0, 0, NULL); } set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT); - if (preserve_hard_links && file->link_u.links) - hard_link_cluster(file, ndx, itemizing, code); + if (preserve_hard_links && IS_HLINKED(file)) + hard_link_cluster(file, ndx, itemizing, code, -1); if (remove_source_files != 1) return; return_with_success: @@ -1458,7 +1457,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, if (fuzzy_dirlist) { int j = flist_find(fuzzy_dirlist, file); if (j >= 0) /* don't use changing file as future fuzzy basis */ - fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY; + fuzzy_dirlist->files[j]->flags |= FLAG_SENT; } /* open the file */ @@ -1469,7 +1468,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, full_fname(fnamecmp)); pretend_missing: /* pretend the file didn't exist */ - if (preserve_hard_links && file->link_u.links + if (preserve_hard_links && IS_HLINKED(file) && hard_link_check(file, ndx, fname, statret, &st, itemizing, code, HL_SKIP)) return; @@ -1489,7 +1488,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, if (robust_unlink(backupptr) && errno != ENOENT) { rsyserr(FERROR, errno, "unlink %s", full_fname(backupptr)); - free(back_file); + unmake_file(back_file); close(fd); return; } @@ -1497,7 +1496,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) { rsyserr(FERROR, errno, "open %s", full_fname(backupptr)); - free(back_file); + unmake_file(back_file); close(fd); return; } @@ -1529,8 +1528,8 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, } if (!do_xfers) { - if (preserve_hard_links && file->link_u.links) - hard_link_cluster(file, ndx, itemizing, code); + if (preserve_hard_links && IS_HLINKED(file)) + hard_link_cluster(file, ndx, itemizing, code, -1); return; } if (read_batch) @@ -1550,7 +1549,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, rprintf(FINFO, "backed up %s to %s\n", fname, backupptr); } - free(back_file); + unmake_file(back_file); } close(fd); @@ -1669,7 +1668,7 @@ void generate_files(int f_out, struct file_list *flist, char *local_name) if (verbose > 2) rprintf(FINFO,"generate_files phase=%d\n",phase); - write_int(f_out, -1); + write_int(f_out, NDX_DONE); /* files can cycle through the system more than once * to catch initial checksum errors */ @@ -1691,10 +1690,10 @@ void generate_files(int f_out, struct file_list *flist, char *local_name) if (verbose > 2) rprintf(FINFO,"generate_files phase=%d\n",phase); - write_int(f_out, -1); + write_int(f_out, NDX_DONE); /* Reduce round-trip lag-time for a useless delay-updates phase. */ if (protocol_version >= 29 && !delay_updates) - write_int(f_out, -1); + write_int(f_out, NDX_DONE); /* Read MSG_DONE for the redo phase (and any prior messages). */ get_redo_num(itemizing, code); @@ -1704,7 +1703,7 @@ void generate_files(int f_out, struct file_list *flist, char *local_name) if (verbose > 2) rprintf(FINFO, "generate_files phase=%d\n", phase); if (delay_updates) - write_int(f_out, -1); + write_int(f_out, NDX_DONE); /* Read MSG_DONE for delay-updates phase & prior messages. */ get_redo_num(itemizing, code); } @@ -1727,7 +1726,7 @@ void generate_files(int f_out, struct file_list *flist, char *local_name) continue; if (!need_retouch_dir_times && file->mode & S_IWUSR) continue; - if (file->flags & FLAG_MISSING) { + if (file->flags & FLAG_MISSING_DIR) { int missing = file->dir.depth; while (++i < flist->count) { file = flist->files[i]; diff --git a/hlink.c b/hlink.c index e0a5265b..d4808fc9 100644 --- a/hlink.c +++ b/hlink.c @@ -24,9 +24,11 @@ #include "rsync.h" extern int verbose; +extern int dry_run; extern int do_xfers; extern int link_dest; extern int make_backups; +extern int flist_extra_ndx; extern int remove_source_files; extern int stdout_format_has_i; extern char *basis_dir[]; @@ -38,19 +40,20 @@ extern struct file_list *the_file_list; #define FINISHED_LINK (-2) #define FPTR(i) (the_file_list->files[i]) -#define LINKED(p1,p2) (FPTR(p1)->F_DEV == FPTR(p2)->F_DEV \ - && FPTR(p1)->F_INODE == FPTR(p2)->F_INODE) +#define LINKED(i1,i2) ((i1)->dev == (i2)->dev && (i1)->ino == (i2)->ino) static int hlink_compare(int *int1, int *int2) { struct file_struct *f1 = FPTR(*int1); struct file_struct *f2 = FPTR(*int2); + struct idev *i1 = F_IDEV(f1); + struct idev *i2 = F_IDEV(f2); - if (f1->F_DEV != f2->F_DEV) - return (int) (f1->F_DEV > f2->F_DEV ? 1 : -1); + if (i1->dev != i2->dev) + return (int)(i1->dev > i2->dev ? 1 : -1); - if (f1->F_INODE != f2->F_INODE) - return (int) (f1->F_INODE > f2->F_INODE ? 1 : -1); + if (i1->ino != i2->ino) + return (int)(i1->ino > i2->ino ? 1 : -1); return f_name_cmp(f1, f2); } @@ -62,42 +65,48 @@ static int32 hlink_count; * linked, and replace the dev+inode data with the hlindex+next linked list. */ static void link_idev_data(void) { - int32 cur, from, to, start; + int32 from, to, start; + struct file_struct *file, *file_next; + struct idev *idev, *idev_next; + struct hlist *hl; alloc_pool_t hlink_pool; alloc_pool_t idev_pool = the_file_list->hlink_pool; - hlink_pool = pool_create(128 * 1024, sizeof (struct hlink), out_of_memory, POOL_INTERN); + hlink_pool = pool_create(128 * 1024, sizeof (struct hlist), out_of_memory, POOL_INTERN); for (from = to = 0; from < hlink_count; from++) { start = from; - while (1) { - cur = hlink_list[from]; - if (from == hlink_count-1 - || !LINKED(cur, hlink_list[from+1])) + for (file = FPTR(hlink_list[from]), idev = F_IDEV(file); + from < hlink_count-1; + file = file_next, idev = idev_next) + { + file_next = FPTR(hlink_list[from+1]); + idev_next = F_IDEV(file_next); + if (!LINKED(idev, idev_next)) break; - pool_free(idev_pool, 0, FPTR(cur)->link_u.idev); - FPTR(cur)->link_u.links = pool_talloc(hlink_pool, - struct hlink, 1, "hlink_list"); - - FPTR(cur)->F_HLINDEX = to; - FPTR(cur)->F_NEXT = hlink_list[++from]; - FPTR(cur)->link_u.links->link_dest_used = 0; + pool_free(idev_pool, 0, idev); + hl = pool_talloc(hlink_pool, struct hlist, 1, + "hlink_list"); + hl->hlindex = to; + hl->next = hlink_list[++from]; + hl->dest_used = 0; + F_HLIST(file) = hl; } - pool_free(idev_pool, 0, FPTR(cur)->link_u.idev); + pool_free(idev_pool, 0, idev); if (from > start) { int head = hlink_list[start]; - FPTR(cur)->link_u.links = pool_talloc(hlink_pool, - struct hlink, 1, "hlink_list"); - - FPTR(head)->flags |= FLAG_HLINK_TOL; - FPTR(cur)->F_HLINDEX = to; - FPTR(cur)->F_NEXT = head; - FPTR(cur)->flags |= FLAG_HLINK_EOL; - FPTR(cur)->link_u.links->link_dest_used = 0; + hl = pool_talloc(hlink_pool, struct hlist, 1, + "hlink_list"); + FPTR(head)->flags |= FLAG_HLINK_FIRST; + hl->hlindex = to; + hl->next = head; + hl->dest_used = 0; hlink_list[to++] = head; + file->flags |= FLAG_HLINK_LAST; + F_HLIST(file) = hl; } else - FPTR(cur)->link_u.links = NULL; + file->flags &= ~FLAG_HLINK_INFO; } if (!to) { @@ -129,7 +138,7 @@ void init_hard_links(void) hlink_count = 0; for (i = 0; i < the_file_list->count; i++) { - if (FPTR(i)->link_u.idev) + if (IS_HLINKED(FPTR(i))) hlink_list[hlink_count++] = i; } @@ -180,20 +189,23 @@ int hard_link_check(struct file_struct *file, int ndx, char *fname, { #ifdef SUPPORT_HARD_LINKS int head; - if (skip && !(file->flags & FLAG_HLINK_EOL)) - head = hlink_list[file->F_HLINDEX] = file->F_NEXT; + struct hlist *hl = F_HLIST(file); + + if (skip && !(file->flags & FLAG_HLINK_LAST)) + head = hlink_list[hl->hlindex] = hl->next; else - head = hlink_list[file->F_HLINDEX]; + head = hlink_list[hl->hlindex]; if (ndx != head) { struct file_struct *head_file = FPTR(head); + struct hlist *hf_hl = F_HLIST(head_file); if (!stdout_format_has_i && verbose > 1) { rprintf(FINFO, "\"%s\" is a hard link\n", f_name(file, NULL)); } - if (head_file->F_HLINDEX == FINISHED_LINK) { + if (hf_hl->hlindex == FINISHED_LINK) { STRUCT_STAT st2, st3; char toname[MAXPATHLEN]; - int ldu = head_file->link_u.links->link_dest_used; + int ldu = hf_hl->dest_used; if (ldu) { pathjoin(toname, MAXPATHLEN, basis_dir[ldu-1], f_name(head_file, NULL)); @@ -238,9 +250,9 @@ int hard_link_check(struct file_struct *file, int ndx, char *fname, SIVAL(numbuf, 0, ndx); send_msg(MSG_SUCCESS, numbuf, 4); } - file->F_HLINDEX = FINISHED_LINK; + hl->hlindex = FINISHED_LINK; } else - file->F_HLINDEX = SKIPPED_LINK; + hl->hlindex = SKIPPED_LINK; return 1; } #endif @@ -277,27 +289,32 @@ int hard_link_one(struct file_struct *file, int ndx, char *fname, void hard_link_cluster(struct file_struct *file, int master, int itemizing, - enum logcode code) + enum logcode code, int dest_used) { #ifdef SUPPORT_HARD_LINKS char hlink1[MAXPATHLEN]; char *hlink2; STRUCT_STAT st1, st2; int statret, ndx = master; + struct hlist *hl = F_HLIST(file); - file->F_HLINDEX = FINISHED_LINK; + hl->hlindex = FINISHED_LINK; + if (dry_run) + hl->dest_used = dest_used + 1; if (link_stat(f_name(file, hlink1), &st1, 0) < 0) return; - if (!(file->flags & FLAG_HLINK_TOL)) { - while (!(file->flags & FLAG_HLINK_EOL)) { - ndx = file->F_NEXT; + if (!(file->flags & FLAG_HLINK_FIRST)) { + while (!(file->flags & FLAG_HLINK_LAST)) { + ndx = hl->next; file = FPTR(ndx); + hl = F_HLIST(file); } } do { - ndx = file->F_NEXT; + ndx = hl->next; file = FPTR(ndx); - if (file->F_HLINDEX != SKIPPED_LINK) + hl = F_HLIST(file); + if (hl->hlindex != SKIPPED_LINK) continue; hlink2 = f_name(file, NULL); statret = link_stat(hlink2, &st2, 0); @@ -308,7 +325,7 @@ void hard_link_cluster(struct file_struct *file, int master, int itemizing, SIVAL(numbuf, 0, ndx); send_msg(MSG_SUCCESS, numbuf, 4); } - file->F_HLINDEX = FINISHED_LINK; - } while (!(file->flags & FLAG_HLINK_EOL)); + hl->hlindex = FINISHED_LINK; + } while (!(file->flags & FLAG_HLINK_LAST)); #endif } diff --git a/log.c b/log.c index 9e34ffdf..79c3f299 100644 --- a/log.c +++ b/log.c @@ -37,6 +37,10 @@ extern int msg_fd_out; extern int allow_8bit_chars; extern int protocol_version; extern int preserve_times; +extern int preserve_uid; +extern int preserve_gid; +extern int flist_extra_ndx; +extern int file_struct_len; extern int stdout_format_has_i; extern int stdout_format_has_o_or_i; extern int logfile_format_has_i; @@ -475,16 +479,16 @@ static void log_formatted(enum logcode code, const char *format, const char *op, case 'U': strlcat(fmt, "ld", sizeof fmt); snprintf(buf2, sizeof buf2, fmt, - (long)file->uid); + preserve_uid ? (long)F_UID(file) : 0); n = buf2; break; case 'G': - if (file->gid == GID_NONE) + if (!preserve_gid || F_GID(file) == GID_NONE) n = "DEFAULT"; else { strlcat(fmt, "ld", sizeof fmt); snprintf(buf2, sizeof buf2, fmt, - (long)file->gid); + (long)F_GID(file)); n = buf2; } break; @@ -544,8 +548,8 @@ static void log_formatted(enum logcode code, const char *format, const char *op, if (hlink && *hlink) { n = hlink; strlcpy(buf2, " => ", sizeof buf2); - } else if (S_ISLNK(file->mode) && file->u.link) { - n = file->u.link; + } else if (S_ISLNK(file->mode)) { + n = F_SYMLINK(file); strlcpy(buf2, " -> ", sizeof buf2); } else { n = ""; diff --git a/main.c b/main.c index 31d2faa6..28334357 100644 --- a/main.c +++ b/main.c @@ -619,7 +619,7 @@ static void read_final_goodbye(int f_in, int f_out) } } - if (i != -1) { + if (i != NDX_DONE) { rprintf(FERROR, "Invalid packet at end of run (%d) [%s]\n", i, who_am_i()); exit_cleanup(RERR_PROTOCOL); @@ -768,7 +768,7 @@ static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name) io_flush(FULL_FLUSH); if (protocol_version >= 24) { /* send a final goodbye message */ - write_int(f_out, -1); + write_int(f_out, NDX_DONE); } io_flush(FULL_FLUSH); diff --git a/options.c b/options.c index 3c5ecb5e..9c1013db 100644 --- a/options.c +++ b/options.c @@ -116,7 +116,7 @@ int checksum_seed = 0; int inplace = 0; int delay_updates = 0; long block_size = 0; /* "long" because popt can't set an int32. */ - +int flist_extra_ndx = 1; /* index of optional file-list items */ /** Network address family. **/ #ifdef INET6 @@ -1219,6 +1219,11 @@ int parse_arguments(int *argc, const char ***argv, int frommain) need_messages_from_generator = 1; } + if (preserve_uid) + preserve_uid = flist_extra_ndx++; + if (preserve_gid) + preserve_gid = flist_extra_ndx++; + *argv = poptGetArgs(pc); *argc = count_args(*argv); diff --git a/receiver.c b/receiver.c index 28eec5fb..b9931b8f 100644 --- a/receiver.c +++ b/receiver.c @@ -39,6 +39,7 @@ extern int preserve_hard_links; extern int preserve_perms; extern int basis_dir_cnt; extern int make_backups; +extern int flist_extra_ndx; extern int cleanup_got_literal; extern int remove_source_files; extern int append_mode; @@ -300,8 +301,7 @@ static void handle_delayed_updates(struct file_list *flist, char *local_name) full_fname(fname), partialptr); } else { if (remove_source_files - || (preserve_hard_links - && file->link_u.links)) { + || (preserve_hard_links && IS_HLINKED(file))) { SIVAL(numbuf, 0, i); send_msg(MSG_SUCCESS,numbuf,4); } @@ -369,7 +369,7 @@ int recv_files(int f_in, struct file_list *flist, char *local_name) cleanup_disable(); i = read_int(f_in); - if (i == -1) { + if (i == NDX_DONE) { if (read_batch) { get_next_gen_i(batch_gen_fd, next_gen_i, flist->count); @@ -655,7 +655,7 @@ int recv_files(int f_in, struct file_list *flist, char *local_name) if (recv_ok > 0) { if (remove_source_files - || (preserve_hard_links && file->link_u.links)) { + || (preserve_hard_links && IS_HLINKED(file))) { SIVAL(numbuf, 0, i); send_msg(MSG_SUCCESS, numbuf, 4); } diff --git a/rsync.c b/rsync.c index 4649752e..da3e9297 100644 --- a/rsync.c +++ b/rsync.c @@ -215,9 +215,9 @@ int set_file_attrs(char *fname, struct file_struct *file, STRUCT_STAT *st, updated = 1; } - change_uid = am_root && preserve_uid && st->st_uid != file->uid; - change_gid = preserve_gid && file->gid != GID_NONE - && st->st_gid != file->gid; + change_uid = am_root && preserve_uid && st->st_uid != F_UID(file); + change_gid = preserve_gid && F_GID(file) != GID_NONE + && st->st_gid != F_GID(file); #if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK if (S_ISLNK(st->st_mode)) ; @@ -229,18 +229,18 @@ int set_file_attrs(char *fname, struct file_struct *file, STRUCT_STAT *st, rprintf(FINFO, "set uid of %s from %ld to %ld\n", fname, - (long)st->st_uid, (long)file->uid); + (long)st->st_uid, (long)F_UID(file)); } if (change_gid) { rprintf(FINFO, "set gid of %s from %ld to %ld\n", fname, - (long)st->st_gid, (long)file->gid); + (long)st->st_gid, (long)F_GID(file)); } } if (do_lchown(fname, - change_uid ? file->uid : st->st_uid, - change_gid ? file->gid : st->st_gid) != 0) { + change_uid ? F_UID(file) : st->st_uid, + change_gid ? F_GID(file) : st->st_gid) != 0) { /* shouldn't have attempted to change uid or gid * unless have the privilege */ rsyserr(FERROR, errno, "%s %s failed", diff --git a/rsync.h b/rsync.h index d2e328e3..85e59450 100644 --- a/rsync.h +++ b/rsync.h @@ -57,14 +57,14 @@ /* These flags are used in the live flist data. */ -#define FLAG_TOP_DIR (1<<0) -#define FLAG_SENT (1<<1) /* sender */ -#define FLAG_HLINK_EOL (1<<1) /* receiver/generator */ -#define FLAG_MOUNT_POINT (1<<2) /* sender/generator */ -#define FLAG_DEL_HERE (1<<3) /* receiver/generator */ -#define FLAG_HLINK_TOL (1<<4) /* receiver/generator */ -#define FLAG_NO_FUZZY (1<<5) /* generator */ -#define FLAG_MISSING (1<<6) /* generator */ +#define FLAG_TOP_DIR (1<<0) /* sender/receiver/generator */ +#define FLAG_SENT (1<<1) /* sender/generator */ +#define FLAG_XFER_DIR (1<<2) /* sender/receiver/generator */ +#define FLAG_MOUNT_DIR (1<<3) /* sender/generator */ +#define FLAG_MISSING_DIR (1<<4) /* generator (dirs only) */ +#define FLAG_HLINK_INFO (1<<5) /* receiver/generator */ +#define FLAG_HLINK_FIRST (1<<6) /* receiver/generator */ +#define FLAG_HLINK_LAST (1<<7) /* receiver/generator */ /* update this if you make incompatible changes */ #define PROTOCOL_VERSION 30 @@ -176,6 +176,8 @@ enum msgcode { MSG_DONE=86 /* current phase is done */ }; +#define NDX_DONE -1 + #include "errcode.h" #include "config.h" @@ -447,7 +449,7 @@ enum msgcode { */ struct idev { - int64 inode; + int64 ino; int64 dev; }; @@ -498,24 +500,13 @@ struct idev { #define HL_CHECK_MASTER 0 #define HL_SKIP 1 -struct hlink { +struct hlist { int32 next; int32 hlindex; - unsigned short link_dest_used; + unsigned short dest_used; }; -#define F_DEV link_u.idev->dev -#define F_INODE link_u.idev->inode - -#define F_HLINDEX link_u.links->hlindex -#define F_NEXT link_u.links->next - struct file_struct { - union { - dev_t rdev; /* The device number, if this is a device */ - const char *sum;/* Only a normal file can have a checksum */ - const char *link;/* Points to symlink string, if a symlink */ - } u; OFF_T length; const char *basename; /* The current item's name (AKA filename) */ const char *dirname; /* The directory info inside the transfer */ @@ -523,13 +514,7 @@ struct file_struct { const char *root;/* Sender-side dir info outside transfer */ int depth; /* Receiver-side directory depth info */ } dir; - union { - struct idev *idev; - struct hlink *links; - } link_u; time_t modtime; - uid_t uid; - gid_t gid; mode_t mode; uchar flags; /* this item MUST remain last */ }; @@ -541,13 +526,40 @@ struct file_struct { #define FLIST_START (32 * 1024) #define FLIST_LINEAR (FLIST_START * 512) +union flist_extras { + uid_t uid; /* the user ID number */ + uid_t gid; /* the group ID number or GID_NONE */ + struct idev *idev; /* The hard-link info during matching */ + struct hlist *hlist; /* The hard-link info after matching */ + int32 num; /* A general-purpose number */ +}; + +#define FLIST_EXTRA(f,j) ((union flist_extras *)(f))[-(j)] +#define IS_HLINKED(f) ((f)->flags & FLAG_HLINK_INFO) + +/* When enabled, all entries have these: */ +#define F_UID(f) FLIST_EXTRA(f, preserve_uid).uid +#define F_GID(f) FLIST_EXTRA(f, preserve_gid).gid + +/* These are per-entry optional and mutally exclusive: */ +#define F_IDEV(f) FLIST_EXTRA(f, flist_extra_ndx).idev +#define F_HLIST(f) FLIST_EXTRA(f, flist_extra_ndx).hlist + +/* These are per-entry optional, but always both or neither: */ +#define F_DMAJOR(f) FLIST_EXTRA(f, flist_extra_ndx + (IS_HLINKED(f)? 1 : 0)).num +#define F_DMINOR(f) FLIST_EXTRA(f, flist_extra_ndx + (IS_HLINKED(f)? 2 : 1)).num + +/* This is the first string past the struct (mutually exclusive). */ +#define F_SYMLINK(f) ((char*)(f) + file_struct_len) +#define F_SUM(f) F_SYMLINK(f) + /* - * Extent size for allocation pools A minimum size of 128KB + * Extent size for allocation pools: A minimum size of 128KB * is needed to mmap them so that freeing will release the * space to the OS. * * Larger sizes reduce leftover fragments and speed free calls - * (when they happen) Smaller sizes increase the chance of + * (when they happen). Smaller sizes increase the chance of * freed allocations freeing whole extents. */ #define FILE_EXTENT (256 * 1024) @@ -560,9 +572,8 @@ struct file_list { struct file_struct **files; alloc_pool_t file_pool; alloc_pool_t hlink_pool; - int count; - int malloced; - int low, high; + int count, malloced; + int low, high; /* 0-relative index values excluding empties */ }; #define SUMFLG_SAME_OFFSET (1<<0) @@ -692,7 +703,7 @@ int vsnprintf(char *str, size_t count, const char *fmt, va_list args); #if !defined HAVE_SNPRINTF || !defined HAVE_C99_VSNPRINTF #define snprintf rsync_snprintf -int snprintf(char *str,size_t count,const char *fmt,...); +int snprintf(char *str, size_t count, const char *fmt,...); #endif diff --git a/sender.c b/sender.c index 26cdafa0..0dff9e04 100644 --- a/sender.c +++ b/sender.c @@ -182,13 +182,13 @@ void send_files(struct file_list *flist, int f_out, int f_in) unsigned int offset; i = read_int(f_in); - if (i == -1) { + if (i == NDX_DONE) { if (++phase > max_phase) break; csum_length = SUM_LENGTH; if (verbose > 2) rprintf(FINFO, "send_files phase=%d\n", phase); - write_int(f_out, -1); + write_int(f_out, NDX_DONE); /* For inplace: redo phase turns off the backup * flag so that we do a regular inplace send. */ make_backups = 0; @@ -332,5 +332,5 @@ void send_files(struct file_list *flist, int f_out, int f_in) match_report(); - write_int(f_out, -1); + write_int(f_out, NDX_DONE); } diff --git a/uidlist.c b/uidlist.c index 83eee454..8104eca7 100644 --- a/uidlist.c +++ b/uidlist.c @@ -270,9 +270,6 @@ void send_uid_list(int f) { struct idlist *list; - if (numeric_ids) - return; - if (preserve_uid) { int len; /* we send sequences of uid/byte-length/name */ @@ -338,10 +335,10 @@ void recv_uid_list(int f, struct file_list *flist) /* Now convert all the uids/gids from sender values to our values. */ if (am_root && preserve_uid && !numeric_ids) { for (i = 0; i < flist->count; i++) - flist->files[i]->uid = match_uid(flist->files[i]->uid); + F_UID(flist->files[i]) = match_uid(F_UID(flist->files[i])); } if (preserve_gid && (!am_root || !numeric_ids)) { for (i = 0; i < flist->count; i++) - flist->files[i]->gid = match_gid(flist->files[i]->gid); + F_GID(flist->files[i]) = match_gid(F_GID(flist->files[i])); } } -- 2.34.1