mail-index-sync-update.c revision 1032e5427bf10566098f3b3bb9110e2bc1227e85
/* Copyright (c) 2004-2008 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "ioloop.h"
#include "array.h"
#include "mmap-util.h"
#include "mail-index-modseq.h"
#include "mail-index-view-private.h"
#include "mail-index-sync-private.h"
#include "mail-transaction-log.h"
#include "mail-transaction-log-private.h"
/* If we have less than this many bytes to sync from log file, don't bother
reading the main index */
#define MAIL_INDEX_SYNC_MIN_READ_INDEX_SIZE 2048
static void
{
&prev_seq, &prev_offset);
if (!eol) {
/* previous transaction was an extension introduction.
we probably came here from
mail_index_sync_ext_reset(). if there are any more
views which want to continue syncing it needs the
intro. so back up a bit more.
don't do this in case the last transaction in the
log is the extension intro, so we don't keep trying
to sync it over and over again. */
}
} else {
}
}
}
struct mail_index_map *map)
{
}
static void
{
}
}
struct mail_index_map *
{
}
static int
const char **error_r)
{
/* different seen-flag */
if (hdr->seen_messages_count == 0) {
*error_r = "Seen counter wrong";
return -1;
}
} else {
*error_r = "Seen counter wrong";
return -1;
}
}
}
/* different deleted-flag */
if ((old_flags & MAIL_DELETED) == 0) {
*error_r = "Deleted counter wrong";
return -1;
}
} else {
if (hdr->deleted_messages_count == 0 ||
*error_r = "Deleted counter wrong";
return -1;
}
if (--hdr->deleted_messages_count == 0)
}
}
return 0;
}
static void
{
struct mail_index_map *const *maps;
const char *error;
unsigned int i, count;
for (i = 0; i < count; i++) {
continue;
&error) < 0)
}
}
static void
{
const char *error;
if (all) {
} else {
&error) < 0)
}
}
static void
{
struct mail_index_map *const *maps;
unsigned int i, count;
for (i = 0; i < count; i++) {
if ((flags & MAIL_DELETED) != 0 &&
}
}
static void
{
const struct mail_index_expunge_handler *eh;
struct mail_index_record *rec;
unsigned int i, count;
/* call expunge handlers only when syncing index file */
return;
if (!ctx->expunge_handlers_set)
return;
/* FIXME: does expunge handler's return value matter?
we probably shouldn't disallow expunges if the
handler returns failure.. should it be just changed
to return void? */
}
}
}
static int
struct mail_index_sync_map_ctx *ctx)
{
struct mail_index_record *rec;
unsigned int i;
for (i = 0; i < count; i++, e++) {
/* everything expunged already */
continue;
}
FALSE);
}
/* @UNSAFE */
}
return 1;
}
{
}
struct mail_index_sync_map_ctx *ctx)
{
const struct mail_index_record *old_rec;
enum mail_flags new_flags;
void *dest;
"Append with UID %u, but next_uid = %u",
return -1;
}
/* move to memory. the mapping is written when unlocking so we don't
waste time re-mmap()ing multiple times or waste space growing index
file too large */
/* the flags may have changed since it was added to map.
use the updated flags already, so flag counters won't get
broken. */
} else {
/* don't rely on buffer->used being at the correct position.
at least expunges can move it */
}
if ((new_flags & MAIL_INDEX_MAIL_FLAG_DIRTY) != 0)
return 1;
}
static int sync_flag_update(const struct mail_transaction_flag_update *u,
struct mail_index_sync_map_ctx *ctx)
{
struct mail_index_record *rec;
return 1;
if (!MAIL_TRANSACTION_FLAG_UPDATE_IS_INTERNAL(u)) {
u->add_flags | u->remove_flags,
}
if ((u->add_flags & MAIL_INDEX_MAIL_FLAG_DIRTY) != 0)
flag_mask = ~u->remove_flags;
if (((u->add_flags | u->remove_flags) &
(MAIL_SEEN | MAIL_DELETED)) == 0) {
/* we're not modifying any counted/lowwatered flags */
}
} else {
}
}
return 1;
}
static int sync_header_update(const struct mail_transaction_header_update *u,
struct mail_index_sync_map_ctx *ctx)
{
#define MAIL_INDEX_HEADER_UPDATE_FIELD_IN_RANGE(u, field) \
"Header update outside range: %u + %u > %u",
return -1;
}
/* @UNSAFE */
u + 1, u->size);
}
/* UIDVALIDITY can be changed only by resetting the index */
if (orig_uid_validity != 0 &&
"uid_validity updated unexpectedly: %u -> %u",
/* let it through anyway, although this could give wrong
"next_uid shrank" errors if the value actually changed.. */
}
"next_uid shrank ignored: %u -> %u",
return -1;
}
/* the tail offset updates are intended for internal transaction
log handling. we'll update the offset in the header only when
the sync is finished. */
return 1;
}
const struct mail_transaction_header *hdr,
const void *data)
{
int ret = 0;
case MAIL_TRANSACTION_APPEND: {
if (ret <= 0)
break;
}
break;
}
case MAIL_TRANSACTION_EXPUNGE:
/* this is simply a request for expunge */
break;
}
break;
}
case MAIL_TRANSACTION_FLAG_UPDATE: {
if (ret <= 0)
break;
}
break;
}
case MAIL_TRANSACTION_HEADER_UPDATE: {
const struct mail_transaction_header_update *rec;
unsigned int i;
if (ret <= 0)
break;
if ((i % 4) != 0)
i += 4 - (i % 4);
}
break;
}
case MAIL_TRANSACTION_EXT_INTRO: {
unsigned int i;
&prev_seq, &prev_offset);
/* should be just extra padding */
break;
}
"ext intro: name_size too large");
ret = -1;
break;
}
if (ret <= 0)
break;
if ((i % 4) != 0)
i += 4 - (i % 4);
}
break;
}
case MAIL_TRANSACTION_EXT_RESET: {
struct mail_transaction_ext_reset rec;
/* old versions have only new_reset_id */
"ext reset: invalid record size");
ret = -1;
break;
}
break;
}
case MAIL_TRANSACTION_EXT_HDR_UPDATE: {
unsigned int i;
"ext hdr update: invalid record size");
ret = -1;
break;
}
if (ret <= 0)
break;
if ((i % 4) != 0)
i += 4 - (i % 4);
}
break;
}
case MAIL_TRANSACTION_EXT_REC_UPDATE: {
const struct mail_transaction_ext_rec_update *rec;
const struct mail_index_ext *ext;
unsigned int i, record_size;
"Extension record updated "
"without intro prefix");
ret = -1;
break;
}
if (ctx->cur_ext_ignore) {
ret = 1;
break;
}
/* the record is padded to 32bits in the transaction log */
"ext rec update: invalid record size");
ret = -1;
break;
}
if (ret <= 0)
break;
}
break;
}
case MAIL_TRANSACTION_KEYWORD_UPDATE: {
break;
}
case MAIL_TRANSACTION_KEYWORD_RESET: {
break;
}
default:
"Unknown transaction record type 0x%x",
ret = -1;
break;
}
return ret;
}
struct mail_index_view *view,
{
}
{
}
{
const struct mail_index_record *rec;
unsigned int i;
return;
/* do we have dirty flags anymore? */
break;
}
}
}
#ifdef DEBUG
{
for (i = 0; i < hdr->messages_count; i++) {
const struct mail_index_record *rec;
del++;
}
seen++;
else
}
}
#endif
{
struct mail_index_view *view;
struct mail_index_sync_map_ctx sync_map_ctx;
const struct mail_transaction_header *thdr;
const void *tdata;
int ret;
return 0;
}
/* see if we'd prefer to reopen the index file instead of
syncing the current map from the transaction log.
don't check this if mmap is disabled, because reopening
index causes sync to get lost. */
/* we don't know the index's size, so use the
smallest index size we're willing to read */
} else {
}
/* this isn't necessary correct currently, but it should be
close enough */
if (log_size > start_offset &&
return 0;
}
if (ret <= 0) {
(void)mail_index_fsck(index);
}
/* can't use it. sync by re-reading index. */
return 0;
}
/* view referenced the map. avoid unnecessary map cloning by
unreferencing the map while view exists. */
if (had_dirty) {
}
/* if syncing updates the header, it updates hdr_copy_buf
and updates hdr_base to hdr_copy_buf. so the buffer must
initially contain a valid header or we'll break it when
writing it. */
}
&prev_seq, &prev_offset);
if (reset) {
/* Reset the entire index. Leave only indexid and
log_file_seq. */
&prev_seq, &prev_offset);
}
/* FIXME: when transaction sync lock is removed, we'll need to handle
the case when a transaction is committed while mailbox is being
synced ([synced transactions][new transaction][ext transaction]).
this means int_offset contains [synced] and ext_offset contains
all */
&tdata)) > 0) {
&prev_seq, &prev_offset);
/* this has been synced already. we're here only to call
expunge handlers and extension update handlers. */
continue;
continue;
}
/* we'll just skip over broken entries */
T_BEGIN {
(void)mail_index_sync_record(&sync_map_ctx,
} T_END;
}
if (had_dirty)
#ifdef DEBUG
#endif
/* transaction log tracks internally the current tail offset.
besides using header updates, it also updates the offset to skip
over following external transactions to avoid extra unneeded log
reading. */
}
if (!MAIL_INDEX_MAP_IS_IN_MEMORY(map)) {
}
/* restore refcount before closing the view. this is necessary also
if map got cloned, because view closing would otherwise destroy it */
if (mail_index_map_check_header(map) <= 0) {
"Synchronization corrupted index header: %s",
(void)mail_index_fsck(index);
} else if (sync_map_ctx.errors) {
/* make sure the index looks valid now */
(void)mail_index_fsck(index);
}
}