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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "index-storage.h"
#include "index-sync-changes.h"
struct index_sync_changes_context {
struct mail_index_sync_ctx *index_sync_ctx;
struct mail_index_view *sync_view;
struct mail_index_transaction *sync_trans;
ARRAY(struct mail_index_sync_rec) syncs;
struct mail_index_sync_rec sync_rec;
bool dirty_flag_updates;
};
struct index_sync_changes_context *
index_sync_changes_init(struct mail_index_sync_ctx *index_sync_ctx,
struct mail_index_view *sync_view,
struct mail_index_transaction *sync_trans,
bool dirty_flag_updates)
{
struct index_sync_changes_context *ctx;
ctx = i_new(struct index_sync_changes_context, 1);
ctx->index_sync_ctx = index_sync_ctx;
ctx->sync_view = sync_view;
ctx->sync_trans = sync_trans;
ctx->dirty_flag_updates = dirty_flag_updates;
i_array_init(&ctx->syncs, 16);
return ctx;
}
void index_sync_changes_deinit(struct index_sync_changes_context **_ctx)
{
struct index_sync_changes_context *ctx = *_ctx;
*_ctx = NULL;
array_free(&ctx->syncs);
i_free(ctx);
}
void index_sync_changes_reset(struct index_sync_changes_context *ctx)
{
array_clear(&ctx->syncs);
i_zero(&ctx->sync_rec);
}
void index_sync_changes_delete_to(struct index_sync_changes_context *ctx,
uint32_t last_uid)
{
struct mail_index_sync_rec *syncs;
unsigned int src, dest, count;
syncs = array_get_modifiable(&ctx->syncs, &count);
for (src = dest = 0; src < count; src++) {
i_assert(last_uid >= syncs[src].uid1);
if (last_uid <= syncs[src].uid2) {
/* keep it */
if (src != dest)
syncs[dest] = syncs[src];
dest++;
}
}
array_delete(&ctx->syncs, dest, count - dest);
}
static bool
index_sync_changes_have_expunges(struct index_sync_changes_context *ctx,
unsigned int count,
guid_128_t expunged_guid_128_r)
{
const struct mail_index_sync_rec *syncs;
unsigned int i;
syncs = array_front(&ctx->syncs);
for (i = 0; i < count; i++) {
if (syncs[i].type == MAIL_INDEX_SYNC_TYPE_EXPUNGE) {
memcpy(expunged_guid_128_r, syncs[i].guid_128,
GUID_128_SIZE);
return TRUE;
}
}
return FALSE;
}
void index_sync_changes_read(struct index_sync_changes_context *ctx,
uint32_t uid, bool *sync_expunge_r,
guid_128_t expunged_guid_128_r)
{
struct mail_index_sync_rec *sync_rec = &ctx->sync_rec;
uint32_t seq1, seq2;
unsigned int orig_count;
*sync_expunge_r = FALSE;
index_sync_changes_delete_to(ctx, uid);
orig_count = array_count(&ctx->syncs);
while (uid >= sync_rec->uid1) {
if (uid <= sync_rec->uid2) {
array_push_back(&ctx->syncs, sync_rec);
if (sync_rec->type == MAIL_INDEX_SYNC_TYPE_EXPUNGE) {
*sync_expunge_r = TRUE;
memcpy(expunged_guid_128_r, sync_rec->guid_128,
GUID_128_SIZE);
}
}
if (!mail_index_sync_next(ctx->index_sync_ctx, sync_rec)) {
i_zero(sync_rec);
break;
}
switch (sync_rec->type) {
case MAIL_INDEX_SYNC_TYPE_EXPUNGE:
break;
case MAIL_INDEX_SYNC_TYPE_FLAGS:
case MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD:
case MAIL_INDEX_SYNC_TYPE_KEYWORD_REMOVE:
if (!ctx->dirty_flag_updates)
break;
/* mark the changes as dirty */
(void)mail_index_lookup_seq_range(ctx->sync_view,
sync_rec->uid1,
sync_rec->uid2,
&seq1, &seq2);
i_zero(sync_rec);
if (seq1 == 0)
break;
mail_index_update_flags_range(ctx->sync_trans,
seq1, seq2, MODIFY_ADD,
(enum mail_flags)MAIL_INDEX_MAIL_FLAG_DIRTY);
break;
}
}
if (!*sync_expunge_r && orig_count > 0) {
*sync_expunge_r =
index_sync_changes_have_expunges(ctx, orig_count,
expunged_guid_128_r);
}
}
bool index_sync_changes_have(struct index_sync_changes_context *ctx)
{
return array_count(&ctx->syncs) > 0;
}
uint32_t
index_sync_changes_get_next_uid(struct index_sync_changes_context *ctx)
{
return ctx->sync_rec.uid1;
}
void index_sync_changes_apply(struct index_sync_changes_context *ctx,
pool_t pool, uint8_t *flags,
ARRAY_TYPE(keyword_indexes) *keywords,
enum mail_index_sync_type *sync_type_r)
{
const struct mail_index_sync_rec *syncs;
unsigned int i, count;
enum mail_index_sync_type sync_type = 0;
syncs = array_get(&ctx->syncs, &count);
for (i = 0; i < count; i++) {
switch (syncs[i].type) {
case MAIL_INDEX_SYNC_TYPE_FLAGS:
mail_index_sync_flags_apply(&syncs[i], flags);
sync_type |= MAIL_INDEX_SYNC_TYPE_FLAGS;
break;
case MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD:
case MAIL_INDEX_SYNC_TYPE_KEYWORD_REMOVE:
if (!array_is_created(keywords)) {
/* no existing keywords */
if (syncs[i].type !=
MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD)
break;
/* adding, create the array */
p_array_init(keywords, pool,
I_MIN(10, count - i));
}
if (mail_index_sync_keywords_apply(&syncs[i], keywords))
sync_type |= syncs[i].type;
break;
default:
break;
}
}
*sync_type_r = sync_type;
}
|