Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/stickyflag/patches/tmpnam.rb

Class Method Summary collapse

Class Method Details

.tmpnam(ext = '') ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/stickyflag/patches/tmpnam.rb', line 7

def self.tmpnam(ext = '')
  if ext != ''
    if ext[0] != '.'
      ext = ".#{ext}"
    end
  end

  pid = Process.pid
  time = Time.now
  sec = time.to_i
  usec = time.usec

  counter = 0
  path = File.join(Dir.tmpdir, "#{pid}_#{sec}_#{usec}_#{rand(1000)}#{ext}")

  # This is a corner case that should effectively never be possible, so
  # don't try to cover it.
  #:nocov:
  while File.exist? path
    sec = Time.now.to_i
    usec = Time.now.usec
    path = File.join(Dir.tmpdir, "#{pid}_#{sec}_#{usec}_#{rand(1000)}#{ext}")
  
    counter += 1
    raise 'ERROR: Cannot get unique temporary name' if counter >= 100
  end
  #:nocov:

  path
end