From 9333107f3bfc06258b6c38db3722783548076f59 Mon Sep 17 00:00:00 2001
From: Alberto Bertogli <albertito@blitiri.com.ar>
Date: Sat, 1 Aug 2009 11:35:28 -0300
Subject: [PATCH 59/74] libjio: Make jfsck() remove the transactions and simplify jfsck_cleanup()

jfsck() is the one which should remove the transactions as it cleans them
up, and this patch makes it so. This went unnoticed for a long time
because it almost always performs a jfsck_cleanup() afterwards.

That also allow us to simplify jfsck_cleanup() by making it remove only
the files we know about, which currently is only the "lock" file, but
it could be more in the future. The "broken" file is explicitly ignored
because it should only be removed by jfsck().

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

diff --git a/libjio/check.c b/libjio/check.c
index 965c177..73202be 100644
--- a/libjio/check.c
+++ b/libjio/check.c
@@ -21,26 +21,19 @@
 #include "trans.h"
 
 
-/** Remove all the files in the journal directory (if any).
+/** Remove the journal directory (if it's clean).
  *
  * @param name path to the file
- * @param jdir path to the journal directory, use NULL for the default
+ * @param jdir path to the journal directory
  * @returns 0 on success, < 0 on error
  */
 static int jfsck_cleanup(const char *name, const char *jdir)
 {
-	char path[PATH_MAX], tfile[PATH_MAX*3];
+	char tfile[PATH_MAX*3];
 	DIR *dir;
 	struct dirent *dent;
 
-	if (jdir == NULL) {
-		if (!get_jdir(name, path))
-			return -1;
-	} else {
-		strcpy(path, jdir);
-	}
-
-	dir = opendir(path);
+	dir = opendir(jdir);
 	if (dir == NULL && errno == ENOENT)
 		/* it doesn't exist, so it's clean */
 		return 0;
@@ -49,14 +42,15 @@ static int jfsck_cleanup(const char *name, const char *jdir)
 
 	for (errno = 0, dent = readdir(dir); dent != NULL;
 			errno = 0, dent = readdir(dir)) {
-		/* we only care about transactions (named as numbers > 0) and
-		 * the lockfile (named "lock"); ignore everything else */
-		if (strcmp(dent->d_name, "lock") && atoi(dent->d_name) <= 0)
+		/* We only care about files we know, and ignore everything
+		 * else. Note that transactions should have been removed by
+		 * jfsck(), we will not do it to prevent accidental misuse */
+		if (strcmp(dent->d_name, "lock"))
 			continue;
 
 		/* build the full path to the transaction file */
 		memset(tfile, 0, PATH_MAX * 3);
-		strcat(tfile, path);
+		strcat(tfile, jdir);
 		strcat(tfile, "/");
 		strcat(tfile, dent->d_name);
 
@@ -79,7 +73,7 @@ static int jfsck_cleanup(const char *name, const char *jdir)
 	if (closedir(dir) != 0)
 		return -1;
 
-	if (rmdir(path) != 0)
+	if (rmdir(jdir) != 0)
 		return -1;
 
 	return 0;
@@ -254,7 +248,7 @@ enum jfsck_return jfsck(const char *name, const char *jdir,
 		if (tfd < 0) {
 			if (errno == ENOENT) {
 				res->invalid++;
-				goto loop;
+				goto nounlink_loop;
 			} else {
 				ret = J_EIO;
 				goto exit;
@@ -309,6 +303,12 @@ enum jfsck_return jfsck(const char *name, const char *jdir,
 		res->reapplied++;
 
 loop:
+		if (unlink(tname) != 0) {
+			ret = J_EIO;
+			goto exit;
+		}
+
+nounlink_loop:
 		if (tfd >= 0) {
 			close(tfd);
 			tfd = -1;
-- 
1.6.2.2.646.gb214


