From 61cae99758d34df15f57755e1f402e1f102ce239 Mon Sep 17 00:00:00 2001 From: sizzlins Date: Wed, 24 Jun 2026 09:42:19 +0700 Subject: [PATCH] Expose std::filesystem::remove_all to Lua as dfhack.filesystem.rmdir_recursive --- docs/dev/Lua API.rst | 4 ++++ library/LuaApi.cpp | 1 + library/include/modules/Filesystem.h | 1 + library/modules/Filesystem.cpp | 13 +++++++++++++ 4 files changed, 19 insertions(+) diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index 0fbcb419714..fd43fec1bf0 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -3291,6 +3291,10 @@ unless otherwise noted. Removes a directory. Only works if the directory is already empty. +* ``dfhack.filesystem.rmdir_recursive(path)`` + + Removes a directory and all of its contents recursively. + * ``dfhack.filesystem.mtime(path)`` Returns the modification time (in seconds) of the file or directory diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index d95b6c1a2a7..fafa3f84bb2 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -3338,6 +3338,7 @@ static const LuaWrapper::FunctionReg dfhack_filesystem_module[] = { WRAPM(Filesystem, mkdir), WRAPM(Filesystem, mkdir_recursive), WRAPM(Filesystem, rmdir), + WRAPM(Filesystem, rmdir_recursive), WRAPM(Filesystem, exists), WRAPM(Filesystem, isfile), WRAPM(Filesystem, isdir), diff --git a/library/include/modules/Filesystem.h b/library/include/modules/Filesystem.h index 68da9ec7f61..282d72664fe 100644 --- a/library/include/modules/Filesystem.h +++ b/library/include/modules/Filesystem.h @@ -64,6 +64,7 @@ namespace DFHack { // returns true on success or if directory already exists DFHACK_EXPORT bool mkdir_recursive(std::filesystem::path path) noexcept; DFHACK_EXPORT bool rmdir(std::filesystem::path path) noexcept; + DFHACK_EXPORT bool rmdir_recursive(std::filesystem::path path) noexcept; DFHACK_EXPORT bool stat(std::filesystem::path path, std::filesystem::file_status& info) noexcept; DFHACK_EXPORT bool exists(std::filesystem::path path) noexcept; DFHACK_EXPORT bool isfile(std::filesystem::path path) noexcept; diff --git a/library/modules/Filesystem.cpp b/library/modules/Filesystem.cpp index 6d49c6a3b74..4721a45ee9f 100644 --- a/library/modules/Filesystem.cpp +++ b/library/modules/Filesystem.cpp @@ -139,6 +139,19 @@ bool Filesystem::rmdir (std::filesystem::path path) noexcept } } +bool Filesystem::rmdir_recursive (std::filesystem::path path) noexcept +{ + try + { + std::filesystem::remove_all(path); + return true; + } + catch (std::filesystem::filesystem_error&) + { + return false; + } +} + bool Filesystem::stat (std::filesystem::path path, std::filesystem::file_status &info) noexcept { try