dovecot-2.0: lib-index: Avoid corrupting dovecot.index file when...

dovecot at dovecot.org dovecot at dovecot.org
Mon Jan 17 21:25:06 EET 2011


details:   http://hg.dovecot.org/dovecot-2.0/rev/7ecc5e10da57
changeset: 12567:7ecc5e10da57
user:      Timo Sirainen <tss at iki.fi>
date:      Mon Jan 17 21:25:01 2011 +0200
description:
lib-index: Avoid corrupting dovecot.index file when recreating it even in case of broken file locking.

diffstat:

 src/lib-index/mail-index.c |  13 ++++++++++++-
 1 files changed, 12 insertions(+), 1 deletions(-)

diffs (24 lines):

diff -r d8179b523116 -r 7ecc5e10da57 src/lib-index/mail-index.c
--- a/src/lib-index/mail-index.c	Sun Jan 16 21:47:15 2011 +0200
+++ b/src/lib-index/mail-index.c	Mon Jan 17 21:25:01 2011 +0200
@@ -423,8 +423,19 @@
 
 	path = *path_r = t_strconcat(index->filepath, ".tmp", NULL);
 	old_mask = umask(0);
-	fd = open(path, O_RDWR|O_CREAT|O_TRUNC, index->mode);
+	fd = open(path, O_RDWR|O_CREAT|O_EXCL, index->mode);
 	umask(old_mask);
+	if (fd == -1 && errno == EEXIST) {
+		/* stale temp file. unlink and recreate rather than overwriting,
+		   just to make sure locking problems won't cause corruption */
+		if (unlink(path) < 0) {
+			i_error("unlink(%s) failed: %m", path);
+			return -1;
+		}
+		old_mask = umask(0);
+		fd = open(path, O_RDWR|O_CREAT|O_EXCL, index->mode);
+		umask(old_mask);
+	}
 	if (fd == -1) {
 		mail_index_file_set_syscall_error(index, path, "creat()");
 		return -1;


More information about the dovecot-cvs mailing list