mail-cache-fields.c revision a817fdcc43aedf423e2134091d5f83f91d64bcc9
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/* Copyright (C) 2004 Timo Sirainen */
c977374bd4651cafc1626ebe308aa66dfd8b30e0Timo Sirainenvoid mail_cache_register_fields(struct mail_cache *cache,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen unsigned int new_idx;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; i < fields_count; i++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (hash_lookup_full(cache->field_name_hash, fields[i].name,
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen /* @UNSAFE */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen cache->fields_count * sizeof(*cache->field_file_map),
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainen for (i = 0; i < fields_count; i++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* new index - save it */
73a87c2ff65c6116cde6fb158dfddb8ef7346901Timo Sirainen cache->fields[idx].field.field_size = (unsigned int)-1;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenmail_cache_register_lookup(struct mail_cache *cache, const char *name)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (hash_lookup_full(cache->field_name_hash, name,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return POINTER_CAST_TO(orig_value, unsigned int);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return (unsigned int)-1;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenmail_cache_register_get_list(struct mail_cache *cache, pool_t pool,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen unsigned int *count_r)
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen unsigned int i;
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen list = p_new(pool, struct mail_cache_field, cache->fields_count);
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainenstatic int mail_cache_header_fields_get_offset(struct mail_cache *cache,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen const struct mail_cache_header_fields *field_hdr;
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen /* find the latest header */
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen mail_index_offset_to_uint32(cache->hdr->field_header_offset);
6ae329de09afb7214c906d762320847e05469d53Timo Sirainen while (next_offset != 0) {
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen "next_offset in field header loops");
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* we can't trust that the cached data is valid */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen file_cache_invalidate(cache->file_cache, offset,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen field_hdr = CONST_PTR_OFFSET(cache->data, offset);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_index_offset_to_uint32(field_hdr->next_offset);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint mail_cache_header_fields_read(struct mail_cache *cache)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen const struct mail_cache_header_fields *field_hdr = NULL;
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen if (mail_cache_header_fields_get_offset(cache, &offset) < 0)
73a87c2ff65c6116cde6fb158dfddb8ef7346901Timo Sirainen /* no fields - the file is empty */
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen field_hdr = CONST_PTR_OFFSET(cache->data, offset);
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen if (offset + field_hdr->size > cache->mmap_length) {
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen "field header points outside file");
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen /* check the fixed size of the header. name[] has to be checked
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen separately */
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen field_hdr->fields_count * (sizeof(uint32_t)*2 + 1 + 2)) {
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen mail_cache_set_corrupted(cache, "invalid field header size");
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen if (field_hdr->size > sizeof(*field_hdr) + CACHE_HDR_PREFETCH) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* we can't trust that the cached data is valid */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen file_cache_invalidate(cache->file_cache, offset,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (mail_cache_map(cache, offset, field_hdr->size) < 0)
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen field_hdr = CONST_PTR_OFFSET(cache->data, offset);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen cache->file_fields_count * sizeof(unsigned int),
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen field_hdr->fields_count * sizeof(unsigned int));
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen cache->file_fields_count = field_hdr->fields_count;
d0b2bd9e2246eb68ed952c7f2e13d1969d657c8fTimo Sirainen last_used = CONST_PTR_OFFSET(field_hdr, MAIL_CACHE_FIELD_LAST_USED());
1f1ee8db68d9ae1604350801cd8dc33ebe29fe8aTimo Sirainen MAIL_CACHE_FIELD_SIZE(field_hdr->fields_count));
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen MAIL_CACHE_FIELD_TYPE(field_hdr->fields_count));
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen MAIL_CACHE_FIELD_DECISION(field_hdr->fields_count));
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen MAIL_CACHE_FIELD_NAMES(field_hdr->fields_count));
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen end = CONST_PTR_OFFSET(field_hdr, field_hdr->size);
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen /* clear the old mapping */
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen for (i = 0; i < field_hdr->fields_count; i++) {
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen for (p = names; p != end && *p != '\0'; p++) ;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen "field header names corrupted");
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (hash_lookup_full(cache->field_name_hash, names,
d22301419109ed4a38351715e6760011421dadecTimo Sirainen /* already exists, see if decision can be updated */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen field.idx = POINTER_CAST_TO(orig_value, unsigned int);
3f91e60401495a4046c73992fabaa5e77200a451Timo Sirainen if (!cache->fields[field.idx].decision_dirty) {
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen if (cache->fields[field.idx].field.type != types[i]) {
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen "registered field type changed");
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (cache->field_file_map[field.idx] != (uint32_t)-1) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* update last_used if it's newer than ours */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (last_used[i] > cache->fields[field.idx].last_used)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen cache->fields[field.idx].last_used = last_used[i];
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainenstatic void copy_to_buf(struct mail_cache *cache, buffer_t *dest,
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen unsigned int i, field;
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen for (i = 0; i < cache->file_fields_count; i++) {
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen data = CONST_PTR_OFFSET(&cache->fields[field], offset);
ddb018bc886680f462463b2c87f983fdedbf6cf0Timo Sirainen data = CONST_PTR_OFFSET(&cache->fields[i], offset);
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainenstatic void copy_to_buf_byte(struct mail_cache *cache, buffer_t *dest,
37ee89f3cba22cd975912a882f0d3097fa5031e1Timo Sirainen unsigned int i, field;
37ee89f3cba22cd975912a882f0d3097fa5031e1Timo Sirainen for (i = 0; i < cache->file_fields_count; i++) {
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen data = CONST_PTR_OFFSET(&cache->fields[field], offset);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen data = CONST_PTR_OFFSET(&cache->fields[i], offset);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic int mail_cache_header_fields_update_locked(struct mail_cache *cache)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (mail_cache_header_fields_read(cache) < 0 ||
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_cache_header_fields_get_offset(cache, &offset) < 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen buffer = buffer_create_dynamic(pool_datastack_create(), 256);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen offsetof(struct mail_cache_field_private, last_used),
4fded1eec06aba9ce37887ac30619768760cd0d0Timo Sirainen sizeof(uint8_t) * cache->file_fields_count, offset +
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen MAIL_CACHE_FIELD_DECISION(cache->file_fields_count));
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; i < cache->file_fields_count; i++)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint mail_cache_header_fields_update(struct mail_cache *cache)
f71c2d4e6b802bf8e622bcd5df29286262d05d5aTimo Sirainen return mail_cache_header_fields_update_locked(cache);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ret = mail_cache_header_fields_update_locked(cache);
unsigned int field;
const char *name;
uint32_t i;
sizeof(uint32_t));
sizeof(uint32_t));
if (*offset_r == 0) {