Commit 5a451dac authored by Karel Koci's avatar Karel Koci 🤘

Don't fail if there is no journal to recover

parent 77a44a21
--[[
Copyright 2016, CZ.NIC z.s.p.o. (http://www.nic.cz/)
Copyright 2016-2017, CZ.NIC z.s.p.o. (http://www.nic.cz/)
This file is part of the turris updater.
......@@ -39,6 +39,7 @@ local utils = require "utils"
local journal = require "journal"
local DBG = DBG
local WARN = WARN
local INFO = INFO
local state_dump = state_dump
local sync = sync
local log_event = log_event
......@@ -349,6 +350,10 @@ end
function recover()
local run_state = backend.run_state()
local previous = journal.recover()
if not previous then
INFO("No journal to recover");
return {}
end
local status = {}
for _, value in ipairs(previous) do
assert(not status[value.type])
......
/*
* Copyright 2016, CZ.NIC z.s.p.o. (http://www.nic.cz/)
* Copyright 2016-2017, CZ.NIC z.s.p.o. (http://www.nic.cz/)
*
* This file is part of the turris updater.
*
......@@ -119,7 +119,7 @@ static void journal_write(enum record_type type, size_t num_params, const size_t
ASSERT_MSG(!error, "Failed to write journal: %s", strerror(errno));
}
static void journal_open(lua_State *L, int flags) {
static bool journal_open(lua_State *L, int flags) {
DBG("Opening journal");
if (fd != -1)
luaL_error(L, "Journal already open");
......@@ -133,7 +133,7 @@ static void journal_open(lua_State *L, int flags) {
luaL_error(L, "Unfinished journal exists");
case ENOENT:
if (!(flags & O_CREAT))
luaL_error(L, "No journal to recover");
return false;
// Otherwise ‒ fall through to the default section
default:
luaL_error(L, "Error opening journal: %s", strerror(errno));
......@@ -142,6 +142,7 @@ static void journal_open(lua_State *L, int flags) {
ASSERT_MSG(fcntl(fd, F_SETFD, (long)FD_CLOEXEC) != -1, "Failed to set close on exec on journal FD: %s", strerror(errno));
// Keep a copy of the journal path, someone might change it and we want to remove the correct journal on finish
journal_path = strdup(path);
return true;
}
static int lua_fresh(lua_State *L) {
......@@ -255,7 +256,8 @@ FAIL:
}
static int lua_recover(lua_State *L) {
journal_open(L, 0);
if (!journal_open(L, 0))
return 0;
lua_newtable(L);
size_t i = 0;
off_t offset = 0;
......
......@@ -190,7 +190,7 @@ There are following functions:
fresh():: Open a new journal. It fails if there's a journal file
already.
recover():: Open a previous journal. It returns table with the
content of the journal. It fails in case there's no journal.
content of the journal. If there is no journal nil is returned.
finish([keep]):: Close the journal. If the keep is set to true,
it keeps the journal in place, otherwise it is deleted. The idea
is that the application would close the journal when it succeeds,
......
--[[
Copyright 2016, CZ.NIC z.s.p.o. (http://www.nic.cz/)
Copyright 2016-2017, CZ.NIC z.s.p.o. (http://www.nic.cz/)
This file is part of the turris updater.
......@@ -79,7 +79,7 @@ function test_recover_empty()
local dir = dir_init()
assert_table_equal({}, ls(dir))
-- Nothing to recover
assert_error(function () J.recover() end)
assert_nil(J.recover())
J.fresh()
-- Keep the journal file
J.finish(true)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment