From 3d147e9bcd8f99c94900a00181692c2a09c3c3c9 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 5 Oct 2010 07:54:13 +0000 Subject: Unicorn::Util.tmpio => Unicorn::TmpIO.new This is slightly shorter and hopefully easier to find. --- lib/unicorn/tmpio.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 lib/unicorn/tmpio.rb (limited to 'lib/unicorn/tmpio.rb') diff --git a/lib/unicorn/tmpio.rb b/lib/unicorn/tmpio.rb new file mode 100644 index 0000000..cce89ac --- /dev/null +++ b/lib/unicorn/tmpio.rb @@ -0,0 +1,29 @@ +# -*- encoding: binary -*- +# :stopdoc: +require 'tmpdir' + +# 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. +class Unicorn::TmpIO < File + + # creates and returns a new File object. The File is unlinked + # immediately, switched to binary mode, and userspace output + # buffering is disabled + def self.new + fp = begin + open("#{Dir::tmpdir}/#{rand}", RDWR|CREAT|EXCL, 0600) + rescue Errno::EEXIST + retry + end + unlink(fp.path) + fp.binmode + fp.sync = true + fp + end + + # for easier env["rack.input"] compatibility with Rack <= 1.1 + def size + stat.size + end +end -- cgit v1.2.3-24-ge0c7