yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
blob ca86b4eed761b2833dd2b78d14e15422a3f1c7ad 993 bytes (raw)
name: lib/yahns/tmpio.rb 	 # note: path name is non-authoritative(*)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
# -*- 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:
  include Kgio::PipeMethods

  # 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(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
end

debug log:

solving ca86b4e ...
found ca86b4e in https://yhbt.net/yahns.git/

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhbt.net/yahns.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).