Skip to content

Commit 65c53db

Browse files
DerpDaysstevearc
andauthored
fix: correctly check group permissions in unix (#428)
* fix: set modifiable when user in group * feat: add mode caching, fallback to previous, and better checking of permissions * fix: make is_modifiable check group permissions even if the user is owner of the directory * refactor: simplify group ID caching --------- Co-authored-by: Steven Arcangeli <[email protected]>
1 parent f6df58a commit 65c53db

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

lua/oil/adapters/files.lua

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,26 @@ M.list = function(url, column_defs, cb)
412412
end, 10000)
413413
end
414414

415+
---@type nil|integer[]
416+
local _group_ids
417+
---@return integer[]
418+
local function get_group_ids()
419+
if not _group_ids then
420+
local output = vim.fn.system({ "id", "-G" })
421+
if vim.v.shell_error == 0 then
422+
_group_ids = vim.tbl_map(tonumber, vim.split(output, "%s+", { trimempty = true }))
423+
else
424+
-- If the id command fails, fall back to just using the process group
425+
_group_ids = { uv.getgid() }
426+
vim.notify(
427+
"[oil] missing the `id` command. Some directories may not be modifiable even if you have group access.",
428+
vim.log.levels.WARN
429+
)
430+
end
431+
end
432+
return _group_ids
433+
end
434+
415435
---@param bufnr integer
416436
---@return boolean
417437
M.is_modifiable = function(bufnr)
@@ -433,14 +453,12 @@ M.is_modifiable = function(bufnr)
433453
end
434454

435455
local uid = uv.getuid()
436-
local gid = uv.getgid()
437-
local rwx
456+
local rwx = stat.mode
438457
if uid == stat.uid then
439-
rwx = bit.rshift(stat.mode, 6)
440-
elseif gid == stat.gid then
441-
rwx = bit.rshift(stat.mode, 3)
442-
else
443-
rwx = stat.mode
458+
rwx = bit.bor(rwx, bit.rshift(stat.mode, 6))
459+
end
460+
if vim.tbl_contains(get_group_ids(), stat.gid) then
461+
rwx = bit.bor(rwx, bit.rshift(stat.mode, 3))
444462
end
445463
return bit.band(rwx, 2) ~= 0
446464
end

0 commit comments

Comments
 (0)