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: f(::Function) variants for mktemp and mktempdir #9017

Merged
merged 3 commits into from Nov 18, 2014
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
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