From 1c09ee5e42550d6339bffa58d4cba3461948e19c Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Wed, 5 Jun 2019 16:01:16 +0200 Subject: [PATCH] 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 --- .../luasrc/model/cbi/admin_system/backupfiles.lua | 15 +++++++++++---- .../luasrc/model/cbi/admin_system/crontab.lua | 1 + .../luasrc/model/cbi/admin_system/startup.lua | 3 +++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/modules/luci-mod-system/luasrc/model/cbi/admin_system/backupfiles.lua b/modules/luci-mod-system/luasrc/model/cbi/admin_system/backupfiles.lua index ee2401e93..c81466c87 100644 --- a/modules/luci-mod-system/luasrc/model/cbi/admin_system/backupfiles.lua +++ b/modules/luci-mod-system/luasrc/model/cbi/admin_system/backupfiles.lua @@ -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 diff --git a/modules/luci-mod-system/luasrc/model/cbi/admin_system/crontab.lua b/modules/luci-mod-system/luasrc/model/cbi/admin_system/crontab.lua index 016a6199a..beb24e7ca 100644 --- a/modules/luci-mod-system/luasrc/model/cbi/admin_system/crontab.lua +++ b/modules/luci-mod-system/luasrc/model/cbi/admin_system/crontab.lua @@ -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() diff --git a/modules/luci-mod-system/luasrc/model/cbi/admin_system/startup.lua b/modules/luci-mod-system/luasrc/model/cbi/admin_system/startup.lua index 9e19ac50a..c3f14540e 100644 --- a/modules/luci-mod-system/luasrc/model/cbi/admin_system/startup.lua +++ b/modules/luci-mod-system/luasrc/model/cbi/admin_system/startup.lua @@ -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 -- 2.25.1