From 6c458fbfbf1c542036af2adf11f4311b0413f663 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Mon, 2 Sep 2024 17:57:25 -0300 Subject: fs: more consistent function signatures --- src/luarocks/fs.d.tl | 4 ++-- src/luarocks/fs/lua.lua | 11 ++++++----- src/luarocks/fs/unix/tools.lua | 16 ++++++++++------ 3 files changed, 18 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/luarocks/fs.d.tl b/src/luarocks/fs.d.tl index 580f7c3f..45044b72 100644 --- a/src/luarocks/fs.d.tl +++ b/src/luarocks/fs.d.tl @@ -15,7 +15,7 @@ local record fs is_file: function(file: string): boolean current_dir: function(): string list_dir: function(?string): {string} - delete: function(string) + delete: function(string): boolean, string -- signing is_tool_available: function(string, string): string, string execute: function(...: string): boolean, string, string @@ -57,7 +57,7 @@ local record fs wrap_script: function(string, string, string, string, string): boolean, string is_lua: function(string): boolean copy_binary: function(string, string): boolean, string - move: function(string, string, string): boolean, string + move: function(string, string, perms?: string): boolean, string -- writer replace_file: function(string, string): boolean, string get_md5: function(string): string, string diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 02d9353a..0f1ee55e 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -562,14 +562,15 @@ end local function recursive_delete(name) local ok = os.remove(name) if ok then return true end - local pok, ok, err = pcall(function() + local pok, err + 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)) + ok, err = recursive_delete(dir.path(name, file)) if not ok then return nil, err end end end - local ok, err = lfs.rmdir(name) + ok, err = lfs.rmdir(name) return ok, (not ok) and err end) if pok then @@ -581,10 +582,10 @@ end --- Delete a file or a directory and all its contents. -- @param name string: Pathname of source --- @return nil +-- @return true on success, or nil and an error message on failure function fs_lua.delete(name) name = dir.normalize(name) - recursive_delete(name) + return recursive_delete(name) end --- Internal implementation function for fs.dir. diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index f1513820..2127aed6 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -92,12 +92,16 @@ function tools.copy_contents(src, dest) end --- Delete a file or a directory and all its contents. -- For safety, this only accepts absolute paths. --- @param arg string: Pathname of source --- @return nil -function tools.delete(arg) - assert(arg) - assert(arg:sub(1,1) == "/") - fs.execute_quiet(vars.RM, "-rf", arg) +-- @param pathname string: Pathname of source +-- @return true on success, nil and an error on failure +function tools.delete(pathname) + assert(pathname) + assert(pathname:sub(1,1) == "/") + if fs.execute_quiet(vars.RM, "-rf", pathname) then + return true + else + return nil, "failed deleting " .. pathname + end end --- Recursively scan the contents of a directory. -- cgit v1.2.3-55-g6feb