-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchmod.lua
More file actions
32 lines (32 loc) · 1.12 KB
/
chmod.lua
File metadata and controls
32 lines (32 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
-- Different mode syntax:
-- (user)?[+-=][rwxs]+(,(user)?[+-=][rwxs]+)*
-- Octal permissions are retained for compatibility, but group perms are ignored
local filesystem = require "system.filesystem"
local args = {...}
local recursive = false
if args[1] == "-R" then
recursive = true
table.remove(args, 1)
end
local mode = table.remove(args, 1)
local oct = tonumber(mode, 8)
local function setmode(file)
local stat = filesystem.stat(file, true)
if oct then
filesystem.chmod(file, stat.owner, bit32.band(bit32.rshift(oct, 6), 7))
filesystem.chmod(file, nil, bit32.band(oct, 7))
else
for perm in mode:gmatch "[^,]+" do
local user, arg = perm:match "^([^%+%-=]*)([%+%-=][rwxs]+)$"
if not user then error("chmod: invalid mode: " .. perm) end
if user == "" then user = nil end
filesystem.chmod(file, user, arg)
end
end
if recursive and stat.type == "directory" then
for _, v in ipairs(filesystem.list(file)) do
setmode(filesystem.combine(file, v))
end
end
end
for _, file in ipairs(args) do setmode(file) end