mail-index-map.c revision a28a6267f48971117dec958b160deefd14ebb7a6
/* Copyright (C) 2003-2007 Timo Sirainen */
#include "lib.h"
#include "array.h"
#include "nfs-workarounds.h"
#include "mmap-util.h"
#include "read-full.h"
#include "mail-index-private.h"
#include "mail-index-sync-private.h"
#include "mail-transaction-log-private.h"
unsigned int initial_count)
{
#define EXTENSION_NAME_APPROX_LEN 20
#define EXT_GLOBAL_ALLOC_SIZE \
#define EXT_PER_ALLOC_SIZE \
sizeof(struct mail_index_ext) + sizeof(uint32_t))
pool_alloconly_create("map extensions",
} else {
/* try to use the existing pool's size for initial_count so
we don't grow it unneededly */
}
}
}
{
const struct mail_index_ext *extensions;
unsigned int i, size;
return (uint32_t)-1;
for (i = 0; i < size; i++) {
return i;
}
return (uint32_t)-1;
}
{
struct mail_index_ext *ext;
idx = 0;
} else {
}
/* Update index ext_id -> map ext_id mapping. Fill non-used
ext_ids with (uint32_t)-1 */
return idx;
}
{
return FALSE;
return TRUE;
}
{
}
{
const struct mail_index_ext_header *ext_hdr;
unsigned int i, old_count;
const char *name;
/* extension headers always start from 64bit offsets, so if base header
doesn't happen to be 64bit aligned we'll skip some bytes */
/* nothing to do, skip allocatations and all */
return 1;
}
for (i = 0; i < old_count; i++)
/* Extension header contains:
- struct mail_index_ext_header
- name (not 0-terminated)
- 64bit alignment padding
- extension header contents
- 64bit alignment padding
*/
"Header extension goes outside header",
return -1;
}
t_push();
"Duplicate header extension %s",
t_pop();
return -1;
}
*name == '\0') {
"Broken header extension %s",
t_pop();
return -1;
}
"Record field %s points outside record size "
t_pop();
return -1;
}
if (ext_hdr->record_size > 0 &&
"Record field %s alignmentation %u not used",
t_pop();
return -1;
}
t_pop();
}
return 1;
}
const struct mail_index_header *hdr,
{
enum mail_index_header_compat_flags compat_flags = 0;
#ifndef WORDS_BIGENDIAN
#endif
/* major version change - handle silently(?) */
return FALSE;
}
/* we've already complained about it */
return FALSE;
}
/* architecture change */
"CPU architecture changed",
return FALSE;
}
"Corrupted header sizes (base %u, full %u)",
hdr->header_size);
return FALSE;
}
return FALSE;
}
"indexid changed: %u -> %u",
}
}
return TRUE;
}
{
return -1;
/* following some extra checks that only take a bit of CPU */
"uid_validity = 0, next_uid = %u",
return -1;
}
"record_size too small: %u < %"PRIuSIZE_T,
sizeof(struct mail_index_record));
return -1;
}
return 0;
return 0;
return 0;
if (hdr->first_recent_uid == 0 ||
return 0;
if (hdr->messages_count > 0) {
/* last message's UID must be smaller than next_uid.
also make sure it's not zero. */
const struct mail_index_record *rec;
return 0;
}
return 1;
}
const struct mail_index_header *hdr)
{
/* header smaller than ours, make a copy so our newer headers
won't have garbage in them */
} else {
}
}
{
const struct mail_index_header *hdr;
unsigned int records_count;
if (file_size > SSIZE_T_MAX) {
/* too large file to map into memory */
return -1;
}
return -1;
}
/* major version change - handle silently */
return 0;
}
return 0;
}
/* Can't use this file */
return 0;
}
"messages_count too large (%u > %u)",
return 0;
}
return 1;
}
{
int ret;
/* try to read the whole header, but it's not necessarily an error to
read less since the older versions of the index format could be
smaller. Request reading up to buf_size, but accept if we only got
the header. */
pos = 0;
do {
if (ret > 0)
return ret;
}
static int
{
const struct mail_index_header *hdr;
unsigned char read_buf[4096];
const void *buf;
unsigned int records_count, extra;
/* major version change - handle silently */
return 0;
}
/* Can't use this file */
return 0;
}
/* place the base header into memory. */
/* @UNSAFE: read the rest of the header into memory */
hdr->header_size -
pos);
}
}
if (ret > 0) {
/* header read, read the records now. */
(hdr->record_size != 0 &&
"messages_count too large (%u > %u)",
return 0;
}
}
/* @UNSAFE */
extra = 0;
else {
extra);
}
if (records_size > extra) {
records_size - extra);
}
}
if (ret < 0) {
/* a new index file was renamed over this one. */
return 0;
}
return -1;
}
if (ret == 0) {
"Corrupted index file %s: File too small",
return 0;
}
return 1;
}
{
mail_index_sync_lost_handler_t *const *handlers;
unsigned int i, count;
int ret;
/* notify all "sync lost" handlers */
for (i = 0; i < count; i++)
for (i = 0;; i++) {
/* fstat() below failed */
ret = 0;
} else {
}
break;
/* ESTALE - reopen index file */
if (ret <= 0) {
if (ret == 0) {
/* the file was lost */
}
return -1;
}
else {
return -1;
}
}
}
return ret;
}
struct mail_index_header *hdr)
{
#ifndef WORDS_BIGENDIAN
#endif
}
{
struct mail_index_map tmp_map;
/* a bit kludgy way to do this, but it initializes everything
nicely and correctly */
return mail_index_map_clone(&tmp_map);
}
struct mail_index_map **map)
{
struct mail_index_map *new_map;
unsigned int lock_id;
bool use_mmap;
int ret;
if (ret <= 0) {
if (ret < 0)
return -1;
build it from the transaction log. */
return 0;
}
/* the index file is still open, lock it */
return -1;
else {
return -1;
}
}
/* mmaping seems to be slower than just reading the file, so even if
mmap isn't disabled don't use it unless the file is large enough */
if (use_mmap) {
} else {
}
if (ret > 0) {
/* make sure the header is ok before using this mapping */
if (ret >= 0)
if (ret++ == 0)
}
if (ret <= 0) {
return ret;
}
return 1;
}
{
int ret;
/* first try updating the existing mapping from transaction log. */
/* we're not creating the index, or opening transaction log.
sync this as a view from transaction log. */
} else {
ret = 0;
}
if (ret == 0) {
/* try to open and read the latest index. if it fails for
any reason, we'll fallback to updating the existing mapping
from transaction logs (which we'll also do even if the
reopening succeeds) */
/* if we're creating the index file, we don't have any
logs yet */
/* and update the map with the latest changes from
transaction log */
}
}
return ret;
}
struct mail_index_record_map *rec_map)
{
}
}
{
struct mail_index_map *const *maps;
unsigned int i, count;
for (i = 0; i < count; i++) {
if (i == 0 && count == 1)
return;
}
}
i_unreached();
}
{
return;
}
{
return 0;
return 0;
}
return -1;
return 0;
}
{
return;
}
const struct mail_index_record_map *src,
unsigned int record_size)
{
/* if the map is ever written back to disk, we need to keep track of
what has changed. */
}
const struct mail_index_map *src)
{
/* use src->hdr copy directly, because if we got here
from syncing it has the latest changes. */
else {
dest->hdr_copy_buf =
}
}
}
static struct mail_index_record_map *
{
struct mail_index_record_map *rec_map;
return rec_map;
}
{
struct mail_index_map *mem_map;
struct mail_index_ext *extensions;
unsigned int i, count;
} else {
}
/* copy extensions */
/* fix the name pointers to use our own pool */
for (i = 0; i < count; i++) {
extensions[i].record_size <=
extensions[i].name);
}
}
return mem_map;
}
{
struct mail_index_record_map *new_map;
return;
}
{
struct mail_index_record_map *new_map;
return;
}
}
{
return 0;
}