Skip to content
Snippets Groups Projects
Commit 1ac27258 authored by Filip Siroky's avatar Filip Siroky Committed by Daniel Salzman
Browse files

journal: refactored journal_open

parent 78608c6c
No related branches found
No related tags found
1 merge request!581journal: refactored journal_open
File mode changed from 100644 to 100755
......@@ -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);
......
......@@ -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.
......
......@@ -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]);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment