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