-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Description
As outlined on Discourse, there are two unrelated issues with samefile:
-
samefileresults can be inconsistent in a border case
Assume files "A" and "B" do not exist:julia> using Base.Filesystem julia> samefile(stat("A"), stat("B")) # -> samefile(::StatStruct, ::StatStruct) true julia> samefile("A", "B") # -> samefile(::AbstractString, ::AbstractString) falseThe latter uses
ispathto check for file existence, and arguably has the expected/"correct" behavior. -
samefilemight be too restrictive
The Filesystem API provides many specific functions on StatStruct likeisfile(::StatStruct), and unrestricted, generic variants likeisfile(x) = isfile(stat(x)). Types that arestat()-able thus can fit in nicely (e.g., types cashing theStatStruct).
samefile(::AbstractStrings, ::AbstractStrings)seems to be unnecessarily restrictive.
Not sure if it is a good idea to mix up two unrelated changes, but it's all within a handful of LOC; so a proposed change:
OLD/current:
# samefile can be used for files and directories: #11145#issuecomment-99511194
samefile(a::StatStruct, b::StatStruct) = a.device==b.device && a.inode==b.inode
"""
samefile(path_a::AbstractString, path_b::AbstractString)
Check if the paths `path_a` and `path_b` refer to the same existing file or directory.
"""
function samefile(a::AbstractString, b::AbstractString)
infoa = stat(a)
infob = stat(b)
if ispath(infoa) && ispath(infob)
samefile(infoa, infob)
else
return false
end
end
NEW:
"""
samefile(patha, pathb) -> Bool
Check if `patha` and `pathb` refer to the same existing file or directory.
"""
function samefile(sta::StatStruct, stb::StatStruct)
ispath(sta) || return false
ispath(stb) || return false
return sta.device == stb.device && sta.inode == stb.inode
end
samefile(patha, pathb) = samefile(stat(patha), stat(pathb))
(Reasons for doc string location, arg names in doc string,.. are given on Discourse.)
There are other issues that might be interesting in this context (should stat return nothing instead of a 0-struct; is the name samefile ideal if it can be used for dirs,..), but maybe for now it's best to keep this minimal/surgical?