luci-mod-system: fix SimpleForm usage on file editing pages

When a value identical to the stored one is submitted, the CBI framework
will not emit an option write event and therfore not store the value in
the form data dictionary passed to SimpleForm.handle().

This usage pattern usally works be accident for file editor views such
as admin_system/crontab because \r\n windows style line endings are
substituted with unix \n ones before writing the data, defeating the
equality check in CBI.

When a single line without trailing newline is submitted however, the
CBI will not see a difference to the data stored in the file and clear
out the value on subsequent saves.

This commit alignes the logic used by various SimpleForm views to
behave identically and predictable:

 - File data is handled in the SimpleForm.handle() callback
 - The forcewrite property is used to disable equality checks
 - Submission of an empty string empties the backing file

Fixes: #2737
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Jo-Philipp Wich 2019-06-05 16:01:16 +02:00
parent 15608fd2e5
commit 1c09ee5e42
3 changed files with 15 additions and 4 deletions

View file

@ -20,7 +20,8 @@ if luci.http.formvalue("display") ~= "list" then
l.inputstyle = "apply"
c = f:option(TextValue, "_custom")
c.rmempty = false
c.forcewrite = true
c.rmempty = true
c.cols = 70
c.rows = 30
@ -28,9 +29,15 @@ if luci.http.formvalue("display") ~= "list" then
return nixio.fs.readfile("/etc/sysupgrade.conf")
end
c.write = function(self, section, value)
value = value:gsub("\r\n?", "\n")
return nixio.fs.writefile("/etc/sysupgrade.conf", value)
m.handle = function(self, state, data)
if state == FORM_VALID then
if data._custom then
nixio.fs.writefile("/etc/sysupgrade.conf", data._custom:gsub("\r\n", "\n"))
else
nixio.fs.writefile("/etc/sysupgrade.conf", "")
end
end
return true
end
else
m.submit = false

View file

@ -11,6 +11,7 @@ f = SimpleForm("crontab", translate("Scheduled Tasks"),
"crontab file was empty before editing."))
t = f:field(TextValue, "crons")
f.forcewrite = true
t.rmempty = true
t.rows = 10
function t.cfgvalue()

View file

@ -78,6 +78,7 @@ f = SimpleForm("rc", translate("Local Startup"),
translate("This is the content of /etc/rc.local. Insert your own commands here (in front of 'exit 0') to execute them at the end of the boot process."))
t = f:field(TextValue, "rcs")
t.forcewrite = true
t.rmempty = true
t.rows = 20
@ -89,6 +90,8 @@ function f.handle(self, state, data)
if state == FORM_VALID then
if data.rcs then
fs.writefile("/etc/rc.local", data.rcs:gsub("\r\n", "\n"))
else
fs.writefile("/etc/rc.local", "")
end
end
return true