]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Squat: Create files with the correct mode and group.
authorTimo Sirainen <tss@iki.fi>
Mon, 22 Sep 2008 18:04:38 +0000 (21:04 +0300)
committerTimo Sirainen <tss@iki.fi>
Mon, 22 Sep 2008 18:04:38 +0000 (21:04 +0300)
--HG--
branch : HEAD

src/plugins/fts-squat/fts-backend-squat.c
src/plugins/fts-squat/squat-test.c
src/plugins/fts-squat/squat-trie-private.h
src/plugins/fts-squat/squat-trie.c
src/plugins/fts-squat/squat-trie.h
src/plugins/fts-squat/squat-uidlist.c

index 78f6a954920b77f2f725a9f5018caa9cc6867c93..292b0359ae87a05755763b2bbfbb49040813cfb6 100644 (file)
@@ -78,7 +78,8 @@ static struct fts_backend *fts_backend_squat_init(struct mailbox *box)
        backend->trie =
                squat_trie_init(t_strconcat(path, "/"SQUAT_FILE_PREFIX, NULL),
                                status.uidvalidity, storage->lock_method,
-                               flags);
+                               flags, box->file_create_mode,
+                               box->file_create_gid);
 
        env = getenv("FTS_SQUAT");
        if (env != NULL)
index 371b6a5e09055654288c001789cba39c8cce2074..05697391de0de9b55184644add5f1c997723cce7 100644 (file)
@@ -56,7 +56,7 @@ int main(int argc ATTR_UNUSED, char *argv[])
        (void)unlink(trie_path);
        (void)unlink(uidlist_path);
        trie = squat_trie_init(trie_path, time(NULL),
-                              FILE_LOCK_METHOD_FCNTL, FALSE);
+                              FILE_LOCK_METHOD_FCNTL, FALSE, 0600, (gid_t)-1);
 
        clock_start = clock();
        gettimeofday(&tv_start, NULL);
index 9512705a4d6ec0e3ea4648e821b9a295c6102325..efe93fe0b7627aa3e9cdcdeb7c7bcf1a88232556 100644 (file)
@@ -118,6 +118,8 @@ struct squat_trie {
 
        enum squat_index_flags flags;
        enum file_lock_method lock_method;
+       mode_t create_mode;
+       gid_t create_gid;
        uint32_t uidvalidity;
 
        char *path;
@@ -184,6 +186,7 @@ static inline uint32_t squat_unpack_num(const uint8_t **p, const uint8_t *end)
        return value;
 }
 
+int squat_trie_create_fd(struct squat_trie *trie, const char *path, int flags);
 void squat_trie_delete(struct squat_trie *trie);
 
 #endif
index 0c3e737a9fa739e9cc84d2025641ce7701b23a2d..e48cc4e39ea5e6312c780db35e4748e28d745362 100644 (file)
@@ -128,7 +128,8 @@ static void node_free(struct squat_trie *trie, struct squat_node *node)
 
 struct squat_trie *
 squat_trie_init(const char *path, uint32_t uidvalidity,
-               enum file_lock_method lock_method, enum squat_index_flags flags)
+               enum file_lock_method lock_method, enum squat_index_flags flags,
+               mode_t mode, gid_t gid)
 {
        struct squat_trie *trie;
 
@@ -139,6 +140,8 @@ squat_trie_init(const char *path, uint32_t uidvalidity,
        trie->lock_method = lock_method;
        trie->uidvalidity = uidvalidity;
        trie->flags = flags;
+       trie->create_mode = mode;
+       trie->create_gid = gid;
        squat_trie_normalize_map_build(trie);
 
        trie->dotlock_set.use_excl_lock =
@@ -1532,6 +1535,28 @@ static int squat_trie_map(struct squat_trie *trie, bool building)
                node_read_children(trie, &trie->root, 1);
 }
 
+int squat_trie_create_fd(struct squat_trie *trie, const char *path, int flags)
+{
+       mode_t old_mask;
+       int fd;
+
+       old_mask = umask(0);
+       fd = open(path, O_RDWR | O_CREAT | flags, trie->create_mode);
+       umask(old_mask);
+       if (fd == -1) {
+               i_error("creat(%s) failed: %m", path);
+               return -1;
+       }
+       if (trie->create_gid != (gid_t)-1) {
+               if (fchown(fd, (uid_t)-1, trie->create_gid) < 0) {
+                       i_error("fchown(%s, -1, %ld) failed: %m",
+                               path, (long)trie->create_gid);
+                       return -1;
+               }
+       }
+       return fd;
+}
+
 int squat_trie_build_init(struct squat_trie *trie, uint32_t *last_uid_r,
                          struct squat_trie_build_context **ctx_r)
 {
@@ -1539,11 +1564,10 @@ int squat_trie_build_init(struct squat_trie *trie, uint32_t *last_uid_r,
        struct squat_uidlist_build_context *uidlist_build_ctx;
 
        if (trie->fd == -1) {
-               trie->fd = open(trie->path, O_RDWR | O_CREAT, 0600);
-               if (trie->fd == -1) {
-                       i_error("creat(%s) failed: %m", trie->path);
+               trie->fd = squat_trie_create_fd(trie, trie->path, 0);
+               if (trie->fd == -1)
                        return -1;
-               }
+
                if (trie->file_cache != NULL)
                        file_cache_set_fd(trie->file_cache, trie->fd);
                i_assert(trie->locked_file_size == 0);
@@ -1593,11 +1617,10 @@ static int squat_trie_write(struct squat_trie_build_context *ctx)
                ctx->compress_nodes = TRUE;
 
                path = t_strconcat(trie->path, ".tmp", NULL);
-               fd = open(path, O_RDWR | O_CREAT, 0600);
-               if (fd == -1) {
-                       i_error("creat(%s) failed: %m", path);
+               fd = squat_trie_create_fd(trie, path, O_TRUNC);
+               if (fd == -1)
                        return -1;
-               }
+
                if (trie->lock_method != FILE_LOCK_METHOD_DOTLOCK) {
                        ret = file_wait_lock(fd, path, F_WRLCK,
                                             trie->lock_method,
index 2e4adb52ad1264f504f9d36028b601c961c220cc..466e800d0966c6e3ec0bbed174962115c2369249 100644 (file)
@@ -20,7 +20,7 @@ struct squat_trie_build_context;
 struct squat_trie *
 squat_trie_init(const char *path, uint32_t uidvalidity,
                enum file_lock_method lock_method,
-               enum squat_index_flags flags);
+               enum squat_index_flags flags, mode_t mode, gid_t gid);
 void squat_trie_deinit(struct squat_trie **trie);
 
 void squat_trie_set_partial_len(struct squat_trie *trie, unsigned int len);
index f076b0f9f874426753952f064d32f137933cc482..93f49b819f7f1822572babf47f6b3399b9f2e43f 100644 (file)
@@ -638,11 +638,10 @@ static int squat_uidlist_lock(struct squat_uidlist *uidlist)
                        return -1;
 
                squat_uidlist_close(uidlist);
-               uidlist->fd = open(uidlist->path, O_RDWR | O_CREAT, 0600);
-               if (uidlist->fd == -1) {
-                       i_error("open(%s) failed: %m", uidlist->path);
+               uidlist->fd = squat_trie_create_fd(uidlist->trie,
+                                                  uidlist->path, 0);
+               if (uidlist->fd == -1)
                        return -1;
-               }
        }
        return 1;
 }
@@ -652,11 +651,10 @@ static int squat_uidlist_open_or_create(struct squat_uidlist *uidlist)
        int ret;
 
        if (uidlist->fd == -1) {
-               uidlist->fd = open(uidlist->path, O_RDWR | O_CREAT, 0600);
-               if (uidlist->fd == -1) {
-                       i_error("creat(%s) failed: %m", uidlist->path);
+               uidlist->fd = squat_trie_create_fd(uidlist->trie,
+                                                  uidlist->path, 0);
+               if (uidlist->fd == -1)
                        return -1;
-               }
        }
        if (squat_uidlist_lock(uidlist) <= 0)
                return -1;
@@ -920,11 +918,9 @@ int squat_uidlist_rebuild_init(struct squat_uidlist_build_context *build_ctx,
                return -1;
 
        temp_path = t_strconcat(build_ctx->uidlist->path, ".tmp", NULL);
-       fd = open(temp_path, O_RDWR | O_TRUNC | O_CREAT, 0600);
-       if (fd < 0) {
-               i_error("open(%s) failed: %m", temp_path);
+       fd = squat_trie_create_fd(build_ctx->uidlist->trie, temp_path, O_TRUNC);
+       if (fd == -1)
                return -1;
-       }
 
        ctx = i_new(struct squat_uidlist_rebuild_context, 1);
        ctx->uidlist = build_ctx->uidlist;