summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElijah Duffy <enduffy2014@outlook.com>2021-11-10 10:10:20 -0800
committerGitHub <noreply@github.com>2021-11-10 19:10:20 +0100
commitcbf658f83d206bf340ab4aa8eab02b058e9b293f (patch)
tree88d88074d7c8f229c7d393746747ad67d29860b9
parent6db914780ed6e27d9876763d561ea0daafe01f4f (diff)
downloadminetest-cbf658f83d206bf340ab4aa8eab02b058e9b293f.tar.gz
minetest-cbf658f83d206bf340ab4aa8eab02b058e9b293f.tar.bz2
minetest-cbf658f83d206bf340ab4aa8eab02b058e9b293f.zip
Lua API: Add `rmdir`, `cpdir` and `mvdir` (#9638)
Co-authored-by: rubenwardy <rw@rubenwardy.com>
-rw-r--r--doc/lua_api.txt13
-rw-r--r--src/script/lua_api/l_util.cpp49
-rw-r--r--src/script/lua_api/l_util.h9
3 files changed, 71 insertions, 0 deletions
diff --git a/doc/lua_api.txt b/doc/lua_api.txt
index f3007671b..3b9f4c339 100644
--- a/doc/lua_api.txt
+++ b/doc/lua_api.txt
@@ -4624,6 +4624,19 @@ Utilities
* `minetest.mkdir(path)`: returns success.
* Creates a directory specified by `path`, creating parent directories
if they don't exist.
+* `minetest.rmdir(path, recursive)`: returns success.
+ * Removes a directory specified by `path`.
+ * If `recursive` is set to `true`, the directory is recursively removed.
+ Otherwise, the directory will only be removed if it is empty.
+ * Returns true on success, false on failure.
+* `minetest.cpdir(source, destination)`: returns success.
+ * Copies a directory specified by `path` to `destination`
+ * Any files in `destination` will be overwritten if they already exist.
+ * Returns true on success, false on failure.
+* `minetest.mvdir(source, destination)`: returns success.
+ * Moves a directory specified by `path` to `destination`.
+ * If the `destination` is a non-empty directory, then the move will fail.
+ * Returns true on success, false on failure.
* `minetest.get_dir_list(path, [is_dir])`: returns list of entry names
* is_dir is one of:
* nil: return all entries,
diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp
index 53319ccfd..528d9c6dd 100644
--- a/src/script/lua_api/l_util.cpp
+++ b/src/script/lua_api/l_util.cpp
@@ -349,6 +349,49 @@ int ModApiUtil::l_mkdir(lua_State *L)
return 1;
}
+// rmdir(path, recursive)
+int ModApiUtil::l_rmdir(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char *path = luaL_checkstring(L, 1);
+ CHECK_SECURE_PATH(L, path, true);
+
+ bool recursive = readParam<bool>(L, 2, false);
+
+ if (recursive)
+ lua_pushboolean(L, fs::RecursiveDelete(path));
+ else
+ lua_pushboolean(L, fs::DeleteSingleFileOrEmptyDirectory(path));
+
+ return 1;
+}
+
+// cpdir(source, destination)
+int ModApiUtil::l_cpdir(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char *source = luaL_checkstring(L, 1);
+ const char *destination = luaL_checkstring(L, 2);
+ CHECK_SECURE_PATH(L, source, false);
+ CHECK_SECURE_PATH(L, destination, true);
+
+ lua_pushboolean(L, fs::CopyDir(source, destination));
+ return 1;
+}
+
+// mpdir(source, destination)
+int ModApiUtil::l_mvdir(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char *source = luaL_checkstring(L, 1);
+ const char *destination = luaL_checkstring(L, 2);
+ CHECK_SECURE_PATH(L, source, true);
+ CHECK_SECURE_PATH(L, destination, true);
+
+ lua_pushboolean(L, fs::MoveDir(source, destination));
+ return 1;
+}
+
// get_dir_list(path, is_dir)
int ModApiUtil::l_get_dir_list(lua_State *L)
{
@@ -588,6 +631,9 @@ void ModApiUtil::Initialize(lua_State *L, int top)
API_FCT(decompress);
API_FCT(mkdir);
+ API_FCT(rmdir);
+ API_FCT(cpdir);
+ API_FCT(mvdir);
API_FCT(get_dir_list);
API_FCT(safe_file_write);
@@ -651,6 +697,9 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top)
API_FCT(decompress);
API_FCT(mkdir);
+ API_FCT(rmdir);
+ API_FCT(cpdir);
+ API_FCT(mvdir);
API_FCT(get_dir_list);
API_FCT(encode_base64);
diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h
index 314e92f5c..fcf8a1057 100644
--- a/src/script/lua_api/l_util.h
+++ b/src/script/lua_api/l_util.h
@@ -80,6 +80,15 @@ private:
// mkdir(path)
static int l_mkdir(lua_State *L);
+ // rmdir(path, recursive)
+ static int l_rmdir(lua_State *L);
+
+ // cpdir(source, destination, remove_source)
+ static int l_cpdir(lua_State *L);
+
+ // mvdir(source, destination)
+ static int l_mvdir(lua_State *L);
+
// get_dir_list(path, is_dir)
static int l_get_dir_list(lua_State *L);