file.exists, file.isfile, file.isdir nothrow now
This commit is contained in:
@@ -18,10 +18,17 @@ namespace scripting {
|
|||||||
|
|
||||||
using namespace scripting;
|
using namespace scripting;
|
||||||
|
|
||||||
static fs::path resolve_path(lua_State*, const std::string& path) {
|
static fs::path resolve_path(const std::string& path) {
|
||||||
return engine->getPaths()->resolve(path);
|
return engine->getPaths()->resolve(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static fs::path resolve_path_soft(const std::string& path) {
|
||||||
|
if (path.find(':') == std::string::npos) {
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
return resolve_path(path);
|
||||||
|
}
|
||||||
|
|
||||||
static int l_file_find(lua_State* L) {
|
static int l_file_find(lua_State* L) {
|
||||||
std::string path = state->requireString(1);
|
std::string path = state->requireString(1);
|
||||||
try {
|
try {
|
||||||
@@ -33,13 +40,13 @@ static int l_file_find(lua_State* L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int l_file_resolve(lua_State* L) {
|
static int l_file_resolve(lua_State* L) {
|
||||||
fs::path path = resolve_path(L, state->requireString(1));
|
fs::path path = resolve_path(state->requireString(1));
|
||||||
lua_pushstring(L, path.u8string().c_str());
|
lua_pushstring(L, path.u8string().c_str());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_file_read(lua_State* L) {
|
static int l_file_read(lua_State* L) {
|
||||||
fs::path path = resolve_path(L, state->requireString(1));
|
fs::path path = resolve_path(state->requireString(1));
|
||||||
if (fs::is_regular_file(path)) {
|
if (fs::is_regular_file(path)) {
|
||||||
lua_pushstring(L, files::read_string(path).c_str());
|
lua_pushstring(L, files::read_string(path).c_str());
|
||||||
return 1;
|
return 1;
|
||||||
@@ -47,55 +54,50 @@ static int l_file_read(lua_State* L) {
|
|||||||
throw std::runtime_error("file does not exists "+util::quote(path.u8string()));
|
throw std::runtime_error("file does not exists "+util::quote(path.u8string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_file_write(lua_State* L) {
|
static int l_file_write(lua_State*) {
|
||||||
fs::path path = resolve_path(L, state->requireString(1));
|
fs::path path = resolve_path(state->requireString(1));
|
||||||
auto text = state->requireString(2);
|
auto text = state->requireString(2);
|
||||||
files::write_string(path, text);
|
files::write_string(path, text);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_file_remove(lua_State* L) {
|
static int l_file_remove(lua_State*) {
|
||||||
std::string rawpath = state->requireString(1);
|
std::string rawpath = state->requireString(1);
|
||||||
fs::path path = resolve_path(L, rawpath);
|
fs::path path = resolve_path(rawpath);
|
||||||
auto entryPoint = rawpath.substr(0, rawpath.find(':'));
|
auto entryPoint = rawpath.substr(0, rawpath.find(':'));
|
||||||
if (entryPoint != "world") {
|
if (entryPoint != "world") {
|
||||||
throw std::runtime_error("access denied");
|
throw std::runtime_error("access denied");
|
||||||
}
|
}
|
||||||
lua_pushboolean(L, fs::remove(path));
|
return state->pushboolean(fs::remove(path));
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_file_remove_tree(lua_State* L) {
|
static int l_file_remove_tree(lua_State*) {
|
||||||
std::string rawpath = state->requireString(1);
|
std::string rawpath = state->requireString(1);
|
||||||
fs::path path = resolve_path(L, rawpath);
|
fs::path path = resolve_path(rawpath);
|
||||||
auto entryPoint = rawpath.substr(0, rawpath.find(':'));
|
auto entryPoint = rawpath.substr(0, rawpath.find(':'));
|
||||||
if (entryPoint != "world") {
|
if (entryPoint != "world") {
|
||||||
throw std::runtime_error("access denied");
|
throw std::runtime_error("access denied");
|
||||||
}
|
}
|
||||||
lua_pushinteger(L, fs::remove_all(path));
|
return state->pushinteger(fs::remove_all(path));
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_file_exists(lua_State* L) {
|
static int l_file_exists(lua_State*) {
|
||||||
fs::path path = resolve_path(L, state->requireString(1));
|
fs::path path = resolve_path_soft(state->requireString(1));
|
||||||
lua_pushboolean(L, fs::exists(path));
|
return state->pushboolean(fs::exists(path));
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_file_isfile(lua_State* L) {
|
static int l_file_isfile(lua_State*) {
|
||||||
fs::path path = resolve_path(L, state->requireString(1));
|
fs::path path = resolve_path_soft(state->requireString(1));
|
||||||
lua_pushboolean(L, fs::is_regular_file(path));
|
return state->pushboolean(fs::is_regular_file(path));
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_file_isdir(lua_State* L) {
|
static int l_file_isdir(lua_State*) {
|
||||||
fs::path path = resolve_path(L, state->requireString(1));
|
fs::path path = resolve_path_soft(state->requireString(1));
|
||||||
lua_pushboolean(L, fs::is_directory(path));
|
return state->pushboolean(fs::is_directory(path));
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_file_length(lua_State* L) {
|
static int l_file_length(lua_State* L) {
|
||||||
fs::path path = resolve_path(L, state->requireString(1));
|
fs::path path = resolve_path(state->requireString(1));
|
||||||
if (fs::exists(path)){
|
if (fs::exists(path)){
|
||||||
lua_pushinteger(L, fs::file_size(path));
|
lua_pushinteger(L, fs::file_size(path));
|
||||||
} else {
|
} else {
|
||||||
@@ -105,19 +107,19 @@ static int l_file_length(lua_State* L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int l_file_mkdir(lua_State* L) {
|
static int l_file_mkdir(lua_State* L) {
|
||||||
fs::path path = resolve_path(L, state->requireString(1));
|
fs::path path = resolve_path(state->requireString(1));
|
||||||
lua_pushboolean(L, fs::create_directory(path));
|
lua_pushboolean(L, fs::create_directory(path));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_file_mkdirs(lua_State* L) {
|
static int l_file_mkdirs(lua_State* L) {
|
||||||
fs::path path = resolve_path(L, state->requireString(1));
|
fs::path path = resolve_path(state->requireString(1));
|
||||||
lua_pushboolean(L, fs::create_directories(path));
|
lua_pushboolean(L, fs::create_directories(path));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_file_read_bytes(lua_State* L) {
|
static int l_file_read_bytes(lua_State* L) {
|
||||||
fs::path path = resolve_path(L, state->requireString(1));
|
fs::path path = resolve_path(state->requireString(1));
|
||||||
if (fs::is_regular_file(path)) {
|
if (fs::is_regular_file(path)) {
|
||||||
size_t length = static_cast<size_t>(fs::file_size(path));
|
size_t length = static_cast<size_t>(fs::file_size(path));
|
||||||
|
|
||||||
@@ -159,7 +161,7 @@ static int l_file_write_bytes(lua_State* L) {
|
|||||||
throw std::runtime_error("string expected");
|
throw std::runtime_error("string expected");
|
||||||
}
|
}
|
||||||
|
|
||||||
fs::path path = resolve_path(L, state->requireString(pathIndex));
|
fs::path path = resolve_path(state->requireString(pathIndex));
|
||||||
|
|
||||||
std::vector<ubyte> bytes;
|
std::vector<ubyte> bytes;
|
||||||
|
|
||||||
@@ -188,7 +190,7 @@ static int l_file_list(lua_State* L) {
|
|||||||
if (dirname.find(':') == std::string::npos) {
|
if (dirname.find(':') == std::string::npos) {
|
||||||
return l_file_list_all_res(L, dirname);
|
return l_file_list_all_res(L, dirname);
|
||||||
}
|
}
|
||||||
fs::path path = resolve_path(L, dirname);
|
fs::path path = resolve_path(dirname);
|
||||||
if (!fs::is_directory(path)) {
|
if (!fs::is_directory(path)) {
|
||||||
throw std::runtime_error(util::quote(path.u8string())+" is not a directory");
|
throw std::runtime_error(util::quote(path.u8string())+" is not a directory");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user