Bug #18933 ยป tmpdir.diff
lib/tmpdir.rb | ||
---|---|---|
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
|
||
... | ... | |
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()
|
||
... | ... | |
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)
|