summaryrefslogtreecommitdiffstats
path: root/src/lib-storage/index/mbox/mbox-sync-list-index.c
blob: 934b7ea5d4b1ec82f8decffee5722cefed7c5426 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "mbox-storage.h"
#include "mbox-sync-private.h"

static unsigned int
mbox_list_get_ext_id(struct mbox_mailbox *mbox,
		     struct mail_index_view *view)
{
	if (mbox->mbox_list_index_ext_id == (uint32_t)-1) {
		mbox->mbox_list_index_ext_id =
			mail_index_ext_register(mail_index_view_get_index(view),
				"mbox", 0,
				sizeof(struct mbox_list_index_record),
				sizeof(uint32_t));
	}
	return mbox->mbox_list_index_ext_id;
}

int mbox_list_index_has_changed(struct mailbox *box,
				struct mail_index_view *list_view,
				uint32_t seq, bool quick, const char **reason_r)
{
	struct mbox_mailbox *mbox = MBOX_MAILBOX(box);
	const struct mbox_list_index_record *rec;
	const void *data;
	const char *path;
	struct stat st;
	uint32_t ext_id;
	bool expunged;
	int ret;

	ret = index_storage_list_index_has_changed(box, list_view, seq,
						   quick, reason_r);
	if (ret != 0 || box->storage->set->mailbox_list_index_very_dirty_syncs)
		return ret;

	ext_id = mbox_list_get_ext_id(mbox, list_view);
	mail_index_lookup_ext(list_view, seq, ext_id, &data, &expunged);
	rec = data;

	if (rec == NULL) {
		*reason_r = "mbox record is missing";
		return 1;
	} else if (expunged) {
		*reason_r = "mbox record is expunged";
		return 1;
	} else if (rec->mtime == 0) {
		/* not synced */
		*reason_r = "mbox record mtime=0";
		return 1;
	}

	ret = mailbox_get_path_to(box, MAILBOX_LIST_PATH_TYPE_MAILBOX, &path);
	if (ret < 0)
		return ret;
	i_assert(ret > 0);

	if (stat(path, &st) < 0) {
		mailbox_set_critical(box, "stat(%s) failed: %m", path);
		return -1;
	}
	if ((time_t)rec->mtime != st.st_mtime) {
		*reason_r = t_strdup_printf(
			"mbox record mtime changed %u != %"PRIdTIME_T,
			rec->mtime, st.st_mtime);
		return 1;
	}
	uint32_t new_size = (uint32_t)(st.st_size & 0xffffffffU);
	if (rec->size != new_size) {
		*reason_r = t_strdup_printf("mbox record size changed %u != %u",
					    rec->size, new_size);
		return 1;
	}
	return 0;
}

void mbox_list_index_update_sync(struct mailbox *box,
				 struct mail_index_transaction *trans,
				 uint32_t seq)
{
	struct mbox_mailbox *mbox = MBOX_MAILBOX(box);
	struct mail_index_view *list_view;
	const struct mbox_index_header *mhdr = &mbox->mbox_hdr;
	const struct mbox_list_index_record *old_rec;
	struct mbox_list_index_record new_rec;
	const void *data;
	uint32_t ext_id;
	bool expunged;

	index_storage_list_index_update_sync(box, trans, seq);

	/* get the current record */
	list_view = mail_index_transaction_get_view(trans);
	ext_id = mbox_list_get_ext_id(mbox, list_view);
	mail_index_lookup_ext(list_view, seq, ext_id, &data, &expunged);
	if (expunged)
		return;
	old_rec = data;

	i_zero(&new_rec);
	new_rec.mtime = mhdr->sync_mtime;
	new_rec.size = mhdr->sync_size & 0xffffffffU;

	if (old_rec == NULL ||
	    memcmp(old_rec, &new_rec, sizeof(*old_rec)) != 0)
		mail_index_update_ext(trans, seq, ext_id, &new_rec, NULL);
}