Switch over to Matt's idea of using FLAG_OWNED_BY_US.
[rsync.git] / generator.c
index fad252890bea51c199ded518ee693d9809e00d85..0c3060458deba2d6ba9aeec9ac97c5d4ac2dbe88 100644 (file)
@@ -44,8 +44,6 @@ extern int preserve_hard_links;
 extern int preserve_executability;
 extern int preserve_perms;
 extern int preserve_times;
-extern int uid_ndx;
-extern int gid_ndx;
 extern int delete_mode;
 extern int delete_before;
 extern int delete_during;
@@ -76,7 +74,7 @@ extern int fuzzy_basis;
 extern int always_checksum;
 extern int checksum_len;
 extern char *partial_dir;
-extern char *basis_dir[];
+extern char *basis_dir[MAX_BASIS_DIRS+1];
 extern int compare_dest;
 extern int copy_dest;
 extern int link_dest;
@@ -169,19 +167,12 @@ static enum delret delete_item(char *fbuf, uint16 mode, uint16 flags)
                do_chmod(fbuf, mode | S_IWUSR);
 
        if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
-               int save_uid_ndx = uid_ndx;
                /* This only happens on the first call to delete_item() since
                 * delete_dir_contents() always calls us w/DEL_DIR_IS_EMPTY. */
-               if (!uid_ndx)
-                       uid_ndx = ++file_extra_cnt;
                ignore_perishable = 1;
                /* If DEL_RECURSE is not set, this just reports emptiness. */
                ret = delete_dir_contents(fbuf, flags);
                ignore_perishable = 0;
-               if (!save_uid_ndx) {
-                       --file_extra_cnt;
-                       uid_ndx = 0;
-               }
                if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT)
                        goto check_ret;
                /* OK: try to delete the directory. */
@@ -294,7 +285,7 @@ static enum delret delete_dir_contents(char *fname, uint16 flags)
                }
 
                strlcpy(p, fp->basename, remainder);
-               if (!(fp->mode & S_IWUSR) && !am_root && (uid_t)F_OWNER(fp) == our_uid)
+               if (!(fp->mode & S_IWUSR) && !am_root && fp->flags & FLAG_OWNED_BY_US)
                        do_chmod(fname, fp->mode | S_IWUSR);
                /* Save stack by recursing to ourself directly. */
                if (S_ISDIR(fp->mode)) {
@@ -472,7 +463,6 @@ static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
        struct file_list *dirlist;
        char delbuf[MAXPATHLEN];
        int dlen, i;
-       int save_uid_ndx = uid_ndx;
 
        if (!fbuf) {
                change_local_filter_dir(NULL, 0, 0);
@@ -504,9 +494,6 @@ static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
                        return;
        }
 
-       if (!uid_ndx)
-               uid_ndx = ++file_extra_cnt;
-
        dirlist = get_dirlist(fbuf, dlen, 0);
 
        /* If an item in dirlist is not found in flist, delete it
@@ -526,7 +513,7 @@ static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
                 * a delete_item call with a DEL_MAKE_ROOM flag. */
                if (flist_find_ignore_dirness(cur_flist, fp) < 0) {
                        int flags = DEL_RECURSE;
-                       if (!(fp->mode & S_IWUSR) && !am_root && (uid_t)F_OWNER(fp) == our_uid)
+                       if (!(fp->mode & S_IWUSR) && !am_root && fp->flags & FLAG_OWNED_BY_US)
                                flags |= DEL_NO_UID_WRITE;
                        f_name(fp, delbuf);
                        if (delete_during == 2) {
@@ -538,11 +525,6 @@ static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
        }
 
        flist_free(dirlist);
-
-       if (!save_uid_ndx) {
-               --file_extra_cnt;
-               uid_ndx = 0;
-       }
 }
 
 /* This deletes any files on the receiving side that are not present on the
@@ -761,6 +743,12 @@ static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
        int s2length;
        int64 l;
 
+       if (len < 0) {
+               /* The file length overflowed our int64 var, so we can't process this file. */
+               sum->count = -1; /* indicate overflow error */
+               return;
+       }
+
        if (block_size)
                blength = block_size;
        else if (len <= BLOCK_SIZE * BLOCK_SIZE)
@@ -1300,6 +1288,12 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
                skip_dir = NULL;
        }
 
+#ifdef SUPPORT_ACLS
+       sx.acc_acl = sx.def_acl = NULL;
+#endif
+#ifdef SUPPORT_XATTRS
+       sx.xattr = NULL;
+#endif
        if (daemon_filter_list.head && (*fname != '.' || fname[1])) {
                if (check_filter(&daemon_filter_list, FLOG, fname, is_dir) < 0) {
                        if (is_dir < 0)
@@ -1317,12 +1311,6 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
                }
        }
 
-#ifdef SUPPORT_ACLS
-       sx.acc_acl = sx.def_acl = NULL;
-#endif
-#ifdef SUPPORT_XATTRS
-       sx.xattr = NULL;
-#endif
        if (dry_run > 1 || (dry_missing_dir && is_below(file, dry_missing_dir))) {
          parent_is_dry_missing:
                if (fuzzy_dirlist) {
@@ -1361,7 +1349,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
 
                if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
                        strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
-                       fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
+                       fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, GDL_IGNORE_FILTER_RULES);
                        need_fuzzy_dirlist = 0;
                }
 
@@ -1402,9 +1390,18 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
                goto cleanup;
        }
 
+       fnamecmp = fname;
+
        if (is_dir) {
+               mode_t added_perms;
                if (!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
                        goto cleanup;
+               if (am_root < 0) {
+                       /* For --fake-super, the dir must be useable by the copying
+                        * user, just like it would be for root. */
+                       added_perms = S_IRUSR|S_IWUSR|S_IXUSR;
+               } else
+                       added_perms = 0;
                if (is_dir < 0) {
                        /* In inc_recurse mode we want to make sure any missing
                         * directories get created while we're still processing
@@ -1415,7 +1412,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
                         && (S_ISDIR(sx.st.st_mode)
                          || delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0))
                                goto cleanup; /* Any errors get reported later. */
-                       if (do_mkdir(fname, file->mode & 0700) == 0)
+                       if (do_mkdir(fname, (file->mode|added_perms) & 0700) == 0)
                                file->flags |= FLAG_DIR_CREATED;
                        goto cleanup;
                }
@@ -1449,17 +1446,19 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
                                itemizing = 0;
                                code = FNONE;
                                statret = 1;
-                       } else if (j >= 0)
+                       } else if (j >= 0) {
                                statret = 1;
+                               fnamecmp = fnamecmpbuf;
+                       }
                }
                if (itemizing && f_out != -1) {
-                       itemize(fname, file, ndx, statret, &sx,
+                       itemize(fnamecmp, file, ndx, statret, &sx,
                                statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
                }
-               if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
+               if (real_ret != 0 && do_mkdir(fname,file->mode|added_perms) < 0 && errno != EEXIST) {
                        if (!relative_paths || errno != ENOENT
-                           || create_directory_path(fname) < 0
-                           || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
+                        || create_directory_path(fname) < 0
+                        || (do_mkdir(fname, file->mode|added_perms) < 0 && errno != EEXIST)) {
                                rsyserr(FERROR_XFER, errno,
                                        "recv_generator: mkdir %s failed",
                                        full_fname(fname));
@@ -1532,11 +1531,14 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
                const char *sl = F_SYMLINK(file);
                if (safe_symlinks && unsafe_symlink(sl, fname)) {
                        if (verbose) {
-                               if (solo_file)
+                               if (solo_file) {
+                                       /* fname contains the destination path, but we
+                                        * want to report the source path. */
                                        fname = f_name(file, NULL);
+                               }
                                rprintf(FINFO,
-                                       "ignoring unsafe symlink %s -> \"%s\"\n",
-                                       full_fname(fname), sl);
+                                       "ignoring unsafe symlink \"%s\" -> \"%s\"\n",
+                                       fname, sl);
                        }
                        return;
                }
@@ -1730,7 +1732,6 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
                goto cleanup;
        }
 
-       fnamecmp = fname;
        fnamecmp_type = FNAMECMP_FNAME;
 
        if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
@@ -2215,7 +2216,7 @@ void generate_files(int f_out, const char *local_name)
        /* Since we often fill up the outgoing socket and then just sit around
         * waiting for the other 2 processes to do their thing, we don't want
         * to exit on a timeout.  If the data stops flowing, the receiver will
-        * notice that and let us know via the redo pipe (or its closing). */
+        * notice that and let us know via the message pipe (or its closing). */
        ignore_timeout = 1;
 
        dflt_perms = (ACCESSPERMS & ~orig_umask);
@@ -2309,6 +2310,7 @@ void generate_files(int f_out, const char *local_name)
                rprintf(FINFO, "generate_files phase=%d\n", phase);
 
        write_ndx(f_out, NDX_DONE);
+
        /* Reduce round-trip lag-time for a useless delay-updates phase. */
        if (protocol_version >= 29 && !delay_updates)
                write_ndx(f_out, NDX_DONE);