Module: FakeFS::Dir::Tmpname

Defined in:
lib/fakefs/dir.rb

Class Method Summary collapse

Class Method Details

.create(basename, *rest) ⇒ Object


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/fakefs/dir.rb', line 196

def create(basename, *rest)
  if (opts = Hash.try_convert(rest[-1]))
    opts = opts.dup if rest.pop.equal?(opts)
    max_try = opts.delete(:max_try)
    opts = [opts]
  else
    opts = []
  end
  tmpdir, = *rest
  if $SAFE > 0 && tmpdir.tainted?
    tmpdir = '/tmp'
  else
    tmpdir ||= self.tmpdir
  end
  n = nil
  begin
    path = File.join(tmpdir, make_tmpname(basename, n))
    yield(path, n, *opts)
  rescue Errno::EEXIST
    n ||= 0
    n += 1
    retry if !max_try || n < max_try
    raise "cannot generate temporary name using `#{basename}' " \
      "under `#{tmpdir}'"
  end
  path
end

.make_tmpname(prefix_suffix, n) ⇒ Object


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/fakefs/dir.rb', line 178

def make_tmpname(prefix_suffix, n)
  case prefix_suffix
  when String
    prefix = prefix_suffix
    suffix = ''
  when Array
    prefix = prefix_suffix[0]
    suffix = prefix_suffix[1]
  else
    fail ArgumentError,
         "unexpected prefix_suffix: #{prefix_suffix.inspect}"
  end
  t = Time.now.strftime('%Y%m%d')
  path = "#{prefix}#{t}-#{$PID}-#{rand(0x100000000).to_s(36)}"
  path << "-#{n}" if n
  path << suffix
end

.tmpdirObject


174
175
176
# File 'lib/fakefs/dir.rb', line 174

def tmpdir
  Dir.tmpdir
end