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
15 local os = require "os"
16 local io = require "io"
17 local fs = require "nixio.fs"
18 local util = require "luci.util"
25 local ipkg = "opkg --force-removal-of-dependent-packages --force-overwrite --nocase"
26 local icfg = "/etc/opkg.conf"
28 --- LuCI OPKG call abstraction library
29 module "luci.model.ipkg"
32 -- Internal action function
33 local function _action(cmd, ...)
35 for k, v in pairs({...}) do
36 pkg = pkg .. " '" .. v:gsub("'", "") .. "'"
39 local c = "%s %s %s >/tmp/opkg.stdout 2>/tmp/opkg.stderr" %{ ipkg, cmd, pkg }
40 local r = os.execute(c)
41 local e = fs.readfile("/tmp/opkg.stderr")
42 local o = fs.readfile("/tmp/opkg.stdout")
44 fs.unlink("/tmp/opkg.stderr")
45 fs.unlink("/tmp/opkg.stdout")
47 return r, o or "", e or ""
50 -- Internal parser function
51 local function _parselist(rawdata)
52 if type(rawdata) ~= "function" then
53 error("OPKG: Invalid rawdata given")
60 for line in rawdata do
61 if line:sub(1, 1) ~= " " then
62 local key, val = line:match("(.-): ?(.*)%s*")
65 if key == "Package" then
68 elseif key == "Status" then
70 for j in val:gmatch("([^ ]+)") do
80 c[l] = c[l] .. "\n" .. line
87 -- Internal lookup function
88 local function _lookup(act, pkg)
89 local cmd = ipkg .. " " .. act
91 cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
94 -- OPKG sometimes kills the whole machine because it sucks
95 -- Therefore we have to use a sucky approach too and use
96 -- tmpfiles instead of directly reading the output
97 local tmpfile = os.tmpname()
98 os.execute(cmd .. (" >%s 2>/dev/null" % tmpfile))
100 local data = _parselist(io.lines(tmpfile))
106 --- Return information about installed and available packages.
107 -- @param pkg Limit output to a (set of) packages
108 -- @return Table containing package information
110 return _lookup("info", pkg)
113 --- Return the package status of one or more packages.
114 -- @param pkg Limit output to a (set of) packages
115 -- @return Table containing package status information
117 return _lookup("status", pkg)
120 --- Install one or more packages.
121 -- @param ... List of packages to install
122 -- @return Boolean indicating the status of the action
123 -- @return OPKG return code, STDOUT and STDERR
124 function install(...)
125 return _action("install", ...)
128 --- Determine whether a given package is installed.
129 -- @param pkg Package
131 function installed(pkg)
132 local p = status(pkg)[pkg]
133 return (p and p.Status and p.Status.installed)
136 --- Remove one or more packages.
137 -- @param ... List of packages to install
138 -- @return Boolean indicating the status of the action
139 -- @return OPKG return code, STDOUT and STDERR
141 return _action("remove", ...)
144 --- Update package lists.
145 -- @return Boolean indicating the status of the action
146 -- @return OPKG return code, STDOUT and STDERR
148 return _action("update")
151 --- Upgrades all installed packages.
152 -- @return Boolean indicating the status of the action
153 -- @return OPKG return code, STDOUT and STDERR
155 return _action("upgrade")
159 function _list(action, pat, cb)
160 local fd = io.popen(ipkg .. " " .. action ..
161 (pat and (" '%s'" % pat:gsub("'", "")) or ""))
164 local name, version, desc
166 local line = fd:read("*l")
167 if not line then break end
169 name, version, desc = line:match("^(.-) %- (.-) %- (.+)")
172 name, version = line:match("^(.-) %- (.+)")
176 cb(name, version, desc)
187 --- List all packages known to opkg.
188 -- @param pat Only find packages matching this pattern, nil lists all packages
189 -- @param cb Callback function invoked for each package, receives name, version and description as arguments
191 function list_all(pat, cb)
192 _list("list", pat, cb)
195 --- List installed packages.
196 -- @param pat Only find packages matching this pattern, nil lists all packages
197 -- @param cb Callback function invoked for each package, receives name, version and description as arguments
199 function list_installed(pat, cb)
200 _list("list_installed", pat, cb)
203 --- Find packages that match the given pattern.
204 -- @param pat Find packages whose names or descriptions match this pattern, nil results in zero results
205 -- @param cb Callback function invoked for each patckage, receives name, version and description as arguments
207 function find(pat, cb)
208 _list("find", pat, cb)
212 --- Determines the overlay root used by opkg.
213 -- @return String containing the directory path of the overlay root.
214 function overlay_root()
216 local fd = io.open(icfg, "r")
223 if ln and ln:match("^%s*option%s+overlay_root%s+") then
224 od = ln:match("^%s*option%s+overlay_root%s+(%S+)")
226 local s = fs.stat(od)
227 if not s or s.type ~= "dir" then