9abd4ed39a0ce2c39f8d3913e6a717c072db36b0
[project/luci.git] / libs / ipkg / luasrc / model / ipkg.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
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 --autoremove"
27 local icfg = "/etc/opkg.conf"
28
29 --- LuCI IPKG/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 = ipkg.." "..cmd.." "..pkg.." >/dev/null 2>&1"
41         local r = os.execute(c)
42         return (r == 0), r
43 end
44
45 -- Internal parser function
46 local function _parselist(rawdata)
47         if type(rawdata) ~= "function" then
48                 error("IPKG: Invalid rawdata given")
49         end
50
51         local data = {}
52         local c = {}
53         local l = nil
54
55         for line in rawdata do
56                 if line:sub(1, 1) ~= " " then
57                         local key, val = line:match("(.-): ?(.*)%s*")
58
59                         if key and val then
60                                 if key == "Package" then
61                                         c = {Package = val}
62                                         data[val] = c
63                                 elseif key == "Status" then
64                                         c.Status = {}
65                                         for j in val:gmatch("([^ ]+)") do
66                                                 c.Status[j] = true
67                                         end
68                                 else
69                                         c[key] = val
70                                 end
71                                 l = key
72                         end
73                 else
74                         -- Multi-line field
75                         c[l] = c[l] .. "\n" .. line
76                 end
77         end
78
79         return data
80 end
81
82 -- Internal lookup function
83 local function _lookup(act, pkg)
84         local cmd = ipkg .. " " .. act
85         if pkg then
86                 cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
87         end
88
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))
94
95         local data = _parselist(io.lines(tmpfile))
96         os.remove(tmpfile)
97         return data
98 end
99
100
101 --- Return information about installed and available packages.
102 -- @param pkg Limit output to a (set of) packages
103 -- @return Table containing package information
104 function info(pkg)
105         return _lookup("info", pkg)
106 end
107
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
111 function status(pkg)
112         return _lookup("status", pkg)
113 end
114
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", ...)
121 end
122
123 --- Determine whether a given package is installed.
124 -- @param pkg Package
125 -- @return Boolean
126 function installed(pkg)
127         local p = status(pkg)[pkg]
128         return (p and p.Status and p.Status.installed)
129 end
130
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
135 function remove(...)
136         return _action("remove", ...)
137 end
138
139 --- Update package lists.
140 -- @return Boolean indicating the status of the action
141 -- @return IPKG return code
142 function update()
143         return _action("update")
144 end
145
146 --- Upgrades all installed packages.
147 -- @return Boolean indicating the status of the action
148 -- @return IPKG return code
149 function upgrade()
150         return _action("upgrade")
151 end
152
153 -- List helper
154 function _list(action, pat, cb)
155         local fd = io.popen(ipkg .. " " .. action .. (pat and " '*" .. pat:gsub("'", "") .. "*'" or ""))
156         if fd then
157                 local name, version, desc
158                 while true do
159                         local line = fd:read("*l")
160                         if not line then break end
161
162                         if line:sub(1,1) ~= " " then
163                                 name, version, desc = line:match("^(.-) %- (.-) %- (.+)")
164
165                                 if not name then
166                                         name, version = line:match("^(.-) %- (.+)")
167                                         desc = ""
168                                 end
169
170                                 cb(name, version, desc)
171
172                                 name    = nil
173                                 version = nil
174                                 desc    = nil
175                         end
176                 end
177
178                 fd:close()
179         end
180 end
181
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
185 -- @return      nothing
186 function list_all(pat, cb)
187         _list("list", pat, cb)
188 end
189
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
193 -- @return      nothing
194 function list_installed(pat, cb)
195         _list("list_installed", pat, cb)
196 end
197
198 --- Determines the overlay root used by opkg.
199 -- @return              String containing the directory path of the overlay root.
200 function overlay_root()
201         local od = "/"
202         local fd = io.open(icfg, "r")
203
204         if fd then
205                 local ln
206
207                 repeat
208                         ln = fd:read("*l")
209                         if ln and ln:match("^%s*option%s+overlay_root%s+") then
210                                 od = ln:match("^%s*option%s+overlay_root%s+(%S+)")
211
212                                 local s = fs.stat(od)
213                                 if not s or s.type ~= "dir" then
214                                         od = "/"
215                                 end
216
217                                 break
218                         end
219                 until not ln
220
221                 fd:close()
222         end
223
224         return od
225 end
226