summaryrefslogtreecommitdiffhomepage
path: root/modules/luci-base/luasrc/cbi.lua
diff options
context:
space:
mode:
authorJo-Philipp Wich <jow@openwrt.org>2015-05-26 17:11:41 +0200
committerJo-Philipp Wich <jow@openwrt.org>2015-05-26 17:12:42 +0200
commitfe14cd5a66a26423adcfb2366c1b9e643024fd46 (patch)
treef5864364059b30d0059e6627a81221d8f30edaa9 /modules/luci-base/luasrc/cbi.lua
parent03610cee3acb5fab9defc2baf2f0e4ff8a9cfd71 (diff)
luci-base: fallback to a simple text editor if uci config cannot be loaded
Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
Diffstat (limited to 'modules/luci-base/luasrc/cbi.lua')
-rw-r--r--modules/luci-base/luasrc/cbi.lua26
1 files changed, 24 insertions, 2 deletions
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