bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2003-2018 Dovecot authors, see the included COPYING file */
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainenstatic void index_transaction_free(struct mailbox_transaction_context *t)
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainenindex_transaction_index_commit(struct mail_index_transaction *index_trans,
9f19a50d5966643c4d1c5ca06868ac2ad31bc4d5Timo Sirainen struct mail_index_transaction_commit_result *result_r)
e2d268e9531227ead6a98466ecf3c046c857ef70Timo Sirainen struct index_mailbox_sync_pvt_context *pvt_sync_ctx = NULL;
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen if (dict_transaction_commit(&t->attr_pvt_trans, &error) < 0) {
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen "Dict private transaction commit failed: %s", error);
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen if (dict_transaction_commit(&t->attr_shared_trans, &error) < 0) {
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen "Dict shared transaction commit failed: %s", error);
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen t->box->v.transaction_save_rollback(t->save_ctx);
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen } else if (t->box->v.transaction_save_commit_pre(t->save_ctx) < 0) {
e2d268e9531227ead6a98466ecf3c046c857ef70Timo Sirainen if (index_mailbox_sync_pvt_init(t->box, TRUE, &pvt_sync_ctx) < 0)
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainen if (t->super.commit(index_trans, result_r) < 0) {
8828d7a1e76b7d301bcd7385aa27c52512e855ceTimo Sirainen t->changes->changes_mask = result_r->changes_mask;
1460ef7a18c53216ddb4a94bb62fba96076aae8eTimo Sirainen t->box->v.transaction_save_commit_post(t->save_ctx, result_r);
e2d268e9531227ead6a98466ecf3c046c857ef70Timo Sirainen if (index_mailbox_sync_pvt_newmails(pvt_sync_ctx, t) < 0) {
e2d268e9531227ead6a98466ecf3c046c857ef70Timo Sirainen /* failed to add private flags. a bit too late to
e2d268e9531227ead6a98466ecf3c046c857ef70Timo Sirainen return failure though, so just ignore silently */
103b44c67f38862d1e72c9bfdf11f067b4670675Timo Sirainen mail_index_set_error_nolog(t->box->index, mailbox_get_last_error(t->box, NULL));
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainenindex_transaction_index_rollback(struct mail_index_transaction *index_trans)
acfeae4e2b5c2acdab88a1377ff6cd4a9b5f0a3aTimo Sirainen dict_transaction_rollback(&t->attr_pvt_trans);
acfeae4e2b5c2acdab88a1377ff6cd4a9b5f0a3aTimo Sirainen dict_transaction_rollback(&t->attr_shared_trans);
1460ef7a18c53216ddb4a94bb62fba96076aae8eTimo Sirainen t->box->v.transaction_save_rollback(t->save_ctx);
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenindex_transaction_flags_get(enum mailbox_transaction_flags flags)
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen enum mail_index_transaction_flags itrans_flags;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen itrans_flags = MAIL_INDEX_TRANSACTION_FLAG_AVOID_FLAG_UPDATES;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen if ((flags & MAILBOX_TRANSACTION_FLAG_HIDE) != 0)
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen itrans_flags |= MAIL_INDEX_TRANSACTION_FLAG_HIDE;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen if ((flags & MAILBOX_TRANSACTION_FLAG_EXTERNAL) != 0)
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen itrans_flags |= MAIL_INDEX_TRANSACTION_FLAG_EXTERNAL;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen if ((flags & MAILBOX_TRANSACTION_FLAG_SYNC) != 0)
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen itrans_flags |= MAIL_INDEX_TRANSACTION_FLAG_SYNC;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenvoid index_transaction_init_pvt(struct mailbox_transaction_context *t)
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen enum mail_index_transaction_flags itrans_flags;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen if (t->box->view_pvt == NULL || t->itrans_pvt != NULL)
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen itrans_flags = index_transaction_flags_get(t->flags);
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen t->itrans_pvt = mail_index_transaction_begin(t->box->view_pvt,
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen t->view_pvt = mail_index_transaction_open_updated_view(t->itrans_pvt);
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainenvoid index_transaction_init(struct mailbox_transaction_context *t,
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen enum mail_index_transaction_flags itrans_flags;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen itrans_flags = index_transaction_flags_get(flags);
9293bf90039454f47e94e4ba3722a775cfa7d25cTimo Sirainen if ((flags & MAILBOX_TRANSACTION_FLAG_REFRESH) != 0)
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen t->itrans = mail_index_transaction_begin(box->view, itrans_flags);
cd83124e5d070a016c590bb0b1096d7828c7b6adTimo Sirainen t->view = mail_index_transaction_open_updated_view(t->itrans);
cd83124e5d070a016c590bb0b1096d7828c7b6adTimo Sirainen array_create(&t->module_contexts, default_pool,
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen sizeof(void *), 5);
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainen t->cache_view = mail_cache_view_open(box->cache, t->view);
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainen t->cache_trans = mail_cache_get_transaction(t->cache_view, t->itrans);
ab9a91eb05a54f7675e0bf861aca53f417e1980dTimo Sirainen if ((flags & MAILBOX_TRANSACTION_FLAG_NO_CACHE_DEC) != 0)
ab9a91eb05a54f7675e0bf861aca53f417e1980dTimo Sirainen mail_cache_view_update_cache_decisions(t->cache_view, FALSE);
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen /* set up after mail_cache_get_transaction(), so that we'll still
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen have the cache_trans available in _index_commit() */
cd83124e5d070a016c590bb0b1096d7828c7b6adTimo Sirainen t->itrans->v.commit = index_transaction_index_commit;
cd83124e5d070a016c590bb0b1096d7828c7b6adTimo Sirainen t->itrans->v.rollback = index_transaction_index_rollback;
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainen MODULE_CONTEXT_SET(t->itrans, mail_storage_mail_index_module, t);
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainen t = i_new(struct mailbox_transaction_context, 1);
c6ce2e251ac75fa650c7fbfa52150eae69386293Martti Rannanjärvi index_transaction_init(t, box, flags, reason);
cd83124e5d070a016c590bb0b1096d7828c7b6adTimo Sirainenint index_transaction_commit(struct mailbox_transaction_context *t,
96308127e006bb3b1108093bcf4cc1fd9481cb7aTimo Sirainen struct mail_transaction_commit_changes *changes_r)
cd83124e5d070a016c590bb0b1096d7828c7b6adTimo Sirainen struct mail_index_transaction *itrans = t->itrans;
9f19a50d5966643c4d1c5ca06868ac2ad31bc4d5Timo Sirainen struct mail_index_transaction_commit_result result;
6d931bbce16786df431e9ae8201a78a95084316dTimo Sirainen changes_r->pool = pool_alloconly_create(MEMPOOL_GROWING
96308127e006bb3b1108093bcf4cc1fd9481cb7aTimo Sirainen p_array_init(&changes_r->saved_uids, changes_r->pool, 32);
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen ret = mail_index_transaction_commit(&t->itrans_pvt);
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen if (mail_index_transaction_commit_full(&itrans, &result) < 0)
61b0637759146621cbb7edcbd0b03a71cfd66dfeTimo Sirainen if (ret < 0 && mail_index_is_deleted(box->index))
9f19a50d5966643c4d1c5ca06868ac2ad31bc4d5Timo Sirainen changes_r->ignored_modseq_changes = result.ignored_modseq_changes;
cd83124e5d070a016c590bb0b1096d7828c7b6adTimo Sirainenvoid index_transaction_rollback(struct mailbox_transaction_context *t)
cd83124e5d070a016c590bb0b1096d7828c7b6adTimo Sirainen struct mail_index_transaction *itrans = t->itrans;