From 201ac63c7692355b07b83034c1813c000ec70e84 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Wed, 5 Feb 2020 15:18:14 +0100 Subject: fs.lua: fix error message when source file is missing Fixes #1147. --- spec/fs_spec.lua | 4 +++- src/luarocks/fs/lua.lua | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/spec/fs_spec.lua b/spec/fs_spec.lua index 5d8926c4..16295869 100644 --- a/spec/fs_spec.lua +++ b/spec/fs_spec.lua @@ -894,7 +894,9 @@ describe("Luarocks fs test #unit", function() it("returns false and does nothing if the source file does not exist", function() srcfile = get_tmp_path() dstfile = get_tmp_path() - assert.falsy(fs.copy(srcfile, dstfile, nil)) + local ok, err = fs.copy(srcfile, dstfile, nil) + assert.falsy(ok) + assert.not_match("are the same file", err) assert.falsy(exists_file(dstfile)) end) diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 6028a925..c5cebd2f 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -380,6 +380,20 @@ function fs_lua.remove_dir_tree_if_empty(d) end end +local function are_the_same_file(f1, f2) + if f1 == f2 then + return true + end + if cfg.is_platform("unix") then + local i1 = lfs.attributes(f1, "ino") + local i2 = lfs.attributes(f2, "ino") + if i1 ~= nil and i1 == i2 then + return true + end + end + return false +end + --- Copy a file. -- @param src string: Pathname of source -- @param dest string: Pathname of destination @@ -395,7 +409,7 @@ function fs_lua.copy(src, dest, perms) if destmode == "directory" then dest = dir.path(dest, dir.base_name(src)) end - if src == dest or (cfg.is_platform("unix") and lfs.attributes(src, "ino") == lfs.attributes(dest, "ino")) then + if are_the_same_file(src, dest) then return nil, "The source and destination are the same files" end local src_h, err = io.open(src, "rb") -- cgit v1.2.3-55-g6feb