Improved handling of a system that doesn't have a 64-bit offset type.
[rsync/rsync.git] / flist.c
diff --git a/flist.c b/flist.c
index f8e809a..0554b5d 100644 (file)
--- a/flist.c
+++ b/flist.c
@@ -242,10 +242,10 @@ static inline int is_daemon_excluded(const char *fname, int is_dir)
        return 0;
 }
 
-static inline int path_is_daemon_excluded(const char *path, int ignore_filename)
+static inline int path_is_daemon_excluded(char *path, int ignore_filename)
 {
        if (daemon_filter_list.head) {
-               char *slash = (char*)path;
+               char *slash = path;
 
                while ((slash = strchr(slash+1, '/')) != NULL) {
                        int ret;
@@ -293,7 +293,6 @@ static void send_directory(int f, struct file_list *flist,
 static const char *pathname, *orig_dir;
 static int pathname_len;
 
-
 /* Make sure flist can hold at least flist->used + extra entries. */
 static void flist_expand(struct file_list *flist, int extra)
 {
@@ -339,29 +338,51 @@ static void flist_done_allocating(struct file_list *flist)
                flist->pool_boundary = ptr;
 }
 
-int push_pathname(const char *dir, int len)
+/* Call this with EITHER (1) "file, NULL, 0" to chdir() to the file's
+ * F_PATHNAME(), or (2) "NULL, dir, dirlen" to chdir() to the supplied dir,
+ * with dir == NULL taken to be the starting directory, and dirlen < 0
+ * indicating that strdup(dir) should be called and then the -dirlen length
+ * value checked to ensure that it is not daemon-excluded. */
+int change_pathname(struct file_struct *file, const char *dir, int dirlen)
 {
-       if (dir == pathname)
-               return 1;
+       if (dirlen < 0) {
+               char *cpy = strdup(dir);
+               if (*cpy != '/')
+                       change_dir(orig_dir, CD_SKIP_CHDIR);
+               if (path_is_daemon_excluded(cpy, 0))
+                       goto chdir_error;
+               dir = cpy;
+               dirlen = -dirlen;
+       } else {
+               if (file) {
+                       if (pathname == F_PATHNAME(file))
+                               return 1;
+                       dir = F_PATHNAME(file);
+                       if (dir)
+                               dirlen = strlen(dir);
+               } else if (pathname == dir)
+                       return 1;
+               if (dir && *dir != '/')
+                       change_dir(orig_dir, CD_SKIP_CHDIR);
+       }
 
-       if (!orig_dir)
-               orig_dir = strdup(curr_dir);
+       pathname = dir;
+       pathname_len = dirlen;
 
-       if (pathname && !pop_dir(orig_dir)) {
-               rsyserr(FERROR, errno, "pop_dir %s failed",
-                       full_fname(orig_dir));
-               exit_cleanup(RERR_FILESELECT);
-       }
+       if (!dir)
+               dir = orig_dir;
 
-       if (dir && (path_is_daemon_excluded(dir, 0) || !push_dir(dir, 0))) {
+       if (!change_dir(dir, CD_NORMAL)) {
+         chdir_error:
                io_error |= IOERR_GENERAL;
-               rsyserr(FERROR, errno, "push_dir %s failed", full_fname(dir));
+               rsyserr(FERROR, errno, "change_dir %s failed", full_fname(dir));
+               if (dir != orig_dir)
+                       change_dir(orig_dir, CD_NORMAL);
+               pathname = NULL;
+               pathname_len = 0;
                return 0;
        }
 
-       pathname = dir;
-       pathname_len = len >= 0 ? len : dir ? (int)strlen(dir) : 0;
-
        return 1;
 }
 
@@ -617,7 +638,7 @@ static struct file_struct *recv_file_entry(struct file_list *flist,
        int alloc_len, basename_len, linkname_len;
        int extra_len = file_extra_cnt * EXTRA_LEN;
        int first_hlink_ndx = -1;
-       OFF_T file_length;
+       int64 file_length;
        const char *basename;
        struct file_struct *file;
        alloc_pool_t *pool;
@@ -816,8 +837,14 @@ static struct file_struct *recv_file_entry(struct file_list *flist,
        if (always_checksum && S_ISREG(mode))
                extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
 
+#if SIZEOF_INT64 >= 8
        if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
                extra_len += EXTRA_LEN;
+#endif
+       if (file_length < 0) {
+               rprintf(FERROR, "Offset underflow: file-length is negative\n");
+               exit_cleanup(RERR_UNSUPPORTED);
+       }
 
        if (inc_recurse && S_ISDIR(mode)) {
                if (one_file_system) {
@@ -850,10 +877,17 @@ static struct file_struct *recv_file_entry(struct file_list *flist,
 #endif
        file->modtime = (time_t)modtime;
        file->len32 = (uint32)file_length;
+#if SIZEOF_INT64 >= 8
        if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
+#if SIZEOF_CAPITAL_OFF_T < 8
+               rprintf(FERROR, "Offset overflow: attempted 64-bit file-length\n");
+               exit_cleanup(RERR_UNSUPPORTED);
+#else
                file->flags |= FLAG_LENGTH64;
                OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
+#endif
        }
+#endif
        file->mode = mode;
        if (preserve_uid)
                F_OWNER(file) = uid;
@@ -1002,7 +1036,6 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
        const char *basename;
        alloc_pool_t *pool;
        STRUCT_STAT st;
-       int excl_ret;
        char *bp;
 
        if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
@@ -1057,7 +1090,6 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
                return NULL;
        }
 
-       /* backup.c calls us with filter_level set to NO_FILTERS. */
        if (filter_level == NO_FILTERS)
                goto skip_filters;
 
@@ -1085,17 +1117,7 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
        } else
                flags &= ~FLAG_CONTENT_DIR;
 
-       if (S_ISDIR(st.st_mode)) {
-               if (flags & FLAG_DOTDIR_NAME) {
-                       /* A "." fname (or "/." fname in relative mode) is
-                        * never excluded.  No other trailing-dotdir names
-                        * are possible. */
-                       excl_ret = 0;
-               } else
-                       excl_ret = is_excluded(thisname, 1, filter_level);
-       } else
-               excl_ret = is_excluded(thisname, 0, filter_level);
-       if (excl_ret) {
+       if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
                if (ignore_perishable)
                        non_perishable_cnt++;
                return NULL;
@@ -1154,8 +1176,10 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
        linkname_len = 0;
 #endif
 
+#if SIZEOF_CAPITAL_OFF_T >= 8
        if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
                extra_len += EXTRA_LEN;
+#endif
 
 #if EXTRA_ROUNDING > 0
        if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
@@ -1200,10 +1224,12 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
        file->flags = flags;
        file->modtime = st.st_mtime;
        file->len32 = (uint32)st.st_size;
+#if SIZEOF_CAPITAL_OFF_T >= 8
        if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
                file->flags |= FLAG_LENGTH64;
                OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
        }
+#endif
        file->mode = st.st_mode;
        if (uid_ndx) /* Check uid_ndx instead of preserve_uid for del support */
                F_OWNER(file) = st.st_uid;
@@ -1666,10 +1692,8 @@ static void send1extra(int f, struct file_struct *file, struct file_list *flist)
        f_name(file, fbuf);
        dlen = strlen(fbuf);
 
-       if (F_PATHNAME(file) != pathname) {
-               if (!push_pathname(F_PATHNAME(file), -1))
-                       exit_cleanup(RERR_FILESELECT);
-       }
+       if (!change_pathname(file, NULL, 0))
+               exit_cleanup(RERR_FILESELECT);
 
        change_local_filter_dir(fbuf, dlen, send_dir_depth);
 
@@ -1834,7 +1858,7 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
        int64 start_write;
        int use_ff_fd = 0;
        int disable_buffering;
-       int arg_flags, flags = recurse ? FLAG_CONTENT_DIR : 0;
+       int flags = recurse ? FLAG_CONTENT_DIR : 0;
        int reading_remotely = filesfrom_host != NULL;
        int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS)
 #ifdef ICONV_OPTION
@@ -1852,6 +1876,9 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
        start_write = stats.total_written;
        gettimeofday(&start_tv, NULL);
 
+       if (!orig_dir)
+               orig_dir = strdup(curr_dir);
+
        if (relative_paths && protocol_version >= 30)
                implied_dirs = 1; /* We send flagged implied dirs */
 
@@ -1869,8 +1896,8 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
 
        disable_buffering = io_start_buffering_out(f);
        if (filesfrom_fd >= 0) {
-               if (argv[0] && !push_dir(argv[0], 0)) {
-                       rsyserr(FERROR_XFER, errno, "push_dir %s failed",
+               if (argv[0] && !change_dir(argv[0], CD_NORMAL)) {
+                       rsyserr(FERROR_XFER, errno, "change_dir %s failed",
                                full_fname(argv[0]));
                        exit_cleanup(RERR_FILESELECT);
                }
@@ -1994,11 +2021,11 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
 
                dirlen = dir ? strlen(dir) : 0;
                if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
-                       if (!push_pathname(dir ? strdup(dir) : NULL, dirlen))
+                       if (!change_pathname(NULL, dir, -dirlen))
                                continue;
                        lastdir = pathname;
                        lastdir_len = pathname_len;
-               } else if (!push_pathname(lastdir, lastdir_len))
+               } else if (!change_pathname(NULL, lastdir, lastdir_len))
                        continue;
 
                if (fn != fbuf)
@@ -2013,6 +2040,11 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
                        continue;
                }
 
+               /* A dot-dir should not be excluded! */
+               if (name_type != DOTDIR_NAME
+                && is_excluded(fbuf, S_ISDIR(st.st_mode) != 0, ALL_FILTERS))
+                       continue;
+
                if (S_ISDIR(st.st_mode) && !xfer_dirs) {
                        rprintf(FINFO, "skipping directory %s\n", fbuf);
                        continue;
@@ -2038,13 +2070,11 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
                if (one_file_system)
                        filesystem_dev = st.st_dev;
 
-               arg_flags = name_type == DOTDIR_NAME ? FLAG_DOTDIR_NAME : 0;
-
                if (recurse || (xfer_dirs && name_type != NORMAL_NAME)) {
                        struct file_struct *file;
-                       arg_flags |= FLAG_TOP_DIR | FLAG_CONTENT_DIR;
                        file = send_file_name(f, flist, fbuf, &st,
-                                             arg_flags | flags, ALL_FILTERS);
+                                             FLAG_TOP_DIR | FLAG_CONTENT_DIR | flags,
+                                             NO_FILTERS);
                        if (!file)
                                continue;
                        if (inc_recurse) {
@@ -2058,7 +2088,7 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
                        } else
                                send_if_directory(f, flist, file, fbuf, len, flags);
                } else
-                       send_file_name(f, flist, fbuf, &st, arg_flags | flags, ALL_FILTERS);
+                       send_file_name(f, flist, fbuf, &st, flags, NO_FILTERS);
        }
 
        gettimeofday(&end_tv, NULL);