1*11be35a1SLionel Sambuc // Copyright 2011 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc // documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc // may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc // without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc #include "utils/fs/lua_module.hpp"
30*11be35a1SLionel Sambuc
31*11be35a1SLionel Sambuc extern "C" {
32*11be35a1SLionel Sambuc #include <dirent.h>
33*11be35a1SLionel Sambuc }
34*11be35a1SLionel Sambuc
35*11be35a1SLionel Sambuc #include <cerrno>
36*11be35a1SLionel Sambuc #include <cstring>
37*11be35a1SLionel Sambuc #include <stdexcept>
38*11be35a1SLionel Sambuc #include <string>
39*11be35a1SLionel Sambuc
40*11be35a1SLionel Sambuc #include <lutok/operations.hpp>
41*11be35a1SLionel Sambuc #include <lutok/state.ipp>
42*11be35a1SLionel Sambuc
43*11be35a1SLionel Sambuc #include "utils/format/macros.hpp"
44*11be35a1SLionel Sambuc #include "utils/fs/operations.hpp"
45*11be35a1SLionel Sambuc #include "utils/fs/path.hpp"
46*11be35a1SLionel Sambuc #include "utils/sanity.hpp"
47*11be35a1SLionel Sambuc
48*11be35a1SLionel Sambuc namespace fs = utils::fs;
49*11be35a1SLionel Sambuc
50*11be35a1SLionel Sambuc
51*11be35a1SLionel Sambuc namespace {
52*11be35a1SLionel Sambuc
53*11be35a1SLionel Sambuc
54*11be35a1SLionel Sambuc /// Lua binding for fs::path::basename.
55*11be35a1SLionel Sambuc ///
56*11be35a1SLionel Sambuc /// \pre stack(-1) The input path.
57*11be35a1SLionel Sambuc /// \post stack(-1) The basename of the input path.
58*11be35a1SLionel Sambuc ///
59*11be35a1SLionel Sambuc /// \param state The Lua state.
60*11be35a1SLionel Sambuc ///
61*11be35a1SLionel Sambuc /// \return The number of result values, i.e. 1.
62*11be35a1SLionel Sambuc static int
lua_fs_basename(lutok::state & state)63*11be35a1SLionel Sambuc lua_fs_basename(lutok::state& state)
64*11be35a1SLionel Sambuc {
65*11be35a1SLionel Sambuc if (!state.is_string())
66*11be35a1SLionel Sambuc throw std::runtime_error("Need a string parameter");
67*11be35a1SLionel Sambuc const fs::path path(state.to_string());
68*11be35a1SLionel Sambuc
69*11be35a1SLionel Sambuc state.push_string(path.leaf_name().c_str());
70*11be35a1SLionel Sambuc return 1;
71*11be35a1SLionel Sambuc }
72*11be35a1SLionel Sambuc
73*11be35a1SLionel Sambuc
74*11be35a1SLionel Sambuc /// Lua binding for fs::path::dirname.
75*11be35a1SLionel Sambuc ///
76*11be35a1SLionel Sambuc /// \pre stack(-1) The input path.
77*11be35a1SLionel Sambuc /// \post stack(-1) The directory part of the input path.
78*11be35a1SLionel Sambuc ///
79*11be35a1SLionel Sambuc /// \param state The Lua state.
80*11be35a1SLionel Sambuc ///
81*11be35a1SLionel Sambuc /// \return The number of result values, i.e. 1.
82*11be35a1SLionel Sambuc static int
lua_fs_dirname(lutok::state & state)83*11be35a1SLionel Sambuc lua_fs_dirname(lutok::state& state)
84*11be35a1SLionel Sambuc {
85*11be35a1SLionel Sambuc if (!state.is_string())
86*11be35a1SLionel Sambuc throw std::runtime_error("Need a string parameter");
87*11be35a1SLionel Sambuc const fs::path path(state.to_string());
88*11be35a1SLionel Sambuc
89*11be35a1SLionel Sambuc state.push_string(path.branch_path().c_str());
90*11be35a1SLionel Sambuc return 1;
91*11be35a1SLionel Sambuc }
92*11be35a1SLionel Sambuc
93*11be35a1SLionel Sambuc
94*11be35a1SLionel Sambuc /// Lua binding for fs::path::exists.
95*11be35a1SLionel Sambuc ///
96*11be35a1SLionel Sambuc /// \pre stack(-1) The input path.
97*11be35a1SLionel Sambuc /// \post stack(-1) Whether the input path exists or not.
98*11be35a1SLionel Sambuc ///
99*11be35a1SLionel Sambuc /// \param state The Lua state.
100*11be35a1SLionel Sambuc ///
101*11be35a1SLionel Sambuc /// \return The number of result values, i.e. 1.
102*11be35a1SLionel Sambuc static int
lua_fs_exists(lutok::state & state)103*11be35a1SLionel Sambuc lua_fs_exists(lutok::state& state)
104*11be35a1SLionel Sambuc {
105*11be35a1SLionel Sambuc if (!state.is_string())
106*11be35a1SLionel Sambuc throw std::runtime_error("Need a string parameter");
107*11be35a1SLionel Sambuc const fs::path path(state.to_string());
108*11be35a1SLionel Sambuc
109*11be35a1SLionel Sambuc state.push_boolean(fs::exists(path));
110*11be35a1SLionel Sambuc return 1;
111*11be35a1SLionel Sambuc }
112*11be35a1SLionel Sambuc
113*11be35a1SLionel Sambuc
114*11be35a1SLionel Sambuc /// Lua binding for the files iterator.
115*11be35a1SLionel Sambuc ///
116*11be35a1SLionel Sambuc /// This function takes an open directory from the closure of the iterator and
117*11be35a1SLionel Sambuc /// returns the next entry. See lua_fs_files() for the iterator generator
118*11be35a1SLionel Sambuc /// function.
119*11be35a1SLionel Sambuc ///
120*11be35a1SLionel Sambuc /// \pre upvalue(1) The userdata containing an open DIR* object.
121*11be35a1SLionel Sambuc ///
122*11be35a1SLionel Sambuc /// \param state The lua state.
123*11be35a1SLionel Sambuc ///
124*11be35a1SLionel Sambuc /// \return The number of result values, i.e. 0 if there are no more entries or
125*11be35a1SLionel Sambuc /// 1 if an entry has been read.
126*11be35a1SLionel Sambuc static int
files_iterator(lutok::state & state)127*11be35a1SLionel Sambuc files_iterator(lutok::state& state)
128*11be35a1SLionel Sambuc {
129*11be35a1SLionel Sambuc DIR** dirp = state.to_userdata< DIR* >(state.upvalue_index(1));
130*11be35a1SLionel Sambuc const struct dirent* entry = ::readdir(*dirp);
131*11be35a1SLionel Sambuc if (entry == NULL)
132*11be35a1SLionel Sambuc return 0;
133*11be35a1SLionel Sambuc else {
134*11be35a1SLionel Sambuc state.push_string(entry->d_name);
135*11be35a1SLionel Sambuc return 1;
136*11be35a1SLionel Sambuc }
137*11be35a1SLionel Sambuc }
138*11be35a1SLionel Sambuc
139*11be35a1SLionel Sambuc
140*11be35a1SLionel Sambuc /// Lua binding for the destruction of the files iterator.
141*11be35a1SLionel Sambuc ///
142*11be35a1SLionel Sambuc /// This function takes an open directory and closes it. See lua_fs_files() for
143*11be35a1SLionel Sambuc /// the iterator generator function.
144*11be35a1SLionel Sambuc ///
145*11be35a1SLionel Sambuc /// \pre stack(-1) The userdata containing an open DIR* object.
146*11be35a1SLionel Sambuc /// \post The DIR* object is closed.
147*11be35a1SLionel Sambuc ///
148*11be35a1SLionel Sambuc /// \param state The lua state.
149*11be35a1SLionel Sambuc ///
150*11be35a1SLionel Sambuc /// \return The number of result values, i.e. 0.
151*11be35a1SLionel Sambuc static int
files_gc(lutok::state & state)152*11be35a1SLionel Sambuc files_gc(lutok::state& state)
153*11be35a1SLionel Sambuc {
154*11be35a1SLionel Sambuc PRE(state.is_userdata());
155*11be35a1SLionel Sambuc
156*11be35a1SLionel Sambuc DIR** dirp = state.to_userdata< DIR* >();
157*11be35a1SLionel Sambuc // For some reason, this may be called more than once. I don't know why
158*11be35a1SLionel Sambuc // this happens, but we must protect against it.
159*11be35a1SLionel Sambuc if (*dirp != NULL) {
160*11be35a1SLionel Sambuc ::closedir(*dirp);
161*11be35a1SLionel Sambuc *dirp = NULL;
162*11be35a1SLionel Sambuc }
163*11be35a1SLionel Sambuc
164*11be35a1SLionel Sambuc return 0;
165*11be35a1SLionel Sambuc }
166*11be35a1SLionel Sambuc
167*11be35a1SLionel Sambuc
168*11be35a1SLionel Sambuc /// Lua binding to create an iterator to scan the contents of a directory.
169*11be35a1SLionel Sambuc ///
170*11be35a1SLionel Sambuc /// \pre stack(-1) The input path.
171*11be35a1SLionel Sambuc /// \post stack(-1) The iterator function.
172*11be35a1SLionel Sambuc ///
173*11be35a1SLionel Sambuc /// \param state The Lua state.
174*11be35a1SLionel Sambuc ///
175*11be35a1SLionel Sambuc /// \return The number of result values, i.e. 1.
176*11be35a1SLionel Sambuc static int
lua_fs_files(lutok::state & state)177*11be35a1SLionel Sambuc lua_fs_files(lutok::state& state)
178*11be35a1SLionel Sambuc {
179*11be35a1SLionel Sambuc if (!state.is_string())
180*11be35a1SLionel Sambuc throw std::runtime_error("Need a string parameter");
181*11be35a1SLionel Sambuc const fs::path path(state.to_string());
182*11be35a1SLionel Sambuc
183*11be35a1SLionel Sambuc DIR** dirp = state.new_userdata< DIR* >();
184*11be35a1SLionel Sambuc
185*11be35a1SLionel Sambuc state.new_table();
186*11be35a1SLionel Sambuc state.push_string("__gc");
187*11be35a1SLionel Sambuc state.push_cxx_function(files_gc);
188*11be35a1SLionel Sambuc state.set_table();
189*11be35a1SLionel Sambuc
190*11be35a1SLionel Sambuc state.set_metatable();
191*11be35a1SLionel Sambuc
192*11be35a1SLionel Sambuc *dirp = ::opendir(path.c_str());
193*11be35a1SLionel Sambuc if (*dirp == NULL) {
194*11be35a1SLionel Sambuc const int original_errno = errno;
195*11be35a1SLionel Sambuc throw std::runtime_error(F("Failed to open directory: %s") %
196*11be35a1SLionel Sambuc std::strerror(original_errno));
197*11be35a1SLionel Sambuc }
198*11be35a1SLionel Sambuc
199*11be35a1SLionel Sambuc state.push_cxx_closure(files_iterator, 1);
200*11be35a1SLionel Sambuc
201*11be35a1SLionel Sambuc return 1;
202*11be35a1SLionel Sambuc }
203*11be35a1SLionel Sambuc
204*11be35a1SLionel Sambuc
205*11be35a1SLionel Sambuc /// Lua binding for fs::path::is_absolute.
206*11be35a1SLionel Sambuc ///
207*11be35a1SLionel Sambuc /// \pre stack(-1) The input path.
208*11be35a1SLionel Sambuc /// \post stack(-1) Whether the input path is absolute or not.
209*11be35a1SLionel Sambuc ///
210*11be35a1SLionel Sambuc /// \param state The Lua state.
211*11be35a1SLionel Sambuc ///
212*11be35a1SLionel Sambuc /// \return The number of result values, i.e. 1.
213*11be35a1SLionel Sambuc static int
lua_fs_is_absolute(lutok::state & state)214*11be35a1SLionel Sambuc lua_fs_is_absolute(lutok::state& state)
215*11be35a1SLionel Sambuc {
216*11be35a1SLionel Sambuc if (!state.is_string())
217*11be35a1SLionel Sambuc throw std::runtime_error("Need a string parameter");
218*11be35a1SLionel Sambuc const fs::path path(state.to_string());
219*11be35a1SLionel Sambuc
220*11be35a1SLionel Sambuc state.push_boolean(path.is_absolute());
221*11be35a1SLionel Sambuc return 1;
222*11be35a1SLionel Sambuc }
223*11be35a1SLionel Sambuc
224*11be35a1SLionel Sambuc
225*11be35a1SLionel Sambuc /// Lua binding for fs::path::operator/.
226*11be35a1SLionel Sambuc ///
227*11be35a1SLionel Sambuc /// \pre stack(-2) The first input path.
228*11be35a1SLionel Sambuc /// \pre stack(-1) The second input path.
229*11be35a1SLionel Sambuc /// \post stack(-1) The concatenation of the two paths.
230*11be35a1SLionel Sambuc ///
231*11be35a1SLionel Sambuc /// \param state The Lua state.
232*11be35a1SLionel Sambuc ///
233*11be35a1SLionel Sambuc /// \return The number of result values, i.e. 1.
234*11be35a1SLionel Sambuc static int
lua_fs_join(lutok::state & state)235*11be35a1SLionel Sambuc lua_fs_join(lutok::state& state)
236*11be35a1SLionel Sambuc {
237*11be35a1SLionel Sambuc if (!state.is_string(-2))
238*11be35a1SLionel Sambuc throw std::runtime_error("Need a string parameter");
239*11be35a1SLionel Sambuc const fs::path path1(state.to_string(-2));
240*11be35a1SLionel Sambuc
241*11be35a1SLionel Sambuc if (!state.is_string(-1))
242*11be35a1SLionel Sambuc throw std::runtime_error("Need a string parameter");
243*11be35a1SLionel Sambuc const fs::path path2(state.to_string(-1));
244*11be35a1SLionel Sambuc
245*11be35a1SLionel Sambuc state.push_string((path1 / path2).c_str());
246*11be35a1SLionel Sambuc return 1;
247*11be35a1SLionel Sambuc }
248*11be35a1SLionel Sambuc
249*11be35a1SLionel Sambuc
250*11be35a1SLionel Sambuc } // anonymous namespace
251*11be35a1SLionel Sambuc
252*11be35a1SLionel Sambuc
253*11be35a1SLionel Sambuc /// Creates a Lua 'fs' module.
254*11be35a1SLionel Sambuc ///
255*11be35a1SLionel Sambuc /// \post The global 'fs' symbol is set to a table that contains functions to a
256*11be35a1SLionel Sambuc /// variety of utilites from the fs C++ module.
257*11be35a1SLionel Sambuc ///
258*11be35a1SLionel Sambuc /// \param s The Lua state.
259*11be35a1SLionel Sambuc void
open_fs(lutok::state & s)260*11be35a1SLionel Sambuc fs::open_fs(lutok::state& s)
261*11be35a1SLionel Sambuc {
262*11be35a1SLionel Sambuc std::map< std::string, lutok::cxx_function > members;
263*11be35a1SLionel Sambuc members["basename"] = lua_fs_basename;
264*11be35a1SLionel Sambuc members["dirname"] = lua_fs_dirname;
265*11be35a1SLionel Sambuc members["exists"] = lua_fs_exists;
266*11be35a1SLionel Sambuc members["files"] = lua_fs_files;
267*11be35a1SLionel Sambuc members["is_absolute"] = lua_fs_is_absolute;
268*11be35a1SLionel Sambuc members["join"] = lua_fs_join;
269*11be35a1SLionel Sambuc lutok::create_module(s, "fs", members);
270*11be35a1SLionel Sambuc }
271