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"
|
2014-07-19 03:21:36 +02:00
|
|
|
#include "libaegisub/lua/ffi.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-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); });
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2014-07-19 03:21:36 +02:00
|
|
|
agi::lua::register_lib_table(L, {"DirectoryIterator"},
|
|
|
|
"chdir", lfs_chdir,
|
|
|
|
"currentdir", currentdir,
|
|
|
|
"mkdir", mkdir,
|
|
|
|
"rmdir", lfs_rmdir,
|
|
|
|
"touch", touch,
|
|
|
|
"get_mtime", get_mtime,
|
|
|
|
"get_mode", get_mode,
|
|
|
|
"get_size", get_size,
|
|
|
|
"dir_new", dir_new,
|
|
|
|
"dir_free", dir_free,
|
|
|
|
"dir_next", dir_next,
|
|
|
|
"dir_close", dir_close);
|
2014-04-26 15:35:49 +02:00
|
|
|
return 1;
|
2014-04-26 00:40:43 +02:00
|
|
|
}
|