Skip to content

Commit

Permalink
Use libuv tempdir
Browse files Browse the repository at this point in the history
  • Loading branch information
musm committed Jun 14, 2019
1 parent 5479d1d commit 8b354e2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
44 changes: 26 additions & 18 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -416,18 +416,36 @@ function touch(path::AbstractString)
path
end

const temp_prefix = "jl_"

if Sys.iswindows()
"""
tempdir()
Obtain the path of a temporary directory (possibly shared with other processes). On Windows,
`tempdir()` uses the first environment variable found in the ordered list `TMP`, `TEMP`,
`USERPROFILE`. On all other operating systems, `tempdir()` uses the first environment
variable found in the ordered list `TMPDIR`, `TMP`, `TEMP`, and `TEMPDIR`. If none of these are
found, the path `"/tmp"` is used.
"""
function tempdir()
temppath = Vector{UInt16}(undef, 32767)
lentemppath = ccall(:GetTempPathW, stdcall, UInt32, (UInt32, Ptr{UInt16}), length(temppath), temppath)
windowserror("GetTempPath", lentemppath >= length(temppath) || lentemppath == 0)
resize!(temppath, lentemppath)
return transcode(String, temppath)
path_max = 1024
buf = Base.StringVector(path_max - 1) # space for null-terminator implied by StringVector
sz = RefValue{Csize_t}(path_max)
while true
rc = ccall(:uv_os_tmpdir, Cint, (Ptr{UInt8}, Ptr{Csize_t}), buf, sz)
if rc == 0
resize!(buf, sz[])
return String(buf)
elseif rc == Base.UV_ENOBUFS
resize!(buf, sz[] - 1) # space for null-terminator implied by StringVector
else
uv_error(:tmpdir, rc)
end
end
end

const temp_prefix = "jl_"

if Sys.iswindows()

function _win_tempname(temppath::AbstractString, uunique::UInt32)
tempp = cwstring(temppath)
temppfx = cwstring(temp_prefix)
Expand Down Expand Up @@ -473,9 +491,6 @@ function tempname()
return s
end

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

# Create and return the name of a temporary file along with an IOStream
function mktemp(parent=tempdir())
b = joinpath(parent, temp_prefix * "XXXXXX")
Expand All @@ -488,13 +503,6 @@ end
end # os-test


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

"""
tempname()
Expand Down
18 changes: 16 additions & 2 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,22 @@ close(s)
end
end

my_tempdir = tempdir()
@test isdir(my_tempdir) == true
@testset "tempdir" begin
my_tempdir = tempdir()
@test isdir(my_tempdir)
@test my_tempdir[end] != '/'
@test my_tempdir[end] != '\\'

var = Sys.iswindows() ? "TMP" : "TMPDIR"
PATH_PREFIX = Sys.iswindows() ? "C:\\" : "/tmp/"
MAX_PATH = (Sys.iswindows() ? 260 : 1024) - length(PATH_PREFIX) - 1 # null-termination character
for i = 0:9
local tmp = PATH_PREFIX * "x"^MAX_PATH * "123456789"[1:i]
@test withenv(var => tmp) do
tempdir()
end == tmp
end
end

let path = tempname()
# issue #9053
Expand Down

0 comments on commit 8b354e2

Please sign in to comment.