mail-index-view.c revision b5ea11802f2bafbec06282a7b3b6704dc5fae584
a8c5a86d183db25a57bf193c06b41e092ec2e151Timo Sirainen/* Copyright (C) 2003-2004 Timo Sirainen */
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenstruct mail_index_view *mail_index_view_open(struct mail_index *index)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen view->log_view = mail_transaction_log_view_open(index->log);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen view->messages_count = view->map->records_count;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen view->log_file_offset = view->map->log_file_offset;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenvoid mail_index_view_close(struct mail_index_view *view)
4b89231f4ec9cc69f4aea715e1d34f405c7e317dTimo Sirainen mail_transaction_log_view_close(view->log_view);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenstatic int mail_index_view_map_protect(struct mail_index_view *view)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen /* not head mapping, no need to lock */
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen if (mail_index_map_lock_mprotect(view->index, view->map,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenint mail_index_view_lock_head(struct mail_index_view *view, int update_index)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen unsigned int lock_id;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen if (!mail_index_is_locked(view->index, view->lock_id)) {
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen if (mail_index_lock_shared(view->index, update_index,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen if (mail_index_map(view->index, FALSE) <= 0) {
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen /* index was rebuilt */
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen if (mail_index_lock_shared(view->index, TRUE, &lock_id) < 0)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen mail_index_unlock(view->index, view->lock_id);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen /* mail_index_lock_shared() may have reopened the file,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen so do this after it. */
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenint mail_index_view_lock(struct mail_index_view *view)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen return mail_index_view_lock_head(view, FALSE);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenvoid mail_index_view_unlock(struct mail_index_view *view)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen (void)mail_index_map_lock_mprotect(view->index, view->map,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen mail_index_unlock(view->index, view->lock_id);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenuint32_t mail_index_view_get_message_count(struct mail_index_view *view)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenint mail_index_view_is_inconsistent(struct mail_index_view *view)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenstruct mail_index *mail_index_view_get_index(struct mail_index_view *view)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenvoid mail_index_view_transaction_ref(struct mail_index_view *view)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenvoid mail_index_view_transaction_unref(struct mail_index_view *view)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenint mail_index_get_header(struct mail_index_view *view,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen if (view->map->hdr->messages_count == view->messages_count)
8fcf84e6b7a55049a6d407b17af50d6ae8c6386fPhil Carmody /* messages_count differs, use a modified copy */
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen view->tmp_hdr_copy.messages_count = view->messages_count;
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainenstatic int mail_index_lookup_int(struct mail_index_view *view, uint32_t seq,
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen rec = MAIL_INDEX_MAP_IDX(view->index, view->map, seq-1);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen if (mail_index_view_lock_head(view, FALSE) < 0)
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen /* look for it in the head mapping */
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen // FIXME: we could be skipping more by uid diff
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen n_rec = MAIL_INDEX_MAP_IDX(view->index, map, seq);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen } while (seq > 0);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenint mail_index_lookup(struct mail_index_view *view, uint32_t seq,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen return mail_index_lookup_int(view, seq, &map, rec_r);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenint mail_index_lookup_uid(struct mail_index_view *view, uint32_t seq,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen *uid_r = MAIL_INDEX_MAP_IDX(view->index, view->map, seq-1)->uid;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenint mail_index_lookup_extra(struct mail_index_view *view, uint32_t seq,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen if ((ret = mail_index_lookup_int(view, seq, &map, &rec)) < 0)
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen /* FIXME: do data_id mapping conversion */
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen offset = view->index->extra_records[data_id].offset;
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenstatic uint32_t mail_index_bsearch_uid(struct mail_index_view *view,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen const struct mail_index_record *rec_base, *rec;
bddd52cb7f3e5a894c080f60750aa76b5aeaf103Timo Sirainen uint32_t idx, left_idx, right_idx, record_size;
if (nearest_side > 0) {
*first_seq_r = 0;
*last_seq_r = 0;
left_idx = 0;
if (*first_seq_r == 0 ||
last_uid) {
*first_seq_r = 0;
*last_seq_r = 0;
#define LOW_UPDATE(x) \
*seq_r = 0;
if (seq == 0)