Mercurial > dovecot > core-2.2
diff src/lib-index/mail-index.c @ 12702:7ecc5e10da57
lib-index: Avoid corrupting dovecot.index file when recreating it even in case of broken file locking.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Mon, 17 Jan 2011 21:25:01 +0200 |
parents | 41e99ee5c1dd |
children | 74300385cce0 44d0474a451e |
line wrap: on
line diff
--- 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;