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