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 io = require "io"
28 local os = require "os"
29 local ltn12 = require "luci.ltn12"
30 local fs = require "nixio.fs"
31 local nutil = require "nixio.util"
35 --- LuCI filesystem library.
38 --- Test for file access permission on given path.
41 -- @param str String value containing the path
42 -- @return Number containing the return code, 0 on sucess or nil on error
43 -- @return String containing the error description (if any)
44 -- @return Number containing the os specific errno (if any)
47 --- Evaluate given shell glob pattern and return a table containing all matching
48 -- file and directory entries.
51 -- @param filename String containing the path of the file to read
52 -- @return Table containing file and directory entries or nil if no matches
53 -- @return String containing the error description (if no matches)
54 -- @return Number containing the os specific errno (if no matches)
56 local iter, code, msg = fs.glob(...)
58 return nutil.consume(iter)
64 --- Checks wheather the given path exists and points to a regular file.
65 -- @param filename String containing the path of the file to test
66 -- @return Boolean indicating wheather given path points to regular file
67 function isfile(filename)
68 return fs.stat(filename, "type") == "reg"
71 --- Checks wheather the given path exists and points to a directory.
72 -- @param dirname String containing the path of the directory to test
73 -- @return Boolean indicating wheather given path points to directory
74 function isdirectory(dirname)
75 return fs.stat(dirname, "type") == "dir"
78 --- Read the whole content of the given file into memory.
79 -- @param filename String containing the path of the file to read
80 -- @return String containing the file contents or nil on error
81 -- @return String containing the error message on error
82 readfile = fs.readfile
84 --- Write the contents of given string to given file.
85 -- @param filename String containing the path of the file to read
86 -- @param data String containing the data to write
87 -- @return Boolean containing true on success or nil on error
88 -- @return String containing the error message on error
89 writefile = fs.writefile
92 -- @param source Source file
93 -- @param dest Destination
94 -- @return Boolean containing true on success or nil on error
98 -- @param source Source file
99 -- @param dest Destination
100 -- @return Boolean containing true on success or nil on error
103 --- Get the last modification time of given file path in Unix epoch format.
104 -- @param path String containing the path of the file or directory to read
105 -- @return Number containing the epoch time or nil on error
106 -- @return String containing the error description (if any)
107 -- @return Number containing the os specific errno (if any)
109 return fs.stat(path, "mtime")
112 --- Set the last modification time of given file path in Unix epoch format.
113 -- @param path String containing the path of the file or directory to read
114 -- @param mtime Last modification timestamp
115 -- @param atime Last accessed timestamp
116 -- @return 0 in case of success nil on error
117 -- @return String containing the error description (if any)
118 -- @return Number containing the os specific errno (if any)
119 function utime(path, mtime, atime)
120 return fs.utimes(path, atime, mtime)
123 --- Return the last element - usually the filename - from the given path with
124 -- the directory component stripped.
127 -- @param path String containing the path to strip
128 -- @return String containing the base name of given path
130 basename = fs.basename
132 --- Return the directory component of the given path with the last element
136 -- @param path String containing the path to strip
137 -- @return String containing the directory component of given path
141 --- Return a table containing all entries of the specified directory.
144 -- @param path String containing the path of the directory to scan
145 -- @return Table containing file and directory entries or nil on error
146 -- @return String containing the error description on error
147 -- @return Number containing the os specific errno on error
149 local iter, code, msg = fs.dir(...)
151 local t = nutil.consume(iter)
156 return nil, code, msg
160 --- Create a new directory, recursively on demand.
161 -- @param path String with the name or path of the directory to create
162 -- @param recursive Create multiple directory levels (optional, default is true)
163 -- @return Number with the return code, 0 on sucess or nil on error
164 -- @return String containing the error description on error
165 -- @return Number containing the os specific errno on error
166 function mkdir(path, recursive)
167 return recursive and fs.mkdirr(path) or fs.mkdir(path)
170 --- Remove the given empty directory.
173 -- @param path String containing the path of the directory to remove
174 -- @return Number with the return code, 0 on sucess or nil on error
175 -- @return String containing the error description on error
176 -- @return Number containing the os specific errno on error
183 chr = "character device",
184 blk = "block device",
188 --- Get information about given file or directory.
191 -- @param path String containing the path of the directory to query
192 -- @return Table containing file or directory properties or nil on error
193 -- @return String containing the error description on error
194 -- @return Number containing the os specific errno on error
195 function stat(path, key)
196 local data, code, msg = fs.stat(path)
198 data.mode = data.modestr
199 data.type = stat_tr[data.type] or "?"
201 return key and data and data[key] or data, code, msg
204 --- Set permissions on given file or directory.
207 -- @param path String containing the path of the directory
208 -- @param perm String containing the permissions to set ([ugoa][+-][rwx])
209 -- @return Number with the return code, 0 on sucess or nil on error
210 -- @return String containing the error description on error
211 -- @return Number containing the os specific errno on error
214 --- Create a hard- or symlink from given file (or directory) to specified target
215 -- file (or directory) path.
218 -- @param path1 String containing the source path to link
219 -- @param path2 String containing the destination path for the link
220 -- @param symlink Boolean indicating wheather to create a symlink (optional)
221 -- @return Number with the return code, 0 on sucess or nil on error
222 -- @return String containing the error description on error
223 -- @return Number containing the os specific errno on error
224 function link(src, dest, sym)
225 return sym and fs.symlink(src, dest) or fs.link(src, dest)
228 --- Remove the given file.
231 -- @param path String containing the path of the file to remove
232 -- @return Number with the return code, 0 on sucess or nil on error
233 -- @return String containing the error description on error
234 -- @return Number containing the os specific errno on error
237 --- Retrieve target of given symlink.
240 -- @param path String containing the path of the symlink to read
241 -- @return String containing the link target or nil on error
242 -- @return String containing the error description on error
243 -- @return Number containing the os specific errno on error
244 readlink = fs.readlink