view src/lib-index/maildir/maildir-rebuild.c @ 221:ed0d5b17c7a4 HEAD

Added extra functions for easier printing of error messages. Moved file_set_size() to generic function in lib. If there's no space to build hash file, it builds itself in anon-mmaped memory and stays there.
author Timo Sirainen <tss@iki.fi>
date Fri, 13 Sep 2002 03:01:23 +0300
parents 3b1985cbc908
children cf4d065f2f85
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 "mail-hash.h"

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

int maildir_index_rebuild(MailIndex *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->header);

	/* update indexid */
	index->indexid = index->header->indexid;

	if (msync(index->mmap_base, sizeof(MailIndexHeader), MS_SYNC) == -1)
		return FALSE;

	/* truncate the file first, so it won't contain
	   any invalid data even if we crash */
	if (ftruncate(index->fd, sizeof(MailIndexHeader)) == -1) {
		index_set_syscall_error(index, "ftruncate()");
		return FALSE;
	}

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

	/* rebuild cur/ directory */
	cur_dir = t_strconcat(index->dir, "/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->dir, "/new", NULL);
	if (!maildir_index_build_dir(index, new_dir, cur_dir))
		return FALSE;

	/* update sync stamp */
	if (stat(cur_dir, &st) == -1) {
		index_set_error(index, "stat() failed for maildir %s: %m",
				cur_dir);
		return FALSE;
	}

	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;
}