Class: Fairy::FastTempfile
- Inherits:
-
Object
- Object
- Fairy::FastTempfile
- Extended by:
- Forwardable
- Defined in:
- lib/fairy/share/fast-tempfile.rb
Defined Under Namespace
Classes: Entry
Class Method Summary collapse
- .gen_tmpname(prefix, tmpdir) ⇒ Object
- .open(prefix, tmpdir = @DEFAULT_TMPDIR) ⇒ Object
- .reset ⇒ Object
- .terminate_proc(entry) ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
- #close! ⇒ Object
-
#initialize(prefix, tmpdir) ⇒ FastTempfile
constructor
A new instance of FastTempfile.
- #open ⇒ Object
Constructor Details
#initialize(prefix, tmpdir) ⇒ FastTempfile
Returns a new instance of FastTempfile.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/fairy/share/fast-tempfile.rb', line 40 def initialize(prefix, tmpdir) @entry = Entry.new ObjectSpace.define_finalizer(self, FastTempfile.terminate_proc(@entry)) @entry.path = FastTempfile.gen_tmpname(prefix, tmpdir) begin @entry.io = File.open(path, File::RDWR|File::CREAT|File::EXCL) rescue Errno::ENOENT unless File.directory?(tmpdir) ERR::Fail ERR::NoTmpDir, tmpdir end raise end end |
Class Method Details
.gen_tmpname(prefix, tmpdir) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/fairy/share/fast-tempfile.rb', line 30 def self.gen_tmpname(prefix, tmpdir) @Mutex.synchronize do # forkしたときのため reset if @PID != $$ name = tmpdir+"/"+prefix+@HEAD+@Seq @Seq = @Seq.succ name end end |
.open(prefix, tmpdir = @DEFAULT_TMPDIR) ⇒ Object
26 27 28 |
# File 'lib/fairy/share/fast-tempfile.rb', line 26 def self.open(prefix, tmpdir = @DEFAULT_TMPDIR) new(prefix, tmpdir) end |
.reset ⇒ Object
17 18 19 20 21 22 |
# File 'lib/fairy/share/fast-tempfile.rb', line 17 def self.reset @DEFAULT_TMPDIR = CONF.TMP_DIR @PID = $$ @HEAD = Time.now.strftime("%Y%m%d")+format("-%05d-", @PID) @Seq = "00000" end |
.terminate_proc(entry) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/fairy/share/fast-tempfile.rb', line 77 def self.terminate_proc(entry) pid = @PID Proc.new { if pid == $$ entry.io.close if entry.io # keep this order for thread safeness if entry.path File.unlink(entry.path) if File.exist?(entry.path) end end } end |
Instance Method Details
#close ⇒ Object
64 65 66 67 |
# File 'lib/fairy/share/fast-tempfile.rb', line 64 def close @entry.io.close @entry.io = nil end |
#close! ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/fairy/share/fast-tempfile.rb', line 69 def close! @entry.io.close if @entry.io if File.exist?(@entry.path) File.unlink @entry.path end ObjectSpace.undefine_finalizer(self) end |
#open ⇒ Object
59 60 61 62 |
# File 'lib/fairy/share/fast-tempfile.rb', line 59 def open @entry.io.close if @entry.io @entry.io = File.open(path) end |