changeset 13317:f08bf97278ac

lib-index: When creating dovecot.index.backup files, fdatasync() them first. Otherwise if the dovecot.index file had been written to recently, it could lose both the new and the old backup data.
author Timo Sirainen <tss@iki.fi>
date Tue, 23 Aug 2011 23:25:14 +0300
parents de0ea56932e5
children 32eb50cc35d4
files src/lib-index/mail-index-write.c
diffstat 1 files changed, 10 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-index/mail-index-write.c	Tue Aug 23 23:03:41 2011 +0300
+++ b/src/lib-index/mail-index-write.c	Tue Aug 23 23:25:14 2011 +0300
@@ -18,6 +18,16 @@
 	const char *backup_path, *tmp_backup_path;
 	int ret;
 
+	if (index->fd != -1) {
+		/* we very much want to avoid creating a backup file that
+		   hasn't been written to disk yet */
+		if (fdatasync(index->fd) < 0) {
+			mail_index_set_error(index, "fdatasync(%s) failed: %m",
+					     tmp_backup_path);
+			return -1;
+		}
+	}
+
 	backup_path = t_strconcat(index->filepath, ".backup", NULL);
 	tmp_backup_path = t_strconcat(backup_path, ".tmp", NULL);
 	ret = link(index->filepath, tmp_backup_path);