maildir-rebuild.c revision 88e275325fdb8b81581298a706c102edd140f428
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (C) 2002 Timo Sirainen */
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen#include "lib.h"
76d4ff1c1b31a1a09f6cbfe613a8d0efe62cbfd2Timo Sirainen#include "maildir-index.h"
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen#include "mail-index-data.h"
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen#include "mail-index-util.h"
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen#include "mail-hash.h"
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen#include <unistd.h>
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen#include <sys/stat.h>
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen#include <sys/mman.h>
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainenint maildir_index_rebuild(MailIndex *index)
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen{
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen struct stat st;
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen const char *cur_dir, *new_dir;
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen i_assert(index->lock_type != MAIL_LOCK_SHARED);
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen if (!mail_index_set_lock(index, MAIL_LOCK_EXCLUSIVE))
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen return FALSE;
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen /* reset the header */
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen mail_index_init_header(index->header);
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen index->mmap_used_length = index->header->used_file_size;
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen /* update indexid, which also means that our state has completely
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen changed */
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen index->indexid = index->header->indexid;
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen index->inconsistent = TRUE;
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen if (msync(index->mmap_base, sizeof(MailIndexHeader), MS_SYNC) < 0)
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen return FALSE;
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen /* reset data file */
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen if (!mail_index_data_reset(index->data))
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen return FALSE;
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen /* rebuild cur/ directory */
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen cur_dir = t_strconcat(index->dir, "/cur", NULL);
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen if (!maildir_index_build_dir(index, cur_dir, NULL))
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen return FALSE;
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen /* also see if there's new mail */
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen new_dir = t_strconcat(index->dir, "/new", NULL);
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen if (!maildir_index_build_dir(index, new_dir, cur_dir))
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen return FALSE;
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen /* update sync stamp */
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen if (stat(cur_dir, &st) < 0)
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen return index_file_set_syscall_error(index, cur_dir, "stat()");
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen index->file_sync_stamp = st.st_mtime;
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen /* rebuild is complete - remove the flag */
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen index->header->flags &= ~(MAIL_INDEX_FLAG_REBUILD|MAIL_INDEX_FLAG_FSCK);
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen return TRUE;
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen}
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen