From ec44db639c2a84e488abf7f7fbd31872afadcb2c Mon Sep 17 00:00:00 2001
From: Hisham Muhammad <hisham@gobolinux.org>
Date: Wed, 28 Feb 2024 20:04:39 -0300
Subject: feat: better error message when lacking permissions

The lock error message has clobbered the check_command_permissions
error message. This is an interim solution, but we should probably
move the permissions check further up for a more informative error.
---
 src/luarocks/cmd.lua           | 13 ++++++++-----
 src/luarocks/fs/unix/tools.lua |  5 ++++-
 2 files changed, 12 insertions(+), 6 deletions(-)

(limited to 'src')

diff --git a/src/luarocks/cmd.lua b/src/luarocks/cmd.lua
index ca22c0db..b6d0d47e 100644
--- a/src/luarocks/cmd.lua
+++ b/src/luarocks/cmd.lua
@@ -732,12 +732,15 @@ function cmd.run_command(description, commands, external_namespace, ...)
    if cmd_mod.needs_lock and cmd_mod.needs_lock(args) then
       lock, err = fs.lock_access(path.root_dir(cfg.root_dir), args.force_lock)
       if not lock then
-         local try_force = args.force_lock
-                           and (" - failed to force the lock" .. (err and ": " .. err or ""))
-                           or  " - use --force-lock to overwrite the lock"
+         err = args.force_lock
+               and ("failed to force the lock" .. (err and ": " .. err or ""))
+               or  (err and err ~= "File exists")
+                   and err
+                   or  "try --force-lock to overwrite the lock"
+
          die("command '" .. args.command .. "' " ..
-             "requires exclusive access to " .. path.root_dir(cfg.root_dir) ..
-             try_force, cmd.errorcodes.LOCK)
+             "requires exclusive write access to " .. path.root_dir(cfg.root_dir) .. " - " ..
+             err, cmd.errorcodes.LOCK)
       end
    end
 
diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua
index efd4d624..a0fba784 100644
--- a/src/luarocks/fs/unix/tools.lua
+++ b/src/luarocks/fs/unix/tools.lua
@@ -319,7 +319,10 @@ function tools.is_superuser()
 end
 
 function tools.lock_access(dirname, force)
-   fs.make_dir(dirname)
+   local ok, err = fs.make_dir(dirname)
+   if not ok then
+      return nil, err
+   end
 
    local tempfile = dir.path(dirname, ".lock.tmp." .. tostring(math.random(100000000)))
 
-- 
cgit v1.2.3-55-g6feb