1 -- Copyright 2018 Jo-Philipp Wich <jo@mein.io>
2 -- Licensed to the public under the Apache License 2.0.
4 module("luci.controller.opkg", package.seeall)
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
14 function action_list(mode)
17 if mode == "installed" then
18 cmd = { "/bin/cat", "/usr/lib/opkg/status" }
20 cmd = { "/bin/sh", "-c", [[find /tmp/opkg-lists/ -type f '!' -name '*.sig' | xargs -r gzip -cd]] }
23 luci.http.prepare_content("text/plain; charset=utf-8")
24 luci.sys.process.exec(cmd, luci.http.write)
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")
32 if luci.http.formvalue("autoremove") == "true" then
33 cmd[#cmd + 1] = "--autoremove"
36 cmd[#cmd + 1] = command
42 luci.http.prepare_content("application/json")
43 luci.http.write_json(sys.process.exec(cmd, true, true))
46 function action_statvfs()
47 local fs = require "nixio.fs"
49 luci.http.prepare_content("application/json")
50 luci.http.write_json(fs.statvfs("/") or {})
53 function action_config()
54 local fs = require "nixio.fs"
55 local js = require "luci.jsonc"
56 local data = luci.http.formvalue("data")
62 luci.http.status(400, "Bad Request")
67 for file, content in pairs(data) do
68 if type(content) ~= "string" or
69 (file ~= "opkg.conf" and not file:match("^opkg/[^/]+%.conf$"))
71 luci.http.status(400, "Bad Request")
75 local path = "/etc/%s" % file
76 if not fs.access(path, "w") then
77 luci.http.status(403, "Permission denied")
81 fs.writefile(path, content:gsub("\r\n", "\n"))
84 luci.http.status(204, "Saved")
86 local rv = { ["opkg.conf"] = fs.readfile("/etc/opkg.conf") }
87 local entries = fs.dir("/etc/opkg")
90 for entry in entries do
91 if entry:match("%.conf$") then
92 rv["opkg/%s" % entry] = fs.readfile("/etc/opkg/%s" % entry)
97 luci.http.prepare_content("application/json")
98 luci.http.write_json(rv)