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)
37 -- Checks whether a file exists
38 function isfile(filename)
39 local fp = io.open(filename, "r")
40 if fp then fp:close() end
44 -- Returns the content of file
45 function readfile(filename)
46 local fp, err = io.open(filename)
52 local data = fp:read("*a")
57 -- Writes given data to a file
58 function writefile(filename, data)
59 local fp, err = io.open(filename, "w")
71 -- Returns the file modification date/time of "path"
73 return posix.stat(path, "mtime")
77 basename = posix.basename
80 dirname = posix.dirname
85 -- wrapper for posix.mkdir
86 function mkdir(path, recursive)
90 if path:sub(1,1) == "/" then
92 path = path:gsub("^/+","")
95 for elem in path:gmatch("([^/]+)/*") do
96 base = base .. "/" .. elem
98 local stat = posix.stat( base )
101 local stat, errmsg, errno = posix.mkdir( base )
103 if type(stat) ~= "number" or stat ~= 0 then
104 return stat, errmsg, errno
107 if stat.type ~= "directory" then
108 return nil, base .. ": File exists", 17
115 return posix.mkdir( path )
119 -- Alias for posix.rmdir
122 -- Alias for posix.stat
125 -- Alias for posix.chmod
128 -- Alias for posix.link
131 -- Alias for posix.unlink
132 unlink = posix.unlink