luci-base: break circular luci.config <> luci.model.uci dependency
[project/luci.git] / modules / luci-base / luasrc / model / ipkg.lua
index 2e26bd7..e27ea52 100644 (file)
@@ -20,12 +20,14 @@ module "luci.model.ipkg"
 
 -- Internal action function
 local function _action(cmd, ...)
-       local pkg = ""
+       local cmdline = { ipkg, cmd }
+
+       local k, v
        for k, v in pairs({...}) do
-               pkg = pkg .. " '" .. v:gsub("'", "") .. "'"
+               cmdline[#cmdline+1] = util.shellquote(v)
        end
 
-       local c = "%s %s %s >/tmp/opkg.stdout 2>/tmp/opkg.stderr" %{ ipkg, cmd, pkg }
+       local c = "%s >/tmp/opkg.stdout 2>/tmp/opkg.stderr" % table.concat(cmdline, " ")
        local r = os.execute(c)
        local e = fs.readfile("/tmp/opkg.stderr")
        local o = fs.readfile("/tmp/opkg.stdout")
@@ -74,17 +76,17 @@ local function _parselist(rawdata)
 end
 
 -- Internal lookup function
-local function _lookup(act, pkg)
-       local cmd = ipkg .. " " .. act
+local function _lookup(cmd, pkg)
+       local cmdline = { ipkg, cmd }
        if pkg then
-               cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
+               cmdline[#cmdline+1] = util.shellquote(pkg)
        end
 
        -- OPKG sometimes kills the whole machine because it sucks
        -- 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))
+       os.execute("%s >%s 2>/dev/null" %{ table.concat(cmdline, " "), tmpfile })
 
        local data = _parselist(io.lines(tmpfile))
        os.remove(tmpfile)
@@ -123,26 +125,36 @@ end
 
 -- List helper
 local function _list(action, pat, cb)
-       local fd = io.popen(ipkg .. " " .. action ..
-               (pat and (" '%s'" % pat:gsub("'", "")) or ""))
+       local cmdline = { ipkg, action }
+       if pat then
+               cmdline[#cmdline+1] = util.shellquote(pat)
+       end
 
+       local fd = io.popen(table.concat(cmdline, " "))
        if fd then
-               local name, version, desc
+               local name, version, sz, desc
                while true do
                        local line = fd:read("*l")
                        if not line then break end
 
-                       name, version, desc = line:match("^(.-) %- (.-) %- (.+)")
+                       name, version, sz, desc = line:match("^(.-) %- (.-) %- (.-) %- (.+)")
 
                        if not name then
-                               name, version = line:match("^(.-) %- (.+)")
+                               name, version, sz = line:match("^(.-) %- (.-) %- (.+)")
                                desc = ""
                        end
 
-                       cb(name, version, desc)
+                       if name and version then
+                               if #version > 26 then
+                                       version = version:sub(1,21) .. ".." .. version:sub(-3,-1)
+                               end
+
+                               cb(name, version, sz, desc)
+                       end
 
                        name    = nil
                        version = nil
+                       sz      = nil
                        desc    = nil
                end
 
@@ -151,15 +163,15 @@ local function _list(action, pat, cb)
 end
 
 function list_all(pat, cb)
-       _list("list", pat, cb)
+       _list("list --size", pat, cb)
 end
 
 function list_installed(pat, cb)
-       _list("list_installed", pat, cb)
+       _list("list_installed --size", pat, cb)
 end
 
 function find(pat, cb)
-       _list("find", pat, cb)
+       _list("find --size", pat, cb)
 end
 
 
@@ -233,4 +245,3 @@ function compare_versions(ver1, comp, ver2)
        -- all equal and not compare greater or lower then true
        return not (comp == "<" or comp == ">")
 end
-