Globally reduce copyright headers
[project/luci.git] / modules / luci-mod-admin-mini / luasrc / controller / mini / system.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
3 -- Licensed to the public under the Apache License 2.0.
4
5 module("luci.controller.mini.system", package.seeall)
6
7 function index()
8 entry({"mini", "system"}, alias("mini", "system", "index"), _("System"), 40).index = true
9 entry({"mini", "system", "index"}, cbi("mini/system", {autoapply=true}), _("General"), 1)
10 entry({"mini", "system", "passwd"}, form("mini/passwd"), _("Admin Password"), 10)
11 entry({"mini", "system", "backup"}, call("action_backup"), _("Backup / Restore"), 80)
12 entry({"mini", "system", "upgrade"}, call("action_upgrade"), _("Flash Firmware"), 90)
13 entry({"mini", "system", "reboot"}, call("action_reboot"), _("Reboot"), 100)
14 end
15
16 function action_backup()
17 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
18 local restore_cmd = "gunzip | tar -xC/ >/dev/null 2>&1"
19 local backup_cmd = "tar -c %s | gzip 2>/dev/null"
20
21 local restore_fpi
22 luci.http.setfilehandler(
23 function(meta, chunk, eof)
24 if not restore_fpi then
25 restore_fpi = io.popen(restore_cmd, "w")
26 end
27 if chunk then
28 restore_fpi:write(chunk)
29 end
30 if eof then
31 restore_fpi:close()
32 end
33 end
34 )
35
36 local upload = luci.http.formvalue("archive")
37 local backup = luci.http.formvalue("backup")
38 local reset = reset_avail and luci.http.formvalue("reset")
39
40 if upload and #upload > 0 then
41 luci.template.render("mini/applyreboot")
42 luci.sys.reboot()
43 elseif backup then
44 local reader = ltn12_popen(backup_cmd:format(_keep_pattern()))
45 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
46 luci.sys.hostname(), os.date("%Y-%m-%d")})
47 luci.http.prepare_content("application/x-targz")
48 luci.ltn12.pump.all(reader, luci.http.write)
49 elseif reset then
50 luci.template.render("mini/applyreboot")
51 luci.util.exec("mtd -r erase rootfs_data")
52 else
53 luci.template.render("mini/backup", {reset_avail = reset_avail})
54 end
55 end
56
57 function action_reboot()
58 local reboot = luci.http.formvalue("reboot")
59 luci.template.render("mini/reboot", {reboot=reboot})
60 if reboot then
61 luci.sys.reboot()
62 end
63 end
64
65 function action_upgrade()
66 require("luci.model.uci")
67
68 local tmpfile = "/tmp/firmware.img"
69
70 local function image_supported()
71 -- XXX: yay...
72 return ( 0 == os.execute(
73 ". /lib/functions.sh; " ..
74 "include /lib/upgrade; " ..
75 "platform_check_image %q >/dev/null"
76 % tmpfile
77 ) )
78 end
79
80 local function image_checksum()
81 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
82 end
83
84 local function storage_size()
85 local size = 0
86 if nixio.fs.access("/proc/mtd") then
87 for l in io.lines("/proc/mtd") do
88 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
89 if n == "linux" then
90 size = tonumber(s, 16)
91 break
92 end
93 end
94 elseif nixio.fs.access("/proc/partitions") then
95 for l in io.lines("/proc/partitions") do
96 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
97 if b and n and not n:match('[0-9]') then
98 size = tonumber(b) * 1024
99 break
100 end
101 end
102 end
103 return size
104 end
105
106
107 -- Install upload handler
108 local file
109 luci.http.setfilehandler(
110 function(meta, chunk, eof)
111 if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
112 file = io.open(tmpfile, "w")
113 end
114 if file and chunk then
115 file:write(chunk)
116 end
117 if file and eof then
118 file:close()
119 end
120 end
121 )
122
123
124 -- Determine state
125 local keep_avail = true
126 local step = tonumber(luci.http.formvalue("step") or 1)
127 local has_image = nixio.fs.access(tmpfile)
128 local has_support = image_supported()
129 local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
130 local has_upload = luci.http.formvalue("image")
131
132 -- This does the actual flashing which is invoked inside an iframe
133 -- so don't produce meaningful errors here because the the
134 -- previous pages should arrange the stuff as required.
135 if step == 4 then
136 if has_platform and has_image and has_support then
137 -- Mimetype text/plain
138 luci.http.prepare_content("text/plain")
139 luci.http.write("Starting luci-flash...\n")
140
141 -- Now invoke sysupgrade
142 local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
143 local flash = ltn12_popen("/sbin/luci-flash %s %q" %{
144 keepcfg and "-k %q" % _keep_pattern() or "", tmpfile
145 })
146
147 luci.ltn12.pump.all(flash, luci.http.write)
148
149 -- Make sure the device is rebooted
150 luci.sys.reboot()
151 end
152
153
154 --
155 -- This is step 1-3, which does the user interaction and
156 -- image upload.
157 --
158
159 -- Step 1: file upload, error on unsupported image format
160 elseif not has_image or not has_support or step == 1 then
161 -- If there is an image but user has requested step 1
162 -- or type is not supported, then remove it.
163 if has_image then
164 nixio.fs.unlink(tmpfile)
165 end
166
167 luci.template.render("mini/upgrade", {
168 step=1,
169 bad_image=(has_image and not has_support or false),
170 keepavail=keep_avail,
171 supported=has_platform
172 } )
173
174 -- Step 2: present uploaded file, show checksum, confirmation
175 elseif step == 2 then
176 luci.template.render("mini/upgrade", {
177 step=2,
178 checksum=image_checksum(),
179 filesize=nixio.fs.stat(tmpfile).size,
180 flashsize=storage_size(),
181 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
182 } )
183
184 -- Step 3: load iframe which calls the actual flash procedure
185 elseif step == 3 then
186 luci.template.render("mini/upgrade", {
187 step=3,
188 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
189 } )
190 end
191 end
192
193 function _keep_pattern()
194 local kpattern = ""
195 local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
196 if files then
197 kpattern = ""
198 for k, v in pairs(files) do
199 if k:sub(1,1) ~= "." and nixio.fs.glob(v)() then
200 kpattern = kpattern .. " " .. v
201 end
202 end
203 end
204 return kpattern
205 end
206
207 function ltn12_popen(command)
208
209 local fdi, fdo = nixio.pipe()
210 local pid = nixio.fork()
211
212 if pid > 0 then
213 fdo:close()
214 local close
215 return function()
216 local buffer = fdi:read(2048)
217 local wpid, stat = nixio.waitpid(pid, "nohang")
218 if not close and wpid and stat == "exited" then
219 close = true
220 end
221
222 if buffer and #buffer > 0 then
223 return buffer
224 elseif close then
225 fdi:close()
226 return nil
227 end
228 end
229 elseif pid == 0 then
230 nixio.dup(fdo, nixio.stdout)
231 fdi:close()
232 fdo:close()
233 nixio.exec("/bin/sh", "-c", command)
234 end
235 end