dovecot-1.2: Squat: Create files with the correct mode and group.

dovecot at dovecot.org dovecot at dovecot.org
Mon Sep 22 21:04:41 EEST 2008


details:   http://hg.dovecot.org/dovecot-1.2/rev/b62042ddb79e
changeset: 8205:b62042ddb79e
user:      Timo Sirainen <tss at iki.fi>
date:      Mon Sep 22 21:04:38 2008 +0300
description:
Squat: Create files with the correct mode and group.

diffstat:

6 files changed, 48 insertions(+), 25 deletions(-)
src/plugins/fts-squat/fts-backend-squat.c  |    3 +-
src/plugins/fts-squat/squat-test.c         |    2 -
src/plugins/fts-squat/squat-trie-private.h |    3 ++
src/plugins/fts-squat/squat-trie.c         |   41 +++++++++++++++++++++-------
src/plugins/fts-squat/squat-trie.h         |    2 -
src/plugins/fts-squat/squat-uidlist.c      |   22 ++++++---------

diffs (186 lines):

diff -r f643f8c0e29d -r b62042ddb79e src/plugins/fts-squat/fts-backend-squat.c
--- a/src/plugins/fts-squat/fts-backend-squat.c	Sat Sep 20 19:30:07 2008 +0300
+++ b/src/plugins/fts-squat/fts-backend-squat.c	Mon Sep 22 21:04:38 2008 +0300
@@ -78,7 +78,8 @@ static struct fts_backend *fts_backend_s
 	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)
diff -r f643f8c0e29d -r b62042ddb79e src/plugins/fts-squat/squat-test.c
--- a/src/plugins/fts-squat/squat-test.c	Sat Sep 20 19:30:07 2008 +0300
+++ b/src/plugins/fts-squat/squat-test.c	Mon Sep 22 21:04:38 2008 +0300
@@ -56,7 +56,7 @@ int main(int argc ATTR_UNUSED, char *arg
 	(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);
diff -r f643f8c0e29d -r b62042ddb79e src/plugins/fts-squat/squat-trie-private.h
--- a/src/plugins/fts-squat/squat-trie-private.h	Sat Sep 20 19:30:07 2008 +0300
+++ b/src/plugins/fts-squat/squat-trie-private.h	Mon Sep 22 21:04:38 2008 +0300
@@ -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(
 	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
diff -r f643f8c0e29d -r b62042ddb79e src/plugins/fts-squat/squat-trie.c
--- a/src/plugins/fts-squat/squat-trie.c	Sat Sep 20 19:30:07 2008 +0300
+++ b/src/plugins/fts-squat/squat-trie.c	Mon Sep 22 21:04:38 2008 +0300
@@ -128,7 +128,8 @@ static void node_free(struct squat_trie 
 
 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
 	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_t
 		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_t
 	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
 		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,
diff -r f643f8c0e29d -r b62042ddb79e src/plugins/fts-squat/squat-trie.h
--- a/src/plugins/fts-squat/squat-trie.h	Sat Sep 20 19:30:07 2008 +0300
+++ b/src/plugins/fts-squat/squat-trie.h	Mon Sep 22 21:04:38 2008 +0300
@@ -20,7 +20,7 @@ struct squat_trie *
 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);
diff -r f643f8c0e29d -r b62042ddb79e src/plugins/fts-squat/squat-uidlist.c
--- a/src/plugins/fts-squat/squat-uidlist.c	Sat Sep 20 19:30:07 2008 +0300
+++ b/src/plugins/fts-squat/squat-uidlist.c	Mon Sep 22 21:04:38 2008 +0300
@@ -638,11 +638,10 @@ static int squat_uidlist_lock(struct squ
 			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(
 	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 sq
 		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);
-		return -1;
-	}
+	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;


More information about the dovecot-cvs mailing list