changeset 3174:091821cac480 HEAD

If we failed to lock the dotlock, don't try to close the file twice.
author Timo Sirainen <tss@iki.fi>
date Sat, 05 Mar 2005 21:03:03 +0200
parents d137899ea853
children 5d6656a798e5
files src/lib/file-dotlock.c
diffstat 1 files changed, 13 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/file-dotlock.c	Sat Mar 05 14:41:22 2005 +0200
+++ b/src/lib/file-dotlock.c	Sat Mar 05 21:03:03 2005 +0200
@@ -270,6 +270,8 @@
 
 	if (lock_info->temp_path == NULL) {
 		/* we'll need our temp file first. */
+		i_assert(lock_info->fd == -1);
+
 		if (temp_prefix == NULL) {
 			temp_prefix = t_strconcat(".temp.", my_hostname, ".",
 						  my_pid, ".", NULL);
@@ -381,15 +383,9 @@
 		now = time(NULL);
 	} while (now < max_wait_time);
 
-	if (ret <= 0 && lock_info.fd != -1) {
-		int old_errno = errno;
-
-		if (close(lock_info.fd) < 0)
-			i_error("close(%s) failed: %m", path);
-		errno = old_errno;
-	} else {
+	if (ret > 0) {
 		if (fstat(lock_info.fd, &st) < 0) {
-			i_error("fstat(%s) failed: %m", path);
+			i_error("fstat(%s) failed: %m", lock_path);
 			ret = -1;
 		} else {
 			dotlock->dev = st.st_dev;
@@ -397,9 +393,18 @@
 
 			dotlock->path = i_strdup(path);
 			dotlock->fd = lock_info.fd;
+			lock_info.fd = -1;
 		}
 	}
 
+	if (lock_info.fd != -1) {
+		int old_errno = errno;
+
+		if (close(lock_info.fd) < 0)
+			i_error("close(%s) failed: %m", lock_path);
+		errno = old_errno;
+	}
+
 	if (ret == 0)
 		errno = EAGAIN;
 	return ret;