2 LuCI - Lua Configuration Interface
4 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
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 IPKG/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 = ipkg.." "..cmd.." "..pkg.." >/dev/null 2>&1"
41 local r = os.execute(c)
45 -- Internal parser function
46 local function _parselist(rawdata)
47 if type(rawdata) ~= "function" then
48 error("IPKG: Invalid rawdata given")
55 for line in rawdata do
56 if line:sub(1, 1) ~= " " then
57 local key, val = line:match("(.-): ?(.*)%s*")
60 if key == "Package" then
63 elseif key == "Status" then
65 for j in val:gmatch("([^ ]+)") do
75 c[l] = c[l] .. "\n" .. line
82 -- Internal lookup function
83 local function _lookup(act, pkg)
84 local cmd = ipkg .. " " .. act
86 cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
89 -- IPKG sometimes kills the whole machine because it sucks
90 -- Therefore we have to use a sucky approach too and use
91 -- tmpfiles instead of directly reading the output
92 local tmpfile = os.tmpname()
93 os.execute(cmd .. (" >%s 2>/dev/null" % tmpfile))
95 local data = _parselist(io.lines(tmpfile))
101 --- Return information about installed and available packages.
102 -- @param pkg Limit output to a (set of) packages
103 -- @return Table containing package information
105 return _lookup("info", pkg)
108 --- Return the package status of one or more packages.
109 -- @param pkg Limit output to a (set of) packages
110 -- @return Table containing package status information
112 return _lookup("status", pkg)
115 --- Install one or more packages.
116 -- @param ... List of packages to install
117 -- @return Boolean indicating the status of the action
118 -- @return IPKG return code
119 function install(...)
120 return _action("install", ...)
123 --- Determine whether a given package is installed.
124 -- @param pkg Package
126 function installed(pkg)
127 local p = status(pkg)[pkg]
128 return (p and p.Status and p.Status.installed)
131 --- Remove one or more packages.
132 -- @param ... List of packages to install
133 -- @return Boolean indicating the status of the action
134 -- @return IPKG return code
136 return _action("remove", ...)
139 --- Update package lists.
140 -- @return Boolean indicating the status of the action
141 -- @return IPKG return code
143 return _action("update")
146 --- Upgrades all installed packages.
147 -- @return Boolean indicating the status of the action
148 -- @return IPKG return code
150 return _action("upgrade")
154 function _list(action, pat, cb)
155 local fd = io.popen(ipkg .. " " .. action .. (pat and " '*" .. pat:gsub("'", "") .. "*'" or ""))
157 local name, version, desc
159 local line = fd:read("*l")
160 if not line then break end
162 if line:sub(1,1) ~= " " then
163 name, version, desc = line:match("^(.-) %- (.-) %- (.+)")
166 name, version = line:match("^(.-) %- (.+)")
170 cb(name, version, desc)
182 --- List all packages known to opkg.
183 -- @param pat Only find packages matching this pattern, nil lists all packages
184 -- @param cb Callback function invoked for each package, receives name, version and description as arguments
186 function list_all(pat, cb)
187 _list("list", pat, cb)
190 --- List installed packages.
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_installed(pat, cb)
195 _list("list_installed", pat, cb)
198 --- Determines the overlay root used by opkg.
199 -- @return String containing the directory path of the overlay root.
200 function overlay_root()
202 local fd = io.open(icfg, "r")
209 if ln and ln:match("^%s*option%s+overlay_root%s+") then
210 od = ln:match("^%s*option%s+overlay_root%s+(%S+)")
212 local s = fs.stat(od)
213 if not s or s.type ~= "dir" then