From 15dd1857d40544e2c356d4f1c87c42a0579c665c Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Sun, 2 Oct 2011 16:35:22 +0300 Subject: [PATCH] lda/lmtp: Moved common raw mailbox allocation code to raw-storage. --- 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(-) diff --git a/src/lda/main.c b/src/lda/main.c index 35a1077a01..ccc1b5d09b 100644 --- a/src/lda/main.c +++ b/src/lda/main.c @@ -212,14 +212,13 @@ int main(int argc, char *argv[]) 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 @@ int main(int argc, char *argv[]) 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 @@ int main(int argc, char *argv[]) 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 --git a/src/lib-storage/index/raw/raw-storage.c b/src/lib-storage/index/raw/raw-storage.c index 2ebf2d0ebd..e65b52b7c1 100644 --- a/src/lib-storage/index/raw/raw-storage.c +++ b/src/lib-storage/index/raw/raw-storage.c @@ -39,6 +39,52 @@ raw_storage_create_from_set(const struct setting_parser_info *set_info, 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 --git a/src/lib-storage/index/raw/raw-storage.h b/src/lib-storage/index/raw/raw-storage.h index e7f5553259..fddd998a91 100644 --- a/src/lib-storage/index/raw/raw-storage.h +++ b/src/lib-storage/index/raw/raw-storage.h @@ -28,4 +28,11 @@ struct mail_user * 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 --git a/src/lmtp/commands.c b/src/lmtp/commands.c index 90e0bf703a..1637ab5971 100644 --- a/src/lmtp/commands.c +++ b/src/lmtp/commands.c @@ -626,23 +626,18 @@ static int client_open_raw_mail(struct client *client, struct istream *input) 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); -- 2.47.3