[dovecot-cvs] dovecot/src/lib-index mail-index-open.c,1.27,1.28 mail-tree.c,1.16,1.17 mail-tree.h,1.8,1.9

cras at procontrol.fi cras at procontrol.fi
Wed Apr 16 19:13:26 EEST 2003


Update of /home/cvs/dovecot/src/lib-index
In directory danu:/tmp/cvs-serv24654/lib-index

Modified Files:
	mail-index-open.c mail-tree.c mail-tree.h 
Log Message:
Some fixes to opening index.



Index: mail-index-open.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mail-index-open.c,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -d -r1.27 -r1.28
--- mail-index-open.c	9 Apr 2003 20:10:01 -0000	1.27
+++ mail-index-open.c	16 Apr 2003 15:13:24 -0000	1.28
@@ -301,11 +301,86 @@
 	return TRUE;
 }
 
+static int mail_index_open_index(struct mail_index *index,
+				 enum mail_index_open_flags flags)
+{
+	struct mail_index_header hdr;
+	int ret;
+
+	if ((flags & _MAIL_INDEX_OPEN_FLAG_CREATING) == 0)
+		index->lock_type = MAIL_LOCK_SHARED;
+	else
+		index->lock_type = MAIL_LOCK_EXCLUSIVE;
+
+	/* if index is being created, we'll wait here until it's finished */
+	if (!mail_index_wait_lock(index, MAIL_LOCK_TO_FLOCK(index->lock_type)))
+		return FALSE;
+#ifdef DEBUG
+	if (index->mmap_base != NULL) {
+		mprotect(index->mmap_base, index->mmap_used_length,
+			 PROT_READ|PROT_WRITE);
+	}
+#endif
+
+	if ((ret = mail_index_read_header(index, &hdr)) < 0)
+		return FALSE;
+
+	if (ret == 0 || !mail_index_is_compatible(&hdr)) {
+		if ((flags & MAIL_INDEX_OPEN_FLAG_CREATE) == 0)
+			return FALSE;
+
+		flags |= _MAIL_INDEX_OPEN_FLAG_CREATING;
+
+		/* so, we're creating the index */
+		if (index->lock_type != MAIL_LOCK_EXCLUSIVE) {
+			/* have to get exclusive lock first */
+			if (!mail_index_wait_lock(index, F_UNLCK))
+				return FALSE;
+			return mail_index_open_index(index, flags);
+		}
+
+		mail_index_init_header(index, &hdr);
+		if (!mail_index_init_file(index, &hdr))
+			return FALSE;
+	}
+
+	index->indexid = hdr.indexid;
+
+	if (!mail_index_mmap_update(index))
+		return FALSE;
+
+	if (index->lock_type == MAIL_LOCK_SHARED) {
+		/* we don't want to keep the shared lock while opening
+		   indexes. opening should work unlocked and some
+		   things want exclusive lock */
+		if (!mail_index_wait_lock(index, F_UNLCK))
+			return FALSE;
+		index->lock_type = MAIL_LOCK_UNLOCK;
+	}
+
+	if (!index_open_and_fix(index, flags)) {
+		if ((index->set_flags & MAIL_INDEX_FLAG_REBUILD) == 0 ||
+		    (flags & _MAIL_INDEX_OPEN_FLAG_CREATING) != 0)
+			return FALSE;
+
+		/* needs a rebuild */
+		if (!index->set_lock(index, MAIL_LOCK_UNLOCK))
+			return FALSE;
+
+		flags |= _MAIL_INDEX_OPEN_FLAG_CREATING;
+		return mail_index_open_index(index, flags);
+	}
+
+	if (!index->set_lock(index, MAIL_LOCK_UNLOCK))
+		return FALSE;
+
+	index->opened = TRUE;
+	return TRUE;
+}
+
 int mail_index_open(struct mail_index *index, enum mail_index_open_flags flags)
 {
-        struct mail_index_header hdr;
 	const char *path;
-	int ret;
 
 	i_assert(!index->opened);
 
@@ -328,68 +403,10 @@
 
 	index->filepath = i_strdup(path);
 
-	for (;;) {
-		/* if index is being created, we'll wait here until it's
-		   finished */
-		if ((flags & _MAIL_INDEX_OPEN_FLAG_CREATING) == 0)
-			index->lock_type = MAIL_LOCK_SHARED;
-		else
-			index->lock_type = MAIL_LOCK_EXCLUSIVE;
-		if (!mail_index_wait_lock(index,
-					  MAIL_LOCK_TO_FLOCK(index->lock_type)))
-			break;
-
-		if ((ret = mail_index_read_header(index, &hdr)) < 0)
-			break;
-
-		if (ret == 0 || !mail_index_is_compatible(&hdr)) {
-			if ((flags & MAIL_INDEX_OPEN_FLAG_CREATE) == 0)
-				break;
-
-			flags |= _MAIL_INDEX_OPEN_FLAG_CREATING;
-
-			/* so, we're creating the index */
-			if (index->lock_type != MAIL_LOCK_EXCLUSIVE) {
-				/* have to get exclusive lock first */
-				if (!mail_index_wait_lock(index, F_UNLCK))
-					break;
-				continue;
-			}
-
-			mail_index_init_header(index, &hdr);
-			if (!mail_index_init_file(index, &hdr))
-				break;
-		}
-
-		index->indexid = hdr.indexid;
-
-		if (!mail_index_mmap_update(index))
-			break;
-
-		if (index->lock_type == MAIL_LOCK_SHARED) {
-			/* we don't want to keep the shared lock while opening
-			   indexes. opening should work unlocked and some
-			   things want exclusive lock */
-			if (!mail_index_wait_lock(index, F_UNLCK))
-				break;
-			index->lock_type = MAIL_LOCK_UNLOCK;
-		}
-
-		if (!index_open_and_fix(index, flags) ||
-		    !index->set_lock(index, MAIL_LOCK_UNLOCK)) {
-			mail_index_close(index);
-			return mail_index_create_memory(index, flags);
-		}
-
-		index->opened = TRUE;
-		return TRUE;
+	if (!mail_index_open_index(index, flags)) {
+		mail_index_close(index);
+		return mail_index_create_memory(index, flags);
 	}
 
-	(void)close(index->fd);
-	index->fd = -1;
-
-	i_free(index->filepath);
-	index->filepath = NULL;
-
-	return mail_index_create_memory(index, flags);
+	return TRUE;
 }

Index: mail-tree.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mail-tree.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -d -r1.16 -r1.17
--- mail-tree.c	30 Mar 2003 12:48:37 -0000	1.16
+++ mail-tree.c	16 Apr 2003 15:13:24 -0000	1.17
@@ -318,6 +318,19 @@
 	return TRUE;
 }
 
+int mail_tree_reset(struct mail_tree *tree)
+{
+	i_assert(tree->index->lock_type == MAIL_LOCK_EXCLUSIVE);
+
+	if (!mail_tree_init(tree) ||
+	    (!tree->anon_mmap && !_mail_tree_mmap_update(tree, TRUE))) {
+		tree->index->header->flags |= MAIL_INDEX_FLAG_REBUILD_TREE;
+		return FALSE;
+	}
+
+	return TRUE;
+}
+
 int mail_tree_rebuild(struct mail_tree *tree)
 {
 	struct mail_index_record *rec;
@@ -325,11 +338,8 @@
 	if (!tree->index->set_lock(tree->index, MAIL_LOCK_EXCLUSIVE))
 		return FALSE;
 
-	if (!mail_tree_init(tree) ||
-	    (!tree->anon_mmap && !_mail_tree_mmap_update(tree, TRUE))) {
-		tree->index->header->flags |= MAIL_INDEX_FLAG_REBUILD_TREE;
+	if (!mail_tree_reset(tree))
 		return FALSE;
-	}
 
 	rec = tree->index->lookup(tree->index, 1);
 	while (rec != NULL) {

Index: mail-tree.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mail-tree.h,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- mail-tree.h	5 Jan 2003 13:09:52 -0000	1.8
+++ mail-tree.h	16 Apr 2003 15:13:24 -0000	1.9
@@ -47,6 +47,7 @@
 int mail_tree_open_or_create(struct mail_index *index);
 void mail_tree_free(struct mail_tree *tree);
 
+int mail_tree_reset(struct mail_tree *tree);
 int mail_tree_rebuild(struct mail_tree *tree);
 int mail_tree_sync_file(struct mail_tree *tree, int *fsync_fd);
 




More information about the dovecot-cvs mailing list