-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add prefix argument to mktempdir() #23634
Closed
Closed
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
3f0526f
mktempdir() now supports prefix.
sambitdash 155ec68
Documentation error fix.
sambitdash e891f58
OS specific test anomalies detected.
sambitdash 7b371ab
A test error previously not reported fixed.
sambitdash 536c3ca
modified test cases for Windows OS
sambitdash db4a9b5
Removing the directory created by the testcase.
sambitdash 48bd1f4
Windows test case update.
sambitdash f9fc9aa
Clarifying the test cases further.
sambitdash 60ddcce
Rebasing from master
sambitdash 7f08fdf
mktempdir() now supports prefix.
sambitdash 3e71966
OS specific test anomalies detected.
sambitdash 033f6e7
modified test cases for Windows OS
sambitdash d585569
Removing the directory created by the testcase.
sambitdash c4170f2
Removing the directory created by the testcase.
sambitdash c8d4c29
Fixing formating issue with the code.
sambitdash 17f6597
Fixed comment. Removed unreachable assert.
sambitdash e5efc91
Trimming file separator from parent as well as prefix.
sambitdash 5bb17e3
Trimming file separator from parent as well as prefix.
sambitdash 2622ca2
Adding repr to ensure invalid characters are cleared
sambitdash 70346a2
Merge branch 'master' into I22922
nalimilan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -254,6 +254,8 @@ function touch(path::AbstractString) | |
end | ||
end | ||
|
||
const temp_prefix = "jl_" | ||
|
||
if Sys.iswindows() | ||
|
||
function tempdir() | ||
|
@@ -265,13 +267,17 @@ function tempdir() | |
resize!(temppath,lentemppath) | ||
return transcode(String, temppath) | ||
end | ||
|
||
tempname(uunique::UInt32=UInt32(0)) = tempname(tempdir(), uunique) | ||
const temp_prefix = cwstring("jl_") | ||
|
||
function tempname(temppath::AbstractString,uunique::UInt32) | ||
tempp = cwstring(temppath) | ||
temppfx = cwstring(temp_prefix) | ||
tname = Vector{UInt16}(32767) | ||
uunique = ccall(:GetTempFileNameW,stdcall,UInt32,(Ptr{UInt16},Ptr{UInt16},UInt32,Ptr{UInt16}), tempp,temp_prefix,uunique,tname) | ||
lentname = findfirst(iszero,tname)-1 | ||
uunique = ccall(:GetTempFileNameW,stdcall,UInt32, | ||
(Ptr{UInt16}, Ptr{UInt16}, UInt32, Ptr{UInt16}), | ||
tempp, temppfx, uunique, tname) | ||
lentname = findfirst(iszero, tname)-1 | ||
if uunique == 0 || lentname <= 0 | ||
error("GetTempFileName failed: $(Libc.FormatMessage())") | ||
end | ||
|
@@ -284,22 +290,6 @@ function mktemp(parent=tempdir()) | |
return (filename, Base.open(filename, "r+")) | ||
end | ||
|
||
function mktempdir(parent=tempdir()) | ||
seed::UInt32 = rand(UInt32) | ||
while true | ||
if (seed & typemax(UInt16)) == 0 | ||
seed += 1 | ||
end | ||
filename = tempname(parent, seed) | ||
ret = ccall(:_wmkdir, Int32, (Ptr{UInt16},), cwstring(filename)) | ||
if ret == 0 | ||
return filename | ||
end | ||
systemerror(:mktempdir, Libc.errno()!=Libc.EEXIST) | ||
seed += 1 | ||
end | ||
end | ||
|
||
else # !windows | ||
# Obtain a temporary filename. | ||
function tempname() | ||
|
@@ -322,13 +312,6 @@ function mktemp(parent=tempdir()) | |
return (b, fdio(p, true)) | ||
end | ||
|
||
# Create and return the name of a temporary directory | ||
function mktempdir(parent=tempdir()) | ||
b = joinpath(parent, "tmpXXXXXX") | ||
p = ccall(:mkdtemp, Cstring, (Cstring,), b) | ||
systemerror(:mktempdir, p == C_NULL) | ||
return unsafe_string(p) | ||
end | ||
|
||
end # os-test | ||
|
||
|
@@ -356,13 +339,42 @@ is an open file object for this path. | |
mktemp(parent) | ||
|
||
""" | ||
mktempdir(parent=tempdir()) | ||
mktempdir(parent=tempdir(); prefix="$(repr(temp_prefix))") | ||
|
||
Create a temporary directory in the `parent` directory and return its path. | ||
If `parent` does not exist, throw an error. | ||
An optional `prefix` to the directory name can be provided. | ||
""" | ||
mktempdir(parent) | ||
function mktempdir(parent=tempdir(); prefix=temp_prefix) | ||
i = endof(parent) | ||
while i >= 1 && parent[i:i] == path_separator | ||
i -= 1 | ||
end | ||
parent = parent[1:i] | ||
i = 1 | ||
while i <= endof(prefix) && prefix[i:i] == path_separator | ||
i += 1 | ||
end | ||
prefix = prefix[i:end] | ||
|
||
tpath = "$(parent)$(path_separator)$(prefix)XXXXXX" | ||
|
||
req = Libc.malloc(_sizeof_uv_fs) | ||
try | ||
ret = ccall(:uv_fs_mkdtemp, Int32, | ||
(Ptr{Void}, Ptr{Void}, Cstring, Ptr{Void}), | ||
eventloop(), req, tpath, C_NULL) | ||
if ret < 0 | ||
ccall(:uv_fs_req_cleanup, Void, (Ptr{Void},), req) | ||
uv_error("mktempdir", ret) | ||
end | ||
path = unsafe_string(ccall(:jl_uv_fs_t_path, Ptr{Cchar}, (Ptr{Void},), req)) | ||
ccall(:uv_fs_req_cleanup, Void, (Ptr{Void},), req) | ||
return path | ||
finally | ||
Libc.free(req) | ||
end | ||
end | ||
|
||
""" | ||
mktemp(f::Function, parent=tempdir()) | ||
|
@@ -381,13 +393,14 @@ function mktemp(fn::Function, parent=tempdir()) | |
end | ||
|
||
""" | ||
mktempdir(f::Function, parent=tempdir()) | ||
mktempdir(f::Function, parent=tempdir(); prefix="$(repr(temp_prefix))") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this will include two sets of quotes (quotes are included in the output of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See previous comment: #23634 (comment) |
||
|
||
Apply the function `f` to the result of [`mktempdir(parent)`](@ref) and remove the | ||
Apply the function `f` to the result of [`mktempdir(parent; prefix)`](@ref) and remove the | ||
temporary directory upon completion. | ||
An optional `prefix` to the directory name can be provided. | ||
""" | ||
function mktempdir(fn::Function, parent=tempdir()) | ||
tmpdir = mktempdir(parent) | ||
function mktempdir(fn::Function, parent=tempdir(); prefix=temp_prefix) | ||
tmpdir = mktempdir(parent; prefix=prefix) | ||
try | ||
fn(tmpdir) | ||
finally | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
prefix
contains the letterX
this is going to be wrong; it's hard to say if that's avoidable or not, given the way the underlyinguv_fs_mkdtemp
API works.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is that the last 6 characters need to be
X
, but it doesn't matter what the other characters are.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. It's not even clear why these chars need to be
X
rather than any other ASCII char.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Links to documentation for
uv_fs_mkdtemp
andmkdtemp
given below:http://docs.libuv.org/en/v1.x/fs.html
https://linux.die.net/man/3/mkdtemp