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:
		
							parent
							
								
									0cc57ea018
								
							
						
					
					
						commit
						25d56ea7b7
					
				
					 1 changed files with 9 additions and 13 deletions
				
			
		| 
						 | 
				
			
			@ -106,18 +106,6 @@ class Dir
 | 
			
		|||
      Dir.tmpdir
 | 
			
		||||
    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)
 | 
			
		||||
      if $SAFE > 0 and tmpdir.tainted?
 | 
			
		||||
        tmpdir = '/tmp'
 | 
			
		||||
| 
						 | 
				
			
			@ -125,8 +113,16 @@ class Dir
 | 
			
		|||
        tmpdir ||= tmpdir()
 | 
			
		||||
      end
 | 
			
		||||
      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
 | 
			
		||||
        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)
 | 
			
		||||
      rescue Errno::EEXIST
 | 
			
		||||
        n ||= 0
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue