2014-04-26 15:35:49 +02:00
|
|
|
// Copyright (c) 2014, Thomas Goyne <plorkyeran@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
2014-07-18 19:33:57 +02:00
|
|
|
#include "libaegisub/fs.h"
|
|
|
|
#include "libaegisub/type_name.h"
|
2014-04-26 15:35:49 +02:00
|
|
|
|
2014-05-23 00:40:16 +02:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
#include <boost/filesystem/path.hpp>
|
2014-07-18 19:33:57 +02:00
|
|
|
#include <lua.hpp>
|
2014-04-26 15:35:49 +02:00
|
|
|
|
|
|
|
using namespace agi::fs;
|
|
|
|
namespace bfs = boost::filesystem;
|
|
|
|
|
2014-07-18 19:33:57 +02:00
|
|
|
namespace agi {
|
|
|
|
AGI_DEFINE_TYPE_NAME(DirectoryIterator);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
template<typename Func>
|
|
|
|
auto wrap(char **err, Func f) -> decltype(f()) {
|
2014-04-26 15:35:49 +02:00
|
|
|
try {
|
2014-07-18 19:33:57 +02:00
|
|
|
return f();
|
2014-04-26 15:35:49 +02:00
|
|
|
}
|
|
|
|
catch (bfs::filesystem_error const& e) {
|
2014-07-18 19:33:57 +02:00
|
|
|
*err = strdup(e.what());
|
|
|
|
return 0;
|
2014-04-26 15:35:49 +02:00
|
|
|
}
|
|
|
|
catch (agi::Exception const& e) {
|
2014-07-18 19:33:57 +02:00
|
|
|
*err = strdup(e.GetMessage().c_str());
|
|
|
|
return 0;
|
2014-04-27 16:20:11 +02:00
|
|
|
}
|
2014-04-26 15:35:49 +02:00
|
|
|
}
|
|
|
|
|
2014-07-18 19:33:57 +02:00
|
|
|
template<typename Ret>
|
|
|
|
bool setter(const char *path, char **err, Ret (*f)(bfs::path const&)) {
|
|
|
|
return wrap(err, [=]{
|
|
|
|
f(path);
|
|
|
|
return true;
|
|
|
|
});
|
2014-04-26 15:35:49 +02:00
|
|
|
}
|
|
|
|
|
2014-07-18 19:33:57 +02:00
|
|
|
bool lfs_chdir(const char *dir, char **err) {
|
|
|
|
return setter(dir, err, &bfs::current_path);
|
2014-04-26 15:35:49 +02:00
|
|
|
}
|
|
|
|
|
2014-07-18 19:33:57 +02:00
|
|
|
char *currentdir(char **err) {
|
|
|
|
return wrap(err, []{
|
|
|
|
return strdup(bfs::current_path().string().c_str());
|
|
|
|
});
|
2014-04-26 15:35:49 +02:00
|
|
|
}
|
|
|
|
|
2014-07-18 19:33:57 +02:00
|
|
|
bool mkdir(const char *dir, char **err) {
|
|
|
|
return setter(dir, err, &CreateDirectory);
|
2014-04-26 15:35:49 +02:00
|
|
|
}
|
|
|
|
|
2014-07-18 19:33:57 +02:00
|
|
|
bool lfs_rmdir(const char *dir, char **err) {
|
|
|
|
return setter(dir, err, &Remove);
|
2014-04-26 15:35:49 +02:00
|
|
|
}
|
|
|
|
|
2014-07-18 19:33:57 +02:00
|
|
|
bool touch(const char *path, char **err) {
|
|
|
|
return setter(path, err, &Touch);
|
2014-04-26 15:35:49 +02:00
|
|
|
}
|
|
|
|
|
2014-07-18 19:33:57 +02:00
|
|
|
char *dir_next(DirectoryIterator &it, char **err) {
|
|
|
|
if (it == end(it)) return nullptr;
|
|
|
|
return wrap(err, [&]{
|
|
|
|
auto str = strdup((*it).c_str());
|
|
|
|
++it;
|
|
|
|
return str;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void dir_close(DirectoryIterator &it) {
|
2014-04-26 15:35:49 +02:00
|
|
|
it = DirectoryIterator();
|
2014-07-18 19:33:57 +02:00
|
|
|
}
|
2014-04-26 15:35:49 +02:00
|
|
|
|
2014-07-18 19:33:57 +02:00
|
|
|
void dir_free(DirectoryIterator *it) {
|
|
|
|
delete it;
|
2014-04-26 15:35:49 +02:00
|
|
|
}
|
2014-07-18 19:33:57 +02:00
|
|
|
|
|
|
|
DirectoryIterator *dir_new(const char *path, char **err) {
|
|
|
|
return wrap(err, [=]{
|
|
|
|
return new DirectoryIterator(path, "");
|
|
|
|
});
|
2014-04-26 15:35:49 +02:00
|
|
|
}
|
|
|
|
|
2014-07-18 19:33:57 +02:00
|
|
|
const char *get_mode(const char *path, char **err) {
|
|
|
|
return wrap(err, [=]() -> const char * {
|
|
|
|
switch (bfs::status(path).type()) {
|
|
|
|
case bfs::file_not_found: return nullptr; break;
|
|
|
|
case bfs::regular_file: return "file"; break;
|
|
|
|
case bfs::directory_file: return "directory"; break;
|
|
|
|
case bfs::symlink_file: return "link"; break;
|
|
|
|
case bfs::block_file: return "block device"; break;
|
|
|
|
case bfs::character_file: return "char device"; break;
|
|
|
|
case bfs::fifo_file: return "fifo"; break;
|
|
|
|
case bfs::socket_file: return "socket"; break;
|
|
|
|
case bfs::reparse_file: return "reparse point"; break;
|
|
|
|
default: return "other"; break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-04-26 15:35:49 +02:00
|
|
|
|
2014-07-18 19:33:57 +02:00
|
|
|
time_t get_mtime(const char *path, char **err) {
|
|
|
|
return wrap(err, [=] { return ModifiedTime(path); });
|
|
|
|
}
|
|
|
|
|
|
|
|
uintmax_t get_size(const char *path, char **err) {
|
|
|
|
return wrap(err, [=] { return Size(path); });
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void push_ffi_function(lua_State *L, const char *name, T *func) {
|
|
|
|
lua_pushvalue(L, -2); // push cast function
|
|
|
|
lua_pushstring(L, agi::type_name<T*>::name().c_str());
|
|
|
|
// This cast isn't legal, but LuaJIT internally requires that it work
|
|
|
|
lua_pushlightuserdata(L, (void *)func);
|
|
|
|
lua_call(L, 2, 1);
|
|
|
|
lua_setfield(L, -2, name);
|
|
|
|
}
|
|
|
|
}
|
2014-04-26 15:35:49 +02:00
|
|
|
|
2014-07-18 19:33:57 +02:00
|
|
|
extern "C" int luaopen_lfs_impl(lua_State *L) {
|
|
|
|
lua_getglobal(L, "require");
|
|
|
|
lua_pushstring(L, "ffi");
|
|
|
|
lua_call(L, 1, 1);
|
|
|
|
|
|
|
|
lua_getfield(L, -1, "cdef");
|
|
|
|
lua_pushstring(L, "typedef struct DirectoryIterator DirectoryIterator;");
|
|
|
|
lua_call(L, 1, 0);
|
|
|
|
|
|
|
|
lua_getfield(L, -1, "cast");
|
|
|
|
lua_remove(L, -2); // ffi table
|
|
|
|
|
|
|
|
lua_createtable(L, 0, 12);
|
|
|
|
push_ffi_function(L, "chdir", lfs_chdir);
|
|
|
|
push_ffi_function(L, "currentdir", currentdir);
|
|
|
|
push_ffi_function(L, "mkdir", mkdir);
|
|
|
|
push_ffi_function(L, "rmdir", lfs_rmdir);
|
|
|
|
push_ffi_function(L, "touch", touch);
|
|
|
|
push_ffi_function(L, "get_mtime", get_mtime);
|
|
|
|
push_ffi_function(L, "get_mode", get_mode);
|
|
|
|
push_ffi_function(L, "get_size", get_size);
|
|
|
|
|
|
|
|
push_ffi_function(L, "dir_new", dir_new);
|
|
|
|
push_ffi_function(L, "dir_free", dir_free);
|
|
|
|
push_ffi_function(L, "dir_next", dir_next);
|
|
|
|
push_ffi_function(L, "dir_close", dir_close);
|
|
|
|
|
|
|
|
lua_remove(L, -2); // ffi.cast function
|
2014-04-26 15:35:49 +02:00
|
|
|
return 1;
|
2014-04-26 00:40:43 +02:00
|
|
|
}
|