dovecot-2.1: lda/lmtp: Moved common raw mailbox allocation code ...

dovecot at dovecot.org dovecot at dovecot.org
Sun Oct 2 16:28:02 EEST 2011


details:   http://hg.dovecot.org/dovecot-2.1/rev/ac9fecc9202e
changeset: 13581:ac9fecc9202e
user:      Timo Sirainen <tss at iki.fi>
date:      Sun Oct 02 16:35:22 2011 +0300
description:
lda/lmtp: Moved common raw mailbox allocation code to raw-storage.

diffstat:

 src/lda/main.c                          |  33 +++++++----------------
 src/lib-storage/index/raw/raw-storage.c |  46 +++++++++++++++++++++++++++++++++
 src/lib-storage/index/raw/raw-storage.h |   7 +++++
 src/lmtp/commands.c                     |  11 ++-----
 4 files changed, 66 insertions(+), 31 deletions(-)

diffs (173 lines):

diff -r b7995a25c052 -r ac9fecc9202e src/lda/main.c
--- a/src/lda/main.c	Sun Oct 02 16:34:41 2011 +0300
+++ b/src/lda/main.c	Sun Oct 02 16:35:22 2011 +0300
@@ -212,14 +212,13 @@
 	struct mail_storage_service_user *service_user;
 	struct mail_storage_service_input service_input;
 	struct mail_user *raw_mail_user;
-	struct mail_namespace *raw_ns;
 	struct mail_storage *storage;
 	struct mailbox *box;
-	struct raw_mailbox *raw_box;
 	struct istream *input;
 	struct mailbox_transaction_context *t;
 	struct mailbox_header_lookup_ctx *headers_ctx;
 	const char *user_source = "", *destaddr_source = "";
+	const char *envelope_sender;
 	void **sets;
 	uid_t process_euid;
 	bool stderr_rejection = FALSE;
@@ -372,35 +371,24 @@
 	sets = master_service_settings_get_others(master_service);
 	raw_mail_user =
 		raw_storage_create_from_set(ctx.dest_user->set_info, sets[0]);
-	raw_ns = raw_mail_user->namespaces;
 
+	envelope_sender = ctx.src_envelope_sender != NULL ?
+		ctx.src_envelope_sender : DEFAULT_ENVELOPE_SENDER;
 	if (path == NULL) {
 		input = create_raw_stream(&ctx, 0, &mtime);
 		i_stream_set_name(input, "stdin");
-		box = mailbox_alloc(raw_ns->list, "Dovecot Delivery Mail",
-				    MAILBOX_FLAG_NO_INDEX_FILES);
-		if (mailbox_open_stream(box, input) < 0) {
-			i_fatal("Can't open delivery mail as raw: %s",
-				mailbox_get_last_error(box, &error));
-		}
+		ret = raw_mailbox_alloc_stream(raw_mail_user, input, mtime,
+					       envelope_sender, &box);
 		i_stream_unref(&input);
 	} else {
-		mtime = (time_t)-1;
-		box = mailbox_alloc(raw_ns->list, path,
-				    MAILBOX_FLAG_NO_INDEX_FILES);
-		if (mailbox_open(box) < 0) {
-			i_fatal("Can't open delivery mail as raw: %s",
-				mailbox_get_last_error(box, &error));
-		}
+		ret = raw_mailbox_alloc_path(raw_mail_user, path, (time_t)-1,
+					     envelope_sender, &box);
 	}
-	if (mailbox_sync(box, 0) < 0) {
-		i_fatal("Can't sync delivery mail: %s",
+	if (ret < 0) {
+		i_fatal("Can't open delivery mail as raw: %s",
 			mailbox_get_last_error(box, &error));
 	}
-	raw_box = (struct raw_mailbox *)box;
-	raw_box->envelope_sender = ctx.src_envelope_sender != NULL ?
-		ctx.src_envelope_sender : DEFAULT_ENVELOPE_SENDER;
-	raw_box->mtime = mtime;
+	mail_user_unref(&raw_mail_user);
 
 	t = mailbox_transaction_begin(box, 0);
 	headers_ctx = mailbox_header_lookup_init(box, wanted_headers);
@@ -468,7 +456,6 @@
 	mailbox_free(&box);
 
 	mail_user_unref(&ctx.dest_user);
-	mail_user_unref(&raw_mail_user);
 	mail_deliver_session_deinit(&ctx.session);
 
 	mail_storage_service_user_free(&service_user);
diff -r b7995a25c052 -r ac9fecc9202e src/lib-storage/index/raw/raw-storage.c
--- a/src/lib-storage/index/raw/raw-storage.c	Sun Oct 02 16:34:41 2011 +0300
+++ b/src/lib-storage/index/raw/raw-storage.c	Sun Oct 02 16:35:22 2011 +0300
@@ -39,6 +39,52 @@
 	return user;
 }
 
+static int
+raw_mailbox_alloc_common(struct mail_user *user, struct istream *input,
+			 const char *path, time_t received_time,
+			 const char *envelope_sender, struct mailbox **box_r)
+{
+	struct mail_namespace *ns = user->namespaces;
+	struct mailbox *box;
+	struct raw_mailbox *raw_box;
+	const char *name;
+
+	name = path != NULL ? path : i_stream_get_name(input);
+	box = *box_r = mailbox_alloc(ns->list, name,
+				     MAILBOX_FLAG_NO_INDEX_FILES);
+	if (input != NULL) {
+		if (mailbox_open_stream(box, input) < 0)
+			return -1;
+	} else {
+		if (mailbox_open(box) < 0)
+			return -1;
+	}
+	if (mailbox_sync(box, 0) < 0)
+		return -1;
+
+	i_assert(strcmp(box->storage->name, RAW_STORAGE_NAME) == 0);
+	raw_box = (struct raw_mailbox *)box;
+	raw_box->envelope_sender = envelope_sender;
+	raw_box->mtime = received_time;
+	return 0;
+}
+
+int raw_mailbox_alloc_stream(struct mail_user *user, struct istream *input,
+			     time_t received_time, const char *envelope_sender,
+			     struct mailbox **box_r)
+{
+	return raw_mailbox_alloc_common(user, input, NULL, received_time,
+					envelope_sender, box_r);
+}
+
+int raw_mailbox_alloc_path(struct mail_user *user, const char *path,
+			   time_t received_time, const char *envelope_sender,
+			   struct mailbox **box_r)
+{
+	return raw_mailbox_alloc_common(user, NULL, path, received_time,
+					envelope_sender, box_r);
+}
+
 static struct mail_storage *raw_storage_alloc(void)
 {
 	struct raw_storage *storage;
diff -r b7995a25c052 -r ac9fecc9202e src/lib-storage/index/raw/raw-storage.h
--- a/src/lib-storage/index/raw/raw-storage.h	Sun Oct 02 16:34:41 2011 +0300
+++ b/src/lib-storage/index/raw/raw-storage.h	Sun Oct 02 16:35:22 2011 +0300
@@ -28,4 +28,11 @@
 raw_storage_create_from_set(const struct setting_parser_info *set_info,
 			    const struct mail_user_settings *set);
 
+int raw_mailbox_alloc_stream(struct mail_user *user, struct istream *input,
+			     time_t received_time, const char *envelope_sender,
+			     struct mailbox **box_r);
+int raw_mailbox_alloc_path(struct mail_user *user, const char *path,
+			   time_t received_time, const char *envelope_sender,
+			   struct mailbox **box_r);
+
 #endif
diff -r b7995a25c052 -r ac9fecc9202e src/lmtp/commands.c
--- a/src/lmtp/commands.c	Sun Oct 02 16:34:41 2011 +0300
+++ b/src/lmtp/commands.c	Sun Oct 02 16:35:22 2011 +0300
@@ -626,23 +626,18 @@
 		NULL
 	};
 	struct mailbox *box;
-	struct raw_mailbox *raw_box;
 	struct mailbox_header_lookup_ctx *headers_ctx;
 	enum mail_error error;
 
-	box = mailbox_alloc(client->raw_mail_user->namespaces->list,
-			    "Dovecot Delivery Mail",
-			    MAILBOX_FLAG_NO_INDEX_FILES);
-	if (mailbox_open_stream(box, input) < 0 ||
-	    mailbox_sync(box, 0) < 0) {
+	if (raw_mailbox_alloc_stream(client->raw_mail_user, input,
+				     (time_t)-1, client->state.mail_from,
+				     &box) < 0) {
 		i_error("Can't open delivery mail as raw: %s",
 			mailbox_get_last_error(box, &error));
 		mailbox_free(&box);
 		client_rcpt_fail_all(client);
 		return -1;
 	}
-	raw_box = (struct raw_mailbox *)box;
-	raw_box->envelope_sender = client->state.mail_from;
 
 	client->state.raw_box = box;
 	client->state.raw_trans = mailbox_transaction_begin(box, 0);


More information about the dovecot-cvs mailing list