Skip to content

Commit

Permalink
Lua API: Add rmdir, cpdir and mvdir (#9638)
Browse files Browse the repository at this point in the history
Co-authored-by: rubenwardy <rw@rubenwardy.com>
  • Loading branch information
octacian and rubenwardy committed Nov 10, 2021
1 parent 6db9147 commit cbf658f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
13 changes: 13 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -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,
Expand Down
49 changes: 49 additions & 0 deletions src/script/lua_api/l_util.cpp
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions src/script/lua_api/l_util.h
Expand Up @@ -80,6 +80,15 @@ class ModApiUtil : public ModApiBase
// 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);

Expand Down

0 comments on commit cbf658f

Please sign in to comment.