diff options
| author | George Roman <george.roman.99@gmail.com> | 2018-07-07 16:23:07 +0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2018-07-16 20:34:30 -0300 |
| commit | 1b3b6525a4313404af84fce0fbbc29695e664f73 (patch) | |
| tree | 76ad679d512e216ace739662ac60d1785d43af2c | |
| parent | f2772aee0d4ee5012d9b1be65f685b535d7380bd (diff) | |
| download | luarocks-1b3b6525a4313404af84fce0fbbc29695e664f73.tar.gz luarocks-1b3b6525a4313404af84fce0fbbc29695e664f73.tar.bz2 luarocks-1b3b6525a4313404af84fce0fbbc29695e664f73.zip | |
Update the behavior of fs.set_permissions on Windows
| -rw-r--r-- | spec/fs_spec.lua | 8 | ||||
| -rw-r--r-- | src/luarocks/fs/win32/tools.lua | 57 |
2 files changed, 45 insertions, 20 deletions
diff --git a/spec/fs_spec.lua b/spec/fs_spec.lua index 3b4e6bac..eb8425e2 100644 --- a/spec/fs_spec.lua +++ b/spec/fs_spec.lua | |||
| @@ -31,7 +31,7 @@ describe("Luarocks fs test #unit", function() | |||
| 31 | 31 | ||
| 32 | local make_unreadable = function(path) | 32 | local make_unreadable = function(path) |
| 33 | if is_win then | 33 | if is_win then |
| 34 | fs.execute("icacls " .. fs.Q(path) .. " /deny %USERNAME%:(RD)") | 34 | fs.execute("icacls " .. fs.Q(path) .. " /inheritance:d /deny %USERNAME%:(R)") |
| 35 | else | 35 | else |
| 36 | fs.execute("chmod -r " .. fs.Q(path)) | 36 | fs.execute("chmod -r " .. fs.Q(path)) |
| 37 | end | 37 | end |
| @@ -39,7 +39,7 @@ describe("Luarocks fs test #unit", function() | |||
| 39 | 39 | ||
| 40 | local make_unwritable = function(path) | 40 | local make_unwritable = function(path) |
| 41 | if is_win then | 41 | if is_win then |
| 42 | fs.execute("icacls " .. fs.Q(path) .. " /deny %USERNAME%:(WD,AD)") | 42 | fs.execute("icacls " .. fs.Q(path) .. " /inheritance:d /deny %USERNAME%:(W,M)") |
| 43 | else | 43 | else |
| 44 | fs.execute("chmod -w " .. fs.Q(path)) | 44 | fs.execute("chmod -w " .. fs.Q(path)) |
| 45 | end | 45 | end |
| @@ -47,12 +47,12 @@ describe("Luarocks fs test #unit", function() | |||
| 47 | 47 | ||
| 48 | local make_unexecutable = function(path) | 48 | local make_unexecutable = function(path) |
| 49 | if is_win then | 49 | if is_win then |
| 50 | fs.execute("icacls " .. fs.Q(path) .. " /deny %USERNAME%:(X)") | 50 | fs.execute("icacls " .. fs.Q(path) .. " /inheritance:d /deny %USERNAME%:(X)") |
| 51 | else | 51 | else |
| 52 | fs.execute("chmod -x " .. fs.Q(path)) | 52 | fs.execute("chmod -x " .. fs.Q(path)) |
| 53 | end | 53 | end |
| 54 | end | 54 | end |
| 55 | 55 | ||
| 56 | local runner | 56 | local runner |
| 57 | 57 | ||
| 58 | setup(function() | 58 | setup(function() |
diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index c03b0d7b..c267b316 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua | |||
| @@ -182,33 +182,58 @@ end | |||
| 182 | function tools.set_permissions(filename, mode, scope) | 182 | function tools.set_permissions(filename, mode, scope) |
| 183 | assert(filename and mode and scope) | 183 | assert(filename and mode and scope) |
| 184 | 184 | ||
| 185 | local who, what | ||
| 186 | if scope == "user" then | 185 | if scope == "user" then |
| 187 | who = os.getenv("USERNAME") | 186 | local perms |
| 188 | elseif scope == "all" then | 187 | if mode == "read" then |
| 189 | who = "Everyone" | 188 | perms = "(R,W,M)" |
| 190 | end | 189 | elseif mode == "exec" then |
| 191 | if mode == "read" then | 190 | perms = "(F)" |
| 192 | what = "(RD)" | 191 | end |
| 193 | elseif mode == "exec" then | ||
| 194 | what = "(X)" | ||
| 195 | end | ||
| 196 | if not who or not what then | ||
| 197 | return false, "Invalid permission " .. mode .. " for " .. scope | ||
| 198 | end | ||
| 199 | 192 | ||
| 200 | if scope == "user" then | 193 | local ok |
| 194 | -- Take ownership of the given file | ||
| 195 | ok = fs.execute_quiet("takeown /f " .. fs.Q(filename)) | ||
| 196 | if not ok then | ||
| 197 | return false, "Could not take ownership of the given file" | ||
| 198 | end | ||
| 199 | -- Grant the current user the proper rights | ||
| 200 | ok = fs.execute_quiet(fs.Q(vars.ICACLS) .. " " .. fs.Q(filename) .. " /inheritance:d /grant:r %USERNAME%:" .. perms) | ||
| 201 | if not ok then | ||
| 202 | return false, "Failed setting permission " .. mode .. " for " .. scope | ||
| 203 | end | ||
| 204 | -- Finally, remove all the other users from the ACL in order to deny them access to the file | ||
| 201 | for _, user in pairs(get_system_users()) do | 205 | for _, user in pairs(get_system_users()) do |
| 202 | if user ~= who then | 206 | if user ~= who then |
| 203 | local ok = fs.execute(fs.Q(vars.ICACLS) .. " " .. fs.Q(filename) .. " /deny " .. fs.Q(user) .. ":" .. fs.Q(what)) | 207 | local ok = fs.execute_quiet(fs.Q(vars.ICACLS) .. " " .. fs.Q(filename) .. " /remove " .. fs.Q(user)) |
| 204 | if not ok then | 208 | if not ok then |
| 205 | return false, "Failed setting permission " .. mode .. " for " .. scope | 209 | return false, "Failed setting permission " .. mode .. " for " .. scope |
| 206 | end | 210 | end |
| 207 | end | 211 | end |
| 208 | end | 212 | end |
| 213 | elseif scope == "all" then | ||
| 214 | local my_perms, others_perms | ||
| 215 | if mode == "read" then | ||
| 216 | my_perms = "(R,W,M)" | ||
| 217 | others_perms = "(R)" | ||
| 218 | elseif mode == "exec" then | ||
| 219 | my_perms = "(F)" | ||
| 220 | others_perms = "(RX)" | ||
| 221 | end | ||
| 222 | |||
| 223 | local ok | ||
| 224 | -- Grant permissions available to all users | ||
| 225 | ok = fs.execute_quiet(fs.Q(vars.ICACLS) .. " " .. fs.Q(filename) .. " /inheritance:d /grant:r Everyone:" .. others_perms) | ||
| 226 | if not ok then | ||
| 227 | return false, "Failed setting permission " .. mode .. " for " .. scope | ||
| 228 | end | ||
| 229 | -- Grant permissions available only to the current user | ||
| 230 | ok = fs.execute_quiet(fs.Q(vars.ICACLS) .. " " .. fs.Q(filename) .. " /inheritance:d /grant %USERNAME%:" .. my_perms) | ||
| 231 | if not ok then | ||
| 232 | return false, "Failed setting permission " .. mode .. " for " .. scope | ||
| 233 | end | ||
| 209 | end | 234 | end |
| 210 | 235 | ||
| 211 | return fs.execute(fs.Q(vars.ICACLS) .. " " .. fs.Q(filename) .. " /grant " .. fs.Q(who) .. ":" .. fs.Q(what)) | 236 | return true |
| 212 | end | 237 | end |
| 213 | 238 | ||
| 214 | 239 | ||
