mdbox-save.c revision ddedc8b77c5bccc8d224ab5f8716d9874f5d8512
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen/* Copyright (c) 2007-2012 Dovecot authors, see the included COPYING file */
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen
c0a708fa3f7b8f4fbca32052da5faf7a0125189dTimo Sirainen#include "lib.h"
c0a708fa3f7b8f4fbca32052da5faf7a0125189dTimo Sirainen#include "array.h"
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen#include "fdatasync-path.h"
c49a19168dab6fda80aee16ad799a8a56d3bc18fTimo Sirainen#include "hex-binary.h"
c0a708fa3f7b8f4fbca32052da5faf7a0125189dTimo Sirainen#include "hex-dec.h"
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen#include "str.h"
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen#include "istream.h"
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen#include "istream-crlf.h"
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen#include "ostream.h"
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen#include "write-full.h"
a75907609d7c410c9e17beedfafbf28b4439fa8aTimo Sirainen#include "index-mail.h"
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen#include "mail-copy.h"
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen#include "dbox-save.h"
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen#include "mdbox-storage.h"
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen#include "mdbox-map.h"
5a9e240ebf8d0daaf029973973b52e415148070bTimo Sirainen#include "mdbox-file.h"
5a9e240ebf8d0daaf029973973b52e415148070bTimo Sirainen#include "mdbox-sync.h"
d99107ddf4d9bccb710994482daf65276a9d6321Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen#include <stdlib.h>
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstruct dbox_save_mail {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct dbox_file_append_context *file_append;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen uint32_t seq;
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen uint32_t append_offset;
00fa8dcbc66f56daa737487c9dec7166c37de79eTimo Sirainen};
00fa8dcbc66f56daa737487c9dec7166c37de79eTimo Sirainen
00fa8dcbc66f56daa737487c9dec7166c37de79eTimo Sirainenstruct mdbox_save_context {
00fa8dcbc66f56daa737487c9dec7166c37de79eTimo Sirainen struct dbox_save_context ctx;
00fa8dcbc66f56daa737487c9dec7166c37de79eTimo Sirainen
00fa8dcbc66f56daa737487c9dec7166c37de79eTimo Sirainen struct mdbox_mailbox *mbox;
00fa8dcbc66f56daa737487c9dec7166c37de79eTimo Sirainen struct mdbox_sync_context *sync_ctx;
00fa8dcbc66f56daa737487c9dec7166c37de79eTimo Sirainen
00fa8dcbc66f56daa737487c9dec7166c37de79eTimo Sirainen struct dbox_file_append_context *cur_file_append;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct mdbox_map_append_context *append_ctx;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ARRAY_TYPE(uint32_t) copy_map_uids;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct mdbox_map_atomic_context *atomic;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct mdbox_map_transaction_context *map_trans;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ARRAY_DEFINE(mails, struct dbox_save_mail);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen};
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
2598b2f36365b52d9754b9348a5be29569293e46Timo Sirainenstatic struct dbox_file *
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainenmdbox_copy_file_get_file(struct mailbox_transaction_context *t,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen uint32_t seq, uoff_t *offset_r)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen{
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct mdbox_save_context *ctx =
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen (struct mdbox_save_context *)t->save_ctx;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const struct mdbox_mail_index_record *rec;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const void *data;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen bool expunged;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen uint32_t file_id;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen mail_index_lookup_ext(t->view, seq, ctx->mbox->ext_id,
f059a046515f4b2b15a6c2a10a6f12f6166e39a5Timo Sirainen &data, &expunged);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen rec = data;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (mdbox_map_lookup(ctx->mbox->storage->map, rec->map_uid,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen &file_id, offset_r) < 0)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen i_unreached();
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen return mdbox_file_init(ctx->mbox->storage, file_id);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen}
46ec792dd4ccf6c34706c4774228301fafde6aa9Timo Sirainen
a75907609d7c410c9e17beedfafbf28b4439fa8aTimo Sirainenstruct dbox_file *
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenmdbox_save_file_get_file(struct mailbox_transaction_context *t,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen uint32_t seq, uoff_t *offset_r)
00fa8dcbc66f56daa737487c9dec7166c37de79eTimo Sirainen{
00fa8dcbc66f56daa737487c9dec7166c37de79eTimo Sirainen struct mdbox_save_context *ctx =
c0a708fa3f7b8f4fbca32052da5faf7a0125189dTimo Sirainen (struct mdbox_save_context *)t->save_ctx;
ad004e44be109684521494b5af2ad1da39b8bb27Timo Sirainen const struct dbox_save_mail *mails, *mail;
97db4761382024093f441e4bc78ba8b6a056504dTimo Sirainen unsigned int count;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen mails = array_get(&ctx->mails, &count);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen i_assert(count > 0);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen i_assert(seq >= mails[0].seq);
ad004e44be109684521494b5af2ad1da39b8bb27Timo Sirainen
64e244defe74f513ce94f33d000a048ddbe2ea23Timo Sirainen mail = &mails[seq - mails[0].seq];
c0a708fa3f7b8f4fbca32052da5faf7a0125189dTimo Sirainen i_assert(mail->seq == seq);
12cf3d0e03fc70fb0c8b91bc8fd83b4e14d7cdefTimo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (mail->file_append == NULL) {
97db4761382024093f441e4bc78ba8b6a056504dTimo Sirainen /* copied mail */
bf72c930996df0691932fb1143f360d260f27a06Timo Sirainen return mdbox_copy_file_get_file(t, seq, offset_r);
c0a708fa3f7b8f4fbca32052da5faf7a0125189dTimo Sirainen }
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* saved mail */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (dbox_file_append_flush(mail->file_append) < 0)
660b99a7059824676b2b8d6f79b8e15d47df25a2Timo Sirainen ctx->ctx.failed = TRUE;
5a9e240ebf8d0daaf029973973b52e415148070bTimo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen mail->file_append->file->refcount++;
bbba7d0fce1b6ce5baa2d7ef946eb1b63e2ab518Timo Sirainen *offset_r = mail->append_offset;
bbba7d0fce1b6ce5baa2d7ef946eb1b63e2ab518Timo Sirainen return mail->file_append->file;
c0a708fa3f7b8f4fbca32052da5faf7a0125189dTimo Sirainen}
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstruct mail_save_context *
3612ee5c737954d5fb88fd1775aad80f7bf1dc4eTimo Sirainenmdbox_save_alloc(struct mailbox_transaction_context *t)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen{
c49a19168dab6fda80aee16ad799a8a56d3bc18fTimo Sirainen struct mdbox_mailbox *mbox = (struct mdbox_mailbox *)t->box;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen struct mdbox_save_context *ctx =
a75907609d7c410c9e17beedfafbf28b4439fa8aTimo Sirainen (struct mdbox_save_context *)t->save_ctx;
c49a19168dab6fda80aee16ad799a8a56d3bc18fTimo Sirainen
38cb3d139aefb7c65919cf4aba5ded7b5fd50e6fTimo Sirainen i_assert((t->flags & MAILBOX_TRANSACTION_FLAG_EXTERNAL) != 0);
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen
c49a19168dab6fda80aee16ad799a8a56d3bc18fTimo Sirainen if (ctx != NULL) {
bf72c930996df0691932fb1143f360d260f27a06Timo Sirainen /* use the existing allocated structure */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ctx->ctx.failed = FALSE;
02ccba3d3be96444abd15b5254864c9151bbeb30Timo Sirainen ctx->ctx.finished = FALSE;
087939d3fa9c4056419386c9d6c81f147de534cdTimo Sirainen ctx->ctx.cur_file = NULL;
02ccba3d3be96444abd15b5254864c9151bbeb30Timo Sirainen ctx->ctx.dbox_output = NULL;
bf72c930996df0691932fb1143f360d260f27a06Timo Sirainen ctx->cur_file_append = NULL;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen return &ctx->ctx.ctx;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen }
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ctx = i_new(struct mdbox_save_context, 1);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ctx->ctx.ctx.transaction = t;
c49a19168dab6fda80aee16ad799a8a56d3bc18fTimo Sirainen ctx->ctx.trans = t->itrans;
c49a19168dab6fda80aee16ad799a8a56d3bc18fTimo Sirainen ctx->mbox = mbox;
89795c6bbbc52bb382e88bc8617d22092223e9a5Timo Sirainen ctx->atomic = mdbox_map_atomic_begin(mbox->storage->map);
c49a19168dab6fda80aee16ad799a8a56d3bc18fTimo Sirainen ctx->append_ctx = mdbox_map_append_begin(ctx->atomic);
02ccba3d3be96444abd15b5254864c9151bbeb30Timo Sirainen i_array_init(&ctx->mails, 32);
3ffb7fd86484c474b42f3f1e981ab0f7168b5df9Timo Sirainen t->save_ctx = &ctx->ctx.ctx;
3ffb7fd86484c474b42f3f1e981ab0f7168b5df9Timo Sirainen return t->save_ctx;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen}
c0a708fa3f7b8f4fbca32052da5faf7a0125189dTimo Sirainen
c0a708fa3f7b8f4fbca32052da5faf7a0125189dTimo Sirainenint mdbox_save_begin(struct mail_save_context *_ctx, struct istream *input)
c0a708fa3f7b8f4fbca32052da5faf7a0125189dTimo Sirainen{
ad004e44be109684521494b5af2ad1da39b8bb27Timo Sirainen struct mdbox_save_context *ctx = (struct mdbox_save_context *)_ctx;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct dbox_save_mail *save_mail;
ad004e44be109684521494b5af2ad1da39b8bb27Timo Sirainen uoff_t mail_size, append_offset;
f059a046515f4b2b15a6c2a10a6f12f6166e39a5Timo Sirainen
f059a046515f4b2b15a6c2a10a6f12f6166e39a5Timo Sirainen /* get the size of the mail to be saved, if possible */
f059a046515f4b2b15a6c2a10a6f12f6166e39a5Timo Sirainen if (i_stream_get_size(input, TRUE, &mail_size) <= 0) {
f059a046515f4b2b15a6c2a10a6f12f6166e39a5Timo Sirainen const struct stat *st;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* we couldn't find out the exact size. fallback to non-exact,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen maybe it'll give something useful. the mail size is used
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen only to figure out if it's causing mdbox file to grow
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen too large. */
ec23e16ed879e289d12c6e1a5f9745dd3979004aTimo Sirainen st = i_stream_stat(input, FALSE);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen mail_size = st->st_size > 0 ? st->st_size : 0;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen }
c0a708fa3f7b8f4fbca32052da5faf7a0125189dTimo Sirainen if (mdbox_map_append_next(ctx->append_ctx, mail_size, 0,
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen &ctx->cur_file_append,
ad004e44be109684521494b5af2ad1da39b8bb27Timo Sirainen &ctx->ctx.dbox_output) < 0) {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ctx->ctx.failed = TRUE;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen return -1;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen }
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen i_assert(ctx->ctx.dbox_output->offset <= (uint32_t)-1);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen append_offset = ctx->ctx.dbox_output->offset;
660b99a7059824676b2b8d6f79b8e15d47df25a2Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ctx->ctx.cur_file = ctx->cur_file_append->file;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen dbox_save_begin(&ctx->ctx, input);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen save_mail = array_append_space(&ctx->mails);
00fa8dcbc66f56daa737487c9dec7166c37de79eTimo Sirainen save_mail->file_append = ctx->cur_file_append;
00fa8dcbc66f56daa737487c9dec7166c37de79eTimo Sirainen save_mail->seq = ctx->ctx.seq;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen save_mail->append_offset = append_offset;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen return ctx->ctx.failed ? -1 : 0;
00fa8dcbc66f56daa737487c9dec7166c37de79eTimo Sirainen}
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
46ec792dd4ccf6c34706c4774228301fafde6aa9Timo Sirainenstatic int mdbox_save_mail_write_metadata(struct mdbox_save_context *ctx,
a75907609d7c410c9e17beedfafbf28b4439fa8aTimo Sirainen struct dbox_save_mail *mail)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen{
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct dbox_file *file = mail->file_append->file;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct dbox_message_header dbox_msg_hdr;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen uoff_t message_size;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen guid_128_t guid_128;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen i_assert(file->msg_header_size == sizeof(dbox_msg_hdr));
c49a19168dab6fda80aee16ad799a8a56d3bc18fTimo Sirainen
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen message_size = ctx->ctx.dbox_output->offset -
71f1783adc89b4fe3588c72b23e059b320da8fadTimo Sirainen mail->append_offset - mail->file_append->file->msg_header_size;
c0a708fa3f7b8f4fbca32052da5faf7a0125189dTimo Sirainen
c0a708fa3f7b8f4fbca32052da5faf7a0125189dTimo Sirainen dbox_save_write_metadata(&ctx->ctx.ctx, ctx->ctx.dbox_output,
caea325346da6fb1cf503b35a619467a997acbfaTimo Sirainen message_size, ctx->mbox->box.name, guid_128);
caea325346da6fb1cf503b35a619467a997acbfaTimo Sirainen /* save the 128bit GUID to index so if the map index gets corrupted
caea325346da6fb1cf503b35a619467a997acbfaTimo Sirainen we can still find the message */
c0a708fa3f7b8f4fbca32052da5faf7a0125189dTimo Sirainen mail_index_update_ext(ctx->ctx.trans, ctx->ctx.seq,
ctx->mbox->guid_ext_id, guid_128, NULL);
dbox_msg_header_fill(&dbox_msg_hdr, message_size);
if (o_stream_pwrite(ctx->ctx.dbox_output, &dbox_msg_hdr,
sizeof(dbox_msg_hdr), mail->append_offset) < 0) {
dbox_file_set_syscall_error(file, "pwrite()");
return -1;
}
return 0;
}
static int mdbox_save_finish_write(struct mail_save_context *_ctx)
{
struct mdbox_save_context *ctx = (struct mdbox_save_context *)_ctx;
struct dbox_save_mail *mail;
ctx->ctx.finished = TRUE;
if (ctx->ctx.dbox_output == NULL)
return -1;
dbox_save_end(&ctx->ctx);
index_mail_cache_parse_deinit(_ctx->dest_mail,
_ctx->received_date, !ctx->ctx.failed);
mail = array_idx_modifiable(&ctx->mails, array_count(&ctx->mails) - 1);
if (!ctx->ctx.failed) T_BEGIN {
if (mdbox_save_mail_write_metadata(ctx, mail) < 0)
ctx->ctx.failed = TRUE;
else
mdbox_map_append_finish(ctx->append_ctx);
} T_END;
if (mail->file_append->file->input != NULL) {
/* if we try to read the saved mail before unlocking file,
make sure the input stream doesn't have stale data */
i_stream_sync(mail->file_append->file->input);
}
i_stream_unref(&ctx->ctx.input);
if (ctx->ctx.failed) {
mail_index_expunge(ctx->ctx.trans, ctx->ctx.seq);
mdbox_map_append_abort(ctx->append_ctx);
array_delete(&ctx->mails, array_count(&ctx->mails) - 1, 1);
return -1;
}
return 0;
}
int mdbox_save_finish(struct mail_save_context *ctx)
{
int ret;
ret = mdbox_save_finish_write(ctx);
index_save_context_free(ctx);
return ret;
}
void mdbox_save_cancel(struct mail_save_context *_ctx)
{
struct dbox_save_context *ctx = (struct dbox_save_context *)_ctx;
ctx->failed = TRUE;
(void)mdbox_save_finish(_ctx);
}
static void
mdbox_save_set_map_uids(struct mdbox_save_context *ctx,
uint32_t first_map_uid, uint32_t last_map_uid)
{
struct mdbox_mailbox *mbox = ctx->mbox;
struct mail_index_view *view = ctx->ctx.ctx.transaction->view;
const struct mdbox_mail_index_record *old_rec;
struct mdbox_mail_index_record rec;
const struct dbox_save_mail *mails;
unsigned int i, count;
const void *data;
bool expunged;
uint32_t next_map_uid = first_map_uid;
mdbox_update_header(mbox, ctx->ctx.trans, NULL);
memset(&rec, 0, sizeof(rec));
rec.save_date = ioloop_time;
mails = array_get(&ctx->mails, &count);
for (i = 0; i < count; i++) {
mail_index_lookup_ext(view, mails[i].seq, mbox->ext_id,
&data, &expunged);
old_rec = data;
if (old_rec != NULL && old_rec->map_uid != 0) {
/* message was copied. keep the existing map uid */
continue;
}
rec.map_uid = next_map_uid++;
mail_index_update_ext(ctx->ctx.trans, mails[i].seq,
mbox->ext_id, &rec, NULL);
}
i_assert(next_map_uid == last_map_uid + 1);
}
int mdbox_transaction_save_commit_pre(struct mail_save_context *_ctx)
{
struct mdbox_save_context *ctx = (struct mdbox_save_context *)_ctx;
struct mailbox_transaction_context *_t = _ctx->transaction;
const struct mail_index_header *hdr;
uint32_t first_map_uid, last_map_uid;
i_assert(ctx->ctx.finished);
/* flush/fsync writes to m.* files before locking the map */
if (mdbox_map_append_flush(ctx->append_ctx) < 0) {
mdbox_transaction_save_rollback(_ctx);
return -1;
}
/* make sure the map gets locked */
if (mdbox_map_atomic_lock(ctx->atomic) < 0) {
mdbox_transaction_save_rollback(_ctx);
return -1;
}
/* assign map UIDs for newly saved messages. they're written to
transaction log immediately within this function, but the map
is left locked. */
if (mdbox_map_append_assign_map_uids(ctx->append_ctx, &first_map_uid,
&last_map_uid) < 0) {
mdbox_transaction_save_rollback(_ctx);
return -1;
}
/* lock the mailbox after map to avoid deadlocks. if we've noticed
any corruption, deal with it later, otherwise we won't have
up-to-date atomic->sync_view */
if (mdbox_sync_begin(ctx->mbox, MDBOX_SYNC_FLAG_NO_PURGE |
MDBOX_SYNC_FLAG_FORCE |
MDBOX_SYNC_FLAG_FSYNC |
MDBOX_SYNC_FLAG_NO_REBUILD, ctx->atomic,
&ctx->sync_ctx) < 0) {
mdbox_transaction_save_rollback(_ctx);
return -1;
}
/* assign UIDs for new messages */
hdr = mail_index_get_header(ctx->sync_ctx->sync_view);
mail_index_append_finish_uids(ctx->ctx.trans, hdr->next_uid,
&_t->changes->saved_uids);
/* save map UIDs to mailbox index */
if (first_map_uid != 0)
mdbox_save_set_map_uids(ctx, first_map_uid, last_map_uid);
/* increase map's refcount for copied mails */
if (array_is_created(&ctx->copy_map_uids)) {
ctx->map_trans = mdbox_map_transaction_begin(ctx->atomic, FALSE);
if (mdbox_map_update_refcounts(ctx->map_trans,
&ctx->copy_map_uids, 1) < 0) {
mdbox_transaction_save_rollback(_ctx);
return -1;
}
}
if (ctx->ctx.mail != NULL)
mail_free(&ctx->ctx.mail);
_t->changes->uid_validity = hdr->uid_validity;
return 0;
}
void mdbox_transaction_save_commit_post(struct mail_save_context *_ctx,
struct mail_index_transaction_commit_result *result)
{
struct mdbox_save_context *ctx = (struct mdbox_save_context *)_ctx;
struct mail_storage *storage = _ctx->transaction->box->storage;
_ctx->transaction = NULL; /* transaction is already freed */
mail_index_sync_set_commit_result(ctx->sync_ctx->index_sync_ctx,
result);
/* finish writing the mailbox APPENDs */
if (mdbox_sync_finish(&ctx->sync_ctx, TRUE) == 0) {
/* commit refcount increases for copied mails */
if (ctx->map_trans != NULL) {
if (mdbox_map_transaction_commit(ctx->map_trans) < 0)
mdbox_map_atomic_set_failed(ctx->atomic);
}
/* flush file append writes */
if (mdbox_map_append_commit(ctx->append_ctx) < 0)
mdbox_map_atomic_set_failed(ctx->atomic);
}
mdbox_map_append_free(&ctx->append_ctx);
/* update the sync tail offset, everything else
was already written at this point. */
(void)mdbox_map_atomic_finish(&ctx->atomic);
if (storage->set->parsed_fsync_mode != FSYNC_MODE_NEVER) {
const char *box_path = mailbox_get_path(&ctx->mbox->box);
if (fdatasync_path(box_path) < 0) {
mail_storage_set_critical(storage,
"fdatasync_path(%s) failed: %m", box_path);
}
}
mdbox_transaction_save_rollback(_ctx);
}
void mdbox_transaction_save_rollback(struct mail_save_context *_ctx)
{
struct mdbox_save_context *ctx = (struct mdbox_save_context *)_ctx;
if (!ctx->ctx.finished)
mdbox_save_cancel(&ctx->ctx.ctx);
if (ctx->append_ctx != NULL)
mdbox_map_append_free(&ctx->append_ctx);
if (ctx->map_trans != NULL)
mdbox_map_transaction_free(&ctx->map_trans);
if (ctx->atomic != NULL)
(void)mdbox_map_atomic_finish(&ctx->atomic);
if (array_is_created(&ctx->copy_map_uids))
array_free(&ctx->copy_map_uids);
if (ctx->sync_ctx != NULL)
(void)mdbox_sync_finish(&ctx->sync_ctx, FALSE);
if (ctx->ctx.mail != NULL)
mail_free(&ctx->ctx.mail);
array_free(&ctx->mails);
i_free(ctx);
}
int mdbox_copy(struct mail_save_context *_ctx, struct mail *mail)
{
struct mdbox_save_context *ctx = (struct mdbox_save_context *)_ctx;
struct dbox_save_mail *save_mail;
struct mdbox_mailbox *src_mbox;
struct mdbox_mail_index_record rec;
const void *data;
bool expunged;
ctx->ctx.finished = TRUE;
if (mail->box->storage != _ctx->transaction->box->storage ||
_ctx->transaction->box->disable_reflink_copy_to ||
_ctx->guid != NULL)
return mail_storage_copy(_ctx, mail);
src_mbox = (struct mdbox_mailbox *)mail->box;
memset(&rec, 0, sizeof(rec));
rec.save_date = ioloop_time;
if (mdbox_mail_lookup(src_mbox, mail->transaction->view, mail->seq,
&rec.map_uid) < 0)
return -1;
/* remember the map_uid so we can later increase its refcount */
if (!array_is_created(&ctx->copy_map_uids))
i_array_init(&ctx->copy_map_uids, 32);
array_append(&ctx->copy_map_uids, &rec.map_uid, 1);
/* add message to mailbox index */
dbox_save_add_to_index(&ctx->ctx);
mail_index_update_ext(ctx->ctx.trans, ctx->ctx.seq,
ctx->mbox->ext_id, &rec, NULL);
mail_index_lookup_ext(mail->transaction->view, mail->seq,
src_mbox->guid_ext_id, &data, &expunged);
if (data != NULL) {
mail_index_update_ext(ctx->ctx.trans, ctx->ctx.seq,
ctx->mbox->guid_ext_id, data, NULL);
}
index_copy_cache_fields(_ctx, mail, ctx->ctx.seq);
save_mail = array_append_space(&ctx->mails);
save_mail->seq = ctx->ctx.seq;
if (_ctx->dest_mail != NULL)
mail_set_seq_saving(_ctx->dest_mail, ctx->ctx.seq);
return 0;
}