Merge pull request #509 from neheb/master
[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 module "luci.model.ipkg"
19
20
21 -- Internal action function
22 local function _action(cmd, ...)
23         local pkg = ""
24         for k, v in pairs({...}) do
25                 pkg = pkg .. " '" .. v:gsub("'", "") .. "'"
26         end
27
28         local c = "%s %s %s >/tmp/opkg.stdout 2>/tmp/opkg.stderr" %{ ipkg, cmd, pkg }
29         local r = os.execute(c)
30         local e = fs.readfile("/tmp/opkg.stderr")
31         local o = fs.readfile("/tmp/opkg.stdout")
32
33         fs.unlink("/tmp/opkg.stderr")
34         fs.unlink("/tmp/opkg.stdout")
35
36         return r, o or "", e or ""
37 end
38
39 -- Internal parser function
40 local function _parselist(rawdata)
41         if type(rawdata) ~= "function" then
42                 error("OPKG: Invalid rawdata given")
43         end
44
45         local data = {}
46         local c = {}
47         local l = nil
48
49         for line in rawdata do
50                 if line:sub(1, 1) ~= " " then
51                         local key, val = line:match("(.-): ?(.*)%s*")
52
53                         if key and val then
54                                 if key == "Package" then
55                                         c = {Package = val}
56                                         data[val] = c
57                                 elseif key == "Status" then
58                                         c.Status = {}
59                                         for j in val:gmatch("([^ ]+)") do
60                                                 c.Status[j] = true
61                                         end
62                                 else
63                                         c[key] = val
64                                 end
65                                 l = key
66                         end
67                 else
68                         -- Multi-line field
69                         c[l] = c[l] .. "\n" .. line
70                 end
71         end
72
73         return data
74 end
75
76 -- Internal lookup function
77 local function _lookup(act, pkg)
78         local cmd = ipkg .. " " .. act
79         if pkg then
80                 cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
81         end
82
83         -- OPKG sometimes kills the whole machine because it sucks
84         -- Therefore we have to use a sucky approach too and use
85         -- tmpfiles instead of directly reading the output
86         local tmpfile = os.tmpname()
87         os.execute(cmd .. (" >%s 2>/dev/null" % tmpfile))
88
89         local data = _parselist(io.lines(tmpfile))
90         os.remove(tmpfile)
91         return data
92 end
93
94
95 function info(pkg)
96         return _lookup("info", pkg)
97 end
98
99 function status(pkg)
100         return _lookup("status", pkg)
101 end
102
103 function install(...)
104         return _action("install", ...)
105 end
106
107 function installed(pkg)
108         local p = status(pkg)[pkg]
109         return (p and p.Status and p.Status.installed)
110 end
111
112 function remove(...)
113         return _action("remove", ...)
114 end
115
116 function update()
117         return _action("update")
118 end
119
120 function upgrade()
121         return _action("upgrade")
122 end
123
124 -- List helper
125 local function _list(action, pat, cb)
126         local fd = io.popen(ipkg .. " " .. action ..
127                 (pat and (" '%s'" % pat:gsub("'", "")) or ""))
128
129         if fd then
130                 local name, version, sz, desc
131                 while true do
132                         local line = fd:read("*l")
133                         if not line then break end
134
135                         name, version, sz, desc = line:match("^(.-) %- (.-) %- (.-) %- (.+)")
136
137                         if not name then
138                                 name, version, sz = line:match("^(.-) %- (.-) %- (.+)")
139                                 desc = ""
140                         end
141
142                         if name and version then
143                                 if #version > 26 then
144                                         version = version:sub(1,21) .. ".." .. version:sub(-3,-1)
145                                 end
146
147                                 cb(name, version, sz, desc)
148                         end
149
150                         name    = nil
151                         version = nil
152                         sz      = nil
153                         desc    = nil
154                 end
155
156                 fd:close()
157         end
158 end
159
160 function list_all(pat, cb)
161         _list("list --size", pat, cb)
162 end
163
164 function list_installed(pat, cb)
165         _list("list_installed --size", pat, cb)
166 end
167
168 function find(pat, cb)
169         _list("find --size", pat, cb)
170 end
171
172
173 function overlay_root()
174         local od = "/"
175         local fd = io.open(icfg, "r")
176
177         if fd then
178                 local ln
179
180                 repeat
181                         ln = fd:read("*l")
182                         if ln and ln:match("^%s*option%s+overlay_root%s+") then
183                                 od = ln:match("^%s*option%s+overlay_root%s+(%S+)")
184
185                                 local s = fs.stat(od)
186                                 if not s or s.type ~= "dir" then
187                                         od = "/"
188                                 end
189
190                                 break
191                         end
192                 until not ln
193
194                 fd:close()
195         end
196
197         return od
198 end
199
200 function compare_versions(ver1, comp, ver2)
201         if not ver1 or not ver2
202         or not comp or not (#comp > 0) then
203                 error("Invalid parameters")
204                 return nil
205         end
206         -- correct compare string
207         if comp == "<>" or comp == "><" or comp == "!=" or comp == "~=" then comp = "~="
208         elseif comp == "<=" or comp == "<" or comp == "=<" then comp = "<="
209         elseif comp == ">=" or comp == ">" or comp == "=>" then comp = ">="
210         elseif comp == "="  or comp == "==" then comp = "=="
211         elseif comp == "<<" then comp = "<"
212         elseif comp == ">>" then comp = ">"
213         else
214                 error("Invalid compare string")
215                 return nil
216         end
217
218         local av1 = util.split(ver1, "[%.%-]", nil, true)
219         local av2 = util.split(ver2, "[%.%-]", nil, true)
220
221         local max = table.getn(av1)
222         if (table.getn(av1) < table.getn(av2)) then
223                 max = table.getn(av2)
224         end
225
226         for i = 1, max, 1  do
227                 local s1 = av1[i] or ""
228                 local s2 = av2[i] or ""
229
230                 -- first "not equal" found return true
231                 if comp == "~=" and (s1 ~= s2) then return true end
232                 -- first "lower" found return true
233                 if (comp == "<" or comp == "<=") and (s1 < s2) then return true end
234                 -- first "greater" found return true
235                 if (comp == ">" or comp == ">=") and (s1 > s2) then return true end
236                 -- not equal then return false
237                 if (s1 ~= s2) then return false end
238         end
239
240         -- all equal and not compare greater or lower then true
241         return not (comp == "<" or comp == ">")
242 end