From 08bc198e368b8a3cec391c005b440b49c5eaf11e Mon Sep 17 00:00:00 2001
From: Nils Schneider
Date: Tue, 18 Feb 2014 22:21:31 +0100
Subject: [PATCH] gluon-luci-admin: fix firmware upgrade feature
---
.../lib/lua/luci/controller/admin/system.lua | 63 ++++++++++---------
.../usr/lib/lua/luci/view/admin/upgrade.htm | 16 +++--
2 files changed, 47 insertions(+), 32 deletions(-)
diff --git a/package/gluon-luci-admin/files/usr/lib/lua/luci/controller/admin/system.lua b/package/gluon-luci-admin/files/usr/lib/lua/luci/controller/admin/system.lua
index 9836284f..fb7eabde 100644
--- a/package/gluon-luci-admin/files/usr/lib/lua/luci/controller/admin/system.lua
+++ b/package/gluon-luci-admin/files/usr/lib/lua/luci/controller/admin/system.lua
@@ -129,35 +129,13 @@ function action_upgrade()
local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
local has_upload = luci.http.formvalue("image")
- -- This does the actual flashing which is invoked inside an iframe
- -- so don't produce meaningful errors here because the the
- -- previous pages should arrange the stuff as required.
- if step == 4 then
- if has_platform and has_image and has_support then
- -- Mimetype text/plain
- luci.http.prepare_content("text/plain")
- luci.http.write("Starting luci-flash...\n")
-
- -- Now invoke sysupgrade
- local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
- local flash = ltn12_popen("/sbin/luci-flash %s %q" %{
- keepcfg and "-k %q" % _keep_pattern() or "", tmpfile
- })
-
- luci.ltn12.pump.all(flash, luci.http.write)
-
- -- Make sure the device is rebooted
- luci.sys.reboot()
- end
-
-
--
-- This is step 1-3, which does the user interaction and
-- image upload.
--
-- Step 1: file upload, error on unsupported image format
- elseif not has_image or not has_support or step == 1 then
+ if not has_image or not has_support or step == 1 then
-- If there is an image but user has requested step 1
-- or type is not supported, then remove it.
if has_image then
@@ -183,11 +161,16 @@ function action_upgrade()
-- Step 3: load iframe which calls the actual flash procedure
elseif step == 3 then
- luci.template.render("admin/upgrade", {
- step=3,
- keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
- } )
- end
+ -- invoke sysupgrade
+ local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
+ fork_exec("/sbin/sysupgrade %s %q" %
+ { keepcfg and "" or "-n"
+ , tmpfile
+ }
+ )
+
+ luci.template.render("admin/upgrade", { step=3 } )
+ end
end
function _keep_pattern()
@@ -233,3 +216,27 @@ function ltn12_popen(command)
nixio.exec("/bin/sh", "-c", command)
end
end
+
+function fork_exec(command)
+ local pid = nixio.fork()
+ if pid > 0 then
+ return
+ elseif pid == 0 then
+ -- change to root dir
+ nixio.chdir("/")
+
+ -- patch stdin, out, err to /dev/null
+ local null = nixio.open("/dev/null", "w+")
+ if null then
+ nixio.dup(null, nixio.stderr)
+ nixio.dup(null, nixio.stdout)
+ nixio.dup(null, nixio.stdin)
+ if null:fileno() > 2 then
+ null:close()
+ end
+ end
+
+ -- replace with target command
+ nixio.exec("/bin/sh", "-c", command)
+ end
+end
diff --git a/package/gluon-luci-admin/files/usr/lib/lua/luci/view/admin/upgrade.htm b/package/gluon-luci-admin/files/usr/lib/lua/luci/view/admin/upgrade.htm
index 3380dc63..d8827a03 100644
--- a/package/gluon-luci-admin/files/usr/lib/lua/luci/view/admin/upgrade.htm
+++ b/package/gluon-luci-admin/files/usr/lib/lua/luci/view/admin/upgrade.htm
@@ -67,8 +67,18 @@ $Id$
- Checksum:
<%=checksum%>
- Size: <%
- local w = require "luci.tools.webadmin"
- write(w.byte_format(filesize))
+ function byte_format(byte)
+ local suff = {"B", "KB", "MB", "GB", "TB"}
+ for i=1, 5 do
+ if byte > 1024 and i < 5 then
+ byte = byte / 1024
+ else
+ return string.format("%.2f %s", byte, suff[i])
+ end
+ end
+ end
+
+ write(byte_format(filesize))
if flashsize > 0 then
write(luci.i18n.translatef(
@@ -97,8 +107,6 @@ $Id$
Wait a few minutes until you try to reconnect.
It might be necessary to renew the address of your computer to reach the device
again, depending on your settings.
-
-
<% end %>
<%+footer%>