2015-01-16 22:46:42 +00:00
|
|
|
-- Copyright 2008-2011 Jo-Philipp Wich <jow@openwrt.org>
|
2015-01-16 22:38:38 +00:00
|
|
|
-- Copyright 2008 Steven Barth <steven@midlink.org>
|
|
|
|
-- Licensed to the public under the Apache License 2.0.
|
2008-04-03 08:56:06 +00:00
|
|
|
|
2008-08-29 15:47:56 +00:00
|
|
|
local os = require "os"
|
2008-11-12 19:05:03 +00:00
|
|
|
local io = require "io"
|
2010-03-07 21:51:45 +00:00
|
|
|
local fs = require "nixio.fs"
|
2008-08-29 15:47:56 +00:00
|
|
|
local util = require "luci.util"
|
2008-04-03 08:56:06 +00:00
|
|
|
|
2008-08-29 15:47:56 +00:00
|
|
|
local type = type
|
|
|
|
local pairs = pairs
|
|
|
|
local error = error
|
2010-02-28 23:38:22 +00:00
|
|
|
local table = table
|
2008-04-02 10:10:32 +00:00
|
|
|
|
2012-11-15 20:59:03 +00:00
|
|
|
local ipkg = "opkg --force-removal-of-dependent-packages --force-overwrite --nocase"
|
2010-03-07 21:51:45 +00:00
|
|
|
local icfg = "/etc/opkg.conf"
|
2008-04-03 08:56:06 +00:00
|
|
|
|
2008-08-29 15:47:56 +00:00
|
|
|
module "luci.model.ipkg"
|
2008-04-03 08:56:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- Internal action function
|
2008-08-29 15:47:56 +00:00
|
|
|
local function _action(cmd, ...)
|
2008-04-03 08:56:06 +00:00
|
|
|
local pkg = ""
|
2010-02-28 23:38:22 +00:00
|
|
|
for k, v in pairs({...}) do
|
2008-04-03 08:56:06 +00:00
|
|
|
pkg = pkg .. " '" .. v:gsub("'", "") .. "'"
|
2008-04-02 10:10:32 +00:00
|
|
|
end
|
2009-02-17 01:37:18 +00:00
|
|
|
|
2011-06-23 10:12:03 +00:00
|
|
|
local c = "%s %s %s >/tmp/opkg.stdout 2>/tmp/opkg.stderr" %{ ipkg, cmd, pkg }
|
2008-04-02 10:10:32 +00:00
|
|
|
local r = os.execute(c)
|
2011-06-23 10:12:03 +00:00
|
|
|
local e = fs.readfile("/tmp/opkg.stderr")
|
|
|
|
local o = fs.readfile("/tmp/opkg.stdout")
|
|
|
|
|
|
|
|
fs.unlink("/tmp/opkg.stderr")
|
|
|
|
fs.unlink("/tmp/opkg.stdout")
|
|
|
|
|
|
|
|
return r, o or "", e or ""
|
2008-04-02 10:10:32 +00:00
|
|
|
end
|
|
|
|
|
2008-04-03 08:56:06 +00:00
|
|
|
-- Internal parser function
|
2009-02-17 01:37:18 +00:00
|
|
|
local function _parselist(rawdata)
|
2008-10-08 23:35:54 +00:00
|
|
|
if type(rawdata) ~= "function" then
|
2011-06-23 10:12:03 +00:00
|
|
|
error("OPKG: Invalid rawdata given")
|
2008-04-02 10:10:32 +00:00
|
|
|
end
|
2009-02-17 01:37:18 +00:00
|
|
|
|
2008-04-02 10:10:32 +00:00
|
|
|
local data = {}
|
|
|
|
local c = {}
|
|
|
|
local l = nil
|
2009-02-17 01:37:18 +00:00
|
|
|
|
2008-10-08 23:35:54 +00:00
|
|
|
for line in rawdata do
|
2008-04-02 10:10:32 +00:00
|
|
|
if line:sub(1, 1) ~= " " then
|
2008-10-08 23:35:54 +00:00
|
|
|
local key, val = line:match("(.-): ?(.*)%s*")
|
2009-02-17 01:37:18 +00:00
|
|
|
|
2008-04-02 10:10:32 +00:00
|
|
|
if key and val then
|
|
|
|
if key == "Package" then
|
|
|
|
c = {Package = val}
|
|
|
|
data[val] = c
|
|
|
|
elseif key == "Status" then
|
|
|
|
c.Status = {}
|
2008-10-08 23:35:54 +00:00
|
|
|
for j in val:gmatch("([^ ]+)") do
|
2008-04-02 10:10:32 +00:00
|
|
|
c.Status[j] = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
c[key] = val
|
|
|
|
end
|
|
|
|
l = key
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- Multi-line field
|
2008-10-08 23:35:54 +00:00
|
|
|
c[l] = c[l] .. "\n" .. line
|
2008-04-02 10:10:32 +00:00
|
|
|
end
|
|
|
|
end
|
2009-02-17 01:37:18 +00:00
|
|
|
|
2008-04-02 10:10:32 +00:00
|
|
|
return data
|
2008-08-29 15:47:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Internal lookup function
|
|
|
|
local function _lookup(act, pkg)
|
|
|
|
local cmd = ipkg .. " " .. act
|
|
|
|
if pkg then
|
|
|
|
cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
|
|
|
|
end
|
2009-02-17 01:37:18 +00:00
|
|
|
|
2011-06-23 10:12:03 +00:00
|
|
|
-- OPKG sometimes kills the whole machine because it sucks
|
2008-11-12 19:05:03 +00:00
|
|
|
-- Therefore we have to use a sucky approach too and use
|
|
|
|
-- tmpfiles instead of directly reading the output
|
|
|
|
local tmpfile = os.tmpname()
|
|
|
|
os.execute(cmd .. (" >%s 2>/dev/null" % tmpfile))
|
|
|
|
|
|
|
|
local data = _parselist(io.lines(tmpfile))
|
|
|
|
os.remove(tmpfile)
|
|
|
|
return data
|
2008-08-29 15:47:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function info(pkg)
|
|
|
|
return _lookup("info", pkg)
|
|
|
|
end
|
|
|
|
|
|
|
|
function status(pkg)
|
|
|
|
return _lookup("status", pkg)
|
|
|
|
end
|
|
|
|
|
|
|
|
function install(...)
|
|
|
|
return _action("install", ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
function installed(pkg)
|
|
|
|
local p = status(pkg)[pkg]
|
|
|
|
return (p and p.Status and p.Status.installed)
|
|
|
|
end
|
|
|
|
|
|
|
|
function remove(...)
|
|
|
|
return _action("remove", ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
function update()
|
|
|
|
return _action("update")
|
|
|
|
end
|
|
|
|
|
|
|
|
function upgrade()
|
|
|
|
return _action("upgrade")
|
|
|
|
end
|
2010-02-28 23:38:22 +00:00
|
|
|
|
|
|
|
-- List helper
|
|
|
|
function _list(action, pat, cb)
|
2011-10-15 03:10:19 +00:00
|
|
|
local fd = io.popen(ipkg .. " " .. action ..
|
2012-11-15 20:59:03 +00:00
|
|
|
(pat and (" '%s'" % pat:gsub("'", "")) or ""))
|
2011-10-15 03:10:19 +00:00
|
|
|
|
2010-02-28 23:38:22 +00:00
|
|
|
if fd then
|
|
|
|
local name, version, desc
|
|
|
|
while true do
|
|
|
|
local line = fd:read("*l")
|
|
|
|
if not line then break end
|
|
|
|
|
2012-11-15 20:59:03 +00:00
|
|
|
name, version, desc = line:match("^(.-) %- (.-) %- (.+)")
|
2010-02-28 23:38:22 +00:00
|
|
|
|
2012-11-15 20:59:03 +00:00
|
|
|
if not name then
|
|
|
|
name, version = line:match("^(.-) %- (.+)")
|
|
|
|
desc = ""
|
|
|
|
end
|
2010-02-28 23:38:22 +00:00
|
|
|
|
2012-11-15 20:59:03 +00:00
|
|
|
cb(name, version, desc)
|
2010-02-28 23:38:22 +00:00
|
|
|
|
2012-11-15 20:59:03 +00:00
|
|
|
name = nil
|
|
|
|
version = nil
|
|
|
|
desc = nil
|
2010-02-28 23:38:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
fd:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function list_all(pat, cb)
|
|
|
|
_list("list", pat, cb)
|
|
|
|
end
|
|
|
|
|
|
|
|
function list_installed(pat, cb)
|
|
|
|
_list("list_installed", pat, cb)
|
|
|
|
end
|
2010-03-07 21:51:45 +00:00
|
|
|
|
2012-11-15 20:59:03 +00:00
|
|
|
function find(pat, cb)
|
|
|
|
_list("find", pat, cb)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2010-03-07 21:51:45 +00:00
|
|
|
function overlay_root()
|
|
|
|
local od = "/"
|
|
|
|
local fd = io.open(icfg, "r")
|
|
|
|
|
|
|
|
if fd then
|
|
|
|
local ln
|
|
|
|
|
|
|
|
repeat
|
|
|
|
ln = fd:read("*l")
|
|
|
|
if ln and ln:match("^%s*option%s+overlay_root%s+") then
|
|
|
|
od = ln:match("^%s*option%s+overlay_root%s+(%S+)")
|
|
|
|
|
|
|
|
local s = fs.stat(od)
|
|
|
|
if not s or s.type ~= "dir" then
|
|
|
|
od = "/"
|
|
|
|
end
|
|
|
|
|
|
|
|
break
|
|
|
|
end
|
|
|
|
until not ln
|
|
|
|
|
|
|
|
fd:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
return od
|
|
|
|
end
|