mdbox-save.c revision 7f735cb86b2d8abd8f230089065eacfc24e9e5d6
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen/* Copyright (c) 2007-2010 Dovecot authors, see the included COPYING file */
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "lib.h"
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen#include "array.h"
24fc71a693331ffe77e2b6d81c70aca6fa055e47Timo Sirainen#include "fdatasync-path.h"
24fc71a693331ffe77e2b6d81c70aca6fa055e47Timo Sirainen#include "hex-binary.h"
c5794838af9995f50bfecb06a3cd4f9a0ac77858Timo Sirainen#include "hex-dec.h"
c5794838af9995f50bfecb06a3cd4f9a0ac77858Timo Sirainen#include "str.h"
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen#include "istream.h"
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen#include "istream-crlf.h"
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen#include "ostream.h"
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen#include "write-full.h"
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen#include "index-mail.h"
beb6125ee872e7fed57745ab33e6de99639180f3Timo Sirainen#include "mail-copy.h"
81a5d8714c566ce50c4a2409f59e82e716d576d2Timo Sirainen#include "dbox-save.h"
81a5d8714c566ce50c4a2409f59e82e716d576d2Timo Sirainen#include "mdbox-storage.h"
81a5d8714c566ce50c4a2409f59e82e716d576d2Timo Sirainen#include "mdbox-map.h"
81a5d8714c566ce50c4a2409f59e82e716d576d2Timo Sirainen#include "mdbox-file.h"
beb6125ee872e7fed57745ab33e6de99639180f3Timo Sirainen#include "mdbox-sync.h"
beb6125ee872e7fed57745ab33e6de99639180f3Timo Sirainen
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen#include <stdlib.h>
5e40ed3f0a2c2acddc9b8eab59670c7a850114c5Timo Sirainen
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainenstruct dbox_save_mail {
9f19a50d5966643c4d1c5ca06868ac2ad31bc4d5Timo Sirainen struct dbox_file_append_context *file_append;
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen uint32_t seq;
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen uint32_t append_offset;
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen};
46c31f64b9f0949f00b7819f45b22f2d64b2ea27Timo Sirainen
46c31f64b9f0949f00b7819f45b22f2d64b2ea27Timo Sirainenstruct mdbox_save_context {
46c31f64b9f0949f00b7819f45b22f2d64b2ea27Timo Sirainen struct dbox_save_context ctx;
46c31f64b9f0949f00b7819f45b22f2d64b2ea27Timo Sirainen
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen struct mdbox_mailbox *mbox;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen struct mdbox_sync_context *sync_ctx;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen struct dbox_file_append_context *cur_file_append;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen struct mdbox_map_append_context *append_ctx;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ARRAY_TYPE(uint32_t) copy_map_uids;
ef4d0eafab4d26bba047551db1e23ceff8aa9404Timo Sirainen struct mdbox_map_atomic_context *atomic;
09c3a491f4f6ccebe290c7709bdc0d79a187610bTimo Sirainen struct mdbox_map_transaction_context *map_trans;
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen
db87d16551d1081ada01f787ea21aa3ed1402c31Timo Sirainen ARRAY_DEFINE(mails, struct dbox_save_mail);
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen};
09c3a491f4f6ccebe290c7709bdc0d79a187610bTimo Sirainen
515f81466f673c1b4f72e053f1a9686e6fca6b61Timo Sirainenstatic struct dbox_file *
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenmdbox_copy_file_get_file(struct mailbox_transaction_context *t,
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen uint32_t seq, uoff_t *offset_r)
0c909e3461607eadcd66f4eac69b7f34e37fccf1Timo Sirainen{
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen struct mdbox_save_context *ctx =
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen (struct mdbox_save_context *)t->save_ctx;
cf93189100a9136f2fd508c26e1573410bc8c1aaTimo Sirainen const struct mdbox_mail_index_record *rec;
48270badadd82279bfe50ae3d187aea8b0b2b30eTimo Sirainen const void *data;
48270badadd82279bfe50ae3d187aea8b0b2b30eTimo Sirainen bool expunged;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen uint32_t file_id;
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen mail_index_lookup_ext(t->view, seq, ctx->mbox->ext_id,
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen &data, &expunged);
c251a38df327599a62d341bf5c2282f31352faa5Timo Sirainen rec = data;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
aa38d1a0945f0bc13a225d043f53fad2eec666b1Timo Sirainen if (mdbox_map_lookup(ctx->mbox->storage->map, rec->map_uid,
aa38d1a0945f0bc13a225d043f53fad2eec666b1Timo Sirainen &file_id, offset_r) <= 0)
aa38d1a0945f0bc13a225d043f53fad2eec666b1Timo Sirainen i_unreached();
aa38d1a0945f0bc13a225d043f53fad2eec666b1Timo Sirainen
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen return mdbox_file_init(ctx->mbox->storage, file_id);
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen}
c5794838af9995f50bfecb06a3cd4f9a0ac77858Timo Sirainen
c5794838af9995f50bfecb06a3cd4f9a0ac77858Timo Sirainenstruct dbox_file *
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainenmdbox_save_file_get_file(struct mailbox_transaction_context *t,
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen uint32_t seq, uoff_t *offset_r)
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen{
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen struct mdbox_save_context *ctx =
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainen (struct mdbox_save_context *)t->save_ctx;
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen const struct dbox_save_mail *mails, *mail;
fee561b9d9162b130e662914fcebc9dd99b5c320Timo Sirainen unsigned int count;
238812433b4f7965fd662dce0f4efccb092630a8Timo Sirainen
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen mails = array_get(&ctx->mails, &count);
4b89231f4ec9cc69f4aea715e1d34f405c7e317dTimo Sirainen i_assert(count > 0);
48270badadd82279bfe50ae3d187aea8b0b2b30eTimo Sirainen i_assert(seq >= mails[0].seq);
48270badadd82279bfe50ae3d187aea8b0b2b30eTimo Sirainen
48270badadd82279bfe50ae3d187aea8b0b2b30eTimo Sirainen mail = &mails[seq - mails[0].seq];
46c31f64b9f0949f00b7819f45b22f2d64b2ea27Timo Sirainen i_assert(mail->seq == seq);
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen if (mail->file_append == NULL) {
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen /* copied mail */
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen return mdbox_copy_file_get_file(t, seq, offset_r);
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen }
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen /* saved mail */
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen if (dbox_file_append_flush(mail->file_append) < 0)
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen ctx->ctx.failed = TRUE;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen mail->file_append->file->refcount++;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen *offset_r = mail->append_offset;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen return mail->file_append->file;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen}
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen
a022088674a5ae72ed29ae001834cbad62a4f19fTimo Sirainenstruct mail_save_context *
a022088674a5ae72ed29ae001834cbad62a4f19fTimo Sirainenmdbox_save_alloc(struct mailbox_transaction_context *t)
a022088674a5ae72ed29ae001834cbad62a4f19fTimo Sirainen{
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen struct mdbox_mailbox *mbox = (struct mdbox_mailbox *)t->box;
a2cbf1d392ee983520451bc9b849a490f28ac298Timo Sirainen struct mdbox_save_context *ctx =
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen (struct mdbox_save_context *)t->save_ctx;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_assert((t->flags & MAILBOX_TRANSACTION_FLAG_EXTERNAL) != 0);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (ctx != NULL) {
8872e5c991430f96138a46e36b7f3c2c40d8e5c2Timo Sirainen /* use the existing allocated structure */
8872e5c991430f96138a46e36b7f3c2c40d8e5c2Timo Sirainen ctx->ctx.failed = FALSE;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen ctx->ctx.finished = FALSE;
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen ctx->ctx.cur_file = NULL;
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen ctx->ctx.dbox_output = NULL;
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen ctx->cur_file_append = NULL;
24fc71a693331ffe77e2b6d81c70aca6fa055e47Timo Sirainen return &ctx->ctx.ctx;
24fc71a693331ffe77e2b6d81c70aca6fa055e47Timo Sirainen }
24fc71a693331ffe77e2b6d81c70aca6fa055e47Timo Sirainen
09c3a491f4f6ccebe290c7709bdc0d79a187610bTimo Sirainen ctx = i_new(struct mdbox_save_context, 1);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen ctx->ctx.ctx.transaction = t;
b346610430690398b8c840006004a2df4aa8ce92Timo Sirainen ctx->ctx.trans = t->itrans;
09c3a491f4f6ccebe290c7709bdc0d79a187610bTimo Sirainen ctx->mbox = mbox;
985fa802913c96ce6f2e25bbc788ee39c416a7e0Timo Sirainen ctx->atomic = mdbox_map_atomic_begin(mbox->storage->map);
0c909e3461607eadcd66f4eac69b7f34e37fccf1Timo Sirainen ctx->append_ctx = mdbox_map_append_begin(ctx->atomic);
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen i_array_init(&ctx->mails, 32);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen t->save_ctx = &ctx->ctx.ctx;
b346610430690398b8c840006004a2df4aa8ce92Timo Sirainen return t->save_ctx;
985fa802913c96ce6f2e25bbc788ee39c416a7e0Timo Sirainen}
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainenint mdbox_save_begin(struct mail_save_context *_ctx, struct istream *input)
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen{
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen struct mdbox_save_context *ctx = (struct mdbox_save_context *)_ctx;
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen struct dbox_save_mail *save_mail;
515f81466f673c1b4f72e053f1a9686e6fca6b61Timo Sirainen uoff_t mail_size, append_offset;
515f81466f673c1b4f72e053f1a9686e6fca6b61Timo Sirainen
515f81466f673c1b4f72e053f1a9686e6fca6b61Timo Sirainen /* get the size of the mail to be saved, if possible */
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen if (i_stream_get_size(input, TRUE, &mail_size) <= 0) {
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen const struct stat *st;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen /* we couldn't find out the exact size. fallback to non-exact,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen maybe it'll give something useful. the mail size is used
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen only to figure out if it's causing mdbox file to grow
5e88e4624aa6d482b5b195acd2f4e02aeb385f20Timo Sirainen too large. */
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen st = i_stream_stat(input, FALSE);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen mail_size = st->st_size > 0 ? st->st_size : 0;
0c909e3461607eadcd66f4eac69b7f34e37fccf1Timo Sirainen }
0c909e3461607eadcd66f4eac69b7f34e37fccf1Timo Sirainen if (mdbox_map_append_next(ctx->append_ctx, mail_size, 0,
b346610430690398b8c840006004a2df4aa8ce92Timo Sirainen &ctx->cur_file_append,
b346610430690398b8c840006004a2df4aa8ce92Timo Sirainen &ctx->ctx.dbox_output) < 0) {
b346610430690398b8c840006004a2df4aa8ce92Timo Sirainen ctx->ctx.failed = TRUE;
b346610430690398b8c840006004a2df4aa8ce92Timo Sirainen return -1;
b346610430690398b8c840006004a2df4aa8ce92Timo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen i_assert(ctx->ctx.dbox_output->offset <= (uint32_t)-1);
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen append_offset = ctx->ctx.dbox_output->offset;
16c28dd75976f94acc4940d6ba68b6cd4853aac7Timo Sirainen
16c28dd75976f94acc4940d6ba68b6cd4853aac7Timo Sirainen ctx->ctx.cur_file = ctx->cur_file_append->file;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen dbox_save_begin(&ctx->ctx, input);
save_mail = array_append_space(&ctx->mails);
save_mail->file_append = ctx->cur_file_append;
save_mail->seq = ctx->ctx.seq;
save_mail->append_offset = append_offset;
return ctx->ctx.failed ? -1 : 0;
}
static int mdbox_save_mail_write_metadata(struct mdbox_save_context *ctx,
struct dbox_save_mail *mail)
{
struct dbox_file *file = mail->file_append->file;
struct dbox_message_header dbox_msg_hdr;
uoff_t message_size;
uint8_t guid_128[MAIL_GUID_128_SIZE];
i_assert(file->msg_header_size == sizeof(dbox_msg_hdr));
message_size = ctx->ctx.dbox_output->offset -
mail->append_offset - mail->file_append->file->msg_header_size;
dbox_save_write_metadata(&ctx->ctx.ctx, ctx->ctx.dbox_output,
message_size, ctx->mbox->box.name, guid_128);
/* save the 128bit GUID to index so if the map index gets corrupted
we can still find the message */
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) {
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);
/* 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 (mdbox_sync_begin(ctx->mbox, MDBOX_SYNC_FLAG_NO_PURGE |
MDBOX_SYNC_FLAG_FORCE |
MDBOX_SYNC_FLAG_FSYNC, 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) {
if (fdatasync_path(ctx->mbox->box.path) < 0) {
mail_storage_set_critical(storage,
"fdatasync_path(%s) failed: %m",
ctx->mbox->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)
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(_ctx->dest_mail, ctx->ctx.seq);
_ctx->dest_mail->saving = TRUE;
}
return 0;
}