From 688addd5d2bfcca07997c1ab7c8ada0950b817ee Mon Sep 17 00:00:00 2001 From: Risto Date: Mon, 20 Feb 2023 05:56:33 +0100 Subject: [PATCH] feat(LUA): Add delete command to filesystem API (#3181) --- radio/src/lua/api_filesystem.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/radio/src/lua/api_filesystem.cpp b/radio/src/lua/api_filesystem.cpp index 1392cba11..482605b1b 100644 --- a/radio/src/lua/api_filesystem.cpp +++ b/radio/src/lua/api_filesystem.cpp @@ -151,6 +151,36 @@ int luaFstat(lua_State* L) return 1; } +/*luadoc +@function del(file or directory) + + Returns FRESULT (e.g. 0=OK, 4=File not found, 5=Path not found, 6=Path invalid) + +@status current Introduced in 2.9.0 + +### Example + +```lua + if del("/SCRIPTS/TOOLS/deleteme.txt") == 0 then + -- successfully deleted file + else + -- failed to delete file + end + +*/ +int luaDelete(lua_State* L) +{ + const char* filename = luaL_optstring(L, 1, nullptr); + + FRESULT res = f_unlink(filename); + if (res != FR_OK) { + printf("luaDelete cannot delete file/folder %s\n", filename); + } + + lua_pushunsigned(L, res); + return 1; +} + LROT_BEGIN(dir_handle, NULL, LROT_MASK_GC) LROT_FUNCENTRY( __gc, dir_gc ) @@ -159,6 +189,7 @@ LROT_END(dir_handle, NULL, LROT_MASK_GC) LROT_BEGIN(etxdir, NULL, 0) LROT_FUNCENTRY( dir, luaDir ) LROT_FUNCENTRY( fstat, luaFstat ) + LROT_FUNCENTRY( del, luaDelete ) LROT_END(etxdir, NULL, 0) extern "C" {