From afa198726851f224857871e8772d9e84f0371708 Mon Sep 17 00:00:00 2001
From: Alberto Bertogli <albertito@blitiri.com.ar>
Date: Fri, 11 Sep 2009 01:47:35 -0300
Subject: [PATCH 07/14] journal.c: Avoid leaving files around if plockf() fails

If we fail to lock the transaction file, we should remove it before
returning failure.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
---
 libjio/journal.c |   23 ++++-------------------
 1 files changed, 4 insertions(+), 19 deletions(-)

diff --git a/libjio/journal.c b/libjio/journal.c
index 7c8d9bf..5a6b245 100644
--- a/libjio/journal.c
+++ b/libjio/journal.c
@@ -250,24 +250,6 @@ static int is_broken(struct jfs *fs)
 	return access(broken_path, F_OK) == 0;
 }
 
-/* Open and lock (exclusive) the given file name. Returns the file descriptor,
- * or -1 on error. */
-static int open_and_lockw(const char *name, int flags, int mode)
-{
-	int fd;
-
-	fd = open(name, flags, mode);
-	if (fd < 0)
-		return -1;
-
-	if (plockf(fd, F_LOCKW, 0, 0) != 0) {
-		close(fd);
-		return -1;
-	}
-
-	return fd;
-}
-
 
 /*
  * Journal functions
@@ -301,10 +283,13 @@ struct journal_op *journal_new(struct jfs *fs, unsigned int flags)
 
 	/* open the transaction file */
 	get_jtfile(fs, id, name);
-	fd = open_and_lockw(name, O_RDWR | O_CREAT | O_TRUNC, 0600);
+	fd = open(name, O_RDWR | O_CREAT | O_TRUNC, 0600);
 	if (fd < 0)
 		goto error;
 
+	if (plockf(fd, F_LOCKW, 0, 0) != 0)
+		goto unlink_error;
+
 	jop->id = id;
 	jop->fd = fd;
 	jop->numops = 0;
-- 
1.6.2.2.646.gb214

