Merge pull request #313 from jgmdev/add-system-rmdir

Added `system.rmdir(path)`, and `common.rm(path, recursive)`
This commit is contained in:
Adam 2021-06-29 17:44:58 -04:00 committed by GitHub
commit 9126b5e64d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 95 additions and 0 deletions

View File

@ -322,4 +322,51 @@ function common.mkdirp(path)
return true return true
end end
function common.rm(path, recursively)
local stat = system.get_file_info(path)
if not stat or (stat.type ~= "file" and stat.type ~= "dir") then
return false, "invalid path given", path
end
if stat.type == "file" then
local removed, error = os.remove(path)
if not removed then
return false, error, path
end
else
local contents = system.list_dir(path)
if #contents > 0 and not recursively then
return false, "directory is not empty", path
end
for _, item in pairs(contents) do
local item_path = path .. PATHSEP .. item
local item_stat = system.get_file_info(item_path)
if not item_stat then
return false, "invalid file encountered", item_path
end
if item_stat.type == "dir" then
local deleted, error, ipath = common.rm(item_path, recursively)
if not deleted then
return false, error, ipath
end
elseif item_stat.type == "file" then
local removed, error = os.remove(item_path)
if not removed then
return false, error, item_path
end
end
end
local removed, error = system.rmdir(path)
if not removed then
return false, error, path
end
end
return true
end
return common return common

View File

@ -10,6 +10,7 @@
#ifdef _WIN32 #ifdef _WIN32
#include <direct.h> #include <direct.h>
#include <windows.h> #include <windows.h>
#include <fileapi.h>
#endif #endif
extern SDL_Window *window; extern SDL_Window *window;
@ -384,6 +385,52 @@ static int f_show_fatal_error(lua_State *L) {
} }
// removes an empty directory
static int f_rmdir(lua_State *L) {
const char *path = luaL_checkstring(L, 1);
#ifdef _WIN32
int deleted = RemoveDirectoryA(path);
if(deleted > 0) {
lua_pushboolean(L, 1);
} else {
DWORD error_code = GetLastError();
LPVOID message;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error_code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &message,
0,
NULL
);
lua_pushboolean(L, 0);
lua_pushlstring(L, (LPCTSTR)message, lstrlen((LPCTSTR)message));
LocalFree(message);
return 2;
}
#else
int deleted = remove(path);
if(deleted < 0) {
lua_pushboolean(L, 0);
lua_pushstring(L, strerror(errno));
return 2;
} else {
lua_pushboolean(L, 1);
}
#endif
return 1;
}
static int f_chdir(lua_State *L) { static int f_chdir(lua_State *L) {
const char *path = luaL_checkstring(L, 1); const char *path = luaL_checkstring(L, 1);
int err = chdir(path); int err = chdir(path);
@ -604,6 +651,7 @@ static const luaL_Reg lib[] = {
{ "set_window_size", f_set_window_size }, { "set_window_size", f_set_window_size },
{ "window_has_focus", f_window_has_focus }, { "window_has_focus", f_window_has_focus },
{ "show_fatal_error", f_show_fatal_error }, { "show_fatal_error", f_show_fatal_error },
{ "rmdir", f_rmdir },
{ "chdir", f_chdir }, { "chdir", f_chdir },
{ "mkdir", f_mkdir }, { "mkdir", f_mkdir },
{ "list_dir", f_list_dir }, { "list_dir", f_list_dir },