mail-cache-fields.c revision dbd9604da561399cc6255289d5b6f6f662ab2d00
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (C) 2004 Timo Sirainen */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mail_cache_register_fields(struct mail_cache *cache,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen unsigned int new_idx;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen for (i = 0; i < fields_count; i++) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (hash_lookup_full(cache->field_name_hash, fields[i].name,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen /* @UNSAFE */
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen cache->fields_count * sizeof(*cache->field_file_map),
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen for (i = 0; i < fields_count; i++) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* new index - save it */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen cache->fields[idx].field.field_size = (unsigned int)-1;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenmail_cache_register_lookup(struct mail_cache *cache, const char *name)
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen if (hash_lookup_full(cache->field_name_hash, name,
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen return POINTER_CAST_TO(orig_value, unsigned int);
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen return (unsigned int)-1;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenstatic int mail_cache_header_fields_get_offset(struct mail_cache *cache,
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen const struct mail_cache_header_fields *field_hdr;
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen /* find the latest header */
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen mail_index_offset_to_uint32(cache->hdr->field_header_offset);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen while (next_offset != 0) {
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen "next_offset in field header loops");
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen /* we can't trust that the cached data is valid */
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen file_cache_invalidate(cache->file_cache, offset,
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen field_hdr = CONST_PTR_OFFSET(cache->data, offset);
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen mail_index_offset_to_uint32(field_hdr->next_offset);
5626ae5e3316eced244adb6485c0927f1c7fdc41Timo Sirainenint mail_cache_header_fields_read(struct mail_cache *cache)
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen const struct mail_cache_header_fields *field_hdr = NULL;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen if (mail_cache_header_fields_get_offset(cache, &offset) < 0)
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen /* no fields - the file is empty */
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen field_hdr = CONST_PTR_OFFSET(cache->data, offset);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen if (offset + field_hdr->size > cache->mmap_length) {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen "field header points outside file");
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen /* check the fixed size of the header. name[] has to be checked
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen separately */
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen field_hdr->fields_count * (sizeof(uint32_t)*2 + 1 + 2)) {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen mail_cache_set_corrupted(cache, "invalid field header size");
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen if (field_hdr->size > sizeof(*field_hdr) + CACHE_HDR_PREFETCH) {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen /* we can't trust that the cached data is valid */
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen file_cache_invalidate(cache->file_cache, offset,
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen if (mail_cache_map(cache, offset, field_hdr->size) < 0)
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen field_hdr = CONST_PTR_OFFSET(cache->data, offset);
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen cache->file_fields_count * sizeof(unsigned int),
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen field_hdr->fields_count * sizeof(unsigned int));
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen cache->file_fields_count = field_hdr->fields_count;
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen last_used = CONST_PTR_OFFSET(field_hdr, MAIL_CACHE_FIELD_LAST_USED());
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen MAIL_CACHE_FIELD_SIZE(field_hdr->fields_count));
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen MAIL_CACHE_FIELD_TYPE(field_hdr->fields_count));
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen MAIL_CACHE_FIELD_DECISION(field_hdr->fields_count));
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen MAIL_CACHE_FIELD_NAMES(field_hdr->fields_count));
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen end = CONST_PTR_OFFSET(field_hdr, field_hdr->size);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen /* clear the old mapping */
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen for (i = 0; i < field_hdr->fields_count; i++) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen for (p = names; p != end && *p != '\0'; p++) ;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen "field header names corrupted");
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (hash_lookup_full(cache->field_name_hash, names,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen /* already exists, see if decision can be updated */
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen field.idx = POINTER_CAST_TO(orig_value, unsigned int);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (!cache->fields[field.idx].decision_dirty) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (cache->fields[field.idx].field.type != types[i]) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen "registered field type changed");
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen if (cache->field_file_map[field.idx] != (uint32_t)-1) {
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen /* update last_used if it's newer than ours */
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen if (last_used[i] > cache->fields[field.idx].last_used)
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen cache->fields[field.idx].last_used = last_used[i];
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainenstatic void copy_to_buf(struct mail_cache *cache, buffer_t *dest,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen unsigned int i, field;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen for (i = 0; i < cache->file_fields_count; i++) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen data = CONST_PTR_OFFSET(&cache->fields[field], offset);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen data = CONST_PTR_OFFSET(&cache->fields[i], offset);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenstatic void copy_to_buf_byte(struct mail_cache *cache, buffer_t *dest,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen unsigned int i, field;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen for (i = 0; i < cache->file_fields_count; i++) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen data = CONST_PTR_OFFSET(&cache->fields[field], offset);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen data = CONST_PTR_OFFSET(&cache->fields[i], offset);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainenstatic int mail_cache_header_fields_update_locked(struct mail_cache *cache)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (mail_cache_header_fields_read(cache) < 0 ||
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen mail_cache_header_fields_get_offset(cache, &offset) < 0)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen buffer = buffer_create_dynamic(pool_datastack_create(), 256);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen offsetof(struct mail_cache_field_private, last_used),
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen sizeof(uint8_t) * cache->file_fields_count, offset +
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen MAIL_CACHE_FIELD_DECISION(cache->file_fields_count));
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen for (i = 0; i < cache->file_fields_count; i++)
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainenint mail_cache_header_fields_update(struct mail_cache *cache)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return mail_cache_header_fields_update_locked(cache);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen ret = mail_cache_header_fields_update_locked(cache);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainenvoid mail_cache_header_fields_get(struct mail_cache *cache, buffer_t *dest)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen unsigned int field;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* we have to keep the field order for the existing fields. */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen offsetof(struct mail_cache_field_private, last_used),
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen copy_to_buf(cache, dest, offsetof(struct mail_cache_field, field_size),
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen copy_to_buf_byte(cache, dest, offsetof(struct mail_cache_field, type));
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen i_assert(buffer_get_used_size(dest) == sizeof(hdr) +
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen for (i = 0; i < cache->file_fields_count; i++) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen buffer_append(dest, null4, 4 - (hdr.size & 3));
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenint mail_cache_header_fields_get_next_offset(struct mail_cache *cache,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (mail_cache_header_fields_get_offset(cache, offset_r) < 0)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen *offset_r = offsetof(struct mail_cache_header,