* lib/tempfile.rb: Embed Rdoc style comments.
* lib/tempfile.rb: Add length as an alias for size. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
8451053741
commit
9c95229c73
@ -1,3 +1,9 @@
|
|||||||
|
Fri Dec 20 04:58:22 2002 Akinori MUSHA <knu@iDaemons.org>
|
||||||
|
|
||||||
|
* lib/tempfile.rb: Embed Rdoc style comments.
|
||||||
|
|
||||||
|
* lib/tempfile.rb: Add length as an alias for size.
|
||||||
|
|
||||||
Fri Dec 20 03:57:32 2002 Akinori MUSHA <knu@iDaemons.org>
|
Fri Dec 20 03:57:32 2002 Akinori MUSHA <knu@iDaemons.org>
|
||||||
|
|
||||||
* lib/tempfile.rb: Add Tempfile#close!() as a shorthand for
|
* lib/tempfile.rb: Add Tempfile#close!() as a shorthand for
|
||||||
|
@ -1,48 +1,26 @@
|
|||||||
#
|
#
|
||||||
|
# tempfile - manipulates temporary files
|
||||||
|
#
|
||||||
# $Id$
|
# $Id$
|
||||||
#
|
#
|
||||||
# This is a class for managing temporary files.
|
|
||||||
#
|
|
||||||
# o Tempfile::new("basename") creates a temporary file whose name is
|
|
||||||
# "basename.pid.n" and opens with mode "w+".
|
|
||||||
# o A Tempfile object can be treated as an IO object.
|
|
||||||
# o The temporary directory is determined by ENV['TMPDIR'],
|
|
||||||
# ENV['TMP'], and ENV['TEMP'] in the order named, and if none of
|
|
||||||
# them is available, it is set to /tmp.
|
|
||||||
# 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! 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.
|
|
||||||
# o This library is (considered to be) thread safe.
|
|
||||||
|
|
||||||
require 'delegate'
|
require 'delegate'
|
||||||
|
|
||||||
|
# A class for managing temporary files. This library is written to be
|
||||||
|
# thread safe.
|
||||||
class Tempfile < SimpleDelegator
|
class Tempfile < SimpleDelegator
|
||||||
MAX_TRY = 10
|
MAX_TRY = 10
|
||||||
@@cleanlist = []
|
@@cleanlist = []
|
||||||
|
|
||||||
def Tempfile.callback(data)
|
# Creates a temporary file of mode 0600 in the temporary directory
|
||||||
pid = $$
|
# whose name is basename.pid.n and opens with mode "w+". A Tempfile
|
||||||
lambda{
|
# object works just like a File object.
|
||||||
if pid == $$
|
#
|
||||||
path, tmpfile, cleanlist = *data
|
# If tmpdir is omitted, the temporary directory is determined by
|
||||||
|
# ENV['TMPDIR'], ENV['TMP'] and and ENV['TEMP'] in the order named.
|
||||||
print "removing ", path, "..." if $DEBUG
|
# If none of them is available, or when $SAFE > 0 and the given
|
||||||
|
# tmpdir is tainted, it uses /tmp. (Note that ENV values are
|
||||||
tmpfile.close if tmpfile
|
# tainted by default)
|
||||||
|
|
||||||
# keep this order for thread safeness
|
|
||||||
File.unlink(path) if File.exist?(path)
|
|
||||||
cleanlist.delete(path) if cleanlist
|
|
||||||
|
|
||||||
print "done\n" if $DEBUG
|
|
||||||
end
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def initialize(basename, tmpdir=ENV['TMPDIR']||ENV['TMP']||ENV['TEMP']||'/tmp')
|
def initialize(basename, tmpdir=ENV['TMPDIR']||ENV['TMP']||ENV['TEMP']||'/tmp')
|
||||||
if $SAFE > 0 and tmpdir.tainted?
|
if $SAFE > 0 and tmpdir.tainted?
|
||||||
tmpdir = '/tmp'
|
tmpdir = '/tmp'
|
||||||
@ -88,10 +66,7 @@ class Tempfile < SimpleDelegator
|
|||||||
Dir.rmdir(lock)
|
Dir.rmdir(lock)
|
||||||
end
|
end
|
||||||
|
|
||||||
def Tempfile.open(*args)
|
# Opens or reopens the file with mode "r+".
|
||||||
Tempfile.new(*args)
|
|
||||||
end
|
|
||||||
|
|
||||||
def open
|
def open
|
||||||
@tmpfile.close if @tmpfile
|
@tmpfile.close if @tmpfile
|
||||||
@tmpfile = File.open(@tmpname, 'r+')
|
@tmpfile = File.open(@tmpname, 'r+')
|
||||||
@ -99,13 +74,18 @@ class Tempfile < SimpleDelegator
|
|||||||
__setobj__(@tmpfile)
|
__setobj__(@tmpfile)
|
||||||
end
|
end
|
||||||
|
|
||||||
def _close
|
def _close # :nodoc:
|
||||||
@tmpfile.close if @tmpfile
|
@tmpfile.close if @tmpfile
|
||||||
@data[1] = @tmpfile = nil
|
@data[1] = @tmpfile = nil
|
||||||
end
|
end
|
||||||
protected :_close
|
protected :_close
|
||||||
|
|
||||||
def close(real=false)
|
# Closes the file. If the optional flag is true, unlinks the file
|
||||||
|
# after closing.
|
||||||
|
#
|
||||||
|
# If you don't explicitly unlink the temporary file, the removal
|
||||||
|
# will be delayed until the object is finalized.
|
||||||
|
def close(unlink=false)
|
||||||
if real
|
if real
|
||||||
close!
|
close!
|
||||||
else
|
else
|
||||||
@ -113,12 +93,17 @@ class Tempfile < SimpleDelegator
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Closes and unlinks the file.
|
||||||
def close!
|
def close!
|
||||||
_close
|
_close
|
||||||
@clean_proc.call
|
@clean_proc.call
|
||||||
ObjectSpace.undefine_finalizer(self)
|
ObjectSpace.undefine_finalizer(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Unlinks the file. On UNIX-like systems, it is often a good idea
|
||||||
|
# to unlink a temporary file immediately after creating and opening
|
||||||
|
# it, because it leaves other programs zero chance to access the
|
||||||
|
# file.
|
||||||
def unlink
|
def unlink
|
||||||
# keep this order for thread safeness
|
# keep this order for thread safeness
|
||||||
File.unlink(@tmpname) if File.exist?(@tmpname)
|
File.unlink(@tmpname) if File.exist?(@tmpname)
|
||||||
@ -126,10 +111,13 @@ class Tempfile < SimpleDelegator
|
|||||||
end
|
end
|
||||||
alias delete unlink
|
alias delete unlink
|
||||||
|
|
||||||
|
# Returns the full path name of the temporary file.
|
||||||
def path
|
def path
|
||||||
@tmpname
|
@tmpname
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns the size of the temporary file. As a side effect, the IO
|
||||||
|
# buffer is flushed before determining the size.
|
||||||
def size
|
def size
|
||||||
if @tmpfile
|
if @tmpfile
|
||||||
@tmpfile.flush
|
@tmpfile.flush
|
||||||
@ -138,6 +126,33 @@ class Tempfile < SimpleDelegator
|
|||||||
0
|
0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
alias length size
|
||||||
|
|
||||||
|
class << self
|
||||||
|
def callback(data) # :nodoc:
|
||||||
|
pid = $$
|
||||||
|
lambda{
|
||||||
|
if pid == $$
|
||||||
|
path, tmpfile, cleanlist = *data
|
||||||
|
|
||||||
|
print "removing ", path, "..." if $DEBUG
|
||||||
|
|
||||||
|
tmpfile.close if tmpfile
|
||||||
|
|
||||||
|
# keep this order for thread safeness
|
||||||
|
File.unlink(path) if File.exist?(path)
|
||||||
|
cleanlist.delete(path) if cleanlist
|
||||||
|
|
||||||
|
print "done\n" if $DEBUG
|
||||||
|
end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
# Equivalent to new().
|
||||||
|
def open(*args)
|
||||||
|
new(*args)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if __FILE__ == $0
|
if __FILE__ == $0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user