diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2018-06-24 19:23:31 -0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2018-07-01 15:51:36 -0300 |
| commit | c9708d8eabf178f2fdea371a024045355ff00505 (patch) | |
| tree | 3f57085c3716a6728d2c899f63280aff8b78117b /src | |
| parent | bd7da4f5a06fd9f417d9963d9fcbb074a6176ba2 (diff) | |
| download | luarocks-c9708d8eabf178f2fdea371a024045355ff00505.tar.gz luarocks-c9708d8eabf178f2fdea371a024045355ff00505.tar.bz2 luarocks-c9708d8eabf178f2fdea371a024045355ff00505.zip | |
fs: add LuaPosix-version of set_permissions
This implemention the ugly side-effect of "exposing" some Unix-specific
utility functions to the public API, so they can be shared by
`luarocks.fs.lua` and `luarocks.fs.unix.tools`. I named those functions
`_unix_*` (with a Python-style `_` at the beginning) to clarify that they
should not be used publicly.
Diffstat (limited to 'src')
| -rw-r--r-- | src/luarocks/fs/lua.lua | 46 | ||||
| -rw-r--r-- | src/luarocks/fs/unix.lua | 40 | ||||
| -rw-r--r-- | src/luarocks/fs/unix/tools.lua | 57 |
3 files changed, 92 insertions, 51 deletions
diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 37e1bffc..b5232a52 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua | |||
| @@ -788,16 +788,46 @@ local octal_to_rwx = { | |||
| 788 | ["7"] = "rwx", | 788 | ["7"] = "rwx", |
| 789 | } | 789 | } |
| 790 | 790 | ||
| 791 | function fs_lua.chmod(file, mode) | 791 | do |
| 792 | -- LuaPosix (as of 5.1.15) does not support octal notation... | 792 | local umask_cache |
| 793 | if mode:sub(1,1) == "0" then | 793 | function fs_lua._unix_umask() |
| 794 | local new_mode = {} | 794 | if umask_cache then |
| 795 | for c in mode:sub(-3):gmatch(".") do | 795 | return umask_cache |
| 796 | table.insert(new_mode, octal_to_rwx[c]) | 796 | end |
| 797 | -- LuaPosix (as of 34.0.4) only returns the umask as rwx | ||
| 798 | local rwx = posix.umask() | ||
| 799 | local oct = 0 | ||
| 800 | for i = 1, 9 do | ||
| 801 | if rwx:sub(10 - i, 10 - i) == "-" then | ||
| 802 | oct = oct + 2^i | ||
| 803 | end | ||
| 797 | end | 804 | end |
| 798 | mode = table.concat(new_mode) | 805 | umask_cache = ("%03o"):format(oct) |
| 806 | return umask_cache | ||
| 807 | end | ||
| 808 | end | ||
| 809 | |||
| 810 | function fs_lua.set_permissions(filename, mode, scope) | ||
| 811 | local perms | ||
| 812 | if mode == "read" and scope == "user" then | ||
| 813 | perms = fs._unix_moderate_permissions("600") | ||
| 814 | elseif mode == "exec" and scope == "user" then | ||
| 815 | perms = fs._unix_moderate_permissions("700") | ||
| 816 | elseif mode == "read" and scope == "all" then | ||
| 817 | perms = fs._unix_moderate_permissions("644") | ||
| 818 | elseif mode == "exec" and scope == "all" then | ||
| 819 | perms = fs._unix_moderate_permissions("755") | ||
| 820 | else | ||
| 821 | return false, "Invalid permission " .. mode .. " for " .. scope | ||
| 822 | end | ||
| 823 | |||
| 824 | -- LuaPosix (as of 5.1.15) does not support octal notation... | ||
| 825 | local new_perms = {} | ||
| 826 | for c in perms:sub(-3):gmatch(".") do | ||
| 827 | table.insert(new_perms, octal_to_rwx[c]) | ||
| 799 | end | 828 | end |
| 800 | local err = posix.chmod(file, mode) | 829 | perms = table.concat(new_perms) |
| 830 | local err = posix.chmod(filename, perms) | ||
| 801 | return err == 0 | 831 | return err == 0 |
| 802 | end | 832 | end |
| 803 | 833 | ||
diff --git a/src/luarocks/fs/unix.lua b/src/luarocks/fs/unix.lua index dbe48cca..4ca68ce5 100644 --- a/src/luarocks/fs/unix.lua +++ b/src/luarocks/fs/unix.lua | |||
| @@ -165,4 +165,44 @@ function unix.export_cmd(var, val) | |||
| 165 | return ("export %s='%s'"):format(var, val) | 165 | return ("export %s='%s'"):format(var, val) |
| 166 | end | 166 | end |
| 167 | 167 | ||
| 168 | --- Moderate the given permissions based on the local umask | ||
| 169 | -- @param perms string: permissions to moderate | ||
| 170 | -- @return string: the moderated permissions | ||
| 171 | function unix._unix_moderate_permissions(perms) | ||
| 172 | local octal_to_rwx = { | ||
| 173 | ["0"] = "---", | ||
| 174 | ["1"] = "--x", | ||
| 175 | ["2"] = "-w-", | ||
| 176 | ["3"] = "-wx", | ||
| 177 | ["4"] = "r--", | ||
| 178 | ["5"] = "r-x", | ||
| 179 | ["6"] = "rw-", | ||
| 180 | ["7"] = "rwx", | ||
| 181 | } | ||
| 182 | local rwx_to_octal = {} | ||
| 183 | for octal, rwx in pairs(octal_to_rwx) do | ||
| 184 | rwx_to_octal[rwx] = octal | ||
| 185 | end | ||
| 186 | |||
| 187 | local umask = fs._unix_umask() | ||
| 188 | |||
| 189 | local moderated_perms = "" | ||
| 190 | for i = 1, 3 do | ||
| 191 | local p_rwx = octal_to_rwx[perms:sub(i, i)] | ||
| 192 | local u_rwx = octal_to_rwx[umask:sub(i, i)] | ||
| 193 | local new_perm = "" | ||
| 194 | for j = 1, 3 do | ||
| 195 | local p_val = p_rwx:sub(j, j) | ||
| 196 | local u_val = u_rwx:sub(j, j) | ||
| 197 | if p_val == u_val then | ||
| 198 | new_perm = new_perm .. "-" | ||
| 199 | else | ||
| 200 | new_perm = new_perm .. p_val | ||
| 201 | end | ||
| 202 | end | ||
| 203 | moderated_perms = moderated_perms .. rwx_to_octal[new_perm] | ||
| 204 | end | ||
| 205 | return moderated_perms | ||
| 206 | end | ||
| 207 | |||
| 168 | return unix | 208 | return unix |
diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 06098399..e0b0d22a 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua | |||
| @@ -160,47 +160,18 @@ function tools.is_file(file) | |||
| 160 | return fs.execute(vars.TEST, "-f", file) | 160 | return fs.execute(vars.TEST, "-f", file) |
| 161 | end | 161 | end |
| 162 | 162 | ||
| 163 | --- Moderate the given permissions based on the local umask | 163 | do |
| 164 | -- @param perms string: permissions to moderate | 164 | local umask_cache |
| 165 | -- @return string: the moderated permissions | 165 | function tools._unix_umask() |
| 166 | local function moderate_permissions(perms) | 166 | if umask_cache then |
| 167 | local octal_to_rwx = { | 167 | return umask_cache |
| 168 | ["0"] = "---", | ||
| 169 | ["1"] = "--x", | ||
| 170 | ["2"] = "-w-", | ||
| 171 | ["3"] = "-wx", | ||
| 172 | ["4"] = "r--", | ||
| 173 | ["5"] = "r-x", | ||
| 174 | ["6"] = "rw-", | ||
| 175 | ["7"] = "rwx", | ||
| 176 | } | ||
| 177 | local rwx_to_octal = {} | ||
| 178 | for octal, rwx in pairs(octal_to_rwx) do | ||
| 179 | rwx_to_octal[rwx] = octal | ||
| 180 | end | ||
| 181 | |||
| 182 | local fd = assert(io.popen("umask")) | ||
| 183 | local umask = assert(fd:read("*a")) | ||
| 184 | umask = umask:gsub("\n", "") | ||
| 185 | umask = umask:sub(2, 4) | ||
| 186 | |||
| 187 | local moderated_perms = "" | ||
| 188 | for i = 1, 3 do | ||
| 189 | local p_rwx = octal_to_rwx[perms:sub(i, i)] | ||
| 190 | local u_rwx = octal_to_rwx[umask:sub(i, i)] | ||
| 191 | local new_perm = "" | ||
| 192 | for j = 1, 3 do | ||
| 193 | local p_val = p_rwx:sub(j, j) | ||
| 194 | local u_val = u_rwx:sub(j, j) | ||
| 195 | if p_val == u_val then | ||
| 196 | new_perm = new_perm .. "-" | ||
| 197 | else | ||
| 198 | new_perm = new_perm .. p_val | ||
| 199 | end | ||
| 200 | end | 168 | end |
| 201 | moderated_perms = moderated_perms .. rwx_to_octal[new_perm] | 169 | local fd = assert(io.popen("umask")) |
| 170 | local umask = assert(fd:read("*a")) | ||
| 171 | umask = umask:gsub("\n", "") | ||
| 172 | umask_cache = umask:sub(2, 4) | ||
| 173 | return umask_cache | ||
| 202 | end | 174 | end |
| 203 | return moderated_perms | ||
| 204 | end | 175 | end |
| 205 | 176 | ||
| 206 | --- Set permissions for file or directory | 177 | --- Set permissions for file or directory |
| @@ -214,13 +185,13 @@ function tools.set_permissions(filename, mode, scope) | |||
| 214 | 185 | ||
| 215 | local perms | 186 | local perms |
| 216 | if mode == "read" and scope == "user" then | 187 | if mode == "read" and scope == "user" then |
| 217 | perms = moderate_permissions("600") | 188 | perms = fs._unix_moderate_permissions("600") |
| 218 | elseif mode == "exec" and scope == "user" then | 189 | elseif mode == "exec" and scope == "user" then |
| 219 | perms = moderate_permissions("700") | 190 | perms = fs._unix_moderate_permissions("700") |
| 220 | elseif mode == "read" and scope == "all" then | 191 | elseif mode == "read" and scope == "all" then |
| 221 | perms = moderate_permissions("644") | 192 | perms = fs._unix_moderate_permissions("644") |
| 222 | elseif mode == "exec" and scope == "all" then | 193 | elseif mode == "exec" and scope == "all" then |
| 223 | perms = moderate_permissions("755") | 194 | perms = fs._unix_moderate_permissions("755") |
| 224 | else | 195 | else |
| 225 | return false, "Invalid permission " .. mode .. " for " .. scope | 196 | return false, "Invalid permission " .. mode .. " for " .. scope |
| 226 | end | 197 | end |
