Drop support for luaposix and bitlib (obsoleted by nixio)
[project/luci.git] / libs / core / luasrc / fs.lua
1 --[[
2 LuCI - Filesystem tools
3
4 Description:
5 A module offering often needed filesystem manipulation functions
6
7 FileId:
8 $Id$
9
10 License:
11 Copyright 2008 Steven Barth <steven@midlink.org>
12
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
16
17 http://www.apache.org/licenses/LICENSE-2.0
18
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.
24
25 ]]--
26
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"
32
33 local type = type
34
35 --- LuCI filesystem library.
36 module "luci.fs"
37
38 --- Test for file access permission on given path.
39 -- @class function
40 -- @name access
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)
45 access = fs.access
46
47 --- Evaluate given shell glob pattern and return a table containing all matching
48 -- file and directory entries.
49 -- @class function
50 -- @name glob
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)
55 function glob(...)
56 local iter, code, msg = fs.glob(...)
57 if iter then
58 return nutil.consume(iter)
59 else
60 return nil, code, msg
61 end
62 end
63
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"
69 end
70
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"
76 end
77
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
83
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
90
91 --- Copies a file.
92 -- @param source Source file
93 -- @param dest Destination
94 -- @return Boolean containing true on success or nil on error
95 copy = fs.datacopy
96
97 --- Renames a file.
98 -- @param source Source file
99 -- @param dest Destination
100 -- @return Boolean containing true on success or nil on error
101 rename = fs.move
102
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)
108 function mtime(path)
109 return fs.stat(path, "mtime")
110 end
111
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)
121 end
122
123 --- Return the last element - usually the filename - from the given path with
124 -- the directory component stripped.
125 -- @class function
126 -- @name basename
127 -- @param path String containing the path to strip
128 -- @return String containing the base name of given path
129 -- @see dirname
130 basename = fs.basename
131
132 --- Return the directory component of the given path with the last element
133 -- stripped of.
134 -- @class function
135 -- @name dirname
136 -- @param path String containing the path to strip
137 -- @return String containing the directory component of given path
138 -- @see basename
139 dirname = fs.dirname
140
141 --- Return a table containing all entries of the specified directory.
142 -- @class function
143 -- @name dir
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
148 function dir(...)
149 local iter, code, msg = fs.dir(...)
150 if iter then
151 local t = nutil.consume(iter)
152 t[#t+1] = "."
153 t[#t+1] = ".."
154 return t
155 else
156 return nil, code, msg
157 end
158 end
159
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)
168 end
169
170 --- Remove the given empty directory.
171 -- @class function
172 -- @name rmdir
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
177 rmdir = fs.rmdir
178
179 --- Get information about given file or directory.
180 -- @class function
181 -- @name stat
182 -- @param path String containing the path of the directory to query
183 -- @return Table containing file or directory properties or nil on error
184 -- @return String containing the error description on error
185 -- @return Number containing the os specific errno on error
186 stat = fs.stat
187
188 --- Set permissions on given file or directory.
189 -- @class function
190 -- @name chmod
191 -- @param path String containing the path of the directory
192 -- @param perm String containing the permissions to set ([ugoa][+-][rwx])
193 -- @return Number with the return code, 0 on sucess or nil on error
194 -- @return String containing the error description on error
195 -- @return Number containing the os specific errno on error
196 chmod = fs.chmod
197
198 --- Create a hard- or symlink from given file (or directory) to specified target
199 -- file (or directory) path.
200 -- @class function
201 -- @name link
202 -- @param path1 String containing the source path to link
203 -- @param path2 String containing the destination path for the link
204 -- @param symlink Boolean indicating wheather to create a symlink (optional)
205 -- @return Number with the return code, 0 on sucess or nil on error
206 -- @return String containing the error description on error
207 -- @return Number containing the os specific errno on error
208 function link(src, dest, sym)
209 return sym and fs.symlink(src, dest) or fs.link(src, dest)
210 end
211
212 --- Remove the given file.
213 -- @class function
214 -- @name unlink
215 -- @param path String containing the path of the file to remove
216 -- @return Number with the return code, 0 on sucess or nil on error
217 -- @return String containing the error description on error
218 -- @return Number containing the os specific errno on error
219 unlink = fs.unlink
220
221 --- Retrieve target of given symlink.
222 -- @class function
223 -- @name readlink
224 -- @param path String containing the path of the symlink to read
225 -- @return String containing the link target or nil on error
226 -- @return String containing the error description on error
227 -- @return Number containing the os specific errno on error
228 readlink = fs.readlink