sdbox-save.c revision 1f19649986397419d014febd1337c6eb7b530f26
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2007-2010 Dovecot authors, see the included COPYING file */
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmodysdbox_save_file_get_file(struct mailbox_transaction_context *t, uint32_t seq)
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody unsigned int count;
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmodysdbox_save_alloc(struct mailbox_transaction_context *t)
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody struct sdbox_mailbox *mbox = (struct sdbox_mailbox *)t->box;
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody i_assert((t->flags & MAILBOX_TRANSACTION_FLAG_EXTERNAL) != 0);
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody /* use the existing allocated structure */
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmodyvoid sdbox_save_add_file(struct mail_save_context *_ctx, struct dbox_file *file)
d6bbf85809664a810726b5c711c7213874d8df57Phil Carmody struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmodyint sdbox_save_begin(struct mail_save_context *_ctx, struct istream *input)
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch ctx->append_ctx = dbox_file_append_init(file);
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch ret = dbox_file_get_append_stream(ctx->append_ctx,
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Boschstatic int dbox_save_mail_write_metadata(struct dbox_save_context *ctx,
191153d1a5b0eb0c129139570e3aa5212f28d2acJosef 'Jeff' Sipek i_assert(file->msg_header_size == sizeof(dbox_msg_hdr));
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch file->msg_header_size - file->file_header_size;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch dbox_save_write_metadata(&ctx->ctx, ctx->dbox_output,
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch dbox_msg_header_fill(&dbox_msg_hdr, message_size);
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch if (o_stream_pwrite(ctx->dbox_output, &dbox_msg_hdr,
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch dbox_file_set_syscall_error(file, "pwrite()");
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Boschstatic int dbox_save_finish_write(struct mail_save_context *_ctx)
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch /* we can't change ctime, but we can add the date to cache */
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch struct index_mail *mail = (struct index_mail *)_ctx->dest_mail;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch index_mail_cache_add(mail, MAIL_CACHE_SAVE_DATE, &t, sizeof(t));
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch index_mail_cache_parse_deinit(_ctx->dest_mail,
d6bbf85809664a810726b5c711c7213874d8df57Phil Carmody files = array_idx_modifiable(&ctx->files, array_count(&ctx->files) - 1);
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch if (dbox_save_mail_write_metadata(&ctx->ctx, *files) < 0)
87b4215acbf020aa5b8dea686b23fc664140cda0Stephan Bosch else if (dbox_file_append_commit(&ctx->append_ctx) < 0)
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Boschint sdbox_save_finish(struct mail_save_context *ctx)
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Boschvoid sdbox_save_cancel(struct mail_save_context *_ctx)
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch struct dbox_save_context *ctx = (struct dbox_save_context *)_ctx;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Boschstatic int dbox_save_assign_uids(struct sdbox_save_context *ctx,
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch unsigned int i, count, n = 0;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch for (i = 0; i < count; i++) {
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch struct sdbox_file *sfile = (struct sdbox_file *)files[i];
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch ret = seq_range_array_iter_nth(&iter, n++, &uid);
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch i_assert(!seq_range_array_iter_nth(&iter, n, &uid));
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Boschstatic void dbox_save_unref_files(struct sdbox_save_context *ctx)
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch struct mail_storage *storage = &ctx->mbox->storage->storage.storage;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch unsigned int i, count;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch files = array_get_modifiable(&ctx->files, &count);
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch for (i = 0; i < count; i++) {
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch "unlink(%s) failed: %m",
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmodyint sdbox_transaction_save_commit_pre(struct mail_save_context *_ctx)
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody struct mailbox_transaction_context *_t = _ctx->transaction;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody if (sdbox_sync_begin(ctx->mbox, SDBOX_SYNC_FLAG_FORCE |
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody /* assign UIDs for new messages */
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody hdr = mail_index_get_header(ctx->sync_ctx->sync_view);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody mail_index_append_finish_uids(ctx->ctx.trans, hdr->next_uid,
bee4935fb14a078052e4524070910fd0b8135ef7Phil Carmody if (dbox_save_assign_uids(ctx, &_t->changes->saved_uids) < 0) {
47ca7a853a116b1be3d73b46301d97e572f2f806Phil Carmodyvoid sdbox_transaction_save_commit_post(struct mail_save_context *_ctx,
47ca7a853a116b1be3d73b46301d97e572f2f806Phil Carmody struct mail_index_transaction_commit_result *result)
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody _ctx->transaction = NULL; /* transaction is already freed */
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody mail_index_sync_set_commit_result(ctx->sync_ctx->index_sync_ctx,
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody if (sdbox_sync_finish(&ctx->sync_ctx, TRUE) < 0)
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody if (!ctx->mbox->storage->storage.storage.set->fsync_disable) {
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmodyvoid sdbox_transaction_save_rollback(struct mail_save_context *_ctx)
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx;