mail-hash.c revision e89b9e95f9f6fef47e48d03007591ca832d00f91
/* Copyright (C) 2002 Timo Sirainen */
#include "lib.h"
#include "ioloop.h"
#include "primes.h"
#include "mmap-util.h"
#include "write-full.h"
#include "mail-index.h"
#include "mail-index-util.h"
#include "mail-hash.h"
#include <stdio.h>
#include <fcntl.h>
/* Minimum record count for a hash file. By default, the hash file size is
the number of messages * 3, and it's rebuilt after the file is 3/4 full.
Use only primes as hash file sizes. */
#define MIN_HASH_SIZE 109
/* Maximum record count for a hash file. */
#define MAX_HASH_SIZE \
/* When rebuilding hash, make it 30% full */
#define MIN_PERCENTAGE 30
/* Try rebuilding hash sometimes soon after it's 60% full */
#define REBUILD_PERCENTAGE 60
/* Force a rebuild when hash is 80% full */
#define FORCED_REBUILD_PERCENTAGE 80
/* our hashing function is simple - UID*2. The *2 is there because UIDs are
normally contiguous, so once the UIDs wrap around, we don't want to go
through lots of records just to find an empty spot */
struct _MailHash {
unsigned int sync_id;
unsigned int size;
int fd;
char *filepath;
void *mmap_base;
unsigned int dirty_mmap:1;
unsigned int modified:1;
};
{
"hash: mmap() failed with file %s: %m",
return FALSE;
}
return TRUE;
}
{
sizeof(MailHashRecord) != 0) {
/* hash must be corrupted, rebuilding should have noticed
if it was only partially written. */
(unsigned long) hash->mmap_length);
return FALSE;
}
sizeof(MailHashRecord);
/* invalid size, probably corrupted. */
return FALSE;
}
return TRUE;
}
{
return FALSE;
if (!hash->dirty_mmap) {
/* see if someone else modified it */
return TRUE;
}
}
{
return hash;
}
{
}
{
int failed;
return mail_hash_rebuild(hash);
if (!mmap_update_real(hash)) {
/* mmap() failure is fatal */
return FALSE;
}
/* make sure the header looks fine */
if (hash_verify_header(hash)) {
else {
"IndexID mismatch for hash file %s",
}
}
if (failed) {
/* recreate it */
return mail_hash_rebuild(hash);
}
return TRUE;
}
{
}
}
{
char block[1024];
unsigned int i, full_blocks;
/* try truncating it to the size we want. if this succeeds, the written
area is full of zeros - exactly what we want. however, this may not
work at all, in which case we fallback to write()ing the zeros. */
if (pos == -1)
return FALSE;
/* ftruncate() failed for some reason, even while we were
trying to make the file smaller */
return FALSE;
}
/* start growing the file */
/* write in 1kb blocks */
for (i = 0; i < full_blocks; i++) {
return FALSE;
}
/* write the remainder */
}
unsigned int hash_size,
unsigned int messages_count)
{
void *mmap_base;
unsigned int i, count;
/* fill the file with zeros */
"Failed to fill temp hash to size %lu: %m",
(unsigned long) new_size);
return FALSE;
}
/* now, mmap() it */
if (mmap_base == MAP_FAILED) {
return FALSE;
}
/* we have empty hash file mmap()ed now. fill it by reading the
messages from index. */
}
if (count != messages_count) {
/* mark this as an error but don't fail because of it. */
"hash file %s - %u found, header says %u",
}
/* setup header */
}
{
return TRUE;
return TRUE;
else {
return FALSE;
}
}
{
const char *path;
unsigned int hash_size;
int fd;
return FALSE;
/* first get the number of messages in index */
/* then figure out size for our hash */
if (hash_size < MIN_HASH_SIZE)
hash_size > MAX_HASH_SIZE) {
/* either our calculation overflowed, or we reached the
max. value primes_closest() gave us. and there's more
mails - very unlikely. */
"max. hash file size reached for %s",
return FALSE;
}
/* create the hash in a new temp file */
if (fd == -1)
return FALSE;
return FALSE;
}
"fsync() failed with temp hash %s: %m", path);
return FALSE;
}
/* replace old hash file with this new one */
return FALSE;
}
/* switch fds */
return TRUE;
}
{
if (!mmap_update(hash))
return 0;
sizeof(MailHashHeader));
/* check from hash index to end of file */
/* empty hash record - not found. */
return 0;
}
}
/* check from beginning of file to hash index */
/* empty hash record - not found. */
return 0;
}
}
/* checked through the whole hash file. this really shouldn't happen,
we should have rebuilt it long time ago.. */
return 0;
}
{
sizeof(MailHashHeader));
/* check from hash index to end of file */
}
/* check from beginning of file to hash index */
}
/* hash file is full */
return NULL;
}
{
unsigned int max_used;
if (!mmap_update(hash))
return;
/* we really need a rebuild. */
}
/* place the hash into first free record after wanted position */
/* this should never happen, we had already checked that
at least 1/5 of hash was empty. except, if the header
contained invalid record count for some reason. rebuild.. */
i_error("Hash file was 100%% full, rebuilding");
}
if (pos != 0) {
/* update records count, and see if hash is
getting full */
}
}
} else {
/* delete record */
}
}