1 -- Copyright 2008-2011 Jo-Philipp Wich <xm@subsignal.org>
2 -- Copyright 2008 Steven Barth <steven@midlink.org>
3 -- Licensed to the public under the Apache License 2.0.
5 local os = require "os"
6 local io = require "io"
7 local fs = require "nixio.fs"
8 local util = require "luci.util"
15 local ipkg = "opkg --force-removal-of-dependent-packages --force-overwrite --nocase"
16 local icfg = "/etc/opkg.conf"
18 --- LuCI OPKG call abstraction library
19 module "luci.model.ipkg"
22 -- Internal action function
23 local function _action(cmd, ...)
25 for k, v in pairs({...}) do
26 pkg = pkg .. " '" .. v:gsub("'", "") .. "'"
29 local c = "%s %s %s >/tmp/opkg.stdout 2>/tmp/opkg.stderr" %{ ipkg, cmd, pkg }
30 local r = os.execute(c)
31 local e = fs.readfile("/tmp/opkg.stderr")
32 local o = fs.readfile("/tmp/opkg.stdout")
34 fs.unlink("/tmp/opkg.stderr")
35 fs.unlink("/tmp/opkg.stdout")
37 return r, o or "", e or ""
40 -- Internal parser function
41 local function _parselist(rawdata)
42 if type(rawdata) ~= "function" then
43 error("OPKG: Invalid rawdata given")
50 for line in rawdata do
51 if line:sub(1, 1) ~= " " then
52 local key, val = line:match("(.-): ?(.*)%s*")
55 if key == "Package" then
58 elseif key == "Status" then
60 for j in val:gmatch("([^ ]+)") do
70 c[l] = c[l] .. "\n" .. line
77 -- Internal lookup function
78 local function _lookup(act, pkg)
79 local cmd = ipkg .. " " .. act
81 cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
84 -- OPKG sometimes kills the whole machine because it sucks
85 -- Therefore we have to use a sucky approach too and use
86 -- tmpfiles instead of directly reading the output
87 local tmpfile = os.tmpname()
88 os.execute(cmd .. (" >%s 2>/dev/null" % tmpfile))
90 local data = _parselist(io.lines(tmpfile))
96 --- Return information about installed and available packages.
97 -- @param pkg Limit output to a (set of) packages
98 -- @return Table containing package information
100 return _lookup("info", pkg)
103 --- Return the package status of one or more packages.
104 -- @param pkg Limit output to a (set of) packages
105 -- @return Table containing package status information
107 return _lookup("status", pkg)
110 --- Install one or more packages.
111 -- @param ... List of packages to install
112 -- @return Boolean indicating the status of the action
113 -- @return OPKG return code, STDOUT and STDERR
114 function install(...)
115 return _action("install", ...)
118 --- Determine whether a given package is installed.
119 -- @param pkg Package
121 function installed(pkg)
122 local p = status(pkg)[pkg]
123 return (p and p.Status and p.Status.installed)
126 --- Remove one or more packages.
127 -- @param ... List of packages to install
128 -- @return Boolean indicating the status of the action
129 -- @return OPKG return code, STDOUT and STDERR
131 return _action("remove", ...)
134 --- Update package lists.
135 -- @return Boolean indicating the status of the action
136 -- @return OPKG return code, STDOUT and STDERR
138 return _action("update")
141 --- Upgrades all installed packages.
142 -- @return Boolean indicating the status of the action
143 -- @return OPKG return code, STDOUT and STDERR
145 return _action("upgrade")
149 function _list(action, pat, cb)
150 local fd = io.popen(ipkg .. " " .. action ..
151 (pat and (" '%s'" % pat:gsub("'", "")) or ""))
154 local name, version, desc
156 local line = fd:read("*l")
157 if not line then break end
159 name, version, desc = line:match("^(.-) %- (.-) %- (.+)")
162 name, version = line:match("^(.-) %- (.+)")
166 cb(name, version, desc)
177 --- List all packages known to opkg.
178 -- @param pat Only find packages matching this pattern, nil lists all packages
179 -- @param cb Callback function invoked for each package, receives name, version and description as arguments
181 function list_all(pat, cb)
182 _list("list", pat, cb)
185 --- List installed packages.
186 -- @param pat Only find packages matching this pattern, nil lists all packages
187 -- @param cb Callback function invoked for each package, receives name, version and description as arguments
189 function list_installed(pat, cb)
190 _list("list_installed", pat, cb)
193 --- Find packages that match the given pattern.
194 -- @param pat Find packages whose names or descriptions match this pattern, nil results in zero results
195 -- @param cb Callback function invoked for each patckage, receives name, version and description as arguments
197 function find(pat, cb)
198 _list("find", pat, cb)
202 --- Determines the overlay root used by opkg.
203 -- @return String containing the directory path of the overlay root.
204 function overlay_root()
206 local fd = io.open(icfg, "r")
213 if ln and ln:match("^%s*option%s+overlay_root%s+") then
214 od = ln:match("^%s*option%s+overlay_root%s+(%S+)")
216 local s = fs.stat(od)
217 if not s or s.type ~= "dir" then