Update my email addresses in the license headers
[project/luci.git] / modules / luci-base / luasrc / model / ipkg.lua
1 -- Copyright 2008-2011 Jo-Philipp Wich <jow@openwrt.org>
2 -- Copyright 2008 Steven Barth <steven@midlink.org>
3 -- Licensed to the public under the Apache License 2.0.
4
5 local os = require "os"
6 local io = require "io"
7 local fs = require "nixio.fs"
8 local util = require "luci.util"
9
10 local type = type
11 local pairs = pairs
12 local error = error
13 local table = table
14
15 local ipkg = "opkg --force-removal-of-dependent-packages --force-overwrite --nocase"
16 local icfg = "/etc/opkg.conf"
17
18 --- LuCI OPKG call abstraction library
19 module "luci.model.ipkg"
20
21
22 -- Internal action function
23 local function _action(cmd, ...)
24 local pkg = ""
25 for k, v in pairs({...}) do
26 pkg = pkg .. " '" .. v:gsub("'", "") .. "'"
27 end
28
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")
33
34 fs.unlink("/tmp/opkg.stderr")
35 fs.unlink("/tmp/opkg.stdout")
36
37 return r, o or "", e or ""
38 end
39
40 -- Internal parser function
41 local function _parselist(rawdata)
42 if type(rawdata) ~= "function" then
43 error("OPKG: Invalid rawdata given")
44 end
45
46 local data = {}
47 local c = {}
48 local l = nil
49
50 for line in rawdata do
51 if line:sub(1, 1) ~= " " then
52 local key, val = line:match("(.-): ?(.*)%s*")
53
54 if key and val then
55 if key == "Package" then
56 c = {Package = val}
57 data[val] = c
58 elseif key == "Status" then
59 c.Status = {}
60 for j in val:gmatch("([^ ]+)") do
61 c.Status[j] = true
62 end
63 else
64 c[key] = val
65 end
66 l = key
67 end
68 else
69 -- Multi-line field
70 c[l] = c[l] .. "\n" .. line
71 end
72 end
73
74 return data
75 end
76
77 -- Internal lookup function
78 local function _lookup(act, pkg)
79 local cmd = ipkg .. " " .. act
80 if pkg then
81 cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
82 end
83
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))
89
90 local data = _parselist(io.lines(tmpfile))
91 os.remove(tmpfile)
92 return data
93 end
94
95
96 --- Return information about installed and available packages.
97 -- @param pkg Limit output to a (set of) packages
98 -- @return Table containing package information
99 function info(pkg)
100 return _lookup("info", pkg)
101 end
102
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
106 function status(pkg)
107 return _lookup("status", pkg)
108 end
109
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", ...)
116 end
117
118 --- Determine whether a given package is installed.
119 -- @param pkg Package
120 -- @return Boolean
121 function installed(pkg)
122 local p = status(pkg)[pkg]
123 return (p and p.Status and p.Status.installed)
124 end
125
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
130 function remove(...)
131 return _action("remove", ...)
132 end
133
134 --- Update package lists.
135 -- @return Boolean indicating the status of the action
136 -- @return OPKG return code, STDOUT and STDERR
137 function update()
138 return _action("update")
139 end
140
141 --- Upgrades all installed packages.
142 -- @return Boolean indicating the status of the action
143 -- @return OPKG return code, STDOUT and STDERR
144 function upgrade()
145 return _action("upgrade")
146 end
147
148 -- List helper
149 function _list(action, pat, cb)
150 local fd = io.popen(ipkg .. " " .. action ..
151 (pat and (" '%s'" % pat:gsub("'", "")) or ""))
152
153 if fd then
154 local name, version, desc
155 while true do
156 local line = fd:read("*l")
157 if not line then break end
158
159 name, version, desc = line:match("^(.-) %- (.-) %- (.+)")
160
161 if not name then
162 name, version = line:match("^(.-) %- (.+)")
163 desc = ""
164 end
165
166 cb(name, version, desc)
167
168 name = nil
169 version = nil
170 desc = nil
171 end
172
173 fd:close()
174 end
175 end
176
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
180 -- @return nothing
181 function list_all(pat, cb)
182 _list("list", pat, cb)
183 end
184
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
188 -- @return nothing
189 function list_installed(pat, cb)
190 _list("list_installed", pat, cb)
191 end
192
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
196 -- @return nothing
197 function find(pat, cb)
198 _list("find", pat, cb)
199 end
200
201
202 --- Determines the overlay root used by opkg.
203 -- @return String containing the directory path of the overlay root.
204 function overlay_root()
205 local od = "/"
206 local fd = io.open(icfg, "r")
207
208 if fd then
209 local ln
210
211 repeat
212 ln = fd:read("*l")
213 if ln and ln:match("^%s*option%s+overlay_root%s+") then
214 od = ln:match("^%s*option%s+overlay_root%s+(%S+)")
215
216 local s = fs.stat(od)
217 if not s or s.type ~= "dir" then
218 od = "/"
219 end
220
221 break
222 end
223 until not ln
224
225 fd:close()
226 end
227
228 return od
229 end