diff options
| author | ruki <[email protected]> | 2017-05-23 10:50:50 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-05-23 10:55:06 +0800 |
| commit | 5e5897bfb063f13282dabf6ecea7a1c3000664e4 (patch) | |
| tree | 1870c36844d68a6990d642071cfb71aaa64e65f3 | |
| parent | ffa57872e4e1df66db1df8174ffcde4ff0dcd296 (diff) | |
modify some code styles
| -rw-r--r-- | core/src/xmake/os/getown.c | 6 | ||||
| -rw-r--r-- | core/src/xmake/os/gid.c | 22 | ||||
| -rw-r--r-- | core/src/xmake/os/uid.c | 22 | ||||
| -rw-r--r-- | xmake/actions/install/main.lua | 2 | ||||
| -rw-r--r-- | xmake/actions/uninstall/main.lua | 2 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 15 | ||||
| -rw-r--r-- | xmake/core/base/privilege.lua | 9 |
7 files changed, 45 insertions, 33 deletions
diff --git a/core/src/xmake/os/getown.c b/core/src/xmake/os/getown.c index aa4e2de66..9a3889a1a 100644 --- a/core/src/xmake/os/getown.c +++ b/core/src/xmake/os/getown.c @@ -36,10 +36,14 @@ #ifndef TB_CONFIG_OS_WINDOWS # include <unistd.h> # include <sys/stat.h> +#endif /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ + +#ifndef TB_CONFIG_OS_WINDOWS + // get owner by a given path tb_int_t xm_os_getown(lua_State* lua) { @@ -52,7 +56,7 @@ tb_int_t xm_os_getown(lua_State* lua) // get stat struct stat sts; - if(stat(pathname, &sts) != 0) + if (stat(pathname, &sts) != 0) return 0; // push diff --git a/core/src/xmake/os/gid.c b/core/src/xmake/os/gid.c index 7d949ebc6..603aad8b1 100644 --- a/core/src/xmake/os/gid.c +++ b/core/src/xmake/os/gid.c @@ -36,10 +36,12 @@ #ifndef TB_CONFIG_OS_WINDOWS # include <unistd.h> # include <errno.h> +#endif /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ +#ifndef TB_CONFIG_OS_WINDOWS // get & set gid tb_int_t xm_os_gid(lua_State* lua) @@ -47,10 +49,9 @@ tb_int_t xm_os_gid(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - tb_int_t rgidset = -1, egidset = -1; - + tb_int_t rgidset = -1; + tb_int_t egidset = -1; tb_int_t argc = lua_gettop(lua); - if (argc == 1) { if (lua_istable(lua, 1)) @@ -80,17 +81,20 @@ tb_int_t xm_os_gid(lua_State* lua) rgidset = (tb_int_t)lua_tonumber(lua, -1); } lua_pop(lua, 1); - } else if (lua_isnumber(lua, 1)) + } + else if (lua_isnumber(lua, 1)) { // os.gid(gid) rgidset = egidset = (tb_int_t)lua_tonumber(lua, 1); - } else + } + else { lua_pushfstring(lua, "invalid argument type(%s) for os.gid", luaL_typename(lua, 1)); lua_error(lua); return 0; } - } else if (argc == 2) + } + else if (argc == 2) { // os.gid(rgid, egid) if (!lua_isnil(lua, 1)) @@ -113,7 +117,8 @@ tb_int_t xm_os_gid(lua_State* lua) } egidset = (tb_int_t)lua_tonumber(lua, 2); } - } else if (argc != 0) + } + else if (argc != 0) { lua_pushstring(lua, "invalid argument count for os.gid"); lua_error(lua); @@ -132,7 +137,8 @@ tb_int_t xm_os_gid(lua_State* lua) } // get gid & egid - gid_t gid = getgid(), egid = getegid(); + gid_t gid = getgid(); + gid_t egid = getegid(); // push lua_pushstring(lua, "rgid"); diff --git a/core/src/xmake/os/uid.c b/core/src/xmake/os/uid.c index 4bcc9d122..0c5c583c3 100644 --- a/core/src/xmake/os/uid.c +++ b/core/src/xmake/os/uid.c @@ -36,10 +36,12 @@ #ifndef TB_CONFIG_OS_WINDOWS # include <unistd.h> # include <errno.h> +#endif /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ +#ifndef TB_CONFIG_OS_WINDOWS // get & set uid tb_int_t xm_os_uid(lua_State* lua) @@ -47,10 +49,9 @@ tb_int_t xm_os_uid(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - tb_int_t ruidset = -1, euidset = -1; - + tb_int_t ruidset = -1; + tb_int_t euidset = -1; tb_int_t argc = lua_gettop(lua); - if (argc == 1) { if (lua_istable(lua, 1)) @@ -80,17 +81,20 @@ tb_int_t xm_os_uid(lua_State* lua) ruidset = (tb_int_t)lua_tonumber(lua, -1); } lua_pop(lua, 1); - } else if (lua_isnumber(lua, 1)) + } + else if (lua_isnumber(lua, 1)) { // os.uid(uid) ruidset = euidset = (tb_int_t)lua_tonumber(lua, 1); - } else + } + else { lua_pushfstring(lua, "invalid argument type(%s) for os.uid", luaL_typename(lua, 1)); lua_error(lua); return 0; } - } else if (argc == 2) + } + else if (argc == 2) { // os.uid(ruid, euid) if (!lua_isnil(lua, 1)) @@ -113,7 +117,8 @@ tb_int_t xm_os_uid(lua_State* lua) } euidset = (tb_int_t)lua_tonumber(lua, 2); } - } else if (argc != 0) + } + else if (argc != 0) { lua_pushstring(lua, "invalid argument count for os.uid"); lua_error(lua); @@ -132,7 +137,8 @@ tb_int_t xm_os_uid(lua_State* lua) } // get uid & euid - uid_t uid = getuid(), euid = geteuid(); + uid_t uid = getuid(); + uid_t euid = geteuid(); // push lua_pushstring(lua, "ruid"); diff --git a/xmake/actions/install/main.lua b/xmake/actions/install/main.lua index f9bca9419..ae98f3eea 100644 --- a/xmake/actions/install/main.lua +++ b/xmake/actions/install/main.lua @@ -63,6 +63,7 @@ function main() local ok = try { function () + -- install target install.install(targetname or ifelse(option.get("all"), "__all", "__def")) @@ -77,6 +78,7 @@ function main() -- release privilege privilege.store() + -- ok? if ok then return end end diff --git a/xmake/actions/uninstall/main.lua b/xmake/actions/uninstall/main.lua index 2b25a058d..a0460dbe2 100644 --- a/xmake/actions/uninstall/main.lua +++ b/xmake/actions/uninstall/main.lua @@ -63,6 +63,7 @@ function main() local ok = try { function () + -- uninstall target uninstall.uninstall(targetname) @@ -77,6 +78,7 @@ function main() -- release privilege privilege.store() + -- ok? if ok then return end end diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index ea6d3a9b0..5a496b414 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -651,22 +651,11 @@ function os.gid(...) return os._GID end --- check run command as root +-- check the current command is running as root function os.isroot() - os._ISROOT = nil -- check it - if os.uid().euid == 0 then - os._ISROOT = true - end - - -- not root? - if os._ISROOT == nil then - os._ISROOT = false - end - - -- root? - return os._ISROOT + return os.uid().euid == 0 end -- get version info diff --git a/xmake/core/base/privilege.lua b/xmake/core/base/privilege.lua index 275a7e7b5..96426cbf3 100644 --- a/xmake/core/base/privilege.lua +++ b/xmake/core/base/privilege.lua @@ -30,6 +30,7 @@ local os = require("base/os") -- store privilege function privilege.store() + -- check if root if not os.isroot() then return false @@ -40,8 +41,9 @@ function privilege.store() assert(projectdir) local owner = os.getown(projectdir) if not owner then + -- fallback to current dir - owner = os.getown(".") + owner = os.getown(os.curdir()) if not owner then return false end @@ -53,7 +55,7 @@ function privilege.store() end -- set uid - if os.uid({["ruid"] = owner.uid}).errno ~= 0 or os.uid({["euid"] = owner.uid}).errno ~= 0 then + if os.uid({ruid = owner.uid}).errno ~= 0 or os.uid({euid = owner.uid}).errno ~= 0 then return false end @@ -70,13 +72,14 @@ function privilege.has() end function privilege.get() + -- has? if privilege._HAS_PRIVILEGE ~= true then return false end -- set uid - if os.uid({["euid"] = 0}).errno ~= 0 or os.uid({["ruid"] = 0}).errno ~= 0 then + if os.uid({euid = 0}).errno ~= 0 or os.uid({ruid = 0}).errno ~= 0 then return false end |
