Skip to content

Commit cbf658f

Browse files
octacianrubenwardy
andauthoredNov 10, 2021
Lua API: Add rmdir, cpdir and mvdir (#9638)
Co-authored-by: rubenwardy <rw@rubenwardy.com>
1 parent 6db9147 commit cbf658f

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
 

‎doc/lua_api.txt

+13
Original file line numberDiff line numberDiff line change
@@ -4624,6 +4624,19 @@ Utilities
46244624
* `minetest.mkdir(path)`: returns success.
46254625
* Creates a directory specified by `path`, creating parent directories
46264626
if they don't exist.
4627+
* `minetest.rmdir(path, recursive)`: returns success.
4628+
* Removes a directory specified by `path`.
4629+
* If `recursive` is set to `true`, the directory is recursively removed.
4630+
Otherwise, the directory will only be removed if it is empty.
4631+
* Returns true on success, false on failure.
4632+
* `minetest.cpdir(source, destination)`: returns success.
4633+
* Copies a directory specified by `path` to `destination`
4634+
* Any files in `destination` will be overwritten if they already exist.
4635+
* Returns true on success, false on failure.
4636+
* `minetest.mvdir(source, destination)`: returns success.
4637+
* Moves a directory specified by `path` to `destination`.
4638+
* If the `destination` is a non-empty directory, then the move will fail.
4639+
* Returns true on success, false on failure.
46274640
* `minetest.get_dir_list(path, [is_dir])`: returns list of entry names
46284641
* is_dir is one of:
46294642
* nil: return all entries,

‎src/script/lua_api/l_util.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,49 @@ int ModApiUtil::l_mkdir(lua_State *L)
349349
return 1;
350350
}
351351

352+
// rmdir(path, recursive)
353+
int ModApiUtil::l_rmdir(lua_State *L)
354+
{
355+
NO_MAP_LOCK_REQUIRED;
356+
const char *path = luaL_checkstring(L, 1);
357+
CHECK_SECURE_PATH(L, path, true);
358+
359+
bool recursive = readParam<bool>(L, 2, false);
360+
361+
if (recursive)
362+
lua_pushboolean(L, fs::RecursiveDelete(path));
363+
else
364+
lua_pushboolean(L, fs::DeleteSingleFileOrEmptyDirectory(path));
365+
366+
return 1;
367+
}
368+
369+
// cpdir(source, destination)
370+
int ModApiUtil::l_cpdir(lua_State *L)
371+
{
372+
NO_MAP_LOCK_REQUIRED;
373+
const char *source = luaL_checkstring(L, 1);
374+
const char *destination = luaL_checkstring(L, 2);
375+
CHECK_SECURE_PATH(L, source, false);
376+
CHECK_SECURE_PATH(L, destination, true);
377+
378+
lua_pushboolean(L, fs::CopyDir(source, destination));
379+
return 1;
380+
}
381+
382+
// mpdir(source, destination)
383+
int ModApiUtil::l_mvdir(lua_State *L)
384+
{
385+
NO_MAP_LOCK_REQUIRED;
386+
const char *source = luaL_checkstring(L, 1);
387+
const char *destination = luaL_checkstring(L, 2);
388+
CHECK_SECURE_PATH(L, source, true);
389+
CHECK_SECURE_PATH(L, destination, true);
390+
391+
lua_pushboolean(L, fs::MoveDir(source, destination));
392+
return 1;
393+
}
394+
352395
// get_dir_list(path, is_dir)
353396
int ModApiUtil::l_get_dir_list(lua_State *L)
354397
{
@@ -588,6 +631,9 @@ void ModApiUtil::Initialize(lua_State *L, int top)
588631
API_FCT(decompress);
589632

590633
API_FCT(mkdir);
634+
API_FCT(rmdir);
635+
API_FCT(cpdir);
636+
API_FCT(mvdir);
591637
API_FCT(get_dir_list);
592638
API_FCT(safe_file_write);
593639

@@ -651,6 +697,9 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top)
651697
API_FCT(decompress);
652698

653699
API_FCT(mkdir);
700+
API_FCT(rmdir);
701+
API_FCT(cpdir);
702+
API_FCT(mvdir);
654703
API_FCT(get_dir_list);
655704

656705
API_FCT(encode_base64);

‎src/script/lua_api/l_util.h

+9
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ class ModApiUtil : public ModApiBase
8080
// mkdir(path)
8181
static int l_mkdir(lua_State *L);
8282

83+
// rmdir(path, recursive)
84+
static int l_rmdir(lua_State *L);
85+
86+
// cpdir(source, destination, remove_source)
87+
static int l_cpdir(lua_State *L);
88+
89+
// mvdir(source, destination)
90+
static int l_mvdir(lua_State *L);
91+
8392
// get_dir_list(path, is_dir)
8493
static int l_get_dir_list(lua_State *L);
8594

0 commit comments

Comments
 (0)
Please sign in to comment.