Skip to content

Commit

Permalink
Samefile: return false if a path does not exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
peter1000 committed May 9, 2015
1 parent e0a318b commit fec7ef1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
42 changes: 21 additions & 21 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,32 @@ end


# The following use Unix command line facilites

function cptree(src::AbstractString, dst::AbstractString; remove_destination::Bool=false,
follow_symlinks::Bool=false)
isdir(src) || throw(ArgumentError("'$src' is not a directory. Use `cp(src, dst)`"))
# check for https://github.com/JuliaLang/julia/pull/11172#issuecomment-100391076
function checkfor_mv_cp_cptree(src::AbstractString, dst::AbstractString, txt::AbstractString;
remove_destination::Bool=false)
if ispath(dst)
if remove_destination
# check for https://github.com/JuliaLang/julia/pull/11172#issuecomment-100391076
if Base.samefile(src, dst)
abs_src = islink(src) ? abspath(readlink(src)) : abspath(src)
abs_dst = islink(dst) ? abspath(readlink(dst)) : abspath(dst)
throw(ArgumentError(string("'src' and 'dst' refer to the same file/dir.",
"This is not supported.\n ",
"`src` referce to: $(abs_src)\n ",
"`dst` referce to: $(abs_dst)\n")))
end
rm(dst; recursive=true)
else
throw(ArgumentError(string("'$dst' exists. `remove_destination=true` ",
"is required to remove '$dst' before copying.")))
"is required to remove '$dst' before $(txt).")))
end
end
end

function cptree(src::AbstractString, dst::AbstractString; remove_destination::Bool=false,
follow_symlinks::Bool=false)
isdir(src) || throw(ArgumentError("'$src' is not a directory. Use `cp(src, dst)`"))
checkfor_mv_cp_cptree(src, dst, "copying"; remove_destination=remove_destination)
mkdir(dst)
for name in readdir(src)
srcname = joinpath(src, name)
Expand All @@ -99,14 +113,7 @@ end

function cp(src::AbstractString, dst::AbstractString; remove_destination::Bool=false,
follow_symlinks::Bool=false)
if ispath(dst)
if remove_destination
rm(dst; recursive=true)
else
throw(ArgumentError(string("'$dst' exists. `remove_destination=true` ",
"is required to remove '$dst' before copying.")))
end
end
checkfor_mv_cp_cptree(src, dst, "copying"; remove_destination=remove_destination)
if !follow_symlinks && islink(src)
symlink(readlink(src), dst)
elseif isdir(src)
Expand All @@ -117,14 +124,7 @@ function cp(src::AbstractString, dst::AbstractString; remove_destination::Bool=f
end

function mv(src::AbstractString, dst::AbstractString; remove_destination::Bool=false)
if ispath(dst)
if remove_destination
rm(dst; recursive=true)
else
throw(ArgumentError(string("'$dst' exists. `remove_destination=true` ",
"is required to remove '$dst' before moving.")))
end
end
checkfor_mv_cp_cptree(src, dst, "copying"; remove_destination=remove_destination)
FS.rename(src, dst)
end

Expand Down
8 changes: 7 additions & 1 deletion base/stat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,10 @@ filesize(path...) = stat(path...).size

# 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(a::AbstractString, b::AbstractString) = samefile(stat(a),stat(b))
function samefile(a::AbstractString, b::AbstractString)
if ispath(a) && ispath(b)
samefile(stat(a),stat(b))
else
return false
end
end

0 comments on commit fec7ef1

Please sign in to comment.