73fa3c305c13ce38ccffc73b21187ec3a6305782
[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 .. (pat and " '*" .. pat:gsub("'", "") .. "*'" or ""))
162 if fd then
163 local name, version, desc
164 while true do
165 local line = fd:read("*l")
166 if not line then break end
167
168 if line:sub(1,1) ~= " " then
169 name, version, desc = line:match("^(.-) %- (.-) %- (.+)")
170
171 if not name then
172 name, version = line:match("^(.-) %- (.+)")
173 desc = ""
174 end
175
176 cb(name, version, desc)
177
178 name = nil
179 version = nil
180 desc = nil
181 end
182 end
183
184 fd:close()
185 end
186 end
187
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
191 -- @return nothing
192 function list_all(pat, cb)
193 _list("list", pat, cb)
194 end
195
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
199 -- @return nothing
200 function list_installed(pat, cb)
201 _list("list_installed", pat, cb)
202 end
203
204 --- Determines the overlay root used by opkg.
205 -- @return String containing the directory path of the overlay root.
206 function overlay_root()
207 local od = "/"
208 local fd = io.open(icfg, "r")
209
210 if fd then
211 local ln
212
213 repeat
214 ln = fd:read("*l")
215 if ln and ln:match("^%s*option%s+overlay_root%s+") then
216 od = ln:match("^%s*option%s+overlay_root%s+(%S+)")
217
218 local s = fs.stat(od)
219 if not s or s.type ~= "dir" then
220 od = "/"
221 end
222
223 break
224 end
225 until not ln
226
227 fd:close()
228 end
229
230 return od
231 end