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 .. (pat and " '*" .. pat:gsub("'", "") .. "*'" or ""))
163 local name, version, desc
165 local line = fd:read("*l")
166 if not line then break end
168 if line:sub(1,1) ~= " " then
169 name, version, desc = line:match("^(.-) %- (.-) %- (.+)")
172 name, version = line:match("^(.-) %- (.+)")
176 cb(name, version, desc)
188 --- List all packages known to opkg.
189 -- @param pat Only find packages matching this pattern, nil lists all packages
190 -- @param cb Callback function invoked for each package, receives name, version and description as arguments
192 function list_all(pat, cb)
193 _list("list", pat, cb)
196 --- List installed packages.
197 -- @param pat Only find packages matching this pattern, nil lists all packages
198 -- @param cb Callback function invoked for each package, receives name, version and description as arguments
200 function list_installed(pat, cb)
201 _list("list_installed", pat, cb)
204 --- Determines the overlay root used by opkg.
205 -- @return String containing the directory path of the overlay root.
206 function overlay_root()
208 local fd = io.open(icfg, "r")
215 if ln and ln:match("^%s*option%s+overlay_root%s+") then
216 od = ln:match("^%s*option%s+overlay_root%s+(%S+)")
218 local s = fs.stat(od)
219 if not s or s.type ~= "dir" then