mail-cache-fields.c revision a04b6515c20b431294626400e173d81f3d25889b
89a126810703c666309310d0f3189e9834d70b5bTimo Sirainen/* Copyright (c) 2004-2007 Dovecot authors, see the included COPYING file */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen#include "lib.h"
6157a322f2ac1ea1332d9003ecb0b11466aa8fe7Timo Sirainen#include "ioloop.h"
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen#include "buffer.h"
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen#include "hash.h"
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen#include "file-cache.h"
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen#include "read-full.h"
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen#include "write-full.h"
c4cfee078c4a185b5ba8f0c55f51275b7e885b2cTimo Sirainen#include "mmap-util.h"
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen#include "mail-cache-private.h"
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen#include <stddef.h>
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen#define CACHE_FIELD_IS_NEWLY_WANTED(cache, field_idx) \
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen ((cache)->field_file_map[field_idx] == (uint32_t)-1 && \
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen (cache)->fields[field_idx].used)
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainenstatic bool field_has_fixed_size(enum mail_cache_field_type type)
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen{
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen switch (type) {
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen case MAIL_CACHE_FIELD_FIXED_SIZE:
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen case MAIL_CACHE_FIELD_BITMASK:
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen return TRUE;
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen case MAIL_CACHE_FIELD_VARIABLE_SIZE:
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen case MAIL_CACHE_FIELD_STRING:
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen case MAIL_CACHE_FIELD_HEADER:
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen return FALSE;
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen case MAIL_CACHE_FIELD_COUNT:
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen break;
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen }
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen i_unreached();
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen return FALSE;
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen}
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainenstatic bool field_decision_is_valid(enum mail_cache_decision_type type)
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen{
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen switch (type & ~MAIL_CACHE_DECISION_FORCED) {
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen case MAIL_CACHE_DECISION_NO:
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen case MAIL_CACHE_DECISION_TEMP:
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen case MAIL_CACHE_DECISION_YES:
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen return TRUE;
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen default:
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen return FALSE;
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen }
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen}
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainenstatic int field_type_verify(struct mail_cache *cache, unsigned int idx,
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen enum mail_cache_field_type type, unsigned int size)
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen{
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen const struct mail_cache_field *field = &cache->fields[idx].field;
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen if (field->type != type) {
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen mail_cache_set_corrupted(cache,
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen "registered field %s type changed", field->name);
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen return -1;
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen }
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen if (field->field_size != size && field_has_fixed_size(type)) {
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen mail_cache_set_corrupted(cache,
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen "registered field %s size changed", field->name);
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen return -1;
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen }
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen return 0;
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen}
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenvoid mail_cache_register_fields(struct mail_cache *cache,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen struct mail_cache_field *fields,
9d3ccd79130199ffdb19a688027d49bf20a4aaaaTimo Sirainen unsigned int fields_count)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen{
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen void *orig_key, *orig_value;
8d6cb44a0161d88743756733f83c4fb278485987Timo Sirainen char *name;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen unsigned int new_idx;
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen unsigned int i, j;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen new_idx = cache->fields_count;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen for (i = 0; i < fields_count; i++) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if (hash_lookup_full(cache->field_name_hash, fields[i].name,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen &orig_key, &orig_value)) {
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen i_assert(fields[i].type < MAIL_CACHE_FIELD_COUNT);
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen fields[i].idx =
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen POINTER_CAST_TO(orig_value, unsigned int);
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen (void)field_type_verify(cache, fields[i].idx,
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen fields[i].type,
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen fields[i].field_size);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen continue;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen }
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen /* check if the same header is being registered in the
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen same field array */
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen for (j = 0; j < i; j++) {
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen if (strcasecmp(fields[i].name, fields[j].name) == 0) {
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen fields[i].idx = fields[j].idx;
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen break;
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen }
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen }
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen if (j == i)
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen fields[i].idx = new_idx++;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen }
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if (new_idx == cache->fields_count)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen return;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* @UNSAFE */
0cea9b1f4fa0495a48f5f097e40492517d67e1baTimo Sirainen cache->fields = i_realloc(cache->fields,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen cache->fields_count * sizeof(*cache->fields),
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen new_idx * sizeof(*cache->fields));
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen cache->field_file_map =
0cea9b1f4fa0495a48f5f097e40492517d67e1baTimo Sirainen i_realloc(cache->field_file_map,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen cache->fields_count * sizeof(*cache->field_file_map),
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen new_idx * sizeof(*cache->field_file_map));
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen for (i = 0; i < fields_count; i++) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen unsigned int idx = fields[i].idx;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if (idx < cache->fields_count)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen continue;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* new index - save it */
8d6cb44a0161d88743756733f83c4fb278485987Timo Sirainen name = p_strdup(cache->field_pool, fields[i].name);
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen cache->fields[idx].field = fields[i];
8d6cb44a0161d88743756733f83c4fb278485987Timo Sirainen cache->fields[idx].field.name = name;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen cache->field_file_map[idx] = (uint32_t)-1;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen if (!field_has_fixed_size(cache->fields[idx].field.type))
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen cache->fields[idx].field.field_size = (unsigned int)-1;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
8d6cb44a0161d88743756733f83c4fb278485987Timo Sirainen hash_insert(cache->field_name_hash, name, POINTER_CAST(idx));
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen }
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen cache->fields_count = new_idx;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen}
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenunsigned int
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenmail_cache_register_lookup(struct mail_cache *cache, const char *name)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen{
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen void *orig_key, *orig_value;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if (hash_lookup_full(cache->field_name_hash, name,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen &orig_key, &orig_value))
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen return POINTER_CAST_TO(orig_value, unsigned int);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen else
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen return (unsigned int)-1;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen}
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
16598a1fb870ae40d6177755a4306216e4d6a4cdTimo Sirainenconst struct mail_cache_field *
16598a1fb870ae40d6177755a4306216e4d6a4cdTimo Sirainenmail_cache_register_get_list(struct mail_cache *cache, pool_t pool,
16598a1fb870ae40d6177755a4306216e4d6a4cdTimo Sirainen unsigned int *count_r)
16598a1fb870ae40d6177755a4306216e4d6a4cdTimo Sirainen{
16598a1fb870ae40d6177755a4306216e4d6a4cdTimo Sirainen struct mail_cache_field *list;
16598a1fb870ae40d6177755a4306216e4d6a4cdTimo Sirainen unsigned int i;
16598a1fb870ae40d6177755a4306216e4d6a4cdTimo Sirainen
bc793bfcee945ce8871edfa298fe7235744425b6Timo Sirainen if (!cache->opened)
bc793bfcee945ce8871edfa298fe7235744425b6Timo Sirainen (void)mail_cache_open_and_verify(cache);
bc793bfcee945ce8871edfa298fe7235744425b6Timo Sirainen
16598a1fb870ae40d6177755a4306216e4d6a4cdTimo Sirainen list = p_new(pool, struct mail_cache_field, cache->fields_count);
ea4ca37ec14913354f3a0deebc0df96097eb9468Timo Sirainen for (i = 0; i < cache->fields_count; i++) {
16598a1fb870ae40d6177755a4306216e4d6a4cdTimo Sirainen list[i] = cache->fields[i].field;
ea4ca37ec14913354f3a0deebc0df96097eb9468Timo Sirainen list[i].name = p_strdup(pool, list[i].name);
ea4ca37ec14913354f3a0deebc0df96097eb9468Timo Sirainen }
16598a1fb870ae40d6177755a4306216e4d6a4cdTimo Sirainen
16598a1fb870ae40d6177755a4306216e4d6a4cdTimo Sirainen *count_r = cache->fields_count;
16598a1fb870ae40d6177755a4306216e4d6a4cdTimo Sirainen return list;
16598a1fb870ae40d6177755a4306216e4d6a4cdTimo Sirainen}
16598a1fb870ae40d6177755a4306216e4d6a4cdTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenstatic int mail_cache_header_fields_get_offset(struct mail_cache *cache,
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen uint32_t *offset_r, bool map)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen{
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen const struct mail_cache_header_fields *field_hdr;
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen struct mail_cache_header_fields tmp_field_hdr;
6e235046e1d8e9d89fc948f5c623676c20421a28Timo Sirainen uint32_t offset = 0, next_offset;
abb83d133dd082527d500916fca66a72fbbbaa8dTimo Sirainen unsigned int next_count = 0;
e4423c16a4f798ecf75ca2b0c3ef834000faed4bTimo Sirainen bool invalidate = FALSE;
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen int ret;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if (MAIL_CACHE_IS_UNUSABLE(cache)) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen *offset_r = 0;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen return 0;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen }
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* find the latest header */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen offset = 0;
6e235046e1d8e9d89fc948f5c623676c20421a28Timo Sirainen next_offset = cache->last_field_header_offset != 0 ?
6e235046e1d8e9d89fc948f5c623676c20421a28Timo Sirainen cache->last_field_header_offset :
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen mail_index_offset_to_uint32(cache->hdr->field_header_offset);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen while (next_offset != 0) {
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen if (next_offset == offset) {
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen mail_cache_set_corrupted(cache,
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen "next_offset in field header loops");
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen return -1;
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen }
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen offset = next_offset;
e4423c16a4f798ecf75ca2b0c3ef834000faed4bTimo Sirainen invalidate = TRUE;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen if (cache->mmap_base != NULL) {
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen if (mail_cache_map(cache, offset,
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen sizeof(*field_hdr)) < 0)
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen return -1;
c4cfee078c4a185b5ba8f0c55f51275b7e885b2cTimo Sirainen
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen field_hdr = CONST_PTR_OFFSET(cache->data, offset);
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen } else {
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen /* if we need to follow multiple offsets to get to
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen the last one, it's faster to just pread() the file
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen instead of going through cache */
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen ret = pread_full(cache->fd, &tmp_field_hdr,
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen sizeof(tmp_field_hdr), offset);
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen if (ret < 0) {
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen mail_cache_set_syscall_error(cache, "pread()");
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen return -1;
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen }
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen if (ret == 0) {
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen mail_cache_set_corrupted(cache,
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen "next_offset points outside file");
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen return -1;
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen }
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen field_hdr = &tmp_field_hdr;
114a0f74e0f825c6bd8aeadfafb248a030762a1fTimo Sirainen }
c4cfee078c4a185b5ba8f0c55f51275b7e885b2cTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen next_offset =
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen mail_index_offset_to_uint32(field_hdr->next_offset);
abb83d133dd082527d500916fca66a72fbbbaa8dTimo Sirainen next_count++;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen }
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
6e235046e1d8e9d89fc948f5c623676c20421a28Timo Sirainen if (offset == 0) {
6e235046e1d8e9d89fc948f5c623676c20421a28Timo Sirainen mail_cache_set_corrupted(cache, "missing header fields");
6e235046e1d8e9d89fc948f5c623676c20421a28Timo Sirainen return -1;
6e235046e1d8e9d89fc948f5c623676c20421a28Timo Sirainen }
6e235046e1d8e9d89fc948f5c623676c20421a28Timo Sirainen cache->last_field_header_offset = offset;
6e235046e1d8e9d89fc948f5c623676c20421a28Timo Sirainen
abb83d133dd082527d500916fca66a72fbbbaa8dTimo Sirainen if (next_count > MAIL_CACHE_HEADER_FIELD_CONTINUE_COUNT)
abb83d133dd082527d500916fca66a72fbbbaa8dTimo Sirainen cache->need_compress_file_seq = cache->hdr->file_seq;
abb83d133dd082527d500916fca66a72fbbbaa8dTimo Sirainen
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen if (map) {
e4423c16a4f798ecf75ca2b0c3ef834000faed4bTimo Sirainen if (cache->file_cache != NULL && invalidate) {
e4423c16a4f798ecf75ca2b0c3ef834000faed4bTimo Sirainen /* if this isn't the first header in file and we hadn't
e4423c16a4f798ecf75ca2b0c3ef834000faed4bTimo Sirainen read this before, we can't trust that the cached
e4423c16a4f798ecf75ca2b0c3ef834000faed4bTimo Sirainen data is valid */
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen file_cache_invalidate(cache->file_cache, offset,
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen field_hdr->size);
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen }
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen if (mail_cache_map(cache, offset, field_hdr->size) < 0)
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen return -1;
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen }
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen *offset_r = offset;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen return 0;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen}
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenint mail_cache_header_fields_read(struct mail_cache *cache)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen{
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen const struct mail_cache_header_fields *field_hdr = NULL;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen struct mail_cache_field field;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen const uint32_t *last_used, *sizes;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen const uint8_t *types, *decisions;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen const char *p, *names, *end;
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen void *orig_key, *orig_value;
a04b6515c20b431294626400e173d81f3d25889bTimo Sirainen unsigned int fidx, new_fields_count;
9a583c7a827f7a4d89ee43774f2d51ea6a214543Timo Sirainen time_t max_drop_time;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen uint32_t offset, i;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen if (mail_cache_header_fields_get_offset(cache, &offset, TRUE) < 0)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen return -1;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if (offset == 0) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* no fields - the file is empty */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen return 0;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen }
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen field_hdr = CONST_PTR_OFFSET(cache->data, offset);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if (offset + field_hdr->size > cache->mmap_length) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen mail_cache_set_corrupted(cache,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen "field header points outside file");
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen return -1;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen }
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* check the fixed size of the header. name[] has to be checked
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen separately */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if (field_hdr->size < sizeof(*field_hdr) +
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen field_hdr->fields_count * (sizeof(uint32_t)*2 + 1 + 2)) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen mail_cache_set_corrupted(cache, "invalid field header size");
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen return -1;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen }
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen field_hdr = CONST_PTR_OFFSET(cache->data, offset);
11768c622a8e5aaf0e5d9b3c9a872867b62b4830Timo Sirainen new_fields_count = field_hdr->fields_count;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
11768c622a8e5aaf0e5d9b3c9a872867b62b4830Timo Sirainen if (new_fields_count != 0) {
265e9d8457aa5ce01b7aad954632aed8c36deeb8Timo Sirainen cache->file_field_map =
265e9d8457aa5ce01b7aad954632aed8c36deeb8Timo Sirainen i_realloc(cache->file_field_map,
265e9d8457aa5ce01b7aad954632aed8c36deeb8Timo Sirainen cache->file_fields_count *
265e9d8457aa5ce01b7aad954632aed8c36deeb8Timo Sirainen sizeof(unsigned int),
11768c622a8e5aaf0e5d9b3c9a872867b62b4830Timo Sirainen new_fields_count * sizeof(unsigned int));
265e9d8457aa5ce01b7aad954632aed8c36deeb8Timo Sirainen } else {
265e9d8457aa5ce01b7aad954632aed8c36deeb8Timo Sirainen i_free_and_null(cache->file_field_map);
265e9d8457aa5ce01b7aad954632aed8c36deeb8Timo Sirainen }
11768c622a8e5aaf0e5d9b3c9a872867b62b4830Timo Sirainen cache->file_fields_count = new_fields_count;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen last_used = CONST_PTR_OFFSET(field_hdr, MAIL_CACHE_FIELD_LAST_USED());
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen sizes = CONST_PTR_OFFSET(field_hdr,
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen MAIL_CACHE_FIELD_SIZE(field_hdr->fields_count));
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen types = CONST_PTR_OFFSET(field_hdr,
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen MAIL_CACHE_FIELD_TYPE(field_hdr->fields_count));
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen decisions = CONST_PTR_OFFSET(field_hdr,
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen MAIL_CACHE_FIELD_DECISION(field_hdr->fields_count));
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen names = CONST_PTR_OFFSET(field_hdr,
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen MAIL_CACHE_FIELD_NAMES(field_hdr->fields_count));
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen end = CONST_PTR_OFFSET(field_hdr, field_hdr->size);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* clear the old mapping */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen for (i = 0; i < cache->fields_count; i++)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen cache->field_file_map[i] = (uint32_t)-1;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
6ebcdea168735ee76e32b871c1f50f3526690447Timo Sirainen max_drop_time = cache->index->map->hdr.day_stamp == 0 ? 0 :
6ebcdea168735ee76e32b871c1f50f3526690447Timo Sirainen cache->index->map->hdr.day_stamp - MAIL_CACHE_FIELD_DROP_SECS;
9a583c7a827f7a4d89ee43774f2d51ea6a214543Timo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen memset(&field, 0, sizeof(field));
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen for (i = 0; i < field_hdr->fields_count; i++) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen for (p = names; p != end && *p != '\0'; p++) ;
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen if (p == end || *names == '\0') {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen mail_cache_set_corrupted(cache,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen "field header names corrupted");
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen return -1;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen }
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen if (types[i] > MAIL_CACHE_FIELD_COUNT) {
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen mail_cache_set_corrupted(cache, "field type corrupted");
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen return -1;
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen }
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen if (!field_decision_is_valid(decisions[i])) {
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen mail_cache_set_corrupted(cache,
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen "field decision type corrupted");
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen return -1;
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen }
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen if (hash_lookup_full(cache->field_name_hash, names,
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen &orig_key, &orig_value)) {
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen /* already exists, see if decision can be updated */
a04b6515c20b431294626400e173d81f3d25889bTimo Sirainen fidx = POINTER_CAST_TO(orig_value, unsigned int);
a04b6515c20b431294626400e173d81f3d25889bTimo Sirainen if (!cache->fields[fidx].decision_dirty) {
a04b6515c20b431294626400e173d81f3d25889bTimo Sirainen cache->fields[fidx].field.decision =
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen decisions[i];
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen }
a04b6515c20b431294626400e173d81f3d25889bTimo Sirainen if (field_type_verify(cache, fidx,
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen types[i], sizes[i]) < 0)
7bb939ef70752f2731d27b18c944ea94e5b23eb5Timo Sirainen return -1;
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen } else {
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen field.name = names;
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen field.type = types[i];
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen field.field_size = sizes[i];
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen field.decision = decisions[i];
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen mail_cache_register_fields(cache, &field, 1);
a04b6515c20b431294626400e173d81f3d25889bTimo Sirainen fidx = field.idx;
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen }
a04b6515c20b431294626400e173d81f3d25889bTimo Sirainen if (cache->field_file_map[fidx] != (uint32_t)-1) {
8e574a603c2c02105b073906a7ca91904271b80eTimo Sirainen mail_cache_set_corrupted(cache,
8e574a603c2c02105b073906a7ca91904271b80eTimo Sirainen "Duplicated field in header: %s", names);
8e574a603c2c02105b073906a7ca91904271b80eTimo Sirainen return -1;
8e574a603c2c02105b073906a7ca91904271b80eTimo Sirainen }
a04b6515c20b431294626400e173d81f3d25889bTimo Sirainen cache->fields[fidx].used = TRUE;
9a583c7a827f7a4d89ee43774f2d51ea6a214543Timo Sirainen
a04b6515c20b431294626400e173d81f3d25889bTimo Sirainen cache->field_file_map[fidx] = i;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen cache->file_field_map[i] = field.idx;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen /* update last_used if it's newer than ours */
a04b6515c20b431294626400e173d81f3d25889bTimo Sirainen if (last_used[i] > cache->fields[fidx].last_used)
a04b6515c20b431294626400e173d81f3d25889bTimo Sirainen cache->fields[fidx].last_used = last_used[i];
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen
a04b6515c20b431294626400e173d81f3d25889bTimo Sirainen if ((time_t)cache->fields[fidx].last_used < max_drop_time &&
a04b6515c20b431294626400e173d81f3d25889bTimo Sirainen cache->fields[fidx].last_used != 0) {
9c4c535b86e9473ad97c6e9242ed84f3d9d69d0dTimo Sirainen /* time to drop this field. don't bother dropping
9c4c535b86e9473ad97c6e9242ed84f3d9d69d0dTimo Sirainen fields that have never been used. */
9a583c7a827f7a4d89ee43774f2d51ea6a214543Timo Sirainen cache->need_compress_file_seq = cache->hdr->file_seq;
9a583c7a827f7a4d89ee43774f2d51ea6a214543Timo Sirainen }
9a583c7a827f7a4d89ee43774f2d51ea6a214543Timo Sirainen
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen names = p + 1;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen }
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen return 0;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen}
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainenstatic void copy_to_buf(struct mail_cache *cache, buffer_t *dest, bool add_new,
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen size_t offset, size_t size)
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen{
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen const void *data;
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen unsigned int i, field;
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen /* copy the existing fields */
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen for (i = 0; i < cache->file_fields_count; i++) {
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen field = cache->file_field_map[i];
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen data = CONST_PTR_OFFSET(&cache->fields[field], offset);
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen buffer_append(dest, data, size);
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen }
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen if (!add_new)
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen return;
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen /* copy newly wanted fields */
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen for (i = 0; i < cache->fields_count; i++) {
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen if (CACHE_FIELD_IS_NEWLY_WANTED(cache, i)) {
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen data = CONST_PTR_OFFSET(&cache->fields[i], offset);
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen buffer_append(dest, data, size);
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen }
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen }
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen}
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainenstatic void copy_to_buf_byte(struct mail_cache *cache, buffer_t *dest,
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen bool add_new, size_t offset)
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen{
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen const int *data;
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen unsigned int i, field;
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen uint8_t byte;
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen /* copy the existing fields */
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen for (i = 0; i < cache->file_fields_count; i++) {
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen field = cache->file_field_map[i];
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen data = CONST_PTR_OFFSET(&cache->fields[field], offset);
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen byte = (uint8_t)*data;
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen buffer_append(dest, &byte, 1);
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen }
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen if (!add_new)
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen return;
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen /* copy newly wanted fields */
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen for (i = 0; i < cache->fields_count; i++) {
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen if (CACHE_FIELD_IS_NEWLY_WANTED(cache, i)) {
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen data = CONST_PTR_OFFSET(&cache->fields[i], offset);
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen byte = (uint8_t)*data;
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen buffer_append(dest, &byte, 1);
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen }
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen }
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen}
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen
6fdf8b5e4e71a69f5974f59eec2b8c19bc421fe2Timo Sirainenstatic int mail_cache_header_fields_update_locked(struct mail_cache *cache)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen{
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen buffer_t *buffer;
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen uint32_t i, offset, dec_offset;
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen int ret = 0;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen if (mail_cache_header_fields_read(cache) < 0 ||
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen mail_cache_header_fields_get_offset(cache, &offset, FALSE) < 0)
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen return -1;
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen buffer = buffer_create_dynamic(pool_datastack_create(), 256);
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen copy_to_buf(cache, buffer, FALSE,
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen offsetof(struct mail_cache_field_private, last_used),
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen sizeof(uint32_t));
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen ret = mail_cache_write(cache, buffer->data, buffer->used,
dbd9604da561399cc6255289d5b6f6f662ab2d00Timo Sirainen offset + MAIL_CACHE_FIELD_LAST_USED());
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen if (ret == 0) {
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen buffer_set_used_size(buffer, 0);
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen copy_to_buf_byte(cache, buffer, FALSE,
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen offsetof(struct mail_cache_field, decision));
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen dec_offset = offset +
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen MAIL_CACHE_FIELD_DECISION(cache->file_fields_count);
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen ret = mail_cache_write(cache, buffer->data, buffer->used,
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen dec_offset);
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen if (ret == 0) {
2c20ffcb5bb1ccdfdcd0b0ff0c7296f65b990362Timo Sirainen for (i = 0; i < cache->file_fields_count; i++)
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen cache->fields[i].decision_dirty = FALSE;
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen }
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen }
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen
114a0f74e0f825c6bd8aeadfafb248a030762a1fTimo Sirainen if (ret == 0)
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen cache->field_header_write_pending = FALSE;
6fdf8b5e4e71a69f5974f59eec2b8c19bc421fe2Timo Sirainen return ret;
6fdf8b5e4e71a69f5974f59eec2b8c19bc421fe2Timo Sirainen}
6fdf8b5e4e71a69f5974f59eec2b8c19bc421fe2Timo Sirainen
6fdf8b5e4e71a69f5974f59eec2b8c19bc421fe2Timo Sirainenint mail_cache_header_fields_update(struct mail_cache *cache)
6fdf8b5e4e71a69f5974f59eec2b8c19bc421fe2Timo Sirainen{
6fdf8b5e4e71a69f5974f59eec2b8c19bc421fe2Timo Sirainen int ret;
6fdf8b5e4e71a69f5974f59eec2b8c19bc421fe2Timo Sirainen
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen if (cache->locked) {
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen T_FRAME(
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen ret = mail_cache_header_fields_update_locked(cache);
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen );
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen return ret;
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen }
6fdf8b5e4e71a69f5974f59eec2b8c19bc421fe2Timo Sirainen
df6478c4cf605bd81b3891c148b84c14eb6c4035Timo Sirainen if (mail_cache_lock(cache, NULL) <= 0)
6fdf8b5e4e71a69f5974f59eec2b8c19bc421fe2Timo Sirainen return -1;
6fdf8b5e4e71a69f5974f59eec2b8c19bc421fe2Timo Sirainen
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen T_FRAME(
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen ret = mail_cache_header_fields_update_locked(cache);
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen );
a817fdcc43aedf423e2134091d5f83f91d64bcc9Timo Sirainen if (mail_cache_unlock(cache) < 0)
a817fdcc43aedf423e2134091d5f83f91d64bcc9Timo Sirainen ret = -1;
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen return ret;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen}
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenvoid mail_cache_header_fields_get(struct mail_cache *cache, buffer_t *dest)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen{
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen struct mail_cache_header_fields hdr;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen unsigned int field;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen const char *name;
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen uint32_t i;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen memset(&hdr, 0, sizeof(hdr));
9a583c7a827f7a4d89ee43774f2d51ea6a214543Timo Sirainen hdr.fields_count = cache->file_fields_count;
9a583c7a827f7a4d89ee43774f2d51ea6a214543Timo Sirainen for (i = 0; i < cache->fields_count; i++) {
6157a322f2ac1ea1332d9003ecb0b11466aa8fe7Timo Sirainen if (cache->fields[i].last_used == 0) {
6157a322f2ac1ea1332d9003ecb0b11466aa8fe7Timo Sirainen /* return newly added fields' last_used as
6157a322f2ac1ea1332d9003ecb0b11466aa8fe7Timo Sirainen the current time */
6157a322f2ac1ea1332d9003ecb0b11466aa8fe7Timo Sirainen cache->fields[i].last_used = ioloop_time;
6157a322f2ac1ea1332d9003ecb0b11466aa8fe7Timo Sirainen }
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen if (CACHE_FIELD_IS_NEWLY_WANTED(cache, i))
9a583c7a827f7a4d89ee43774f2d51ea6a214543Timo Sirainen hdr.fields_count++;
9a583c7a827f7a4d89ee43774f2d51ea6a214543Timo Sirainen }
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen buffer_append(dest, &hdr, sizeof(hdr));
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* we have to keep the field order for the existing fields. */
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen copy_to_buf(cache, dest, TRUE,
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen offsetof(struct mail_cache_field_private, last_used),
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen sizeof(uint32_t));
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen copy_to_buf(cache, dest, TRUE,
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen offsetof(struct mail_cache_field, field_size),
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen sizeof(uint32_t));
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen copy_to_buf_byte(cache, dest, TRUE,
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen offsetof(struct mail_cache_field, type));
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen copy_to_buf_byte(cache, dest, TRUE,
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen offsetof(struct mail_cache_field, decision));
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen i_assert(dest->used == sizeof(hdr) +
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen (sizeof(uint32_t)*2 + 2) * hdr.fields_count);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen /* add existing fields' names */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen for (i = 0; i < cache->file_fields_count; i++) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen field = cache->file_field_map[i];
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen name = cache->fields[field].field.name;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen buffer_append(dest, name, strlen(name)+1);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen }
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen /* add newly wanted fields' names */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen for (i = 0; i < cache->fields_count; i++) {
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen if (CACHE_FIELD_IS_NEWLY_WANTED(cache, i)) {
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen name = cache->fields[i].field.name;
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen buffer_append(dest, name, strlen(name)+1);
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen }
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen }
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen hdr.size = dest->used;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen buffer_write(dest, 0, &hdr, sizeof(hdr));
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if ((hdr.size & 3) != 0)
d5ac54ef50db16b50689b5c8b7bb64d344190832Timo Sirainen buffer_append_zero(dest, 4 - (hdr.size & 3));
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen}
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenint mail_cache_header_fields_get_next_offset(struct mail_cache *cache,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen uint32_t *offset_r)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen{
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen if (mail_cache_header_fields_get_offset(cache, offset_r, FALSE) < 0)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen return -1;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if (*offset_r == 0) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen *offset_r = offsetof(struct mail_cache_header,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen field_header_offset);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen } else {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen *offset_r += offsetof(struct mail_cache_header_fields,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen next_offset);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen }
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen return 0;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen}