Merge pull request #2174 from rosysong/lease-status
[oweals/luci.git] / applications / luci-app-opkg / luasrc / controller / opkg.lua
1 -- Copyright 2018 Jo-Philipp Wich <jo@mein.io>
2 -- Licensed to the public under the Apache License 2.0.
3
4 module("luci.controller.opkg", package.seeall)
5
6 function index()
7         entry({"admin", "system", "opkg"}, template("opkg"), _("Software"), 30)
8         entry({"admin", "system", "opkg", "list"}, call("action_list")).leaf = true
9         entry({"admin", "system", "opkg", "exec"}, post("action_exec")).leaf = true
10         entry({"admin", "system", "opkg", "statvfs"}, call("action_statvfs")).leaf = true
11         entry({"admin", "system", "opkg", "config"}, post_on({ data = true }, "action_config")).leaf = true
12 end
13
14 function action_list(mode)
15         local cmd
16
17         if mode == "installed" then
18                 cmd = { "/bin/cat", "/usr/lib/opkg/status" }
19         else
20                 cmd = { "/bin/sh", "-c", [[find /tmp/opkg-lists/ -type f '!' -name '*.sig' | xargs -r gzip -cd]] }
21         end
22
23         luci.http.prepare_content("text/plain; charset=utf-8")
24         luci.sys.process.exec(cmd, luci.http.write)
25 end
26
27 function action_exec(command, package)
28         local sys = require "luci.sys"
29         local cmd = { "/bin/opkg", "--force-removal-of-dependent-packages", "--force-overwrite" }
30         local pkg = luci.http.formvalue("package")
31
32         if luci.http.formvalue("autoremove") == "true" then
33                 cmd[#cmd + 1] = "--autoremove"
34         end
35
36         cmd[#cmd + 1] = command
37
38         if pkg then
39                 cmd[#cmd + 1] = pkg
40         end
41
42         luci.http.prepare_content("application/json")
43         luci.http.write_json(sys.process.exec(cmd, true, true))
44 end
45
46 function action_statvfs()
47         local fs = require "nixio.fs"
48
49         luci.http.prepare_content("application/json")
50         luci.http.write_json(fs.statvfs("/") or {})
51 end
52
53 function action_config()
54         local fs = require "nixio.fs"
55         local js = require "luci.jsonc"
56         local data = luci.http.formvalue("data")
57
58         if data then
59                 data = js.parse(data)
60
61                 if not data then
62                         luci.http.status(400, "Bad Request")
63                         return
64                 end
65
66                 local file, content
67                 for file, content in pairs(data) do
68                         if type(content) ~= "string" or
69                            (file ~= "opkg.conf" and not file:match("^opkg/[^/]+%.conf$"))
70                         then
71                                 luci.http.status(400, "Bad Request")
72                                 return
73                         end
74
75                         local path = "/etc/%s" % file
76                         if not fs.access(path, "w") then
77                                 luci.http.status(403, "Permission denied")
78                                 return
79                         end
80
81                         fs.writefile(path, content:gsub("\r\n", "\n"))
82                 end
83
84                 luci.http.status(204, "Saved")
85         else
86                 local rv = { ["opkg.conf"] = fs.readfile("/etc/opkg.conf") }
87                 local entries = fs.dir("/etc/opkg")
88                 if entries then
89                         local entry
90                         for entry in entries do
91                                 if entry:match("%.conf$") then
92                                         rv["opkg/%s" % entry] = fs.readfile("/etc/opkg/%s" % entry)
93                                 end
94                         end
95                 end
96
97                 luci.http.prepare_content("application/json")
98                 luci.http.write_json(rv)
99         end
100 end