modules/base: work around wireless status changes
[project/luci.git] / modules / base / 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 ]]--
14
15 local os   = require "os"
16 local io   = require "io"
17 local fs   = require "nixio.fs"
18 local util = require "luci.util"
19
20 local type  = type
21 local pairs = pairs
22 local error = error
23 local table = table
24
25 local ipkg = "opkg --force-removal-of-dependent-packages --force-overwrite --nocase"
26 local icfg = "/etc/opkg.conf"
27
28 --- LuCI OPKG call abstraction library
29 module "luci.model.ipkg"
30
31
32 -- Internal action function
33 local function _action(cmd, ...)
34         local pkg = ""
35         for k, v in pairs({...}) do
36                 pkg = pkg .. " '" .. v:gsub("'", "") .. "'"
37         end
38
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")
43
44         fs.unlink("/tmp/opkg.stderr")
45         fs.unlink("/tmp/opkg.stdout")
46
47         return r, o or "", e or ""
48 end
49
50 -- Internal parser function
51 local function _parselist(rawdata)
52         if type(rawdata) ~= "function" then
53                 error("OPKG: Invalid rawdata given")
54         end
55
56         local data = {}
57         local c = {}
58         local l = nil
59
60         for line in rawdata do
61                 if line:sub(1, 1) ~= " " then
62                         local key, val = line:match("(.-): ?(.*)%s*")
63
64                         if key and val then
65                                 if key == "Package" then
66                                         c = {Package = val}
67                                         data[val] = c
68                                 elseif key == "Status" then
69                                         c.Status = {}
70                                         for j in val:gmatch("([^ ]+)") do
71                                                 c.Status[j] = true
72                                         end
73                                 else
74                                         c[key] = val
75                                 end
76                                 l = key
77                         end
78                 else
79                         -- Multi-line field
80                         c[l] = c[l] .. "\n" .. line
81                 end
82         end
83
84         return data
85 end
86
87 -- Internal lookup function
88 local function _lookup(act, pkg)
89         local cmd = ipkg .. " " .. act
90         if pkg then
91                 cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
92         end
93
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))
99
100         local data = _parselist(io.lines(tmpfile))
101         os.remove(tmpfile)
102         return data
103 end
104
105
106 --- Return information about installed and available packages.
107 -- @param pkg Limit output to a (set of) packages
108 -- @return Table containing package information
109 function info(pkg)
110         return _lookup("info", pkg)
111 end
112
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
116 function status(pkg)
117         return _lookup("status", pkg)
118 end
119
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", ...)
126 end
127
128 --- Determine whether a given package is installed.
129 -- @param pkg Package
130 -- @return Boolean
131 function installed(pkg)
132         local p = status(pkg)[pkg]
133         return (p and p.Status and p.Status.installed)
134 end
135
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
140 function remove(...)
141         return _action("remove", ...)
142 end
143
144 --- Update package lists.
145 -- @return Boolean indicating the status of the action
146 -- @return OPKG return code, STDOUT and STDERR
147 function update()
148         return _action("update")
149 end
150
151 --- Upgrades all installed packages.
152 -- @return Boolean indicating the status of the action
153 -- @return OPKG return code, STDOUT and STDERR
154 function upgrade()
155         return _action("upgrade")
156 end
157
158 -- List helper
159 function _list(action, pat, cb)
160         local fd = io.popen(ipkg .. " " .. action ..
161                 (pat and (" '%s'" % pat:gsub("'", "")) or ""))
162
163         if fd then
164                 local name, version, desc
165                 while true do
166                         local line = fd:read("*l")
167                         if not line then break end
168
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
183                 fd:close()
184         end
185 end
186
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
190 -- @return      nothing
191 function list_all(pat, cb)
192         _list("list", pat, cb)
193 end
194
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
198 -- @return      nothing
199 function list_installed(pat, cb)
200         _list("list_installed", pat, cb)
201 end
202
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
206 -- @return      nothing
207 function find(pat, cb)
208         _list("find", pat, cb)
209 end
210
211
212 --- Determines the overlay root used by opkg.
213 -- @return              String containing the directory path of the overlay root.
214 function overlay_root()
215         local od = "/"
216         local fd = io.open(icfg, "r")
217
218         if fd then
219                 local ln
220
221                 repeat
222                         ln = fd:read("*l")
223                         if ln and ln:match("^%s*option%s+overlay_root%s+") then
224                                 od = ln:match("^%s*option%s+overlay_root%s+(%S+)")
225
226                                 local s = fs.stat(od)
227                                 if not s or s.type ~= "dir" then
228                                         od = "/"
229                                 end
230
231                                 break
232                         end
233                 until not ln
234
235                 fd:close()
236         end
237
238         return od
239 end