d0d9788fd8be6c861a74aaead8ec23a06026608c
[project/luci.git] / libs / ipkg / luasrc / model / ipkg.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 (c) 2008-2011 Jo-Philipp Wich <xm@subsignal.org>
5 (c) 2008 Steven Barth <steven@midlink.org>
6
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
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 local os = require "os"
17 local io = require "io"
18 local fs = require "nixio.fs"
19 local util = require "luci.util"
20
21 local type = type
22 local pairs = pairs
23 local error = error
24 local table = table
25
26 local ipkg = "opkg --force-removal-of-dependent-packages --force-overwrite"
27 local icfg = "/etc/opkg.conf"
28
29 --- LuCI OPKG call abstraction library
30 module "luci.model.ipkg"
31
32
33 -- Internal action function
34 local function _action(cmd, ...)
35 local pkg = ""
36 for k, v in pairs({...}) do
37 pkg = pkg .. " '" .. v:gsub("'", "") .. "'"
38 end
39
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")
44
45 fs.unlink("/tmp/opkg.stderr")
46 fs.unlink("/tmp/opkg.stdout")
47
48 return r, o or "", e or ""
49 end
50
51 -- Internal parser function
52 local function _parselist(rawdata)
53 if type(rawdata) ~= "function" then
54 error("OPKG: Invalid rawdata given")
55 end
56
57 local data = {}
58 local c = {}
59 local l = nil
60
61 for line in rawdata do
62 if line:sub(1, 1) ~= " " then
63 local key, val = line:match("(.-): ?(.*)%s*")
64
65 if key and val then
66 if key == "Package" then
67 c = {Package = val}
68 data[val] = c
69 elseif key == "Status" then
70 c.Status = {}
71 for j in val:gmatch("([^ ]+)") do
72 c.Status[j] = true
73 end
74 else
75 c[key] = val
76 end
77 l = key
78 end
79 else
80 -- Multi-line field
81 c[l] = c[l] .. "\n" .. line
82 end
83 end
84
85 return data
86 end
87
88 -- Internal lookup function
89 local function _lookup(act, pkg)
90 local cmd = ipkg .. " " .. act
91 if pkg then
92 cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
93 end
94
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))
100
101 local data = _parselist(io.lines(tmpfile))
102 os.remove(tmpfile)
103 return data
104 end
105
106
107 --- Return information about installed and available packages.
108 -- @param pkg Limit output to a (set of) packages
109 -- @return Table containing package information
110 function info(pkg)
111 return _lookup("info", pkg)
112 end
113
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
117 function status(pkg)
118 return _lookup("status", pkg)
119 end
120
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", ...)
127 end
128
129 --- Determine whether a given package is installed.
130 -- @param pkg Package
131 -- @return Boolean
132 function installed(pkg)
133 local p = status(pkg)[pkg]
134 return (p and p.Status and p.Status.installed)
135 end
136
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
141 function remove(...)
142 return _action("remove", ...)
143 end
144
145 --- Update package lists.
146 -- @return Boolean indicating the status of the action
147 -- @return OPKG return code, STDOUT and STDERR
148 function update()
149 return _action("update")
150 end
151
152 --- Upgrades all installed packages.
153 -- @return Boolean indicating the status of the action
154 -- @return OPKG return code, STDOUT and STDERR
155 function upgrade()
156 return _action("upgrade")
157 end
158
159 -- List helper
160 function _list(action, pat, cb)
161 local fd = io.popen(ipkg .. " " .. action ..
162 (pat and (" '%s'" % pat:gsub("'", "")) or "")) -- .. " | grep -vE '^ '")
163
164 if fd then
165 local name, version, desc
166 while true do
167 local line = fd:read("*l")
168 if not line then break end
169
170 if line:sub(1,1) ~= " " then
171 name, version, desc = line:match("^(.-) %- (.-) %- (.+)")
172
173 if not name then
174 name, version = line:match("^(.-) %- (.+)")
175 desc = ""
176 end
177
178 cb(name, version, desc)
179
180 name = nil
181 version = nil
182 desc = nil
183 end
184 end
185
186 fd:close()
187 end
188 end
189
190 --- List all packages known to opkg.
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
193 -- @return nothing
194 function list_all(pat, cb)
195 _list("list", pat, cb)
196 end
197
198 --- List installed packages.
199 -- @param pat Only find packages matching this pattern, nil lists all packages
200 -- @param cb Callback function invoked for each package, receives name, version and description as arguments
201 -- @return nothing
202 function list_installed(pat, cb)
203 _list("list_installed", pat, cb)
204 end
205
206 --- Determines the overlay root used by opkg.
207 -- @return String containing the directory path of the overlay root.
208 function overlay_root()
209 local od = "/"
210 local fd = io.open(icfg, "r")
211
212 if fd then
213 local ln
214
215 repeat
216 ln = fd:read("*l")
217 if ln and ln:match("^%s*option%s+overlay_root%s+") then
218 od = ln:match("^%s*option%s+overlay_root%s+(%S+)")
219
220 local s = fs.stat(od)
221 if not s or s.type ~= "dir" then
222 od = "/"
223 end
224
225 break
226 end
227 until not ln
228
229 fd:close()
230 end
231
232 return od
233 end