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
|
module (..., package.seeall)
-- no initializer in model - use controller.init for that
local function file_info ( path )
local file = io.popen("ls -lh " .. path )
local file_inuse = io.popen("fuser " .. path )
split = require("split")
local file_info_result = file:read("*a") or "unknown"
local fileinuseresult = file_inuse:read("*a") or "unkown"
if fileinuseresult == "" then
fileinuseresult = "Delete"
else
fileinuseresult = "in use"
end
local size = rawget(split("%s+", file_info_result),5)
local lastmod = rawget(split("%s+", file_info_result),6) .. " " .. rawget(split("%s+", file_info_result),7) .. " " .. rawget(split("%s+", file_info_result),8)
file:close()
file_inuse:close()
return lastmod,size,fileinuseresult
end
-- function to recursively inserts all filenames in a dir into an array
function recursedir(path, filearray)
local k,v
for k,v in pairs(posix.dir(path) or {}) do
-- we ignore files that begins with a '.'
if not string.match(v, "^%.") then
local f = path .. "/" .. v
if (posix.stat(f).type == "directory") then
recursedir(f, filearray)
else
table.insert(filearray, f)
end
end
end
end
local function list_files ( ... )
local listed_files = { cfe{inuse="Delete", save="Save", view="View",
size="Size", lastmod="Last Modified", name="File"} }
local open_files = {}
-- split = require("split")
local files = {}
local k,v
-- generate a singe table with all the files
for k,v in pairs{...} do
recursedir(v, files)
end
for k,v in pairs(files) do
local lastmod,size,fileinuseresult = file_info(v)
if ( fileinuseresult == "Delete" ) then
del="/delete?name=" .. v
else
del=nil
end
table.insert ( listed_files , cfe{inuse=fileinuseresult, del=del, save="/download?name=" .. v, view="/view?name=" .. v, size=size, lastmod=lastmod, name=v, type="", id=k} )
end
return listed_files
end
local function checkfilefordelete ( filetodelete )
local deletedfile = {}
local available_files = get()
for k,v in pairs(available_files) do
if ( available_files[k].name == filetodelete ) then
if ( available_files[k].del ) then
local status, err = os.remove( filetodelete )
if not ( err ) then
deletedfile = cfe{value="File '" .. filetodelete .. "' has been successfully deleted!", type="text", option=nil, errtxt=err}
end
else
end
end
end
if not ( deletedfile.value ) then
if not ( err ) then
err = "The file is not accepted for removal!"
end
deletedfile = cfe{value="File '" .. filetodelete .. "' has not been deleted!", type="text", option=nil, errtxt=err}
end
return deletedfile
end
local function checkfileforview ( path )
local file = {}
local available_files = get()
filecontent = cfe{value="", type="longtext", option=nil, errtxt="You are not allowed to view this file!"}
for k,v in pairs(available_files) do
if ( available_files[k].name == path ) then
local file = io.open( path )
local file_result = file:read("*a") or "unknown"
file:close()
err=nil
filecontent = cfe{value=file_result, type="text", option=nil, name=basename(path), errtxt=err}
end
end
return filecontent
end
get = function (self)
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
|