Skip to content

Commit 09ed863

Browse files
carlsmedstadhishamhm
authored andcommitted
Add function fs.is_superuser()
For environments other than UNIX, this will return false. Suggested by @hishamhm.
1 parent 2360071 commit 09ed863

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

src/luarocks/cmd.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ do
8585
replace_tree(args, root_dir)
8686
end
8787
elseif args["local"] then
88-
if os.getenv("USER") == "root" then
88+
if fs.is_superuser() then
8989
return nil, "The --local flag is meant for operating in a user's home directory.\n"..
9090
"You are running as a superuser, which is intended for system-wide operation.\n"..
9191
"To force using the superuser's home, use --tree explicitly."
@@ -642,7 +642,7 @@ function cmd.run_command(description, commands, external_namespace, ...)
642642
end
643643

644644
-- if running as superuser, use system cache dir
645-
if os.getenv("USER") == "root" then
645+
if fs.is_superuser() then
646646
cfg.local_cache = dir.path(fs.system_cache_dir(), "luarocks")
647647
end
648648

src/luarocks/fs/lua.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,10 @@ function fs_lua.current_user()
10341034
return posix.getpwuid(posix.geteuid()).pw_name
10351035
end
10361036

1037+
function fs_lua.is_superuser()
1038+
return false
1039+
end
1040+
10371041
-- This call is not available on all systems, see #677
10381042
if posix.mkdtemp then
10391043

src/luarocks/fs/unix.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ function unix.current_user()
179179
return os.getenv("USER")
180180
end
181181

182+
function unix.is_superuser()
183+
return os.getenv("USER") == "root"
184+
end
185+
182186
function unix.export_cmd(var, val)
183187
return ("export %s='%s'"):format(var, val)
184188
end

src/luarocks/fs/win32.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ function win32.current_user()
351351
return os.getenv("USERNAME")
352352
end
353353

354+
function win32.is_superuser()
355+
return false
356+
end
357+
354358
function win32.export_cmd(var, val)
355359
return ("SET %s=%s"):format(var, val)
356360
end

0 commit comments

Comments
 (0)