Skip to content

Commit

Permalink
Merge pull request JuliaLang#11172 from peter1000/adds_kw_remove_dest…
Browse files Browse the repository at this point in the history
…ination_to__mv

Adds kw `remove_destination` to `mv` function JuliaLang#11145
  • Loading branch information
tkelman committed May 13, 2015
2 parents c790369 + 43eb785 commit c2a28ee
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 88 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ Library improvements

* The `cp` function now accepts keyword arguments `remove_destination` and `follow_symlinks` ([#10888]).

* The `mv` function now accepts keyword argument `remove_destination` ([#11145]).

* Other improvements

* You can now tab-complete Emoji characters via their [short names](http:https://www.emoji-cheat-sheet.com/), using `\:name:<tab>` ([#10709]).
Expand Down Expand Up @@ -1405,3 +1407,4 @@ Too numerous to mention.
[#10893]: https://github.com/JuliaLang/julia/issues/10893
[#10914]: https://github.com/JuliaLang/julia/issues/10914
[#10994]: https://github.com/JuliaLang/julia/issues/10994
[#11145]: https://github.com/JuliaLang/julia/issues/11145
39 changes: 25 additions & 14 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)`"))
function checkfor_mv_cp_cptree(src::AbstractString, dst::AbstractString, txt::AbstractString;
remove_destination::Bool=false)
if ispath(dst)
if remove_destination
# Check for issue when: (src == dst) or when one is a link to the other
# 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` refers to: $(abs_src)\n ",
"`dst` refers 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 @@ -115,7 +122,11 @@ function cp(src::AbstractString, dst::AbstractString; remove_destination::Bool=f
FS.sendfile(src, dst)
end
end
mv(src::AbstractString, dst::AbstractString) = FS.rename(src, dst)

function mv(src::AbstractString, dst::AbstractString; remove_destination::Bool=false)
checkfor_mv_cp_cptree(src, dst, "moving"; remove_destination=remove_destination)
FS.rename(src, dst)
end

function touch(path::AbstractString)
f = FS.open(path,JL_O_WRONLY | JL_O_CREAT, 0o0666)
Expand Down
10 changes: 3 additions & 7 deletions base/fs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,11 @@ end
# For move command
function rename(src::AbstractString, dst::AbstractString)
err = ccall(:jl_fs_rename, Int32, (Cstring, Cstring), src, dst)

# on error, default to cp && rm
if err < 0
# Note that those two functions already handle their errors.
# first copy
sendfile(src, dst)

# then rm
unlink(src)
# remove_destination: is already done in the mv function
cp(src, dst; remove_destination=false, follow_symlinks=false)
rm(src; recursive=true)
end
end

Expand Down
9 changes: 8 additions & 1 deletion base/stat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,12 @@ filesize(path...) = stat(path...).size
mtime(path...) = stat(path...).mtime
ctime(path...) = stat(path...).ctime

# 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
5 changes: 3 additions & 2 deletions doc/helpdb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5261,9 +5261,10 @@ Millisecond(v)
"),

("Base","mv","mv(src::AbstractString, dst::AbstractString)
("Base","mv","mv(src::AbstractString,dst::AbstractString; remove_destination::Bool=false)
Move a file from *src* to *dst*.
Move the file, link, or directory from *src* to *dest*.
\"remove_destination=true\" will first remove an existing `dst`.
"),

Expand Down
5 changes: 3 additions & 2 deletions doc/stdlib/file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@
use or situations in which more options are need, please use a package that provides the
desired functionality instead.

.. function:: mv(src::AbstractString,dst::AbstractString)
.. function:: mv(src::AbstractString,dst::AbstractString; remove_destination::Bool=false)

Move a file from `src` to `dst`.
Move the file, link, or directory from *src* to *dest*.
\"remove_destination=true\" will first remove an existing `dst`.

.. function:: rm(path::AbstractString; recursive=false)

Expand Down
Loading

0 comments on commit c2a28ee

Please sign in to comment.