Module: Dir::Tmpdir

Included in:
Dir
Defined in:
lib/astrails/safe/multi.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

Super =
Dir.send(:method, :tmpdir)
Hostname =
Socket.gethostname || 'localhost'
Pid =
Process.pid
Ppid =
Process.ppid

Instance Method Summary collapse

Instance Method Details

#tmpdir(*args, &block) ⇒ Object

Raises:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/astrails/safe/multi.rb', line 90

def tmpdir *args, &block
  options = Hash === args.last ? args.pop : {}

  dirname = Super.call(*args)

  return dirname unless block

  turd = options['turd'] || options[:turd]

  basename = [
    Hostname,
    Ppid,
    Pid,
    Thread.current.object_id.abs,
    rand
  ].join('-')

  pathname = File.join dirname, basename

  made = false

  42.times do
    begin
      FileUtils.mkdir_p pathname
      break(made = true)
    rescue Object
      sleep rand
      :retry
    end
  end

  raise Error, "failed to make tmpdir in #{ dirname.inspect }" unless made

  begin
    return Dir.chdir(pathname, &block)
  ensure
    unless turd
      FileUtils.rm_rf(pathname) if made
    end
  end
end