From 0b80c36bc1dfe85c882918b30f45d3a3fdf0e5ce Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Tue, 2 Apr 2019 11:30:19 -0300 Subject: Simplify issue with superuser cache and avoid annoying message When running as root, simply switch to use /var/cache/luarocks as a cache. --- src/luarocks/cmd.lua | 28 +++------------------------- src/luarocks/core/cfg.lua | 7 ------- src/luarocks/fs/lua.lua | 20 ++++++-------------- src/luarocks/fs/unix.lua | 7 +++++++ src/luarocks/fs/unix/tools.lua | 13 ------------- src/luarocks/fs/win32.lua | 13 ++++--------- 6 files changed, 20 insertions(+), 68 deletions(-) (limited to 'src') diff --git a/src/luarocks/cmd.lua b/src/luarocks/cmd.lua index a46b0722..93bc5525 100644 --- a/src/luarocks/cmd.lua +++ b/src/luarocks/cmd.lua @@ -23,21 +23,6 @@ cmd.errorcodes = { CRASH = 99 } -local function is_ownership_ok(directory) - local me = fs.current_user() - if not me then - return nil, "can't determine current user's name" - end - for _ = 1,3 do -- try up to grandparent - local owner = fs.attributes(directory, "owner") - if owner then - return owner == me - end - directory = dir.dir_name(directory) - end - return false -end - local function check_popen() local popen_ok, popen_result = pcall(io.popen, "") if popen_ok then @@ -393,16 +378,9 @@ function cmd.run_command(description, commands, external_namespace, ...) end end - local user_owns_local_cache = is_ownership_ok(cfg.local_cache) - if user_owns_local_cache == false then - util.warning("The directory '" .. cfg.local_cache .. "' or its parent directory ".. - "is not owned by the current user and the cache has been disabled. ".. - "Please check the permissions and owner of that directory. ".. - (cfg.is_platform("unix") - and ("If executing "..util.this_program("luarocks").." with sudo, you may want sudo's -H flag.") - or "")) - cfg.local_cache = fs.make_temp_dir("local_cache") - util.schedule_function(fs.delete, cfg.local_cache) + -- if running as superuser, use system cache dir + if not cfg.home_tree then + cfg.local_cache = dir.path(fs.system_cache_dir(), "luarocks") end if commands[command] then diff --git a/src/luarocks/core/cfg.lua b/src/luarocks/core/cfg.lua index 859f77e0..0c2e3f67 100644 --- a/src/luarocks/core/cfg.lua +++ b/src/luarocks/core/cfg.lua @@ -259,15 +259,12 @@ local function make_defaults(lua_version, target_cpu, platforms, home) MD5SUM = "md5sum", OPENSSL = "openssl", MD5 = "md5", - STAT = "stat", TOUCH = "touch", CMAKE = "cmake", SEVENZ = "7z", RSYNCFLAGS = "--exclude=.git -Oavz", - STATPERMFLAG = "-c '%a'", - STATOWNERFLAG = "-c '%U'", CURLNOCERTFLAG = "", WGETNOCERTFLAG = "", }, @@ -419,8 +416,6 @@ local function make_defaults(lua_version, target_cpu, platforms, home) if platforms.bsd then defaults.variables.MAKE = "gmake" - defaults.variables.STATPERMFLAG = "-f '%OLp'" - defaults.variables.STATOWNERFLAG = "-f '%Su'" end if platforms.macosx then @@ -428,8 +423,6 @@ local function make_defaults(lua_version, target_cpu, platforms, home) defaults.external_lib_extension = "dylib" defaults.arch = "macosx-"..target_cpu defaults.variables.LIBFLAG = "-bundle -undefined dynamic_lookup -all_load" - defaults.variables.STAT = "/usr/bin/stat" - defaults.variables.STATFLAG = "-f '%A'" local version = util.popen_read("sw_vers -productVersion") version = tonumber(version and version:match("^[^.]+%.([^.]+)")) or 3 if version >= 10 then diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index c3bf87e3..6ad2b1b8 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -243,6 +243,10 @@ function fs_lua.filter_file(fn, input_filename, output_filename) return true end +function fs_lua.system_temp_dir() + return os.getenv("TMPDIR") or os.getenv("TEMP") or "/tmp" +end + --------------------------------------------------------------------- -- LuaFileSystem functions --------------------------------------------------------------------- @@ -986,18 +990,6 @@ function fs_lua.set_permissions(filename, mode, scope) return err == 0 end -function fs_lua.attributes(file, attrtype) - if attrtype == "permissions" then - return posix.stat(file, "mode") or nil - elseif attrtype == "owner" then - local uid = posix.stat(file, "uid") - if not uid then return nil end - return posix.getpwuid(uid).pw_name or nil - else - return nil - end -end - function fs_lua.current_user() return posix.getpwuid(posix.geteuid()).pw_name end @@ -1013,7 +1005,7 @@ function fs_lua.make_temp_dir(name_pattern) assert(type(name_pattern) == "string") name_pattern = dir.normalize(name_pattern) - return posix.mkdtemp((os.getenv("TMPDIR") or "/tmp") .. "/luarocks_" .. name_pattern:gsub("/", "_") .. "-XXXXXX") + return posix.mkdtemp(fs.system_temp_dir() .. "/luarocks_" .. name_pattern:gsub("/", "_") .. "-XXXXXX") end end -- if posix.mkdtemp @@ -1030,7 +1022,7 @@ function fs_lua.make_temp_dir(name_pattern) assert(type(name_pattern) == "string") name_pattern = dir.normalize(name_pattern) - local pattern = (os.getenv("TMPDIR") or os.getenv("TEMP") or "/tmp") .. "/luarocks_" .. name_pattern:gsub("/", "_") .. "-" + local pattern = fs.system_temp_dir() .. "/luarocks_" .. name_pattern:gsub("/", "_") .. "-" while true do local name = pattern .. tostring(math.random(10000000)) diff --git a/src/luarocks/fs/unix.lua b/src/luarocks/fs/unix.lua index 849259e8..075581e4 100644 --- a/src/luarocks/fs/unix.lua +++ b/src/luarocks/fs/unix.lua @@ -245,4 +245,11 @@ function unix.is_file(file) return false end +function unix.system_cache_dir() + if fs.is_dir("/var/cache") then + return "/var/cache" + end + return dir.path(fs.system_temp_dir(), "cache") +end + return unix diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 9a2f99a7..65b5681c 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -233,19 +233,6 @@ function tools.set_permissions(filename, mode, scope) return fs.execute(vars.CHMOD, perms, filename) end -function tools.attributes(filename, attrtype) - local flag = ((attrtype == "permissions") and vars.STATPERMFLAG) - or ((attrtype == "owner") and vars.STATOWNERFLAG) - if not flag then return "" end - local pipe = io.popen(fs.quiet_stderr(vars.STAT.." "..flag.." "..fs.Q(filename))) - local ret = pipe:read("*l") - pipe:close() - if ret == "" then - return nil - end - return ret -end - function tools.browser(url) return fs.execute(cfg.web_browser, url) end diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index 498eceb7..f66466fc 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua @@ -209,15 +209,6 @@ function win32.copy_binary(filename, dest) return true end -function win32.attributes(filename, attrtype) - if attrtype == "permissions" then - return "" -- FIXME - elseif attrtype == "owner" then - return os.getenv("USERNAME") -- FIXME popen_read('powershell -Command "& {(get-acl '..filename..').owner}"'):gsub("^[^\\]*\\", "") - end - return nil -end - --- Move a file on top of the other. -- The new file ceases to exist under its original name, -- and takes over the name of the old file. @@ -329,4 +320,8 @@ function win32.export_cmd(var, val) return ("SET %s=%s"):format(var, val) end +function win32.system_cache_dir() + return dir.path(fs.system_temp_dir(), "cache") +end + return win32 -- cgit v1.2.3-55-g6feb