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.
5 local os = require "os"
6 local io = require "io"
7 local fs = require "nixio.fs"
8 local util = require "luci.util"
15 local ipkg = "opkg --force-removal-of-dependent-packages --force-overwrite --nocase"
16 local icfg = "/etc/opkg.conf"
18 module "luci.model.ipkg"
21 -- Internal action function
22 local function _action(cmd, ...)
23 local cmdline = { ipkg, cmd }
26 for k, v in pairs({...}) do
27 cmdline[#cmdline+1] = util.shellquote(v)
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")
35 fs.unlink("/tmp/opkg.stderr")
36 fs.unlink("/tmp/opkg.stdout")
38 return r, o or "", e or ""
41 -- Internal parser function
42 local function _parselist(rawdata)
43 if type(rawdata) ~= "function" then
44 error("OPKG: Invalid rawdata given")
51 for line in rawdata do
52 if line:sub(1, 1) ~= " " then
53 local key, val = line:match("(.-): ?(.*)%s*")
56 if key == "Package" then
59 elseif key == "Status" then
61 for j in val:gmatch("([^ ]+)") do
71 c[l] = c[l] .. "\n" .. line
78 -- Internal lookup function
79 local function _lookup(cmd, pkg)
80 local cmdline = { ipkg, cmd }
82 cmdline[#cmdline+1] = util.shellquote(pkg)
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 })
91 local data = _parselist(io.lines(tmpfile))
98 return _lookup("info", pkg)
102 return _lookup("status", pkg)
105 function install(...)
106 return _action("install", ...)
109 function installed(pkg)
110 local p = status(pkg)[pkg]
111 return (p and p.Status and p.Status.installed)
115 return _action("remove", ...)
119 return _action("update")
123 return _action("upgrade")
127 local function _list(action, pat, cb)
128 local cmdline = { ipkg, action }
130 cmdline[#cmdline+1] = util.shellquote(pat)
133 local fd = io.popen(table.concat(cmdline, " "))
135 local name, version, sz, desc
137 local line = fd:read("*l")
138 if not line then break end
140 name, version, sz, desc = line:match("^(.-) %- (.-) %- (.-) %- (.+)")
143 name, version, sz = line:match("^(.-) %- (.-) %- (.+)")
147 if name and version then
148 if #version > 26 then
149 version = version:sub(1,21) .. ".." .. version:sub(-3,-1)
152 cb(name, version, sz, desc)
165 function list_all(pat, cb)
166 _list("list --size", pat, cb)
169 function list_installed(pat, cb)
170 _list("list_installed --size", pat, cb)
173 function find(pat, cb)
174 _list("find --size", pat, cb)
178 function overlay_root()
180 local fd = io.open(icfg, "r")
187 if ln and ln:match("^%s*option%s+overlay_root%s+") then
188 od = ln:match("^%s*option%s+overlay_root%s+(%S+)")
190 local s = fs.stat(od)
191 if not s or s.type ~= "dir" then
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")
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 = ">"
219 error("Invalid compare string")
223 local av1 = util.split(ver1, "[%.%-]", nil, true)
224 local av2 = util.split(ver2, "[%.%-]", nil, true)
226 local max = table.getn(av1)
227 if (table.getn(av1) < table.getn(av2)) then
228 max = table.getn(av2)
232 local s1 = av1[i] or ""
233 local s2 = av2[i] or ""
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
245 -- all equal and not compare greater or lower then true
246 return not (comp == "<" or comp == ">")