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

RFC: Add remove_atexit keyword argument to mktempdir(parent) #32335

Closed
wants to merge 7 commits into from
Closed
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
11 changes: 9 additions & 2 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -518,14 +518,18 @@ is an open file object for this path.
mktemp(parent)

"""
mktempdir(parent=tempdir(); prefix=$(repr(temp_prefix)))
mktempdir(parent=tempdir(); prefix=$(repr(temp_prefix)), remove_atexit=false)

Create a temporary directory in the `parent` directory with a name
constructed from the given prefix and a random suffix, and return its path.
Additionally, any trailing `X` characters may be replaced with random characters.
If `parent` does not exist, throw an error.

!!! compat "Julia 1.3"
The `remove_atexit` keyword argument requires at least Julia 1.3.
If `remove_atexit` is true, then the temporary directory will be removed when Julia exits.
"""
function mktempdir(parent=tempdir(); prefix=temp_prefix)
function mktempdir(parent=tempdir(); prefix=temp_prefix, remove_atexit=false)
if isempty(parent) || occursin(path_separator_re, parent[end:end])
# append a path_separator only if parent didn't already have one
tpath = "$(parent)$(prefix)XXXXXX"
Expand All @@ -544,6 +548,9 @@ function mktempdir(parent=tempdir(); prefix=temp_prefix)
end
path = unsafe_string(ccall(:jl_uv_fs_t_path, Cstring, (Ptr{Cvoid},), req))
ccall(:uv_fs_req_cleanup, Cvoid, (Ptr{Cvoid},), req)
if remove_atexit
atexit(() -> rm(path; force=true, recursive=true))
end
return path
finally
Libc.free(req)
Expand Down
12 changes: 12 additions & 0 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
starttime = time()
pwd_ = pwd()
dir = mktempdir()
code = """
child_dir = mktempdir(; remove_atexit=true)
ispath(child_dir) || error("`child_dir` is not a path")
isdir(child_dir) || error("`child_dir` is not a directory")
println(child_dir)
"""
cmd = ```
$(Base.julia_cmd()) --eval $code
```
child_dir = chomp(read(cmd, String))
@test !isdir(child_dir)
@test !ispath(child_dir)
file = joinpath(dir, "afile.txt")
# like touch, but lets the operating system update the timestamp
# for greater precision on some platforms (windows)
Expand Down