aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2018-07-25 19:22:16 -0300
committerHisham Muhammad <hisham@gobolinux.org>2018-07-26 10:13:02 -0300
commit842d7342eb12d133fa85514d801983095300e76c (patch)
tree1434ecffe84bd8e90bd5cb64cdbda96caf90271b
parent63a8fb17b00b8b60419ad34dae31a4143f744be5 (diff)
downloadluarocks-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.lua22
-rw-r--r--src/luarocks/fs/lua.lua24
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
1466end) 1488end)
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
874function 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)
884end
885
874if posix_ok then 886if posix_ok then
875 887
876local octal_to_rwx = { 888local octal_to_rwx = {
@@ -884,16 +896,6 @@ local octal_to_rwx = {
884 ["7"] = "rwx", 896 ["7"] = "rwx",
885} 897}
886 898
887function 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
895end
896
897do 899do
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