From efe492c5c067df472280fec7e182e788d7731381 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Wed, 1 Apr 2020 22:19:52 +0300 Subject: [PATCH] lib-index: Don't compress cache file if there are cache transactions with changes The cache compression could have lost changes in the uncommitted transactions if they had already written some changes to the file. This seems to have happened sometimes at least with dsync. --- src/lib-index/mail-cache-private.h | 1 + src/lib-index/mail-cache-transaction.c | 12 ++++++++++++ src/lib-index/mail-index-sync.c | 3 ++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/lib-index/mail-cache-private.h b/src/lib-index/mail-cache-private.h index 93ff789e22..f7265b1ea3 100644 --- a/src/lib-index/mail-cache-private.h +++ b/src/lib-index/mail-cache-private.h @@ -251,6 +251,7 @@ const struct mail_cache_record * mail_cache_transaction_lookup_rec(struct mail_cache_transaction_ctx *ctx, unsigned int seq, unsigned int *trans_next_idx); +bool mail_cache_transactions_have_changes(struct mail_cache *cache); /* Return data from the specified position in the cache file. Returns 1 if successful, 0 if offset/size points outside the cache file, -1 if I/O diff --git a/src/lib-index/mail-cache-transaction.c b/src/lib-index/mail-cache-transaction.c index a978aee21c..93ded20427 100644 --- a/src/lib-index/mail-cache-transaction.c +++ b/src/lib-index/mail-cache-transaction.c @@ -178,6 +178,18 @@ void mail_cache_transaction_rollback(struct mail_cache_transaction_ctx **_ctx) i_free(ctx); } +bool mail_cache_transactions_have_changes(struct mail_cache *cache) +{ + struct mail_cache_view *view; + + for (view = cache->views; view != NULL; view = view->next) { + if (view->transaction != NULL && + view->transaction->changes) + return TRUE; + } + return FALSE; +} + static int mail_cache_transaction_compress(struct mail_cache_transaction_ctx *ctx) { diff --git a/src/lib-index/mail-index-sync.c b/src/lib-index/mail-index-sync.c index f5ce512133..544eae34d3 100644 --- a/src/lib-index/mail-index-sync.c +++ b/src/lib-index/mail-index-sync.c @@ -939,7 +939,8 @@ int mail_index_sync_commit(struct mail_index_sync_ctx **_ctx) /* The previously called expunged handlers will update cache's record_count and deleted_record_count. That also has a side effect of updating whether cache needs to be compressed. */ - if (ret == 0 && mail_cache_need_compress(index->cache)) { + if (ret == 0 && mail_cache_need_compress(index->cache) && + !mail_cache_transactions_have_changes(index->cache)) { if (mail_cache_compress(index->cache, index->cache->need_compress_file_seq) < 0) { /* can't really do anything if it fails */ -- 2.47.3