mail-cache-fields.c revision bc793bfcee945ce8871edfa298fe7235744425b6
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen/* Copyright (C) 2004 Timo Sirainen */
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "lib.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "buffer.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "hash.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "file-cache.h"
1e21e6be70994b1aa9e52ca0e2f51afefca6d0dfTimo Sirainen#include "write-full.h"
1e21e6be70994b1aa9e52ca0e2f51afefca6d0dfTimo Sirainen#include "mail-cache-private.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
86bea1f8bffc2d98196f8655eecea9174c4f458aTimo Sirainen#include <stddef.h>
86bea1f8bffc2d98196f8655eecea9174c4f458aTimo Sirainen
86bea1f8bffc2d98196f8655eecea9174c4f458aTimo Sirainen#define CACHE_HDR_PREFETCH 1024
86bea1f8bffc2d98196f8655eecea9174c4f458aTimo Sirainen
86bea1f8bffc2d98196f8655eecea9174c4f458aTimo Sirainenstatic bool field_has_fixed_size(enum mail_cache_field_type type)
86bea1f8bffc2d98196f8655eecea9174c4f458aTimo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen switch (type) {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen case MAIL_CACHE_FIELD_FIXED_SIZE:
b82474d60c15409eda71c55971710fd3b12b8a0fTimo Sirainen case MAIL_CACHE_FIELD_BITMASK:
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen return TRUE;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen case MAIL_CACHE_FIELD_VARIABLE_SIZE:
7d6389e4053c2dac1fb37180b5756b00785983dcTimo Sirainen case MAIL_CACHE_FIELD_STRING:
7d6389e4053c2dac1fb37180b5756b00785983dcTimo Sirainen case MAIL_CACHE_FIELD_HEADER:
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainen return FALSE;
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen case MAIL_CACHE_FIELD_COUNT:
7d6389e4053c2dac1fb37180b5756b00785983dcTimo Sirainen break;
484e12acec34f16e5a8adc001e23ae48f1dda8c7Timo Sirainen }
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen
22535a9e685e29214082878e37a267157044618eTimo Sirainen i_unreached();
b321df9603081896b70ec44635af96d674a9839aTimo Sirainen return FALSE;
b321df9603081896b70ec44635af96d674a9839aTimo Sirainen}
b321df9603081896b70ec44635af96d674a9839aTimo Sirainen
b321df9603081896b70ec44635af96d674a9839aTimo Sirainenstatic bool field_decision_is_valid(enum mail_cache_decision_type type)
b321df9603081896b70ec44635af96d674a9839aTimo Sirainen{
b321df9603081896b70ec44635af96d674a9839aTimo Sirainen switch (type & ~MAIL_CACHE_DECISION_FORCED) {
b321df9603081896b70ec44635af96d674a9839aTimo Sirainen case MAIL_CACHE_DECISION_NO:
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen case MAIL_CACHE_DECISION_TEMP:
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen case MAIL_CACHE_DECISION_YES:
7d6389e4053c2dac1fb37180b5756b00785983dcTimo Sirainen return TRUE;
7d6389e4053c2dac1fb37180b5756b00785983dcTimo Sirainen default:
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen return FALSE;
a3dd97fb6d92a89c3de0597fed2d4b044c7aeb84Timo Sirainen }
a3dd97fb6d92a89c3de0597fed2d4b044c7aeb84Timo Sirainen}
7d6389e4053c2dac1fb37180b5756b00785983dcTimo Sirainen
7d6389e4053c2dac1fb37180b5756b00785983dcTimo Sirainenstatic int field_type_verify(struct mail_cache *cache, unsigned int idx,
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen enum mail_cache_field_type type, unsigned int size)
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen{
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen const struct mail_cache_field *field = &cache->fields[idx].field;
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen if (field->type != type) {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen mail_cache_set_corrupted(cache,
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen "registered field %s type changed", field->name);
25ee72451d16374ed27fdbf829f4ec756c778352Timo Sirainen return -1;
25ee72451d16374ed27fdbf829f4ec756c778352Timo Sirainen }
25ee72451d16374ed27fdbf829f4ec756c778352Timo Sirainen if (field->field_size != size && field_has_fixed_size(type)) {
25ee72451d16374ed27fdbf829f4ec756c778352Timo Sirainen mail_cache_set_corrupted(cache,
25ee72451d16374ed27fdbf829f4ec756c778352Timo Sirainen "registered field %s size changed", field->name);
25ee72451d16374ed27fdbf829f4ec756c778352Timo Sirainen return -1;
25ee72451d16374ed27fdbf829f4ec756c778352Timo Sirainen }
25ee72451d16374ed27fdbf829f4ec756c778352Timo Sirainen return 0;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen}
25ee72451d16374ed27fdbf829f4ec756c778352Timo Sirainen
25ee72451d16374ed27fdbf829f4ec756c778352Timo Sirainenvoid mail_cache_register_fields(struct mail_cache *cache,
25ee72451d16374ed27fdbf829f4ec756c778352Timo Sirainen struct mail_cache_field *fields,
25ee72451d16374ed27fdbf829f4ec756c778352Timo Sirainen unsigned int fields_count)
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen{
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen void *orig_key, *orig_value;
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen char *name;
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen unsigned int new_idx;
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen unsigned int i, j;
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen new_idx = cache->fields_count;
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen for (i = 0; i < fields_count; i++) {
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen if (hash_lookup_full(cache->field_name_hash, fields[i].name,
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen &orig_key, &orig_value)) {
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen i_assert(fields[i].type < MAIL_CACHE_FIELD_COUNT);
a3dd97fb6d92a89c3de0597fed2d4b044c7aeb84Timo Sirainen
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainen fields[i].idx =
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen POINTER_CAST_TO(orig_value, unsigned int);
473080c7c0d25ddfdf77e7dfa0ba8f73c6c669d5Timo Sirainen (void)field_type_verify(cache, fields[i].idx,
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainen fields[i].type,
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen fields[i].field_size);
157bce86d0a01477bb8ebd0d380e6b2297f326f7Timo Sirainen continue;
8eb94c5190ba09bb6f6f068eec7bf96750f08d1dTimo Sirainen }
e9503210d3521a6833ed62dc332fc42ffb0e7a13Timo Sirainen
e9503210d3521a6833ed62dc332fc42ffb0e7a13Timo Sirainen /* check if the same header is being registered in the
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen same field array */
43a66a0b16299bd4f7615acd85e98bd3832c54d5Timo Sirainen for (j = 0; j < i; j++) {
43a66a0b16299bd4f7615acd85e98bd3832c54d5Timo Sirainen if (strcasecmp(fields[i].name, fields[j].name) == 0) {
43a66a0b16299bd4f7615acd85e98bd3832c54d5Timo Sirainen fields[i].idx = fields[j].idx;
43a66a0b16299bd4f7615acd85e98bd3832c54d5Timo Sirainen break;
43a66a0b16299bd4f7615acd85e98bd3832c54d5Timo Sirainen }
43a66a0b16299bd4f7615acd85e98bd3832c54d5Timo Sirainen }
1e21e6be70994b1aa9e52ca0e2f51afefca6d0dfTimo Sirainen
1e21e6be70994b1aa9e52ca0e2f51afefca6d0dfTimo Sirainen if (j == i)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen fields[i].idx = new_idx++;
}
if (new_idx == cache->fields_count)
return;
/* @UNSAFE */
cache->fields = i_realloc(cache->fields,
cache->fields_count * sizeof(*cache->fields),
new_idx * sizeof(*cache->fields));
cache->field_file_map =
i_realloc(cache->field_file_map,
cache->fields_count * sizeof(*cache->field_file_map),
new_idx * sizeof(*cache->field_file_map));
for (i = 0; i < fields_count; i++) {
unsigned int idx = fields[i].idx;
if (idx < cache->fields_count)
continue;
/* new index - save it */
name = p_strdup(cache->field_pool, fields[i].name);
cache->fields[idx].field = fields[i];
cache->fields[idx].field.name = name;
cache->field_file_map[idx] = (uint32_t)-1;
if (!field_has_fixed_size(cache->fields[idx].field.type))
cache->fields[idx].field.field_size = (unsigned int)-1;
hash_insert(cache->field_name_hash, name, POINTER_CAST(idx));
}
cache->fields_count = new_idx;
}
unsigned int
mail_cache_register_lookup(struct mail_cache *cache, const char *name)
{
void *orig_key, *orig_value;
if (hash_lookup_full(cache->field_name_hash, name,
&orig_key, &orig_value))
return POINTER_CAST_TO(orig_value, unsigned int);
else
return (unsigned int)-1;
}
const struct mail_cache_field *
mail_cache_register_get_list(struct mail_cache *cache, pool_t pool,
unsigned int *count_r)
{
struct mail_cache_field *list;
unsigned int i;
if (!cache->opened)
(void)mail_cache_open_and_verify(cache);
list = p_new(pool, struct mail_cache_field, cache->fields_count);
for (i = 0; i < cache->fields_count; i++) {
list[i] = cache->fields[i].field;
list[i].name = p_strdup(pool, list[i].name);
}
*count_r = cache->fields_count;
return list;
}
static int mail_cache_header_fields_get_offset(struct mail_cache *cache,
uint32_t *offset_r)
{
const struct mail_cache_header_fields *field_hdr;
uint32_t offset, next_offset;
if (MAIL_CACHE_IS_UNUSABLE(cache)) {
*offset_r = 0;
return 0;
}
/* find the latest header */
offset = 0;
next_offset =
mail_index_offset_to_uint32(cache->hdr->field_header_offset);
while (next_offset != 0) {
if (next_offset == offset) {
mail_cache_set_corrupted(cache,
"next_offset in field header loops");
return -1;
}
offset = next_offset;
if (cache->file_cache != NULL) {
/* we can't trust that the cached data is valid */
file_cache_invalidate(cache->file_cache, offset,
sizeof(*field_hdr) +
CACHE_HDR_PREFETCH);
}
if (mail_cache_map(cache, offset,
sizeof(*field_hdr) + CACHE_HDR_PREFETCH) < 0)
return -1;
field_hdr = CONST_PTR_OFFSET(cache->data, offset);
next_offset =
mail_index_offset_to_uint32(field_hdr->next_offset);
}
*offset_r = offset;
return 0;
}
int mail_cache_header_fields_read(struct mail_cache *cache)
{
const struct mail_cache_header_fields *field_hdr = NULL;
struct mail_cache_field field;
const uint32_t *last_used, *sizes;
const uint8_t *types, *decisions;
const char *p, *names, *end;
void *orig_key, *orig_value;
unsigned int new_fields_count;
uint32_t offset, i;
if (mail_cache_header_fields_get_offset(cache, &offset) < 0)
return -1;
if (offset == 0) {
/* no fields - the file is empty */
return 0;
}
field_hdr = CONST_PTR_OFFSET(cache->data, offset);
if (offset + field_hdr->size > cache->mmap_length) {
mail_cache_set_corrupted(cache,
"field header points outside file");
return -1;
}
/* check the fixed size of the header. name[] has to be checked
separately */
if (field_hdr->size < sizeof(*field_hdr) +
field_hdr->fields_count * (sizeof(uint32_t)*2 + 1 + 2)) {
mail_cache_set_corrupted(cache, "invalid field header size");
return -1;
}
if (field_hdr->size > sizeof(*field_hdr) + CACHE_HDR_PREFETCH) {
if (cache->file_cache != NULL) {
/* we can't trust that the cached data is valid */
file_cache_invalidate(cache->file_cache, offset,
sizeof(*field_hdr) +
CACHE_HDR_PREFETCH);
}
if (mail_cache_map(cache, offset, field_hdr->size) < 0)
return -1;
}
field_hdr = CONST_PTR_OFFSET(cache->data, offset);
new_fields_count = field_hdr->fields_count;
if (new_fields_count != 0) {
cache->file_field_map =
i_realloc(cache->file_field_map,
cache->file_fields_count *
sizeof(unsigned int),
new_fields_count * sizeof(unsigned int));
} else {
i_free_and_null(cache->file_field_map);
}
cache->file_fields_count = new_fields_count;
last_used = CONST_PTR_OFFSET(field_hdr, MAIL_CACHE_FIELD_LAST_USED());
sizes = CONST_PTR_OFFSET(field_hdr,
MAIL_CACHE_FIELD_SIZE(field_hdr->fields_count));
types = CONST_PTR_OFFSET(field_hdr,
MAIL_CACHE_FIELD_TYPE(field_hdr->fields_count));
decisions = CONST_PTR_OFFSET(field_hdr,
MAIL_CACHE_FIELD_DECISION(field_hdr->fields_count));
names = CONST_PTR_OFFSET(field_hdr,
MAIL_CACHE_FIELD_NAMES(field_hdr->fields_count));
end = CONST_PTR_OFFSET(field_hdr, field_hdr->size);
/* clear the old mapping */
for (i = 0; i < cache->fields_count; i++)
cache->field_file_map[i] = (uint32_t)-1;
memset(&field, 0, sizeof(field));
for (i = 0; i < field_hdr->fields_count; i++) {
for (p = names; p != end && *p != '\0'; p++) ;
if (p == end || *names == '\0') {
mail_cache_set_corrupted(cache,
"field header names corrupted");
return -1;
}
if (types[i] > MAIL_CACHE_FIELD_COUNT) {
mail_cache_set_corrupted(cache, "field type corrupted");
return -1;
}
if (!field_decision_is_valid(decisions[i])) {
mail_cache_set_corrupted(cache,
"field decision type corrupted");
return -1;
}
if (hash_lookup_full(cache->field_name_hash, names,
&orig_key, &orig_value)) {
/* already exists, see if decision can be updated */
field.idx = POINTER_CAST_TO(orig_value, unsigned int);
if (!cache->fields[field.idx].decision_dirty) {
cache->fields[field.idx].field.decision =
decisions[i];
}
if (field_type_verify(cache, field.idx,
types[i], sizes[i]) < 0)
return -1;
} else {
field.name = names;
field.type = types[i];
field.field_size = sizes[i];
field.decision = decisions[i];
mail_cache_register_fields(cache, &field, 1);
}
if (cache->field_file_map[field.idx] != (uint32_t)-1) {
mail_cache_set_corrupted(cache,
"Duplicated field in header: %s", names);
return -1;
}
cache->field_file_map[field.idx] = i;
cache->file_field_map[i] = field.idx;
/* update last_used if it's newer than ours */
if (last_used[i] > cache->fields[field.idx].last_used)
cache->fields[field.idx].last_used = last_used[i];
names = p + 1;
}
return 0;
}
static void copy_to_buf(struct mail_cache *cache, buffer_t *dest,
size_t offset, size_t size)
{
const void *data;
unsigned int i, field;
for (i = 0; i < cache->file_fields_count; i++) {
field = cache->file_field_map[i];
data = CONST_PTR_OFFSET(&cache->fields[field], offset);
buffer_append(dest, data, size);
}
for (i = 0; i < cache->fields_count; i++) {
if (cache->field_file_map[i] != (uint32_t)-1)
continue;
data = CONST_PTR_OFFSET(&cache->fields[i], offset);
buffer_append(dest, data, size);
}
}
static void copy_to_buf_byte(struct mail_cache *cache, buffer_t *dest,
size_t offset)
{
const int *data;
unsigned int i, field;
uint8_t byte;
for (i = 0; i < cache->file_fields_count; i++) {
field = cache->file_field_map[i];
data = CONST_PTR_OFFSET(&cache->fields[field], offset);
byte = (uint8_t)*data;
buffer_append(dest, &byte, 1);
}
for (i = 0; i < cache->fields_count; i++) {
if (cache->field_file_map[i] != (uint32_t)-1)
continue;
data = CONST_PTR_OFFSET(&cache->fields[i], offset);
byte = (uint8_t)*data;
buffer_append(dest, &byte, 1);
}
}
static int mail_cache_header_fields_update_locked(struct mail_cache *cache)
{
buffer_t *buffer;
uint32_t i, offset;
int ret = 0;
if (mail_cache_header_fields_read(cache) < 0 ||
mail_cache_header_fields_get_offset(cache, &offset) < 0)
return -1;
t_push();
buffer = buffer_create_dynamic(pool_datastack_create(), 256);
copy_to_buf(cache, buffer,
offsetof(struct mail_cache_field_private, last_used),
sizeof(uint32_t));
ret = mail_cache_write(cache, buffer->data,
sizeof(uint32_t) * cache->file_fields_count,
offset + MAIL_CACHE_FIELD_LAST_USED());
if (ret == 0) {
buffer_set_used_size(buffer, 0);
copy_to_buf_byte(cache, buffer,
offsetof(struct mail_cache_field, decision));
ret = mail_cache_write(cache, buffer->data,
sizeof(uint8_t) * cache->file_fields_count, offset +
MAIL_CACHE_FIELD_DECISION(cache->file_fields_count));
if (ret == 0) {
for (i = 0; i < cache->file_fields_count; i++)
cache->fields[i].decision_dirty = FALSE;
}
}
t_pop();
if (ret == 0)
cache->field_header_write_pending = FALSE;
return ret;
}
int mail_cache_header_fields_update(struct mail_cache *cache)
{
int ret;
if (cache->locked)
return mail_cache_header_fields_update_locked(cache);
if (mail_cache_lock(cache, NULL) <= 0)
return -1;
ret = mail_cache_header_fields_update_locked(cache);
if (mail_cache_unlock(cache) < 0)
ret = -1;
return ret;
}
void mail_cache_header_fields_get(struct mail_cache *cache, buffer_t *dest)
{
struct mail_cache_header_fields hdr;
unsigned int field;
const char *name;
uint32_t i;
memset(&hdr, 0, sizeof(hdr));
hdr.fields_count = cache->fields_count;
buffer_append(dest, &hdr, sizeof(hdr));
/* we have to keep the field order for the existing fields. */
copy_to_buf(cache, dest,
offsetof(struct mail_cache_field_private, last_used),
sizeof(uint32_t));
copy_to_buf(cache, dest, offsetof(struct mail_cache_field, field_size),
sizeof(uint32_t));
copy_to_buf_byte(cache, dest, offsetof(struct mail_cache_field, type));
copy_to_buf_byte(cache, dest,
offsetof(struct mail_cache_field, decision));
i_assert(buffer_get_used_size(dest) == sizeof(hdr) +
(sizeof(uint32_t)*2 + 2) * hdr.fields_count);
for (i = 0; i < cache->file_fields_count; i++) {
field = cache->file_field_map[i];
name = cache->fields[field].field.name;
buffer_append(dest, name, strlen(name)+1);
}
for (i = 0; i < cache->fields_count; i++) {
if (cache->field_file_map[i] != (uint32_t)-1)
continue;
name = cache->fields[i].field.name;
buffer_append(dest, name, strlen(name)+1);
}
hdr.size = buffer_get_used_size(dest);
buffer_write(dest, 0, &hdr, sizeof(hdr));
if ((hdr.size & 3) != 0)
buffer_append_zero(dest, 4 - (hdr.size & 3));
}
int mail_cache_header_fields_get_next_offset(struct mail_cache *cache,
uint32_t *offset_r)
{
if (mail_cache_header_fields_get_offset(cache, offset_r) < 0)
return -1;
if (*offset_r == 0) {
*offset_r = offsetof(struct mail_cache_header,
field_header_offset);
} else {
*offset_r += offsetof(struct mail_cache_header_fields,
next_offset);
}
return 0;
}