1
0

opkg.lua 2.6 KB

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