dbox-save.c revision e6440616c02bb1404dc35debf45d9741260c7831
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2007-2010 Dovecot authors, see the included COPYING file */
b6b9c99fefbbc662bd9a0006566133c4480bf0e8Timo Sirainenvoid dbox_save_add_to_index(struct dbox_save_context *ctx)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen mail_index_append(ctx->trans, ctx->ctx.uid, &ctx->seq);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen mail_index_update_flags(ctx->trans, ctx->seq, MODIFY_REPLACE,
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen mail_index_update_keywords(ctx->trans, ctx->seq,
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen mail_index_update_modseq(ctx->trans, ctx->seq,
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenvoid dbox_save_begin(struct dbox_save_context *ctx, struct istream *input)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen ctx->mail = mail_alloc(_ctx->transaction, 0, NULL);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen ctx->input = index_mail_cache_parse_init(_ctx->dest_mail, crlf_input);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen /* write a dummy header. it'll get rewritten when we're finished */
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen memset(&dbox_msg_hdr, 0, sizeof(dbox_msg_hdr));
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen if (o_stream_send(ctx->dbox_output, &dbox_msg_hdr,
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen sizeof(dbox_msg_hdr)) < 0) {
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen mail_storage_set_critical(_ctx->transaction->box->storage,
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen "o_stream_send(%s) failed: %m",
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenint dbox_save_continue(struct mail_save_context *_ctx)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen struct dbox_save_context *ctx = (struct dbox_save_context *)_ctx;
b6b9c99fefbbc662bd9a0006566133c4480bf0e8Timo Sirainen struct mail_storage *storage = _ctx->transaction->box->storage;
d40c98b71de3acd7dd9e41178d449ca84d26e406Timo Sirainen if (o_stream_send_istream(_ctx->output, ctx->input) < 0) {
d40c98b71de3acd7dd9e41178d449ca84d26e406Timo Sirainen if (!mail_storage_set_error_from_errno(storage)) {
d40c98b71de3acd7dd9e41178d449ca84d26e406Timo Sirainen "o_stream_send_istream(%s) failed: %m",
d40c98b71de3acd7dd9e41178d449ca84d26e406Timo Sirainen index_mail_cache_parse_continue(_ctx->dest_mail);
d40c98b71de3acd7dd9e41178d449ca84d26e406Timo Sirainen /* both tee input readers may consume data from our primary
d40c98b71de3acd7dd9e41178d449ca84d26e406Timo Sirainen input stream. we'll have to make sure we don't return with
d40c98b71de3acd7dd9e41178d449ca84d26e406Timo Sirainen one of the streams still having data in them. */
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenvoid dbox_save_end(struct dbox_save_context *ctx)
2726de0d31ed0f1a1425e4c8d167f7681e07fc25Timo Sirainen struct ostream *dbox_output = ctx->dbox_output;
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen /* e.g. zlib plugin had changed this */
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenvoid dbox_save_write_metadata(struct mail_save_context *ctx,
2dcf09e68a6d84aba506b0a93897b186ea11520fTimo Sirainen memset(&metadata_hdr, 0, sizeof(metadata_hdr));
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen memcpy(metadata_hdr.magic_post, DBOX_MAGIC_POST,
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen o_stream_send(output, &metadata_hdr, sizeof(metadata_hdr));
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen str_printfa(str, "%c%lx\n", DBOX_METADATA_RECEIVED_TIME,
f561fba30221abcc40288cace406289a8e44da89Timo Sirainen str_printfa(str, "%c%lx\n", DBOX_METADATA_SAVE_TIME,
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen (unsigned long)ioloop_time);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen if (mail_get_virtual_size(ctx->dest_mail, &vsize) < 0)
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen str_printfa(str, "%c%llx\n", DBOX_METADATA_VIRTUAL_SIZE,
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen (unsigned long long)vsize);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen guid = binary_to_hex(guid_128, MAIL_GUID_128_SIZE);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen str_printfa(str, "%c%s\n", DBOX_METADATA_GUID, guid);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen /* save the original mailbox name so if mailbox indexes get
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen corrupted we can place at least some (hopefully most) of
f561fba30221abcc40288cace406289a8e44da89Timo Sirainen the messages to correct mailboxes. */
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen str_printfa(str, "%c%s\n", DBOX_METADATA_ORIG_MAILBOX,