Class: Yahns::TmpIO

Inherits:
File
  • Object
show all
Includes:
Kgio::PipeMethods
Defined in:
lib/yahns/tmpio.rb

Overview

some versions of Ruby had a broken Tempfile which didn’t work well with unlinked files. This one is much shorter, easier to understand, and slightly faster (no delegation).

Direct Known Subclasses

CapInput

Class Method Summary collapse

Class Method Details

.new(dir) ⇒ Object

creates and returns a new File object. The File is unlinked immediately, switched to binary mode, and userspace output buffering is disabled



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/yahns/tmpio.rb', line 16

def self.new(dir)
  retried = false
  begin
    fp = super("#{dir || Dir.tmpdir}/#{rand}", RDWR|CREAT|EXCL|APPEND, 0600)
  rescue Errno::EEXIST
    retry
  rescue Errno::EMFILE, Errno::ENFILE
    raise if retried
    retried = true
    Thread.current[:yahns_fdmap].desperate_expire(5)
    sleep(1)
    retry
  end
  unlink(fp.path)
  fp.binmode
  fp.sync = true
  fp
end