Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Recursive cp #10448

Merged
merged 1 commit into from Mar 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,20 @@ end

# The following use Unix command line facilites

cp(src::AbstractString, dst::AbstractString) = FS.sendfile(src, dst)
function cp(src::AbstractString, dst::AbstractString; recursive::Bool=false)
if islink(src) || !isdir(src)
FS.sendfile(src, dst)
elseif recursive
mkdir(dst)
for p in readdir(src)
cp(joinpath(src, p), joinpath(dst, p), recursive=recursive)
end
else
throw(ArgumentError(string("'$src' is a directory. ",
"Use `cp(src, dst, recursive=true)` ",
"to copy directories recursively.")))
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably have an else here that throws an error if the user tries to copy a directory without recursive.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, should we create a new error for this? Python throws IsADirectoryError.

end
mv(src::AbstractString, dst::AbstractString) = FS.rename(src, dst)

function touch(path::AbstractString)
Expand Down
32 changes: 32 additions & 0 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,38 @@ write(af, "This is indeed a test")
bfile = joinpath(dir, "b.txt")
cp(afile, bfile)

mktempdir() do tmpdir
src = joinpath(tmpdir, "src")
dst = joinpath(tmpdir, "dst")
mkdir(src)

@test_throws ArgumentError cp(src, dst)
end

# Recursive copy
mktempdir() do tmpdir
src = joinpath(tmpdir, "src")
dst = joinpath(tmpdir, "dst")
mkdir(src)
touch(joinpath(src, "foo"))
mkdir(joinpath(src, "bar"))
touch(joinpath(src, "bar", "qux"))

cp(src, dst, recursive=true)
@test isfile(joinpath(dst, "foo"))
@test isfile(joinpath(dst, "bar", "qux"))
end

# issue #10434
mktempdir() do tmpdir
src = joinpath(tmpdir, "src")
dst = joinpath(tmpdir, "dst")
mkdir(src)

try cp(src, dst) end
@test !ispath(dst)
end

# issue #8698
cfile = joinpath(dir, "c.txt")
open(cfile, "w") do cf
Expand Down