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