The uid & gid are now stored using an id_pair structure.
[rsync/rsync.git] / flist.c
diff --git a/flist.c b/flist.c
index 08ac3c9..277750c 100644 (file)
--- a/flist.c
+++ b/flist.c
@@ -70,10 +70,10 @@ 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;
 
 static char empty_sum[MD4_SUM_LENGTH];
 static int flist_count_offset;
-static unsigned int file_struct_len;
 
 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
 static void output_flist(struct file_list *flist);
@@ -306,7 +306,7 @@ void flist_expand(struct file_list *flist)
                out_of_memory("flist_expand");
 }
 
-void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
+static void send_file_entry(struct file_struct *file, int f)
 {
        unsigned short flags;
        static time_t modtime;
@@ -337,7 +337,7 @@ void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
 
        f_name(file, fname);
 
-       flags = base_flags;
+       flags = file->flags & XMIT_TOP_DIR;
 
        if (file->mode == mode)
                flags |= XMIT_SAME_MODE;
@@ -362,14 +362,14 @@ void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
                                flags |= XMIT_RDEV_MINOR_IS_SMALL;
                }
        }
-       if (file->uid == uid)
+       if (file->ids->uid == uid)
                flags |= XMIT_SAME_UID;
        else
-               uid = file->uid;
-       if (file->gid == gid)
+               uid = file->ids->uid;
+       if (file->ids->gid == gid)
                flags |= XMIT_SAME_GID;
        else
-               gid = file->gid;
+               gid = file->ids->gid;
        if (file->modtime == modtime)
                flags |= XMIT_SAME_TIME;
        else
@@ -569,6 +569,9 @@ static struct file_struct *receive_file_entry(struct file_list *flist,
        if (!(flags & XMIT_SAME_MODE))
                mode = from_wire_mode(read_int(f));
 
+       if (chmod_modes && !S_ISLNK(mode))
+               mode = tweak_mode(mode, chmod_modes);
+
        if (preserve_uid && !(flags & XMIT_SAME_UID))
                uid = (uid_t)read_int(f);
        if (preserve_gid && !(flags & XMIT_SAME_GID))
@@ -616,12 +619,10 @@ static struct file_struct *receive_file_entry(struct file_list *flist,
        memset(bp, 0, file_struct_len);
        bp += file_struct_len;
 
-       file->flags = 0;
        file->modtime = modtime;
        file->length = file_length;
        file->mode = mode;
-       file->uid = uid;
-       file->gid = gid;
+       file->ids = id_pair(uid, gid);
 
        if (dirname_len) {
                file->dirname = lastdir = bp;
@@ -644,13 +645,14 @@ static struct file_struct *receive_file_entry(struct file_list *flist,
                        in_del_hier = recurse;
                        del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
                        if (relative_paths && del_hier_name_len > 2
-                           && basename_len == 1+1 && *basename == '.')
+                           && 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;
                } else if (in_del_hier) {
                        if (!relative_paths || !del_hier_name_len
                         || (l1 >= del_hier_name_len
-                         && thisname[del_hier_name_len] == '/'))
+                         && lastname[del_hier_name_len] == '/'))
                                file->flags |= FLAG_DEL_HERE;
                        else
                                in_del_hier = 0;
@@ -733,6 +735,7 @@ 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(char *fname, struct file_list *flist,
+                             STRUCT_STAT *stp, unsigned short flags,
                              int filter_level)
 {
        static char *lastdir;
@@ -744,7 +747,6 @@ struct file_struct *make_file(char *fname, struct file_list *flist,
        char linkname[MAXPATHLEN];
        int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
        char *basename, *dirname, *bp;
-       unsigned short flags = 0;
 
        if (!flist || !flist->count)    /* Ignore lastdir when invalid. */
                lastdir_len = -1;
@@ -760,7 +762,9 @@ struct file_struct *make_file(char *fname, struct file_list *flist,
 
        memset(sum, 0, SUM_LENGTH);
 
-       if (readlink_stat(thisname, &st, linkname) != 0) {
+       if (stp && S_ISDIR(stp->st_mode))
+               st = *stp; /* Needed for "symlink/." with --relative. */
+       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
@@ -804,8 +808,16 @@ struct file_struct *make_file(char *fname, struct file_list *flist,
         * 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
-           && S_ISDIR(st.st_mode))
+        && S_ISDIR(st.st_mode)) {
+               if (one_file_system > 1) {
+                       if (verbose > 2) {
+                               rprintf(FINFO, "skipping mount-point dir %s\n",
+                                       thisname);
+                       }
+                       return NULL;
+               }
                flags |= FLAG_MOUNT_POINT;
+       }
 
        if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
                return NULL;
@@ -865,12 +877,8 @@ struct file_struct *make_file(char *fname, struct file_list *flist,
        file->flags = flags;
        file->modtime = st.st_mtime;
        file->length = st.st_size;
-       if (chmod_modes && am_sender && (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)))
-               file->mode = tweak_mode(st.st_mode, chmod_modes);
-       else
-               file->mode = st.st_mode;
-       file->uid = st.st_uid;
-       file->gid = st.st_gid;
+       file->mode = st.st_mode;
+       file->ids = id_pair(st.st_uid, st.st_gid);
 
 #ifdef SUPPORT_HARD_LINKS
        if (flist && flist->hlink_pool) {
@@ -931,14 +939,13 @@ struct file_struct *make_file(char *fname, struct file_list *flist,
        if (keep_dirlinks && linkname_len && flist) {
                STRUCT_STAT st2;
                int save_mode = file->mode;
-               file->mode = S_IFDIR; /* find a directory w/our name */
+               file->mode = S_IFDIR; /* Find a directory with our name. */
                if (flist_find(the_file_list, file) >= 0
                    && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
                        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->ids = id_pair(st2.st_uid, st2.st_gid);
                        file->u.link = NULL;
                } else
                        file->mode = save_mode;
@@ -951,21 +958,26 @@ struct file_struct *make_file(char *fname, struct file_list *flist,
 }
 
 static struct file_struct *send_file_name(int f, struct file_list *flist,
-                                         char *fname, unsigned short base_flags)
+                                         char *fname, STRUCT_STAT *stp,
+                                         unsigned short flags)
 {
        struct file_struct *file;
 
-       file = make_file(fname, flist, f == -2 ? SERVER_FILTERS : ALL_FILTERS);
+       file = make_file(fname, flist, stp, flags,
+                        f == -2 ? SERVER_FILTERS : ALL_FILTERS);
        if (!file)
                return NULL;
 
+       if (chmod_modes && !S_ISLNK(file->mode))
+               file->mode = tweak_mode(file->mode, chmod_modes);
+
        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, base_flags);
+               send_file_entry(file, f);
        }
        return file;
 }
@@ -1036,7 +1048,7 @@ static void send_directory(int f, struct file_list *flist,
                        continue;
                }
 
-               send_file_name(f, flist, fbuf, 0);
+               send_file_name(f, flist, fbuf, NULL, 0);
        }
 
        fbuf[len] = '\0';
@@ -1169,10 +1181,16 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
                                fn = fbuf;
                        /* Get rid of trailing "/" and "/.". */
                        while (len) {
-                               if (fn[len - 1] == '/')
-                                       len--;
+                               if (fn[len - 1] == '/') {
+                                       is_dot_dir = 1;
+                                       if (!--len && !dir) {
+                                               len++;
+                                               break;
+                                       }
+                               }
                                else if (len >= 2 && fn[len - 1] == '.'
                                                  && fn[len - 2] == '/') {
+                                       is_dot_dir = 1;
                                        if (!(len -= 2) && !dir) {
                                                len++;
                                                break;
@@ -1182,13 +1200,14 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
                        }
                        fn[len] = '\0';
                        /* Reject a ".." dir in the active part of the path. */
-                       if ((p = strstr(fbuf, "..")) != NULL
-                        && (p[2] == '/' || p[2] == '\0')
-                        && (p == fbuf || p[-1] == '/')) {
-                               rprintf(FERROR,
-                                   "using a \"..\" dir is invalid with --relative: %s\n",
-                                   fbuf);
-                               exit_cleanup(RERR_SYNTAX);
+                       for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
+                               if ((p[2] == '/' || p[2] == '\0')
+                                && (p == fn || p[-1] == '/')) {
+                                       rprintf(FERROR,
+                                           "found \"..\" dir in relative path: %s\n",
+                                           fbuf);
+                                       exit_cleanup(RERR_SYNTAX);
+                               }
                        }
                }
 
@@ -1241,7 +1260,7 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
                                xfer_dirs = 1;
                                while ((slash = strchr(slash+1, '/')) != 0) {
                                        *slash = '\0';
-                                       send_file_name(f, flist, fbuf, 0);
+                                       send_file_name(f, flist, fbuf, NULL, 0);
                                        *slash = '/';
                                }
                                copy_links = save_copy_links;
@@ -1257,10 +1276,11 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
 
                if (recurse || (xfer_dirs && is_dot_dir)) {
                        struct file_struct *file;
-                       if ((file = send_file_name(f, flist, fbuf, XMIT_TOP_DIR)))
+                       file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
+                       if (file)
                                send_if_directory(f, flist, file, fbuf, len);
                } else
-                       send_file_name(f, flist, fbuf, 0);
+                       send_file_name(f, flist, fbuf, &st, 0);
 
                if (olddir[0]) {
                        flist_dir = NULL;
@@ -1280,7 +1300,7 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
                stats.flist_buildtime = 1;
        start_tv = end_tv;
 
-       send_file_entry(NULL, f, 0);
+       send_file_entry(NULL, f);
 
        if (show_filelist_p())
                finish_filelist_progress(flist);
@@ -1300,8 +1320,6 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
         * without causing a compatibility problem with older versions. */
        clean_flist(flist, 0, 0);
 
-       /* Now send the uid/gid list. This was introduced in
-        * protocol version 15 */
        send_uid_list(f);
 
        /* send the io_error flag */
@@ -1371,9 +1389,7 @@ struct file_list *recv_file_list(int f)
        clean_flist(flist, relative_paths, 1);
 
        if (f >= 0) {
-               /* Now send the uid/gid list. This was introduced in
-                * protocol version 15 */
-               recv_uid_list(f, flist);
+               recv_uid_list(f);
 
                /* Recv the io_error flag */
                if (lp_ignore_errors(module_id) || ignore_errors)
@@ -1600,11 +1616,11 @@ 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);
+                       sprintf(uidbuf, " uid=%ld", (long)file->ids->uid);
                else
                        *uidbuf = '\0';
-               if (preserve_gid && file->gid != GID_NONE)
-                       sprintf(gidbuf, " gid=%ld", (long)file->gid);
+               if (preserve_gid && file->ids->gid != GID_NONE)
+                       sprintf(gidbuf, " gid=%ld", (long)file->ids->gid);
                else
                        *gidbuf = '\0';
                if (!am_sender)