diff options
| author | Saikari <[email protected]> | 2026-01-28 11:14:05 +0300 |
|---|---|---|
| committer | Saikari <[email protected]> | 2026-01-28 11:14:05 +0300 |
| commit | c1a0fd840c94e54aab5317bd65b5e2f4dbca947d (patch) | |
| tree | 68473b4beb9c7605c1646df1d4c144f5b2192588 | |
| parent | e1d3f3555235dc41e530f563140850a3ee8b3e2b (diff) | |
Add os.access function for file access checking
| -rw-r--r-- | core/src/xmake/engine.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/os/access.c | 25 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 11 |
3 files changed, 30 insertions, 8 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; +} diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index dcd5ab5d1..a752b7a42 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -46,6 +46,7 @@ os._getenvs = os._getenvs or os.getenvs os._cpuinfo = os._cpuinfo or os.cpuinfo os._meminfo = os._meminfo or os.meminfo os._readlink = os._readlink or os.readlink +os._access = os._access or os.access -- syserror code os.SYSERR_UNKNOWN = -1 @@ -1155,15 +1156,9 @@ function os.isexec(filepath) -- check executable program exist if os.isfile(filepath) then - return true - end - if os.host() == "windows" then - for _, suffix in ipairs({".exe", ".cmd", ".bat"}) do - if os.isfile(filepath .. suffix) then - return true - end - end + return os._access(filepath, "x") end + return false end -- get system host |
