Skip to content

Commit

Permalink
Merge pull request #9017 from ninjin/nin/mktmp_do
Browse files Browse the repository at this point in the history
RFC: f(::Function) variants for `mktemp` and `mktempdir`
  • Loading branch information
staticfloat committed Nov 18, 2014
2 parents 6537afe + 2852895 commit 3b61724
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 22 deletions.
46 changes: 33 additions & 13 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ function touch(path::AbstractString)
end
end

@unix_only begin
# Obtain a temporary filename.
@unix_only function tempname()
function tempname()
d = get(ENV, "TMPDIR", C_NULL) # tempnam ignores TMPDIR on darwin
p = ccall(:tempnam, Ptr{UInt8}, (Ptr{UInt8},Ptr{UInt8}), d, "julia")
systemerror(:tempnam, p == C_NULL)
Expand All @@ -95,15 +96,23 @@ end
end

# Obtain a temporary directory's path.
@unix_only tempdir() = dirname(tempname())
tempdir() = dirname(tempname())

# Create and return the name of a temporary file along with an IOStream
@unix_only function mktemp()
function mktemp()
b = joinpath(tempdir(), "tmpXXXXXX")
p = ccall(:mkstemp, Int32, (Ptr{UInt8}, ), b) # modifies b
return (b, fdio(p, true))
end

# Create and return the name of a temporary directory
function mktempdir()
b = joinpath(tempdir(), "tmpXXXXXX")
p = ccall(:mkdtemp, Ptr{UInt8}, (Ptr{UInt8}, ), b)
return bytestring(p)
end
end

@windows_only begin
function tempdir()
temppath = Array(UInt16,32767)
Expand All @@ -130,16 +139,7 @@ function mktemp()
filename = tempname()
return (filename, open(filename,"r+"))
end
end

# Create and return the name of a temporary directory
@unix_only function mktempdir()
b = joinpath(tempdir(), "tmpXXXXXX")
p = ccall(:mkdtemp, Ptr{UInt8}, (Ptr{UInt8}, ), b)
return bytestring(p)
end

@windows_only function mktempdir()
function mktempdir()
seed::UInt32 = rand(UInt32)
dir = tempdir()
while true
Expand All @@ -155,6 +155,26 @@ end
seed += 1
end
end
end

function mktemp(fn::Function)
(tmp_path, tmp_io) = mktemp()
try
fn(tmp_path, tmp_io)
finally
close(tmp_io)
rm(tmp_path)
end
end

function mktempdir(fn::Function)
tmpdir = mktempdir()
try
fn(tmpdir)
finally
rm(tmpdir, recursive=true)
end
end

function readdir(path::AbstractString)
# Allocate space for uv_fs_t struct
Expand Down
38 changes: 29 additions & 9 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,37 @@ close(s)
# This section tests temporary file and directory creation. #
#######################################################################

# my_tempdir = tempdir()
# @test isdir(my_tempdir) == true
my_tempdir = tempdir()
@test isdir(my_tempdir) == true

# path = tempname()
# @test ispath(path) == false
path = tempname()
@test ispath(path) == false

# (file, f) = mktemp()
# print(f, "Here is some text")
# close(f)
# @test isfile(file) == true
# @test readall(file) == "Here is some text"
(p, f) = mktemp()
print(f, "Here is some text")
close(f)
@test isfile(p) == true
@test readall(p) == "Here is some text"
rm(p)

let
tmp_path = mktemp() do p, io
@test isfile(p)
print(io, "鴨かも?")
p
end
@test tmp_path != ""
@test !isfile(tmp_path)
end

let
tmpdir = mktempdir() do d
@test isdir(d)
d
end
@test tmpdir != ""
@test !isdir(tmpdir)
end

emptyfile = joinpath(dir, "empty")
touch(emptyfile)
Expand Down

0 comments on commit 3b61724

Please sign in to comment.