mail-index.c revision de12ff295bb3d0873b4dced5840612cbacd635ef
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (C) 2003-2004 Timo Sirainen */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "lib.h"
16f816d3f3c32ae3351834253f52ddd0212bcbf3Timo Sirainen#include "array.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "buffer.h"
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen#include "hash.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "mmap-util.h"
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen#include "read-full.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "write-full.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "mail-index-private.h"
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen#include "mail-index-sync-private.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "mail-transaction-log.h"
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen#include "mail-cache.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <stdio.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <stddef.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <time.h>
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen#include <sys/stat.h>
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenstatic int mail_index_try_open_only(struct mail_index *index);
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainenstatic void mail_index_create_in_memory(struct mail_index *index,
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen const struct mail_index_header *hdr);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstruct mail_index *mail_index_alloc(const char *dir, const char *prefix)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct mail_index *index;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index = i_new(struct mail_index, 1);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->dir = i_strdup(dir);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->prefix = i_strdup(prefix);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->fd = -1;
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen index->extension_pool = pool_alloconly_create("extension", 512);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen ARRAY_CREATE(&index->extensions, index->extension_pool,
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen struct mail_index_registered_ext, 5);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen ARRAY_CREATE(&index->sync_lost_handlers, default_pool,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen mail_index_sync_lost_handler_t *, 4);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->mode = 0600;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->gid = (gid_t)-1;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen index->keywords_ext_id =
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen mail_index_ext_register(index, "keywords", 128, 2, 1);
41e1c7380edda701719d8ce1fb4d465d2ec4c84dTimo Sirainen index->keywords_pool = pool_alloconly_create("keywords", 512);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen ARRAY_CREATE(&index->keywords, default_pool, const char *, 16);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen index->keywords_hash =
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen hash_create(default_pool, index->keywords_pool, 0,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen strcase_hash, (hash_cmp_callback_t *)strcasecmp);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return index;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mail_index_free(struct mail_index *index)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
ee246b46953e4b94b2f22e093373674fa9155500Timo Sirainen mail_index_close(index);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen hash_destroy(index->keywords_hash);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen pool_unref(index->extension_pool);
41e1c7380edda701719d8ce1fb4d465d2ec4c84dTimo Sirainen pool_unref(index->keywords_pool);
ee246b46953e4b94b2f22e093373674fa9155500Timo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen array_free(&index->sync_lost_handlers);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen array_free(&index->keywords);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_free(index->error);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_free(index->dir);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_free(index->prefix);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_free(index);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainenvoid mail_index_set_permissions(struct mail_index *index,
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen mode_t mode, gid_t gid)
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen{
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen index->mode = mode & 0666;
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen index->gid = gid;
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen}
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenuint32_t mail_index_ext_register(struct mail_index *index, const char *name,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen uint32_t default_hdr_size,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen uint16_t default_record_size,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen uint16_t default_record_align)
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen{
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen const struct mail_index_registered_ext *extensions;
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen struct mail_index_registered_ext rext;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen unsigned int i, ext_count;
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen extensions = array_get(&index->extensions, &ext_count);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen /* see if it's already there */
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen for (i = 0; i < ext_count; i++) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (strcmp(extensions[i].name, name) == 0)
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen return i;
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen }
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen memset(&rext, 0, sizeof(rext));
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen rext.name = p_strdup(index->extension_pool, name);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen rext.index_idx = ext_count;
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen rext.hdr_size = default_hdr_size;
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen rext.record_size = default_record_size;
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen rext.record_align = default_record_align;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen array_append(&index->extensions, &rext, 1);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen return ext_count;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen}
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainenvoid mail_index_register_expunge_handler(struct mail_index *index,
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen uint32_t ext_id,
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen mail_index_expunge_handler_t *cb)
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen{
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen struct mail_index_registered_ext *rext;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen rext = array_idx_modifyable(&index->extensions, ext_id);
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen i_assert(rext->expunge_handler == NULL);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen rext->expunge_handler = cb;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen}
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainenvoid mail_index_unregister_expunge_handler(struct mail_index *index,
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen uint32_t ext_id)
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen{
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen struct mail_index_registered_ext *rext;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen rext = array_idx_modifyable(&index->extensions, ext_id);
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen i_assert(rext->expunge_handler != NULL);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen rext->expunge_handler = NULL;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen}
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainenvoid mail_index_register_sync_handler(struct mail_index *index, uint32_t ext_id,
5626ae5e3316eced244adb6485c0927f1c7fdc41Timo Sirainen mail_index_sync_handler_t *cb,
5626ae5e3316eced244adb6485c0927f1c7fdc41Timo Sirainen enum mail_index_sync_handler_type type)
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen{
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen struct mail_index_registered_ext *rext;
5626ae5e3316eced244adb6485c0927f1c7fdc41Timo Sirainen
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen rext = array_idx_modifyable(&index->extensions, ext_id);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen i_assert(rext->sync_handler.callback == NULL);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen rext->sync_handler.callback = cb;
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen rext->sync_handler.type = type;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen}
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainenvoid mail_index_unregister_sync_handler(struct mail_index *index,
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen uint32_t ext_id)
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen{
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen struct mail_index_registered_ext *rext;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen rext = array_idx_modifyable(&index->extensions, ext_id);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen i_assert(rext->sync_handler.callback != NULL);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen rext->sync_handler.callback = NULL;
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen rext->sync_handler.type = 0;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen}
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainenvoid mail_index_register_sync_lost_handler(struct mail_index *index,
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen mail_index_sync_lost_handler_t *cb)
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen{
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen array_append(&index->sync_lost_handlers, &cb, 1);
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen}
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainenvoid mail_index_unregister_sync_lost_handler(struct mail_index *index,
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen mail_index_sync_lost_handler_t *cb)
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen{
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen mail_index_sync_lost_handler_t *const *handlers;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen unsigned int i, count;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen handlers = array_get(&index->sync_lost_handlers, &count);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen for (i = 0; i < count; i++) {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen if (handlers[i] == cb) {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen array_delete(&index->sync_lost_handlers, i, 1);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen break;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen }
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen }
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen}
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenstatic void mail_index_map_init_extbufs(struct mail_index_map *map,
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen unsigned int initial_count)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen{
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen#define EXTENSION_NAME_APPROX_LEN 20
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen size_t size;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen if (map->extension_pool == NULL) {
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen size = (sizeof(array_t) + BUFFER_APPROX_SIZE) * 2 +
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen initial_count * (EXTENSION_NAME_APPROX_LEN +
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen sizeof(struct mail_index_ext) +
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen sizeof(uint32_t));
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen map->extension_pool =
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen pool_alloconly_create("extensions",
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen nearest_power(size));
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen } else {
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen p_clear(map->extension_pool);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen ARRAY_CREATE(&map->extensions, map->extension_pool,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen struct mail_index_ext, initial_count);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen ARRAY_CREATE(&map->ext_id_map, map->extension_pool,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen uint32_t, initial_count);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen}
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenuint32_t mail_index_map_lookup_ext(struct mail_index_map *map, const char *name)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen{
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen const struct mail_index_ext *extensions;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen unsigned int i, size;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen if (!array_is_created(&map->extensions))
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return (uint32_t)-1;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen extensions = array_get(&map->extensions, &size);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen for (i = 0; i < size; i++) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (strcmp(extensions[i].name, name) == 0)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return i;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen }
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return (uint32_t)-1;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen}
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenuint32_t
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenmail_index_map_register_ext(struct mail_index *index,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen struct mail_index_map *map, const char *name,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen uint32_t hdr_offset, uint32_t hdr_size,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen uint32_t record_offset, uint32_t record_size,
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen uint32_t record_align, uint32_t reset_id)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen{
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen struct mail_index_ext *ext;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen uint32_t idx, empty_idx = (uint32_t)-1;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen if (!array_is_created(&map->extensions)) {
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen mail_index_map_init_extbufs(map, 5);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen idx = 0;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen } else {
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen idx = array_count(&map->extensions);
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen }
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen i_assert(mail_index_map_lookup_ext(map, name) == (uint32_t)-1);
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen
053843989f13d9013b265fb401a4bde7e0e6568eTimo Sirainen ext = array_append_space(&map->extensions);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen memset(ext, 0, sizeof(*ext));
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen ext->name = p_strdup(map->extension_pool, name);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen ext->hdr_offset = hdr_offset;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen ext->hdr_size = hdr_size;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen ext->record_offset = record_offset;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen ext->record_size = record_size;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen ext->record_align = record_align;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen ext->reset_id = reset_id;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen ext->index_idx = mail_index_ext_register(index, name, hdr_size,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen record_size, record_align);
ab286a8b58306eb8d22fc18342b6c199fd428e1eTimo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen while (array_count(&map->ext_id_map) < ext->index_idx)
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen array_append(&map->ext_id_map, &empty_idx, 1);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen array_idx_set(&map->ext_id_map, ext->index_idx, &idx);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return idx;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen}
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenstatic int size_check(size_t *size_left, size_t size)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen{
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (size > *size_left)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return FALSE;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen *size_left -= size;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return TRUE;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen}
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenstatic size_t get_align(size_t name_len)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen{
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen size_t size = sizeof(struct mail_index_ext_header) + name_len;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return MAIL_INDEX_HEADER_SIZE_ALIGN(size) - size;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen}
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenstatic int mail_index_read_extensions(struct mail_index *index,
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen struct mail_index_map *map)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen{
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen const struct mail_index_ext_header *ext_hdr;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen unsigned int i, old_count;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen const char *name;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen uint32_t ext_id, offset, name_offset;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen size_t size_left;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen /* extension headers always start from 64bit offsets, so if base header
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen doesn't happen to be 64bit aligned we'll skip some bytes */
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen offset = MAIL_INDEX_HEADER_SIZE_ALIGN(map->hdr.base_header_size);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if (offset >= map->hdr.header_size && map->extension_pool == NULL) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* nothing to do, skip allocatations and all */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return 1;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen old_count = array_count(&index->extensions);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen mail_index_map_init_extbufs(map, old_count + 5);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen ext_id = (uint32_t)-1;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen for (i = 0; i < old_count; i++)
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen array_append(&map->ext_id_map, &ext_id, 1);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen while (offset < map->hdr.header_size) {
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen ext_hdr = CONST_PTR_OFFSET(map->hdr_base, offset);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen size_left = map->hdr.header_size - offset;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (!size_check(&size_left, sizeof(*ext_hdr)) ||
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen !size_check(&size_left, ext_hdr->name_size) ||
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen !size_check(&size_left, get_align(ext_hdr->name_size)) ||
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen !size_check(&size_left, ext_hdr->hdr_size)) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen "Header extension goes outside header",
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen index->filepath);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return -1;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen offset += sizeof(*ext_hdr);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen name_offset = offset;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen offset += ext_hdr->name_size + get_align(ext_hdr->name_size);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen t_push();
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen name = t_strndup(CONST_PTR_OFFSET(map->hdr_base, name_offset),
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen ext_hdr->name_size);
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen if (mail_index_map_lookup_ext(map, name) != (uint32_t)-1) {
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen "Duplicate header extension %s",
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen index->filepath, name);
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen t_pop();
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen return -1;
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen }
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen if ((ext_hdr->record_offset % ext_hdr->record_align) != 0 ||
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen (map->hdr.record_size % ext_hdr->record_align) != 0) {
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen "Record field %s alignmentation %u not used",
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen index->filepath, name, ext_hdr->record_align);
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen t_pop();
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen return -1;
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen }
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen mail_index_map_register_ext(index, map, name,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen offset, ext_hdr->hdr_size,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen ext_hdr->record_offset,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen ext_hdr->record_size,
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen ext_hdr->record_align,
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen ext_hdr->reset_id);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen t_pop();
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen offset += MAIL_INDEX_HEADER_SIZE_ALIGN(ext_hdr->hdr_size);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return 1;
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen}
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenint mail_index_keyword_lookup(struct mail_index *index,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen const char *keyword, int autocreate,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen unsigned int *idx_r)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen char *keyword_dup;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen void *value;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (hash_lookup_full(index->keywords_hash, keyword, NULL, &value)) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen *idx_r = POINTER_CAST_TO(value, unsigned int);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return TRUE;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (!autocreate) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen *idx_r = (unsigned int)-1;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return FALSE;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen keyword = keyword_dup = p_strdup(index->keywords_pool, keyword);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen *idx_r = array_count(&index->keywords);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen hash_insert(index->keywords_hash, keyword_dup, POINTER_CAST(*idx_r));
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen array_append(&index->keywords, &keyword, 1);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return TRUE;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen}
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenint mail_index_map_read_keywords(struct mail_index *index,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen struct mail_index_map *map)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen{
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen const struct mail_index_ext *ext;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen const struct mail_index_keyword_header *kw_hdr;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen const struct mail_index_keyword_header_rec *kw_rec;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen const char *name;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen unsigned int i, name_len, old_count;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen uint32_t ext_id;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen ext_id = mail_index_map_lookup_ext(map, "keywords");
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (ext_id == (uint32_t)-1) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (array_is_created(&map->keyword_idx_map))
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen array_clear(&map->keyword_idx_map);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen return 0;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen ext = array_idx(&map->extensions, ext_id);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen kw_hdr = CONST_PTR_OFFSET(map->hdr_base, ext->hdr_offset);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen kw_rec = (const void *)(kw_hdr + 1);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen name = (const char *)(kw_rec + kw_hdr->keywords_count);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen old_count = !array_is_created(&map->keyword_idx_map) ? 0 :
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen array_count(&map->keyword_idx_map);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* Keywords can only be added in mapping. */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (kw_hdr->keywords_count == old_count) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* nothing changed */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return 0;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* make sure the header is valid */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (kw_hdr->keywords_count < old_count) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen "Keywords removed unexpectedly",
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen index->filepath);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return -1;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if ((size_t)(name - (const char *)kw_hdr) > ext->hdr_size) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen "keywords_count larger than header size",
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen index->filepath);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen return -1;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen name_len = (const char *)kw_hdr + ext->hdr_size - name;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen for (i = 0; i < kw_hdr->keywords_count; i++) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (kw_rec[i].name_offset > name_len) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen "name_offset points outside allocated header",
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen index->filepath);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen return -1;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (name[name_len-1] != '\0') {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
871c7b8969e8627dc4c8b3e56fd126f948e6bce6Timo Sirainen "Keyword header doesn't end with NUL",
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen index->filepath);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen return -1;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* create file -> index mapping */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (!array_is_created(&map->keyword_idx_map)) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen ARRAY_CREATE(&map->keyword_idx_map, default_pool,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen unsigned int, kw_hdr->keywords_count);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen#ifdef DEBUG
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen for (i = 0; i < array_count(&map->keyword_idx_map); i++) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen const char *keyword = name + kw_rec[i].name_offset;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen const unsigned int *old_idx;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen unsigned int idx;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen old_idx = array_idx(&map->keyword_idx_map, i);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (!mail_index_keyword_lookup(index, keyword, FALSE, &idx) ||
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen idx != *old_idx) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen "Keywords changed unexpectedly",
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen index->filepath);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return -1;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen#endif
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen i = array_count(&map->keyword_idx_map);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen for (; i < kw_hdr->keywords_count; i++) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen const char *keyword = name + kw_rec[i].name_offset;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen unsigned int idx;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen (void)mail_index_keyword_lookup(index, keyword, TRUE, &idx);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen array_append(&map->keyword_idx_map, &idx, 1);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen return 0;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen}
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenconst array_t *mail_index_get_keywords(struct mail_index *index)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen{
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen (void)mail_index_map_read_keywords(index, index->map);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return &index->keywords;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen}
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int mail_index_check_header(struct mail_index *index,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen struct mail_index_map *map)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen const struct mail_index_header *hdr = &map->hdr;
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen unsigned char compat_data[sizeof(hdr->compat_data)];
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen memset(compat_data, 0, sizeof(compat_data));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#ifndef WORDS_BIGENDIAN
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen compat_data[0] = MAIL_INDEX_COMPAT_LITTLE_ENDIAN;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#endif
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen compat_data[1] = sizeof(uoff_t);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen compat_data[2] = sizeof(time_t);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (hdr->major_version != MAIL_INDEX_MAJOR_VERSION) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* major version change - handle silently(?) */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (memcmp(hdr->compat_data, compat_data, sizeof(compat_data)) != 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* architecture change - handle silently(?) */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if ((map->hdr.flags & MAIL_INDEX_HDR_FLAG_CORRUPTED) != 0) {
8830fab191cab8440281eb641dfdd93974b2933bTimo Sirainen /* we've already complained about it */
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen return -1;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* following some extra checks that only take a bit of CPU */
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen if (hdr->uid_validity == 0 && hdr->next_uid != 1) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen "uid_validity = 0, next_uid = %u",
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen index->filepath, hdr->next_uid);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen if (hdr->record_size < sizeof(struct mail_index_record)) {
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen "record_size too small: %u < %"PRIuSIZE_T,
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen index->filepath, hdr->record_size,
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen sizeof(struct mail_index_record));
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen return -1;
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen }
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen
e12648867876aaec17e06ee4caef0bb60363449dTimo Sirainen if ((hdr->flags & MAIL_INDEX_HDR_FLAG_FSCK) != 0)
e12648867876aaec17e06ee4caef0bb60363449dTimo Sirainen return 0;
e12648867876aaec17e06ee4caef0bb60363449dTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (hdr->next_uid == 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen if (hdr->recent_messages_count > hdr->messages_count ||
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen hdr->seen_messages_count > hdr->messages_count ||
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen hdr->deleted_messages_count > hdr->messages_count)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (hdr->first_recent_uid_lowwater > hdr->next_uid ||
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen hdr->first_unseen_uid_lowwater > hdr->next_uid ||
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen hdr->first_deleted_uid_lowwater > hdr->next_uid)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen return mail_index_read_extensions(index, map);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenstatic void mail_index_map_clear(struct mail_index *index,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen struct mail_index_map *map)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (map->buffer != NULL) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_assert(map->mmap_base == NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen buffer_free(map->buffer);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen map->buffer = NULL;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen } else if (map->mmap_base != NULL) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_assert(map->buffer == NULL);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (munmap(map->mmap_base, map->mmap_size) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_set_syscall_error(index, "munmap()");
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen map->mmap_base = NULL;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (map->refcount > 0) {
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen memset(&map->hdr, 0, sizeof(map->hdr));
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen map->mmap_size = 0;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen map->mmap_used_size = 0;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen map->records = NULL;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen map->records_count = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen}
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenvoid mail_index_unmap(struct mail_index *index, struct mail_index_map *map)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen{
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (--map->refcount > 0)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen return;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen i_assert(map->refcount == 0);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen mail_index_map_clear(index, map);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen if (map->extension_pool != NULL)
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen pool_unref(map->extension_pool);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (array_is_created(&map->keyword_idx_map))
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen array_free(&map->keyword_idx_map);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen buffer_free(map->hdr_copy_buf);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_free(map);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenstatic void mail_index_unmap_forced(struct mail_index *index,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen struct mail_index_map *map)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen mail_index_map_clear(index, map);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen mail_index_unmap(index, map);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen}
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainenstatic void mail_index_map_copy_hdr(struct mail_index_map *map,
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen const struct mail_index_header *hdr)
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen{
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen if (hdr->base_header_size < sizeof(map->hdr)) {
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen /* header smaller than ours, make a copy so our newer headers
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen won't have garbage in them */
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen memset(&map->hdr, 0, sizeof(map->hdr));
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen memcpy(&map->hdr, hdr, hdr->base_header_size);
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen } else {
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen map->hdr = *hdr;
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen }
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen}
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainenstatic int mail_index_mmap(struct mail_index *index, struct mail_index_map *map)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen{
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen const struct mail_index_header *hdr;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen unsigned int records_count;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen i_assert(!map->write_to_disk);
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen if (map->buffer != NULL) {
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen /* we had temporarily used a buffer, eg. for updating index */
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen buffer_free(map->buffer);
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen map->buffer = NULL;
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen }
4c1827d5d718d6f610df3209a2eb95a6591698afTimo Sirainen
5a4ab3d6e108a899c8b51bebd0094a37b738d5a1Timo Sirainen map->mmap_base = index->readonly ?
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen mmap_ro_file(index->fd, &map->mmap_size) :
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen mmap_rw_file(index->fd, &map->mmap_size);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (map->mmap_base == MAP_FAILED) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen map->mmap_base = NULL;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen mail_index_set_syscall_error(index, "mmap()");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen hdr = map->mmap_base;
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen if (map->mmap_size >
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen offsetof(struct mail_index_header, major_version) &&
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen hdr->major_version != MAIL_INDEX_MAJOR_VERSION) {
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen /* major version change - handle silently */
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen return 0;
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen }
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (map->mmap_size < MAIL_INDEX_HEADER_MIN_SIZE) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen "File too small (%"PRIuSIZE_T")",
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen index->filepath, map->mmap_size);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen return 0;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen }
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen map->mmap_used_size = hdr->header_size +
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen hdr->messages_count * hdr->record_size;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (map->mmap_used_size > map->mmap_size) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen records_count = (map->mmap_size - hdr->header_size) /
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen hdr->record_size;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen "messages_count too large (%u > %u)",
cd65dfcedc603fabbf644be793efb09096f6c1b5Timo Sirainen index->filepath, hdr->messages_count,
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen records_count);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen return 0;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen }
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen mail_index_map_copy_hdr(map, hdr);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen map->hdr_base = map->mmap_base;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen map->records = PTR_OFFSET(map->mmap_base, map->hdr.header_size);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen map->records_count = map->hdr.messages_count;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen return 1;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen}
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenstatic int mail_index_read_header(struct mail_index *index,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen struct mail_index_header *hdr, size_t *pos_r)
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen{
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen size_t pos;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen int ret;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen memset(hdr, 0, sizeof(*hdr));
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen ret = 1;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen for (pos = 0; ret > 0 && pos < sizeof(*hdr); ) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen ret = pread(index->fd, PTR_OFFSET(hdr, pos),
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen sizeof(*hdr) - pos, pos);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (ret > 0)
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen pos += ret;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen *pos_r = pos;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen return ret;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen}
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainenstatic int mail_index_read_map(struct mail_index *index,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen struct mail_index_map *map, int *retry_r)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen{
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen struct mail_index_header hdr;
28dd1eeb9fdb93051bb5899c80cf300d69ba9f9eTimo Sirainen void *data = NULL;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen ssize_t ret;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen size_t pos, records_size;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
4c1827d5d718d6f610df3209a2eb95a6591698afTimo Sirainen i_assert(map->mmap_base == NULL);
4c1827d5d718d6f610df3209a2eb95a6591698afTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen *retry_r = FALSE;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen ret = mail_index_read_header(index, &hdr, &pos);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
05751847e5e493fecfd0634d35d21722aad44a0bTimo Sirainen if (pos > (ssize_t)offsetof(struct mail_index_header, major_version) &&
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen hdr.major_version != MAIL_INDEX_MAJOR_VERSION) {
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen /* major version change - handle silently */
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen return 0;
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen }
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (ret >= 0 && pos >= MAIL_INDEX_HEADER_MIN_SIZE &&
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen (ret > 0 || pos >= hdr.base_header_size)) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (hdr.base_header_size < MAIL_INDEX_HEADER_MIN_SIZE ||
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen hdr.header_size < hdr.base_header_size) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen "Corrupted header sizes (base %u, full %u)",
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen index->filepath, hdr.base_header_size,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen hdr.header_size);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return 0;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen buffer_reset(map->hdr_copy_buf);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen buffer_append(map->hdr_copy_buf, &hdr, hdr.base_header_size);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen /* @UNSAFE */
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen data = buffer_append_space_unsafe(map->hdr_copy_buf,
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen hdr.header_size -
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen hdr.base_header_size);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen ret = pread_full(index->fd, data,
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen hdr.header_size - hdr.base_header_size,
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen hdr.base_header_size);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (ret > 0) {
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen records_size = hdr.messages_count * hdr.record_size;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (map->buffer == NULL) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen map->buffer = buffer_create_dynamic(default_pool,
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen records_size);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen }
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen /* @UNSAFE */
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen buffer_set_used_size(map->buffer, 0);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen data = buffer_append_space_unsafe(map->buffer, records_size);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen ret = pread_full(index->fd, data, records_size,
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen hdr.header_size);
28dd1eeb9fdb93051bb5899c80cf300d69ba9f9eTimo Sirainen }
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (ret < 0) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (errno == ESTALE) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen *retry_r = TRUE;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen return 0;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen mail_index_set_syscall_error(index, "pread_full()");
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen return -1;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (ret == 0) {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen mail_index_set_error(index,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen "Corrupted index file %s: File too small",
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen index->filepath);
b47c57ee07ccfe76cc23b23bc720e17fe9a46d5cTimo Sirainen return 0;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen map->records = data;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen map->records_count = hdr.messages_count;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen mail_index_map_copy_hdr(map, &hdr);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen map->hdr_base = map->hdr_copy_buf->data;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen index->sync_log_file_seq = hdr.log_file_seq;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen index->sync_log_file_offset = hdr.log_file_int_offset;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen return 1;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen}
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenstatic int mail_index_sync_from_transactions(struct mail_index *index,
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen struct mail_index_map **map,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen int sync_to_index)
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen{
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen const struct mail_index_header *map_hdr = &(*map)->hdr;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen struct mail_index_view *view;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen struct mail_transaction_log_view *log_view;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen struct mail_index_sync_map_ctx sync_map_ctx;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen struct mail_index_header hdr;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen const struct mail_transaction_header *thdr;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen const void *tdata;
0b38c17fb10e2ad1d081369d34b93d0448d971a2Timo Sirainen uint32_t prev_seq, max_seq;
0b38c17fb10e2ad1d081369d34b93d0448d971a2Timo Sirainen uoff_t prev_offset, max_offset;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen size_t pos;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen int ret, skipped;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (sync_to_index) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* read the real log position where we are supposed to be
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen synced */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen ret = mail_index_read_header(index, &hdr, &pos);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (ret < 0 && errno != ESTALE) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen mail_index_set_syscall_error(index, "pread()");
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen return -1;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (pos < MAIL_INDEX_HEADER_MIN_SIZE)
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen return 0;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen if (map_hdr->log_file_seq == hdr.log_file_seq &&
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen map_hdr->log_file_int_offset == hdr.log_file_int_offset) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* nothing to do */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen return 1;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen if (map_hdr->log_file_seq > hdr.log_file_seq ||
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen (map_hdr->log_file_seq == hdr.log_file_seq &&
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen map_hdr->log_file_int_offset > hdr.log_file_int_offset)) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* we went too far, have to re-read the file */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen return 0;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen if (map_hdr->log_file_ext_offset !=
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen map_hdr->log_file_int_offset ||
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen hdr.log_file_ext_offset != hdr.log_file_int_offset) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* too much trouble to get this right. */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen return 0;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen max_seq = hdr.log_file_seq;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen max_offset = hdr.log_file_int_offset;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen } else {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* sync everything there is */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen max_seq = (uint32_t)-1;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen max_offset = (uoff_t)-1;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen log_view = mail_transaction_log_view_open(index->log);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (mail_transaction_log_view_set(log_view,
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen map_hdr->log_file_seq,
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen map_hdr->log_file_int_offset,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen max_seq, max_offset,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen MAIL_TRANSACTION_TYPE_MASK) < 0) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen mail_transaction_log_view_close(log_view);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen return 0;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen index->map = *map;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen view = mail_index_view_open(index);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen mail_index_sync_map_init(&sync_map_ctx, view,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen MAIL_INDEX_SYNC_HANDLER_VIEW);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen while ((ret = mail_transaction_log_view_next(log_view, &thdr, &tdata,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen &skipped)) > 0) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (mail_index_sync_record(&sync_map_ctx, thdr, tdata) < 0) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen ret = -1;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen break;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
0b38c17fb10e2ad1d081369d34b93d0448d971a2Timo Sirainen mail_transaction_log_view_get_prev_pos(log_view, &prev_seq,
0b38c17fb10e2ad1d081369d34b93d0448d971a2Timo Sirainen &prev_offset);
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen i_assert(prev_seq <= max_seq &&
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen (prev_seq != max_seq || prev_offset <= max_offset));
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen index->map->hdr.log_file_seq = prev_seq;
0b38c17fb10e2ad1d081369d34b93d0448d971a2Timo Sirainen index->map->hdr.log_file_int_offset =
0b38c17fb10e2ad1d081369d34b93d0448d971a2Timo Sirainen index->map->hdr.log_file_ext_offset = prev_offset;
0b38c17fb10e2ad1d081369d34b93d0448d971a2Timo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen mail_index_sync_map_deinit(&sync_map_ctx);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen mail_index_view_close(view);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen mail_transaction_log_view_close(log_view);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen *map = index->map;
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen index->map = NULL;
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen
09714f2f8d4c8cc7d89930f7eb836816d8dc9eb8Timo Sirainen if (sync_to_index) {
a80241dc44ee43bc809ed5ec6dedb74711966dafTimo Sirainen /* make sure we did everything right. note that although the
a80241dc44ee43bc809ed5ec6dedb74711966dafTimo Sirainen message counts should be equal, the flag counters may not */
5e2cf2e665bb0c3c68fa34bc6d0e9ce93426fdd0Timo Sirainen i_assert(hdr.messages_count == (*map)->hdr.messages_count);
a80241dc44ee43bc809ed5ec6dedb74711966dafTimo Sirainen i_assert(hdr.log_file_seq == (*map)->hdr.log_file_seq);
a80241dc44ee43bc809ed5ec6dedb74711966dafTimo Sirainen i_assert(hdr.log_file_int_offset == (*map)->hdr.log_file_int_offset);
a80241dc44ee43bc809ed5ec6dedb74711966dafTimo Sirainen i_assert(hdr.log_file_ext_offset == (*map)->hdr.log_file_ext_offset);
09714f2f8d4c8cc7d89930f7eb836816d8dc9eb8Timo Sirainen }
09714f2f8d4c8cc7d89930f7eb836816d8dc9eb8Timo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen return ret < 0 ? -1 : 1;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen}
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenstatic int mail_index_read_map_with_retry(struct mail_index *index,
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen struct mail_index_map **map,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen int sync_to_index)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen{
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen mail_index_sync_lost_handler_t *const *handlers;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen unsigned int i, count;
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen int ret, retry;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
673ab5815277e6f9ac09227c866c90ab57438912Timo Sirainen if (index->log_locked) {
673ab5815277e6f9ac09227c866c90ab57438912Timo Sirainen /* we're most likely syncing the index and we really don't
673ab5815277e6f9ac09227c866c90ab57438912Timo Sirainen want to read more than what was synced last time. */
673ab5815277e6f9ac09227c866c90ab57438912Timo Sirainen sync_to_index = TRUE;
673ab5815277e6f9ac09227c866c90ab57438912Timo Sirainen }
673ab5815277e6f9ac09227c866c90ab57438912Timo Sirainen
2ef8fac74c9d0bfc330bf53bb06df7e9d5f08e7dTimo Sirainen if ((*map)->hdr.indexid != 0 && index->log != NULL) {
2ef8fac74c9d0bfc330bf53bb06df7e9d5f08e7dTimo Sirainen /* we're not creating the index, or opening transaction log.
2ef8fac74c9d0bfc330bf53bb06df7e9d5f08e7dTimo Sirainen sync this as a view from transaction log. */
b9ce555e8624a5593b3bfd81b572b7d2e1e1fca5Timo Sirainen index->sync_update = TRUE;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen ret = mail_index_sync_from_transactions(index, map,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen sync_to_index);
b9ce555e8624a5593b3bfd81b572b7d2e1e1fca5Timo Sirainen index->sync_update = FALSE;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (ret != 0)
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen return ret;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* transaction log lost/broken, fallback to re-reading it */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen /* notify all "sync lost" handlers */
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen handlers = array_get(&index->sync_lost_handlers, &count);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen for (i = 0; i < count; i++)
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen (*handlers[i])(index);
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen
e4fb5bfcdff32d337d054cce36e00e1cdfaae9f8Timo Sirainen for (i = 0; i < MAIL_INDEX_ESTALE_RETRY_COUNT; i++) {
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen ret = mail_index_read_map(index, *map, &retry);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (ret != 0 || !retry)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen return ret;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* ESTALE - reopen index file */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (close(index->fd) < 0)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen mail_index_set_syscall_error(index, "close()");
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen index->fd = -1;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen ret = mail_index_try_open_only(index);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (ret <= 0) {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (ret == 0) {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* the file was lost */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen errno = ENOENT;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen mail_index_set_syscall_error(index, "open()");
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen return -1;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* Too many ESTALE retries */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen mail_index_set_syscall_error(index, "read_map()");
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen return -1;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen}
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainenstatic int mail_index_map_try_existing(struct mail_index_map *map)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen{
19593623a2278039be23fc93e3404c012920b664Timo Sirainen const struct mail_index_header *hdr;
19593623a2278039be23fc93e3404c012920b664Timo Sirainen size_t used_size;
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen if (MAIL_INDEX_MAP_IS_IN_MEMORY(map))
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen return 0;
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen hdr = map->mmap_base;
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen /* always check corrupted-flag to avoid errors later */
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen if ((hdr->flags & MAIL_INDEX_HDR_FLAG_CORRUPTED) != 0)
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen return -1;
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen used_size = hdr->header_size + hdr->messages_count * hdr->record_size;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if (map->mmap_size >= used_size && map->hdr_base == hdr) {
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen map->records_count = hdr->messages_count;
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen mail_index_map_copy_hdr(map, hdr);
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen return 1;
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen }
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen return 0;
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen}
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainenint mail_index_map(struct mail_index *index, int force)
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen{
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen struct mail_index_map *map;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen int ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen i_assert(!index->mapping);
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen i_assert(index->map == NULL || index->map->refcount > 0);
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen i_assert(index->lock_type != F_UNLCK);
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (MAIL_INDEX_IS_IN_MEMORY(index)) {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (index->map == NULL)
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen mail_index_create_in_memory(index, NULL);
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen return 1;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen }
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen index->mapping = TRUE;
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen if (!force && index->map != NULL) {
20c26f4fcf9ef87434761829cc209c2f84ff5716Timo Sirainen i_assert(index->hdr != NULL);
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen ret = mail_index_map_try_existing(index->map);
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen if (ret != 0) {
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen index->mapping = FALSE;
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen return ret;
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen }
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen if (index->lock_type == F_WRLCK) {
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen /* we're syncing, don't break the mapping */
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen index->mapping = FALSE;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen return 1;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen }
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen }
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen if (index->map != NULL && index->map->refcount > 1) {
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen /* this map is already used by some views and they may have
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen pointers into it. leave them and create a new mapping. */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (!index->mmap_disable) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen map = NULL;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen } else {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* create a copy of the mapping instead so we don't
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen have to re-read it */
d7095f3a4466fbb78b2d5eb3d322bc15a5b0ab1fTimo Sirainen map = mail_index_map_clone(index->map,
d7095f3a4466fbb78b2d5eb3d322bc15a5b0ab1fTimo Sirainen index->map->hdr.record_size);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen index->map->refcount--;
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen index->map = NULL;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen } else {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen map = index->map;
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen }
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen if (map == NULL) {
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen map = i_new(struct mail_index_map, 1);
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen map->refcount = 1;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen map->hdr_copy_buf =
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen buffer_create_dynamic(default_pool, sizeof(map->hdr));
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen } else if (MAIL_INDEX_MAP_IS_IN_MEMORY(map)) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen i_assert(!map->write_to_disk);
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen } else if (map->mmap_base != NULL) {
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen i_assert(map->buffer == NULL);
19593623a2278039be23fc93e3404c012920b664Timo Sirainen if (munmap(map->mmap_base, map->mmap_size) < 0)
19593623a2278039be23fc93e3404c012920b664Timo Sirainen mail_index_set_syscall_error(index, "munmap()");
19593623a2278039be23fc93e3404c012920b664Timo Sirainen map->mmap_base = NULL;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->hdr = NULL;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->map = NULL;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
3e4bbbceed7882b8dab558aa90913a491eb15cc6Timo Sirainen if (!index->mmap_disable)
3e4bbbceed7882b8dab558aa90913a491eb15cc6Timo Sirainen ret = mail_index_mmap(index, map);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen else
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen ret = mail_index_read_map_with_retry(index, &map, force);
20c26f4fcf9ef87434761829cc209c2f84ff5716Timo Sirainen i_assert(index->map == NULL);
20c26f4fcf9ef87434761829cc209c2f84ff5716Timo Sirainen
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen if (ret > 0) {
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen ret = mail_index_check_header(index, map);
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen if (ret < 0)
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen ret = 0;
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen else if (ret == 0) {
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen index->fsck = TRUE;
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen ret = 1;
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen if (ret <= 0) {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen mail_index_unmap_forced(index, map);
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen index->mapping = FALSE;
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen return ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen index->hdr = &map->hdr;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->map = map;
20c26f4fcf9ef87434761829cc209c2f84ff5716Timo Sirainen i_assert(map->hdr.messages_count == map->records_count);
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen index->mapping = FALSE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainenint mail_index_get_latest_header(struct mail_index *index,
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen struct mail_index_header *hdr_r)
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen{
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen size_t pos;
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen int ret;
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen if (!index->mmap_disable) {
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen ret = mail_index_map(index, FALSE);
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen if (ret > 0)
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen *hdr_r = *index->hdr;
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen else
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen memset(hdr_r, 0, sizeof(*hdr_r));
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen } else {
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen ret = mail_index_read_header(index, hdr_r, &pos);
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen }
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen return ret;
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen}
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainenstruct mail_index_map *
d7095f3a4466fbb78b2d5eb3d322bc15a5b0ab1fTimo Sirainenmail_index_map_clone(struct mail_index_map *map, uint32_t new_record_size)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct mail_index_map *mem_map;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen struct mail_index_header *hdr;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen struct mail_index_ext *extensions;
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen void *src, *dest;
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen size_t size, copy_size;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen unsigned int i, count;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen size = map->records_count * new_record_size;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mem_map = i_new(struct mail_index_map, 1);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mem_map->refcount = 1;
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen mem_map->buffer = buffer_create_dynamic(default_pool, size);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if (map->hdr.record_size == new_record_size)
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen buffer_append(mem_map->buffer, map->records, size);
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen else {
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen copy_size = I_MIN(map->hdr.record_size, new_record_size);
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen src = map->records;
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen for (i = 0; i < map->records_count; i++) {
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen dest = buffer_append_space_unsafe(mem_map->buffer,
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen new_record_size);
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen memcpy(dest, src, copy_size);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen src = PTR_OFFSET(src, map->hdr.record_size);
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen }
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mem_map->records = buffer_get_modifyable_data(mem_map->buffer, NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mem_map->records_count = map->records_count;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen mem_map->hdr_copy_buf =
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen buffer_create_dynamic(default_pool, map->hdr.header_size);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if (map->hdr.base_header_size < sizeof(*hdr))
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen buffer_append_zero(mem_map->hdr_copy_buf, sizeof(*hdr));
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen buffer_write(mem_map->hdr_copy_buf, 0,
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen &map->hdr, map->hdr.base_header_size);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen buffer_append(mem_map->hdr_copy_buf,
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen CONST_PTR_OFFSET(map->hdr_base,
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen map->hdr.base_header_size),
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen map->hdr.header_size - map->hdr.base_header_size);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen hdr = buffer_get_modifyable_data(mem_map->hdr_copy_buf, NULL);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if (hdr->base_header_size < sizeof(*hdr))
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen hdr->base_header_size = sizeof(*hdr);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen hdr->record_size = new_record_size;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen mem_map->hdr = *hdr;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen mem_map->hdr_base = hdr;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen /* copy extensions */
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen if (array_is_created(&map->ext_id_map)) {
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen count = array_count(&map->ext_id_map);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen mail_index_map_init_extbufs(mem_map, count + 2);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen array_append_array(&mem_map->extensions, &map->extensions);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen array_append_array(&mem_map->ext_id_map, &map->ext_id_map);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* fix the name pointers to use our own pool */
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen extensions = array_get_modifyable(&mem_map->extensions, &count);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen for (i = 0; i < count; i++) {
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen extensions[i].name = p_strdup(mem_map->extension_pool,
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen extensions[i].name);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return mem_map;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenint mail_index_map_get_ext_idx(struct mail_index_map *map,
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen uint32_t ext_id, uint32_t *idx_r)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen{
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen const uint32_t *id;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen if (!array_is_created(&map->ext_id_map) ||
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen ext_id >= array_count(&map->ext_id_map))
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return 0;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen id = array_idx(&map->ext_id_map, ext_id);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen *idx_r = *id;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return *idx_r != (uint32_t)-1;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen}
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenstatic int mail_index_try_open_only(struct mail_index *index)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen{
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen int i;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen i_assert(!MAIL_INDEX_IS_IN_MEMORY(index));
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen for (i = 0; i < 3; i++) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen index->fd = open(index->filepath, O_RDWR);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (index->fd == -1 && errno == EACCES) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen index->fd = open(index->filepath, O_RDONLY);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen index->readonly = TRUE;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen }
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (index->fd != -1 || errno != ESTALE)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen break;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* May happen with some OSes with NFS. Try again, although
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen there's still a race condition with another computer
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen creating the index file again. However, we can't try forever
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen as ESTALE happens also if index directory has been deleted
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen from server.. */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen }
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (index->fd == -1) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (errno != ENOENT)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return mail_index_set_syscall_error(index, "open()");
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* have to create it */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return 0;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen }
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return 1;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen}
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenstatic int
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenmail_index_try_open(struct mail_index *index, unsigned int *lock_id_r)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen{
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen unsigned int lock_id;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen int ret;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (lock_id_r != NULL)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen *lock_id_r = 0;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (MAIL_INDEX_IS_IN_MEMORY(index))
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen return 0;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen ret = mail_index_try_open_only(index);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (ret <= 0)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return ret;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
c9f1a617593eb569fb02f45041bad3c13e534496Timo Sirainen if (mail_index_lock_shared(index, FALSE, &lock_id) < 0) {
c9f1a617593eb569fb02f45041bad3c13e534496Timo Sirainen (void)close(index->fd);
c9f1a617593eb569fb02f45041bad3c13e534496Timo Sirainen index->fd = -1;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return -1;
c9f1a617593eb569fb02f45041bad3c13e534496Timo Sirainen }
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen ret = mail_index_map(index, FALSE);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (ret == 0) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* it's corrupted - recreate it */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mail_index_unlock(index, lock_id);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (lock_id_r != NULL)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen *lock_id_r = 0;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen (void)close(index->fd);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen index->fd = -1;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen } else {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (lock_id_r != NULL)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen *lock_id_r = lock_id;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen else
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mail_index_unlock(index, lock_id);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen }
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return ret;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen}
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainenint mail_index_write_base_header(struct mail_index *index,
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen const struct mail_index_header *hdr)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen size_t hdr_size;
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen hdr_size = I_MIN(sizeof(*hdr), hdr->base_header_size);
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (!MAIL_INDEX_MAP_IS_IN_MEMORY(index->map)) {
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen memcpy(index->map->mmap_base, hdr, hdr_size);
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen if (msync(index->map->mmap_base, hdr_size, MS_SYNC) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return mail_index_set_syscall_error(index, "msync()");
ea9e10039e23a4926984a56860b5cc88f7b1f75eTimo Sirainen index->map->hdr = *hdr;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (!MAIL_INDEX_IS_IN_MEMORY(index)) {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (pwrite_full(index->fd, hdr, hdr_size, 0) < 0) {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen mail_index_set_syscall_error(index,
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen "pwrite_full()");
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen return -1;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
2bc9f8181d576165cd30ce0522754420097037cdTimo Sirainen index->map->hdr = *hdr;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen buffer_write(index->map->hdr_copy_buf, 0, hdr, hdr_size);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint mail_index_create_tmp_file(struct mail_index *index, const char **path_r)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
d22390f33eedbd2413debabc0662dde5241b1aa6Timo Sirainen mode_t old_mask;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *path;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen int fd;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen i_assert(!MAIL_INDEX_IS_IN_MEMORY(index));
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen path = *path_r = t_strconcat(index->filepath, ".tmp", NULL);
d22390f33eedbd2413debabc0662dde5241b1aa6Timo Sirainen old_mask = umask(0);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen fd = open(path, O_RDWR|O_CREAT|O_TRUNC, index->mode);
d22390f33eedbd2413debabc0662dde5241b1aa6Timo Sirainen umask(old_mask);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (fd == -1)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return mail_index_file_set_syscall_error(index, path, "open()");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
d22390f33eedbd2413debabc0662dde5241b1aa6Timo Sirainen if (index->gid != (gid_t)-1 && fchown(fd, (uid_t)-1, index->gid) < 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_file_set_syscall_error(index, path, "fchown()");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return fd;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainenstatic int mail_index_create(struct mail_index *index,
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainen struct mail_index_header *hdr)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *path;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen uint32_t seq;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen uoff_t offset;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen int ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen i_assert(!MAIL_INDEX_IS_IN_MEMORY(index));
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen i_assert(index->lock_type == F_UNLCK);
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* log file lock protects index creation */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mail_transaction_log_sync_lock(index->log, &seq, &offset) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen ret = mail_index_try_open(index, NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ret != 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_transaction_log_sync_unlock(index->log);
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen return ret < 0 ? -1 : 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
9fc077d3886b1584019bcc9b92c717cfffce5c67Timo Sirainen /* mark the existing log file as synced */
9fc077d3886b1584019bcc9b92c717cfffce5c67Timo Sirainen hdr->log_file_seq = seq;
9fc077d3886b1584019bcc9b92c717cfffce5c67Timo Sirainen hdr->log_file_int_offset = offset;
9fc077d3886b1584019bcc9b92c717cfffce5c67Timo Sirainen hdr->log_file_ext_offset = offset;
9fc077d3886b1584019bcc9b92c717cfffce5c67Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* create it fully in index.tmp first */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->fd = mail_index_create_tmp_file(index, &path);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (index->fd == -1)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen else if (write_full(index->fd, hdr, sizeof(*hdr)) < 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_file_set_syscall_error(index, path, "write_full()");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else {
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen index->lock_type = F_WRLCK;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = mail_index_map(index, FALSE);
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen index->lock_type = F_UNLCK;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ret == 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* it's corrupted even while we just created it,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen should never happen unless someone pokes the file directly */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_set_error(index,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "Newly created index file is corrupted: %s", path);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ret < 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (unlink(path) < 0 && errno != ENOENT) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_file_set_syscall_error(index, path,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "unlink()");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainen } else {
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainen /* make it visible to others */
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainen if (rename(path, index->filepath) < 0) {
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainen mail_index_set_error(index, "rename(%s, %s) failed: %m",
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainen path, index->filepath);
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainen ret = -1;
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_transaction_log_sync_unlock(index->log);
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainen return ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainenstatic void mail_index_header_init(struct mail_index_header *hdr)
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen{
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen time_t now = time(NULL);
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen
264bc938e956288ec7a2a43417c01c3a3ce840fdTimo Sirainen i_assert((sizeof(*hdr) % sizeof(uint64_t)) == 0);
264bc938e956288ec7a2a43417c01c3a3ce840fdTimo Sirainen
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen memset(hdr, 0, sizeof(*hdr));
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen hdr->major_version = MAIL_INDEX_MAJOR_VERSION;
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen hdr->minor_version = MAIL_INDEX_MINOR_VERSION;
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen hdr->base_header_size = sizeof(*hdr);
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen hdr->header_size = sizeof(*hdr);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen hdr->record_size = sizeof(struct mail_index_record);
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen#ifndef WORDS_BIGENDIAN
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen hdr->compat_data[0] = MAIL_INDEX_COMPAT_LITTLE_ENDIAN;
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen#endif
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen hdr->compat_data[1] = sizeof(uoff_t);
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen hdr->compat_data[2] = sizeof(time_t);
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen hdr->indexid = now;
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen hdr->next_uid = 1;
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen}
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainenstatic void mail_index_create_in_memory(struct mail_index *index,
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen const struct mail_index_header *hdr)
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen{
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen struct mail_index_header tmp_hdr;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen struct mail_index_map tmp_map;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (hdr == NULL) {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen mail_index_header_init(&tmp_hdr);
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen hdr = &tmp_hdr;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen }
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen memset(&tmp_map, 0, sizeof(tmp_map));
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen tmp_map.hdr = *hdr;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen tmp_map.hdr_base = hdr;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen /* a bit kludgy way to do this, but it initializes everything
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen nicely and correctly */
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen index->map = mail_index_map_clone(&tmp_map, hdr->record_size);
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen index->hdr = &index->map->hdr;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen}
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen/* returns -1 = error, 0 = won't create, 1 = ok */
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainenstatic int mail_index_open_files(struct mail_index *index,
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainen enum mail_index_open_flags flags)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen struct mail_index_header hdr;
40ef82c46f6652412b068ebcdac7c3e74840a284Timo Sirainen unsigned int lock_id = 0;
f239eb76f77afcbc0bfc97c9b52b4407d1bc3fe6Timo Sirainen int ret, created = FALSE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
40ef82c46f6652412b068ebcdac7c3e74840a284Timo Sirainen ret = mail_index_try_open(index, &lock_id);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (ret > 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen hdr = *index->hdr;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen else if (ret == 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* doesn't exist, or corrupted */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if ((flags & MAIL_INDEX_OPEN_FLAG_CREATE) == 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 0;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen mail_index_header_init(&hdr);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->hdr = &hdr;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else if (ret < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->indexid = hdr.indexid;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->log = mail_transaction_log_open_or_create(index);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (index->log == NULL)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen if (index->fd == -1) {
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen if (index->indexid != hdr.indexid) {
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen /* looks like someone else created the transaction log
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen before we had the chance. use its indexid so we
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen don't try to create conflicting ones. */
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen hdr.indexid = index->indexid;
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen }
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen if (lock_id != 0) {
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen mail_index_unlock(index, lock_id);
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen lock_id = 0;
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen }
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (!MAIL_INDEX_IS_IN_MEMORY(index)) {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (mail_index_create(index, &hdr) < 0)
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen return -1;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen } else {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen mail_index_create_in_memory(index, &hdr);
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen }
f239eb76f77afcbc0bfc97c9b52b4407d1bc3fe6Timo Sirainen created = TRUE;
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen }
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen if (lock_id == 0) {
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen if (mail_index_lock_shared(index, FALSE, &lock_id) < 0)
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen return -1;
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen }
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen
f239eb76f77afcbc0bfc97c9b52b4407d1bc3fe6Timo Sirainen index->cache = created ? mail_cache_create(index) :
f239eb76f77afcbc0bfc97c9b52b4407d1bc3fe6Timo Sirainen mail_cache_open_or_create(index);
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen mail_index_unlock(index, lock_id);
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen return 1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
a53cb86b4d733d9c48ee4d285bed477c80825804Timo Sirainenint mail_index_open(struct mail_index *index, enum mail_index_open_flags flags,
a53cb86b4d733d9c48ee4d285bed477c80825804Timo Sirainen enum mail_index_lock_method lock_method)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen int i = 0, ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen if (index->opened) {
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen if (index->hdr != NULL &&
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen (index->hdr->flags & MAIL_INDEX_HDR_FLAG_CORRUPTED) != 0) {
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen /* corrupted, reopen files */
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen mail_index_close(index);
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen } else {
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen return 0;
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen }
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen index->filepath = MAIL_INDEX_IS_IN_MEMORY(index) ?
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen i_strdup("(in-memory index)") :
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen i_strconcat(index->dir, "/", index->prefix, NULL);
ee246b46953e4b94b2f22e093373674fa9155500Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen do {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->shared_lock_count = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->excl_lock_count = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->lock_type = F_UNLCK;
ec1e30ecc38f0deddaf655413cf02d5972ddbc70Timo Sirainen index->lock_id = 2;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
a53cb86b4d733d9c48ee4d285bed477c80825804Timo Sirainen index->readonly = FALSE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->nodiskspace = FALSE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->index_lock_timeout = FALSE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->log_locked = FALSE;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen index->mmap_disable =
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen (flags & MAIL_INDEX_OPEN_FLAG_MMAP_DISABLE) != 0;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen index->mmap_no_write =
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen (flags & MAIL_INDEX_OPEN_FLAG_MMAP_NO_WRITE) != 0;
a53cb86b4d733d9c48ee4d285bed477c80825804Timo Sirainen index->lock_method = lock_method;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen /* don't even bother to handle dotlocking without mmap being
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen disabled. that combination simply doesn't make any sense */
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen if (lock_method == MAIL_INDEX_LOCK_DOTLOCK &&
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen !index->mmap_disable) {
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen i_fatal("lock_method=dotlock and mmap_disable=no "
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen "combination isn't supported. "
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen "You don't _really_ want it anyway.");
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen }
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainen ret = mail_index_open_files(index, flags);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ret <= 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen break;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->opened = TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (index->fsck) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->fsck = FALSE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = mail_index_fsck(index);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ret == 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* completely broken, reopen */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (i++ < 3)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen continue;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* too many tries */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen break;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } while (1);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ret <= 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_close(index);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mail_index_close(struct mail_index *index)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (index->log != NULL) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_transaction_log_close(index->log);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->log = NULL;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (index->map != NULL) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen mail_index_unmap(index, index->map);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen index->map = NULL;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen if (index->cache != NULL) {
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen mail_cache_free(index->cache);
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen index->cache = NULL;
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen }
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (index->fd != -1) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (close(index->fd) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_set_syscall_error(index, "close()");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->fd = -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_free(index->copy_lock_path);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen index->copy_lock_path = NULL;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_free(index->filepath);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->filepath = NULL;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->indexid = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->opened = FALSE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenint mail_index_reopen(struct mail_index *index, int fd)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen{
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen struct mail_index_map *old_map;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen unsigned int old_shared_locks, old_lock_id, lock_id = 0;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen int ret, old_fd, old_lock_type;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen i_assert(!MAIL_INDEX_IS_IN_MEMORY(index));
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen old_map = index->map;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen old_fd = index->fd;
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen old_map->refcount++;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
325d4ad220bd13f6d176391d962a0e33c856a7f6Timo Sirainen /* new file, new locks. the old fd can keep its locks, they don't
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen matter anymore as no-one's going to modify the file. */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen old_lock_type = index->lock_type;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen old_lock_id = index->lock_id;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen old_shared_locks = index->shared_lock_count;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (index->lock_type == F_RDLCK)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen index->lock_type = F_UNLCK;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen index->lock_id += 2;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen index->shared_lock_count = 0;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (fd != -1) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen index->fd = fd;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen ret = 0;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen } else {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen i_assert(index->excl_lock_count == 0);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen ret = mail_index_try_open_only(index);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (ret > 0)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen ret = mail_index_lock_shared(index, FALSE, &lock_id);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen else if (ret == 0) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* index file is lost */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen ret = -1;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen }
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen }
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (ret == 0) {
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen /* read the new mapping. note that with mmap_disable we want
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen to keep the old mapping in index->map so we can update it
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen by reading transaction log. */
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen if (mail_index_map(index, TRUE) <= 0)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen ret = -1;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen }
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (lock_id != 0)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mail_index_unlock(index, lock_id);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (ret == 0) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mail_index_unmap(index, old_map);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (close(old_fd) < 0)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mail_index_set_syscall_error(index, "close()");
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen } else {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (index->map != NULL)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mail_index_unmap(index, index->map);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (index->fd != -1) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (close(index->fd) < 0)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mail_index_set_syscall_error(index, "close()");
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen }
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen index->map = old_map;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen index->hdr = &index->map->hdr;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen index->fd = old_fd;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen index->lock_type = old_lock_type;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen index->lock_id = old_lock_id;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen index->shared_lock_count = old_shared_locks;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen }
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return ret;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen}
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainenint mail_index_reopen_if_needed(struct mail_index *index)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen{
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen struct stat st1, st2;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (MAIL_INDEX_IS_IN_MEMORY(index))
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen return 0;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (fstat(index->fd, &st1) < 0)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return mail_index_set_syscall_error(index, "fstat()");
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (stat(index->filepath, &st2) < 0) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mail_index_set_syscall_error(index, "stat()");
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (errno != ENOENT)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return -1;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* lost it? recreate */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen (void)mail_index_mark_corrupted(index);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return -1;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen }
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (st1.st_ino != st2.st_ino ||
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen !CMP_DEV_T(st1.st_dev, st2.st_dev)) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (mail_index_reopen(index, -1) < 0)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return -1;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return 1;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen } else {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return 0;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen }
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen}
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainenint mail_index_refresh(struct mail_index *index)
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen{
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen unsigned int lock_id;
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen int ret;
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (MAIL_INDEX_IS_IN_MEMORY(index))
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen return 0;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen if (index->excl_lock_count > 0) {
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen /* we have index exclusively locked, nothing could
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen have changed. */
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen return 0;
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen }
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen if (!index->mmap_disable) {
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen /* reopening is all we need */
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen return mail_index_reopen_if_needed(index);
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen }
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen
321241fc56909ccb69d77c6a11686e0c25b4c4d4Timo Sirainen i_assert(!index->mapping);
321241fc56909ccb69d77c6a11686e0c25b4c4d4Timo Sirainen
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen /* mail_index_map() simply reads latest changes from transaction log,
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen which makes us fully refreshed. */
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen if (mail_index_lock_shared(index, TRUE, &lock_id) < 0)
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen return -1;
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen ret = mail_index_map(index, FALSE);
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen mail_index_unlock(index, lock_id);
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen return ret <= 0 ? -1 : 0;
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen}
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstruct mail_cache *mail_index_get_cache(struct mail_index *index)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return index->cache;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint mail_index_set_error(struct mail_index *index, const char *fmt, ...)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen va_list va;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_free(index->error);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (fmt == NULL)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->error = NULL;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen else {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen va_start(va, fmt);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->error = i_strdup_vprintf(fmt, va);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen va_end(va);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_error("%s", index->error);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenvoid mail_index_set_inconsistent(struct mail_index *index)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen{
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen index->indexid = 0;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen}
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
87712707722ef7d73acb065546e61afa4455cd9eTimo Sirainenvoid mail_index_mark_corrupted(struct mail_index *index)
5ec0ca7ff13595daf0d096c17100afb352e6294aTimo Sirainen{
5ec0ca7ff13595daf0d096c17100afb352e6294aTimo Sirainen struct mail_index_header hdr;
5ec0ca7ff13595daf0d096c17100afb352e6294aTimo Sirainen
87712707722ef7d73acb065546e61afa4455cd9eTimo Sirainen mail_index_set_inconsistent(index);
87712707722ef7d73acb065546e61afa4455cd9eTimo Sirainen
5ec0ca7ff13595daf0d096c17100afb352e6294aTimo Sirainen if (index->readonly)
87712707722ef7d73acb065546e61afa4455cd9eTimo Sirainen return;
5ec0ca7ff13595daf0d096c17100afb352e6294aTimo Sirainen
5ec0ca7ff13595daf0d096c17100afb352e6294aTimo Sirainen hdr = *index->hdr;
5ec0ca7ff13595daf0d096c17100afb352e6294aTimo Sirainen hdr.flags |= MAIL_INDEX_HDR_FLAG_CORRUPTED;
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen if (mail_index_write_base_header(index, &hdr) == 0) {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (!MAIL_INDEX_IS_IN_MEMORY(index) && fsync(index->fd) < 0)
87712707722ef7d73acb065546e61afa4455cd9eTimo Sirainen mail_index_set_syscall_error(index, "fsync()");
87712707722ef7d73acb065546e61afa4455cd9eTimo Sirainen }
5ec0ca7ff13595daf0d096c17100afb352e6294aTimo Sirainen}
5ec0ca7ff13595daf0d096c17100afb352e6294aTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint mail_index_set_syscall_error(struct mail_index *index,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *function)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_assert(function != NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ENOSPACE(errno)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->nodiskspace = TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return mail_index_set_error(index, "%s failed with index file %s: %m",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen function, index->filepath);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint mail_index_file_set_syscall_error(struct mail_index *index,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *filepath,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *function)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_assert(filepath != NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_assert(function != NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ENOSPACE(errno)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->nodiskspace = TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return mail_index_set_error(index, "%s failed with file %s: %m",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen function, filepath);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenenum mail_index_error mail_index_get_last_error(struct mail_index *index)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (index->nodiskspace)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return MAIL_INDEX_ERROR_DISKSPACE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (index->error != NULL)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return MAIL_INDEX_ERROR_INTERNAL;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return MAIL_INDEX_ERROR_NONE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenconst char *mail_index_get_error_message(struct mail_index *index)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return index->error;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mail_index_reset_error(struct mail_index *index)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (index->error != NULL) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_free(index->error);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->error = NULL;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->nodiskspace = FALSE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->index_lock_timeout = FALSE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainenuint32_t mail_index_uint32_to_offset(uint32_t offset)
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen{
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen unsigned char buf[4];
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen i_assert(offset < 0x40000000);
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen i_assert((offset & 3) == 0);
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen offset >>= 2;
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen buf[0] = 0x80 | ((offset & 0x0fe00000) >> 21);
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen buf[1] = 0x80 | ((offset & 0x001fc000) >> 14);
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen buf[2] = 0x80 | ((offset & 0x00003f80) >> 7);
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen buf[3] = 0x80 | (offset & 0x0000007f);
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen return *((uint32_t *) buf);
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen}
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainenuint32_t mail_index_offset_to_uint32(uint32_t offset)
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen{
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen const unsigned char *buf = (const unsigned char *) &offset;
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen if ((offset & 0x80808080) != 0x80808080)
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen return 0;
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen return (((uint32_t)buf[3] & 0x7f) << 2) |
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen (((uint32_t)buf[2] & 0x7f) << 9) |
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen (((uint32_t)buf[1] & 0x7f) << 16) |
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen (((uint32_t)buf[0] & 0x7f) << 23);
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen}