diff --git a/lib/tmpdir.rb b/lib/tmpdir.rb index 3b67164039..5098ea6195 100644 --- a/lib/tmpdir.rb +++ b/lib/tmpdir.rb @@ -13,14 +13,22 @@ class Dir - @@systmpdir ||= defined?(Etc.systmpdir) ? Etc.systmpdir : '/tmp' + # Ractor-safe method of referring to systmpdir + class << self + private + + def systmpdir + @systmpdir ||= (defined?(Etc.systmpdir) ? Etc.systmpdir : '/tmp').freeze + end + end + systmpdir ## # Returns the operating system's temporary file path. def self.tmpdir tmp = nil - ['TMPDIR', 'TMP', 'TEMP', ['system temporary path', @@systmpdir], ['/tmp']*2, ['.']*2].each do |name, dir = ENV[name]| + ['TMPDIR', 'TMP', 'TEMP', ['system temporary path', systmpdir], ['/tmp']*2, ['.']*2].each do |name, dir = ENV[name]| next if !dir dir = File.expand_path(dir) stat = File.stat(dir) rescue next @@ -117,14 +125,6 @@ def tmpdir UNUSABLE_CHARS = "^,-.0-9A-Z_a-z~" - 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() @@ -136,9 +136,10 @@ def create(basename, tmpdir=nil, max_try: nil, **opts) suffix &&= (String.try_convert(suffix) or raise ArgumentError, "unexpected suffix: #{suffix.inspect}") suffix &&= suffix.delete(UNUSABLE_CHARS) + random = Random.rand(36**6).to_s(36) begin t = Time.now.strftime("%Y%m%d") - path = "#{prefix}#{t}-#{$$}-#{RANDOM.next}"\ + path = "#{prefix}#{t}-#{$$}-#{random}"\ "#{n ? %[-#{n}] : ''}#{suffix||''}" path = File.join(tmpdir, path) yield(path, n, opts, origdir)