summaryrefslogtreecommitdiff
path: root/tool/lib
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2024-04-23 11:33:50 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2024-04-23 13:38:39 +0900
commit777daae04992fdb472e2382a08aefc9fd2366159 (patch)
treefe4290375a8391ec4f2173b455541f29d1274397 /tool/lib
parente61f76ce9e098143c88e58f4ea91b15eb85ab793 (diff)
Shorten tmpdir path
Diffstat (limited to 'tool/lib')
-rw-r--r--tool/lib/_tmpdir.rb26
1 files changed, 18 insertions, 8 deletions
diff --git a/tool/lib/_tmpdir.rb b/tool/lib/_tmpdir.rb
index cb2a9f69f1..52baa6e00b 100644
--- a/tool/lib/_tmpdir.rb
+++ b/tool/lib/_tmpdir.rb
@@ -1,14 +1,23 @@
-require "tmpdir"
-require "fileutils"
-
template = "rubytest."
-if (tmpdir = Dir.mktmpdir(template)).size > 50 and File.directory?("/tmp")
+
+# This path is only for tests.
+# Assume the directory by these environment variables are safe.
+base = [ENV["TMPDIR"], ENV["TMP"], "/tmp"].find do |tmp|
+ next unless tmp and tmp.size <= 50 and File.directory?(tmp)
# On macOS, the default TMPDIR is very long, inspite of UNIX socket
# path length is limited.
- # Assume "/tmp" always exists on UNIX-like systems where UNIX socket
- # is available, otherwise no need to shorten TMPDIR.
- Dir.rmdir(tmpdir)
- tmpdir = Dir.mktmpdir(template, "/tmp")
+ #
+ # Also Rubygems creates its own temporary directory per tests, and
+ # some tests copy the full path of gemhome there. In that caes, the
+ # path contains both temporary names twice, and can exceed path name
+ # limit very easily.
+ tmp
+end
+begin
+ tmpdir = File.join(base, template + Random.new_seed.to_s(36)[-6..-1])
+ Dir.mkdir(tmpdir, 0o700)
+rescue Errno::EEXIST
+ retry
end
# warn "tmpdir(#{tmpdir.size}) = #{tmpdir}"
@@ -80,6 +89,7 @@ END {
File.unlink(path)
end
end
+ require "fileutils"
FileUtils.rm_rf(tmpdir)
end
end