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

Use UUID's to generate random tempname on Windows #33785

Merged
merged 5 commits into from
Jan 18, 2020
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
31 changes: 19 additions & 12 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -517,20 +517,27 @@ function mktemp(parent::AbstractString=tempdir(); cleanup::Bool=true)
return (filename, Base.open(filename, "r+"))
end

# generate a random string from random bytes
function _rand_string()
nchars = 10
A = Vector{UInt8}(undef, nchars)
ccall((:SystemFunction036, :Advapi32), stdcall, UInt8, (Ptr{Cvoid}, UInt32), A, sizeof(A))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function can fail. The return value should be checked, but there's not much we can do to recover to just throw an error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we do the same thing in the stdlib, so I'm gonna merge this and then fix them both at once.


slug = Base.StringVector(10)
chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i = 1:nchars
slug[i] = chars[(A[i] % length(chars)) + 1]
end
return name = String(slug)
end

function tempname(parent::AbstractString=tempdir(); cleanup::Bool=true)
isdir(parent) || throw(ArgumentError("$(repr(parent)) is not a directory"))
seed::UInt32 = rand(UInt32)
while true
if (seed & typemax(UInt16)) == 0
seed += 1
end
filename = _win_tempname(parent, seed)
if !ispath(filename)
cleanup && temp_cleanup_later(filename)
return filename
end
seed += 1
end
name = _rand_string()
filename = joinpath(parent, temp_prefix * name)
musm marked this conversation as resolved.
Show resolved Hide resolved
@assert !ispath(filename)
cleanup && temp_cleanup_later(filename)
return filename
end

else # !windows
Expand Down
12 changes: 12 additions & 0 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ end

using Random

@testset "that temp names are actually unique" begin
temps = [tempname(cleanup=false) for _ = 1:100]
@test allunique(temps)
temps = map(1:100) do _
path, io = mktemp(cleanup=false)
close(io)
rm(path, force=true)
return path
musm marked this conversation as resolved.
Show resolved Hide resolved
end
@test allunique(temps)
end

@testset "tempname with parent" begin
t = tempname()
@test dirname(t) == tempdir()
Expand Down