Skip to content

Commit

Permalink
Adds kw remove_destination to mv function JuliaLang#11145 incl do…
Browse files Browse the repository at this point in the history
…cs/tests/NEWS entry
  • Loading branch information
peter1000 authored and mbauman committed Jun 5, 2015
1 parent bff97af commit 252e10e
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 69 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,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://www.emoji-cheat-sheet.com/), using `\:name:<tab>` ([#10709]).
Expand Down Expand Up @@ -1417,3 +1419,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
13 changes: 12 additions & 1 deletion base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,18 @@ 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)
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
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
1 change: 1 addition & 0 deletions base/stat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,6 @@ 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))
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 252e10e

Please sign in to comment.