1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

tmpdir.rb: merged make_tmpname to create

* lib/tmpdir.rb (Dir::Tmpname#create): try conversion of prefix
  and suffix just once before loop.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60775 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2017-11-15 08:02:09 +00:00
parent 0cc57ea018
commit 25d56ea7b7

View file

@ -106,18 +106,6 @@ class Dir
Dir.tmpdir Dir.tmpdir
end end
def make_tmpname((prefix, suffix), n)
prefix = (String.try_convert(prefix) or
raise ArgumentError, "unexpected prefix: #{prefix.inspect}")
suffix &&= (String.try_convert(suffix) or
raise ArgumentError, "unexpected suffix: #{suffix.inspect}")
t = Time.now.strftime("%Y%m%d")
path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}".dup
path << "-#{n}" if n
path << suffix if suffix
path
end
def create(basename, tmpdir=nil, max_try: nil, **opts) def create(basename, tmpdir=nil, max_try: nil, **opts)
if $SAFE > 0 and tmpdir.tainted? if $SAFE > 0 and tmpdir.tainted?
tmpdir = '/tmp' tmpdir = '/tmp'
@ -125,8 +113,16 @@ class Dir
tmpdir ||= tmpdir() tmpdir ||= tmpdir()
end end
n = nil n = nil
prefix, suffix = basename
prefix = (String.try_convert(prefix) or
raise ArgumentError, "unexpected prefix: #{prefix.inspect}")
suffix &&= (String.try_convert(suffix) or
raise ArgumentError, "unexpected suffix: #{suffix.inspect}")
begin begin
path = File.join(tmpdir, make_tmpname(basename, n)) t = Time.now.strftime("%Y%m%d")
path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"\
"#{n ? %[-#{n}] : ''}#{suffix||''}"
path = File.join(tmpdir, path)
yield(path, n, opts) yield(path, n, opts)
rescue Errno::EEXIST rescue Errno::EEXIST
n ||= 0 n ||= 0