From e930ef7868056f1eb666dc3179ef7f7ce094d805 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Tue, 14 May 2024 14:59:56 -0300 Subject: fs(unix): honor umask correctly --- src/luarocks/fs/lua.lua | 14 +++----------- src/luarocks/fs/unix.lua | 18 +++++++++++++++++- src/luarocks/fs/unix/tools.lua | 15 ++++----------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 6e8c4670..4016ddcd 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -1070,17 +1070,9 @@ do end function fs_lua.set_permissions(filename, mode, scope) - local perms - if mode == "read" and scope == "user" then - perms = fs._unix_moderate_permissions("600") - elseif mode == "exec" and scope == "user" then - perms = fs._unix_moderate_permissions("700") - elseif mode == "read" and scope == "all" then - perms = fs._unix_moderate_permissions("644") - elseif mode == "exec" and scope == "all" then - perms = fs._unix_moderate_permissions("755") - else - return false, "Invalid permission " .. mode .. " for " .. scope + local perms, err = fs._unix_mode_scope_to_perms(mode, scope) + if err then + return false, err end -- LuaPosix (as of 5.1.15) does not support octal notation... diff --git a/src/luarocks/fs/unix.lua b/src/luarocks/fs/unix.lua index f5f3b349..41a9ba8b 100644 --- a/src/luarocks/fs/unix.lua +++ b/src/luarocks/fs/unix.lua @@ -197,7 +197,7 @@ end --- Moderate the given permissions based on the local umask -- @param perms string: permissions to moderate -- @return string: the moderated permissions -function unix._unix_moderate_permissions(perms) +local function apply_umask(perms) local umask = fs._unix_umask() local moderated_perms = "" @@ -219,6 +219,22 @@ function unix._unix_moderate_permissions(perms) return moderated_perms end +function unix._unix_mode_scope_to_perms(mode, scope) + local perms + if mode == "read" and scope == "user" then + perms = apply_umask("600") + elseif mode == "exec" and scope == "user" then + perms = apply_umask("700") + elseif mode == "read" and scope == "all" then + perms = apply_umask("666") + elseif mode == "exec" and scope == "all" then + perms = apply_umask("777") + else + return false, "Invalid permission " .. mode .. " for " .. scope + end + return perms +end + function unix.system_cache_dir() if fs.is_dir("/var/cache") then return "/var/cache" diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 16b31335..d7334733 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -221,18 +221,11 @@ end function tools.set_permissions(filename, mode, scope) assert(filename and mode and scope) - local perms - if mode == "read" and scope == "user" then - perms = fs._unix_moderate_permissions("600") - elseif mode == "exec" and scope == "user" then - perms = fs._unix_moderate_permissions("700") - elseif mode == "read" and scope == "all" then - perms = fs._unix_moderate_permissions("644") - elseif mode == "exec" and scope == "all" then - perms = fs._unix_moderate_permissions("755") - else - return false, "Invalid permission " .. mode .. " for " .. scope + local perms, err = fs._unix_mode_scope_to_perms(mode, scope) + if err then + return false, err end + return fs.execute(vars.CHMOD, perms, filename) end -- cgit v1.2.3-55-g6feb