From: Timo Sirainen Date: Fri, 27 Mar 2020 09:33:23 +0000 (+0200) Subject: lib-index: mail_cache_decisions_copy() - Create transaction internally X-Git-Tag: 2.3.11.2~419 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0625730eb652c43e802f8be47fc0208d97db9301;p=thirdparty%2Fdovecot%2Fcore.git lib-index: mail_cache_decisions_copy() - Create transaction internally --- diff --git a/src/lib-index/mail-cache-decisions.c b/src/lib-index/mail-cache-decisions.c index 953172ec9e..496922fea0 100644 --- a/src/lib-index/mail-cache-decisions.c +++ b/src/lib-index/mail-cache-decisions.c @@ -152,15 +152,19 @@ void mail_cache_decision_add(struct mail_cache_view *view, uint32_t seq, cache->fields[field].uid_highwater = uid; } -void mail_cache_decisions_copy(struct mail_index_transaction *itrans, - struct mail_cache *src, - struct mail_cache *dst) +int mail_cache_decisions_copy(struct mail_cache *src, struct mail_cache *dst) { struct mail_cache_compress_lock *lock = NULL; - if (mail_cache_open_and_verify(src) < 0 || - MAIL_CACHE_IS_UNUSABLE(src)) - return; + if (mail_cache_open_and_verify(src) < 0) + return -1; + if (MAIL_CACHE_IS_UNUSABLE(src)) + return 0; /* no caching decisions */ + + struct mail_index_view *dest_view = mail_index_view_open(dst->index); + struct mail_index_transaction *itrans = + mail_index_transaction_begin(dest_view, + MAIL_INDEX_TRANSACTION_FLAG_EXTERNAL); unsigned int count = 0; struct mail_cache_field *fields = @@ -169,8 +173,16 @@ void mail_cache_decisions_copy(struct mail_index_transaction *itrans, if (count > 0) mail_cache_register_fields(dst, fields, count); + /* Destination cache isn't expected to exist yet, so use compression + to create it. Setting field_header_write_pending also guarantees + that the fields are updated even if the cache was already created + and no compression was done. */ dst->field_header_write_pending = TRUE; - (void)mail_cache_compress(dst, itrans, &lock); + int ret = mail_cache_compress(dst, itrans, &lock); if (lock != NULL) mail_cache_compress_unlock(&lock); + if (mail_index_transaction_commit(&itrans) < 0) + ret = -1; + mail_index_view_close(&dest_view); + return ret; } diff --git a/src/lib-index/mail-cache.h b/src/lib-index/mail-cache.h index 1f51d989ae..f90735ab67 100644 --- a/src/lib-index/mail-cache.h +++ b/src/lib-index/mail-cache.h @@ -93,10 +93,9 @@ void mail_cache_view_close(struct mail_cache_view **view); void mail_cache_view_update_cache_decisions(struct mail_cache_view *view, bool update); -/* Copy caching decisions */ -void mail_cache_decisions_copy(struct mail_index_transaction *itrans, - struct mail_cache *src, - struct mail_cache *dst); +/* Copy caching decisions. This is expected to be called only for a newly + created empty mailbox. */ +int mail_cache_decisions_copy(struct mail_cache *src, struct mail_cache *dst); /* Get index transaction specific cache transaction. */ struct mail_cache_transaction_ctx * diff --git a/src/lib-storage/mail-storage.c b/src/lib-storage/mail-storage.c index 980d88abef..da49488d9b 100644 --- a/src/lib-storage/mail-storage.c +++ b/src/lib-storage/mail-storage.c @@ -1684,14 +1684,8 @@ static void mailbox_copy_cache_decisions_from_inbox(struct mailbox *box) existence != MAILBOX_EXISTENCE_NONE && mailbox_open(inbox) == 0 && mailbox_open(box) == 0) { - struct mail_index_transaction *dit = - mail_index_transaction_begin(box->view, - MAIL_INDEX_TRANSACTION_FLAG_EXTERNAL); - - mail_cache_decisions_copy(dit, inbox->cache, box->cache); - /* we can't do much about errors here */ - (void)mail_index_transaction_commit(&dit); + (void)mail_cache_decisions_copy(inbox->cache, box->cache); } mailbox_free(&inbox);