summaryrefslogtreecommitdiffstats
path: root/apk-model.lua
blob: 57ed34171794d6c13d86b3cca26af07fe0f1962c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
-- acf model for packages (apk)
module (..., package.seeall)
require("modelfunctions")
require("posix")
fs = require("acf.fs")
format = require("acf.format")

local configfile = "/etc/apk/repositories"
local worldfile = "/etc/apk/world"
local cachelink = "/etc/apk/cache"
local lbuconffile = "/etc/lbu/lbu.conf"

local path = "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin "

local repo = nil
local install_cache = false
local upgrade_cache = false
local toplevel

-- ################################################################################
-- LOCAL FUNCTIONS

local function gettoplevel()
	if not toplevel then
		toplevel = {}
		local top = format.string_to_table(fs.read_file(worldfile) or "", "%s+")
		for i,name in ipairs(top) do
			toplevel[name] = i
		end
	end
	return toplevel
end

local reload_upgrades = function()
	if repo then
		-- clear out upgrade info
		for name,value in pairs(repo) do
			if value then
				value.upgrade = nil
			end
		end
		-- read in which need upgrades
		local f = modelfunctions.run_executable({"apk", "version", "-l", "<"})
		for line in string.gmatch(f, "[^\n]+") do
			local name = string.match(line, "(%S+)%-%d")
			if repo[name] then
				repo[name].upgrade = true
			end
		end
		upgrade_cache = true
	end
	return repo
end

local reload_installed = function()
	if repo then
		-- clear out installed info
		for name,value in pairs(repo) do
			if value then
				value.installed = nil
				value.comment = nil
			end
		end
		-- read in which are installed
		local f = modelfunctions.run_executable({"apk", "info", "-vv"})
		for line in string.gmatch(f, "[^\n]+") do
			local name, ver, comment = string.match(line, "(%S+)%-(%d+%S*)%s+%-%s+(.*)")
			if not repo[name] then
				repo[name] = {}
			end
			repo[name].installed = ver
			repo[name].comment = comment
		end
		install_cache = true
	end
	return repo
end

local repository = function()
	if not repo then
		-- read in all of the packages
		local f,errtxt = modelfunctions.run_executable({"apk", "search"})
		repo = {}
		install_cache = false
		upgrade_cache = false
		for line in string.gmatch(f, "[^\n]+") do
			local name, ver = string.match(line, "(.*)%-(%d+.*)")
			if name and (not repo[name] or repo[name].version < ver) then
				repo[name] = {}
				repo[name].version = ver
			end
		end
	end
	if not install_cache then
		reload_installed()
	end
	if not upgrade_cache then
		reload_upgrades()
	end
	return repo
end

-- Find all the packages that this package depends on (using recursion)
local find_dependents
find_dependents = function(package)
	repo = repo or repository()
	if not repo[package] then
		return {}
	end
	if not repo[package].dependents then
		repo[package].dependents = {}
		local f = modelfunctions.run_executable({"apk", "info", "-R", package})
		for line in string.gmatch(f, "[^\n]+") do
			if not line:find("depends on:") and not line:find("^%s*$") then
				table.insert(repo[package].dependents, line)
				for i,dep in ipairs(find_dependents(line, saved, output)) do
					table.insert(repo[package].dependents, dep)
				end
			end
		end
	end
	return repo[package].dependents
end

local function upgrade_available(package)
	local retval = false
	repo = repo or repository()
	if repo[package] and repo[package].upgrade then
		retval = true
	else -- check the dependents
		for i,dep in ipairs(find_dependents(package)) do
			if repo[dep] and repo[dep].upgrade then
				retval = true
				break
			end
		end
	end
	return retval
end

local resetpermissions = function(self)
	if self.sessiondata then self.sessiondata.menu = nil end
	if self.sessiondata then self.sessiondata.permissions = nil end
end

-- ################################################################################
-- PUBLIC FUNCTIONS

get_loaded_packages = function()
	repo = repository()
	toplevel = gettoplevel()

	-- read in the loaded packages
	local top = cfe({ type="list", value={}, label="Top Level Packages"})
	local depend = cfe({ type="list", value={}, label="Dependent Packages"})
	for name,value in pairs(repo) do
		if value.installed then
			local temp = {}
			temp.name = name
			temp.version = value.installed
			temp.description = value.comment
	                temp.upgrade = value.upgrade
			if toplevel[name] then
				top.value[#top.value+1] = temp
			else
				depend.value[#depend.value+1] = temp
			end
		end
	end
	table.sort(top.value, function(a,b) return (a.name < b.name) end)
	table.sort(depend.value, function(a,b) return (a.name < b.name) end)
	return cfe({ type="group", value={toplevel=top, dependent=depend}, label="Installed Packages" })
end

get_available_packages = function()
	repo = repository()
	-- available are all except same version installed
	local available = cfe({ type="list", value={}, label="Available Packages" })
	for name,value in pairs(repo) do
		if value.version and (not value.installed or value.upgrade) then
			local temp = {}
			temp.name = name
			temp.version = value.version
			temp.upgrade = value.upgrade
			available.value[#available.value + 1] = temp
		end
	end
	table.sort(available.value, function(a,b) return (a.name < b.name) end)
	return available
end

get_delete_package = function(self, clientdata)
	local result = {}
	result.package = cfe({ value=clientdata.package, label="Package" })

	return cfe({ type="group", value=result, label="Result of Delete" })
end

delete_package = function(self, deleterequest)
	deleterequest.descr, deleterequest.errtxt = modelfunctions.run_executable({"apk", "del", deleterequest.value.package.value}, true)
	if deleterequest.errtxt == "" then
		deleterequest.errtxt = "Failed to delete package."
	end
	-- Destroy menu and permissions info in session so recalculated
	resetpermissions(self)

	return deleterequest
end

get_install_package = function(self, clientdata)
	local result = {}
	result.package = cfe({ value=clientdata.package, label="Package" })

	return cfe({ type="group", value=result, label="Result of Install" })
end

install_package = function(self, installrequest)
	installrequest.descr, installrequest.errtxt = modelfunctions.run_executable({"apk", "add", installrequest.value.package.value}, true)
	if not installrequest.errtxt then
		-- Destroy menu and permissions info in session so recalculated
		resetpermissions(self)
	end

	return installrequest 
end

get_upgrade_package = function(self, clientdata)
	local result = {}
	result.package = cfe({ value=clientdata.package, label="Package" })

	return cfe({ type="group", value=result, label="Result of Upgrade" })
end

upgrade_package = function(self, upgraderequest)
	upgraderequest.descr, upgraderequest.errtxt = modelfunctions.run_executable({"apk", "fix", "-u", upgraderequest.value.package.value}, true)
	if upgraderequest.errtxt == "" then
		upgraderequest.errtxt = "Failed to upgrade package."
	end
	-- Destroy menu and permissions info in session so recalculated
	resetpermissions(self)

	return upgraderequest
end

get_update_all = function(self, clientdata)
	local result = {}

	return cfe({ type="group", value=result, label="Result of Update" })
end

update_all = function(self, updaterequest)
	updaterequest.descr, updaterequest.errtxt = modelfunctions.run_executable({"apk", "update"}, true)
	if updaterequest.errtxt == "" then
		updaterequest.errtxt = "Failed to update index."
	end

	return updaterequest
end

get_upgrade_all = function(self, clientdata)
	local result = {}

	return cfe({ type="group", value=result, label="Result of Upgrade" })
end

upgrade_all = function(self, upgraderequest)
	upgraderequest.descr, upgraderequest.errtxt = modelfunctions.run_executable({"apk", "upgrade", "-U"}, true)
	if upgraderequest.errtxt == "" then
		upgraderequest.errtxt = "Failed to upgrade packages."
	end
	-- Destroy menu and permissions info in session so recalculated
	resetpermissions(self)

	return upgraderequest
end

get_cache = function()
	local cache = {}
	cache.enable = cfe({ type="boolean", value=false, label="Enable Cache" })
	cache.directory = cfe({ label="Cache Directory" })
	local link = posix.stat(cachelink, "type")
	if link == "link" then
		cache.enable.value = true
		cache.directory.value = posix.readlink(cachelink)
	else
		if link then
			cache.enable.errtxt = cachelink.." exists but is not a link"
		end
		local lbu_media = format.parse_ini_file(fs.read_file(lbuconffile), "", "LBU_MEDIA")
		if lbu_media then
			cache.directory.value = "/media/"..lbu_media.."/cache"
		end
	end
	return cfe({ type="group", value=cache, label="Cache Settings" })
end

update_cache = function(self, cache)
	cache.value.enable.errtxt = nil
	if not cache.value.enable.value then
		os.remove(cachelink)
	else
		local success = false
		cache.errtxt = "Failed to set cache"
		if cache.value.directory.value == "" then
			cache.value.directory.errtxt = "Directory must be defined"
		else
			local dir = posix.stat(cache.value.directory.value, "type")
			if dir and dir ~= "directory" then
				cache.value.directory.errtxt = "Path exists but is not a directory"
			elseif not dir then
				success = fs.create_directory(cache.value.directory.value)
				if not success then
					cache.value.directory.errtxt = "Failed to create directory"
				end
			else
				success = true
			end
		end
		if success then
			os.remove(cachelink)
			posix.link(cache.value.directory.value, cachelink, true)
			cache.errtxt = nil
		end
	end
	return cache
end

get_configfile = function()
	return modelfunctions.getfiledetails(configfile)
end

update_configfile = function(self, newconfig)
	return modelfunctions.setfiledetails(self, newconfig, {configfile})
end

get_package_details = function(package)
	repo = repo or repository()
	local details = {}
	details.package = cfe({ value=package, label="Package" })
	details.version = cfe({ label="Available Version" })
	details.installed = cfe({ label="Installed Version" })
	details.comment = cfe({ label="Description" })
	details.webpage = cfe({ label="Web Page" })
	details.size = cfe({ label="Size" })
	details.upgrade = cfe({ label="Upgrade Details" })
	if not repo[package] then
		details.package.errtxt = "Invalid package"
	else
		details.version.value = repo[package].version
		if repo[package].installed then
			details.installed.value = repo[package].installed
			details.comment.value = repo[package].comment
			local cmdresult = format.string_to_table((modelfunctions.run_executable({"apk", "info", "-ws", package})), "\n")
			details.webpage.value = cmdresult[2] or ""
			details.size.value = cmdresult[5] or ""
			local dependents = find_dependents(package)
			table.insert(dependents, 1, package)
			local revdeps = {}
			details.upgrade.value = {}
			for i,val in ipairs(dependents) do
				if not revdeps[val] then
					revdeps[val] = true
					if repo[val] and repo[val].upgrade then
						table.insert(details.upgrade.value, val.." "..repo[val].installed.." -> "..repo[val].version)
					end
				end
			end
			details.upgrade.value = table.concat(details.upgrade.value, "\n")
		end
	end
	return cfe({ type="group", value=details, label="Package Details" })
end