зеркало из https://github.com/github/ruby.git
* lib/tempfile.rb (Tempfile#close!, Tempfile#path): added side
notes from Hongli Lai's fork. * lib/tempfile.rb (Tempfile#unlink, Tempfile.callback): do nothing any more once unlinked. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24663 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
d4e85c416d
Коммит
ba7a870a89
|
@ -1,3 +1,11 @@
|
||||||
|
Wed Aug 26 14:34:39 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* lib/tempfile.rb (Tempfile#close!, Tempfile#path): added side
|
||||||
|
notes from Hongli Lai's fork.
|
||||||
|
|
||||||
|
* lib/tempfile.rb (Tempfile#unlink, Tempfile.callback): do nothing
|
||||||
|
any more once unlinked.
|
||||||
|
|
||||||
Wed Aug 26 13:48:33 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Wed Aug 26 13:48:33 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* lib/tempfile.rb (Tempfile#unlink): reverted r23494, since the
|
* lib/tempfile.rb (Tempfile#unlink): reverted r23494, since the
|
||||||
|
|
|
@ -11,7 +11,7 @@ require 'thread'
|
||||||
# A class for managing temporary files. This library is written to be
|
# A class for managing temporary files. This library is written to be
|
||||||
# thread safe.
|
# thread safe.
|
||||||
class Tempfile < DelegateClass(File)
|
class Tempfile < DelegateClass(File)
|
||||||
MAX_TRY = 10
|
MAX_TRY = 10 # :nodoc:
|
||||||
@@cleanlist = []
|
@@cleanlist = []
|
||||||
@@lock = Mutex.new
|
@@lock = Mutex.new
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ class Tempfile < DelegateClass(File)
|
||||||
rescue
|
rescue
|
||||||
failure += 1
|
failure += 1
|
||||||
retry if failure < MAX_TRY
|
retry if failure < MAX_TRY
|
||||||
raise "cannot generate tempfile `%s'" % tmpname
|
raise "cannot generate tempfile `#{tmpname}'"
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,11 +122,10 @@ class Tempfile < DelegateClass(File)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Closes and unlinks the file.
|
# Closes and unlinks the file.
|
||||||
|
# Has the same effect as called <tt>close(true)</tt>.
|
||||||
def close!
|
def close!
|
||||||
_close
|
_close
|
||||||
@clean_proc.call
|
unlink
|
||||||
ObjectSpace.undefine_finalizer(self)
|
|
||||||
@data = @tmpname = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Unlinks the file. On UNIX-like systems, it is often a good idea
|
# Unlinks the file. On UNIX-like systems, it is often a good idea
|
||||||
|
@ -135,11 +134,14 @@ class Tempfile < DelegateClass(File)
|
||||||
# file.
|
# file.
|
||||||
def unlink
|
def unlink
|
||||||
# keep this order for thread safeness
|
# keep this order for thread safeness
|
||||||
|
return unless @tmpname
|
||||||
begin
|
begin
|
||||||
if File.exist?(@tmpname)
|
if File.exist?(@tmpname)
|
||||||
File.unlink(@tmpname)
|
File.unlink(@tmpname)
|
||||||
end
|
end
|
||||||
@@cleanlist.delete(@tmpname)
|
@@cleanlist.delete(@tmpname)
|
||||||
|
# remove tmpname and cleanlist from callback
|
||||||
|
@data[0] = @data[2] = nil
|
||||||
@data = @tmpname = nil
|
@data = @tmpname = nil
|
||||||
ObjectSpace.undefine_finalizer(self)
|
ObjectSpace.undefine_finalizer(self)
|
||||||
rescue Errno::EACCES
|
rescue Errno::EACCES
|
||||||
|
@ -149,6 +151,7 @@ class Tempfile < DelegateClass(File)
|
||||||
alias delete unlink
|
alias delete unlink
|
||||||
|
|
||||||
# Returns the full path name of the temporary file.
|
# Returns the full path name of the temporary file.
|
||||||
|
# This will be nil if #unlink has been called.
|
||||||
def path
|
def path
|
||||||
@tmpname
|
@tmpname
|
||||||
end
|
end
|
||||||
|
@ -177,8 +180,10 @@ class Tempfile < DelegateClass(File)
|
||||||
tmpfile.close if tmpfile
|
tmpfile.close if tmpfile
|
||||||
|
|
||||||
# keep this order for thread safeness
|
# keep this order for thread safeness
|
||||||
File.unlink(path) if File.exist?(path)
|
if path
|
||||||
cleanlist.delete(path) if cleanlist
|
File.unlink(path) if File.exist?(path)
|
||||||
|
cleanlist.delete(path) if cleanlist
|
||||||
|
end
|
||||||
|
|
||||||
print "done\n" if $DEBUG
|
print "done\n" if $DEBUG
|
||||||
end
|
end
|
||||||
|
|
Загрузка…
Ссылка в новой задаче