diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2019-04-01 16:27:08 -0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2019-04-01 16:29:17 -0300 |
| commit | 7e9b7e08a8a31501189a32c60fb9cb7bc6b31b5e (patch) | |
| tree | a92f51e7ae906d74cfefee136afad997a9ce6319 | |
| parent | 8283b817efd6fadad62794045d6bebad71e063cd (diff) | |
| download | luarocks-7e9b7e08a8a31501189a32c60fb9cb7bc6b31b5e.tar.gz luarocks-7e9b7e08a8a31501189a32c60fb9cb7bc6b31b5e.tar.bz2 luarocks-7e9b7e08a8a31501189a32c60fb9cb7bc6b31b5e.zip | |
fs: versions of exists, is_file, is_dir for Unix and Windows that do not fork
Implements versions of exists, is_file, is_dir for POSIX and Windows
using io.open only, based on the semantics of their error codes
on these platforms.
Drops the dependency on TEST.EXE on Windows.
| -rw-r--r-- | install.bat | 13 | ||||
| -rw-r--r-- | src/luarocks/core/cfg.lua | 3 | ||||
| -rw-r--r-- | src/luarocks/fs/lua.lua | 9 | ||||
| -rw-r--r-- | src/luarocks/fs/unix.lua | 40 | ||||
| -rw-r--r-- | src/luarocks/fs/unix/tools.lua | 24 | ||||
| -rw-r--r-- | src/luarocks/fs/win32.lua | 35 | ||||
| -rw-r--r-- | src/luarocks/fs/win32/tools.lua | 24 | ||||
| -rw-r--r-- | win32/COPYING | 2 | ||||
| -rw-r--r-- | win32/tools/test.exe | bin | 62976 -> 0 bytes |
9 files changed, 97 insertions, 53 deletions
diff --git a/install.bat b/install.bat index a98b32fd..43b18e1d 100644 --- a/install.bat +++ b/install.bat | |||
| @@ -67,8 +67,17 @@ local function exec(cmd) | |||
| 67 | end | 67 | end |
| 68 | 68 | ||
| 69 | local function exists(filename) | 69 | local function exists(filename) |
| 70 | local cmd = [[.\win32\tools\test -e "]]..filename..[["]] | 70 | local fd, _, code = io.open(filename, "r") |
| 71 | return exec(cmd) | 71 | if code == 13 then |
| 72 | -- code 13 means "Permission denied" on both Unix and Windows | ||
| 73 | -- io.open on folders always fails with code 13 on Windows | ||
| 74 | return true | ||
| 75 | end | ||
| 76 | if fd then | ||
| 77 | fd:close() | ||
| 78 | return true | ||
| 79 | end | ||
| 80 | return false | ||
| 72 | end | 81 | end |
| 73 | 82 | ||
| 74 | local function mkdir (dir) | 83 | local function mkdir (dir) |
diff --git a/src/luarocks/core/cfg.lua b/src/luarocks/core/cfg.lua index 670f04b2..5b9dec2f 100644 --- a/src/luarocks/core/cfg.lua +++ b/src/luarocks/core/cfg.lua | |||
| @@ -242,7 +242,6 @@ local function make_defaults(lua_version, target_cpu, platforms, home) | |||
| 242 | LS = "ls", | 242 | LS = "ls", |
| 243 | RM = "rm", | 243 | RM = "rm", |
| 244 | FIND = "find", | 244 | FIND = "find", |
| 245 | TEST = "test", | ||
| 246 | CHMOD = "chmod", | 245 | CHMOD = "chmod", |
| 247 | ICACLS = "icacls", | 246 | ICACLS = "icacls", |
| 248 | MKTEMP = "mktemp", | 247 | MKTEMP = "mktemp", |
| @@ -718,7 +717,7 @@ function cfg.init(detected, warning) | |||
| 718 | local defaults = make_defaults(lua_version, processor, platforms, cfg.home) | 717 | local defaults = make_defaults(lua_version, processor, platforms, cfg.home) |
| 719 | 718 | ||
| 720 | if platforms.windows and hardcoded.WIN_TOOLS then | 719 | if platforms.windows and hardcoded.WIN_TOOLS then |
| 721 | local tools = { "SEVENZ", "CP", "FIND", "LS", "MD5SUM", "PWD", "RMDIR", "TEST", "WGET", "MKDIR" } | 720 | local tools = { "SEVENZ", "CP", "FIND", "LS", "MD5SUM", "PWD", "RMDIR", "WGET", "MKDIR" } |
| 722 | for _, tool in ipairs(tools) do | 721 | for _, tool in ipairs(tools) do |
| 723 | defaults.variables[tool] = '"' .. hardcoded.WIN_TOOLS .. "/" .. defaults.variables[tool] .. '.exe"' | 722 | defaults.variables[tool] = '"' .. hardcoded.WIN_TOOLS .. "/" .. defaults.variables[tool] .. '.exe"' |
| 724 | end | 723 | end |
diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 0976dc8d..6625ddda 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua | |||
| @@ -590,6 +590,15 @@ function fs_lua.set_time(file, time) | |||
| 590 | return lfs.touch(file, time) | 590 | return lfs.touch(file, time) |
| 591 | end | 591 | end |
| 592 | 592 | ||
| 593 | else -- if not lfs_ok | ||
| 594 | |||
| 595 | function fs_lua.exists(file) | ||
| 596 | assert(file) | ||
| 597 | file = dir.normalize(fs.absolute_name(file)) | ||
| 598 | -- check if file exists by attempting to open it | ||
| 599 | return util.exists(file) | ||
| 600 | end | ||
| 601 | |||
| 593 | end | 602 | end |
| 594 | 603 | ||
| 595 | --------------------------------------------------------------------- | 604 | --------------------------------------------------------------------- |
diff --git a/src/luarocks/fs/unix.lua b/src/luarocks/fs/unix.lua index 50bfba9c..849259e8 100644 --- a/src/luarocks/fs/unix.lua +++ b/src/luarocks/fs/unix.lua | |||
| @@ -205,4 +205,44 @@ function unix._unix_moderate_permissions(perms) | |||
| 205 | return moderated_perms | 205 | return moderated_perms |
| 206 | end | 206 | end |
| 207 | 207 | ||
| 208 | function unix.is_dir(file) | ||
| 209 | file = fs.absolute_name(file) | ||
| 210 | file = dir.normalize(file) .. "/" | ||
| 211 | local fd, _, code = io.open(file, "r") | ||
| 212 | if code == 2 then -- "No such file or directory" | ||
| 213 | return false | ||
| 214 | end | ||
| 215 | if code == 20 then -- "Not a directory", regardless of permissions | ||
| 216 | return false | ||
| 217 | end | ||
| 218 | if code == 13 then -- "Permission denied", but is a directory | ||
| 219 | return true | ||
| 220 | end | ||
| 221 | if fd then | ||
| 222 | fd:close() | ||
| 223 | return true | ||
| 224 | end | ||
| 225 | return false | ||
| 226 | end | ||
| 227 | |||
| 228 | function unix.is_file(file) | ||
| 229 | file = fs.absolute_name(file) | ||
| 230 | if fs.is_dir(file) then | ||
| 231 | return false | ||
| 232 | end | ||
| 233 | file = dir.normalize(file) | ||
| 234 | local fd, _, code = io.open(file, "r") | ||
| 235 | if code == 2 then -- "No such file or directory" | ||
| 236 | return false | ||
| 237 | end | ||
| 238 | if code == 13 then -- "Permission denied", but it exists | ||
| 239 | return true | ||
| 240 | end | ||
| 241 | if fd then | ||
| 242 | fd:close() | ||
| 243 | return true | ||
| 244 | end | ||
| 245 | return false | ||
| 246 | end | ||
| 247 | |||
| 208 | return unix | 248 | return unix |
diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index c5d83018..9a2f99a7 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua | |||
| @@ -183,30 +183,6 @@ function tools.bunzip2(infile, outfile) | |||
| 183 | return uncompress("bz2", "bunzip2", infile, outfile) | 183 | return uncompress("bz2", "bunzip2", infile, outfile) |
| 184 | end | 184 | end |
| 185 | 185 | ||
| 186 | --- Test is file/directory exists | ||
| 187 | -- @param file string: filename to test | ||
| 188 | -- @return boolean: true if file exists, false otherwise. | ||
| 189 | function tools.exists(file) | ||
| 190 | assert(file) | ||
| 191 | return fs.execute(vars.TEST, "-e", file) | ||
| 192 | end | ||
| 193 | |||
| 194 | --- Test is pathname is a directory. | ||
| 195 | -- @param file string: pathname to test | ||
| 196 | -- @return boolean: true if it is a directory, false otherwise. | ||
| 197 | function tools.is_dir(file) | ||
| 198 | assert(file) | ||
| 199 | return fs.execute(vars.TEST, "-d", file) | ||
| 200 | end | ||
| 201 | |||
| 202 | --- Test is pathname is a regular file. | ||
| 203 | -- @param file string: pathname to test | ||
| 204 | -- @return boolean: true if it is a regular file, false otherwise. | ||
| 205 | function tools.is_file(file) | ||
| 206 | assert(file) | ||
| 207 | return fs.execute(vars.TEST, "-f", file) | ||
| 208 | end | ||
| 209 | |||
| 210 | do | 186 | do |
| 211 | local function rwx_to_octal(rwx) | 187 | local function rwx_to_octal(rwx) |
| 212 | return (rwx:match "r" and 4 or 0) | 188 | return (rwx:match "r" and 4 or 0) |
diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index 87232b77..498eceb7 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua | |||
| @@ -234,6 +234,41 @@ function win32.replace_file(old_file, new_file) | |||
| 234 | return os.rename(new_file, old_file) | 234 | return os.rename(new_file, old_file) |
| 235 | end | 235 | end |
| 236 | 236 | ||
| 237 | function win32.is_dir(file) | ||
| 238 | file = fs.absolute_name(file) | ||
| 239 | file = dir.normalize(file) | ||
| 240 | local fd, _, code = io.open(file, "r") | ||
| 241 | if code == 13 then -- directories return "Permission denied" | ||
| 242 | fd, _, code = io.open(file .. "\\", "r") | ||
| 243 | if code == 2 then -- directories return 2, files return 22 | ||
| 244 | return true | ||
| 245 | end | ||
| 246 | end | ||
| 247 | if fd then | ||
| 248 | fd:close() | ||
| 249 | end | ||
| 250 | return false | ||
| 251 | end | ||
| 252 | |||
| 253 | function win32.is_file(file) | ||
| 254 | file = fs.absolute_name(file) | ||
| 255 | file = dir.normalize(file) | ||
| 256 | local fd, _, code = io.open(file, "r") | ||
| 257 | if code == 13 then -- if "Permission denied" | ||
| 258 | fd, _, code = io.open(file .. "\\", "r") | ||
| 259 | if code == 2 then -- directories return 2, files return 22 | ||
| 260 | return false | ||
| 261 | elseif code == 22 then | ||
| 262 | return true | ||
| 263 | end | ||
| 264 | end | ||
| 265 | if fd then | ||
| 266 | fd:close() | ||
| 267 | return true | ||
| 268 | end | ||
| 269 | return false | ||
| 270 | end | ||
| 271 | |||
| 237 | --- Test is file/dir is writable. | 272 | --- Test is file/dir is writable. |
| 238 | -- Warning: testing if a file/dir is writable does not guarantee | 273 | -- Warning: testing if a file/dir is writable does not guarantee |
| 239 | -- that it will remain writable and therefore it is no replacement | 274 | -- that it will remain writable and therefore it is no replacement |
diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index 77e962eb..f9468517 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua | |||
| @@ -201,22 +201,6 @@ function tools.bunzip2(infile, outfile) | |||
| 201 | return sevenz("bz2", infile, outfile) | 201 | return sevenz("bz2", infile, outfile) |
| 202 | end | 202 | end |
| 203 | 203 | ||
| 204 | --- Test is pathname is a directory. | ||
| 205 | -- @param file string: pathname to test | ||
| 206 | -- @return boolean: true if it is a directory, false otherwise. | ||
| 207 | function tools.is_dir(file) | ||
| 208 | assert(file) | ||
| 209 | return fs.execute_quiet("if not exist " .. fs.Q(file.."\\").." invalidcommandname") | ||
| 210 | end | ||
| 211 | |||
| 212 | --- Test is pathname is a regular file. | ||
| 213 | -- @param file string: pathname to test | ||
| 214 | -- @return boolean: true if it is a regular file, false otherwise. | ||
| 215 | function tools.is_file(file) | ||
| 216 | assert(file) | ||
| 217 | return fs.execute(vars.TEST.." -f", file) | ||
| 218 | end | ||
| 219 | |||
| 220 | --- Helper function for fs.set_permissions | 204 | --- Helper function for fs.set_permissions |
| 221 | -- @return table: an array of all system users | 205 | -- @return table: an array of all system users |
| 222 | local function get_system_users() | 206 | local function get_system_users() |
| @@ -292,14 +276,6 @@ function tools.set_permissions(filename, mode, scope) | |||
| 292 | return true | 276 | return true |
| 293 | end | 277 | end |
| 294 | 278 | ||
| 295 | --- Test for existence of a file. | ||
| 296 | -- @param file string: filename to test | ||
| 297 | -- @return boolean: true if file exists, false otherwise. | ||
| 298 | function tools.exists(file) | ||
| 299 | assert(file) | ||
| 300 | return fs.execute_quiet("if not exist " .. fs.Q(file) .. " invalidcommandname") | ||
| 301 | end | ||
| 302 | |||
| 303 | function tools.browser(url) | 279 | function tools.browser(url) |
| 304 | return fs.execute(cfg.web_browser..' "Starting docs..." '..fs.Q(url)) | 280 | return fs.execute(cfg.web_browser..' "Starting docs..." '..fs.Q(url)) |
| 305 | end | 281 | end |
diff --git a/win32/COPYING b/win32/COPYING index 3413b8c2..d28b2efd 100644 --- a/win32/COPYING +++ b/win32/COPYING | |||
| @@ -5,7 +5,7 @@ following licenses: | |||
| 5 | * For 7z.exe and 7z.dll, please see COPYING_7z for details. | 5 | * For 7z.exe and 7z.dll, please see COPYING_7z for details. |
| 6 | 6 | ||
| 7 | * find.exe, mv.exe, wget.exe, ls.exe, pwd.exe, rmdir.exe, md5sum.exe, | 7 | * find.exe, mv.exe, wget.exe, ls.exe, pwd.exe, rmdir.exe, md5sum.exe, |
| 8 | test.exe, cp.exe, mkdir.exe, and uname.exe are part of UnxUtils, check | 8 | cp.exe, mkdir.exe, and uname.exe are part of UnxUtils, check |
| 9 | http://unxutils.sourceforge.net/ for license information. | 9 | http://unxutils.sourceforge.net/ for license information. |
| 10 | 10 | ||
| 11 | * Files under win32/lua5.1, except for Microsoft.VC80.CRT.manifest and msv*.*, | 11 | * Files under win32/lua5.1, except for Microsoft.VC80.CRT.manifest and msv*.*, |
diff --git a/win32/tools/test.exe b/win32/tools/test.exe deleted file mode 100644 index 94c95f9e..00000000 --- a/win32/tools/test.exe +++ /dev/null | |||
| Binary files differ | |||
