doc: s/bogomips.org/yhbt.net/g
[unicorn.git] / lib / unicorn / tmpio.rb
blob0bbf6ec5cb38ea0acd39136fd6f3ec4b3429035d
1 # -*- encoding: binary -*-
2 # :stopdoc:
3 require 'tmpdir'
5 # some versions of Ruby had a broken Tempfile which didn't work
6 # well with unlinked files.  This one is much shorter, easier
7 # to understand, and slightly faster.
8 class Unicorn::TmpIO < File
10   # creates and returns a new File object.  The File is unlinked
11   # immediately, switched to binary mode, and userspace output
12   # buffering is disabled
13   def self.new
14     path = nil
16     # workaround File#path being tainted:
17     # https://bugs.ruby-lang.org/issues/14485
18     fp = begin
19       path = "#{Dir::tmpdir}/#{rand}"
20       super(path, RDWR|CREAT|EXCL, 0600)
21     rescue Errno::EEXIST
22       retry
23     end
25     unlink(path)
26     fp.binmode
27     fp.sync = true
28     fp
29   end
31   # pretend we're Tempfile for Rack::TempfileReaper
32   alias close! close
33 end