diff options
author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2002-12-19 19:01:59 +0000 |
---|---|---|
committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2002-12-19 19:01:59 +0000 |
commit | 8451053741771a8daf5503061266c2ce9036c878 (patch) | |
tree | a4e74798a5072199c1d268b32ab4f4d5c23c748b | |
parent | d9fbdddb59ec1a240f4eecc3d9995320ae3298af (diff) | |
download | ruby-8451053741771a8daf5503061266c2ce9036c878.tar.gz |
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
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | lib/tempfile.rb | 32 |
2 files changed, 32 insertions, 7 deletions
@@ -1,3 +1,10 @@ +Fri Dec 20 03:57:32 2002 Akinori MUSHA <knu@iDaemons.org> + + * 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 <knu@iDaemons.org> * 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 |