view src/lib-storage/index/index-msgcache.c @ 160:ff05b320482c HEAD

Bigger changes.. full_virtual_size was removed from index record and MessagePart caching is now forced. Also added per-message flags, including binary flags which can be used to check if CRs need to be inserted into message data. Added mbox-rewrite support which can be used to write out mbox file with updated flags. This still has the problem of being able to read changed custom flags, that'll require another bigger change. There's also several other mostly mbox related fixes.
author Timo Sirainen <tss@iki.fi>
date Fri, 06 Sep 2002 16:43:58 +0300
parents 76a3a6c0c452
children db6e288be0e9
line wrap: on
line source

/* Copyright (C) 2002 Timo Sirainen */

#include "lib.h"
#include "iobuffer.h"
#include "imap-message-cache.h"
#include "message-part-serialize.h"
#include "mail-index.h"

#include <unistd.h>

typedef struct {
	MailIndex *index;
	MailIndexRecord *rec;
} IndexMsgcacheContext;

void *index_msgcache_get_context(MailIndex *index, MailIndexRecord *rec)
{
	IndexMsgcacheContext *ctx;

	ctx = t_new(IndexMsgcacheContext, 1);
	ctx->index = index;
	ctx->rec = rec;
	return ctx;
}

static IOBuffer *index_msgcache_open_mail(void *context)
{
        IndexMsgcacheContext *ctx = context;

	return ctx->index->open_mail(ctx->index, ctx->rec);
}

static IOBuffer *index_msgcache_inbuf_rewind(IOBuffer *inbuf,
					     void *context __attr_unused__)
{
	if (!io_buffer_seek(inbuf, 0)) {
		i_error("index_msgcache_inbuf_rewind: lseek() failed: %m");

		(void)close(inbuf->fd);
		io_buffer_destroy(inbuf);
		return NULL;
	}

	return inbuf;
}

static const char *index_msgcache_get_cached_field(ImapCacheField field,
						   void *context)
{
	IndexMsgcacheContext *ctx = context;
	MailField index_field;

	switch (field) {
	case IMAP_CACHE_BODY:
		index_field = FIELD_TYPE_BODY;
		break;
	case IMAP_CACHE_BODYSTRUCTURE:
		index_field = FIELD_TYPE_BODYSTRUCTURE;
		break;
	case IMAP_CACHE_ENVELOPE:
		index_field = FIELD_TYPE_ENVELOPE;
		break;
	default:
		index_field = 0;
	}

	return index_field == 0 ? NULL :
		ctx->index->lookup_field(ctx->index, ctx->rec, index_field);
}

static MessagePart *index_msgcache_get_cached_parts(Pool pool, void *context)
{
	IndexMsgcacheContext *ctx = context;
	MessagePart *part;
	const void *part_data;
	unsigned int part_size;

	part_data = ctx->index->lookup_field_raw(ctx->index, ctx->rec,
						 FIELD_TYPE_MESSAGEPART,
						 &part_size);
	if (part_data == NULL)
		return NULL;

	part = message_part_deserialize(pool, part_data, part_size);
	if (part == NULL) {
		i_error("Error in index file %s: Corrupted cached "
			"MessagePart data", ctx->index->filepath);
		return NULL;
	}

	return part;
}

ImapMessageCacheIface index_msgcache_iface = {
	index_msgcache_open_mail,
	index_msgcache_inbuf_rewind,
	index_msgcache_get_cached_field,
	index_msgcache_get_cached_parts
};