mail-transaction-log-view.c revision 01ac83a86f9f29741b585205eefeec9c0c546f8b
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (C) 2003-2004 Timo Sirainen */
a24f6b02ed8d0dde933a715be1c86f01977bf610Timo Sirainen struct mail_transaction_log_file *cur, *head, *tail;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenmail_transaction_log_view_open(struct mail_transaction_log *log)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen view = i_new(struct mail_transaction_log_view, 1);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mail_transaction_log_view_close(struct mail_transaction_log_view *view)
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainen for (p = &view->log->views; *p != NULL; p = &(*p)->next) {
a24f6b02ed8d0dde933a715be1c86f01977bf610Timo Sirainen for (file = view->tail; file != view->head; file = file->next)
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainenvoid mail_transaction_log_views_close(struct mail_transaction_log *log)
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainen for (view = log->views; view != NULL; view = view->next)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenmail_transaction_log_view_set(struct mail_transaction_log_view *view,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen uint32_t min_file_seq, uoff_t min_file_offset,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen uint32_t max_file_seq, uoff_t max_file_offset,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* FIXME: error handling for "not found" case is bad.. should the
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen caller after all check it and handle as it sees best..? */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct mail_transaction_log_file *file, *first;
0d658231054332c3f4c04aab0422af649de89a8cTimo Sirainen /* new index, transaction file not synced yet */
9df8c9225140d9d1df5ddf4c6c9da61662ae6c44Timo Sirainen if (min_file_seq == view->log->tail->hdr.prev_file_seq &&
9df8c9225140d9d1df5ddf4c6c9da61662ae6c44Timo Sirainen min_file_offset == view->log->tail->hdr.prev_file_offset) {
9df8c9225140d9d1df5ddf4c6c9da61662ae6c44Timo Sirainen /* we can skip this */
9df8c9225140d9d1df5ddf4c6c9da61662ae6c44Timo Sirainen /* empty view */
fc7b17677ac1a5fa3f7fe13d5ef7dcfea8d9b4a1Timo Sirainen /* find the oldest log file first. */
9df8c9225140d9d1df5ddf4c6c9da61662ae6c44Timo Sirainen ret = mail_transaction_log_file_find(view->log, min_file_seq, &file);
40ef82c46f6652412b068ebcdac7c3e74840a284Timo Sirainen "Lost transaction log file %s seq %u",
f75188af11dce30be322cc2eefa3e3884871abf7Timo Sirainen mail_index_set_inconsistent(view->log->index);
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen /* this could happen if internal transactions haven't yet been
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen committed but external are. just assume we're at the
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen beginning. */
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen if (max_file_offset == 0 && min_file_seq == max_file_seq)
ed1f14af0d426b5550521a58fc414d130aa14172Timo Sirainen /* check these later than others as index file may have corrupted
ed1f14af0d426b5550521a58fc414d130aa14172Timo Sirainen log_file_offset. we should have recreated the log file and
ed1f14af0d426b5550521a58fc414d130aa14172Timo Sirainen skipped min_file_seq file above.. max_file_offset can be broken
ed1f14af0d426b5550521a58fc414d130aa14172Timo Sirainen only if min_file_seq = max_file_seq. */
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen i_assert(min_file_offset >= file->hdr.hdr_size);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = mail_transaction_log_file_map(file, min_file_offset, end_offset);
fc7b17677ac1a5fa3f7fe13d5ef7dcfea8d9b4a1Timo Sirainen /* File is corrupted or stale NFS handle */
1b56f5fdd415270c743a38719d41b4d9497bcacdTimo Sirainen "Lost transaction log file %s seq %u",
f75188af11dce30be322cc2eefa3e3884871abf7Timo Sirainen mail_index_set_inconsistent(view->log->index);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen for (seq = min_file_seq+1; seq <= max_file_seq; seq++) {
fc7b17677ac1a5fa3f7fe13d5ef7dcfea8d9b4a1Timo Sirainen if (file == NULL || file->hdr.file_seq != seq) {
fc7b17677ac1a5fa3f7fe13d5ef7dcfea8d9b4a1Timo Sirainen /* see if we could find the missing file */
fc7b17677ac1a5fa3f7fe13d5ef7dcfea8d9b4a1Timo Sirainen ret = mail_transaction_log_file_find(view->log,
fc7b17677ac1a5fa3f7fe13d5ef7dcfea8d9b4a1Timo Sirainen /* not found / corrupted */
fc7b17677ac1a5fa3f7fe13d5ef7dcfea8d9b4a1Timo Sirainen if (file == NULL || file->hdr.file_seq != seq) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (file == NULL && max_file_seq == (uint32_t)-1) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* we just wanted to sync everything */
fc7b17677ac1a5fa3f7fe13d5ef7dcfea8d9b4a1Timo Sirainen /* missing files in the middle */
1b56f5fdd415270c743a38719d41b4d9497bcacdTimo Sirainen "Lost transaction log file %s seq %u",
f75188af11dce30be322cc2eefa3e3884871abf7Timo Sirainen mail_index_set_inconsistent(view->log->index);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen end_offset = file->hdr.file_seq == max_file_seq ?
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen ret = mail_transaction_log_file_map(file, file->hdr.hdr_size,
fc7b17677ac1a5fa3f7fe13d5ef7dcfea8d9b4a1Timo Sirainen /* File is corrupted or stale NFS handle */
1b56f5fdd415270c743a38719d41b4d9497bcacdTimo Sirainen "Lost transaction log file %s seq %u",
f75188af11dce30be322cc2eefa3e3884871abf7Timo Sirainen mail_index_set_inconsistent(view->log->index);
a24f6b02ed8d0dde933a715be1c86f01977bf610Timo Sirainen /* we have all of them. update refcounts. */
a24f6b02ed8d0dde933a715be1c86f01977bf610Timo Sirainen if (view->tail->hdr.file_seq < first->hdr.file_seq) {
a24f6b02ed8d0dde933a715be1c86f01977bf610Timo Sirainen /* unref old files */
a24f6b02ed8d0dde933a715be1c86f01977bf610Timo Sirainen for (file = view->tail; file != first; file = file->next)
9df8c9225140d9d1df5ddf4c6c9da61662ae6c44Timo Sirainen /* going backwards, reference them */
9df8c9225140d9d1df5ddf4c6c9da61662ae6c44Timo Sirainen for (file = first; file != view->tail; file = file->next)
a24f6b02ed8d0dde933a715be1c86f01977bf610Timo Sirainen /* reference all new files */
a24f6b02ed8d0dde933a715be1c86f01977bf610Timo Sirainen for (file = view->head->next; file != NULL; file = file->next)
fadd878cd6098f5b873c21c121209a922679dae4Timo Sirainen i_assert(view->cur_offset <= view->cur->sync_offset);
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen i_assert(view->cur->hdr.file_seq == min_file_seq);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenmail_transaction_log_view_get_prev_pos(struct mail_transaction_log_view *view,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenmail_transaction_log_view_set_corrupted(struct mail_transaction_log_view *view,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *fmt, ...)
c1d45cada20777e1973579d40d0ebe43f89bb053Timo Sirainen mail_transaction_log_file_set_corrupted(view->log->head, "%s",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenmail_transaction_log_view_is_corrupted(struct mail_transaction_log_view *view)
a8012fea2a7315033bc467acbf46be8e7323318cTimo Sirainenlog_view_get_next(struct mail_transaction_log_view *view,
834b90e1f426d1e3308670e09c050bcdea546eb8Timo Sirainen const void **data_r)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const struct mail_transaction_type_map *type_rec;
fadd878cd6098f5b873c21c121209a922679dae4Timo Sirainen /* prev_file_offset should point to beginning of previous log record.
fadd878cd6098f5b873c21c121209a922679dae4Timo Sirainen when we reach EOF, it should be left there, not to beginning of the
fadd878cd6098f5b873c21c121209a922679dae4Timo Sirainen next file. */
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen view->prev_file_seq = view->cur->hdr.file_seq;
fadd878cd6098f5b873c21c121209a922679dae4Timo Sirainen if (view->cur->hdr.file_seq == view->max_file_seq) {
fadd878cd6098f5b873c21c121209a922679dae4Timo Sirainen /* last file */
fadd878cd6098f5b873c21c121209a922679dae4Timo Sirainen if (view->cur_offset == view->max_file_offset ||
fadd878cd6098f5b873c21c121209a922679dae4Timo Sirainen /* we're all finished */
fadd878cd6098f5b873c21c121209a922679dae4Timo Sirainen } else if (view->cur_offset == view->cur->sync_offset) {
fadd878cd6098f5b873c21c121209a922679dae4Timo Sirainen /* end of file, go to next one */
fadd878cd6098f5b873c21c121209a922679dae4Timo Sirainen return log_view_get_next(view, hdr_r, data_r);
f1e1d821d93e4a1dc6ed8f23febde868b5d64cd5Timo Sirainen data = buffer_get_data(file->buffer, &file_size);
a24f6b02ed8d0dde933a715be1c86f01977bf610Timo Sirainen if (view->cur_offset + sizeof(*hdr) > file_size) {
f1e1d821d93e4a1dc6ed8f23febde868b5d64cd5Timo Sirainen "offset points outside file "
f1e1d821d93e4a1dc6ed8f23febde868b5d64cd5Timo Sirainen "(%"PRIuUOFF_T" + %"PRIuSIZE_T" > %"PRIuSIZE_T")",
6eb30032b4a50c383dea4c9c74342d906de6ad36Timo Sirainen i_assert(view->cur_offset >= file->buffer_offset);
a24f6b02ed8d0dde933a715be1c86f01977bf610Timo Sirainen hdr = CONST_PTR_OFFSET(data, view->cur_offset - file->buffer_offset);
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen hdr_size = mail_index_offset_to_uint32(hdr->size);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen type_rec = mail_transaction_type_lookup(hdr->type);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen "unknown record type 0x%x",
1225a5a7ce39f1d5545d5ed3b84ecd4f72438d36Timo Sirainen "record size too small (type=0x%x, "
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if ((hdr_size - sizeof(*hdr)) % record_size != 0) {
1225a5a7ce39f1d5545d5ed3b84ecd4f72438d36Timo Sirainen "record size wrong (type 0x%x, "
01ac83a86f9f29741b585205eefeec9c0c546f8bTimo Sirainen "offset=%"PRIuUOFF_T", size=%"PRIuSIZE_T" %% %u != 0)",
6eb30032b4a50c383dea4c9c74342d906de6ad36Timo Sirainen if (file_size - view->cur_offset < hdr_size) {
1225a5a7ce39f1d5545d5ed3b84ecd4f72438d36Timo Sirainen "record size too large (type=0x%x, "
1225a5a7ce39f1d5545d5ed3b84ecd4f72438d36Timo Sirainen "offset=%"PRIuUOFF_T", size=%u, end=%"PRIuSIZE_T")",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if ((hdr->type & MAIL_TRANSACTION_EXPUNGE) != 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if ((hdr->type & MAIL_TRANSACTION_TYPE_MASK) !=
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (MAIL_TRANSACTION_EXPUNGE|MAIL_TRANSACTION_EXPUNGE_PROT)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "found expunge without protection mask");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else if ((hdr->type & MAIL_TRANSACTION_TYPE_MASK) != type_rec->type) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "extra bits in header type: 0x%x",
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen } else if (hdr->type == MAIL_TRANSACTION_EXT_INTRO) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen const struct mail_transaction_ext_intro *intro;
a9bbb619908c206d9cb319398d4aaad37a22cd67Timo Sirainen for (i = 0; i < hdr_size; ) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen /* should be just extra padding */
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen "extension intro: name_size too large");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint mail_transaction_log_view_next(struct mail_transaction_log_view *view,
834b90e1f426d1e3308670e09c050bcdea546eb8Timo Sirainen while ((ret = log_view_get_next(view, &hdr, &data)) > 0) {
6e354c4070b611471727692919d29440d73a73f7Timo Sirainen /* looks like this is within our mask, but expunge
6e354c4070b611471727692919d29440d73a73f7Timo Sirainen protection may mess up the check. */
6e354c4070b611471727692919d29440d73a73f7Timo Sirainen if ((hdr->type & MAIL_TRANSACTION_EXPUNGE) == 0 ||
6e354c4070b611471727692919d29440d73a73f7Timo Sirainen (view->type_mask & MAIL_TRANSACTION_EXPUNGE) != 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* we don't want this record */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* FIXME: hide flag/cache updates for appends if
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen append isn't in mask */
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen mail_index_offset_to_uint32(view->tmp_hdr.size) - sizeof(*hdr);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if ((hdr->type & MAIL_TRANSACTION_EXPUNGE) != 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* hide expunge protection */