From 7559598e6a126460ca521439aa89ce352b800a03 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Fri, 18 Oct 2013 18:07:17 -0300 Subject: Fix behavior on nonexisting files, and while we're at it make it more efficient and less prone to race conditions. --- src/luarocks/fs/lua.lua | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 0e81d877..707b82bd 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -328,11 +328,9 @@ end -- @return boolean or (boolean, string): true on success, -- or nil and an error message on failure. local function recursive_delete(name) - local mode = lfs.attributes(name, "mode") - - if mode == "file" then - return os.remove(name) - elseif mode == "directory" then + local ok = os.remove(name) + if ok then return true end + local pok, ok, err = pcall(function() for file in lfs.dir(name) do if file ~= "." and file ~= ".." then local ok, err = recursive_delete(dir.path(name, file)) @@ -340,9 +338,13 @@ local function recursive_delete(name) end end local ok, err = lfs.rmdir(name) - if not ok then return nil, err end + return ok, (not ok) and err + end) + if pok then + return ok, err + else + return pok, ok end - return true end --- Delete a file or a directory and all its contents. -- cgit v1.2.3-55-g6feb