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 5 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
10 changes: 7 additions & 3 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -518,14 +518,15 @@ 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_on_exit=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.
If `parent` does not exist, throw an error. If `remove_on_exit` is true, then
DilumAluthge marked this conversation as resolved.
Show resolved Hide resolved
the temporary directory will be removed when Julia exits.
"""
function mktempdir(parent=tempdir(); prefix=temp_prefix)
function mktempdir(parent=tempdir(); prefix=temp_prefix, remove_on_exit=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 +545,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_on_exit
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_on_exit=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