diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2018-07-25 19:22:16 -0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2018-07-26 10:13:02 -0300 |
| commit | 842d7342eb12d133fa85514d801983095300e76c (patch) | |
| tree | 1434ecffe84bd8e90bd5cb64cdbda96caf90271b | |
| parent | 63a8fb17b00b8b60419ad34dae31a4143f744be5 (diff) | |
| download | luarocks-842d7342eb12d133fa85514d801983095300e76c.tar.gz luarocks-842d7342eb12d133fa85514d801983095300e76c.tar.bz2 luarocks-842d7342eb12d133fa85514d801983095300e76c.zip | |
fs: fix rwx_to_number, add negated mask mode
Fixes #856.
| -rw-r--r-- | spec/fs_spec.lua | 22 | ||||
| -rw-r--r-- | src/luarocks/fs/lua.lua | 24 |
2 files changed, 35 insertions, 11 deletions
diff --git a/spec/fs_spec.lua b/spec/fs_spec.lua index 5bec6168..3ed22fbe 100644 --- a/spec/fs_spec.lua +++ b/spec/fs_spec.lua | |||
| @@ -1463,4 +1463,26 @@ describe("Luarocks fs test #unit", function() | |||
| 1463 | assert.same(fs.modules("lib"), {}) | 1463 | assert.same(fs.modules("lib"), {}) |
| 1464 | end) | 1464 | end) |
| 1465 | end) | 1465 | end) |
| 1466 | |||
| 1467 | describe("#unix fs._unix_rwx_to_number", function() | ||
| 1468 | |||
| 1469 | it("converts permissions in rwx notation to numeric ones", function() | ||
| 1470 | assert.same(tonumber("0644", 8), fs._unix_rwx_to_number("rw-r--r--")) | ||
| 1471 | assert.same(tonumber("0755", 8), fs._unix_rwx_to_number("rwxr-xr-x")) | ||
| 1472 | assert.same(tonumber("0000", 8), fs._unix_rwx_to_number("---------")) | ||
| 1473 | assert.same(tonumber("0777", 8), fs._unix_rwx_to_number("rwxrwxrwx")) | ||
| 1474 | assert.same(tonumber("0700", 8), fs._unix_rwx_to_number("rwx------")) | ||
| 1475 | assert.same(tonumber("0600", 8), fs._unix_rwx_to_number("rw-------")) | ||
| 1476 | end) | ||
| 1477 | |||
| 1478 | it("produces a negated mask if asked to", function() | ||
| 1479 | assert.same(tonumber("0133", 8), fs._unix_rwx_to_number("rw-r--r--", true)) | ||
| 1480 | assert.same(tonumber("0022", 8), fs._unix_rwx_to_number("rwxr-xr-x", true)) | ||
| 1481 | assert.same(tonumber("0777", 8), fs._unix_rwx_to_number("---------", true)) | ||
| 1482 | assert.same(tonumber("0000", 8), fs._unix_rwx_to_number("rwxrwxrwx", true)) | ||
| 1483 | assert.same(tonumber("0077", 8), fs._unix_rwx_to_number("rwx------", true)) | ||
| 1484 | assert.same(tonumber("0177", 8), fs._unix_rwx_to_number("rw-------", true)) | ||
| 1485 | end) | ||
| 1486 | end) | ||
| 1487 | |||
| 1466 | end) | 1488 | end) |
diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 74d8f12f..13b46e19 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua | |||
| @@ -871,6 +871,18 @@ end | |||
| 871 | -- POSIX functions | 871 | -- POSIX functions |
| 872 | --------------------------------------------------------------------- | 872 | --------------------------------------------------------------------- |
| 873 | 873 | ||
| 874 | function fs_lua._unix_rwx_to_number(rwx, neg) | ||
| 875 | local num = 0 | ||
| 876 | neg = neg or false | ||
| 877 | for i = 1, 9 do | ||
| 878 | local c = rwx:sub(10 - i, 10 - i) == "-" | ||
| 879 | if neg == c then | ||
| 880 | num = num + 2^(i-1) | ||
| 881 | end | ||
| 882 | end | ||
| 883 | return math.floor(num) | ||
| 884 | end | ||
| 885 | |||
| 874 | if posix_ok then | 886 | if posix_ok then |
| 875 | 887 | ||
| 876 | local octal_to_rwx = { | 888 | local octal_to_rwx = { |
| @@ -884,16 +896,6 @@ local octal_to_rwx = { | |||
| 884 | ["7"] = "rwx", | 896 | ["7"] = "rwx", |
| 885 | } | 897 | } |
| 886 | 898 | ||
| 887 | function fs_lua._unix_rwx_to_number(rwx) | ||
| 888 | local num = 0 | ||
| 889 | for i = 1, 9 do | ||
| 890 | if rwx:sub(10 - i, 10 - i) == "-" then | ||
| 891 | num = num + 2^i | ||
| 892 | end | ||
| 893 | end | ||
| 894 | return num | ||
| 895 | end | ||
| 896 | |||
| 897 | do | 899 | do |
| 898 | local umask_cache | 900 | local umask_cache |
| 899 | function fs_lua._unix_umask() | 901 | function fs_lua._unix_umask() |
| @@ -902,7 +904,7 @@ do | |||
| 902 | end | 904 | end |
| 903 | -- LuaPosix (as of 34.0.4) only returns the umask as rwx | 905 | -- LuaPosix (as of 34.0.4) only returns the umask as rwx |
| 904 | local rwx = posix.umask() | 906 | local rwx = posix.umask() |
| 905 | local num = fs_lua._unix_rwx_to_number(rwx) | 907 | local num = fs_lua._unix_rwx_to_number(rwx, true) |
| 906 | umask_cache = ("%03o"):format(num) | 908 | umask_cache = ("%03o"):format(num) |
| 907 | return umask_cache | 909 | return umask_cache |
| 908 | end | 910 | end |
