Merge pull request #302 from chris5560/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 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, desc
131                 while true do
132                         local line = fd:read("*l")
133                         if not line then break end
134
135                         name, version, desc = line:match("^(.-) %- (.-) %- (.+)")
136
137                         if not name then
138                                 name, version = line:match("^(.-) %- (.+)")
139                                 desc = ""
140                         end
141
142                         cb(name, version, desc)
143
144                         name    = nil
145                         version = nil
146                         desc    = nil
147                 end
148
149                 fd:close()
150         end
151 end
152
153 function list_all(pat, cb)
154         _list("list", pat, cb)
155 end
156
157 function list_installed(pat, cb)
158         _list("list_installed", pat, cb)
159 end
160
161 function find(pat, cb)
162         _list("find", pat, cb)
163 end
164
165
166 function overlay_root()
167         local od = "/"
168         local fd = io.open(icfg, "r")
169
170         if fd then
171                 local ln
172
173                 repeat
174                         ln = fd:read("*l")
175                         if ln and ln:match("^%s*option%s+overlay_root%s+") then
176                                 od = ln:match("^%s*option%s+overlay_root%s+(%S+)")
177
178                                 local s = fs.stat(od)
179                                 if not s or s.type ~= "dir" then
180                                         od = "/"
181                                 end
182
183                                 break
184                         end
185                 until not ln
186
187                 fd:close()
188         end
189
190         return od
191 end