Merge pull request #3400 from TDT-AG/pr/20191210-luci-mk
[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 action_list(mode)
7         local util = require "luci.util"
8         local cmd
9
10         if mode == "installed" then
11                 cmd = { "/bin/cat", "/usr/lib/opkg/status" }
12         else
13                 local lists_dir = nil
14
15                 local fd = io.popen([[sed -rne 's#^lists_dir \S+ (\S+)#\1#p' /etc/opkg.conf /etc/opkg/*.conf 2>/dev/null]], "r")
16                 if fd then
17                         lists_dir = fd:read("*l")
18                         fd:close()
19                 end
20
21                 if not lists_dir or #lists_dir == 0 then
22                         lists_dir = "/tmp/opkg-lists"
23                 end
24
25                 cmd = { "/bin/sh", "-c", [[find %s -type f '!' -name '*.sig' | xargs -r gzip -cd]] % util.shellquote(lists_dir) }
26         end
27
28         luci.http.prepare_content("text/plain; charset=utf-8")
29         luci.sys.process.exec(cmd, luci.http.write)
30 end
31
32 function action_exec(command, package)
33         local sys = require "luci.sys"
34         local cmd = { "/bin/opkg", "--force-removal-of-dependent-packages" }
35         local pkg = luci.http.formvalue("package")
36
37         if luci.http.formvalue("autoremove") == "true" then
38                 cmd[#cmd + 1] = "--autoremove"
39         end
40
41         if luci.http.formvalue("overwrite") == "true" then
42                 cmd[#cmd + 1] = "--force-overwrite"
43         end
44
45         cmd[#cmd + 1] = command
46
47         if pkg then
48                 cmd[#cmd + 1] = pkg
49         end
50
51         luci.http.prepare_content("application/json")
52         luci.http.write_json(sys.process.exec(cmd, true, true))
53 end
54
55 function action_statvfs()
56         local fs = require "nixio.fs"
57
58         luci.http.prepare_content("application/json")
59         luci.http.write_json(fs.statvfs("/") or {})
60 end
61
62 function action_config()
63         local fs = require "nixio.fs"
64         local js = require "luci.jsonc"
65         local data = luci.http.formvalue("data")
66
67         if data then
68                 data = js.parse(data)
69
70                 if not data then
71                         luci.http.status(400, "Bad Request")
72                         return
73                 end
74
75                 local file, content
76                 for file, content in pairs(data) do
77                         if type(content) ~= "string" or
78                            (file ~= "opkg.conf" and not file:match("^opkg/[^/]+%.conf$"))
79                         then
80                                 luci.http.status(400, "Bad Request")
81                                 return
82                         end
83
84                         local path = "/etc/%s" % file
85                         if not fs.access(path, "w") then
86                                 luci.http.status(403, "Permission denied")
87                                 return
88                         end
89
90                         fs.writefile(path, content:gsub("\r\n", "\n"))
91                 end
92
93                 luci.http.status(204, "Saved")
94         else
95                 local rv = { ["opkg.conf"] = fs.readfile("/etc/opkg.conf") }
96                 local entries = fs.dir("/etc/opkg")
97                 if entries then
98                         local entry
99                         for entry in entries do
100                                 if entry:match("%.conf$") then
101                                         rv["opkg/%s" % entry] = fs.readfile("/etc/opkg/%s" % entry)
102                                 end
103                         end
104                 end
105
106                 luci.http.prepare_content("application/json")
107                 luci.http.write_json(rv)
108         end
109 end