view src/lib-index/maildir/maildir-rebuild.c @ 903:fd8888f6f037 HEAD

Naming style changes, finally got tired of most of the typedefs. Also the previous enum -> macro change reverted so that we don't use the highest bit anymore, that's incompatible with old indexes so they will be rebuilt.
author Timo Sirainen <tss@iki.fi>
date Sun, 05 Jan 2003 15:09:51 +0200
parents 86cf24da85f1
children e80c784252ea
line wrap: on
line source

/* Copyright (C) 2002 Timo Sirainen */

#include "lib.h"
#include "maildir-index.h"
#include "mail-index-data.h"
#include "mail-index-util.h"

#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>

int maildir_index_rebuild(struct mail_index *index)
{
	struct stat st;
	const char *cur_dir, *new_dir;

	i_assert(index->lock_type != MAIL_LOCK_SHARED);

	if (!mail_index_set_lock(index, MAIL_LOCK_EXCLUSIVE))
		return FALSE;

	/* reset the header */
	mail_index_init_header(index, index->header);
	index->mmap_used_length = index->header->used_file_size;

	/* require these fields */
	index->header->cache_fields |= DATA_FIELD_LOCATION;

	/* update indexid, which also means that our state has completely
	   changed */
	index->indexid = index->header->indexid;
	index->inconsistent = TRUE;

	if (msync(index->mmap_base,
		  sizeof(struct mail_index_header), MS_SYNC) < 0)
		return FALSE;

	/* reset data file */
	if (!mail_index_data_reset(index->data))
		return FALSE;

	/* rebuild cur/ directory */
	cur_dir = t_strconcat(index->mailbox_path, "/cur", NULL);
	if (!maildir_index_build_dir(index, cur_dir, NULL))
		return FALSE;

	/* also see if there's new mail */
	new_dir = t_strconcat(index->mailbox_path, "/new", NULL);
	if (!maildir_index_build_dir(index, new_dir, cur_dir))
		return FALSE;

	/* update sync stamp */
	if (stat(cur_dir, &st) < 0)
		return index_file_set_syscall_error(index, cur_dir, "stat()");

	index->file_sync_stamp = st.st_mtime;

	/* rebuild is complete - remove the flag */
	index->header->flags &= ~(MAIL_INDEX_FLAG_REBUILD|MAIL_INDEX_FLAG_FSCK);
	return TRUE;
}