Skip to content

Commit

Permalink
replace rmdir with rm(path; recursive)
Browse files Browse the repository at this point in the history
remove isfileorlink
  • Loading branch information
JeffBezanson committed Jun 20, 2014
1 parent 1e14b2d commit a2d4dc9
Show file tree
Hide file tree
Showing 17 changed files with 42 additions and 54 deletions.
1 change: 1 addition & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,4 @@ end
scale!{T<:Base.LinAlg.BlasReal}(X::Array{T}, s::Complex) = error("scale!: Cannot scale a real array by a complex value in-place. Use scale(X::Array{Real}, s::Complex) instead.")

@deprecate which(f::Callable, args...) @which f(args...)
@deprecate rmdir rm
2 changes: 0 additions & 2 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,6 @@ export
isfifo,
isfile,
islink,
isfileorlink,
ispath,
isreadable,
issetgid,
Expand All @@ -1256,7 +1255,6 @@ export
operm,
pwd,
rm,
rmdir,
stat,
symlink,
tempdir,
Expand Down
20 changes: 11 additions & 9 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,24 @@ end
mkdir(path::String, mode::Signed) = error("mode must be an unsigned integer; try 0o$mode")
mkpath(path::String, mode::Signed) = error("mode must be an unsigned integer; try 0o$mode")

function rmdir(path::String, recursive::Bool=false)
if recursive
for p=readdir(path)
p = joinpath(path, p)
isfileorlink(p) ? rm(p) : rmdir(p, true)
function rm(path::String; recursive::Bool=false)
if islink(path) || !isdir(path)
FS.unlink(path)
else
if recursive
for p in readdir(path)
rm(joinpath(path, p), recursive=true)
end
end
@unix_only ret = ccall(:rmdir, Int32, (Ptr{Uint8},), bytestring(path))
@windows_only ret = ccall(:_wrmdir, Int32, (Ptr{Uint16},), utf16(path))
systemerror(:rmdir, ret != 0)
end
@unix_only ret = ccall(:rmdir, Int32, (Ptr{Uint8},), bytestring(path))
@windows_only ret = ccall(:_wrmdir, Int32, (Ptr{Uint16},), utf16(path))
systemerror(:rmdir, ret != 0)
end


# The following use Unix command line facilites

rm(path::String) = FS.unlink(path)
cp(src::String, dst::String) = FS.sendfile(src, dst)
mv(src::String, dst::String) = FS.rename(src, dst)
touch(path::String) = run(`touch $path`)
Expand Down
2 changes: 1 addition & 1 deletion base/pkg/cache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function prefetch{S<:String}(pkg::String, url::String, sha1s::Vector{S})
info("Cloning cache of $pkg from $url")
try Git.run(`clone -q --mirror $url $cache`)
catch
rmdir(cache, true)
rm(cache, recursive=true)
rethrow()
end
end
Expand Down
2 changes: 1 addition & 1 deletion base/pkg/dir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function init(meta::String=DEFAULT_META, branch::String=META_BRANCH)
run(`touch REQUIRE`)
end
catch e
rmdir(dir, true)
rm(dir, recursive=true)
rethrow(e)
end
end
Expand Down
2 changes: 1 addition & 1 deletion base/pkg/entry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function clone(url::String, pkg::String)
Git.run(`clone -q $url $pkg`)
Git.set_remote_url(url, dir=pkg)
catch
rmdir(pkg, true)
rm(pkg, recursive=true)
rethrow()
end
isempty(Reqs.parse("$pkg/REQUIRE")) && return
Expand Down
2 changes: 1 addition & 1 deletion base/pkg/generate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function package(
end
end
catch
isnew && rmdir(pkg, true)
isnew && rm(pkg, recursive=true)
rethrow()
end
end
Expand Down
2 changes: 1 addition & 1 deletion base/pkg/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ end

function remove(pkg::String)
isdir(".trash") || mkdir(".trash")
ispath(".trash/$pkg") && rmdir(".trash/$pkg", true)
ispath(".trash/$pkg") && rm(".trash/$pkg", recursive=true)
mv(pkg, ".trash/$pkg")
end

Expand Down
23 changes: 10 additions & 13 deletions base/stat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,23 @@ lstat(path...) = lstat(joinpath(path...))

# mode type predicates

ispath(st::StatStruct) = st.mode & 0xf000 != 0x0000
isfifo(st::StatStruct) = st.mode & 0xf000 == 0x1000
ischardev(st::StatStruct) = st.mode & 0xf000 == 0x2000
isdir(st::StatStruct) = st.mode & 0xf000 == 0x4000
isblockdev(st::StatStruct) = st.mode & 0xf000 == 0x6000
isfile(st::StatStruct) = st.mode & 0xf000 == 0x8000
islink(st::StatStruct) = st.mode & 0xf000 == 0xa000
issocket(st::StatStruct) = st.mode & 0xf000 == 0xc000
isfileorlink(st::StatStruct) = st.mode & 0xd000 == 0x8000
ispath(st::StatStruct) = st.mode & 0xf000 != 0x0000
isfifo(st::StatStruct) = st.mode & 0xf000 == 0x1000
ischardev(st::StatStruct) = st.mode & 0xf000 == 0x2000
isdir(st::StatStruct) = st.mode & 0xf000 == 0x4000
isblockdev(st::StatStruct) = st.mode & 0xf000 == 0x6000
isfile(st::StatStruct) = st.mode & 0xf000 == 0x8000
islink(st::StatStruct) = st.mode & 0xf000 == 0xa000
issocket(st::StatStruct) = st.mode & 0xf000 == 0xc000

# mode permission predicates

issetuid(st::StatStruct) = (st.mode & 0o4000) > 0
issetgid(st::StatStruct) = (st.mode & 0o2000) > 0
issticky(st::StatStruct) = (st.mode & 0o1000) > 0

isreadable(st::StatStruct) = (st.mode & 0o444) > 0
iswritable(st::StatStruct) = (st.mode & 0o222) > 0
isreadable(st::StatStruct) = (st.mode & 0o444) > 0
iswritable(st::StatStruct) = (st.mode & 0o222) > 0
isexecutable(st::StatStruct) = (st.mode & 0o111) > 0

uperm(st::StatStruct) = uint8(st.mode >> 6) & 0x7
Expand All @@ -92,7 +91,6 @@ for f in {
:isblockdev
:isfile
:islink
:isfileorlink
:issocket
:issetuid
:issetgid
Expand All @@ -108,7 +106,6 @@ for f in {
end

islink(path...) = islink(lstat(path...))
isfileorlink(path...) = isfileorlink(lstat(path...))


# some convenience functions
Expand Down
9 changes: 3 additions & 6 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1778,9 +1778,10 @@ I/O

Move a file from `src` to `dst`.

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

Delete the file at the given path. Note that this does not work on directories.
Delete the file, link, or empty directory at the given path. If ``recursive=true`` is
passed and the path is a directory, then all contents are removed recursively.

.. function:: touch(path::String)

Expand Down Expand Up @@ -5152,10 +5153,6 @@ System
Create all directories in the given ``path``, with permissions ``mode``.
``mode`` defaults to 0o777, modified by the current file creation mask.

.. function:: rmdir(path, [recursive=false])

Remove the directory named ``path``. To remove a non-empty directory you must pass recursive true.

.. function:: getpid() -> Int32

Get julia's process ID.
Expand Down
4 changes: 0 additions & 4 deletions doc/stdlib/file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ Filesystem

Returns ``true`` if ``path`` is a symbolic link, ``false`` otherwise.

.. function:: isfileorlink(path) -> Bool

Returns ``true`` if ``path`` is a regular file or a symbolic link, ``false`` otherwise.

.. function:: ispath(path) -> Bool

Returns ``true`` if ``path`` is a valid filesystem path, ``false`` otherwise.
Expand Down
15 changes: 6 additions & 9 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ end
@test isdir(dir)
@test !isfile(dir)
@test !islink(dir)
@test !isfileorlink(dir)
@test !isdir(file)
@test isfile(file)
@test !islink(file)
@test isfileorlink(file)
@test isreadable(file)
@test iswritable(file)
# Here's something else that might be UNIX-specific?
Expand All @@ -54,7 +52,6 @@ newfile = joinpath(dir, "bfile.txt")
mv(file, newfile)
@test !ispath(file)
@test isfile(newfile)
@test isfileorlink(newfile)
file = newfile

# Test renaming directories
Expand All @@ -73,9 +70,9 @@ mv(a_tmpdir, b_tmpdir)
b_stat = stat(b_tmpdir)
@test Base.samefile(a_stat, b_stat)

rmdir(b_tmpdir)
rm(b_tmpdir)

# rmdir recursive TODO add links
# rm recursive TODO add links
c_tmpdir = mktempdir()
c_subdir = joinpath(c_tmpdir, "c_subdir")
mkdir(c_subdir)
Expand All @@ -84,9 +81,9 @@ cp(newfile, c_file)

@test isdir(c_subdir)
@test isfile(c_file)
@test_throws rmdir(c_tmpdir)
@test_throws SystemError rm(c_tmpdir)

rmdir(c_tmpdir, true)
rm(c_tmpdir, recursive=true)
@test !isdir(c_tmpdir)


Expand Down Expand Up @@ -298,8 +295,8 @@ close(f)
@non_windowsxp_only rm(dirlink)

rm(file)
rmdir(subdir)
rmdir(dir)
rm(subdir)
rm(dir)

@test !ispath(file)
@test !ispath(dir)
2 changes: 1 addition & 1 deletion test/git.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ try cd(dir) do
end
git_verify(states[1:3]...)

end finally rmdir(dir, true) end
end finally rm(dir, recursive=true) end
2 changes: 1 addition & 1 deletion test/gitutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function git_setup(h::Dict, i::Dict, w::Dict, parents::String...)
# clear the repo
for line in eachline(`ls -A`)
name = chomp(line)
name == ".git" || rmdir(name, true)
name == ".git" || rm(name, recursive=true)
end

# create the head commit
Expand Down
2 changes: 1 addition & 1 deletion test/pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function temp_pkg_dir(fn::Function)

fn()
finally
rmdir(tmpdir, true)
rm(tmpdir, recursive=true)
end
end

Expand Down
4 changes: 2 additions & 2 deletions test/replcompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ c,r = test_latexcomplete(s)
# @test "CompletionFoo2" in c
# @test s[r] == "Completion"
#
# rmdir(Pkg.dir("MyAwesomePackage"))
# rmdir(Pkg.dir("CompletionFooPackage"))
# rm(Pkg.dir("MyAwesomePackage"))
# rm(Pkg.dir("CompletionFooPackage"))
#end

@unix_only begin
Expand Down
2 changes: 1 addition & 1 deletion test/unicode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@ else
for encoding in ["UTF-32BE", "UTF-32LE", "UTF-16BE", "UTF-16LE", "UTF-8"]
rm(joinpath(unicodedir,encoding*".unicode"))
end
rmdir(unicodedir)
rm(unicodedir)
end

0 comments on commit a2d4dc9

Please sign in to comment.