From 1ac27258e1faea5f27239f3689108d4d252f76d8 Mon Sep 17 00:00:00 2001
From: Filip Siroky <filip.siroky@nic.cz>
Date: Tue, 23 Aug 2016 11:58:21 +0200
Subject: [PATCH] journal: refactored journal_open

---
 python/libknot/control.py |  0
 src/knot/server/journal.c | 57 ++++++++++++++++++++-------------------
 src/knot/server/journal.h |  3 ++-
 tests/journal.c           | 21 ++++++++-------
 4 files changed, 42 insertions(+), 39 deletions(-)
 mode change 100644 => 100755 python/libknot/control.py

diff --git a/python/libknot/control.py b/python/libknot/control.py
old mode 100644
new mode 100755
diff --git a/src/knot/server/journal.c b/src/knot/server/journal.c
index 22faf20540..b515feec4a 100644
--- a/src/knot/server/journal.c
+++ b/src/knot/server/journal.c
@@ -468,18 +468,18 @@ int journal_write_out(journal_t *journal, journal_node_t *n)
 	return KNOT_EOK;
 }
 
-journal_t* journal_open(const char *path, size_t fslimit)
+int journal_open(journal_t **journal, const char *path, size_t fslimit)
 {
-	if (path == NULL) {
-		return NULL;
+	if (journal == NULL || path == NULL) {
+		return KNOT_EINVAL;
 	}
 
-	journal_t *j = malloc(sizeof(journal_t));
+	journal_t *j = malloc(sizeof(*j));
 	if (j == NULL) {
-		return NULL;
+		return KNOT_ENOMEM;
 	}
 
-	memset(j, 0, sizeof(journal_t));
+	memset(j, 0, sizeof(*j));
 	j->bflags = JOURNAL_DIRTY;
 	j->fd = -1;
 
@@ -494,17 +494,19 @@ journal_t* journal_open(const char *path, size_t fslimit)
 	j->path = strdup(path);
 	if (j->path == NULL) {
 		free(j);
-		return NULL;
+		return KNOT_ENOMEM;
 	}
 
 	/* Open journal file. */
 	int ret = journal_open_file(j);
 	if (ret != KNOT_EOK) {
 		journal_close(j);
-		return NULL;
+		return ret;
 	}
 
-	return j;
+	*journal = j;
+
+	return KNOT_EOK;
 }
 
 /*!
@@ -864,15 +866,15 @@ static int journal_walk(const char *fn, uint32_t from, uint32_t to,
                         journal_apply_t cb, const zone_t *zone, list_t *chgs)
 {
 	/* Open journal for reading. */
-	journal_t *journal = journal_open(fn, FSLIMIT_INF);
-	if (journal == NULL) {
-		return KNOT_ENOMEM;
+	journal_t *journal = NULL;
+	int ret = journal_open(&journal, fn, FSLIMIT_INF);
+	if (ret != KNOT_EOK) {
+		return ret;
 	}
-
 	/* Read entries from starting serial until finished. */
 	uint32_t found_to = from;
 	journal_node_t *n = 0;
-	int ret = journal_fetch(journal, from, journal_key_from_cmp, &n);
+	ret = journal_fetch(journal, from, journal_key_from_cmp, &n);
 	if (ret != KNOT_EOK) {
 		goto finish;
 	}
@@ -972,12 +974,11 @@ int journal_store_changesets(list_t *src, const char *path, size_t size_limit)
 	}
 
 	/* Open journal for reading. */
-	int ret = KNOT_EOK;
-	journal_t *journal = journal_open(path, size_limit);
-	if (journal == NULL) {
-		return KNOT_ENOMEM;
+	journal_t *journal = NULL;
+	int ret = journal_open(&journal, path, size_limit);
+	if (ret != KNOT_EOK) {
+		return ret;
 	}
-
 	/* Begin writing to journal. */
 	changeset_t *chs = NULL;
 	WALK_LIST(chs, *src) {
@@ -998,12 +999,13 @@ int journal_store_changeset(changeset_t *change, const char *path, size_t size_l
 	}
 
 	/* Open journal for reading. */
-	journal_t *journal = journal_open(path, size_limit);
-	if (journal == NULL) {
-		return KNOT_ENOMEM;
+	journal_t *journal = NULL;
+	int ret = journal_open(&journal, path, size_limit);
+	if (ret != KNOT_EOK) {
+		return ret;
 	}
 
-	int ret = changeset_pack(change, journal);
+	ret = changeset_pack(change, journal);
 
 	journal_close(journal);
 	return ret;
@@ -1024,12 +1026,11 @@ int journal_mark_synced(const char *path)
 	if (!journal_exists(path)) {
 		return KNOT_EOK;
 	}
-
-	journal_t *journal = journal_open(path, FSLIMIT_INF);
-	if (journal == NULL) {
-		return KNOT_ENOMEM;
+	journal_t *journal = NULL;
+	int ret = journal_open(&journal, path, FSLIMIT_INF);
+	if (ret != KNOT_EOK) {
+		return ret;
 	}
-
 	size_t i = journal->qhead;
 	for(; i != journal->qtail; i = jnode_next(journal, i)) {
 		mark_synced(journal, journal->nodes + i);
diff --git a/src/knot/server/journal.h b/src/knot/server/journal.h
index ecbf05ce7e..c930bf4152 100644
--- a/src/knot/server/journal.h
+++ b/src/knot/server/journal.h
@@ -109,13 +109,14 @@ typedef struct journal
 /*!
  * \brief Open journal.
  *
+ * \param journal Returned journal.
  * \param path Journal file name.
  * \param fslimit File size limit (0 for no limit).
  *
  * \retval new journal instance if successful.
  * \retval NULL on error.
  */
-journal_t* journal_open(const char *path, size_t fslimit);
+int journal_open(journal_t **journal, const char *path, size_t fslimit);
 
 /*!
  * \brief Map journal entry for read/write.
diff --git a/tests/journal.c b/tests/journal.c
index 967f1bca7a..4970e184b8 100644
--- a/tests/journal.c
+++ b/tests/journal.c
@@ -301,13 +301,14 @@ int main(int argc, char *argv[])
 	remove(jfilename);
 
 	/* Try to open journal with too small fsize. */
-	journal_t *journal = journal_open(jfilename, 1024);
-	ok(journal == NULL, "journal: open too small");
+	journal_t *journal = NULL;
+	int ret = journal_open(&journal, jfilename, 1024);
+	ok(ret != KNOT_EOK, "journal: open too small");
 
 	/* Open/create new journal. */
-	journal = journal_open(jfilename, fsize);
-	ok(journal != NULL, "journal: open journal '%s'", jfilename);
-	if (journal == NULL) {
+	ret = journal_open(&journal, jfilename, fsize);
+	ok(ret == KNOT_EOK, "journal: open journal '%s'", jfilename);
+	if (ret != KNOT_EOK) {
 		goto skip_all;
 	}
 
@@ -316,7 +317,7 @@ int main(int argc, char *argv[])
 	uint64_t chk_key = 0;
 	char chk_buf[64] = {'\0'};
 	randstr(chk_buf, sizeof(chk_buf));
-	int ret = journal_map(journal, chk_key, &mptr, sizeof(chk_buf), false);
+	ret = journal_map(journal, chk_key, &mptr, sizeof(chk_buf), false);
 	is_int(KNOT_EOK, ret, "journal: write data (map)");
 	if (ret == KNOT_EOK) {
 		memcpy(mptr, chk_buf, sizeof(chk_buf));
@@ -335,8 +336,8 @@ int main(int argc, char *argv[])
 
 	/* Reopen log and re-read value. */
 	journal_close(journal);
-	journal = journal_open(jfilename, fsize);
-	ok(journal != NULL, "journal: open journal '%s'", jfilename);
+	ret = journal_open(&journal, jfilename, fsize);
+	ok(ret == KNOT_EOK, "journal: open journal '%s'", jfilename);
 
 	ret = journal_map(journal, chk_key, &mptr, sizeof(chk_buf), true);
 	if (ret == KNOT_EOK) {
@@ -374,8 +375,8 @@ int main(int argc, char *argv[])
 		journal_close(journal);
 		ret = journal_mark_synced(jfilename);
 		is_int(KNOT_EOK, ret, "journal: flush after fillup #%u", i);
-		journal = journal_open(jfilename, fsize);
-		ok(journal != NULL, "journal: reopen after flush #%u", i);
+		ret = journal_open(&journal, jfilename, fsize);
+		ok(ret == KNOT_EOK, "journal: reopen after flush #%u", i);
 		/* Journal fillup. */
 		test_fillup(journal, fsize, i, sizes[i % num_sizes]);
 	}
-- 
GitLab