|
13 | 13 |
|
14 | 14 | class Dir
|
15 | 15 |
|
16 |
| - @@systmpdir ||= defined?(Etc.systmpdir) ? Etc.systmpdir : '/tmp' |
| 16 | + # Class variables are inaccessible from non-main Ractor. |
| 17 | + # And instance variables too, in Ruby 3.0. |
| 18 | + |
| 19 | + # System-wide temporary directory path |
| 20 | + SYSTMPDIR = (defined?(Etc.systmpdir) ? Etc.systmpdir.freeze : '/tmp') |
| 21 | + private_constant :SYSTMPDIR |
17 | 22 |
|
18 | 23 | ##
|
19 | 24 | # Returns the operating system's temporary file path.
|
20 | 25 |
|
21 | 26 | def self.tmpdir
|
22 |
| - ['TMPDIR', 'TMP', 'TEMP', ['system temporary path', @@systmpdir], ['/tmp']*2, ['.']*2].find do |name, dir| |
| 27 | + ['TMPDIR', 'TMP', 'TEMP', ['system temporary path', SYSTMPDIR], ['/tmp']*2, ['.']*2].find do |name, dir| |
23 | 28 | unless dir
|
24 |
| - next if !(dir = ENV[name]) or dir.empty? |
| 29 | + next if !(dir = ENV[name] rescue next) or dir.empty? |
25 | 30 | end
|
26 | 31 | dir = File.expand_path(dir)
|
27 | 32 | stat = File.stat(dir) rescue next
|
@@ -118,16 +123,17 @@ def tmpdir
|
118 | 123 | UNUSABLE_CHARS = "^,-.0-9A-Z_a-z~"
|
119 | 124 |
|
120 | 125 | # Dedicated random number generator
|
121 |
| - RANDOM = Random.new |
| 126 | + RANDOM = Object.new |
122 | 127 | class << RANDOM # :nodoc:
|
123 | 128 | # Maximum random number
|
124 | 129 | MAX = 36**6 # < 0x100000000
|
125 | 130 |
|
126 | 131 | # Returns new random string upto 6 bytes
|
127 | 132 | def next
|
128 |
| - rand(MAX).to_s(36) |
| 133 | + (::Random.urandom(4).unpack1("L")%MAX).to_s(36) |
129 | 134 | end
|
130 | 135 | end
|
| 136 | + RANDOM.freeze |
131 | 137 | private_constant :RANDOM
|
132 | 138 |
|
133 | 139 | # Generates and yields random names to create a temporary name
|
|
0 commit comments