dovecot: Keep index->map always usable, never NULL.

dovecot at dovecot.org dovecot at dovecot.org
Tue Jun 19 15:20:17 EEST 2007


details:   http://hg.dovecot.org/dovecot/rev/622ab4de7d66
changeset: 5773:622ab4de7d66
user:      Timo Sirainen <tss at iki.fi>
date:      Tue Jun 19 15:20:13 2007 +0300
description:
Keep index->map always usable, never NULL.

diffstat:

3 files changed, 16 insertions(+), 57 deletions(-)
src/lib-index/mail-index-map.c         |   59 +++++++-------------------------
src/lib-index/mail-index-sync-update.c |   12 +-----
src/lib-index/mail-index.c             |    2 -

diffs (146 lines):

diff -r 002aa9bbfcb2 -r 622ab4de7d66 src/lib-index/mail-index-map.c
--- a/src/lib-index/mail-index-map.c	Tue Jun 19 15:05:16 2007 +0300
+++ b/src/lib-index/mail-index-map.c	Tue Jun 19 15:20:13 2007 +0300
@@ -704,26 +704,27 @@ static int mail_index_map_latest_file(st
 	return 1;
 }
 
-static int
-mail_index_map_update(struct mail_index *index, struct mail_index_map **_map,
-		      enum mail_index_sync_handler_type type,
-		      unsigned int *lock_id_r)
-{
-	struct mail_index_map *map = *_map;
+int mail_index_map(struct mail_index *index,
+		   enum mail_index_sync_handler_type type,
+		   unsigned int *lock_id_r)
+{
 	unsigned int lock_id = 0;
 	int ret;
 
+	i_assert(index->lock_type != F_WRLCK);
 	i_assert(!index->mapping);
-	i_assert(map->refcount > 0);
 
 	*lock_id_r = 0;
 	index->mapping = TRUE;
 
+	if (index->map == NULL)
+		index->map = mail_index_map_alloc(index);
+
 	/* first try updating the existing mapping from transaction log. */
-	if (map->hdr.indexid != 0) {
+	if (index->map->hdr.indexid != 0) {
 		/* we're not creating the index, or opening transaction log.
 		   sync this as a view from transaction log. */
-		ret = mail_index_sync_map(index, &map, type, FALSE);
+		ret = mail_index_sync_map(index, &index->map, type, FALSE);
 	} else {
 		ret = 0;
 	}
@@ -733,54 +734,20 @@ mail_index_map_update(struct mail_index 
 		   any reason, we'll fallback to updating the existing mapping
 		   from transaction logs (which we'll also do even if the
 		   reopening succeeds) */
-		(void)mail_index_map_latest_file(index, &map, &lock_id);
+		(void)mail_index_map_latest_file(index, &index->map, &lock_id);
 
 		/* and update the map with the latest changes from
 		   transaction log */
-		ret = mail_index_sync_map(index, &map, type, TRUE);
+		ret = mail_index_sync_map(index, &index->map, type, TRUE);
 
 		/* we need the lock only if we didn't move the map to memory */
-		if (!MAIL_INDEX_MAP_IS_IN_MEMORY(map))
+		if (!MAIL_INDEX_MAP_IS_IN_MEMORY(index->map))
 			*lock_id_r = lock_id;
 		else
 			mail_index_unlock(index, lock_id);
 	}
 
-	if (ret <= 0) {
-		/* broken index */
-		mail_index_map_clear(index, map);
-		mail_index_unmap(index, &map);
-	}
-
-	*_map = map;
 	index->mapping = FALSE;
-	return ret;
-}
-
-int mail_index_map(struct mail_index *index,
-		   enum mail_index_sync_handler_type type,
-		   unsigned int *lock_id_r)
-{
-	struct mail_index_map *map = index->map;
-	int ret;
-
-	i_assert(index->lock_type != F_WRLCK);
-
-	if (map == NULL)
-		map = mail_index_map_alloc(index);
-
-	index->map = NULL;
-
-	ret = mail_index_map_update(index, &map, type, lock_id_r);
-	i_assert(index->map == NULL);
-
-	if (ret > 0) {
-		i_assert(map->hdr.messages_count == map->records_count);
-		index->map = map;
-	} else {
-		if (map != NULL)
-			mail_index_unmap(index, &map);
-	}
 	return ret;
 }
 
diff -r 002aa9bbfcb2 -r 622ab4de7d66 src/lib-index/mail-index-sync-update.c
--- a/src/lib-index/mail-index-sync-update.c	Tue Jun 19 15:05:16 2007 +0300
+++ b/src/lib-index/mail-index-sync-update.c	Tue Jun 19 15:20:13 2007 +0300
@@ -686,6 +686,8 @@ int mail_index_sync_map(struct mail_inde
 	int ret;
 	bool had_dirty;
 
+	i_assert(index->map == map || type == MAIL_INDEX_SYNC_HANDLER_VIEW);
+
 	if (!force) {
 		/* see if we'd prefer to reopen the index file instead of
 		   syncing the current map from the transaction log */
@@ -740,11 +742,6 @@ int mail_index_sync_map(struct mail_inde
 		map->hdr_base = map->hdr_copy_buf->data;
 	}
 
-	if (type != MAIL_INDEX_SYNC_HANDLER_VIEW) {
-		i_assert(index->map == NULL);
-		index->map = map;
-	}
-
 	mail_index_sync_map_init(&sync_map_ctx, view, type);
 	map = NULL;
 
@@ -807,10 +804,7 @@ int mail_index_sync_map(struct mail_inde
 
 	mail_index_sync_map_deinit(&sync_map_ctx);
 
-	if (type != MAIL_INDEX_SYNC_HANDLER_VIEW) {
-		i_assert(index->map == map);
-		index->map = NULL;
-	}
+	i_assert(index->map == map || type == MAIL_INDEX_SYNC_HANDLER_VIEW);
 
 	*_map = map;
 	return ret < 0 ? -1 : 1;
diff -r 002aa9bbfcb2 -r 622ab4de7d66 src/lib-index/mail-index.c
--- a/src/lib-index/mail-index.c	Tue Jun 19 15:05:16 2007 +0300
+++ b/src/lib-index/mail-index.c	Tue Jun 19 15:20:13 2007 +0300
@@ -522,8 +522,6 @@ int mail_index_open(struct mail_index *i
 
 static void mail_index_close_file(struct mail_index *index)
 {
-	if (index->map != NULL)
-		mail_index_unmap(index, &index->map);
 	if (index->file_lock != NULL)
 		file_lock_free(&index->file_lock);
 


More information about the dovecot-cvs mailing list