[dovecot-cvs] dovecot/src/lib-index mail-cache-compress.c, 1.29, 1.30 mail-cache-fields.c, 1.10, 1.11 mail-cache.c, 1.56, 1.57

cras at dovecot.org cras at dovecot.org
Sat Dec 4 23:55:44 EET 2004


Update of /var/lib/cvs/dovecot/src/lib-index
In directory talvi:/tmp/cvs-serv13329

Modified Files:
	mail-cache-compress.c mail-cache-fields.c mail-cache.c 
Log Message:
Locking fixes and cleanups



Index: mail-cache-compress.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-index/mail-cache-compress.c,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -d -r1.29 -r1.30
--- mail-cache-compress.c	28 Nov 2004 23:19:53 -0000	1.29
+++ mail-cache-compress.c	4 Dec 2004 21:55:41 -0000	1.30
@@ -233,20 +233,15 @@
 	return mail_index_transaction_commit(t, &seq, &offset);
 }
 
-int mail_cache_compress(struct mail_cache *cache, struct mail_index_view *view)
+static int mail_cache_compress_locked(struct mail_cache *cache,
+				      struct mail_index_view *view)
 {
         mode_t old_mask;
-	int fd, ret, locked;
-
-	if ((ret = mail_cache_lock(cache)) < 0)
-		return -1;
-	locked = ret > 0;
+	int fd;
 
 	/* get the latest info on fields */
-	if (mail_cache_header_fields_read(cache) < 0) {
-		if (locked) mail_cache_unlock(cache);
+	if (mail_cache_header_fields_read(cache) < 0)
 		return -1;
-	}
 
 #ifdef DEBUG
 	i_warning("Compressing cache file %s", cache->filepath);
@@ -261,7 +256,6 @@
 
 	if (fd == -1) {
 		mail_cache_set_syscall_error(cache, "file_dotlock_open()");
-		if (locked) mail_cache_unlock(cache);
 		return -1;
 	}
 
@@ -273,37 +267,51 @@
 
 	// FIXME: check that cache file wasn't just recreated
 
-	ret = 0;
 	if (mail_cache_copy(cache, view, fd) < 0) {
 		(void)file_dotlock_delete(cache->filepath, NULL, fd);
-		ret = -1;
-	} else {
-		if (file_dotlock_replace(cache->filepath, NULL,
-					 -1, FALSE) < 0) {
-			mail_cache_set_syscall_error(cache,
-						     "file_dotlock_replace()");
-			(void)close(fd);
-			ret = -1;
-		} else {
-			mail_cache_file_close(cache);
-			cache->fd = fd;
-
-			if (cache->file_cache != NULL)
-				file_cache_set_fd(cache->file_cache, cache->fd);
+		return -1;
+	}
 
-			if (mail_cache_map(cache, 0, 0) < 0)
-				ret = -1;
-			else if (mail_cache_header_fields_read(cache) < 0)
-				ret = -1;
-		}
+	if (file_dotlock_replace(cache->filepath, NULL,
+				 -1, FALSE) < 0) {
+		mail_cache_set_syscall_error(cache,
+					     "file_dotlock_replace()");
+		(void)close(fd);
+		return -1;
 	}
 
-	if (locked)
-		mail_cache_unlock(cache);
+	mail_cache_file_close(cache);
+	cache->fd = fd;
 
-	if (ret == 0)
-                cache->need_compress = FALSE;
-	return ret;
+	if (cache->file_cache != NULL)
+		file_cache_set_fd(cache->file_cache, cache->fd);
+
+	if (mail_cache_map(cache, 0, 0) < 0)
+		return -1;
+	if (mail_cache_header_fields_read(cache) < 0)
+		return -1;
+
+	cache->need_compress = FALSE;
+	return 0;
+}
+
+int mail_cache_compress(struct mail_cache *cache, struct mail_index_view *view)
+{
+	int ret;
+
+	switch (mail_cache_lock(cache)) {
+	case -1:
+		return -1;
+	case 0:
+		/* couldn't lock, either it's broken or doesn't exist.
+		   just start creating it. */
+		return mail_cache_compress_locked(cache, view);
+	default:
+		/* locking succeeded. */
+		ret = mail_cache_compress_locked(cache, view);
+		mail_cache_unlock(cache);
+		return ret;
+	}
 }
 
 int mail_cache_need_compress(struct mail_cache *cache)

Index: mail-cache-fields.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-index/mail-cache-fields.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- mail-cache-fields.c	8 Nov 2004 00:55:02 -0000	1.10
+++ mail-cache-fields.c	4 Dec 2004 21:55:41 -0000	1.11
@@ -275,23 +275,15 @@
 	}
 }
 
-int mail_cache_header_fields_update(struct mail_cache *cache)
+static int mail_cache_header_fields_update_locked(struct mail_cache *cache)
 {
-	int locked = cache->locked;
 	buffer_t *buffer;
 	uint32_t i, offset;
 	int ret = 0;
 
-	if (!locked) {
-		if (mail_cache_lock(cache) <= 0)
-			return -1;
-	}
-
 	if (mail_cache_header_fields_read(cache) < 0 ||
-	    mail_cache_header_fields_get_offset(cache, &offset) < 0) {
-		mail_cache_unlock(cache);
+	    mail_cache_header_fields_get_offset(cache, &offset) < 0)
 		return -1;
-	}
 
 	t_push();
 	buffer = buffer_create_dynamic(pool_datastack_create(), 256);
@@ -321,8 +313,21 @@
 	if (ret == 0)
 		cache->field_header_write_pending = FALSE;
 
-	if (!locked)
-		mail_cache_unlock(cache);
+	return ret;
+}
+
+int mail_cache_header_fields_update(struct mail_cache *cache)
+{
+	int ret;
+
+	if (cache->locked)
+		return mail_cache_header_fields_update_locked(cache);
+
+	if (mail_cache_lock(cache) <= 0)
+		return -1;
+
+	ret = mail_cache_header_fields_update_locked(cache);
+	mail_cache_unlock(cache);
 	return ret;
 }
 

Index: mail-cache.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-index/mail-cache.c,v
retrieving revision 1.56
retrieving revision 1.57
diff -u -d -r1.56 -r1.57
--- mail-cache.c	29 Nov 2004 17:38:38 -0000	1.56
+++ mail-cache.c	4 Dec 2004 21:55:41 -0000	1.57
@@ -352,12 +352,15 @@
 			file_cache_invalidate(cache->file_cache, 0,
 					      sizeof(struct mail_cache_header));
 		}
-		if (mail_cache_map(cache, 0, 0) < 0)
+		if (mail_cache_map(cache, 0, 0) < 0) {
+			mail_cache_unlock(cache);
 			ret = -1;
+		}
 		cache->hdr_copy = *cache->hdr;
 	}
 
 	mail_index_view_close(view);
+	i_assert((ret <= 0 && !cache->locked) || (ret > 0 && cache->locked));
 	return ret;
 }
 



More information about the dovecot-cvs mailing list