3 local uci = require "uci"
4 local fs = require "nixio.fs"
5 local u_c = uci.cursor()
6 local basepath = u_c.get("filemanager","config","basedir")
10 local bp = basepath:match("(.*)/")
11 if bp and bp ~= "" then
14 _M.basepath = basepath;
16 function _M.is_in_dir(file, dir)
20 return file:sub(1, #dir) == dir
24 function _M.path_valid(path)
25 return _M.is_in_dir(path,_M.basepath) and fs.access(path,"r")
28 function _M.dir_path_valid(path)
29 return _M.is_in_dir(path,_M.basepath) and fs.stat(path,"type")=="dir" and fs.access(path,"w")
32 function _M.new_path_valid(path)
33 local dirpath = fs.dirname(path)
34 return _M.is_in_dir(dirpath,_M.basepath) and fs.access(dirpath,"w")
37 function _M.make_path(path)
38 local realpath = fs.realpath(_M.basepath..'/'..path)
39 if _M.path_valid(realpath) then
46 function _M.make_new_path(path)
47 local realpath = fs.realpath(fs.dirname(_M.basepath..'/'..path))..'/'..fs.basename(path)
48 if _M.new_path_valid(realpath) then
55 function _M.make_dir_path(path)
56 local realpath = fs.realpath(_M.basepath..'/'..path)
57 if _M.dir_path_valid(realpath) then
65 local ftype = fs.stat(item,"type")
66 if ftype == "reg" then
67 return fs.remove(item)
68 elseif ftype == "dir" then
69 local dir = fs.dir(item)
71 if not _M.rm(item..'/'..file) then
81 function _M.chmod(item,mode,recursive)
82 local result = fs.chmod(item,mode)
83 if result and recursive then
84 local dir = fs.dir(item)
87 local ftype = fs.stat(item..'/'..file,"type")
88 if ftype == "dir" then
89 result = _M.chmod(item..'/'..file,mode,recursive)
90 elseif ftype == "reg" then
91 result = _M.chmod(item..'/'..file,string.gsub(mode,"x","-"),false)