changeset 9449:c7948ebd433e HEAD

file_dotlock_create(): If nfs_flush is enabled, fdatasync after writing PID. This makes sure that the write() doesn't fail later in close() and cause problems.
author Timo Sirainen <tss@iki.fi>
date Thu, 22 Oct 2009 12:58:35 -0400
parents ab32d7e2c0d6
children d3d0a2ed804a
files src/lib/file-dotlock.c
diffstat 1 files changed, 7 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/file-dotlock.c	Tue Oct 20 15:49:01 2009 -0400
+++ b/src/lib/file-dotlock.c	Thu Oct 22 12:58:35 2009 -0400
@@ -299,13 +299,14 @@
 	return 0;
 }
 
-static int file_write_pid(int fd, const char *path)
+static int file_write_pid(int fd, const char *path, bool nfs_flush)
 {
 	const char *str;
 
 	/* write our pid and host, if possible */
 	str = t_strdup_printf("%s:%s", my_pid, my_hostname);
-	if (write_full(fd, str, strlen(str)) < 0) {
+	if (write_full(fd, str, strlen(str)) < 0 ||
+	    (nfs_flush && fdatasync(fd) < 0)) {
 		/* failed, leave it empty then */
 		if (ftruncate(fd, 0) < 0) {
 			i_error("ftruncate(%s) failed: %m", path);
@@ -357,7 +358,8 @@
 
 		if (write_pid) {
 			if (file_write_pid(lock_info->fd,
-					   str_c(tmp_path)) < 0) {
+					   str_c(tmp_path),
+					   lock_info->set->nfs_flush) < 0) {
 				(void)close(lock_info->fd);
 				lock_info->fd = -1;
 				return -1;
@@ -402,7 +404,8 @@
 	}
 
 	if (write_pid) {
-		if (file_write_pid(fd, lock_info->lock_path) < 0) {
+		if (file_write_pid(fd, lock_info->lock_path,
+				   lock_info->set->nfs_flush) < 0) {
 			(void)close(fd);
 			return -1;
 		}