git » libjio » commit afa1987

journal.c: Avoid leaving files around if plockf() fails

author Alberto Bertogli
2009-09-11 04:47:35 UTC
committer Alberto Bertogli
2009-09-11 04:47:35 UTC
parent 0fac86100dbfddf4a8fed74d2b35687406410c16

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 +4 -19

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;