From ce2510665f579e2937cd4cb6bf78db6cc633b162 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 13 Nov 2015 10:07:54 +0100 Subject: [PATCH 1/6] Extend mktemp to support file suffixes. --- base/docs/helpdb.jl | 4 ++-- base/file.jl | 18 +++++++++++------- test/file.jl | 6 ++++++ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/base/docs/helpdb.jl b/base/docs/helpdb.jl index e93a0095f2dd5..9bd5801833022 100644 --- a/base/docs/helpdb.jl +++ b/base/docs/helpdb.jl @@ -6114,9 +6114,9 @@ See also: :func:`extrema` that returns ``(minimum(x), maximum(x))`` minmax doc""" - mktemp([parent=tempdir()]) + mktemp([parent=tempdir()]; suffix="") -Returns `(path, io)`, where `path` is the path of a new temporary file in `parent` and `io` is an open file object for this path. +Returns `(path, io)`, where `path` is the path of a new temporary file in `parent` (with suffix `suffix`) and `io` is an open file object for this path. """ mktemp(?) diff --git a/base/file.jl b/base/file.jl index afd1ed3652c91..d711bc54001a3 100644 --- a/base/file.jl +++ b/base/file.jl @@ -188,9 +188,13 @@ end tempdir() = dirname(tempname()) # Create and return the name of a temporary file along with an IOStream -function mktemp(parent=tempdir()) - b = joinpath(parent, "tmpXXXXXX") - p = ccall(:mkstemp, Int32, (Ptr{UInt8},), b) # modifies b +function mktemp(parent=tempdir(); suffix="") + b = joinpath(parent, "tmpXXXXXX$suffix") + if length(suffix) > 0 + p = ccall(:mkstemps, Int32, (Ptr{UInt8}, Cint), b, length(suffix)) + else + p = ccall(:mkstemp, Int32, (Ptr{UInt8},), b) + end systemerror(:mktemp, p == -1) return (b, fdio(p, true)) end @@ -225,8 +229,8 @@ function tempname(temppath::AbstractString,uunique::UInt32) resize!(tname,lentname+1) return utf8(UTF16String(tname)) end -function mktemp(parent=tempdir()) - filename = tempname(parent, UInt32(0)) +function mktemp(parent=tempdir(); suffix="") + filename = tempname(parent, UInt32(0)) * suffix return (filename, Base.open(filename, "r+")) end function mktempdir(parent=tempdir()) @@ -246,8 +250,8 @@ function mktempdir(parent=tempdir()) end end -function mktemp(fn::Function, parent=tempdir()) - (tmp_path, tmp_io) = mktemp(parent) +function mktemp(fn::Function, parent=tempdir(); suffix="") + (tmp_path, tmp_io) = mktemp(parent, suffix=suffix) try fn(tmp_path, tmp_io) finally diff --git a/test/file.jl b/test/file.jl index f8999120bcdf7..94659bde14182 100644 --- a/test/file.jl +++ b/test/file.jl @@ -219,6 +219,12 @@ close(f) @test readall(p) == "Here is some text" rm(p) +(p, f) = mktemp(suffix=".foo") +close(f) +@test isfile(p) == true +@test endswith(p, ".foo") == true +rm(p) + let tmp_path = mktemp() do p, io @test isfile(p) From d71f84cabef8ccda150d312a66457ec1176ab96f Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Mon, 16 Nov 2015 16:43:01 +0100 Subject: [PATCH 2/6] Replace calls to mkstemp/mkstemps to only mkstemps. --- base/file.jl | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/base/file.jl b/base/file.jl index d711bc54001a3..73aaf607cbcd6 100644 --- a/base/file.jl +++ b/base/file.jl @@ -190,11 +190,7 @@ tempdir() = dirname(tempname()) # Create and return the name of a temporary file along with an IOStream function mktemp(parent=tempdir(); suffix="") b = joinpath(parent, "tmpXXXXXX$suffix") - if length(suffix) > 0 - p = ccall(:mkstemps, Int32, (Ptr{UInt8}, Cint), b, length(suffix)) - else - p = ccall(:mkstemp, Int32, (Ptr{UInt8},), b) - end + p = ccall(:mkstemps, Int32, (Ptr{UInt8}, Cint), b, length(suffix)) systemerror(:mktemp, p == -1) return (b, fdio(p, true)) end From 825b7e60953154b9a7d6537177f4222a2a88be7d Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Mon, 16 Nov 2015 16:43:29 +0100 Subject: [PATCH 3/6] Compact test. --- test/file.jl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/file.jl b/test/file.jl index 94659bde14182..3a38d4695f9fe 100644 --- a/test/file.jl +++ b/test/file.jl @@ -219,11 +219,10 @@ close(f) @test readall(p) == "Here is some text" rm(p) -(p, f) = mktemp(suffix=".foo") -close(f) -@test isfile(p) == true -@test endswith(p, ".foo") == true -rm(p) +mktemp(suffix=".foo") do p, f + @test isfile(p) == true + @test endswith(p, ".foo") == true +end let tmp_path = mktemp() do p, io From 8be70f3521157ad1dded69a654e88d49ed903e44 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Mon, 16 Nov 2015 16:44:27 +0100 Subject: [PATCH 4/6] Try-out for mktemp(suffix) for Windows. --- base/file.jl | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/base/file.jl b/base/file.jl index 73aaf607cbcd6..63016d5792047 100644 --- a/base/file.jl +++ b/base/file.jl @@ -226,7 +226,17 @@ function tempname(temppath::AbstractString,uunique::UInt32) return utf8(UTF16String(tname)) end function mktemp(parent=tempdir(); suffix="") - filename = tempname(parent, UInt32(0)) * suffix + filename = tempname(parent, UInt32(0)) + if length(suffix) > 0 + if !endswith(filename, ".TMP") + error("mktemp failed: invalid result by GetTempFileName") + end + new_filename = filename[1:length(filename)-4] * suffix + # NOTE: new_filename is not guaranteed to exist, + # but at least then mv will error out. + mv(filename, new_filename) + filename = new_filename + end return (filename, Base.open(filename, "r+")) end function mktempdir(parent=tempdir()) From 7c9ec493a60337536e71564a53a5caf70a3ee42c Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Tue, 17 Nov 2015 10:12:23 +0100 Subject: [PATCH 5/6] Fix .tmp suffix detection in mktemp. --- base/file.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/base/file.jl b/base/file.jl index 63016d5792047..db54beb4ec1bf 100644 --- a/base/file.jl +++ b/base/file.jl @@ -228,10 +228,11 @@ end function mktemp(parent=tempdir(); suffix="") filename = tempname(parent, UInt32(0)) if length(suffix) > 0 - if !endswith(filename, ".TMP") + fnlen = length(filename) + if fnlen < 4 || lowercase(filename[fnlen-3:end]) != ".tmp" error("mktemp failed: invalid result by GetTempFileName") end - new_filename = filename[1:length(filename)-4] * suffix + new_filename = filename[1:fnlen-4] * suffix # NOTE: new_filename is not guaranteed to exist, # but at least then mv will error out. mv(filename, new_filename) From f6617599e0b69bf0a1b765f5e74038972f8c53fe Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Tue, 17 Nov 2015 11:45:46 +0100 Subject: [PATCH 6/6] Fix a wrong comment. [ci skip] --- base/file.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/file.jl b/base/file.jl index db54beb4ec1bf..d650c59ee08bd 100644 --- a/base/file.jl +++ b/base/file.jl @@ -233,7 +233,7 @@ function mktemp(parent=tempdir(); suffix="") error("mktemp failed: invalid result by GetTempFileName") end new_filename = filename[1:fnlen-4] * suffix - # NOTE: new_filename is not guaranteed to exist, + # NOTE: new_filename is not guaranteed to be nonexistent, # but at least then mv will error out. mv(filename, new_filename) filename = new_filename