[dovecot-cvs] dovecot/src/lib-index Makefile.am,1.14,1.15
mail-index-file.c,1.1,1.2 mail-index-open.c,1.43,1.44
mail-index.c,1.96,1.97 mail-index-compress.c,1.27,NONE
cras at procontrol.fi
cras at procontrol.fi
Mon Aug 11 06:40:43 EEST 2003
Update of /home/cvs/dovecot/src/lib-index
In directory danu:/tmp/cvs-serv13350
Modified Files:
Makefile.am mail-index-file.c mail-index-open.c mail-index.c
Removed Files:
mail-index-compress.c
Log Message:
Support for upgrading from old index file with smaller header.
Index: Makefile.am
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/Makefile.am,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- Makefile.am 6 Aug 2003 20:15:31 -0000 1.14
+++ Makefile.am 11 Aug 2003 02:40:40 -0000 1.15
@@ -11,7 +11,6 @@
mail-cache.c \
mail-custom-flags.c \
mail-index.c \
- mail-index-compress.c \
mail-index-file.c \
mail-index-fsck.c \
mail-index-open.c \
Index: mail-index-file.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mail-index-file.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- mail-index-file.c 21 Jul 2003 14:35:39 -0000 1.1
+++ mail-index-file.c 11 Aug 2003 02:40:40 -0000 1.2
@@ -1,9 +1,12 @@
/* Copyright (C) 2003 Timo Sirainen */
#include "lib.h"
+#include "file-set-size.h"
#include "mail-index.h"
#include "mail-index-util.h"
+#include <unistd.h>
+
struct mail_index_record *mail_index_next(struct mail_index *index,
struct mail_index_record *rec)
{
@@ -110,7 +113,91 @@
return rec_p + idx;
}
-int mail_index_compress(struct mail_index *index __attr_unused__)
+int mail_index_compress(struct mail_index *index)
{
+ size_t diff;
+ off_t new_file_size;
+
+ if (index->header_size >= sizeof(struct mail_index_header))
+ return TRUE;
+
+ if (!index->set_lock(index, MAIL_LOCK_EXCLUSIVE))
+ return FALSE;
+
+ /* make sure the file is large enough */
+ diff = sizeof(struct mail_index_header) - index->header_size;
+ if (index->mmap_used_length + diff > index->mmap_full_length) {
+ /* mmap_update ftruncates the file to multiples of
+ mail_index_record, make sure we grow it enough here. */
+ new_file_size = index->mmap_used_length + diff +
+ (sizeof(struct mail_index_record) -
+ (diff % sizeof(struct mail_index_record)));
+ if (file_set_size(index->fd, new_file_size) < 0) {
+ index_set_syscall_error(index, "file_set_size()");
+ return FALSE;
+ }
+
+ index->header->sync_id++;
+ if (!mail_index_mmap_update(index))
+ return FALSE;
+ }
+
+ /* if we break, we'll have to rebuild it completely */
+ index->header->flags |= MAIL_INDEX_HDR_FLAG_REBUILD;
+ if (!mail_index_fmdatasync(index, index->header_size))
+ return FALSE;
+
+ memmove((char *) index->mmap_base + sizeof(struct mail_index_header),
+ (char *) index->mmap_base + index->header_size,
+ index->mmap_used_length - index->header_size);
+ memset((char *) index->mmap_base + index->header_size, 0, diff);
+
+ index->mmap_used_length += diff;
+ index->header_size = sizeof(struct mail_index_header);
+
+ index->header->header_size = sizeof(struct mail_index_header);
+ index->header->used_file_size += diff;
+ index->header->sync_id++;
+
+ if (!mail_index_fmdatasync(index, index->mmap_used_length))
+ return FALSE;
+
+ index->header->flags &= ~MAIL_INDEX_HDR_FLAG_REBUILD;
+ return mail_index_mmap_update(index);
+}
+
+int mail_index_truncate(struct mail_index *index)
+{
+ uoff_t empty_space, truncate_threshold;
+
+ i_assert(index->lock_type == MAIL_LOCK_EXCLUSIVE);
+
+ if (index->mmap_full_length <= INDEX_FILE_MIN_SIZE(index) ||
+ index->anon_mmap)
+ return TRUE;
+
+ /* really truncate the file only when it's almost empty */
+ empty_space = index->mmap_full_length - index->mmap_used_length;
+ truncate_threshold =
+ index->mmap_full_length / 100 * INDEX_TRUNCATE_PERCENTAGE;
+
+ if (empty_space > truncate_threshold) {
+ index->mmap_full_length = index->mmap_used_length +
+ (empty_space * INDEX_TRUNCATE_KEEP_PERCENTAGE / 100);
+
+ /* keep the size record-aligned */
+ index->mmap_full_length -= (index->mmap_full_length -
+ index->header_size) %
+ sizeof(struct mail_index_record);
+
+ if (index->mmap_full_length < INDEX_FILE_MIN_SIZE(index))
+ index->mmap_full_length = INDEX_FILE_MIN_SIZE(index);
+
+ if (ftruncate(index->fd, (off_t)index->mmap_full_length) < 0)
+ return index_set_syscall_error(index, "ftruncate()");
+
+ index->header->sync_id++;
+ }
+
return TRUE;
}
Index: mail-index-open.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mail-index-open.c,v
retrieving revision 1.43
retrieving revision 1.44
diff -u -d -r1.43 -r1.44
--- mail-index-open.c 11 Aug 2003 01:56:37 -0000 1.43
+++ mail-index-open.c 11 Aug 2003 02:40:40 -0000 1.44
@@ -71,6 +71,12 @@
{
int rebuilt;
+ if (index->header_size < sizeof(struct mail_index_header)) {
+ /* upgrading from older index file. */
+ if (!mail_index_compress(index))
+ return FALSE;
+ }
+
if (!mail_cache_open_or_create(index))
return FALSE;
Index: mail-index.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mail-index.c,v
retrieving revision 1.96
retrieving revision 1.97
diff -u -d -r1.96 -r1.97
--- mail-index.c 11 Aug 2003 01:56:37 -0000 1.96
+++ mail-index.c 11 Aug 2003 02:40:40 -0000 1.97
@@ -323,7 +323,7 @@
/* locking index when cache is locked can deadlock */
i_assert(try_lock || index->lock_type == MAIL_LOCK_EXCLUSIVE ||
- !mail_cache_is_locked(index->cache));
+ index->cache == NULL || !mail_cache_is_locked(index->cache));
if (index->inconsistent) {
/* index is in inconsistent state and nothing else than
--- mail-index-compress.c DELETED ---
More information about the dovecot-cvs
mailing list