Skip to content

Commit

Permalink
mktemp{,dir}: add parent directory option
Browse files Browse the repository at this point in the history
For example, `mktemp(dir)` will create a temporary file in the `dir`
directory.
  • Loading branch information
nolta committed May 12, 2015
1 parent 35cdca9 commit f8ccca2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ Library improvements
* New function `relpath` returns a relative filepath to path either from the current
directory or from an optional start directory ([#10893]).

* `mktemp` and `mktempdir` now take an optional argument to set which
directory the temporary file or directory is created in.

Deprecated or removed
---------------------

Expand Down
23 changes: 12 additions & 11 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,18 @@ end
tempdir() = dirname(tempname())

# Create and return the name of a temporary file along with an IOStream
function mktemp()
b = joinpath(tempdir(), "tmpXXXXXX")
p = ccall(:mkstemp, Int32, (Ptr{UInt8}, ), b) # modifies b
function mktemp(parent=tempdir())
b = joinpath(parent, "tmpXXXXXX")
p = ccall(:mkstemp, Int32, (Ptr{UInt8},), b) # modifies b
systemerror(:mktemp, p == -1)
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)
function mktempdir(parent=tempdir())
b = joinpath(parent, "tmpXXXXXX")
p = ccall(:mkdtemp, Ptr{UInt8}, (Ptr{UInt8},), b)
systemerror(:mktempdir, p == C_NULL)
return bytestring(p)
end
end
Expand All @@ -178,18 +180,17 @@ function tempname(temppath::AbstractString,uunique::UInt32)
resize!(tname,lentname+1)
return utf8(UTF16String(tname))
end
function mktemp()
filename = tempname()
function mktemp(parent=tempdir())
filename = tempname(parent, UInt32(0))
return (filename, open(filename,"r+"))
end
function mktempdir()
function mktempdir(parent=tempdir())
seed::UInt32 = rand(UInt32)
dir = tempdir()
while true
if (seed & typemax(UInt16)) == 0
seed += 1
end
filename = tempname(dir, seed)
filename = tempname(parent, seed)
ret = ccall(:_wmkdir, Int32, (Ptr{UInt16},), utf16(filename))
if ret == 0
return filename
Expand Down
8 changes: 4 additions & 4 deletions doc/stdlib/file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@

Obtain the path of a temporary directory (possibly shared with other processes).

.. function:: mktemp()
.. function:: mktemp([parent=tempdir()])

Returns ``(path, io)``, where ``path`` is the path of a new temporary file
and ``io`` is an open file object for this path.
in ``parent`` and ``io`` is an open file object for this path.

.. function:: mktempdir()
.. function:: mktempdir([parent=tempdir()])

Create a temporary directory and return its path.
Create a temporary directory in the ``parent`` directory and return its path.

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

Expand Down
11 changes: 11 additions & 0 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ cp(newfile, c_file)
@test isfile(c_file)
@test_throws SystemError rm(c_tmpdir)

# create temp dir in specific directory
d_tmpdir = mktempdir(c_tmpdir)
@test isdir(d_tmpdir)
@test Base.samefile(dirname(d_tmpdir), c_tmpdir)

# create temp file in specific directory
d_tmpfile,f = mktemp(c_tmpdir)
close(f)
@test isfile(d_tmpfile)
@test Base.samefile(dirname(d_tmpfile), c_tmpdir)

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

Expand Down

0 comments on commit f8ccca2

Please sign in to comment.