summaryrefslogtreecommitdiff
path: root/core/src/xmake
diff options
context:
space:
mode:
authorSaikari <[email protected]>2026-01-28 11:14:05 +0300
committerSaikari <[email protected]>2026-01-28 11:14:05 +0300
commitc1a0fd840c94e54aab5317bd65b5e2f4dbca947d (patch)
tree68473b4beb9c7605c1646df1d4c144f5b2192588 /core/src/xmake
parente1d3f3555235dc41e530f563140850a3ee8b3e2b (diff)
Add os.access function for file access checking
Diffstat (limited to 'core/src/xmake')
-rw-r--r--core/src/xmake/engine.c2
-rw-r--r--core/src/xmake/os/access.c25
2 files changed, 27 insertions, 0 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index 9ceabd156..a77cc4a9b 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -151,6 +151,7 @@ tb_int_t xm_os_cpuinfo(lua_State *lua);
tb_int_t xm_os_meminfo(lua_State *lua);
tb_int_t xm_os_readlink(lua_State *lua);
tb_int_t xm_os_filesize(lua_State *lua);
+tb_int_t xm_os_access(lua_State *lua);
tb_int_t xm_os_emptydir(lua_State *lua);
tb_int_t xm_os_syserror(lua_State *lua);
tb_int_t xm_os_strerror(lua_State *lua);
@@ -446,6 +447,7 @@ static luaL_Reg const g_os_functions[] = {
{ "fscase", xm_os_fscase },
{ "rename", xm_os_rename },
{ "exists", xm_os_exists },
+ { "access", xm_os_access },
{ "setenv", xm_os_setenv },
{ "getenv", xm_os_getenv },
{ "getenvs", xm_os_getenvs },
diff --git a/core/src/xmake/os/access.c b/core/src/xmake/os/access.c
new file mode 100644
index 000000000..51bf00607
--- /dev/null
+++ b/core/src/xmake/os/access.c
@@ -0,0 +1,25 @@
+#include "prefix.h"
+
+tb_int_t xm_os_access(lua_State *lua) {
+ tb_assert_and_check_return_val(lua, 0);
+
+ // check
+ tb_char_t const* path = luaL_checkstring(lua, 1);
+ tb_char_t const* mode_str = luaL_checkstring(lua, 2);
+ tb_check_return_val(path && mode_str, 0);
+
+ // parse mode
+ tb_size_t mode = 0;
+ while (*mode_str) {
+ switch (*mode_str) {
+ case 'r': mode |= TB_FILE_MODE_RO; break;
+ case 'w': mode |= TB_FILE_MODE_WO; break;
+ case 'x': mode |= TB_FILE_MODE_EXEC; break;
+ }
+ mode_str++;
+ }
+
+ // check access
+ lua_pushboolean(lua, tb_file_access(path, mode));
+ return 1;
+}