1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
knu 2002-12-19 19:01:59 +00:00
parent d9fbdddb59
commit 8451053741
2 changed files with 32 additions and 7 deletions

View file

@ -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

View file

@ -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,14 +99,32 @@ 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
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
@ -129,5 +147,5 @@ if __FILE__ == $0
f.close
f.open
p f.gets # => "foo\n"
f.close(true)
f.close!
end