Mercurial > dovecot > original-hg > dovecot-1.2
changeset 6378:12d4b757b825 HEAD
Moved mail_index_map_parse_keywords() to mail-index-map.c
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sat, 15 Sep 2007 10:11:58 +0300 |
parents | 29f427039e00 |
children | b734f6cf4bd8 |
files | src/lib-index/mail-index-map.c src/lib-index/mail-index.c |
diffstat | 2 files changed, 108 insertions(+), 108 deletions(-) [+] |
line wrap: on
line diff
--- a/src/lib-index/mail-index-map.c Sat Sep 15 10:05:56 2007 +0300 +++ b/src/lib-index/mail-index-map.c Sat Sep 15 10:11:58 2007 +0300 @@ -209,6 +209,114 @@ return 0; } +int mail_index_map_parse_keywords(struct mail_index_map *map) +{ + struct mail_index *index = map->index; + const struct mail_index_ext *ext; + const struct mail_index_keyword_header *kw_hdr; + const struct mail_index_keyword_header_rec *kw_rec; + const char *name; + unsigned int i, name_area_end_offset, old_count; + uint32_t idx; + + if (!mail_index_map_lookup_ext(map, "keywords", &idx)) { + if (array_is_created(&map->keyword_idx_map)) + array_clear(&map->keyword_idx_map); + return 0; + } + ext = array_idx(&map->extensions, idx); + + /* Extension header contains: + - struct mail_index_keyword_header + - struct mail_index_keyword_header_rec * keywords_count + - const char names[] * keywords_count + */ + i_assert(ext->hdr_offset < map->hdr.header_size); + kw_hdr = CONST_PTR_OFFSET(map->hdr_base, ext->hdr_offset); + kw_rec = (const void *)(kw_hdr + 1); + name = (const char *)(kw_rec + kw_hdr->keywords_count); + + old_count = !array_is_created(&map->keyword_idx_map) ? 0 : + array_count(&map->keyword_idx_map); + + /* Keywords can only be added into same mapping. Removing requires a + new mapping (recreating the index file) */ + if (kw_hdr->keywords_count == old_count) { + /* nothing changed */ + return 0; + } + + /* make sure the header is valid */ + if (kw_hdr->keywords_count < old_count) { + mail_index_set_error(index, "Corrupted index file %s: " + "Keywords removed unexpectedly", + index->filepath); + return -1; + } + + if ((size_t)(name - (const char *)kw_hdr) > ext->hdr_size) { + mail_index_set_error(index, "Corrupted index file %s: " + "keywords_count larger than header size", + index->filepath); + return -1; + } + + name_area_end_offset = (const char *)kw_hdr + ext->hdr_size - name; + for (i = 0; i < kw_hdr->keywords_count; i++) { + if (kw_rec[i].name_offset > name_area_end_offset) { + mail_index_set_error(index, "Corrupted index file %s: " + "name_offset points outside allocated header", + index->filepath); + return -1; + } + } + if (name[name_area_end_offset-1] != '\0') { + mail_index_set_error(index, "Corrupted index file %s: " + "Keyword header doesn't end with NUL", + index->filepath); + return -1; + } + + /* create file -> index mapping */ + if (!array_is_created(&map->keyword_idx_map)) + i_array_init(&map->keyword_idx_map, kw_hdr->keywords_count); + +#ifdef DEBUG + /* Check that existing headers are still the same. It's behind DEBUG + since it's pretty useless waste of CPU normally. */ + for (i = 0; i < array_count(&map->keyword_idx_map); i++) { + const char *keyword = name + kw_rec[i].name_offset; + const unsigned int *old_idx; + unsigned int idx; + + old_idx = array_idx(&map->keyword_idx_map, i); + if (!mail_index_keyword_lookup(index, keyword, &idx) || + idx != *old_idx) { + mail_index_set_error(index, "Corrupted index file %s: " + "Keywords changed unexpectedly", + index->filepath); + return -1; + } + } +#endif + /* Register the newly seen keywords */ + i = array_count(&map->keyword_idx_map); + for (; i < kw_hdr->keywords_count; i++) { + const char *keyword = name + kw_rec[i].name_offset; + unsigned int idx; + + if (*keyword == '\0') { + mail_index_set_error(index, "Corrupted index file %s: " + "Empty keyword name in header", + index->filepath); + return -1; + } + mail_index_keyword_lookup_or_create(index, keyword, &idx); + array_append(&map->keyword_idx_map, &idx, 1); + } + return 0; +} + static bool mail_index_check_header_compat(struct mail_index *index, const struct mail_index_header *hdr, uoff_t file_size)
--- a/src/lib-index/mail-index.c Sat Sep 15 10:05:56 2007 +0300 +++ b/src/lib-index/mail-index.c Sat Sep 15 10:11:58 2007 +0300 @@ -216,114 +216,6 @@ array_append(&index->keywords, &keyword, 1); } -int mail_index_map_parse_keywords(struct mail_index_map *map) -{ - struct mail_index *index = map->index; - const struct mail_index_ext *ext; - const struct mail_index_keyword_header *kw_hdr; - const struct mail_index_keyword_header_rec *kw_rec; - const char *name; - unsigned int i, name_area_end_offset, old_count; - uint32_t idx; - - if (!mail_index_map_lookup_ext(map, "keywords", &idx)) { - if (array_is_created(&map->keyword_idx_map)) - array_clear(&map->keyword_idx_map); - return 0; - } - ext = array_idx(&map->extensions, idx); - - /* Extension header contains: - - struct mail_index_keyword_header - - struct mail_index_keyword_header_rec * keywords_count - - const char names[] * keywords_count - */ - i_assert(ext->hdr_offset < map->hdr.header_size); - kw_hdr = CONST_PTR_OFFSET(map->hdr_base, ext->hdr_offset); - kw_rec = (const void *)(kw_hdr + 1); - name = (const char *)(kw_rec + kw_hdr->keywords_count); - - old_count = !array_is_created(&map->keyword_idx_map) ? 0 : - array_count(&map->keyword_idx_map); - - /* Keywords can only be added into same mapping. Removing requires a - new mapping (recreating the index file) */ - if (kw_hdr->keywords_count == old_count) { - /* nothing changed */ - return 0; - } - - /* make sure the header is valid */ - if (kw_hdr->keywords_count < old_count) { - mail_index_set_error(index, "Corrupted index file %s: " - "Keywords removed unexpectedly", - index->filepath); - return -1; - } - - if ((size_t)(name - (const char *)kw_hdr) > ext->hdr_size) { - mail_index_set_error(index, "Corrupted index file %s: " - "keywords_count larger than header size", - index->filepath); - return -1; - } - - name_area_end_offset = (const char *)kw_hdr + ext->hdr_size - name; - for (i = 0; i < kw_hdr->keywords_count; i++) { - if (kw_rec[i].name_offset > name_area_end_offset) { - mail_index_set_error(index, "Corrupted index file %s: " - "name_offset points outside allocated header", - index->filepath); - return -1; - } - } - if (name[name_area_end_offset-1] != '\0') { - mail_index_set_error(index, "Corrupted index file %s: " - "Keyword header doesn't end with NUL", - index->filepath); - return -1; - } - - /* create file -> index mapping */ - if (!array_is_created(&map->keyword_idx_map)) - i_array_init(&map->keyword_idx_map, kw_hdr->keywords_count); - -#ifdef DEBUG - /* Check that existing headers are still the same. It's behind DEBUG - since it's pretty useless waste of CPU normally. */ - for (i = 0; i < array_count(&map->keyword_idx_map); i++) { - const char *keyword = name + kw_rec[i].name_offset; - const unsigned int *old_idx; - unsigned int idx; - - old_idx = array_idx(&map->keyword_idx_map, i); - if (!mail_index_keyword_lookup(index, keyword, &idx) || - idx != *old_idx) { - mail_index_set_error(index, "Corrupted index file %s: " - "Keywords changed unexpectedly", - index->filepath); - return -1; - } - } -#endif - /* Register the newly seen keywords */ - i = array_count(&map->keyword_idx_map); - for (; i < kw_hdr->keywords_count; i++) { - const char *keyword = name + kw_rec[i].name_offset; - unsigned int idx; - - if (*keyword == '\0') { - mail_index_set_error(index, "Corrupted index file %s: " - "Empty keyword name in header", - index->filepath); - return -1; - } - mail_index_keyword_lookup_or_create(index, keyword, &idx); - array_append(&map->keyword_idx_map, &idx, 1); - } - return 0; -} - const ARRAY_TYPE(keywords) *mail_index_get_keywords(struct mail_index *index) { return &index->keywords;