1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/fs/lua_module.hpp"
40 #include <lutok/operations.hpp>
41 #include <lutok/stack_cleaner.hpp>
42 #include <lutok/state.ipp>
44 #include "utils/format/macros.hpp"
45 #include "utils/fs/operations.hpp"
46 #include "utils/fs/path.hpp"
47 #include "utils/sanity.hpp"
49 namespace fs = utils::fs;
55 /// Given a path, qualifies it with the module's start directory if necessary.
57 /// \param state The Lua state.
58 /// \param path The path to qualify.
60 /// \return The original path if it was absolute; otherwise the original path
61 /// appended to the module's start directory.
63 /// \throw std::runtime_error If the module's state has been corrupted.
65 qualify_path(lutok::state& state, const fs::path& path)
67 lutok::stack_cleaner cleaner(state);
69 if (path.is_absolute()) {
72 state.get_global("_fs_start_dir");
73 if (!state.is_string(-1))
74 throw std::runtime_error("Missing _fs_start_dir global variable; "
76 return fs::path(state.to_string(-1)) / path;
81 /// Safely gets a path from the Lua state.
83 /// \param state The Lua state.
84 /// \param index The position in the Lua stack that contains the path to query.
86 /// \return The queried path.
88 /// \throw fs::error If the value is not a valid path.
89 /// \throw std::runtime_error If the value on the Lua stack is not convertible
92 to_path(lutok::state& state, const int index)
94 if (!state.is_string(index))
95 throw std::runtime_error("Need a string parameter");
96 return fs::path(state.to_string(index));
100 /// Lua binding for fs::path::basename.
102 /// \pre stack(-1) The input path.
103 /// \post stack(-1) The basename of the input path.
105 /// \param state The Lua state.
107 /// \return The number of result values, i.e. 1.
109 lua_fs_basename(lutok::state& state)
111 lutok::stack_cleaner cleaner(state);
113 const fs::path path = to_path(state, -1);
114 state.push_string(path.leaf_name().c_str());
120 /// Lua binding for fs::path::dirname.
122 /// \pre stack(-1) The input path.
123 /// \post stack(-1) The directory part of the input path.
125 /// \param state The Lua state.
127 /// \return The number of result values, i.e. 1.
129 lua_fs_dirname(lutok::state& state)
131 lutok::stack_cleaner cleaner(state);
133 const fs::path path = to_path(state, -1);
134 state.push_string(path.branch_path().c_str());
140 /// Lua binding for fs::path::exists.
142 /// \pre stack(-1) The input path.
143 /// \post stack(-1) Whether the input path exists or not.
145 /// \param state The Lua state.
147 /// \return The number of result values, i.e. 1.
149 lua_fs_exists(lutok::state& state)
151 lutok::stack_cleaner cleaner(state);
153 const fs::path path = qualify_path(state, to_path(state, -1));
154 state.push_boolean(fs::exists(path));
160 /// Lua binding for the files iterator.
162 /// This function takes an open directory from the closure of the iterator and
163 /// returns the next entry. See lua_fs_files() for the iterator generator
166 /// \pre upvalue(1) The userdata containing an open DIR* object.
168 /// \param state The lua state.
170 /// \return The number of result values, i.e. 0 if there are no more entries or
171 /// 1 if an entry has been read.
173 files_iterator(lutok::state& state)
175 lutok::stack_cleaner cleaner(state);
177 DIR** dirp = state.to_userdata< DIR* >(state.upvalue_index(1));
178 const struct dirent* entry = ::readdir(*dirp);
182 state.push_string(entry->d_name);
189 /// Lua binding for the destruction of the files iterator.
191 /// This function takes an open directory and closes it. See lua_fs_files() for
192 /// the iterator generator function.
194 /// \pre stack(-1) The userdata containing an open DIR* object.
195 /// \post The DIR* object is closed.
197 /// \param state The lua state.
199 /// \return The number of result values, i.e. 0.
201 files_gc(lutok::state& state)
203 lutok::stack_cleaner cleaner(state);
205 PRE(state.is_userdata(-1));
207 DIR** dirp = state.to_userdata< DIR* >(-1);
208 // For some reason, this may be called more than once. I don't know why
209 // this happens, but we must protect against it.
219 /// Lua binding to create an iterator to scan the contents of a directory.
221 /// \pre stack(-1) The input path.
222 /// \post stack(-1) The iterator function.
224 /// \param state The Lua state.
226 /// \return The number of result values, i.e. 1.
228 lua_fs_files(lutok::state& state)
230 lutok::stack_cleaner cleaner(state);
232 const fs::path path = qualify_path(state, to_path(state, -1));
234 DIR** dirp = state.new_userdata< DIR* >();
237 state.push_string("__gc");
238 state.push_cxx_function(files_gc);
241 state.set_metatable(-2);
243 *dirp = ::opendir(path.c_str());
245 const int original_errno = errno;
246 throw std::runtime_error(F("Failed to open directory: %s") %
247 std::strerror(original_errno));
250 state.push_cxx_closure(files_iterator, 1);
257 /// Lua binding for fs::path::is_absolute.
259 /// \pre stack(-1) The input path.
260 /// \post stack(-1) Whether the input path is absolute or not.
262 /// \param state The Lua state.
264 /// \return The number of result values, i.e. 1.
266 lua_fs_is_absolute(lutok::state& state)
268 lutok::stack_cleaner cleaner(state);
270 const fs::path path = to_path(state, -1);
272 state.push_boolean(path.is_absolute());
278 /// Lua binding for fs::path::operator/.
280 /// \pre stack(-2) The first input path.
281 /// \pre stack(-1) The second input path.
282 /// \post stack(-1) The concatenation of the two paths.
284 /// \param state The Lua state.
286 /// \return The number of result values, i.e. 1.
288 lua_fs_join(lutok::state& state)
290 lutok::stack_cleaner cleaner(state);
292 const fs::path path1 = to_path(state, -2);
293 const fs::path path2 = to_path(state, -1);
294 state.push_string((path1 / path2).c_str());
300 } // anonymous namespace
303 /// Creates a Lua 'fs' module with a default start directory of ".".
305 /// \post The global 'fs' symbol is set to a table that contains functions to a
306 /// variety of utilites from the fs C++ module.
308 /// \param s The Lua state.
310 fs::open_fs(lutok::state& s)
312 open_fs(s, fs::current_path());
316 /// Creates a Lua 'fs' module with an explicit start directory.
318 /// \post The global 'fs' symbol is set to a table that contains functions to a
319 /// variety of utilites from the fs C++ module.
321 /// \param s The Lua state.
322 /// \param start_dir The start directory to use in all operations that reference
323 /// the underlying file sytem.
325 fs::open_fs(lutok::state& s, const fs::path& start_dir)
327 lutok::stack_cleaner cleaner(s);
329 s.push_string(start_dir.str());
330 s.set_global("_fs_start_dir");
332 std::map< std::string, lutok::cxx_function > members;
333 members["basename"] = lua_fs_basename;
334 members["dirname"] = lua_fs_dirname;
335 members["exists"] = lua_fs_exists;
336 members["files"] = lua_fs_files;
337 members["is_absolute"] = lua_fs_is_absolute;
338 members["join"] = lua_fs_join;
339 lutok::create_module(s, "fs", members);