AP_Scripting: added isdirectory()

This commit is contained in:
Andrew Tridgell 2023-12-04 17:19:15 +11:00
parent 9065baa329
commit 3446ff78d7
4 changed files with 26 additions and 0 deletions

View File

@ -3062,6 +3062,11 @@ function scripting:restart_all() end
---@return table -- table of filenames
function dirlist(directoryname) end
-- return true if path is a directory
---@param path string
---@return result
function isdirectory(path) end
--desc
---@param filename string
function remove(filename) end

View File

@ -812,6 +812,7 @@ userdata uint32_t manual tofloat uint32_t_tofloat 0
global manual dirlist lua_dirlist 1
global manual remove lua_removefile 1
global manual isdirectory lua_isdirectory 1
global manual print lua_print 1
singleton mavlink depends HAL_GCS_ENABLED

View File

@ -720,6 +720,25 @@ int lua_dirlist(lua_State *L) {
return 1; /* table is already on top */
}
/*
return true if path is a directory
*/
int lua_isdirectory(lua_State *L) {
binding_argcheck(L, 1);
const char *path = luaL_checkstring(L, 1);
struct stat st;
if (AP::FS().stat(path, &st) != 0) {
lua_pushnil(L); /* return nil and ... */
lua_pushstring(L, strerror(errno)); /* error message */
return 2;
}
bool ret = (st.st_mode & S_IFMT) == S_IFDIR;
lua_pushboolean(L, ret);
return 1;
}
/*
remove a file
*/

View File

@ -12,6 +12,7 @@ int lua_get_CAN_device(lua_State *L);
int lua_get_CAN_device2(lua_State *L);
int lua_dirlist(lua_State *L);
int lua_removefile(lua_State *L);
int lua_isdirectory(lua_State *L);
int SRV_Channels_get_safety_state(lua_State *L);
int lua_get_PWMSource(lua_State *L);
int lua_get_SocketAPM(lua_State *L);