From 8451053741771a8daf5503061266c2ce9036c878 Mon Sep 17 00:00:00 2001 From: knu Date: Thu, 19 Dec 2002 19:01:59 +0000 Subject: [PATCH] Add Tempfile#close!() as a shorthand for Tempfile#close(true). Add Tempfile#{unlink,delete}(). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3177 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ lib/tempfile.rb | 32 +++++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 08acfc7b02..6b991973df 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Fri Dec 20 03:57:32 2002 Akinori MUSHA + + * lib/tempfile.rb: Add Tempfile#close!() as a shorthand for + Tempfile#close(true). + + * lib/tempfile.rb: Add Tempfile#{unlink,delete}(). + Fri Dec 20 03:53:01 2002 Akinori MUSHA * lib/README, lib/cgi/final.rb, lib/cgi/session.rb: Delete diff --git a/lib/tempfile.rb b/lib/tempfile.rb index 201a0f14f7..0fc1e81fd3 100644 --- a/lib/tempfile.rb +++ b/lib/tempfile.rb @@ -12,7 +12,7 @@ # o When $SAFE > 0, you should specify a directory via the second argument # of Tempfile::new(), or it will end up finding an ENV value tainted and # pick /tmp. In case you don't have it, an exception will be raised. -# o Tempfile#close(true) gets the temporary file removed immediately. +# o Tempfile#close! gets the temporary file removed immediately. # o Otherwise, the removal is delayed until the object is finalized. # o With Tempfile#open, you can reopen the temporary file. # o The file mode for the temporary files is 0600. @@ -21,7 +21,7 @@ require 'delegate' class Tempfile < SimpleDelegator - Max_try = 10 + MAX_TRY = 10 @@cleanlist = [] def Tempfile.callback(data) @@ -64,7 +64,7 @@ class Tempfile < SimpleDelegator Dir.mkdir(lock) rescue failure += 1 - retry if failure < Max_try + retry if failure < MAX_TRY raise "cannot generate tempfile `%s'" % tmpname ensure Thread.critical = false @@ -99,15 +99,33 @@ class Tempfile < SimpleDelegator __setobj__(@tmpfile) end - def close(real=false) + def _close @tmpfile.close if @tmpfile @data[1] = @tmpfile = nil + end + protected :_close + + def close(real=false) if real - @clean_proc.call - ObjectSpace.undefine_finalizer(self) + close! + else + _close end end + def close! + _close + @clean_proc.call + ObjectSpace.undefine_finalizer(self) + end + + def unlink + # keep this order for thread safeness + File.unlink(@tmpname) if File.exist?(@tmpname) + @@cleanlist.delete(@tmpname) if @@cleanlist + end + alias delete unlink + def path @tmpname end @@ -129,5 +147,5 @@ if __FILE__ == $0 f.close f.open p f.gets # => "foo\n" - f.close(true) + f.close! end