Lua API: Add rmdir, cpdir and mvdir (#9638)

Co-authored-by: rubenwardy <rw@rubenwardy.com>
This commit is contained in:
Elijah Duffy 2021-11-10 10:10:20 -08:00 committed by GitHub
parent 6db914780e
commit cbf658f83d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 0 deletions

@ -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,

@ -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);

@ -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);