aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Smedstad <carl.smedstad@protonmail.com>2021-04-05 20:48:25 +0200
committerHisham Muhammad <hisham@gobolinux.org>2021-04-06 17:24:57 -0300
commit09ed8630f74333eff0bb04ed1dd392fead048347 (patch)
tree7f6ba2c42dab53c1ea79f6200c9193a0a9387676
parent2360071b5a723b02ec80251b569dd3c5e2f9feaa (diff)
downloadluarocks-09ed8630f74333eff0bb04ed1dd392fead048347.tar.gz
luarocks-09ed8630f74333eff0bb04ed1dd392fead048347.tar.bz2
luarocks-09ed8630f74333eff0bb04ed1dd392fead048347.zip
Add function fs.is_superuser()
For environments other than UNIX, this will return false. Suggested by @hishamhm.
-rw-r--r--src/luarocks/cmd.lua4
-rw-r--r--src/luarocks/fs/lua.lua4
-rw-r--r--src/luarocks/fs/unix.lua4
-rw-r--r--src/luarocks/fs/win32.lua4
4 files changed, 14 insertions, 2 deletions
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
85 replace_tree(args, root_dir) 85 replace_tree(args, root_dir)
86 end 86 end
87 elseif args["local"] then 87 elseif args["local"] then
88 if os.getenv("USER") == "root" then 88 if fs.is_superuser() then
89 return nil, "The --local flag is meant for operating in a user's home directory.\n".. 89 return nil, "The --local flag is meant for operating in a user's home directory.\n"..
90 "You are running as a superuser, which is intended for system-wide operation.\n".. 90 "You are running as a superuser, which is intended for system-wide operation.\n"..
91 "To force using the superuser's home, use --tree explicitly." 91 "To force using the superuser's home, use --tree explicitly."
@@ -642,7 +642,7 @@ function cmd.run_command(description, commands, external_namespace, ...)
642 end 642 end
643 643
644 -- if running as superuser, use system cache dir 644 -- if running as superuser, use system cache dir
645 if os.getenv("USER") == "root" then 645 if fs.is_superuser() then
646 cfg.local_cache = dir.path(fs.system_cache_dir(), "luarocks") 646 cfg.local_cache = dir.path(fs.system_cache_dir(), "luarocks")
647 end 647 end
648 648
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()
1034 return posix.getpwuid(posix.geteuid()).pw_name 1034 return posix.getpwuid(posix.geteuid()).pw_name
1035end 1035end
1036 1036
1037function fs_lua.is_superuser()
1038 return false
1039end
1040
1037-- This call is not available on all systems, see #677 1041-- This call is not available on all systems, see #677
1038if posix.mkdtemp then 1042if posix.mkdtemp then
1039 1043
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()
179 return os.getenv("USER") 179 return os.getenv("USER")
180end 180end
181 181
182function unix.is_superuser()
183 return os.getenv("USER") == "root"
184end
185
182function unix.export_cmd(var, val) 186function unix.export_cmd(var, val)
183 return ("export %s='%s'"):format(var, val) 187 return ("export %s='%s'"):format(var, val)
184end 188end
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()
351 return os.getenv("USERNAME") 351 return os.getenv("USERNAME")
352end 352end
353 353
354function win32.is_superuser()
355 return false
356end
357
354function win32.export_cmd(var, val) 358function win32.export_cmd(var, val)
355 return ("SET %s=%s"):format(var, val) 359 return ("SET %s=%s"):format(var, val)
356end 360end