@@ -265,9 +265,9 @@ function tempdir()
265265 resize! (temppath,lentemppath)
266266 return transcode (String, temppath)
267267end
268- tempname (uunique :: UInt32 = UInt32 ( 0 )) = tempname ( tempdir (), uunique)
268+
269269const temp_prefix = cwstring (" jl_" )
270- function tempname (temppath:: AbstractString ,uunique:: UInt32 )
270+ function _win_tempname (temppath:: AbstractString , uunique:: UInt32 )
271271 tempp = cwstring (temppath)
272272 tname = Vector {UInt16} (uninitialized, 32767 )
273273 uunique = ccall (:GetTempFileNameW ,stdcall,UInt32,(Ptr{UInt16},Ptr{UInt16},UInt32,Ptr{UInt16}), tempp,temp_prefix,uunique,tname)
@@ -280,7 +280,7 @@ function tempname(temppath::AbstractString,uunique::UInt32)
280280end
281281
282282function mktemp (parent= tempdir ())
283- filename = tempname (parent, UInt32 (0 ))
283+ filename = _win_tempname (parent, UInt32 (0 ))
284284 return (filename, Base. open (filename, " r+" ))
285285end
286286
@@ -290,7 +290,7 @@ function mktempdir(parent=tempdir())
290290 if (seed & typemax (UInt16)) == 0
291291 seed += 1
292292 end
293- filename = tempname (parent, seed)
293+ filename = _win_tempname (parent, seed)
294294 ret = ccall (:_wmkdir , Int32, (Ptr{UInt16},), cwstring (filename))
295295 if ret == 0
296296 return filename
@@ -300,6 +300,21 @@ function mktempdir(parent=tempdir())
300300 end
301301end
302302
303+ function tempname ()
304+ parent = tempdir ()
305+ seed:: UInt32 = rand (UInt32)
306+ while true
307+ if (seed & typemax (UInt16)) == 0
308+ seed += 1
309+ end
310+ filename = _win_tempname (parent, seed)
311+ if ! ispath (filename)
312+ return filename
313+ end
314+ seed += 1
315+ end
316+ end
317+
303318else # !windows
304319# Obtain a temporary filename.
305320function tempname ()
@@ -343,7 +358,14 @@ tempdir()
343358"""
344359 tempname()
345360
346- Generate a unique temporary file path.
361+ Generate a temporary file path. This function only returns a path; no file is
362+ created. The path is likely to be unique, but this cannot be guaranteed.
363+
364+ !!! warning
365+
366+ This can lead to race conditions if another process obtains the same
367+ file name and creates the file before you are able to.
368+ Using [`mktemp()`](@ref) is recommended instead.
347369"""
348370tempname ()
349371
0 commit comments