git » libjio » commit 7c1171a

jopen(): Do not leak file descriptors if we fail to write to the lockfile

author Alberto Bertogli
2009-07-31 03:00:29 UTC
committer Alberto Bertogli
2009-07-31 03:00:29 UTC
parent 05e7ff26a32f800aef938487f77b5bb37fa434cc

jopen(): Do not leak file descriptors if we fail to write to the lockfile

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

libjio/trans.c +2 -3

diff --git a/libjio/trans.c b/libjio/trans.c
index 429ac99..a379943 100644
--- a/libjio/trans.c
+++ b/libjio/trans.c
@@ -564,6 +564,8 @@ struct jfs *jopen(const char *name, int flags, int mode, unsigned int jflags)
 	if (jfd < 0)
 		goto error_exit;
 
+	fs->jfd = jfd;
+
 	/* initialize the lock file by writing the first tid to it, but only
 	 * if its empty, otherwise there is a race if two processes call
 	 * jopen() simultaneously and both initialize the file */
@@ -573,14 +575,11 @@ struct jfs *jopen(const char *name, int flags, int mode, unsigned int jflags)
 		t = 0;
 		rv = spwrite(jfd, &t, sizeof(t), 0);
 		if (rv != sizeof(t)) {
-			plockf(jfd, F_UNLOCK, 0, 0);
 			goto error_exit;
 		}
 	}
 	plockf(jfd, F_UNLOCK, 0, 0);
 
-	fs->jfd = jfd;
-
 	fs->jmap = (unsigned int *) mmap(NULL, sizeof(unsigned int),
 			PROT_READ | PROT_WRITE, MAP_SHARED, jfd, 0);
 	if (fs->jmap == MAP_FAILED)