summaryrefslogtreecommitdiffstats
path: root/logfiles-model.lua
blob: f00f7f4aab0702b9c95812727e66fb4827b8913d (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
-- acf model for displaying logfiles recusivly 
module (..., package.seeall)
require("fs")

-- no initializer in model - use controller.init for that

-- Function to get detailed information on a specific file.
local function file_info ( path )
	-- Check if file is in use
	local st = fs.stat(path)
	local size = st.size
	local lastmod = st.mtime
	local file_inuse = io.popen("fuser " .. path )
	local fileinuseresult = file_inuse:read("*a") or "unkown"
	file_inuse:close()
	if fileinuseresult == "" then 
		fileinuseresult = "Delete"
		fileinuseurl	= "/delete?name=" .. path
	else
		fileinuseresult = "in use"
		fileinuseurl	= nil
	end
	return lastmod,size,fileinuseresult,fileinuseurl
end

-- Function to recursively inserts all filenames in a dir into an array
local function recursedir(path, filearray)
	local k,v
	for k,v in pairs(posix.dir(path) or {}) do
		-- Ignore files that begins with a '.'
		if not string.match(v, "^%.") then
			local f = path .. "/" .. v
			-- If subfolder exists, list files in this subfolder
			if (posix.stat(f).type == "directory") then
				recursedir(f, filearray)
			else
				table.insert(filearray, f)
			end
		end
	end
end

-- Function to list available files for view/delete
local function list_files ( ... )
	local listed_files = {}
	local open_files = {}
	local files = {}
	local k,v
	-- Generate a singe table with all the files
	for k,v in pairs{...} do
		recursedir(v, files)
	end
	-- Loop through each file and present its info
	for k,v in pairs(files) do
		-- Get info on this specific file and put it in a table
		local lastmod,size,fileinuseresult,fileinuseurl = file_info(v)
		table.insert ( listed_files , cfe{inuse=fileinuseresult, del=fileinuseurl, save="/download?name=" .. v, 
			view="/view?name=" .. v, size=size, lastmod=lastmod, name=v, type="", id=k} )
	end
	table.sort(listed_files, function (a,b) return (a.name < b.name) end )
	return listed_files
end

-- Function to check if a file is deletable, and if it is, then delete it.
local function checkfilefordelete ( filetodelete )
	local deletedfile = {}
	deletedfile = {value=nil, type="text", option=nil, errtxt="File '" .. filetodelete .. "' has not been deleted!"}
	-- Get a list of files that could be deleted
	local available_files = get()
	for k,v in pairs(available_files) do
		if ( available_files[k].name == filetodelete ) then
			-- Check if file is deletable (or in use)
			if ( available_files[k].del ) then
				local status, err = os.remove( filetodelete )
				if not ( err ) then
					deletedfile = {value="File '" .. filetodelete .. "' has been successfully deleted!", type="text"}
				else
					deletedfile = {value=nil, type="text", option=nil, errtxt=err}
				end
			else
			end
		end
	end
	return deletedfile
end

local function checkfileforview ( path )
	local filecontent = nil
	local available_files = get()
	for k,v in pairs(available_files) do
		if ( available_files[k].name == path ) then
			filecontent = path
		end
	end
	return filecontent
end

function get_filedetails(self,path)
	local filedetails = {}
	if (checkfileforview(path)) then
		filedetails.details = fs.stat(path)
		filedetails.value = fs.read_file(path)
		filedetails.name = basename(path)
	end
	return filedetails
end


get = function (self)
	-- These folders (and their subfolers) are going to be listed
	return list_files( "/var/log", "/tmp/squid/log" )
end

delete = function (self,filetodelete)
	return checkfilefordelete( filetodelete )
end

view = function (self,filetoview)
	return checkfileforview( filetoview )
end