2 LuCI - Filesystem tools
5 A module offering often needed filesystem manipulation functions
11 Copyright 2008 Steven Barth <steven@midlink.org>
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at
17 http://www.apache.org/licenses/LICENSE-2.0
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
27 module("luci.fs", package.seeall)
30 posix.umask("rwx------")
35 -- Checks whether a file exists
36 function isfile(filename)
37 local fp = io.open(filename, "r")
38 if fp then fp:close() end
42 -- Returns the content of file
43 function readfile(filename)
44 local fp, err = io.open(filename)
50 local data = fp:read("*a")
55 -- Writes given data to a file
56 function writefile(filename, data)
57 local fp, err = io.open(filename, "w")
69 -- Returns the file modification date/time of "path"
71 return posix.stat(path, "mtime")
75 basename = posix.basename
78 dirname = posix.dirname
83 -- wrapper for posix.mkdir
84 function mkdir(path, recursive)
88 if path:sub(1,1) == "/" then
90 path = path:gsub("^/+","")
93 for elem in path:gmatch("([^/]+)/*") do
94 base = base .. "/" .. elem
96 local stat = posix.stat( base )
99 local stat, errmsg, errno = posix.mkdir( base )
101 if type(stat) ~= "number" or stat ~= 0 then
102 return stat, errmsg, errno
105 if stat.type ~= "directory" then
106 return nil, base .. ": File exists", 17
113 return posix.mkdir( path )
117 -- Alias for posix.rmdir
120 -- Alias for posix.stat
123 -- Alias for posix.chmod
126 -- Alias for posix.link
129 -- Alias for posix.unlink
130 unlink = posix.unlink