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 local posix = require "posix"
28 local io = require "io"
31 --- LuCI filesystem library.
34 --- Test for file access permission on given path.
37 -- @param str String value containing the path
38 -- @return Number containing the return code, 0 on sucess or nil on error
39 -- @return String containing the error description (if any)
40 -- @return Number containing the os specific errno (if any)
43 --- Evaluate given shell glob pattern and return a table containing all matching
44 -- file and directory entries.
47 -- @param filename String containing the path of the file to read
48 -- @return Table containing file and directory entries or nil if no matches
49 -- @return String containing the error description (if no matches)
50 -- @return Number containing the os specific errno (if no matches)
53 --- Checks wheather the given path exists and points to a regular file.
54 -- @param filename String containing the path of the file to read
55 -- @return Boolean indicating wheather given path points to regular file
56 function isfile(filename)
57 return posix.stat(filename, "type") == "regular"
60 --- Read the whole content of the given file into memory.
61 -- @param filename String containing the path of the file to read
62 -- @return String containing the file contents or nil on error
63 -- @return String containing the error message on error
64 function readfile(filename)
65 local fp, err = io.open(filename)
71 local data = fp:read("*a")
76 --- Write the contents of given string to given file.
77 -- @param filename String containing the path of the file to read
78 -- @param data String containing the data to write
79 -- @return Boolean containing true on success or nil on error
80 -- @return String containing the error message on error
81 function writefile(filename, data)
82 local fp, err = io.open(filename, "w")
94 --- Get the last modification time of given file path in Unix epoch format.
95 -- @param path String containing the path of the file or directory to read
96 -- @return Number containing the epoch time or nil on error
97 -- @return String containing the error description (if any)
98 -- @return Number containing the os specific errno (if any)
100 return posix.stat(path, "mtime")
103 --- Return the last element - usually the filename - from the given path with
104 -- the directory component stripped.
107 -- @param path String containing the path to strip
108 -- @return String containing the base name of given path
110 basename = posix.basename
112 --- Return the directory component of the given path with the last element
116 -- @param path String containing the path to strip
117 -- @return String containing the directory component of given path
119 dirname = posix.dirname
121 --- Return a table containing all entries of the specified directory.
124 -- @param path String containing the path of the directory to scan
125 -- @return Table containing file and directory entries or nil on error
126 -- @return String containing the error description on error
127 -- @return Number containing the os specific errno on error
130 --- Create a new directory, recursively on demand.
131 -- @param path String with the name or path of the directory to create
132 -- @param recursive Create multiple directory levels (optional, default is true)
133 -- @return Number with the return code, 0 on sucess or nil on error
134 -- @return String containing the error description on error
135 -- @return Number containing the os specific errno on error
136 function mkdir(path, recursive)
140 if path:sub(1,1) == "/" then
142 path = path:gsub("^/+","")
145 for elem in path:gmatch("([^/]+)/*") do
146 base = base .. "/" .. elem
148 local stat = posix.stat( base )
151 local stat, errmsg, errno = posix.mkdir( base )
153 if type(stat) ~= "number" or stat ~= 0 then
154 return stat, errmsg, errno
157 if stat.type ~= "directory" then
158 return nil, base .. ": File exists", 17
165 return posix.mkdir( path )
169 --- Remove the given empty directory.
172 -- @param path String containing the path of the directory to remove
173 -- @return Number with the return code, 0 on sucess or nil on error
174 -- @return String containing the error description on error
175 -- @return Number containing the os specific errno on error
178 --- Get information about given file or directory.
181 -- @param path String containing the path of the directory to query
182 -- @return Table containing file or directory properties or nil on error
183 -- @return String containing the error description on error
184 -- @return Number containing the os specific errno on error
187 --- Set permissions on given file or directory.
190 -- @param path String containing the path of the directory
191 -- @param perm String containing the permissions to set ([ugoa][+-][rwx])
192 -- @return Number with the return code, 0 on sucess or nil on error
193 -- @return String containing the error description on error
194 -- @return Number containing the os specific errno on error
197 --- Create a hard- or symlink from given file (or directory) to specified target
198 -- file (or directory) path.
201 -- @param path1 String containing the source path to link
202 -- @param path2 String containing the destination path for the link
203 -- @param symlink Boolean indicating wheather to create a symlink (optional)
204 -- @return Number with the return code, 0 on sucess or nil on error
205 -- @return String containing the error description on error
206 -- @return Number containing the os specific errno on error
209 --- Remove the given file.
212 -- @param path String containing the path of the file to remove
213 -- @return Number with the return code, 0 on sucess or nil on error
214 -- @return String containing the error description on error
215 -- @return Number containing the os specific errno on error
216 unlink = posix.unlink
218 --- Retrieve target of given symlink.
221 -- @param path String containing the path of the symlink to read
222 -- @return String containing the link target or nil on error
223 -- @return String containing the error description on error
224 -- @return Number containing the os specific errno on error
225 readlink = posix.readlink