/src/dovecot/src/lib-index/mail-index-view-sync.c
Line | Count | Source |
1 | | /* Copyright (c) Dovecot authors, see top-level COPYING file */ |
2 | | |
3 | | #include "lib.h" |
4 | | #include "array.h" |
5 | | #include "buffer.h" |
6 | | #include "mail-index-view-private.h" |
7 | | #include "mail-index-sync-private.h" |
8 | | #include "mail-index-modseq.h" |
9 | | #include "mail-transaction-log.h" |
10 | | |
11 | | |
12 | | struct mail_index_view_sync_ctx { |
13 | | struct mail_index_view *view; |
14 | | enum mail_index_view_sync_flags flags; |
15 | | struct mail_index_sync_map_ctx sync_map_ctx; |
16 | | |
17 | | /* After syncing view, map is replaced with sync_new_map. */ |
18 | | struct mail_index_map *sync_new_map; |
19 | | |
20 | | ARRAY_TYPE(seq_range) expunges; |
21 | | unsigned int finish_min_msg_count; |
22 | | |
23 | | const struct mail_transaction_header *hdr; |
24 | | const void *data; |
25 | | |
26 | | /* temporary variables while handling lost transaction logs: */ |
27 | | ARRAY_TYPE(keyword_indexes) lost_old_kw, lost_new_kw; |
28 | | buffer_t *lost_kw_buf; |
29 | | uint32_t lost_new_ext_idx; |
30 | | /* result of lost transaction logs: */ |
31 | | ARRAY_TYPE(seq_range) lost_flags; |
32 | | unsigned int lost_flag_idx; |
33 | | |
34 | | size_t data_offset; |
35 | | bool failed:1; |
36 | | bool sync_map_update:1; |
37 | | bool skipped_expunges:1; |
38 | | bool last_read:1; |
39 | | bool log_was_lost:1; |
40 | | bool hidden:1; |
41 | | }; |
42 | | |
43 | | static int |
44 | | view_sync_set_log_view_range(struct mail_index_view *view, bool sync_expunges, |
45 | | bool *reset_r, bool *partial_sync_r, |
46 | | const char **error_r) |
47 | 0 | { |
48 | 0 | const struct mail_index_header *hdr = &view->index->map->hdr; |
49 | 0 | uint32_t start_seq, end_seq; |
50 | 0 | uoff_t start_offset, end_offset; |
51 | 0 | const char *reason; |
52 | 0 | int ret; |
53 | |
|
54 | 0 | *partial_sync_r = FALSE; |
55 | |
|
56 | 0 | if (sync_expunges) { |
57 | | /* Sync everything after the last expunge syncing position. |
58 | | We'll just skip over the non-expunge transaction records |
59 | | that have already been synced previously. */ |
60 | 0 | start_seq = view->log_file_expunge_seq; |
61 | 0 | start_offset = view->log_file_expunge_offset; |
62 | 0 | } else { |
63 | | /* Sync only new changes since the last view sync. */ |
64 | 0 | start_seq = view->log_file_head_seq; |
65 | 0 | start_offset = view->log_file_head_offset; |
66 | 0 | } |
67 | | /* Sync the view up to the (already refreshed) index map. */ |
68 | 0 | end_seq = hdr->log_file_seq; |
69 | 0 | end_offset = hdr->log_file_head_offset; |
70 | |
|
71 | 0 | if (end_seq < view->log_file_head_seq || |
72 | 0 | (end_seq == view->log_file_head_seq && |
73 | 0 | end_offset < view->log_file_head_offset)) { |
74 | 0 | *error_r = t_strdup_printf( |
75 | 0 | "%s log position went backwards " |
76 | 0 | "(%u,%"PRIuUOFF_T" < %u,%"PRIuUOFF_T")", |
77 | 0 | view->index->filepath, end_seq, end_offset, |
78 | 0 | view->log_file_head_seq, view->log_file_head_offset); |
79 | 0 | return -1; |
80 | 0 | } |
81 | | |
82 | 0 | for (;;) { |
83 | | /* the view begins from the first non-synced transaction */ |
84 | 0 | ret = mail_transaction_log_view_set(view->log_view, |
85 | 0 | start_seq, start_offset, |
86 | 0 | end_seq, end_offset, |
87 | 0 | reset_r, &reason); |
88 | 0 | if (ret <= 0) { |
89 | 0 | *error_r = t_strdup_printf( |
90 | 0 | "Failed to map view for %s: %s", |
91 | 0 | view->index->filepath, reason); |
92 | 0 | return ret; |
93 | 0 | } |
94 | | |
95 | 0 | if (!*reset_r || sync_expunges) |
96 | 0 | break; |
97 | | |
98 | | /* log was reset, but we don't want to sync expunges. |
99 | | we can't do this, so sync only up to the reset. */ |
100 | 0 | mail_transaction_log_view_get_prev_pos(view->log_view, |
101 | 0 | &end_seq, &end_offset); |
102 | 0 | end_seq--; end_offset = UOFF_T_MAX; |
103 | 0 | if (end_seq < start_seq) { |
104 | | /* we have only this reset log */ |
105 | 0 | mail_transaction_log_view_clear(view->log_view, |
106 | 0 | view->log_file_expunge_seq); |
107 | 0 | break; |
108 | 0 | } |
109 | 0 | *partial_sync_r = TRUE; |
110 | 0 | } |
111 | 0 | return 1; |
112 | 0 | } |
113 | | |
114 | | static unsigned int |
115 | | view_sync_expunges2seqs(struct mail_index_view_sync_ctx *ctx) |
116 | 0 | { |
117 | 0 | struct mail_index_view *view = ctx->view; |
118 | 0 | struct seq_range *src, *src_end, *dest; |
119 | 0 | unsigned int count, expunge_count = 0; |
120 | 0 | uint32_t prev_seq = 0; |
121 | | |
122 | | /* convert UIDs to sequences */ |
123 | 0 | src = dest = array_get_modifiable(&ctx->expunges, &count); |
124 | 0 | src_end = src + count; |
125 | 0 | for (; src != src_end; src++) { |
126 | 0 | if (!mail_index_lookup_seq_range(view, src->seq1, src->seq2, |
127 | 0 | &dest->seq1, &dest->seq2)) |
128 | 0 | count--; |
129 | 0 | else { |
130 | 0 | i_assert(dest->seq1 > prev_seq); |
131 | 0 | prev_seq = dest->seq2; |
132 | |
|
133 | 0 | expunge_count += dest->seq2 - dest->seq1 + 1; |
134 | 0 | dest++; |
135 | 0 | } |
136 | 0 | } |
137 | 0 | array_delete(&ctx->expunges, count, |
138 | 0 | array_count(&ctx->expunges) - count); |
139 | 0 | return expunge_count; |
140 | 0 | } |
141 | | |
142 | | static void |
143 | | view_sync_add_expunge_range(ARRAY_TYPE(seq_range) *dest, |
144 | | const struct seq_range *src, size_t src_size) |
145 | 0 | { |
146 | 0 | unsigned int i, src_count; |
147 | |
|
148 | 0 | i_assert(src_size % sizeof(*src) == 0); |
149 | | |
150 | 0 | src_count = src_size / sizeof(*src); |
151 | 0 | for (i = 0; i < src_count; i++) |
152 | 0 | seq_range_array_add_range(dest, src[i].seq1, src[i].seq2); |
153 | 0 | } |
154 | | |
155 | | static void |
156 | | view_sync_add_expunge_guids(ARRAY_TYPE(seq_range) *dest, |
157 | | const struct mail_transaction_expunge_guid *src, |
158 | | size_t src_size) |
159 | 0 | { |
160 | 0 | unsigned int i, src_count; |
161 | |
|
162 | 0 | i_assert(src_size % sizeof(*src) == 0); |
163 | | |
164 | 0 | src_count = src_size / sizeof(*src); |
165 | 0 | for (i = 0; i < src_count; i++) |
166 | 0 | seq_range_array_add(dest, src[i].uid); |
167 | 0 | } |
168 | | |
169 | | static int |
170 | | view_sync_get_expunges(struct mail_index_view_sync_ctx *ctx, |
171 | | unsigned int *expunge_count_r) |
172 | 0 | { |
173 | 0 | struct mail_index_view *view = ctx->view; |
174 | 0 | const struct mail_transaction_header *hdr; |
175 | 0 | const void *data; |
176 | 0 | int ret; |
177 | | |
178 | | /* get a list of expunge transactions. there may be some that we have |
179 | | already synced, but it doesn't matter because they'll get dropped |
180 | | out when converting to sequences. the uid ranges' validity has |
181 | | already been verified, so we can use them directly. */ |
182 | 0 | mail_transaction_log_view_mark(view->log_view); |
183 | 0 | while ((ret = mail_transaction_log_view_next(view->log_view, |
184 | 0 | &hdr, &data)) > 0) { |
185 | 0 | if ((hdr->type & MAIL_TRANSACTION_EXTERNAL) == 0) { |
186 | | /* skip expunge requests */ |
187 | 0 | continue; |
188 | 0 | } |
189 | 0 | if ((hdr->type & MAIL_TRANSACTION_EXPUNGE_GUID) != 0) { |
190 | 0 | view_sync_add_expunge_guids(&ctx->expunges, |
191 | 0 | data, hdr->size); |
192 | 0 | } else if ((hdr->type & MAIL_TRANSACTION_EXPUNGE) != 0) { |
193 | 0 | view_sync_add_expunge_range(&ctx->expunges, |
194 | 0 | data, hdr->size); |
195 | 0 | } |
196 | 0 | } |
197 | 0 | mail_transaction_log_view_rewind(view->log_view); |
198 | |
|
199 | 0 | *expunge_count_r = view_sync_expunges2seqs(ctx); |
200 | 0 | return ret; |
201 | 0 | } |
202 | | |
203 | | static bool have_existing_expunges(struct mail_index_view *view, |
204 | | const struct seq_range *range, size_t size) |
205 | 0 | { |
206 | 0 | const struct seq_range *range_end; |
207 | 0 | uint32_t seq1, seq2; |
208 | |
|
209 | 0 | range_end = CONST_PTR_OFFSET(range, size); |
210 | 0 | for (; range != range_end; range++) { |
211 | 0 | if (mail_index_lookup_seq_range(view, range->seq1, range->seq2, |
212 | 0 | &seq1, &seq2)) |
213 | 0 | return TRUE; |
214 | 0 | } |
215 | 0 | return FALSE; |
216 | 0 | } |
217 | | |
218 | | static bool |
219 | | have_existing_guid_expunge(struct mail_index_view *view, |
220 | | const struct mail_transaction_expunge_guid *expunges, |
221 | | size_t size) |
222 | 0 | { |
223 | 0 | const struct mail_transaction_expunge_guid *expunges_end; |
224 | 0 | uint32_t seq; |
225 | |
|
226 | 0 | expunges_end = CONST_PTR_OFFSET(expunges, size); |
227 | 0 | for (; expunges != expunges_end; expunges++) { |
228 | 0 | if (mail_index_lookup_seq(view, expunges->uid, &seq)) |
229 | 0 | return TRUE; |
230 | 0 | } |
231 | 0 | return FALSE; |
232 | 0 | } |
233 | | |
234 | | static bool view_sync_have_expunges(struct mail_index_view *view) |
235 | 0 | { |
236 | 0 | const struct mail_transaction_header *hdr; |
237 | 0 | const void *data; |
238 | 0 | bool have_expunges = FALSE; |
239 | 0 | int ret; |
240 | |
|
241 | 0 | if (mail_transaction_log_view_is_last(view->log_view)) |
242 | 0 | return FALSE; |
243 | | |
244 | 0 | mail_transaction_log_view_mark(view->log_view); |
245 | |
|
246 | 0 | while ((ret = mail_transaction_log_view_next(view->log_view, |
247 | 0 | &hdr, &data)) > 0) { |
248 | 0 | if ((hdr->type & MAIL_TRANSACTION_EXTERNAL) == 0) { |
249 | | /* skip expunge requests */ |
250 | 0 | continue; |
251 | 0 | } |
252 | 0 | if ((hdr->type & MAIL_TRANSACTION_EXPUNGE_GUID) != 0) { |
253 | | /* we have an expunge. see if it still exists. */ |
254 | 0 | if (have_existing_guid_expunge(view, data, hdr->size)) { |
255 | 0 | have_expunges = TRUE; |
256 | 0 | break; |
257 | 0 | } |
258 | 0 | } else if ((hdr->type & MAIL_TRANSACTION_EXPUNGE) != 0) { |
259 | | /* we have an expunge. see if it still exists. */ |
260 | 0 | if (have_existing_expunges(view, data, hdr->size)) { |
261 | 0 | have_expunges = TRUE; |
262 | 0 | break; |
263 | 0 | } |
264 | 0 | } |
265 | 0 | } |
266 | |
|
267 | 0 | mail_transaction_log_view_rewind(view->log_view); |
268 | | |
269 | | /* handle failures as having expunges (which is safer). |
270 | | we'll probably fail later. */ |
271 | 0 | return ret < 0 || have_expunges; |
272 | 0 | } |
273 | | |
274 | | static int uint_cmp(const void *p1, const void *p2) |
275 | 0 | { |
276 | 0 | const unsigned int *u1 = p1, *u2 = p2; |
277 | |
|
278 | 0 | if (*u1 < *u2) |
279 | 0 | return -1; |
280 | 0 | if (*u1 > *u2) |
281 | 0 | return 1; |
282 | 0 | return 0; |
283 | 0 | } |
284 | | |
285 | | static bool view_sync_lost_keywords_equal(struct mail_index_view_sync_ctx *ctx) |
286 | 0 | { |
287 | 0 | unsigned int *old_idx, *new_idx; |
288 | 0 | unsigned int old_count, new_count; |
289 | |
|
290 | 0 | old_idx = array_get_modifiable(&ctx->lost_old_kw, &old_count); |
291 | 0 | new_idx = array_get_modifiable(&ctx->lost_new_kw, &new_count); |
292 | 0 | if (old_count != new_count) |
293 | 0 | return FALSE; |
294 | | |
295 | 0 | qsort(old_idx, old_count, sizeof(*old_idx), uint_cmp); |
296 | 0 | qsort(new_idx, new_count, sizeof(*new_idx), uint_cmp); |
297 | 0 | return memcmp(old_idx, new_idx, old_count * sizeof(*old_idx)) == 0; |
298 | 0 | } |
299 | | |
300 | | static int view_sync_update_keywords(struct mail_index_view_sync_ctx *ctx, |
301 | | uint32_t uid) |
302 | 0 | { |
303 | 0 | struct mail_transaction_header thdr; |
304 | 0 | struct mail_transaction_keyword_update kw_up; |
305 | 0 | const unsigned int *kw_idx; |
306 | 0 | const char *const *kw_names; |
307 | 0 | unsigned int i, count; |
308 | |
|
309 | 0 | kw_idx = array_get(&ctx->lost_new_kw, &count); |
310 | 0 | if (count == 0) |
311 | 0 | return 0; |
312 | 0 | kw_names = array_front(&ctx->view->index->keywords); |
313 | |
|
314 | 0 | i_zero(&thdr); |
315 | 0 | thdr.type = MAIL_TRANSACTION_KEYWORD_UPDATE | MAIL_TRANSACTION_EXTERNAL; |
316 | 0 | i_zero(&kw_up); |
317 | 0 | kw_up.modify_type = MODIFY_ADD; |
318 | | /* add new flags one by one */ |
319 | 0 | for (i = 0; i < count; i++) { |
320 | 0 | kw_up.name_size = strlen(kw_names[kw_idx[i]]); |
321 | 0 | buffer_set_used_size(ctx->lost_kw_buf, 0); |
322 | 0 | buffer_append(ctx->lost_kw_buf, &kw_up, sizeof(kw_up)); |
323 | 0 | buffer_append(ctx->lost_kw_buf, kw_names[kw_idx[i]], |
324 | 0 | kw_up.name_size); |
325 | 0 | if (ctx->lost_kw_buf->used % 4 != 0) { |
326 | 0 | buffer_append_zero(ctx->lost_kw_buf, |
327 | 0 | 4 - ctx->lost_kw_buf->used % 4); |
328 | 0 | } |
329 | 0 | buffer_append(ctx->lost_kw_buf, &uid, sizeof(uid)); |
330 | 0 | buffer_append(ctx->lost_kw_buf, &uid, sizeof(uid)); |
331 | |
|
332 | 0 | thdr.size = ctx->lost_kw_buf->used; |
333 | 0 | if (mail_index_sync_record(&ctx->sync_map_ctx, &thdr, |
334 | 0 | ctx->lost_kw_buf->data) < 0) |
335 | 0 | return -1; |
336 | 0 | } |
337 | 0 | return 0; |
338 | 0 | } |
339 | | |
340 | | static int view_sync_apply_lost_changes(struct mail_index_view_sync_ctx *ctx, |
341 | | uint32_t old_seq, uint32_t new_seq) |
342 | 0 | { |
343 | 0 | struct mail_index_map *old_map = ctx->view->map; |
344 | 0 | struct mail_index_map *new_map = ctx->view->index->map; |
345 | 0 | const struct mail_index_record *old_rec, *new_rec; |
346 | 0 | struct mail_transaction_header thdr; |
347 | 0 | const struct mail_index_ext *ext; |
348 | 0 | const uint64_t *modseqp; |
349 | 0 | uint64_t new_modseq; |
350 | 0 | bool changed = FALSE; |
351 | |
|
352 | 0 | old_rec = MAIL_INDEX_REC_AT_SEQ(old_map, old_seq); |
353 | 0 | new_rec = MAIL_INDEX_REC_AT_SEQ(new_map, new_seq); |
354 | |
|
355 | 0 | i_zero(&thdr); |
356 | 0 | if (old_rec->flags != new_rec->flags) { |
357 | 0 | struct mail_transaction_flag_update flag_update; |
358 | | |
359 | | /* check this before syncing the record, since it updates |
360 | | old_rec. */ |
361 | 0 | if ((old_rec->flags & MAIL_INDEX_FLAGS_MASK) != |
362 | 0 | (new_rec->flags & MAIL_INDEX_FLAGS_MASK)) |
363 | 0 | changed = TRUE; |
364 | |
|
365 | 0 | thdr.type = MAIL_TRANSACTION_FLAG_UPDATE | |
366 | 0 | MAIL_TRANSACTION_EXTERNAL; |
367 | 0 | thdr.size = sizeof(flag_update); |
368 | |
|
369 | 0 | i_zero(&flag_update); |
370 | 0 | flag_update.uid1 = flag_update.uid2 = new_rec->uid; |
371 | 0 | flag_update.add_flags = new_rec->flags; |
372 | 0 | flag_update.remove_flags = ~new_rec->flags & 0xff; |
373 | 0 | if (mail_index_sync_record(&ctx->sync_map_ctx, &thdr, |
374 | 0 | &flag_update) < 0) |
375 | 0 | return -1; |
376 | 0 | } |
377 | | |
378 | 0 | mail_index_map_lookup_keywords(old_map, old_seq, &ctx->lost_old_kw); |
379 | 0 | mail_index_map_lookup_keywords(new_map, new_seq, &ctx->lost_new_kw); |
380 | 0 | if (!view_sync_lost_keywords_equal(ctx)) { |
381 | 0 | struct mail_transaction_keyword_reset kw_reset; |
382 | |
|
383 | 0 | thdr.type = MAIL_TRANSACTION_KEYWORD_RESET | |
384 | 0 | MAIL_TRANSACTION_EXTERNAL; |
385 | 0 | thdr.size = sizeof(kw_reset); |
386 | | |
387 | | /* remove all old flags by resetting them */ |
388 | 0 | i_zero(&kw_reset); |
389 | 0 | kw_reset.uid1 = kw_reset.uid2 = new_rec->uid; |
390 | 0 | if (mail_index_sync_record(&ctx->sync_map_ctx, &thdr, |
391 | 0 | &kw_reset) < 0) |
392 | 0 | return -1; |
393 | | |
394 | 0 | if (view_sync_update_keywords(ctx, new_rec->uid) < 0) |
395 | 0 | return -1; |
396 | 0 | changed = TRUE; |
397 | 0 | } |
398 | | |
399 | 0 | if (changed) { |
400 | | /* flags or keywords changed */ |
401 | 0 | } else if (ctx->view->highest_modseq != 0 && |
402 | 0 | ctx->lost_new_ext_idx != (uint32_t)-1) { |
403 | | /* if modseq has changed include this message in changed flags |
404 | | list, even if we didn't see any changes above. */ |
405 | 0 | ext = array_idx(&new_map->extensions, ctx->lost_new_ext_idx); |
406 | 0 | modseqp = CONST_PTR_OFFSET(new_rec, ext->record_offset); |
407 | 0 | new_modseq = *modseqp; |
408 | |
|
409 | 0 | if (new_modseq > ctx->view->highest_modseq) |
410 | 0 | changed = TRUE; |
411 | 0 | } |
412 | | |
413 | | /* without modseqs lost_flags isn't updated perfectly correctly, because |
414 | | by the time we're comparing old flags it may have changed from what |
415 | | we last sent to the client (because the map is shared). This could |
416 | | be avoided by always keeping a private copy of the map in the view, |
417 | | but that's a waste of memory for as rare of a problem as this. */ |
418 | 0 | if (changed) |
419 | 0 | seq_range_array_add(&ctx->lost_flags, new_rec->uid); |
420 | 0 | return 0; |
421 | 0 | } |
422 | | |
423 | | static int |
424 | | view_sync_get_log_lost_changes(struct mail_index_view_sync_ctx *ctx, |
425 | | unsigned int *expunge_count_r) |
426 | 0 | { |
427 | 0 | struct mail_index_view *view = ctx->view; |
428 | 0 | struct mail_index_map *old_map = view->map; |
429 | 0 | struct mail_index_map *new_map = view->index->map; |
430 | 0 | const unsigned int old_count = old_map->hdr.messages_count; |
431 | 0 | const unsigned int new_count = new_map->hdr.messages_count; |
432 | 0 | const struct mail_index_record *old_rec, *new_rec; |
433 | 0 | struct mail_transaction_header thdr; |
434 | 0 | uint32_t seqi, seqj; |
435 | | |
436 | | /* we don't update the map in the same order as it's typically done. |
437 | | map->rec_map may already have some messages appended that we don't |
438 | | want. get an atomic map to make sure these get removed. */ |
439 | 0 | (void)mail_index_sync_get_atomic_map(&ctx->sync_map_ctx); |
440 | |
|
441 | 0 | if (!mail_index_map_get_ext_idx(new_map, view->index->modseq_ext_id, |
442 | 0 | &ctx->lost_new_ext_idx)) |
443 | 0 | ctx->lost_new_ext_idx = (uint32_t)-1; |
444 | |
|
445 | 0 | i_array_init(&ctx->lost_flags, 64); |
446 | 0 | t_array_init(&ctx->lost_old_kw, 32); |
447 | 0 | t_array_init(&ctx->lost_new_kw, 32); |
448 | 0 | ctx->lost_kw_buf = t_buffer_create(128); |
449 | | |
450 | | /* handle expunges and sync flags */ |
451 | 0 | seqi = seqj = 1; |
452 | 0 | while (seqi <= old_count && seqj <= new_count) { |
453 | 0 | old_rec = MAIL_INDEX_REC_AT_SEQ(old_map, seqi); |
454 | 0 | new_rec = MAIL_INDEX_REC_AT_SEQ(new_map, seqj); |
455 | 0 | if (old_rec->uid == new_rec->uid) { |
456 | | /* message found - check if flags have changed */ |
457 | 0 | if (view_sync_apply_lost_changes(ctx, seqi, seqj) < 0) |
458 | 0 | return -1; |
459 | 0 | seqi++; seqj++; |
460 | 0 | } else if (old_rec->uid < new_rec->uid) { |
461 | | /* message expunged */ |
462 | 0 | seq_range_array_add(&ctx->expunges, old_rec->uid); |
463 | 0 | seqi++; |
464 | 0 | } else { |
465 | | /* new message appeared out of nowhere */ |
466 | 0 | mail_index_set_error(view->index, |
467 | 0 | "%s view is inconsistent: " |
468 | 0 | "uid=%u inserted in the middle of mailbox", |
469 | 0 | view->index->filepath, new_rec->uid); |
470 | 0 | return -1; |
471 | 0 | } |
472 | 0 | } |
473 | | /* if there are old messages left, they're all expunged */ |
474 | 0 | for (; seqi <= old_count; seqi++) { |
475 | 0 | old_rec = MAIL_INDEX_REC_AT_SEQ(old_map, seqi); |
476 | 0 | seq_range_array_add(&ctx->expunges, old_rec->uid); |
477 | 0 | } |
478 | | /* if there are new messages left, they're all new messages */ |
479 | 0 | thdr.type = MAIL_TRANSACTION_APPEND | MAIL_TRANSACTION_EXTERNAL; |
480 | 0 | thdr.size = sizeof(*new_rec); |
481 | 0 | for (; seqj <= new_count; seqj++) { |
482 | 0 | new_rec = MAIL_INDEX_REC_AT_SEQ(new_map, seqj); |
483 | 0 | if (mail_index_sync_record(&ctx->sync_map_ctx, |
484 | 0 | &thdr, new_rec) < 0) |
485 | 0 | return -1; |
486 | 0 | mail_index_map_lookup_keywords(new_map, seqj, |
487 | 0 | &ctx->lost_new_kw); |
488 | 0 | if (view_sync_update_keywords(ctx, new_rec->uid) < 0) |
489 | 0 | return -1; |
490 | 0 | } |
491 | 0 | *expunge_count_r = view_sync_expunges2seqs(ctx); |
492 | | |
493 | | /* we have no idea how far we've synced - make sure these aren't used */ |
494 | 0 | old_map->hdr.log_file_seq = 0; |
495 | 0 | old_map->hdr.log_file_head_offset = 0; |
496 | 0 | old_map->hdr.log_file_tail_offset = 0; |
497 | |
|
498 | 0 | if ((ctx->flags & MAIL_INDEX_VIEW_SYNC_FLAG_NOEXPUNGES) != 0) { |
499 | | /* Expunges aren't wanted to be synced. Remember if we skipped |
500 | | over any expunges. If yes, we must not update |
501 | | log_file_expunge_seq/offset at the end of the view sync |
502 | | so that a later sync can finish the expunges. */ |
503 | 0 | array_clear(&ctx->expunges); |
504 | 0 | ctx->skipped_expunges = *expunge_count_r > 0; |
505 | 0 | } |
506 | | /* After the view sync is finished, update |
507 | | log_file_head_seq/offset, since we've synced everything |
508 | | (except maybe the expunges) up to this point. */ |
509 | 0 | view->log_file_head_seq = new_map->hdr.log_file_seq; |
510 | 0 | view->log_file_head_offset = new_map->hdr.log_file_head_offset; |
511 | 0 | return 0; |
512 | 0 | } |
513 | | |
514 | | static int mail_index_view_sync_init_fix(struct mail_index_view_sync_ctx *ctx) |
515 | 0 | { |
516 | 0 | struct mail_index_view *view = ctx->view; |
517 | 0 | uint32_t seq; |
518 | 0 | uoff_t offset; |
519 | 0 | const char *reason; |
520 | 0 | bool reset; |
521 | 0 | int ret; |
522 | | |
523 | | /* replace the view's map */ |
524 | 0 | view->index->map->refcount++; |
525 | 0 | mail_index_unmap(&view->map); |
526 | 0 | view->map = view->index->map; |
527 | | |
528 | | /* update log positions */ |
529 | 0 | view->log_file_head_seq = seq = view->map->hdr.log_file_seq; |
530 | 0 | view->log_file_head_offset = offset = |
531 | 0 | view->map->hdr.log_file_head_offset; |
532 | |
|
533 | 0 | ret = mail_transaction_log_view_set(view->log_view, seq, offset, |
534 | 0 | seq, offset, &reset, &reason); |
535 | 0 | if (ret <= 0) { |
536 | 0 | mail_index_set_error(view->index, "Failed to fix view for %s: %s", |
537 | 0 | view->index->filepath, reason); |
538 | 0 | return ret; |
539 | 0 | } |
540 | 0 | view->inconsistent = FALSE; |
541 | 0 | return 0; |
542 | 0 | } |
543 | | |
544 | | struct mail_index_view_sync_ctx * |
545 | | mail_index_view_sync_begin(struct mail_index_view *view, |
546 | | enum mail_index_view_sync_flags flags) |
547 | 0 | { |
548 | 0 | struct mail_index_view_sync_ctx *ctx; |
549 | 0 | struct mail_index_map *tmp_map; |
550 | 0 | unsigned int expunge_count = 0; |
551 | 0 | bool reset, partial_sync, sync_expunges, have_expunges; |
552 | 0 | const char *error; |
553 | 0 | int ret; |
554 | |
|
555 | 0 | i_assert(!view->syncing); |
556 | 0 | i_assert(view->transactions_list == NULL); |
557 | | |
558 | 0 | view->syncing = TRUE; |
559 | | |
560 | | /* Syncing the view invalidates all previous looked up records. |
561 | | Unreference the mappings this view keeps because of them. */ |
562 | 0 | mail_index_view_unref_maps(view); |
563 | |
|
564 | 0 | ctx = i_new(struct mail_index_view_sync_ctx, 1); |
565 | 0 | ctx->view = view; |
566 | 0 | ctx->flags = flags; |
567 | |
|
568 | 0 | i_assert((flags & MAIL_INDEX_VIEW_SYNC_FLAG_KEEP_EXPUNGED) == 0 || |
569 | 0 | (flags & MAIL_INDEX_VIEW_SYNC_FLAG_NOEXPUNGES) != 0); |
570 | | |
571 | 0 | sync_expunges = (flags & MAIL_INDEX_VIEW_SYNC_FLAG_NOEXPUNGES) == 0; |
572 | 0 | if (sync_expunges) |
573 | 0 | i_array_init(&ctx->expunges, 64); |
574 | 0 | if ((flags & MAIL_INDEX_VIEW_SYNC_FLAG_FIX_INCONSISTENT) != 0) { |
575 | | /* just get this view synced - don't return anything */ |
576 | 0 | i_assert(sync_expunges); |
577 | 0 | if (mail_index_view_sync_init_fix(ctx) < 0) |
578 | 0 | ctx->failed = TRUE; |
579 | 0 | return ctx; |
580 | 0 | } |
581 | 0 | if (mail_index_view_is_inconsistent(view)) { |
582 | 0 | mail_index_set_error(view->index, "%s view is inconsistent", |
583 | 0 | view->index->filepath); |
584 | 0 | ctx->failed = TRUE; |
585 | 0 | return ctx; |
586 | 0 | } |
587 | | |
588 | 0 | ret = view_sync_set_log_view_range(view, sync_expunges, &reset, |
589 | 0 | &partial_sync, &error); |
590 | 0 | if (ret < 0) { |
591 | 0 | mail_index_set_error(view->index, "%s", error); |
592 | 0 | ctx->failed = TRUE; |
593 | 0 | return ctx; |
594 | 0 | } |
595 | | |
596 | 0 | if (ret == 0) { |
597 | | /* Log the warning only when all expunges have been synced |
598 | | by previous syncs. This way when there's a _FLAG_NOEXPUNGES |
599 | | sync, there's no second warning logged when the expunges |
600 | | finally are synced. */ |
601 | 0 | if (view->log_file_expunge_seq == view->log_file_head_seq && |
602 | 0 | view->log_file_expunge_offset == view->log_file_head_offset) { |
603 | 0 | e_warning(view->index->event, |
604 | 0 | "%s - generating missing logs", error); |
605 | 0 | } |
606 | 0 | ctx->log_was_lost = TRUE; |
607 | 0 | if (!sync_expunges) |
608 | 0 | i_array_init(&ctx->expunges, 64); |
609 | 0 | mail_index_sync_map_init(&ctx->sync_map_ctx, view, |
610 | 0 | MAIL_INDEX_SYNC_HANDLER_VIEW); |
611 | 0 | ret = view_sync_get_log_lost_changes(ctx, &expunge_count); |
612 | 0 | mail_index_modseq_sync_end(&ctx->sync_map_ctx.modseq_ctx); |
613 | 0 | mail_index_sync_map_deinit(&ctx->sync_map_ctx); |
614 | 0 | if (ret < 0) { |
615 | 0 | mail_index_set_error(view->index, |
616 | 0 | "%s view syncing failed to apply changes", |
617 | 0 | view->index->filepath); |
618 | 0 | view->inconsistent = TRUE; |
619 | 0 | ctx->failed = TRUE; |
620 | 0 | return ctx; |
621 | 0 | } |
622 | 0 | have_expunges = expunge_count > 0; |
623 | 0 | } else if (sync_expunges) { |
624 | | /* get list of all expunges first */ |
625 | 0 | if (view_sync_get_expunges(ctx, &expunge_count) < 0) { |
626 | 0 | ctx->failed = TRUE; |
627 | 0 | return ctx; |
628 | 0 | } |
629 | 0 | have_expunges = expunge_count > 0; |
630 | 0 | } else if (view->log_file_expunge_seq == view->log_file_head_seq && |
631 | 0 | view->log_file_expunge_offset == view->log_file_head_offset) { |
632 | | /* Previous syncs haven't left any pending expunges. See if |
633 | | this sync will. */ |
634 | 0 | have_expunges = view_sync_have_expunges(view); |
635 | 0 | } else { |
636 | | /* Expunges weren't synced in the previous sync either. |
637 | | We already know there are missing expunges. */ |
638 | 0 | ctx->skipped_expunges = TRUE; |
639 | 0 | have_expunges = TRUE; |
640 | 0 | } |
641 | | |
642 | 0 | ctx->finish_min_msg_count = reset ? 0 : |
643 | 0 | view->map->hdr.messages_count - expunge_count; |
644 | 0 | if (!reset) |
645 | 0 | ; |
646 | 0 | else if ((flags & MAIL_INDEX_VIEW_SYNC_FLAG_2ND_INDEX) != 0 && |
647 | 0 | view->map->hdr.messages_count == 0) { |
648 | | /* The secondary index is still empty, so it may have |
649 | | just been created for the first time. This is |
650 | | expected, so it shouldn't cause the view to become |
651 | | inconsistent. */ |
652 | 0 | if (mail_index_view_sync_init_fix(ctx) < 0) |
653 | 0 | ctx->failed = TRUE; |
654 | 0 | return ctx; |
655 | 0 | } else { |
656 | 0 | view->inconsistent = TRUE; |
657 | 0 | mail_index_set_error(view->index, |
658 | 0 | "%s reset, view is now inconsistent", |
659 | 0 | view->index->filepath); |
660 | 0 | ctx->failed = TRUE; |
661 | 0 | return ctx; |
662 | 0 | } |
663 | | |
664 | 0 | if (!have_expunges && !partial_sync && |
665 | 0 | (flags & MAIL_INDEX_VIEW_SYNC_FLAG_KEEP_EXPUNGED) == 0) { |
666 | | /* no expunges, we can just replace the map */ |
667 | 0 | if (view->index->map->hdr.messages_count < |
668 | 0 | ctx->finish_min_msg_count) { |
669 | 0 | mail_index_set_error(view->index, |
670 | 0 | "Index %s lost messages without expunging " |
671 | 0 | "(%u -> %u)", view->index->filepath, |
672 | 0 | view->map->hdr.messages_count, |
673 | 0 | view->index->map->hdr.messages_count); |
674 | 0 | ctx->finish_min_msg_count = 0; |
675 | 0 | view->inconsistent = TRUE; |
676 | 0 | } |
677 | |
|
678 | 0 | view->index->map->refcount++; |
679 | 0 | mail_index_unmap(&view->map); |
680 | 0 | view->map = view->index->map; |
681 | 0 | } else { |
682 | | /* a) expunges seen. b) doing a partial sync because we saw |
683 | | a reset. |
684 | | |
685 | | Create a private map which we update. If we're syncing |
686 | | expunges the map will finally be replaced with the head map |
687 | | to remove the expunged messages. */ |
688 | 0 | ctx->sync_map_update = TRUE; |
689 | |
|
690 | 0 | if (view->map->refcount > 1) { |
691 | 0 | tmp_map = mail_index_map_clone(view->map); |
692 | 0 | mail_index_unmap(&view->map); |
693 | 0 | view->map = tmp_map; |
694 | 0 | } |
695 | |
|
696 | 0 | if (sync_expunges) { |
697 | 0 | ctx->sync_new_map = view->index->map; |
698 | 0 | ctx->sync_new_map->refcount++; |
699 | 0 | } |
700 | 0 | } |
701 | 0 | mail_index_sync_map_init(&ctx->sync_map_ctx, view, |
702 | 0 | MAIL_INDEX_SYNC_HANDLER_VIEW); |
703 | |
|
704 | | #ifdef DEBUG |
705 | | mail_index_map_check(view->map); |
706 | | #endif |
707 | 0 | return ctx; |
708 | 0 | } |
709 | | |
710 | | static bool |
711 | | view_sync_is_hidden(struct mail_index_view *view, uint32_t seq, uoff_t offset) |
712 | 0 | { |
713 | 0 | const struct mail_index_view_log_sync_area *sync; |
714 | |
|
715 | 0 | if (!array_is_created(&view->syncs_hidden)) |
716 | 0 | return FALSE; |
717 | | |
718 | 0 | array_foreach(&view->syncs_hidden, sync) { |
719 | 0 | if (sync->log_file_offset <= offset && |
720 | 0 | offset - sync->log_file_offset < sync->length && |
721 | 0 | sync->log_file_seq == seq) |
722 | 0 | return TRUE; |
723 | 0 | } |
724 | | |
725 | 0 | return FALSE; |
726 | 0 | } |
727 | | |
728 | | static bool |
729 | | mail_index_view_sync_want(struct mail_index_view_sync_ctx *ctx, |
730 | | const struct mail_transaction_header *hdr) |
731 | 0 | { |
732 | 0 | struct mail_index_view *view = ctx->view; |
733 | 0 | uint32_t seq; |
734 | 0 | uoff_t offset, next_offset; |
735 | |
|
736 | 0 | mail_transaction_log_view_get_prev_pos(view->log_view, &seq, &offset); |
737 | 0 | next_offset = offset + sizeof(*hdr) + hdr->size; |
738 | |
|
739 | 0 | if ((hdr->type & (MAIL_TRANSACTION_EXPUNGE | |
740 | 0 | MAIL_TRANSACTION_EXPUNGE_GUID)) != 0 && |
741 | 0 | (hdr->type & MAIL_TRANSACTION_EXTERNAL) != 0) { |
742 | 0 | if ((ctx->flags & MAIL_INDEX_VIEW_SYNC_FLAG_NOEXPUNGES) != 0) { |
743 | 0 | i_assert(!LOG_IS_BEFORE(seq, offset, |
744 | 0 | view->log_file_expunge_seq, |
745 | 0 | view->log_file_expunge_offset)); |
746 | 0 | if (!ctx->skipped_expunges) { |
747 | 0 | view->log_file_expunge_seq = seq; |
748 | 0 | view->log_file_expunge_offset = offset; |
749 | 0 | ctx->skipped_expunges = TRUE; |
750 | 0 | } |
751 | 0 | return FALSE; |
752 | 0 | } |
753 | 0 | if (LOG_IS_BEFORE(seq, offset, view->log_file_expunge_seq, |
754 | 0 | view->log_file_expunge_offset)) { |
755 | | /* already synced */ |
756 | 0 | return FALSE; |
757 | 0 | } |
758 | 0 | } |
759 | | |
760 | 0 | if (LOG_IS_BEFORE(seq, offset, view->log_file_head_seq, |
761 | 0 | view->log_file_head_offset)) { |
762 | | /* already synced */ |
763 | 0 | return FALSE; |
764 | 0 | } |
765 | | |
766 | 0 | view->log_file_head_seq = seq; |
767 | 0 | view->log_file_head_offset = next_offset; |
768 | 0 | return TRUE; |
769 | 0 | } |
770 | | |
771 | | static int |
772 | | mail_index_view_sync_get_next_transaction(struct mail_index_view_sync_ctx *ctx) |
773 | 0 | { |
774 | 0 | struct mail_transaction_log_view *log_view = ctx->view->log_view; |
775 | 0 | struct mail_index_view *view = ctx->view; |
776 | 0 | const struct mail_transaction_header *hdr; |
777 | 0 | uint32_t seq; |
778 | 0 | uoff_t offset; |
779 | 0 | int ret; |
780 | 0 | bool synced_to_map; |
781 | |
|
782 | 0 | do { |
783 | | /* Get the next transaction from log. */ |
784 | 0 | ret = mail_transaction_log_view_next(log_view, &ctx->hdr, |
785 | 0 | &ctx->data); |
786 | 0 | if (ret <= 0) { |
787 | 0 | if (ret < 0) |
788 | 0 | return -1; |
789 | | |
790 | 0 | ctx->hdr = NULL; |
791 | 0 | ctx->last_read = TRUE; |
792 | 0 | return 0; |
793 | 0 | } |
794 | | |
795 | 0 | hdr = ctx->hdr; |
796 | | /* skip records we've already synced */ |
797 | 0 | } while (!mail_index_view_sync_want(ctx, hdr)); |
798 | | |
799 | 0 | mail_transaction_log_view_get_prev_pos(log_view, &seq, &offset); |
800 | | |
801 | | /* If we started from a map that we didn't create ourself, |
802 | | some of the transactions may already be synced. at the end |
803 | | of this view sync we'll update file_seq=0 so that this check |
804 | | always becomes FALSE for subsequent syncs. */ |
805 | 0 | synced_to_map = view->map->hdr.log_file_seq != 0 && |
806 | 0 | LOG_IS_BEFORE(seq, offset, view->map->hdr.log_file_seq, |
807 | 0 | view->map->hdr.log_file_head_offset); |
808 | | |
809 | | /* Apply transaction to view's mapping if needed (meaning we |
810 | | didn't just re-map the view to head mapping). */ |
811 | 0 | if (ctx->sync_map_update && !synced_to_map) { |
812 | 0 | if ((hdr->type & (MAIL_TRANSACTION_EXPUNGE | |
813 | 0 | MAIL_TRANSACTION_EXPUNGE_GUID)) == 0) { |
814 | 0 | ret = mail_index_sync_record(&ctx->sync_map_ctx, |
815 | 0 | hdr, ctx->data); |
816 | 0 | } |
817 | 0 | if (ret < 0) |
818 | 0 | return -1; |
819 | 0 | } |
820 | | |
821 | 0 | ctx->hidden = view_sync_is_hidden(view, seq, offset); |
822 | 0 | return 1; |
823 | 0 | } |
824 | | |
825 | | static bool |
826 | | mail_index_view_sync_get_rec(struct mail_index_view_sync_ctx *ctx, |
827 | | struct mail_index_view_sync_rec *rec) |
828 | 0 | { |
829 | 0 | const struct mail_transaction_header *hdr = ctx->hdr; |
830 | 0 | const void *data = ctx->data; |
831 | |
|
832 | 0 | switch (hdr->type & MAIL_TRANSACTION_TYPE_MASK) { |
833 | 0 | case MAIL_TRANSACTION_FLAG_UPDATE: { |
834 | 0 | const struct mail_transaction_flag_update *update = |
835 | 0 | CONST_PTR_OFFSET(data, ctx->data_offset); |
836 | | |
837 | | /* data contains mail_transaction_flag_update[] */ |
838 | 0 | for (;;) { |
839 | 0 | ctx->data_offset += sizeof(*update); |
840 | 0 | if (!MAIL_TRANSACTION_FLAG_UPDATE_IS_INTERNAL(update)) |
841 | 0 | break; |
842 | | |
843 | | /* skip internal flag changes */ |
844 | 0 | if (ctx->data_offset == ctx->hdr->size) |
845 | 0 | return FALSE; |
846 | | |
847 | 0 | update = CONST_PTR_OFFSET(data, ctx->data_offset); |
848 | 0 | } |
849 | | |
850 | 0 | if (update->add_flags != 0 || update->remove_flags != 0) |
851 | 0 | rec->type = MAIL_INDEX_VIEW_SYNC_TYPE_FLAGS; |
852 | 0 | else |
853 | 0 | rec->type = MAIL_INDEX_VIEW_SYNC_TYPE_MODSEQ; |
854 | 0 | rec->uid1 = update->uid1; |
855 | 0 | rec->uid2 = update->uid2; |
856 | 0 | break; |
857 | 0 | } |
858 | 0 | case MAIL_TRANSACTION_KEYWORD_UPDATE: { |
859 | 0 | const struct mail_transaction_keyword_update *update = data; |
860 | 0 | const uint32_t *uids; |
861 | | |
862 | | /* data contains mail_transaction_keyword_update header, |
863 | | the keyword name and an array of { uint32_t uid1, uid2; } */ |
864 | |
|
865 | 0 | if (ctx->data_offset == 0) { |
866 | | /* skip over the header and name */ |
867 | 0 | ctx->data_offset = sizeof(*update) + update->name_size; |
868 | 0 | if ((ctx->data_offset % 4) != 0) |
869 | 0 | ctx->data_offset += 4 - (ctx->data_offset % 4); |
870 | 0 | } |
871 | |
|
872 | 0 | uids = CONST_PTR_OFFSET(data, ctx->data_offset); |
873 | 0 | rec->type = MAIL_INDEX_VIEW_SYNC_TYPE_FLAGS; |
874 | 0 | rec->uid1 = uids[0]; |
875 | 0 | rec->uid2 = uids[1]; |
876 | |
|
877 | 0 | ctx->data_offset += sizeof(uint32_t) * 2; |
878 | 0 | break; |
879 | 0 | } |
880 | 0 | case MAIL_TRANSACTION_KEYWORD_RESET: { |
881 | 0 | const struct mail_transaction_keyword_reset *reset = |
882 | 0 | CONST_PTR_OFFSET(data, ctx->data_offset); |
883 | | |
884 | | /* data contains mail_transaction_keyword_reset[] */ |
885 | 0 | rec->type = MAIL_INDEX_VIEW_SYNC_TYPE_FLAGS; |
886 | 0 | rec->uid1 = reset->uid1; |
887 | 0 | rec->uid2 = reset->uid2; |
888 | 0 | ctx->data_offset += sizeof(*reset); |
889 | 0 | break; |
890 | 0 | } |
891 | 0 | default: |
892 | 0 | ctx->hdr = NULL; |
893 | 0 | return FALSE; |
894 | 0 | } |
895 | | |
896 | 0 | rec->hidden = ctx->hidden; |
897 | 0 | return TRUE; |
898 | 0 | } |
899 | | |
900 | | static bool |
901 | | mail_index_view_sync_next_lost(struct mail_index_view_sync_ctx *ctx, |
902 | | struct mail_index_view_sync_rec *sync_rec) |
903 | 0 | { |
904 | 0 | const struct seq_range *range; |
905 | 0 | unsigned int count; |
906 | |
|
907 | 0 | range = array_get(&ctx->lost_flags, &count); |
908 | 0 | if (ctx->lost_flag_idx == count) { |
909 | 0 | ctx->last_read = TRUE; |
910 | 0 | return FALSE; |
911 | 0 | } |
912 | | |
913 | 0 | sync_rec->type = MAIL_INDEX_VIEW_SYNC_TYPE_FLAGS; |
914 | 0 | sync_rec->uid1 = range[ctx->lost_flag_idx].seq1; |
915 | 0 | sync_rec->uid2 = range[ctx->lost_flag_idx].seq2; |
916 | 0 | sync_rec->hidden = FALSE; |
917 | 0 | ctx->lost_flag_idx++; |
918 | 0 | return TRUE; |
919 | 0 | } |
920 | | |
921 | | bool mail_index_view_sync_next(struct mail_index_view_sync_ctx *ctx, |
922 | | struct mail_index_view_sync_rec *sync_rec) |
923 | 0 | { |
924 | 0 | int ret; |
925 | |
|
926 | 0 | if (ctx->log_was_lost) |
927 | 0 | return mail_index_view_sync_next_lost(ctx, sync_rec); |
928 | | |
929 | 0 | do { |
930 | 0 | if (ctx->hdr == NULL || ctx->data_offset == ctx->hdr->size) { |
931 | 0 | ret = mail_index_view_sync_get_next_transaction(ctx); |
932 | 0 | if (ret <= 0) { |
933 | 0 | if (ret < 0) |
934 | 0 | ctx->failed = TRUE; |
935 | 0 | return FALSE; |
936 | 0 | } |
937 | | |
938 | 0 | ctx->data_offset = 0; |
939 | 0 | } |
940 | 0 | } while (!mail_index_view_sync_get_rec(ctx, sync_rec)); |
941 | | |
942 | 0 | return TRUE; |
943 | 0 | } |
944 | | |
945 | | void mail_index_view_sync_get_expunges(struct mail_index_view_sync_ctx *ctx, |
946 | | const ARRAY_TYPE(seq_range) **expunges_r) |
947 | 0 | { |
948 | 0 | *expunges_r = &ctx->expunges; |
949 | 0 | } |
950 | | |
951 | | static void |
952 | | mail_index_view_sync_clean_log_syncs(struct mail_index_view *view) |
953 | 0 | { |
954 | 0 | const struct mail_index_view_log_sync_area *syncs; |
955 | 0 | unsigned int i, count; |
956 | |
|
957 | 0 | if (!array_is_created(&view->syncs_hidden)) |
958 | 0 | return; |
959 | | |
960 | | /* Clean up to view's tail */ |
961 | 0 | syncs = array_get(&view->syncs_hidden, &count); |
962 | 0 | for (i = 0; i < count; i++) { |
963 | 0 | if ((syncs[i].log_file_offset + |
964 | 0 | syncs[i].length > view->log_file_expunge_offset && |
965 | 0 | syncs[i].log_file_seq == view->log_file_expunge_seq) || |
966 | 0 | syncs[i].log_file_seq > view->log_file_expunge_seq) |
967 | 0 | break; |
968 | 0 | } |
969 | 0 | if (i > 0) |
970 | 0 | array_delete(&view->syncs_hidden, 0, i); |
971 | 0 | } |
972 | | |
973 | | int mail_index_view_sync_commit(struct mail_index_view_sync_ctx **_ctx, |
974 | | bool *delayed_expunges_r) |
975 | 0 | { |
976 | 0 | struct mail_index_view_sync_ctx *ctx = *_ctx; |
977 | 0 | struct mail_index_view *view = ctx->view; |
978 | 0 | int ret = ctx->failed ? -1 : 0; |
979 | |
|
980 | 0 | i_assert(view->syncing); |
981 | | |
982 | 0 | *_ctx = NULL; |
983 | 0 | *delayed_expunges_r = ctx->skipped_expunges; |
984 | |
|
985 | 0 | if ((!ctx->last_read || view->inconsistent) && |
986 | 0 | (ctx->flags & MAIL_INDEX_VIEW_SYNC_FLAG_FIX_INCONSISTENT) == 0) { |
987 | | /* we didn't sync everything */ |
988 | 0 | view->inconsistent = TRUE; |
989 | 0 | ret = -1; |
990 | 0 | } |
991 | 0 | if (ctx->sync_map_ctx.modseq_ctx != NULL) |
992 | 0 | mail_index_modseq_sync_end(&ctx->sync_map_ctx.modseq_ctx); |
993 | |
|
994 | 0 | if (ctx->sync_new_map != NULL) { |
995 | 0 | mail_index_unmap(&view->map); |
996 | 0 | view->map = ctx->sync_new_map; |
997 | 0 | } else if (ctx->sync_map_update) { |
998 | | /* log offsets have no meaning in views. make sure they're not |
999 | | tried to be used wrong by setting them to zero. */ |
1000 | 0 | view->map->hdr.log_file_seq = 0; |
1001 | 0 | view->map->hdr.log_file_head_offset = 0; |
1002 | 0 | view->map->hdr.log_file_tail_offset = 0; |
1003 | 0 | } |
1004 | |
|
1005 | 0 | i_assert(view->map->hdr.messages_count >= ctx->finish_min_msg_count); |
1006 | | |
1007 | 0 | if (!ctx->skipped_expunges) { |
1008 | 0 | view->log_file_expunge_seq = view->log_file_head_seq; |
1009 | 0 | view->log_file_expunge_offset = view->log_file_head_offset; |
1010 | 0 | } |
1011 | |
|
1012 | 0 | if (ctx->sync_map_ctx.view != NULL) |
1013 | 0 | mail_index_sync_map_deinit(&ctx->sync_map_ctx); |
1014 | 0 | mail_index_view_sync_clean_log_syncs(ctx->view); |
1015 | |
|
1016 | | #ifdef DEBUG |
1017 | | mail_index_map_check(view->map); |
1018 | | #endif |
1019 | | |
1020 | | /* set log view to empty range so unneeded memory gets freed */ |
1021 | 0 | mail_transaction_log_view_clear(view->log_view, |
1022 | 0 | view->log_file_expunge_seq); |
1023 | |
|
1024 | 0 | if (array_is_created(&ctx->expunges)) |
1025 | 0 | array_free(&ctx->expunges); |
1026 | 0 | if (array_is_created(&ctx->lost_flags)) |
1027 | 0 | array_free(&ctx->lost_flags); |
1028 | |
|
1029 | 0 | view->highest_modseq = mail_index_map_modseq_get_highest(view->map); |
1030 | 0 | view->syncing = FALSE; |
1031 | 0 | i_free(ctx); |
1032 | 0 | return ret; |
1033 | 0 | } |
1034 | | |
1035 | | void mail_index_view_add_hidden_transaction(struct mail_index_view *view, |
1036 | | uint32_t log_file_seq, |
1037 | | uoff_t log_file_offset, |
1038 | | unsigned int length) |
1039 | 0 | { |
1040 | 0 | struct mail_index_view_log_sync_area *area; |
1041 | |
|
1042 | 0 | if (!array_is_created(&view->syncs_hidden)) |
1043 | 0 | i_array_init(&view->syncs_hidden, 32); |
1044 | |
|
1045 | 0 | area = array_append_space(&view->syncs_hidden); |
1046 | 0 | area->log_file_seq = log_file_seq; |
1047 | 0 | area->log_file_offset = log_file_offset; |
1048 | 0 | area->length = length; |
1049 | 0 | } |