2 LuCI - Lua Configuration Interface
4 (c) 2008-2011 Jo-Philipp Wich <xm@subsignal.org>
5 (c) 2008 Steven Barth <steven@midlink.org>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
16 local os = require "os"
17 local io = require "io"
18 local fs = require "nixio.fs"
19 local util = require "luci.util"
26 local ipkg = "opkg --force-removal-of-dependent-packages --force-overwrite"
27 local icfg = "/etc/opkg.conf"
29 --- LuCI OPKG call abstraction library
30 module "luci.model.ipkg"
33 -- Internal action function
34 local function _action(cmd, ...)
36 for k, v in pairs({...}) do
37 pkg = pkg .. " '" .. v:gsub("'", "") .. "'"
40 local c = "%s %s %s >/tmp/opkg.stdout 2>/tmp/opkg.stderr" %{ ipkg, cmd, pkg }
41 local r = os.execute(c)
42 local e = fs.readfile("/tmp/opkg.stderr")
43 local o = fs.readfile("/tmp/opkg.stdout")
45 fs.unlink("/tmp/opkg.stderr")
46 fs.unlink("/tmp/opkg.stdout")
48 return r, o or "", e or ""
51 -- Internal parser function
52 local function _parselist(rawdata)
53 if type(rawdata) ~= "function" then
54 error("OPKG: Invalid rawdata given")
61 for line in rawdata do
62 if line:sub(1, 1) ~= " " then
63 local key, val = line:match("(.-): ?(.*)%s*")
66 if key == "Package" then
69 elseif key == "Status" then
71 for j in val:gmatch("([^ ]+)") do
81 c[l] = c[l] .. "\n" .. line
88 -- Internal lookup function
89 local function _lookup(act, pkg)
90 local cmd = ipkg .. " " .. act
92 cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
95 -- OPKG sometimes kills the whole machine because it sucks
96 -- Therefore we have to use a sucky approach too and use
97 -- tmpfiles instead of directly reading the output
98 local tmpfile = os.tmpname()
99 os.execute(cmd .. (" >%s 2>/dev/null" % tmpfile))
101 local data = _parselist(io.lines(tmpfile))
107 --- Return information about installed and available packages.
108 -- @param pkg Limit output to a (set of) packages
109 -- @return Table containing package information
111 return _lookup("info", pkg)
114 --- Return the package status of one or more packages.
115 -- @param pkg Limit output to a (set of) packages
116 -- @return Table containing package status information
118 return _lookup("status", pkg)
121 --- Install one or more packages.
122 -- @param ... List of packages to install
123 -- @return Boolean indicating the status of the action
124 -- @return OPKG return code, STDOUT and STDERR
125 function install(...)
126 return _action("install", ...)
129 --- Determine whether a given package is installed.
130 -- @param pkg Package
132 function installed(pkg)
133 local p = status(pkg)[pkg]
134 return (p and p.Status and p.Status.installed)
137 --- Remove one or more packages.
138 -- @param ... List of packages to install
139 -- @return Boolean indicating the status of the action
140 -- @return OPKG return code, STDOUT and STDERR
142 return _action("remove", ...)
145 --- Update package lists.
146 -- @return Boolean indicating the status of the action
147 -- @return OPKG return code, STDOUT and STDERR
149 return _action("update")
152 --- Upgrades all installed packages.
153 -- @return Boolean indicating the status of the action
154 -- @return OPKG return code, STDOUT and STDERR
156 return _action("upgrade")
160 function _list(action, pat, cb)
161 local fd = io.popen(ipkg .. " " .. action ..
162 (pat and (" '%s'" % pat:gsub("'", "")) or "")) -- .. " | grep -vE '^ '")
165 local name, version, desc
167 local line = fd:read("*l")
168 if not line then break end
170 if line:sub(1,1) ~= " " then
171 name, version, desc = line:match("^(.-) %- (.-) %- (.+)")
174 name, version = line:match("^(.-) %- (.+)")
178 cb(name, version, desc)
190 --- List all packages known to opkg.
191 -- @param pat Only find packages matching this pattern, nil lists all packages
192 -- @param cb Callback function invoked for each package, receives name, version and description as arguments
194 function list_all(pat, cb)
195 _list("list", pat, cb)
198 --- List installed packages.
199 -- @param pat Only find packages matching this pattern, nil lists all packages
200 -- @param cb Callback function invoked for each package, receives name, version and description as arguments
202 function list_installed(pat, cb)
203 _list("list_installed", pat, cb)
206 --- Determines the overlay root used by opkg.
207 -- @return String containing the directory path of the overlay root.
208 function overlay_root()
210 local fd = io.open(icfg, "r")
217 if ln and ln:match("^%s*option%s+overlay_root%s+") then
218 od = ln:match("^%s*option%s+overlay_root%s+(%S+)")
220 local s = fs.stat(od)
221 if not s or s.type ~= "dir" then