From 7e9b7e08a8a31501189a32c60fb9cb7bc6b31b5e Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Mon, 1 Apr 2019 16:27:08 -0300 Subject: 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. --- src/luarocks/core/cfg.lua | 3 +-- src/luarocks/fs/lua.lua | 9 +++++++++ src/luarocks/fs/unix.lua | 40 ++++++++++++++++++++++++++++++++++++++++ src/luarocks/fs/unix/tools.lua | 24 ------------------------ src/luarocks/fs/win32.lua | 35 +++++++++++++++++++++++++++++++++++ src/luarocks/fs/win32/tools.lua | 24 ------------------------ 6 files changed, 85 insertions(+), 50 deletions(-) (limited to 'src') 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) LS = "ls", RM = "rm", FIND = "find", - TEST = "test", CHMOD = "chmod", ICACLS = "icacls", MKTEMP = "mktemp", @@ -718,7 +717,7 @@ function cfg.init(detected, warning) local defaults = make_defaults(lua_version, processor, platforms, cfg.home) if platforms.windows and hardcoded.WIN_TOOLS then - local tools = { "SEVENZ", "CP", "FIND", "LS", "MD5SUM", "PWD", "RMDIR", "TEST", "WGET", "MKDIR" } + local tools = { "SEVENZ", "CP", "FIND", "LS", "MD5SUM", "PWD", "RMDIR", "WGET", "MKDIR" } for _, tool in ipairs(tools) do defaults.variables[tool] = '"' .. hardcoded.WIN_TOOLS .. "/" .. defaults.variables[tool] .. '.exe"' 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) return lfs.touch(file, time) end +else -- if not lfs_ok + +function fs_lua.exists(file) + assert(file) + file = dir.normalize(fs.absolute_name(file)) + -- check if file exists by attempting to open it + return util.exists(file) +end + end --------------------------------------------------------------------- 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) return moderated_perms end +function unix.is_dir(file) + file = fs.absolute_name(file) + file = dir.normalize(file) .. "/" + local fd, _, code = io.open(file, "r") + if code == 2 then -- "No such file or directory" + return false + end + if code == 20 then -- "Not a directory", regardless of permissions + return false + end + if code == 13 then -- "Permission denied", but is a directory + return true + end + if fd then + fd:close() + return true + end + return false +end + +function unix.is_file(file) + file = fs.absolute_name(file) + if fs.is_dir(file) then + return false + end + file = dir.normalize(file) + local fd, _, code = io.open(file, "r") + if code == 2 then -- "No such file or directory" + return false + end + if code == 13 then -- "Permission denied", but it exists + return true + end + if fd then + fd:close() + return true + end + return false +end + 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) return uncompress("bz2", "bunzip2", infile, outfile) end ---- Test is file/directory exists --- @param file string: filename to test --- @return boolean: true if file exists, false otherwise. -function tools.exists(file) - assert(file) - return fs.execute(vars.TEST, "-e", file) -end - ---- Test is pathname is a directory. --- @param file string: pathname to test --- @return boolean: true if it is a directory, false otherwise. -function tools.is_dir(file) - assert(file) - return fs.execute(vars.TEST, "-d", file) -end - ---- Test is pathname is a regular file. --- @param file string: pathname to test --- @return boolean: true if it is a regular file, false otherwise. -function tools.is_file(file) - assert(file) - return fs.execute(vars.TEST, "-f", file) -end - do local function rwx_to_octal(rwx) 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) return os.rename(new_file, old_file) end +function win32.is_dir(file) + file = fs.absolute_name(file) + file = dir.normalize(file) + local fd, _, code = io.open(file, "r") + if code == 13 then -- directories return "Permission denied" + fd, _, code = io.open(file .. "\\", "r") + if code == 2 then -- directories return 2, files return 22 + return true + end + end + if fd then + fd:close() + end + return false +end + +function win32.is_file(file) + file = fs.absolute_name(file) + file = dir.normalize(file) + local fd, _, code = io.open(file, "r") + if code == 13 then -- if "Permission denied" + fd, _, code = io.open(file .. "\\", "r") + if code == 2 then -- directories return 2, files return 22 + return false + elseif code == 22 then + return true + end + end + if fd then + fd:close() + return true + end + return false +end + --- Test is file/dir is writable. -- Warning: testing if a file/dir is writable does not guarantee -- 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) return sevenz("bz2", infile, outfile) end ---- Test is pathname is a directory. --- @param file string: pathname to test --- @return boolean: true if it is a directory, false otherwise. -function tools.is_dir(file) - assert(file) - return fs.execute_quiet("if not exist " .. fs.Q(file.."\\").." invalidcommandname") -end - ---- Test is pathname is a regular file. --- @param file string: pathname to test --- @return boolean: true if it is a regular file, false otherwise. -function tools.is_file(file) - assert(file) - return fs.execute(vars.TEST.." -f", file) -end - --- Helper function for fs.set_permissions -- @return table: an array of all system users local function get_system_users() @@ -292,14 +276,6 @@ function tools.set_permissions(filename, mode, scope) return true end ---- Test for existence of a file. --- @param file string: filename to test --- @return boolean: true if file exists, false otherwise. -function tools.exists(file) - assert(file) - return fs.execute_quiet("if not exist " .. fs.Q(file) .. " invalidcommandname") -end - function tools.browser(url) return fs.execute(cfg.web_browser..' "Starting docs..." '..fs.Q(url)) end -- cgit v1.2.3-55-g6feb