dovecot: Create dbox files using mailbox.file_create_mode.

dovecot at dovecot.org dovecot at dovecot.org
Sun Nov 11 17:46:52 EET 2007


details:   http://hg.dovecot.org/dovecot/rev/47c746a769ba
changeset: 6776:47c746a769ba
user:      Timo Sirainen <tss at iki.fi>
date:      Sun Nov 11 17:43:56 2007 +0200
description:
Create dbox files using mailbox.file_create_mode.

diffstat:

3 files changed, 47 insertions(+), 26 deletions(-)
src/lib-storage/index/dbox/dbox-file.c  |   22 ++++++++++---
src/lib-storage/index/dbox/dbox-file.h  |    1 
src/lib-storage/index/dbox/dbox-index.c |   50 +++++++++++++++++--------------

diffs (118 lines):

diff -r b6135e6a5ff2 -r 47c746a769ba src/lib-storage/index/dbox/dbox-file.c
--- a/src/lib-storage/index/dbox/dbox-file.c	Sun Nov 11 17:21:19 2007 +0200
+++ b/src/lib-storage/index/dbox/dbox-file.c	Sun Nov 11 17:43:56 2007 +0200
@@ -431,6 +431,21 @@ static int dbox_file_open(struct dbox_fi
 		dbox_file_read_header(file);
 }
 
+int dbox_create_fd(struct dbox_mailbox *mbox, const char *path)
+{
+	mode_t old_mask;
+	int fd;
+
+	old_mask = umask(0777 & ~mbox->ibox.box.file_create_mode);
+	fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0777);
+	umask(old_mask);
+	if (fd == -1) {
+		mail_storage_set_critical(mbox->ibox.box.storage,
+			"open(%s, O_CREAT) failed: %m", path);
+	}
+	return fd;
+}
+
 static int dbox_file_create(struct dbox_file *file)
 {
 	string_t *hdr;
@@ -442,12 +457,9 @@ static int dbox_file_create(struct dbox_
 		file->current_path =
 			i_strdup_printf("%s/%s", file->mbox->path, file->fname);
 	}
-	file->fd = open(file->current_path, O_RDWR | O_CREAT | O_TRUNC, 0600);
-	if (file->fd == -1) {
-		mail_storage_set_critical(file->mbox->ibox.box.storage,
-			"open(%s, O_CREAT) failed: %m", file->current_path);
+	file->fd = dbox_create_fd(file->mbox, file->current_path);
+	if (file->fd == -1)
 		return -1;
-	}
 	file->output = o_stream_create_fd_file(file->fd, 0, FALSE);
 
 	hdr = t_str_new(128);
diff -r b6135e6a5ff2 -r 47c746a769ba src/lib-storage/index/dbox/dbox-file.h
--- a/src/lib-storage/index/dbox/dbox-file.h	Sun Nov 11 17:21:19 2007 +0200
+++ b/src/lib-storage/index/dbox/dbox-file.h	Sun Nov 11 17:43:56 2007 +0200
@@ -224,6 +224,7 @@ void dbox_msg_header_fill(struct dbox_me
 void dbox_msg_header_fill(struct dbox_message_header *dbox_msg_hdr,
 			  uint32_t uid, uoff_t message_size);
 
+int dbox_create_fd(struct dbox_mailbox *mbox, const char *path);
 void dbox_file_set_syscall_error(struct dbox_file *file, const char *function);
 
 #endif
diff -r b6135e6a5ff2 -r 47c746a769ba src/lib-storage/index/dbox/dbox-index.c
--- a/src/lib-storage/index/dbox/dbox-index.c	Sun Nov 11 17:21:19 2007 +0200
+++ b/src/lib-storage/index/dbox/dbox-index.c	Sun Nov 11 17:43:56 2007 +0200
@@ -517,6 +517,30 @@ dbox_index_append_record(const struct db
 	str_append_c(str, '\n');
 }
 
+static int dbox_index_create_fd(struct dbox_mailbox *mbox, string_t *temp_path,
+				bool locked)
+{
+	mode_t old_mask;
+	int fd;
+
+	if (locked) {
+		str_append(temp_path, ".tmp");
+		return dbox_create_fd(mbox, str_c(temp_path));
+	}
+
+	str_append_c(temp_path, '.');
+	old_mask = umask(0777 & ~mbox->ibox.box.file_create_mode);
+	fd = safe_mkstemp_hostpid(temp_path, 0777, (uid_t)-1, (gid_t)-1);
+	umask(old_mask);
+
+	if (fd == -1) {
+		mail_storage_set_critical(mbox->ibox.box.storage,
+					  "safe_mkstemp_hostpid(%s) failed: %m",
+					  str_c(temp_path));
+	}
+	return fd;
+}
+
 static int dbox_index_recreate(struct dbox_index *index, bool locked)
 {
 	struct mail_storage *storage = &index->mbox->storage->storage;
@@ -530,27 +554,11 @@ static int dbox_index_recreate(struct db
 	t_push();
 	temp_path = t_str_new(256);
 	str_append(temp_path, index->path);
-	if (locked) {
-		str_append(temp_path, ".tmp");
-		fd = open(str_c(temp_path), O_RDWR | O_CREAT | O_TRUNC, 0600);
-		if (fd == -1) {
-			mail_storage_set_critical(storage,
-				"open(%s, O_CREAT) failed: %m",
-				str_c(temp_path));
-			t_pop();
-			return -1;
-		}
-	} else {
-		str_append_c(temp_path, '.');
-		fd = safe_mkstemp_hostpid(temp_path, 0600,
-					  (uid_t)-1, (gid_t)-1);
-		if (fd == -1) {
-			mail_storage_set_critical(storage,
-				"safe_mkstemp_hostpid(%s) failed: %m",
-				str_c(temp_path));
-			t_pop();
-			return -1;
-		}
+
+	fd = dbox_index_create_fd(index->mbox, temp_path, locked);
+	if (fd == -1) {
+		t_pop();
+		return -1;
 	}
 
 	str = t_str_new(256);


More information about the dovecot-cvs mailing list