From fe14cd5a66a26423adcfb2366c1b9e643024fd46 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Tue, 26 May 2015 17:11:41 +0200 Subject: luci-base: fallback to a simple text editor if uci config cannot be loaded Signed-off-by: Jo-Philipp Wich --- modules/luci-base/luasrc/cbi.lua | 26 ++++++++++++++++++++++++-- modules/luci-base/luasrc/view/cbi/error.htm | 19 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 modules/luci-base/luasrc/view/cbi/error.htm (limited to 'modules/luci-base/luasrc') diff --git a/modules/luci-base/luasrc/cbi.lua b/modules/luci-base/luasrc/cbi.lua index 34de44a5f0..7c5944bf27 100644 --- a/modules/luci-base/luasrc/cbi.lua +++ b/modules/luci-base/luasrc/cbi.lua @@ -12,6 +12,7 @@ require("luci.http") local fs = require("nixio.fs") local uci = require("luci.model.uci") local datatypes = require("luci.cbi.datatypes") +local dispatcher = require("luci.dispatcher") local class = util.class local instanceof = util.instanceof @@ -307,8 +308,29 @@ function Map.__init__(self, config, ...) self.changed = false - if not self.uci:load(self.config) then - error("Unable to read UCI data: " .. self.config) + local path = "%s/%s" %{ self.uci:get_confdir(), self.config } + if fs.stat(path, "type") ~= "reg" then + fs.writefile(path, "") + end + + local ok, err = self.uci:load(self.config) + if not ok then + local url = dispatcher.build_url(unpack(dispatcher.context.request)) + local source = self:formvalue("cbi.source") + if type(source) == "string" then + fs.writefile(path, source:gsub("\r\n", "\n")) + ok, err = self.uci:load(self.config) + if ok then + luci.http.redirect(url) + end + end + end + + if not ok then + self.template = "cbi/error" + self.error = err + self.source = fs.readfile(path) or "" + self.pageaction = false end end diff --git a/modules/luci-base/luasrc/view/cbi/error.htm b/modules/luci-base/luasrc/view/cbi/error.htm new file mode 100644 index 0000000000..2acb96924e --- /dev/null +++ b/modules/luci-base/luasrc/view/cbi/error.htm @@ -0,0 +1,19 @@ +
+ <% if self.title and #self.title > 0 then %>

<%=self.title%>

<% end %> + <% if self.description and #self.description > 0 then %>
<%=self.description%>
<% end %> + +

+ <%: The configuration file could not be loaded due to the following error: %>
+ <%=pcdata(self.error)%> +

+ + + +

+ <%: Edit the raw configuration data above to fix any error and hit "Save" to reload the page. %> +

+ +
+ +
+
-- cgit v1.2.3