mail-index.c revision 190237ce467d2389dfb809874b0fec86d3c7968d
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (C) 2003-2004 Timo 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,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstruct mail_index *mail_index_alloc(const char *dir, const char *prefix)
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen index->extension_pool = pool_alloconly_create("extension", 512);
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainen p_array_init(&index->extensions, index->extension_pool, 5);
26a8b7deb3a5b6f26f9c4d71538e1248f680e4beTimo Sirainen array_create(&index->mail_index_module_contexts, default_pool,
26a8b7deb3a5b6f26f9c4d71538e1248f680e4beTimo Sirainen sizeof(void *), I_MIN(5, mail_index_module_id));
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen mail_index_ext_register(index, "keywords", 128, 2, 1);
41e1c7380edda701719d8ce1fb4d465d2ec4c84dTimo Sirainen index->keywords_pool = pool_alloconly_create("keywords", 512);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen hash_create(default_pool, index->keywords_pool, 0,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen strcase_hash, (hash_cmp_callback_t *)strcasecmp);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainenvoid mail_index_free(struct mail_index **_index)
7d7b5c98f086ffa8ac9c90f21db17748ca607202Timo Sirainen array_free(&index->mail_index_module_contexts);
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainenvoid mail_index_set_permissions(struct mail_index *index,
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenuint32_t mail_index_ext_register(struct mail_index *index, const char *name,
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen const struct mail_index_registered_ext *extensions;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen unsigned int i, ext_count;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen extensions = array_get(&index->extensions, &ext_count);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen /* see if it's already there */
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen for (i = 0; i < ext_count; i++) {
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen rext.name = p_strdup(index->extension_pool, name);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainenvoid mail_index_register_expunge_handler(struct mail_index *index,
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen rext = array_idx_modifiable(&index->extensions, ext_id);
7d7b5c98f086ffa8ac9c90f21db17748ca607202Timo Sirainen i_assert(rext->expunge_handler == NULL || rext->expunge_handler == cb);
da985034a708db2f61394b30d117050ae6829ee5Timo Sirainen rext->expunge_handler_call_always = call_always;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainenvoid mail_index_unregister_expunge_handler(struct mail_index *index,
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen rext = array_idx_modifiable(&index->extensions, ext_id);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainenvoid mail_index_register_sync_handler(struct mail_index *index, uint32_t ext_id,
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen rext = array_idx_modifiable(&index->extensions, ext_id);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen i_assert(rext->sync_handler.callback == NULL);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainenvoid mail_index_unregister_sync_handler(struct mail_index *index,
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen rext = array_idx_modifiable(&index->extensions, ext_id);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen i_assert(rext->sync_handler.callback != NULL);
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainenvoid mail_index_register_sync_lost_handler(struct mail_index *index,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen array_append(&index->sync_lost_handlers, &cb, 1);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainenvoid mail_index_unregister_sync_lost_handler(struct mail_index *index,
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen mail_index_sync_lost_handler_t *const *handlers;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen unsigned int i, count;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen handlers = array_get(&index->sync_lost_handlers, &count);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen for (i = 0; i < count; i++) {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen array_delete(&index->sync_lost_handlers, i, 1);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenstatic void mail_index_map_init_extbufs(struct mail_index_map *map,
003079baf8056c81c25162a176a89f9169cc374fTimo Sirainen ((sizeof(map->extensions) + BUFFER_APPROX_SIZE) * 2)
003079baf8056c81c25162a176a89f9169cc374fTimo Sirainen sizeof(struct mail_index_ext) + sizeof(uint32_t))
003079baf8056c81c25162a176a89f9169cc374fTimo Sirainen /* try to use the existing pool's size for initial_count so
003079baf8056c81c25162a176a89f9169cc374fTimo Sirainen we don't grow it unneededly */
003079baf8056c81c25162a176a89f9169cc374fTimo Sirainen size = p_get_max_easy_alloc_size(map->extension_pool);
003079baf8056c81c25162a176a89f9169cc374fTimo Sirainen if (size > EXT_GLOBAL_ALLOC_SIZE + EXT_PER_ALLOC_SIZE) {
003079baf8056c81c25162a176a89f9169cc374fTimo Sirainen initial_count = (size - EXT_GLOBAL_ALLOC_SIZE) /
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainen p_array_init(&map->extensions, map->extension_pool, initial_count);
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainen p_array_init(&map->ext_id_map, map->extension_pool, initial_count);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenuint32_t mail_index_map_lookup_ext(struct mail_index_map *map, const char *name)
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen unsigned int i, size;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen extensions = array_get(&map->extensions, &size);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen for (i = 0; i < size; i++) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenmail_index_map_register_ext(struct mail_index *index,
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen i_assert(mail_index_map_lookup_ext(map, name) == (uint32_t)-1);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen ext->name = p_strdup(map->extension_pool, name);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen ext->index_idx = mail_index_ext_register(index, name, hdr_size,
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen /* Update index ext_id -> map ext_id mapping. Fill non-used
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen ext_ids with (uint32_t)-1 */
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);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainenstatic bool size_check(size_t *size_left, size_t size)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen size_t size = sizeof(struct mail_index_ext_header) + name_len;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return MAIL_INDEX_HEADER_SIZE_ALIGN(size) - size;
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainenstatic int mail_index_parse_extensions(struct mail_index *index,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen unsigned int i, old_count;
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 */
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen mail_index_map_init_extbufs(map, old_count + 5);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen for (i = 0; i < old_count; i++)
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen ext_hdr = CONST_PTR_OFFSET(map->hdr_base, offset);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen /* Extension header contains:
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen - struct mail_index_ext_header
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen - name (not 0-terminated)
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen - 64bit alignment padding
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen - extension header contents
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen - 64bit alignment padding
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)) ||
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen "Header extension goes outside header",
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen offset += ext_hdr->name_size + get_align(ext_hdr->name_size);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen name = t_strndup(CONST_PTR_OFFSET(map->hdr_base, name_offset),
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",
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen ext_hdr->record_offset + ext_hdr->record_size) {
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen "Record field %s points outside record size "
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen ext_hdr->record_offset, ext_hdr->record_size);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if ((ext_hdr->record_offset % ext_hdr->record_align) != 0 ||
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen (map->hdr.record_size % ext_hdr->record_align) != 0) {
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen "Record field %s alignmentation %u not used",
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen index->filepath, name, ext_hdr->record_align);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen offset += MAIL_INDEX_HEADER_SIZE_ALIGN(ext_hdr->hdr_size);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenbool mail_index_keyword_lookup(struct mail_index *index,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen unsigned int *idx_r)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* keywords_hash keeps a name => index mapping of keywords.
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen Keywords are never removed from it, so the index values are valid
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen for the lifetime of the mail_index. */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (hash_lookup_full(index->keywords_hash, keyword, NULL, &value)) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen *idx_r = POINTER_CAST_TO(value, unsigned int);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen keyword = keyword_dup = p_strdup(index->keywords_pool, keyword);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen hash_insert(index->keywords_hash, keyword_dup, POINTER_CAST(*idx_r));
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenint mail_index_map_parse_keywords(struct mail_index *index,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen const struct mail_index_keyword_header *kw_hdr;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen const struct mail_index_keyword_header_rec *kw_rec;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen unsigned int i, name_area_end_offset, old_count;
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen ext_id = mail_index_map_lookup_ext(map, "keywords");
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* Extension header contains:
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen - struct mail_index_keyword_header
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen - struct mail_index_keyword_header_rec * keywords_count
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen - const char names[] * keywords_count
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen i_assert(ext->hdr_offset < map->hdr.header_size);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen kw_hdr = CONST_PTR_OFFSET(map->hdr_base, ext->hdr_offset);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen name = (const char *)(kw_rec + kw_hdr->keywords_count);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen old_count = !array_is_created(&map->keyword_idx_map) ? 0 :
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* Keywords can only be added into same mapping. Removing requires a
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen new mapping (recreating the index file) */
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen /* nothing changed */
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen /* make sure the header is valid */
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen "Keywords removed unexpectedly",
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 name_area_end_offset = (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_area_end_offset) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen "name_offset points outside allocated header",
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen "Keyword header doesn't end with NUL",
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* create file -> index mapping */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen i_array_init(&map->keyword_idx_map, kw_hdr->keywords_count);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* Check that existing headers are still the same. It's behind DEBUG
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen since it's pretty useless waste of CPU normally. */
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;
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen unsigned int idx;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen old_idx = array_idx(&map->keyword_idx_map, i);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (!mail_index_keyword_lookup(index, keyword, FALSE, &idx) ||
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen "Keywords changed unexpectedly",
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen /* Register the newly seen keywords */
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen const char *keyword = name + kw_rec[i].name_offset;
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen unsigned int idx;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen (void)mail_index_keyword_lookup(index, keyword, TRUE, &idx);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainenconst ARRAY_TYPE(keywords) *mail_index_get_keywords(struct mail_index *index)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* Make sure all the keywords are in index->keywords. It's quick to do
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if nothing has changed. */
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen (void)mail_index_map_parse_keywords(index, index->map);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int mail_index_check_header(struct mail_index *index,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const struct mail_index_header *hdr = &map->hdr;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen enum mail_index_header_compat_flags compat_flags = 0;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen compat_flags |= MAIL_INDEX_COMPAT_LITTLE_ENDIAN;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (hdr->major_version != MAIL_INDEX_MAJOR_VERSION) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* major version change - handle silently(?) */
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen /* architecture change - handle silently(?) */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if ((map->hdr.flags & MAIL_INDEX_HDR_FLAG_CORRUPTED) != 0) {
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen /* we've already complained about it */
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen /* following some extra checks that only take a bit of CPU */
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen if (hdr->uid_validity == 0 && hdr->next_uid != 1) {
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen "uid_validity = 0, next_uid = %u",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (hdr->record_size < sizeof(struct mail_index_record)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen sizeof(struct mail_index_record));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if ((hdr->flags & MAIL_INDEX_HDR_FLAG_FSCK) != 0)
5e69206e0b13bbe3dbc50c5dc6c58da007402237Timo Sirainen if (hdr->recent_messages_count > hdr->messages_count ||
5e69206e0b13bbe3dbc50c5dc6c58da007402237Timo Sirainen hdr->seen_messages_count > hdr->messages_count ||
5e69206e0b13bbe3dbc50c5dc6c58da007402237Timo Sirainen hdr->deleted_messages_count > hdr->messages_count)
5e69206e0b13bbe3dbc50c5dc6c58da007402237Timo Sirainen if (hdr->first_recent_uid_lowwater > hdr->next_uid ||
5e69206e0b13bbe3dbc50c5dc6c58da007402237Timo Sirainen hdr->first_unseen_uid_lowwater > hdr->next_uid ||
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen hdr->first_deleted_uid_lowwater > hdr->next_uid)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* last message's UID must be smaller than next_uid.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen also make sure it's not zero. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen rec = MAIL_INDEX_MAP_IDX(map, map->records_count-1);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (rec->uid == 0 || rec->uid >= hdr->next_uid)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return mail_index_parse_extensions(index, map);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenstatic void mail_index_map_clear(struct mail_index *index,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (munmap(map->mmap_base, map->mmap_size) < 0)
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen mail_index_set_syscall_error(index, "munmap()");
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenvoid mail_index_unmap(struct mail_index *index, struct mail_index_map **_map)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainenstatic void mail_index_map_copy_hdr(struct mail_index_map *map,
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (hdr->base_header_size < sizeof(map->hdr)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* header smaller than ours, make a copy so our newer headers
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen won't have garbage in them */
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen memcpy(&map->hdr, hdr, hdr->base_header_size);
5a4ab3d6e108a899c8b51bebd0094a37b738d5a1Timo Sirainenstatic int mail_index_mmap(struct mail_index *index, struct mail_index_map *map)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* we had temporarily used a buffer, eg. for updating index */
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen mail_index_set_syscall_error(index, "mmap()");
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen offsetof(struct mail_index_header, major_version) &&
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen hdr->major_version != MAIL_INDEX_MAJOR_VERSION) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen /* major version change - handle silently */
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (map->mmap_size < MAIL_INDEX_HEADER_MIN_SIZE) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen records_count = (map->mmap_size - hdr->header_size) /
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen "messages_count too large (%u > %u)",
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen map->records = PTR_OFFSET(map->mmap_base, map->hdr.header_size);
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainenstatic int mail_index_read_header(struct mail_index *index,
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen memset(buf, 0, sizeof(struct mail_index_header));
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* try to read the whole header, but it's not necessarily an error to
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen read less since the older versions of the index format could be
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen smaller. Request reading up to buf_size, but accept if we only got
c485524d09c650ff6e6c552129d4257ac6145a8bTimo Sirainen the header. */
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen } while (ret > 0 && pos < sizeof(struct mail_index_header));
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainenmail_index_read_map(struct mail_index *index, struct mail_index_map *map,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen ret = mail_index_read_header(index, buf, sizeof(buf), &pos);
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen if (pos > (ssize_t)offsetof(struct mail_index_header, major_version) &&
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen hdr->major_version != MAIL_INDEX_MAJOR_VERSION) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* major version change - handle silently */
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if (ret >= 0 && pos >= MAIL_INDEX_HEADER_MIN_SIZE &&
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen if (hdr->base_header_size < MAIL_INDEX_HEADER_MIN_SIZE ||
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen "Corrupted header sizes (base %u, full %u)",
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* place the base header into memory. */
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen /* @UNSAFE: read the rest of the header into memory */
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen data = buffer_append_space_unsafe(map->hdr_copy_buf,
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen /* header read, read the records now. */
28dd1eeb9fdb93051bb5899c80cf300d69ba9f9eTimo Sirainen records_size = hdr->messages_count * hdr->record_size;
c485524d09c650ff6e6c552129d4257ac6145a8bTimo Sirainen map->buffer = buffer_create_dynamic(default_pool,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* @UNSAFE */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen data = buffer_append_space_unsafe(map->buffer, records_size);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen ret = pread_full(index->fd, data, records_size,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* a new index file was renamed over this one. */
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen mail_index_set_syscall_error(index, "pread_full()");
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen "Corrupted index file %s: File too small",
fea541eec46707f9b01bd3cbc981d73c1e808a54Timo Sirainen index->sync_log_file_offset = hdr->log_file_int_offset;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenbool mail_index_is_ext_synced(struct mail_transaction_log_view *log_view,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen mail_transaction_log_view_get_prev_pos(log_view, &prev_seq,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenstatic int mail_index_sync_from_transactions(struct mail_index *index,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen const struct mail_index_header *map_hdr = &(*map)->hdr;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* read the real log position where we are supposed to be
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen ret = mail_index_read_header(index, &hdr, sizeof(hdr), &pos);
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen mail_index_set_syscall_error(index, "pread()");
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen if (map_hdr->log_file_seq == hdr.log_file_seq &&
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen map_hdr->log_file_int_offset == hdr.log_file_int_offset) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* nothing to do */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (map_hdr->log_file_seq > hdr.log_file_seq ||
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo 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 hdr.log_file_ext_offset != hdr.log_file_int_offset) {
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen /* too much trouble to get this right. */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* sync everything there is */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen log_view = mail_transaction_log_view_open(index->log);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* can't use it. sync by re-reading index. */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen while ((ret = mail_transaction_log_view_next(log_view, &thdr, &tdata,
0c583855501178ec9c62f2a94946192df2a01be4Timo Sirainen if ((thdr->type & MAIL_TRANSACTION_EXTERNAL) != 0 &&
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (mail_index_is_ext_synced(log_view, index->map))
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen if (mail_index_sync_record(&sync_map_ctx, thdr, tdata) < 0) {
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen mail_transaction_log_view_get_prev_pos(log_view, &prev_seq,
0c583855501178ec9c62f2a94946192df2a01be4Timo Sirainen (prev_seq != max_seq || prev_offset <= max_offset));
a80241dc44ee43bc809ed5ec6dedb74711966dafTimo Sirainen index->map->hdr.log_file_ext_offset = prev_offset;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen /* make sure we did everything right. note that although the
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen message counts should be equal, the flag counters may not */
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen i_assert(hdr.messages_count == (*map)->hdr.messages_count);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen i_assert(hdr.log_file_seq == (*map)->hdr.log_file_seq);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen i_assert(hdr.log_file_int_offset == (*map)->hdr.log_file_int_offset);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen i_assert(hdr.log_file_ext_offset == (*map)->hdr.log_file_ext_offset);
673ab5815277e6f9ac09227c866c90ab57438912Timo Sirainenstatic int mail_index_read_map_with_retry(struct mail_index *index,
2ef8fac74c9d0bfc330bf53bb06df7e9d5f08e7dTimo Sirainen mail_index_sync_lost_handler_t *const *handlers;
b9ce555e8624a5593b3bfd81b572b7d2e1e1fca5Timo Sirainen unsigned int i, count;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* we're most likely syncing the index and we really don't
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen want to read more than what was synced last time. */
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen if ((*map)->hdr.indexid != 0 && index->log != NULL) {
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen /* we're not creating the index, or opening transaction log.
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen sync this as a view from transaction log. */
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen ret = mail_index_sync_from_transactions(index, map,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* transaction log lost/broken, fallback to re-reading it */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* notify all "sync lost" handlers */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen handlers = array_get(&index->sync_lost_handlers, &count);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen for (i = 0; i < count; i++)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen for (i = 0;; i++) {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen ret = mail_index_read_map(index, *map, &retry,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* ESTALE - reopen index file */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen mail_index_set_syscall_error(index, "close()");
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen /* the file was lost */
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen mail_index_set_syscall_error(index, "open()");
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainenstatic int mail_index_map_try_existing(struct mail_index *index)
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen /* always check corrupted-flag to avoid errors later */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if ((hdr->flags & MAIL_INDEX_HDR_FLAG_CORRUPTED) != 0)
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen used_size = hdr->header_size + hdr->messages_count * hdr->record_size;
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen if (map->mmap_size >= used_size && map->hdr_base == hdr) {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen /* make sure the header is still valid. it also re-parses
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen extensions although they shouldn't change without the whole
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen index being recreated */
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen /* broken. fallback to re-mmaping which will catch it */
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainenint mail_index_map(struct mail_index *index, bool force)
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen i_assert(index->map == NULL || index->map->refcount > 0);
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen /* we're syncing, don't break the mapping */
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen if (index->map != NULL && index->map->refcount > 1) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* this map is already used by some views and they may have
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen pointers into it. leave them and create a new mapping. */
19593623a2278039be23fc93e3404c012920b664Timo Sirainen /* create a copy of the mapping instead so we don't
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen have to re-read it */
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen buffer_create_dynamic(default_pool, sizeof(map->hdr));
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen } else if (MAIL_INDEX_MAP_IS_IN_MEMORY(map)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (munmap(map->mmap_base, map->mmap_size) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_set_syscall_error(index, "munmap()");
20c26f4fcf9ef87434761829cc209c2f84ff5716Timo Sirainen ret = mail_index_read_map_with_retry(index, &map, force);
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen else if (ret == 0) {
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen i_assert(map->hdr.messages_count == map->records_count);
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainenint mail_index_get_latest_header(struct mail_index *index,
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen unsigned int i;
c485524d09c650ff6e6c552129d4257ac6145a8bTimo Sirainen for (i = 0;; i++) {
c485524d09c650ff6e6c552129d4257ac6145a8bTimo Sirainen ret = mail_index_read_header(index, hdr_r, sizeof(*hdr_r),
d7095f3a4466fbb78b2d5eb3d322bc15a5b0ab1fTimo Sirainen /* ESTALE - reopen index file */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_set_syscall_error(index, "close()");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* the file was lost */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_set_syscall_error(index, "open()");
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen mail_index_set_syscall_error(index, "pread_full()");
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainenmail_index_map_clone(const struct mail_index_map *map, uint32_t new_record_size)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int i, count;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen mem_map->buffer = buffer_create_dynamic(default_pool, size);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen buffer_append(mem_map->buffer, map->records, size);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen copy_size = I_MIN(map->hdr.record_size, new_record_size);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen dest = buffer_append_space_unsafe(mem_map->buffer,
c234379ba20d2513b1acca97f13c64e2a9e5d622Timo Sirainen mem_map->records = buffer_get_modifiable_data(mem_map->buffer, NULL);
c234379ba20d2513b1acca97f13c64e2a9e5d622Timo Sirainen buffer_create_dynamic(default_pool, map->hdr.header_size);
c234379ba20d2513b1acca97f13c64e2a9e5d622Timo Sirainen buffer_append_zero(mem_map->hdr_copy_buf, sizeof(*hdr));
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen map->hdr.header_size - map->hdr.base_header_size);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen hdr = buffer_get_modifiable_data(mem_map->hdr_copy_buf, NULL);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* if we're syncing transaction log into memory and later use the
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen mapping for updating the index, we need to remember what has
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mem_map->write_seq_first = map->write_seq_first;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen mem_map->write_seq_last = map->write_seq_last;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen /* copy extensions */
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen mail_index_map_init_extbufs(mem_map, count + 2);
5a07b37a9df398b5189c14872a600384208ab74bTimo 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 /* fix the name pointers to use our own pool */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen extensions = array_get_modifiable(&mem_map->extensions, &count);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen for (i = 0; i < count; i++) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen extensions[i].record_size <= hdr->record_size);
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen extensions[i].name = p_strdup(mem_map->extension_pool,
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainenint mail_index_map_get_ext_idx(struct mail_index_map *map,
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenstatic int mail_index_try_open_only(struct mail_index *index)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* Note that our caller must close index->fd by itself.
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mail_index_reopen() for example wants to revert back to old
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen index file if opening the new one fails. */
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen index->fd = nfs_safe_open(index->filepath, O_RDWR);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return mail_index_set_syscall_error(index, "open()");
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* have to create it */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenmail_index_try_open(struct mail_index *index, unsigned int *lock_id_r)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen unsigned int lock_id;
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen if (mail_index_lock_shared(index, FALSE, &lock_id) < 0) {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* it's corrupted - recreate it */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint mail_index_write_base_header(struct mail_index *index,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen hdr_size = I_MIN(sizeof(*hdr), hdr->base_header_size);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!MAIL_INDEX_MAP_IS_IN_MEMORY(index->map)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (msync(index->map->mmap_base, hdr_size, MS_SYNC) < 0)
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen return mail_index_set_syscall_error(index, "msync()");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (pwrite_full(index->fd, hdr, hdr_size, 0) < 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "pwrite_full()");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen buffer_write(index->map->hdr_copy_buf, 0, hdr, hdr_size);
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainenint mail_index_create_tmp_file(struct mail_index *index, const char **path_r)
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen path = *path_r = t_strconcat(index->filepath, ".tmp", NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen fd = open(path, O_RDWR|O_CREAT|O_TRUNC, index->mode);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return mail_index_file_set_syscall_error(index, path, "open()");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo 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()");
9fc077d3886b1584019bcc9b92c717cfffce5c67Timo Sirainenstatic int mail_index_create(struct mail_index *index,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* log file lock protects index creation */
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen if (mail_transaction_log_sync_lock(index->log, &seq, &offset) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* mark the existing log file as synced */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* create it fully in index.tmp first */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->fd = mail_index_create_tmp_file(index, &path);
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainen else if (write_full(index->fd, hdr, sizeof(*hdr)) < 0) {
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainen mail_index_file_set_syscall_error(index, path, "write_full()");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* it's corrupted even while we just created it,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen should never happen unless someone pokes the file directly */
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen "Newly created index file is corrupted: %s", path);
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen mail_index_file_set_syscall_error(index, path,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* make it visible to others */
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen mail_index_set_error(index, "rename(%s, %s) failed: %m",
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainenstatic void mail_index_header_init(struct mail_index_header *hdr)
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen i_assert((sizeof(*hdr) % sizeof(uint64_t)) == 0);
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen hdr->major_version = MAIL_INDEX_MAJOR_VERSION;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen hdr->minor_version = MAIL_INDEX_MINOR_VERSION;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen hdr->record_size = sizeof(struct mail_index_record);
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen hdr->compat_flags |= MAIL_INDEX_COMPAT_LITTLE_ENDIAN;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void mail_index_create_in_memory(struct mail_index *index,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* a bit kludgy way to do this, but it initializes everything
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen nicely and correctly */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->map = mail_index_map_clone(&tmp_map, hdr->record_size);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* returns -1 = error, 0 = won't create, 1 = ok */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int mail_index_open_files(struct mail_index *index,
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen unsigned int lock_id = 0;
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen else if (ret == 0) {
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen /* doesn't exist, or corrupted */
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen if ((flags & MAIL_INDEX_OPEN_FLAG_CREATE) == 0 &&
0d0451206a91e9f96e522075dce28a89adc2325dTimo Sirainen } else if (ret < 0)
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen index->log = mail_transaction_log_open_or_create(index);
f239eb76f77afcbc0bfc97c9b52b4407d1bc3fe6Timo Sirainen /* index->indexid may be updated by transaction log opening,
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen in case someone else had already created a new log file */
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen /* fallback to in-memory index */
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (mail_index_lock_shared(index, FALSE, &lock_id) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->cache = created ? mail_cache_create(index) :
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenint mail_index_open(struct mail_index *index, enum mail_index_open_flags flags,
369a1084c500a9df7448ffa9409ce32e42060bc2Timo Sirainen (index->hdr->flags & MAIL_INDEX_HDR_FLAG_CORRUPTED) != 0) {
a53cb86b4d733d9c48ee4d285bed477c80825804Timo Sirainen /* corrupted, reopen files */
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen index->filepath = MAIL_INDEX_IS_IN_MEMORY(index) ?
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen i_strconcat(index->dir, "/", index->prefix, NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (flags & MAIL_INDEX_OPEN_FLAG_MMAP_DISABLE) != 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (flags & MAIL_INDEX_OPEN_FLAG_MMAP_NO_WRITE) != 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (flags & MAIL_INDEX_OPEN_FLAG_DOTLOCK_USE_EXCL) != 0;
8afec4d1a32b78f540257a27769b372aad753384Timo Sirainen (flags & MAIL_INDEX_OPEN_FLAG_FSYNC_DISABLE) != 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* don't even bother to handle dotlocking without mmap being
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen disabled. that combination simply doesn't make any sense */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (lock_method == FILE_LOCK_METHOD_DOTLOCK &&
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_fatal("lock_method=dotlock and mmap_disable=no "
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "combination isn't supported. "
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "You don't _really_ want it anyway.");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* completely broken, reopen */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* too many tries */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenvoid mail_index_close(struct mail_index *index)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mail_index_set_syscall_error(index, "close()");
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenint mail_index_reopen(struct mail_index *index, int fd)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen unsigned int old_shared_locks, old_lock_id, lock_id = 0;
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen /* new file, new locks. the old fd can keep its locks, they don't
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen matter anymore as no-one's going to modify the file. */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen ret = mail_index_lock_shared(index, FALSE, &lock_id);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen else if (ret == 0) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* index file is lost */
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen /* read the new mapping. note that with mmap_disable we want
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen to keep the old mapping in index->map so we can update it
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen by reading transaction log. */
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen mail_index_set_syscall_error(index, "close()");
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen mail_index_set_syscall_error(index, "close()");
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenint mail_index_reopen_if_needed(struct mail_index *index)
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen /* deleted, reopen */
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen return mail_index_set_syscall_error(index, "fstat()");
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen if (nfs_safe_stat(index->filepath, &st2) < 0) {
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen mail_index_set_syscall_error(index, "stat()");
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen /* lost it? recreate later */
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainenint mail_index_refresh(struct mail_index *index)
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen unsigned int lock_id;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* we have index exclusively locked, nothing could
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen have changed. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* reopening is all we need */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* mail_index_map() simply reads latest changes from transaction log,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen which makes us fully refreshed. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mail_index_lock_shared(index, TRUE, &lock_id) < 0)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenstruct mail_cache *mail_index_get_cache(struct mail_index *index)
a045c3aba2610c6ed0bf1c346df1c6d8f7b9fbfdTimo Sirainenint mail_index_set_error(struct mail_index *index, const char *fmt, ...)
a045c3aba2610c6ed0bf1c346df1c6d8f7b9fbfdTimo Sirainenvoid mail_index_set_inconsistent(struct mail_index *index)
ae14dfd895881f9d1c6899b0c09f1a8b51447d61Timo Sirainenint mail_index_move_to_memory(struct mail_index *index)
a045c3aba2610c6ed0bf1c346df1c6d8f7b9fbfdTimo Sirainen /* set the index as being into memory */
0d0451206a91e9f96e522075dce28a89adc2325dTimo Sirainen /* mbox file was never even opened. just mark it as being in
87712707722ef7d73acb065546e61afa4455cd9eTimo Sirainen memory and let the caller re-open the index. */
87712707722ef7d73acb065546e61afa4455cd9eTimo Sirainen /* move index map to memory */
11fa9bc1e84f7cc88d953c0a0ae4e2cd07881957Timo Sirainen map = mail_index_map_clone(index->map, index->map->hdr.record_size);
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen /* move transaction log to memory */
87712707722ef7d73acb065546e61afa4455cd9eTimo Sirainen if (mail_transaction_log_move_to_memory(index->log) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* close the index file. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_set_syscall_error(index, "close()");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mail_index_mark_corrupted(struct mail_index *index)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mail_index_write_base_header(index, &hdr) == 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!MAIL_INDEX_IS_IN_MEMORY(index) && fsync(index->fd) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_set_syscall_error(index, "fsync()");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint mail_index_set_syscall_error(struct mail_index *index,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return mail_index_set_error(index, "%s failed with index file %s: %m",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint mail_index_file_set_syscall_error(struct mail_index *index,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return mail_index_set_error(index, "%s failed with file %s: %m",
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainenenum mail_index_error mail_index_get_last_error(struct mail_index *index)
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainenconst char *mail_index_get_error_message(struct mail_index *index)