diff options
Diffstat (limited to 'lib/yahns/tmpio.rb')
-rw-r--r-- | lib/yahns/tmpio.rb | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/yahns/tmpio.rb b/lib/yahns/tmpio.rb new file mode 100644 index 0000000..60751c0 --- /dev/null +++ b/lib/yahns/tmpio.rb @@ -0,0 +1,27 @@ +# -*- encoding: binary -*- +# Copyright (C) 2009-2013, Eric Wong <normalperson@yhbt.net> et. al. +# License: GPLv2 or later (https://www.gnu.org/licenses/gpl-2.0.txt) +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 (no delegation). +class Yahns::TmpIO < File # :nodoc: + + # 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 + super("#{Dir.tmpdir}/#{rand}", RDWR|CREAT|EXCL, 0600) + rescue Errno::EEXIST + retry + end + unlink(fp.path) + fp.binmode + fp.sync = true + fp + end + + alias discard close +end |