bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek#define SDBOX_SAVECTX(s) container_of(DBOX_SAVECTX(s), struct sdbox_save_context, ctx)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainensdbox_save_file_get_file(struct mailbox_transaction_context *t, uint32_t seq)
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_save_context *ctx = SDBOX_SAVECTX(t->save_ctx);
cc0a651962a3e54d5a62231ac5847ae7f9f7de7fTimo Sirainen i_assert(((struct sdbox_file *)file)->written_to_disk);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainensdbox_save_alloc(struct mailbox_transaction_context *t)
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_mailbox *mbox = SDBOX_MAILBOX(t->box);
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_save_context *ctx = SDBOX_SAVECTX(t->save_ctx);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_assert((t->flags & MAILBOX_TRANSACTION_FLAG_EXTERNAL) != 0);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* use the existing allocated structure */
e8762c2b4914db7997fa9eb644a91586952d1876Timo Sirainenvoid sdbox_save_add_file(struct mail_save_context *_ctx, struct dbox_file *file)
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_save_context *ctx = SDBOX_SAVECTX(_ctx);
2b5818150ab6701eebd0cf35fc143dc5c7a71aa8Timo Sirainen /* a plugin may leave a previously saved file open.
2b5818150ab6701eebd0cf35fc143dc5c7a71aa8Timo Sirainen we'll close it here to avoid eating too many fds. */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint sdbox_save_begin(struct mail_save_context *_ctx, struct istream *input)
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_save_context *ctx = SDBOX_SAVECTX(_ctx);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ctx->append_ctx = dbox_file_append_init(file);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ret = dbox_file_get_append_stream(ctx->append_ctx,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic int dbox_save_mail_write_metadata(struct dbox_save_context *ctx,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen struct sdbox_file *sfile = (struct sdbox_file *)file;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen const ARRAY_TYPE(mail_attachment_extref) *extrefs_arr;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_assert(file->msg_header_size == sizeof(dbox_msg_hdr));
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen file->msg_header_size - file->file_header_size;
1f19649986397419d014febd1337c6eb7b530f26Timo Sirainen dbox_save_write_metadata(&ctx->ctx, ctx->dbox_output,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen dbox_msg_header_fill(&dbox_msg_hdr, message_size);
e6440616c02bb1404dc35debf45d9741260c7831Timo Sirainen if (o_stream_pwrite(ctx->dbox_output, &dbox_msg_hdr,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen dbox_file_set_syscall_error(file, "pwrite()");
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen /* remember the attachment paths until commit time */
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen extrefs_arr = index_attachment_save_get_extrefs(&ctx->ctx);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen pool_alloconly_create("sdbox attachment paths", 512);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen for (i = 0; i < count; i++) {
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen const char *path = p_strdup(sfile->attachment_pool,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen array_append(&sfile->attachment_paths, &path, 1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic int dbox_save_finish_write(struct mail_save_context *_ctx)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;
b8d314c6355009ad0b9e332b6acecdfac5cc8891Timo Sirainen /* we can't change ctime, but we can add the date to cache */
b8d314c6355009ad0b9e332b6acecdfac5cc8891Timo Sirainen struct index_mail *mail = (struct index_mail *)_ctx->dest_mail;
b8d314c6355009ad0b9e332b6acecdfac5cc8891Timo Sirainen index_mail_cache_add(mail, MAIL_CACHE_SAVE_DATE, &t, sizeof(t));
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen files = array_idx_modifiable(&ctx->files, array_count(&ctx->files) - 1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (dbox_save_mail_write_metadata(&ctx->ctx, *files) < 0)
f776b9a125c59a96de6807e9558942cf7b7ab079Timo Sirainen index_storage_save_abort_last(&ctx->ctx.ctx, ctx->ctx.seq);
9a790c932a9e5d52939c5d9e56e8b0ef02166d43Timo Sirainen array_delete(&ctx->files, array_count(&ctx->files) - 1, 1);
a62470cd293855dff816317d35356a989bda8f20Timo Sirainen if (dbox_file_append_commit(&ctx->append_ctx) < 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint sdbox_save_finish(struct mail_save_context *ctx)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid sdbox_save_cancel(struct mail_save_context *_ctx)
804fa3f03bd9170272168a5ad214053bbe3160c7Josef 'Jeff' Sipek struct dbox_save_context *ctx = DBOX_SAVECTX(_ctx);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic int dbox_save_assign_uids(struct sdbox_save_context *ctx,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; i < count; i++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct sdbox_file *sfile = (struct sdbox_file *)files[i];
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ret = seq_range_array_iter_nth(&iter, n++, &uid);
172b6edea36e425b0d0c35a854721ae63f57c289Timo Sirainen if (sdbox_file_assign_uid(sfile, uid, FALSE) < 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_assert(!seq_range_array_iter_nth(&iter, n, &uid));
e42e27fcc497c7b4a5cc0b6ff304abca5ccfcb4fTimo Sirainenstatic int dbox_save_assign_stub_uids(struct sdbox_save_context *ctx)
e42e27fcc497c7b4a5cc0b6ff304abca5ccfcb4fTimo Sirainen for (i = 0; i < count; i++) {
e42e27fcc497c7b4a5cc0b6ff304abca5ccfcb4fTimo Sirainen struct sdbox_file *sfile = (struct sdbox_file *)files[i];
172b6edea36e425b0d0c35a854721ae63f57c289Timo Sirainen if (sdbox_file_assign_uid(sfile, uid, TRUE) < 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void dbox_save_unref_files(struct sdbox_save_context *ctx)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen files = array_get_modifiable(&ctx->files, &count);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; i < count; i++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint sdbox_transaction_save_commit_pre(struct mail_save_context *_ctx)
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_save_context *ctx = SDBOX_SAVECTX(_ctx);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct mailbox_transaction_context *_t = _ctx->transaction;
c7a50b2c29780eaa3668bbac738e3fa3e4e171e8Timo Sirainen /* the mail must be freed in the commit_pre() */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (sdbox_sync_begin(ctx->mbox, SDBOX_SYNC_FLAG_FORCE |
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen /* update dbox header flags */
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen dbox_save_update_header_flags(&ctx->ctx, ctx->sync_ctx->sync_view,
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen ctx->mbox->hdr_ext_id, offsetof(struct sdbox_index_header, flags));
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen hdr = mail_index_get_header(ctx->sync_ctx->sync_view);
e42e27fcc497c7b4a5cc0b6ff304abca5ccfcb4fTimo Sirainen if ((_ctx->transaction->flags & MAILBOX_TRANSACTION_FLAG_FILL_IN_STUB) == 0) {
e42e27fcc497c7b4a5cc0b6ff304abca5ccfcb4fTimo Sirainen /* assign UIDs for new messages */
e42e27fcc497c7b4a5cc0b6ff304abca5ccfcb4fTimo Sirainen mail_index_append_finish_uids(ctx->ctx.trans, hdr->next_uid,
e42e27fcc497c7b4a5cc0b6ff304abca5ccfcb4fTimo Sirainen if (dbox_save_assign_uids(ctx, &_t->changes->saved_uids) < 0) {
e42e27fcc497c7b4a5cc0b6ff304abca5ccfcb4fTimo Sirainen /* assign UIDs that we stashed away */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen _t->changes->uid_validity = hdr->uid_validity;
9f19a50d5966643c4d1c5ca06868ac2ad31bc4d5Timo Sirainenvoid sdbox_transaction_save_commit_post(struct mail_save_context *_ctx,
9f19a50d5966643c4d1c5ca06868ac2ad31bc4d5Timo Sirainen struct mail_index_transaction_commit_result *result)
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_save_context *ctx = SDBOX_SAVECTX(_ctx);
b780aa272b742a43579cdb523cc79cc8d4521306Timo Sirainen struct mail_storage *storage = _ctx->transaction->box->storage;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen _ctx->transaction = NULL; /* transaction is already freed */
9f19a50d5966643c4d1c5ca06868ac2ad31bc4d5Timo Sirainen mail_index_sync_set_commit_result(ctx->sync_ctx->index_sync_ctx,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (sdbox_sync_finish(&ctx->sync_ctx, TRUE) < 0)
b780aa272b742a43579cdb523cc79cc8d4521306Timo Sirainen if (storage->set->parsed_fsync_mode != FSYNC_MODE_NEVER) {
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen const char *box_path = mailbox_get_path(&ctx->mbox->box);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid sdbox_transaction_save_rollback(struct mail_save_context *_ctx)
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_save_context *ctx = SDBOX_SAVECTX(_ctx);