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)
6 function action_list(mode)
7 local util = require "luci.util"
10 if mode == "installed" then
11 cmd = { "/bin/cat", "/usr/lib/opkg/status" }
15 local fd = io.popen([[sed -rne 's#^lists_dir \S+ (\S+)#\1#p' /etc/opkg.conf /etc/opkg/*.conf 2>/dev/null]], "r")
17 lists_dir = fd:read("*l")
21 if not lists_dir or #lists_dir == 0 then
22 lists_dir = "/tmp/opkg-lists"
25 cmd = { "/bin/sh", "-c", [[find %s -type f '!' -name '*.sig' | xargs -r gzip -cd]] % util.shellquote(lists_dir) }
28 luci.http.prepare_content("text/plain; charset=utf-8")
29 luci.sys.process.exec(cmd, luci.http.write)
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")
37 if luci.http.formvalue("autoremove") == "true" then
38 cmd[#cmd + 1] = "--autoremove"
41 if luci.http.formvalue("overwrite") == "true" then
42 cmd[#cmd + 1] = "--force-overwrite"
45 cmd[#cmd + 1] = command
51 luci.http.prepare_content("application/json")
52 luci.http.write_json(sys.process.exec(cmd, true, true))
55 function action_statvfs()
56 local fs = require "nixio.fs"
58 luci.http.prepare_content("application/json")
59 luci.http.write_json(fs.statvfs("/") or {})
62 function action_config()
63 local fs = require "nixio.fs"
64 local js = require "luci.jsonc"
65 local data = luci.http.formvalue("data")
71 luci.http.status(400, "Bad Request")
76 for file, content in pairs(data) do
77 if type(content) ~= "string" or
78 (file ~= "opkg.conf" and not file:match("^opkg/[^/]+%.conf$"))
80 luci.http.status(400, "Bad Request")
84 local path = "/etc/%s" % file
85 if not fs.access(path, "w") then
86 luci.http.status(403, "Permission denied")
90 fs.writefile(path, content:gsub("\r\n", "\n"))
93 luci.http.status(204, "Saved")
95 local rv = { ["opkg.conf"] = fs.readfile("/etc/opkg.conf") }
96 local entries = fs.dir("/etc/opkg")
99 for entry in entries do
100 if entry:match("%.conf$") then
101 rv["opkg/%s" % entry] = fs.readfile("/etc/opkg/%s" % entry)
106 luci.http.prepare_content("application/json")
107 luci.http.write_json(rv)