Skip to content

Commit

Permalink
Merge branch 'rmdir_recursive' of github.com:hayd/julia into hayd-rmd…
Browse files Browse the repository at this point in the history
…ir_recursive

Conflicts:
	base/pkg/write.jl
  • Loading branch information
JeffBezanson committed Jun 20, 2014
2 parents 460e5e5 + 2b3cf04 commit 1e14b2d
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 21 deletions.
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,7 @@ export
isfifo,
isfile,
islink,
isfileorlink,
ispath,
isreadable,
issetgid,
Expand Down
8 changes: 7 additions & 1 deletion base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ 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)
function rmdir(path::String, recursive::Bool=false)
if recursive
for p=readdir(path)
p = joinpath(path, p)
isfileorlink(p) ? rm(p) : rmdir(p, 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)
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
run(`rm -rf $cache`)
rmdir(cache, 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
run(`rm -rf $dir`)
rmdir(dir, 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
run(`rm -rf $pkg`)
rmdir(pkg, 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 && run(`rm -rf $pkg`)
isnew && rmdir(pkg, 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") && run(`rm -rf .trash/$pkg`)
ispath(".trash/$pkg") && rmdir(".trash/$pkg", true)
mv(pkg, ".trash/$pkg")
end

Expand Down
24 changes: 14 additions & 10 deletions base/stat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,24 @@ 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
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

# 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 @@ -91,6 +92,7 @@ for f in {
:isblockdev
:isfile
:islink
:isfileorlink
:issocket
:issetuid
:issetgid
Expand All @@ -106,6 +108,8 @@ for f in {
end

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


# some convenience functions

Expand Down
4 changes: 2 additions & 2 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5152,9 +5152,9 @@ 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)
.. function:: rmdir(path, [recursive=false])

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

.. function:: getpid() -> Int32

Expand Down
4 changes: 4 additions & 0 deletions doc/stdlib/file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ 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
18 changes: 18 additions & 0 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ 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 @@ -52,6 +54,7 @@ newfile = joinpath(dir, "bfile.txt")
mv(file, newfile)
@test !ispath(file)
@test isfile(newfile)
@test isfileorlink(newfile)
file = newfile

# Test renaming directories
Expand All @@ -72,6 +75,21 @@ b_stat = stat(b_tmpdir)

rmdir(b_tmpdir)

# rmdir recursive TODO add links
c_tmpdir = mktempdir()
c_subdir = joinpath(c_tmpdir, "c_subdir")
mkdir(c_subdir)
c_file = joinpath(c_tmpdir, "cfile.txt")
cp(newfile, c_file)

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

rmdir(c_tmpdir, true)
@test !isdir(c_tmpdir)


#######################################################################
# This section tests file watchers. #
#######################################################################
Expand Down
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 run(`rm -rf $dir`) end
end finally rmdir(dir, 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" || run(`rm -rf $name`)
name == ".git" || rmdir(name, 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
run(`rm -rf $tmpdir`)
rmdir(tmpdir, true)
end
end

Expand Down

0 comments on commit 1e14b2d

Please sign in to comment.