bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2004-2018 Dovecot authors, see the included COPYING file */
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen#include "lib.h"
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen#include "mail-cache-private.h"
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen#include "mail-index-sync-private.h"
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainenstruct mail_cache_sync_context {
2219015cc814b18a72d8d37696a5bffc52edd767Timo Sirainen unsigned expunge_count;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen};
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
38228c961f0e2c5fb8a0620a8ce620bb245dc88dAki Tuomivoid mail_cache_expunge_count(struct mail_cache *cache, unsigned int count)
38228c961f0e2c5fb8a0620a8ce620bb245dc88dAki Tuomi{
38228c961f0e2c5fb8a0620a8ce620bb245dc88dAki Tuomi if (mail_cache_lock(cache) > 0) {
38228c961f0e2c5fb8a0620a8ce620bb245dc88dAki Tuomi cache->hdr_copy.deleted_record_count += count;
38228c961f0e2c5fb8a0620a8ce620bb245dc88dAki Tuomi if (cache->hdr_copy.record_count >= count)
38228c961f0e2c5fb8a0620a8ce620bb245dc88dAki Tuomi cache->hdr_copy.record_count -= count;
38228c961f0e2c5fb8a0620a8ce620bb245dc88dAki Tuomi else
38228c961f0e2c5fb8a0620a8ce620bb245dc88dAki Tuomi cache->hdr_copy.record_count = 0;
38228c961f0e2c5fb8a0620a8ce620bb245dc88dAki Tuomi cache->hdr_modified = TRUE;
38228c961f0e2c5fb8a0620a8ce620bb245dc88dAki Tuomi (void)mail_cache_unlock(cache);
38228c961f0e2c5fb8a0620a8ce620bb245dc88dAki Tuomi }
38228c961f0e2c5fb8a0620a8ce620bb245dc88dAki Tuomi}
38228c961f0e2c5fb8a0620a8ce620bb245dc88dAki Tuomi
1b75126c39121b25f0bba048758b56d4b4b9bf1aTimo Sirainenstatic struct mail_cache_sync_context *mail_cache_handler_init(void **context)
1b75126c39121b25f0bba048758b56d4b4b9bf1aTimo Sirainen{
1b75126c39121b25f0bba048758b56d4b4b9bf1aTimo Sirainen struct mail_cache_sync_context *ctx;
1b75126c39121b25f0bba048758b56d4b4b9bf1aTimo Sirainen
1b75126c39121b25f0bba048758b56d4b4b9bf1aTimo Sirainen if (*context != NULL)
1b75126c39121b25f0bba048758b56d4b4b9bf1aTimo Sirainen ctx = *context;
1b75126c39121b25f0bba048758b56d4b4b9bf1aTimo Sirainen else {
1b75126c39121b25f0bba048758b56d4b4b9bf1aTimo Sirainen *context = i_new(struct mail_cache_sync_context, 1);
1b75126c39121b25f0bba048758b56d4b4b9bf1aTimo Sirainen ctx = *context;
1b75126c39121b25f0bba048758b56d4b4b9bf1aTimo Sirainen }
1b75126c39121b25f0bba048758b56d4b4b9bf1aTimo Sirainen return ctx;
1b75126c39121b25f0bba048758b56d4b4b9bf1aTimo Sirainen}
1b75126c39121b25f0bba048758b56d4b4b9bf1aTimo Sirainen
2219015cc814b18a72d8d37696a5bffc52edd767Timo Sirainenstatic void mail_cache_handler_deinit(struct mail_index_sync_map_ctx *sync_ctx,
2219015cc814b18a72d8d37696a5bffc52edd767Timo Sirainen struct mail_cache_sync_context *ctx)
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen{
2219015cc814b18a72d8d37696a5bffc52edd767Timo Sirainen struct mail_cache *cache = sync_ctx->view->index->cache;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
2219015cc814b18a72d8d37696a5bffc52edd767Timo Sirainen if (ctx == NULL)
2219015cc814b18a72d8d37696a5bffc52edd767Timo Sirainen return;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
38228c961f0e2c5fb8a0620a8ce620bb245dc88dAki Tuomi mail_cache_expunge_count(cache, ctx->expunge_count);
38228c961f0e2c5fb8a0620a8ce620bb245dc88dAki Tuomi
2219015cc814b18a72d8d37696a5bffc52edd767Timo Sirainen i_free(ctx);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen}
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainenint mail_cache_expunge_handler(struct mail_index_sync_map_ctx *sync_ctx,
43d32cbe60fdaef2699d99f1ca259053e9350411Timo Sirainen uint32_t seq ATTR_UNUSED, const void *data,
2219015cc814b18a72d8d37696a5bffc52edd767Timo Sirainen void **sync_context, void *context ATTR_UNUSED)
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen{
da985034a708db2f61394b30d117050ae6829ee5Timo Sirainen struct mail_cache_sync_context *ctx = *sync_context;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen const uint32_t *cache_offset = data;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen if (data == NULL) {
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen mail_cache_handler_deinit(sync_ctx, ctx);
da985034a708db2f61394b30d117050ae6829ee5Timo Sirainen *sync_context = NULL;
da985034a708db2f61394b30d117050ae6829ee5Timo Sirainen return 0;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen }
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen if (*cache_offset == 0)
da985034a708db2f61394b30d117050ae6829ee5Timo Sirainen return 0;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
5d1833b98fa85d8061626aa986f38dcbcd70553eTimo Sirainen ctx = mail_cache_handler_init(sync_context);
2219015cc814b18a72d8d37696a5bffc52edd767Timo Sirainen ctx->expunge_count++;
da985034a708db2f61394b30d117050ae6829ee5Timo Sirainen return 0;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen}