sdbox-save.c revision 9a790c932a9e5d52939c5d9e56e8b0ef02166d43
5a580c3a38ced62d4bcc95b8ac7c4f2935b5d294Timo Sirainen/* Copyright (c) 2007-2012 Dovecot authors, see the included COPYING file */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "lib.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "array.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "fdatasync-path.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "hex-binary.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "hex-dec.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "str.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "istream.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "istream-crlf.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "ostream.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "write-full.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "index-mail.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "mail-copy.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "dbox-attachment.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "dbox-save.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "sdbox-storage.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "sdbox-file.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "sdbox-sync.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include <stdlib.h>
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstruct sdbox_save_context {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct dbox_save_context ctx;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct sdbox_mailbox *mbox;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct sdbox_sync_context *sync_ctx;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct dbox_file_append_context *append_ctx;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uint32_t first_saved_seq;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ARRAY_DEFINE(files, struct dbox_file *);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen};
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstruct dbox_file *
0001f76bf725c5cf403bade8556f142dd43144eeTimo Sirainensdbox_save_file_get_file(struct mailbox_transaction_context *t, uint32_t seq)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
c18ff860dc22960fd37c272d929f889c7939a2c8Timo Sirainen struct sdbox_save_context *ctx =
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen (struct sdbox_save_context *)t->save_ctx;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct dbox_file *const *files;
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen unsigned int count;
c18ff860dc22960fd37c272d929f889c7939a2c8Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_assert(seq >= ctx->first_saved_seq);
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen files = array_get(&ctx->files, &count);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_assert(count > 0);
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen i_assert(seq - ctx->first_saved_seq < count);
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen return files[seq - ctx->first_saved_seq];
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen}
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainenstruct mail_save_context *
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainensdbox_save_alloc(struct mailbox_transaction_context *t)
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen{
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen struct sdbox_mailbox *mbox = (struct sdbox_mailbox *)t->box;
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen struct sdbox_save_context *ctx =
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen (struct sdbox_save_context *)t->save_ctx;
cd83124e5d070a016c590bb0b1096d7828c7b6adTimo Sirainen
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen i_assert((t->flags & MAILBOX_TRANSACTION_FLAG_EXTERNAL) != 0);
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen if (ctx != NULL) {
c18ff860dc22960fd37c272d929f889c7939a2c8Timo Sirainen /* use the existing allocated structure */
714c6a150480112eb1a5f309d0cc39b60613a719Timo Sirainen ctx->ctx.failed = FALSE;
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen ctx->ctx.finished = FALSE;
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen ctx->ctx.cur_file = NULL;
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen ctx->ctx.dbox_output = NULL;
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen return &ctx->ctx.ctx;
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ctx = i_new(struct sdbox_save_context, 1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ctx->ctx.ctx.transaction = t;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ctx->ctx.trans = t->itrans;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ctx->mbox = mbox;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_array_init(&ctx->files, 32);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen t->save_ctx = &ctx->ctx.ctx;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return t->save_ctx;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid sdbox_save_add_file(struct mail_save_context *_ctx, struct dbox_file *file)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;
9a02317c852face76737763fa6ec43b444688de5Timo Sirainen struct dbox_file *const *files;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen unsigned int count;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen if (ctx->first_saved_seq == 0)
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen ctx->first_saved_seq = ctx->ctx.seq;
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen files = array_get(&ctx->files, &count);
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen if (count > 0) {
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen /* a plugin may leave a previously saved file open.
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen we'll close it here to avoid eating too many fds. */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen dbox_file_close(files[count-1]);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen array_append(&ctx->files, &file, 1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint sdbox_save_begin(struct mail_save_context *_ctx, struct istream *input)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct dbox_file *file;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen int ret;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen file = sdbox_file_create(ctx->mbox);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ctx->append_ctx = dbox_file_append_init(file);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ret = dbox_file_get_append_stream(ctx->append_ctx,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen &ctx->ctx.dbox_output);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (ret <= 0) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_assert(ret != 0);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen dbox_file_append_rollback(&ctx->append_ctx);
0001f76bf725c5cf403bade8556f142dd43144eeTimo Sirainen dbox_file_unref(&file);
e2eac5bb5637c2d4aaf453389750740931822b92Timo Sirainen ctx->ctx.failed = TRUE;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return -1;
e2bdacc34dde56aa664059ab56e8b77e82bd1805Timo Sirainen }
e2bdacc34dde56aa664059ab56e8b77e82bd1805Timo Sirainen ctx->ctx.cur_file = file;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen dbox_save_begin(&ctx->ctx, input);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen sdbox_save_add_file(_ctx, file);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return ctx->ctx.failed ? -1 : 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
cd83124e5d070a016c590bb0b1096d7828c7b6adTimo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic int dbox_save_mail_write_metadata(struct dbox_save_context *ctx,
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen struct dbox_file *file)
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct sdbox_file *sfile = (struct sdbox_file *)file;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen const ARRAY_TYPE(mail_attachment_extref) *extrefs_arr;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen const struct mail_attachment_extref *extrefs;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct dbox_message_header dbox_msg_hdr;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uoff_t message_size;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen guid_128_t guid_128;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen unsigned int i, count;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_assert(file->msg_header_size == sizeof(dbox_msg_hdr));
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen message_size = ctx->dbox_output->offset -
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen file->msg_header_size - file->file_header_size;
5e702db5540b2303e25554dee21bbf35a4813381Timo Sirainen
5e702db5540b2303e25554dee21bbf35a4813381Timo Sirainen dbox_save_write_metadata(&ctx->ctx, ctx->dbox_output,
5e702db5540b2303e25554dee21bbf35a4813381Timo Sirainen message_size, NULL, guid_128);
5e702db5540b2303e25554dee21bbf35a4813381Timo Sirainen dbox_msg_header_fill(&dbox_msg_hdr, message_size);
5e702db5540b2303e25554dee21bbf35a4813381Timo Sirainen if (o_stream_pwrite(ctx->dbox_output, &dbox_msg_hdr,
46908da82cd45dd90ca7e438c8453bc9669868ecTimo Sirainen sizeof(dbox_msg_hdr),
46908da82cd45dd90ca7e438c8453bc9669868ecTimo Sirainen file->file_header_size) < 0) {
5e702db5540b2303e25554dee21bbf35a4813381Timo Sirainen dbox_file_set_syscall_error(file, "pwrite()");
c18ff860dc22960fd37c272d929f889c7939a2c8Timo Sirainen return -1;
c18ff860dc22960fd37c272d929f889c7939a2c8Timo Sirainen }
c18ff860dc22960fd37c272d929f889c7939a2c8Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* remember the attachment paths until commit time */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen extrefs_arr = index_attachment_save_get_extrefs(&ctx->ctx);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (extrefs_arr != NULL)
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen extrefs = array_get(extrefs_arr, &count);
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen else {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen extrefs = NULL;
0001f76bf725c5cf403bade8556f142dd43144eeTimo Sirainen count = 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (count > 0) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen sfile->attachment_pool =
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen pool_alloconly_create("sdbox attachment paths", 512);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen p_array_init(&sfile->attachment_paths,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen sfile->attachment_pool, count);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; i < count; i++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen const char *path = p_strdup(sfile->attachment_pool,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen extrefs[i].path);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen array_append(&sfile->attachment_paths, &path, 1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainenstatic int dbox_save_finish_write(struct mail_save_context *_ctx)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct dbox_file **files;
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ctx->ctx.finished = TRUE;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (ctx->ctx.dbox_output == NULL)
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen return -1;
1f19649986397419d014febd1337c6eb7b530f26Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (_ctx->save_date != (time_t)-1) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* we can't change ctime, but we can add the date to cache */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct index_mail *mail = (struct index_mail *)_ctx->dest_mail;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uint32_t t = _ctx->save_date;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen index_mail_cache_add(mail, MAIL_CACHE_SAVE_DATE, &t, sizeof(t));
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen dbox_save_end(&ctx->ctx);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen files = array_idx_modifiable(&ctx->files, array_count(&ctx->files) - 1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (!ctx->ctx.failed) T_BEGIN {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (dbox_save_mail_write_metadata(&ctx->ctx, *files) < 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ctx->ctx.failed = TRUE;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen } T_END;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (ctx->ctx.failed) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_index_expunge(ctx->ctx.trans, ctx->ctx.seq);
06eb8c1371aa06478d8840b1373cab7c2752d5edTimo Sirainen dbox_file_append_rollback(&ctx->append_ctx);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen dbox_file_unlink(*files);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen dbox_file_unref(files);
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen array_delete(&ctx->files, array_count(&ctx->files) - 1, 1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen } else {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen dbox_file_append_checkpoint(ctx->append_ctx);
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen if (dbox_file_append_commit(&ctx->append_ctx) < 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ctx->ctx.failed = TRUE;
06eb8c1371aa06478d8840b1373cab7c2752d5edTimo Sirainen dbox_file_close(*files);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
06eb8c1371aa06478d8840b1373cab7c2752d5edTimo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_stream_unref(&ctx->ctx.input);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ctx->ctx.dbox_output = NULL;
c18ff860dc22960fd37c272d929f889c7939a2c8Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return ctx->ctx.failed ? -1 : 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
06eb8c1371aa06478d8840b1373cab7c2752d5edTimo Sirainen
06eb8c1371aa06478d8840b1373cab7c2752d5edTimo Sirainenint sdbox_save_finish(struct mail_save_context *ctx)
06eb8c1371aa06478d8840b1373cab7c2752d5edTimo Sirainen{
06eb8c1371aa06478d8840b1373cab7c2752d5edTimo Sirainen int ret;
06eb8c1371aa06478d8840b1373cab7c2752d5edTimo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ret = dbox_save_finish_write(ctx);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen index_save_context_free(ctx);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return ret;
9047d770bfbb93ab6af5363dedb2d01363877243Timo Sirainen}
836e57b1e7817d008f8ae05cd4b506f420fed80dTimo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid sdbox_save_cancel(struct mail_save_context *_ctx)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct dbox_save_context *ctx = (struct dbox_save_context *)_ctx;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ctx->failed = TRUE;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen (void)sdbox_save_finish(_ctx);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic int dbox_save_assign_uids(struct sdbox_save_context *ctx,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen const ARRAY_TYPE(seq_range) *uids)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct dbox_file *const *files;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct seq_range_iter iter;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen unsigned int i, count, n = 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uint32_t uid;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen bool ret;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen seq_range_array_iter_init(&iter, uids);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen files = array_get(&ctx->files, &count);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; i < count; i++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct sdbox_file *sfile = (struct sdbox_file *)files[i];
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ret = seq_range_array_iter_nth(&iter, n++, &uid);
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen i_assert(ret);
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen if (sdbox_file_assign_uid(sfile, uid) < 0)
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen return -1;
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen }
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen i_assert(!seq_range_array_iter_nth(&iter, n, &uid));
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen return 0;
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen}
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainenstatic void dbox_save_unref_files(struct sdbox_save_context *ctx)
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen{
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen struct dbox_file **files;
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen unsigned int i, count;
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen files = array_get_modifiable(&ctx->files, &count);
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen for (i = 0; i < count; i++) {
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen if (ctx->ctx.failed) {
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen struct sdbox_file *sfile =
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen (struct sdbox_file *)files[i];
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen (void)sdbox_file_unlink_aborted_save(sfile);
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen }
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen dbox_file_unref(&files[i]);
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen }
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen array_free(&ctx->files);
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen}
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainenint sdbox_transaction_save_commit_pre(struct mail_save_context *_ctx)
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen{
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen struct mailbox_transaction_context *_t = _ctx->transaction;
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen const struct mail_index_header *hdr;
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen i_assert(ctx->ctx.finished);
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen if (array_count(&ctx->files) == 0) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* the mail must be freed in the commit_pre() */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (ctx->ctx.mail != NULL)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_free(&ctx->ctx.mail);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (sdbox_sync_begin(ctx->mbox, SDBOX_SYNC_FLAG_FORCE |
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen SDBOX_SYNC_FLAG_FSYNC, &ctx->sync_ctx) < 0) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen sdbox_transaction_save_rollback(_ctx);
ddedc8b77c5bccc8d224ab5f8716d9874f5d8512Timo Sirainen return -1;
ddedc8b77c5bccc8d224ab5f8716d9874f5d8512Timo Sirainen }
ddedc8b77c5bccc8d224ab5f8716d9874f5d8512Timo Sirainen
ddedc8b77c5bccc8d224ab5f8716d9874f5d8512Timo Sirainen /* assign UIDs for new messages */
ddedc8b77c5bccc8d224ab5f8716d9874f5d8512Timo Sirainen hdr = mail_index_get_header(ctx->sync_ctx->sync_view);
ddedc8b77c5bccc8d224ab5f8716d9874f5d8512Timo Sirainen mail_index_append_finish_uids(ctx->ctx.trans, hdr->next_uid,
f1612f8421207632e1dc9addd6c23e7f7098a54cTimo Sirainen &_t->changes->saved_uids);
56aa9083e1742d0083885aaf0c5b8581577731aeTimo Sirainen if (dbox_save_assign_uids(ctx, &_t->changes->saved_uids) < 0) {
56aa9083e1742d0083885aaf0c5b8581577731aeTimo Sirainen sdbox_transaction_save_rollback(_ctx);
f1612f8421207632e1dc9addd6c23e7f7098a54cTimo Sirainen return -1;
56aa9083e1742d0083885aaf0c5b8581577731aeTimo Sirainen }
f71c2d4e6b802bf8e622bcd5df29286262d05d5aTimo Sirainen
f71c2d4e6b802bf8e622bcd5df29286262d05d5aTimo Sirainen if (ctx->ctx.mail != NULL)
f71c2d4e6b802bf8e622bcd5df29286262d05d5aTimo Sirainen mail_free(&ctx->ctx.mail);
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen _t->changes->uid_validity = hdr->uid_validity;
f71c2d4e6b802bf8e622bcd5df29286262d05d5aTimo Sirainen return 0;
f71c2d4e6b802bf8e622bcd5df29286262d05d5aTimo Sirainen}
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainenvoid sdbox_transaction_save_commit_post(struct mail_save_context *_ctx,
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen struct mail_index_transaction_commit_result *result)
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen{
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;
3c40cf45bb10548bf434aea0fca32b99acc99a35Timo Sirainen struct mail_storage *storage = _ctx->transaction->box->storage;
3c40cf45bb10548bf434aea0fca32b99acc99a35Timo Sirainen
3c40cf45bb10548bf434aea0fca32b99acc99a35Timo Sirainen _ctx->transaction = NULL; /* transaction is already freed */
3c40cf45bb10548bf434aea0fca32b99acc99a35Timo Sirainen
3c40cf45bb10548bf434aea0fca32b99acc99a35Timo Sirainen if (array_count(&ctx->files) == 0) {
3c40cf45bb10548bf434aea0fca32b99acc99a35Timo Sirainen sdbox_transaction_save_rollback(_ctx);
3c40cf45bb10548bf434aea0fca32b99acc99a35Timo Sirainen return;
3c40cf45bb10548bf434aea0fca32b99acc99a35Timo Sirainen }
3c40cf45bb10548bf434aea0fca32b99acc99a35Timo Sirainen
3c40cf45bb10548bf434aea0fca32b99acc99a35Timo Sirainen mail_index_sync_set_commit_result(ctx->sync_ctx->index_sync_ctx,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen result);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (sdbox_sync_finish(&ctx->sync_ctx, TRUE) < 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ctx->ctx.failed = TRUE;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
fb5efc6ed69da679d9da31ef62daa7024de18212Timo Sirainen if (storage->set->parsed_fsync_mode != FSYNC_MODE_NEVER) {
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen const char *box_path = mailbox_get_path(&ctx->mbox->box);
5833a4972491fdb7b78eac2280f31dc4b9fa2bb7Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (fdatasync_path(box_path) < 0) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_storage_set_critical(storage,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen "fdatasync_path(%s) failed: %m", box_path);
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen }
c18ff860dc22960fd37c272d929f889c7939a2c8Timo Sirainen }
c18ff860dc22960fd37c272d929f889c7939a2c8Timo Sirainen sdbox_transaction_save_rollback(_ctx);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid sdbox_transaction_save_rollback(struct mail_save_context *_ctx)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (!ctx->ctx.finished)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen sdbox_save_cancel(_ctx);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen dbox_save_unref_files(ctx);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (ctx->sync_ctx != NULL)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen (void)sdbox_sync_finish(&ctx->sync_ctx, FALSE);
9f19a50d5966643c4d1c5ca06868ac2ad31bc4d5Timo Sirainen
9f19a50d5966643c4d1c5ca06868ac2ad31bc4d5Timo Sirainen if (ctx->ctx.mail != NULL)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_free(&ctx->ctx.mail);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_free(ctx);
b780aa272b742a43579cdb523cc79cc8d4521306Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen