changeset 22037:7d09937dc4ff

lib-index: Add mail_cache_expunge_count It can be called to correctly handle cache counters when records are removed.
author Aki Tuomi <aki.tuomi@dovecot.fi>
date Tue, 09 May 2017 14:29:04 +0300
parents 8ae3f141b7a4
children 801fd8985a0b
files src/lib-index/mail-cache-private.h src/lib-index/mail-cache-sync-update.c
diffstat 2 files changed, 16 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-index/mail-cache-private.h	Tue May 02 12:18:40 2017 +0300
+++ b/src/lib-index/mail-cache-private.h	Tue May 09 14:29:04 2017 +0300
@@ -229,6 +229,7 @@
 void mail_cache_header_fields_get(struct mail_cache *cache, buffer_t *dest);
 int mail_cache_header_fields_get_next_offset(struct mail_cache *cache,
 					     uint32_t *offset_r);
+void mail_cache_expunge_count(struct mail_cache *cache, unsigned int count);
 
 uint32_t mail_cache_lookup_cur_offset(struct mail_index_view *view,
 				      uint32_t seq, uint32_t *reset_id_r);
--- a/src/lib-index/mail-cache-sync-update.c	Tue May 02 12:18:40 2017 +0300
+++ b/src/lib-index/mail-cache-sync-update.c	Tue May 09 14:29:04 2017 +0300
@@ -8,6 +8,19 @@
 	unsigned expunge_count;
 };
 
+void mail_cache_expunge_count(struct mail_cache *cache, unsigned int count)
+{
+	if (mail_cache_lock(cache) > 0) {
+		cache->hdr_copy.deleted_record_count += count;
+		if (cache->hdr_copy.record_count >= count)
+			cache->hdr_copy.record_count -= count;
+		else
+			 cache->hdr_copy.record_count = 0;
+		cache->hdr_modified = TRUE;
+		(void)mail_cache_unlock(cache);
+	}
+}
+
 static struct mail_cache_sync_context *mail_cache_handler_init(void **context)
 {
 	struct mail_cache_sync_context *ctx;
@@ -29,18 +42,8 @@
 	if (ctx == NULL)
 		return;
 
-	if (mail_cache_lock(cache) > 0) {
-		/* update the record counts in the cache file's header. these
-		   are used to figure out when a cache file should be
-		   recreated and the old data dropped. */
-		cache->hdr_copy.deleted_record_count += ctx->expunge_count;
-		if (cache->hdr_copy.record_count >= ctx->expunge_count)
-			cache->hdr_copy.record_count -= ctx->expunge_count;
-		else
-			cache->hdr_copy.record_count = 0;
-		cache->hdr_modified = TRUE;
-		(void)mail_cache_unlock(cache);
-	}
+	mail_cache_expunge_count(cache, ctx->expunge_count);
+
 	i_free(ctx);
 }