sdbox-save.c revision e8762c2b4914db7997fa9eb644a91586952d1876
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2007-2010 Dovecot authors, see the included COPYING file */
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainensdbox_save_file_get_file(struct mailbox_transaction_context *t, uint32_t seq)
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen unsigned int count;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainensdbox_save_alloc(struct mailbox_transaction_context *t)
7569ab8537418b7fc369265f26595b0ef9e4cb35Timo Sirainen struct sdbox_mailbox *mbox = (struct sdbox_mailbox *)t->box;
7569ab8537418b7fc369265f26595b0ef9e4cb35Timo Sirainen i_assert((t->flags & MAILBOX_TRANSACTION_FLAG_EXTERNAL) != 0);
7569ab8537418b7fc369265f26595b0ef9e4cb35Timo Sirainen /* use the existing allocated structure */
798cfe56c9871262770384da1239162b3800cce1Timo Sirainenvoid sdbox_save_add_file(struct mail_save_context *_ctx, struct dbox_file *file)
798cfe56c9871262770384da1239162b3800cce1Timo Sirainen struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;
226259ee6fb9830dafc1a5ba1e95bf5a4345b406Timo Sirainenint sdbox_save_begin(struct mail_save_context *_ctx, struct istream *input)
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;
226259ee6fb9830dafc1a5ba1e95bf5a4345b406Timo Sirainen ctx->append_ctx = dbox_file_append_init(file);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen ret = dbox_file_get_append_stream(ctx->append_ctx,
33dd58ab84a020c4f061d2f6031eb6d4c168df1bTimo Sirainenstatic int dbox_save_mail_write_metadata(struct dbox_save_context *ctx,
798cfe56c9871262770384da1239162b3800cce1Timo Sirainen i_assert(file->msg_header_size == sizeof(dbox_msg_hdr));
82f53ea81671bcc7b9bf24a34b04a4ba2752efd3Timo Sirainen file->msg_header_size - file->file_header_size;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen dbox_save_write_metadata(&ctx->ctx, ctx->dbox_output, NULL, guid_128);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen dbox_msg_header_fill(&dbox_msg_hdr, message_size);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen if (o_stream_pwrite(ctx->dbox_output, &dbox_msg_hdr,
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen dbox_file_set_syscall_error(file, "pwrite()");
e9503210d3521a6833ed62dc332fc42ffb0e7a13Timo Sirainenstatic int dbox_save_finish_write(struct mail_save_context *_ctx)
e9503210d3521a6833ed62dc332fc42ffb0e7a13Timo Sirainen struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen /* we can't change ctime, but we can add the date to cache */
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen struct index_mail *mail = (struct index_mail *)_ctx->dest_mail;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen index_mail_cache_add(mail, MAIL_CACHE_SAVE_DATE, &t, sizeof(t));
70c181da837ed85fc5b0426c010b65609bda5329Timo Sirainen index_mail_cache_parse_deinit(_ctx->dest_mail,
70c181da837ed85fc5b0426c010b65609bda5329Timo Sirainen files = array_idx_modifiable(&ctx->files, array_count(&ctx->files) - 1);
adc409a7ac9689d3baf811712ad5a5432cab2d87Timo Sirainen if (dbox_save_mail_write_metadata(&ctx->ctx, *files) < 0)
adc409a7ac9689d3baf811712ad5a5432cab2d87Timo Sirainen else if (dbox_file_append_commit(&ctx->append_ctx) < 0)
adc409a7ac9689d3baf811712ad5a5432cab2d87Timo Sirainenint sdbox_save_finish(struct mail_save_context *ctx)
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainenvoid sdbox_save_cancel(struct mail_save_context *_ctx)
226259ee6fb9830dafc1a5ba1e95bf5a4345b406Timo Sirainen struct dbox_save_context *ctx = (struct dbox_save_context *)_ctx;
226259ee6fb9830dafc1a5ba1e95bf5a4345b406Timo Sirainenstatic int dbox_save_assign_uids(struct sdbox_save_context *ctx,
226259ee6fb9830dafc1a5ba1e95bf5a4345b406Timo Sirainen unsigned int i, count, n = 0;
226259ee6fb9830dafc1a5ba1e95bf5a4345b406Timo Sirainen for (i = 0; i < count; i++) {
226259ee6fb9830dafc1a5ba1e95bf5a4345b406Timo Sirainen struct sdbox_file *sfile = (struct sdbox_file *)files[i];
226259ee6fb9830dafc1a5ba1e95bf5a4345b406Timo Sirainen ret = seq_range_array_iter_nth(&iter, n++, &uid);
226259ee6fb9830dafc1a5ba1e95bf5a4345b406Timo Sirainen i_assert(!seq_range_array_iter_nth(&iter, n, &uid));
938835576b218f6bb9498d829cef9514f8609c6fTimo Sirainenstatic void dbox_save_unref_files(struct sdbox_save_context *ctx)
938835576b218f6bb9498d829cef9514f8609c6fTimo Sirainen struct mail_storage *storage = &ctx->mbox->storage->storage.storage;
226259ee6fb9830dafc1a5ba1e95bf5a4345b406Timo Sirainen unsigned int i, count;
798cfe56c9871262770384da1239162b3800cce1Timo Sirainen files = array_get_modifiable(&ctx->files, &count);
82f53ea81671bcc7b9bf24a34b04a4ba2752efd3Timo Sirainen for (i = 0; i < count; i++) {
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen "unlink(%s) failed: %m",
e2a700d0628e395d64cbcef4b5b4510816bf51c4Timo Sirainenint sdbox_transaction_save_commit_pre(struct mail_save_context *_ctx)
e2a700d0628e395d64cbcef4b5b4510816bf51c4Timo Sirainen struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;
e9503210d3521a6833ed62dc332fc42ffb0e7a13Timo Sirainen struct mailbox_transaction_context *_t = _ctx->transaction;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen if (sdbox_sync_begin(ctx->mbox, SDBOX_SYNC_FLAG_FORCE |
70c181da837ed85fc5b0426c010b65609bda5329Timo Sirainen /* assign UIDs for new messages */
70c181da837ed85fc5b0426c010b65609bda5329Timo Sirainen hdr = mail_index_get_header(ctx->sync_ctx->sync_view);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen mail_index_append_finish_uids(ctx->ctx.trans, hdr->next_uid,
70c181da837ed85fc5b0426c010b65609bda5329Timo Sirainen if (dbox_save_assign_uids(ctx, &_t->changes->saved_uids) < 0) {
e9503210d3521a6833ed62dc332fc42ffb0e7a13Timo Sirainen _t->changes->uid_validity = hdr->uid_validity;
43358fffb1d9f3091fd94895e0ac4643c50e2388Timo Sirainenvoid sdbox_transaction_save_commit_post(struct mail_save_context *_ctx,
43358fffb1d9f3091fd94895e0ac4643c50e2388Timo Sirainen struct mail_index_transaction_commit_result *result)
43358fffb1d9f3091fd94895e0ac4643c50e2388Timo Sirainen struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;
430c0b0c370bebeeceba2e206be76bc134742f41Timo Sirainen _ctx->transaction = NULL; /* transaction is already freed */
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen mail_index_sync_set_commit_result(ctx->sync_ctx->index_sync_ctx,
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen if (sdbox_sync_finish(&ctx->sync_ctx, TRUE) < 0)
484e12acec34f16e5a8adc001e23ae48f1dda8c7Timo Sirainen if (!ctx->mbox->storage->storage.storage.set->fsync_disable) {
798cfe56c9871262770384da1239162b3800cce1Timo Sirainen if (fdatasync_path(ctx->mbox->box.path) < 0) {
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainenvoid sdbox_transaction_save_rollback(struct mail_save_context *_ctx)
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;