mail-transaction-log-view.c revision f75188af11dce30be322cc2eefa3e3884871abf7
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/* Copyright (C) 2003-2004 Timo Sirainen */
13d98ffa534f2e7d04a832c9d0153fc9c568b878Timo Sirainen struct mail_transaction_log_file *cur, *head, *tail;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenmail_transaction_log_view_open(struct mail_transaction_log *log)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen view = i_new(struct mail_transaction_log_view, 1);
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainenvoid mail_transaction_log_view_close(struct mail_transaction_log_view *view)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen for (p = &view->log->views; *p != NULL; p = &(*p)->next) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen for (file = view->tail; file != view->head; file = file->next)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenvoid mail_transaction_log_views_close(struct mail_transaction_log *log)
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen for (view = log->views; view != NULL; view = view->next)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenmail_transaction_log_view_set(struct mail_transaction_log_view *view,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen uint32_t min_file_seq, uoff_t min_file_offset,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen uint32_t max_file_seq, uoff_t max_file_offset,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* FIXME: error handling for "not found" case is bad.. should the
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen caller after all check it and handle as it sees best..? */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen struct mail_transaction_log_file *file, *first;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen /* new index, transaction file not synced yet */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (min_file_seq == view->log->tail->hdr.prev_file_seq &&
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen min_file_offset == view->log->tail->hdr.prev_file_offset) {
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen /* we can skip this */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* empty view */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen ret = mail_transaction_log_file_find(view->log, min_file_seq, &file);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen "Lost transaction log file %s seq %u",
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen mail_index_set_inconsistent(view->log->index);
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen /* this could happen if internal transactions haven't yet been
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen committed but external are. just assume we're at the
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen beginning. */
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen if (max_file_offset == 0 && min_file_seq == max_file_seq)
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen /* check these later than others as index file may have corrupted
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen log_file_offset. we should have recreated the log file and
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen skipped min_file_seq file above.. max_file_offset can be broken
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen only if min_file_seq = max_file_seq. */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen i_assert(min_file_offset >= file->hdr.hdr_size);
3fe67ec75ccae1230bb9eb9f16affc48377f6441Timo Sirainen ret = mail_transaction_log_file_map(file, min_file_offset, end_offset);
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen "Lost transaction log file %s seq %u",
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen mail_index_set_inconsistent(view->log->index);
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen for (seq = min_file_seq+1; seq <= max_file_seq; seq++) {
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen if (file == NULL || file->hdr.file_seq != seq) {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (file == NULL && max_file_seq == (uint32_t)-1) {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* we just wanted to sync everything */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen "Lost transaction log file %s seq %u",
360123b1b41b7aa8af6c4a91c39046be646cd349Timo Sirainen mail_index_set_inconsistent(view->log->index);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen end_offset = file->hdr.file_seq == max_file_seq ?
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen ret = mail_transaction_log_file_map(file, file->hdr.hdr_size,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen "Lost transaction log file %s seq %u",
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen mail_index_set_inconsistent(view->log->index);
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen /* we have all of them. update refcounts. */
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk if (view->tail->hdr.file_seq < first->hdr.file_seq) {
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk /* unref old files */
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk for (file = view->tail; file != first; file = file->next)
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk /* going backwards, reference them */
7920a47321690c932ffd4d286cd16b4048d22d41Timo Sirainen for (file = first; file != view->tail; file = file->next)
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen /* reference all new files */
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen for (file = view->head->next; file != NULL; file = file->next)
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen i_assert(view->cur_offset <= view->cur->sync_offset);
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen i_assert(view->cur->hdr.file_seq == min_file_seq);
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainenmail_transaction_log_view_get_prev_pos(struct mail_transaction_log_view *view,
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainenmail_transaction_log_view_set_corrupted(struct mail_transaction_log_view *view,
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen const char *fmt, ...)
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen mail_transaction_log_file_set_corrupted(view->log->head, "%s",
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainenmail_transaction_log_view_is_corrupted(struct mail_transaction_log_view *view)
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainenlog_view_get_next(struct mail_transaction_log_view *view,
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen const void **data_r)
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen const struct mail_transaction_type_map *type_rec;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* prev_file_offset should point to beginning of previous log record.
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen when we reach EOF, it should be left there, not to beginning of the
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen next file. */
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen view->prev_file_seq = view->cur->hdr.file_seq;
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen if (view->cur->hdr.file_seq == view->max_file_seq) {
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen /* last file */
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen if (view->cur_offset == view->max_file_offset ||
c58c12049c883b281c088d47a2a7278c21c390e1Timo Sirainen /* we're all finished */
c58c12049c883b281c088d47a2a7278c21c390e1Timo Sirainen } else if (view->cur_offset == view->cur->sync_offset) {
c58c12049c883b281c088d47a2a7278c21c390e1Timo Sirainen /* end of file, go to next one */
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen return log_view_get_next(view, hdr_r, data_r);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen data = buffer_get_data(file->buffer, &file_size);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen if (view->cur_offset + sizeof(*hdr) > file_size) {
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen "offset points outside file "
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen "(%"PRIuUOFF_T" + %"PRIuSIZE_T" > %"PRIuSIZE_T")",
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen hdr = CONST_PTR_OFFSET(data, view->cur_offset - file->buffer_offset);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen hdr_size = mail_index_offset_to_uint32(hdr->size);
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen if (file_size - view->cur_offset < hdr_size) {
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen "record size too large (type=0x%x, offset=%"PRIuUOFF_T
5494a6bc149da8f02fd25c0434a9d612ac33f659Timo Sirainen type_rec = mail_transaction_type_lookup(hdr->type);
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen "unknown record type 0x%x",
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen "record size too small (type=0x%x, size=%u)",
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen hdr->type & MAIL_TRANSACTION_TYPE_MASK, hdr_size);
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen if ((hdr_size - sizeof(*hdr)) % record_size != 0) {
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen "record size wrong (type 0x%x, %u %% %u != 0)",
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if ((hdr->type & MAIL_TRANSACTION_EXPUNGE) != 0) {
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if ((hdr->type & MAIL_TRANSACTION_TYPE_MASK) !=
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen (MAIL_TRANSACTION_EXPUNGE|MAIL_TRANSACTION_EXPUNGE_PROT)) {
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen "found expunge without protection mask");
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen } else if ((hdr->type & MAIL_TRANSACTION_TYPE_MASK) != type_rec->type) {
1433bf361ddb0bba8878c8ada5726d0284edad57Timo Sirainen "extra bits in header type: 0x%x",
1433bf361ddb0bba8878c8ada5726d0284edad57Timo Sirainen } else if (hdr->type == MAIL_TRANSACTION_EXT_INTRO) {
94d8e51119003d2bc5a100c663f90141f297385dTimo Sirainen const struct mail_transaction_ext_intro *intro;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen for (i = 0; i < hdr_size; ) {
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen /* should be just extra padding */
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen "extension intro: name_size too large");
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainenint mail_transaction_log_view_next(struct mail_transaction_log_view *view,
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen while ((ret = log_view_get_next(view, &hdr, &data)) > 0) {
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen /* looks like this is within our mask, but expunge
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen protection may mess up the check. */
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen if ((hdr->type & MAIL_TRANSACTION_EXPUNGE) == 0 ||
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen (view->type_mask & MAIL_TRANSACTION_EXPUNGE) != 0)
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen /* we don't want this record */
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen /* FIXME: hide flag/cache updates for appends if
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen append isn't in mask */
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen mail_index_offset_to_uint32(view->tmp_hdr.size) - sizeof(*hdr);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen if ((hdr->type & MAIL_TRANSACTION_EXPUNGE) != 0) {
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen /* hide expunge protection */