From 4fb48358ab3788a2875e9324bdf622d6fcc67280 Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Sat, 15 Jun 2019 14:37:12 -0400 Subject: [PATCH 1/7] Add `remove_on_exit` keyword argument to `mktempdir(parent=tempdir())` --- base/file.jl | 10 +++++++--- test/file.jl | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/base/file.jl b/base/file.jl index b7f98c7323774..9c2a5ddbc58d4 100644 --- a/base/file.jl +++ b/base/file.jl @@ -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 +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" @@ -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) diff --git a/test/file.jl b/test/file.jl index eec6bcf79d3a5..494032c03252e 100644 --- a/test/file.jl +++ b/test/file.jl @@ -6,6 +6,7 @@ starttime = time() pwd_ = pwd() dir = mktempdir() +dir_removed_on_exit = mktempdir(; remove_on_exit = true) file = joinpath(dir, "afile.txt") # like touch, but lets the operating system update the timestamp # for greater precision on some platforms (windows) From 3faa88153892512a926864ad2a9e2e0db1890c03 Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Mon, 17 Jun 2019 07:33:40 -0400 Subject: [PATCH 2/7] Update file.jl --- test/file.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/file.jl b/test/file.jl index 494032c03252e..363bccda178bf 100644 --- a/test/file.jl +++ b/test/file.jl @@ -6,7 +6,7 @@ starttime = time() pwd_ = pwd() dir = mktempdir() -dir_removed_on_exit = mktempdir(; remove_on_exit = true) +dir_removed_on_exit = mktempdir(; remove_on_exit=true) file = joinpath(dir, "afile.txt") # like touch, but lets the operating system update the timestamp # for greater precision on some platforms (windows) From 03ecfff5de23f47bba97afdd7b91d9bd11522f63 Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Mon, 17 Jun 2019 07:34:04 -0400 Subject: [PATCH 3/7] Update base/file.jl Co-Authored-By: Alex Arslan --- base/file.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/file.jl b/base/file.jl index 9c2a5ddbc58d4..a02e4fb1fb39f 100644 --- a/base/file.jl +++ b/base/file.jl @@ -518,7 +518,7 @@ is an open file object for this path. mktemp(parent) """ - mktempdir(parent=tempdir(); prefix=$(repr(temp_prefix)), remove_on_exit = false) + 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. From 3d40f8880fd4846e1269e0028711d55be5c94cc4 Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Mon, 17 Jun 2019 07:37:48 -0400 Subject: [PATCH 4/7] Update file.jl --- base/file.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/file.jl b/base/file.jl index a02e4fb1fb39f..34a2c87665b02 100644 --- a/base/file.jl +++ b/base/file.jl @@ -526,7 +526,7 @@ Additionally, any trailing `X` characters may be replaced with random characters If `parent` does not exist, throw an error. If `remove_on_exit` is true, then the temporary directory will be removed when Julia exits. """ -function mktempdir(parent=tempdir(); prefix=temp_prefix, remove_on_exit = false) +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" @@ -546,7 +546,7 @@ function mktempdir(parent=tempdir(); prefix=temp_prefix, remove_on_exit = false) 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)) + atexit(() -> rm(path; force=true, recursive=true)) end return path finally From e79ef629819f41dd494345d0c333b11f803b8ef6 Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Mon, 17 Jun 2019 13:41:30 -0400 Subject: [PATCH 5/7] A better test --- test/file.jl | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test/file.jl b/test/file.jl index 363bccda178bf..3c1727d1d9e68 100644 --- a/test/file.jl +++ b/test/file.jl @@ -6,7 +6,18 @@ starttime = time() pwd_ = pwd() dir = mktempdir() -dir_removed_on_exit = mktempdir(; remove_on_exit=true) +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) From 69388ae062fa53586bbd3a2b0bf40045b3fbb140 Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Thu, 1 Aug 2019 20:30:35 -0400 Subject: [PATCH 6/7] Rename `remove_on_exit` to `remove_atexit` --- base/file.jl | 8 ++++---- test/file.jl | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/base/file.jl b/base/file.jl index 34a2c87665b02..dee0ca5b26b74 100644 --- a/base/file.jl +++ b/base/file.jl @@ -518,15 +518,15 @@ is an open file object for this path. mktemp(parent) """ - mktempdir(parent=tempdir(); prefix=$(repr(temp_prefix)), remove_on_exit=false) + 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. If `remove_on_exit` is true, then +If `parent` does not exist, throw an error. If `remove_atexit` is true, then the temporary directory will be removed when Julia exits. """ -function mktempdir(parent=tempdir(); prefix=temp_prefix, remove_on_exit=false) +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" @@ -545,7 +545,7 @@ function mktempdir(parent=tempdir(); prefix=temp_prefix, remove_on_exit=false) 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 + if remove_atexit atexit(() -> rm(path; force=true, recursive=true)) end return path diff --git a/test/file.jl b/test/file.jl index 3c1727d1d9e68..cc58740ac57e6 100644 --- a/test/file.jl +++ b/test/file.jl @@ -7,7 +7,7 @@ starttime = time() pwd_ = pwd() dir = mktempdir() code = """ -child_dir = mktempdir(; remove_on_exit=true) +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) From ad510e8f3f2bdb729bf3436062864d3c3907e2d6 Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Thu, 1 Aug 2019 20:34:29 -0400 Subject: [PATCH 7/7] Add `!!! compat "Julia 1.3"` heading --- base/file.jl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/base/file.jl b/base/file.jl index dee0ca5b26b74..24b5054a09b1b 100644 --- a/base/file.jl +++ b/base/file.jl @@ -523,8 +523,11 @@ mktemp(parent) 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 `remove_atexit` is true, then -the temporary directory will be removed when Julia exits. +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, remove_atexit=false) if isempty(parent) || occursin(path_separator_re, parent[end:end])