From 09ed8630f74333eff0bb04ed1dd392fead048347 Mon Sep 17 00:00:00 2001 From: Carl Smedstad Date: Mon, 5 Apr 2021 20:48:25 +0200 Subject: Add function fs.is_superuser() For environments other than UNIX, this will return false. Suggested by @hishamhm. --- src/luarocks/cmd.lua | 4 ++-- src/luarocks/fs/lua.lua | 4 ++++ src/luarocks/fs/unix.lua | 4 ++++ src/luarocks/fs/win32.lua | 4 ++++ 4 files changed, 14 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/luarocks/cmd.lua b/src/luarocks/cmd.lua index 9255cff8..bc965af4 100644 --- a/src/luarocks/cmd.lua +++ b/src/luarocks/cmd.lua @@ -85,7 +85,7 @@ do replace_tree(args, root_dir) end elseif args["local"] then - if os.getenv("USER") == "root" then + if fs.is_superuser() then return nil, "The --local flag is meant for operating in a user's home directory.\n".. "You are running as a superuser, which is intended for system-wide operation.\n".. "To force using the superuser's home, use --tree explicitly." @@ -642,7 +642,7 @@ function cmd.run_command(description, commands, external_namespace, ...) end -- if running as superuser, use system cache dir - if os.getenv("USER") == "root" then + if fs.is_superuser() then cfg.local_cache = dir.path(fs.system_cache_dir(), "luarocks") end diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 0e98449a..6bd80485 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -1034,6 +1034,10 @@ function fs_lua.current_user() return posix.getpwuid(posix.geteuid()).pw_name end +function fs_lua.is_superuser() + return false +end + -- This call is not available on all systems, see #677 if posix.mkdtemp then diff --git a/src/luarocks/fs/unix.lua b/src/luarocks/fs/unix.lua index d5b36127..2c66eaa3 100644 --- a/src/luarocks/fs/unix.lua +++ b/src/luarocks/fs/unix.lua @@ -179,6 +179,10 @@ function unix.current_user() return os.getenv("USER") end +function unix.is_superuser() + return os.getenv("USER") == "root" +end + function unix.export_cmd(var, val) return ("export %s='%s'"):format(var, val) end diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index 49de8e97..6d869d99 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua @@ -351,6 +351,10 @@ function win32.current_user() return os.getenv("USERNAME") end +function win32.is_superuser() + return false +end + function win32.export_cmd(var, val) return ("SET %s=%s"):format(var, val) end -- cgit v1.2.3-55-g6feb