opkg.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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" }
  24. local pkg = luci.http.formvalue("package")
  25. if luci.http.formvalue("autoremove") == "true" then
  26. cmd[#cmd + 1] = "--autoremove"
  27. end
  28. if luci.http.formvalue("overwrite") == "true" then
  29. cmd[#cmd + 1] = "--force-overwrite"
  30. end
  31. cmd[#cmd + 1] = command
  32. if pkg then
  33. cmd[#cmd + 1] = pkg
  34. end
  35. luci.http.prepare_content("application/json")
  36. luci.http.write_json(sys.process.exec(cmd, true, true))
  37. end
  38. function action_statvfs()
  39. local fs = require "nixio.fs"
  40. luci.http.prepare_content("application/json")
  41. luci.http.write_json(fs.statvfs("/") or {})
  42. end
  43. function action_config()
  44. local fs = require "nixio.fs"
  45. local js = require "luci.jsonc"
  46. local data = luci.http.formvalue("data")
  47. if data then
  48. data = js.parse(data)
  49. if not data then
  50. luci.http.status(400, "Bad Request")
  51. return
  52. end
  53. local file, content
  54. for file, content in pairs(data) do
  55. if type(content) ~= "string" or
  56. (file ~= "opkg.conf" and not file:match("^opkg/[^/]+%.conf$"))
  57. then
  58. luci.http.status(400, "Bad Request")
  59. return
  60. end
  61. local path = "/etc/%s" % file
  62. if not fs.access(path, "w") then
  63. luci.http.status(403, "Permission denied")
  64. return
  65. end
  66. fs.writefile(path, content:gsub("\r\n", "\n"))
  67. end
  68. luci.http.status(204, "Saved")
  69. else
  70. local rv = { ["opkg.conf"] = fs.readfile("/etc/opkg.conf") }
  71. local entries = fs.dir("/etc/opkg")
  72. if entries then
  73. local entry
  74. for entry in entries do
  75. if entry:match("%.conf$") then
  76. rv["opkg/%s" % entry] = fs.readfile("/etc/opkg/%s" % entry)
  77. end
  78. end
  79. end
  80. luci.http.prepare_content("application/json")
  81. luci.http.write_json(rv)
  82. end
  83. end