summaryrefslogtreecommitdiffstats
path: root/src/lib-index/mail-cache-sync-update.c
blob: 90731876f7586b8652ee7f6126f5f9e745078ab7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* Copyright (c) 2004-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "mail-cache-private.h"
#include "mail-index-sync-private.h"

struct mail_cache_sync_context {
	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_flush_and_unlock(cache);
	}
}

static struct mail_cache_sync_context *mail_cache_handler_init(void **context)
{
	struct mail_cache_sync_context *ctx;

	if (*context != NULL)
		ctx = *context;
	else {
		*context = i_new(struct mail_cache_sync_context, 1);
		ctx = *context;
	}
	return ctx;
}

static void mail_cache_handler_deinit(struct mail_index_sync_map_ctx *sync_ctx,
				      struct mail_cache_sync_context *ctx)
{
	struct mail_cache *cache = sync_ctx->view->index->cache;

	if (ctx == NULL)
		return;

	mail_cache_expunge_count(cache, ctx->expunge_count);

	i_free(ctx);
}

int mail_cache_expunge_handler(struct mail_index_sync_map_ctx *sync_ctx,
			       const void *data, void **sync_context)
{
	struct mail_cache_sync_context *ctx = *sync_context;
	const uint32_t *cache_offset = data;

	if (data == NULL) {
		mail_cache_handler_deinit(sync_ctx, ctx);
		*sync_context = NULL;
		return 0;
	}

	if (*cache_offset == 0)
		return 0;

	ctx = mail_cache_handler_init(sync_context);
	ctx->expunge_count++;
	return 0;
}