summaryrefslogtreecommitdiff
path: root/lib/tmpdir.rb
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2020-01-29 10:12:35 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2020-01-29 10:14:15 +0900
commit98f6c74b429f9e8afccb000da4a50920479dffd6 (patch)
treecf44081d3ec2efef483b954e1d2e691c8d804acf /lib/tmpdir.rb
parent30236965057ed7b58b6f83b3103ccace67fbc2d7 (diff)
Isolate the PRNG for tmpdir/tempfile
To get rid of conflicts affected by `srand`.
Diffstat (limited to 'lib/tmpdir.rb')
-rw-r--r--lib/tmpdir.rb10
1 files changed, 9 insertions, 1 deletions
diff --git a/lib/tmpdir.rb b/lib/tmpdir.rb
index ea1d380ef1..c61365577e 100644
--- a/lib/tmpdir.rb
+++ b/lib/tmpdir.rb
@@ -110,6 +110,14 @@ class Dir
UNUSABLE_CHARS = [File::SEPARATOR, File::ALT_SEPARATOR, File::PATH_SEPARATOR, ":"].uniq.join("").freeze
+ class << (RANDOM = Random.new)
+ MAX = 36**6 # < 0x100000000
+ def next
+ rand(MAX).to_s(36)
+ end
+ end
+ private_constant :RANDOM
+
def create(basename, tmpdir=nil, max_try: nil, **opts)
origdir = tmpdir
tmpdir ||= tmpdir()
@@ -123,7 +131,7 @@ class Dir
suffix &&= suffix.delete(UNUSABLE_CHARS)
begin
t = Time.now.strftime("%Y%m%d")
- path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"\
+ path = "#{prefix}#{t}-#{$$}-#{RANDOM.next}"\
"#{n ? %[-#{n}] : ''}#{suffix||''}"
path = File.join(tmpdir, path)
yield(path, n, opts, origdir)