Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: tempfile.rb
#
[0] Fix | Delete
# tempfile - manipulates temporary files
[1] Fix | Delete
#
[2] Fix | Delete
# $Id: tempfile.rb 16127 2008-04-21 09:43:44Z knu $
[3] Fix | Delete
#
[4] Fix | Delete
[5] Fix | Delete
require 'delegate'
[6] Fix | Delete
require 'tmpdir'
[7] Fix | Delete
[8] Fix | Delete
# A class for managing temporary files. This library is written to be
[9] Fix | Delete
# thread safe.
[10] Fix | Delete
class Tempfile < DelegateClass(File)
[11] Fix | Delete
MAX_TRY = 10
[12] Fix | Delete
@@cleanlist = []
[13] Fix | Delete
[14] Fix | Delete
# Creates a temporary file of mode 0600 in the temporary directory,
[15] Fix | Delete
# opens it with mode "w+", and returns a Tempfile object which
[16] Fix | Delete
# represents the created temporary file. A Tempfile object can be
[17] Fix | Delete
# treated just like a normal File object.
[18] Fix | Delete
#
[19] Fix | Delete
# The basename parameter is used to determine the name of a
[20] Fix | Delete
# temporary file. If an Array is given, the first element is used
[21] Fix | Delete
# as prefix string and the second as suffix string, respectively.
[22] Fix | Delete
# Otherwise it is treated as prefix string.
[23] Fix | Delete
#
[24] Fix | Delete
# If tmpdir is omitted, the temporary directory is determined by
[25] Fix | Delete
# Dir::tmpdir provided by 'tmpdir.rb'.
[26] Fix | Delete
# When $SAFE > 0 and the given tmpdir is tainted, it uses
[27] Fix | Delete
# /tmp. (Note that ENV values are tainted by default)
[28] Fix | Delete
def initialize(basename, tmpdir=Dir::tmpdir)
[29] Fix | Delete
if $SAFE > 0 and tmpdir.tainted?
[30] Fix | Delete
tmpdir = '/tmp'
[31] Fix | Delete
end
[32] Fix | Delete
[33] Fix | Delete
lock = nil
[34] Fix | Delete
n = failure = 0
[35] Fix | Delete
[36] Fix | Delete
begin
[37] Fix | Delete
Thread.critical = true
[38] Fix | Delete
[39] Fix | Delete
begin
[40] Fix | Delete
tmpname = File.join(tmpdir, make_tmpname(basename, n))
[41] Fix | Delete
lock = tmpname + '.lock'
[42] Fix | Delete
n += 1
[43] Fix | Delete
end while @@cleanlist.include?(tmpname) or
[44] Fix | Delete
File.exist?(lock) or File.exist?(tmpname)
[45] Fix | Delete
[46] Fix | Delete
Dir.mkdir(lock)
[47] Fix | Delete
rescue
[48] Fix | Delete
failure += 1
[49] Fix | Delete
retry if failure < MAX_TRY
[50] Fix | Delete
raise "cannot generate tempfile `%s'" % tmpname
[51] Fix | Delete
ensure
[52] Fix | Delete
Thread.critical = false
[53] Fix | Delete
end
[54] Fix | Delete
[55] Fix | Delete
@data = [tmpname]
[56] Fix | Delete
@clean_proc = Tempfile.callback(@data)
[57] Fix | Delete
ObjectSpace.define_finalizer(self, @clean_proc)
[58] Fix | Delete
[59] Fix | Delete
@tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL, 0600)
[60] Fix | Delete
@tmpname = tmpname
[61] Fix | Delete
@@cleanlist << @tmpname
[62] Fix | Delete
@data[1] = @tmpfile
[63] Fix | Delete
@data[2] = @@cleanlist
[64] Fix | Delete
[65] Fix | Delete
super(@tmpfile)
[66] Fix | Delete
[67] Fix | Delete
# Now we have all the File/IO methods defined, you must not
[68] Fix | Delete
# carelessly put bare puts(), etc. after this.
[69] Fix | Delete
[70] Fix | Delete
Dir.rmdir(lock)
[71] Fix | Delete
end
[72] Fix | Delete
[73] Fix | Delete
def make_tmpname(basename, n)
[74] Fix | Delete
case basename
[75] Fix | Delete
when Array
[76] Fix | Delete
prefix, suffix = *basename
[77] Fix | Delete
else
[78] Fix | Delete
prefix, suffix = basename, ''
[79] Fix | Delete
end
[80] Fix | Delete
[81] Fix | Delete
t = Time.now.strftime("%Y%m%d")
[82] Fix | Delete
path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}-#{n}#{suffix}"
[83] Fix | Delete
end
[84] Fix | Delete
private :make_tmpname
[85] Fix | Delete
[86] Fix | Delete
# Opens or reopens the file with mode "r+".
[87] Fix | Delete
def open
[88] Fix | Delete
@tmpfile.close if @tmpfile
[89] Fix | Delete
@tmpfile = File.open(@tmpname, 'r+')
[90] Fix | Delete
@data[1] = @tmpfile
[91] Fix | Delete
__setobj__(@tmpfile)
[92] Fix | Delete
end
[93] Fix | Delete
[94] Fix | Delete
def _close # :nodoc:
[95] Fix | Delete
@tmpfile.close if @tmpfile
[96] Fix | Delete
@tmpfile = nil
[97] Fix | Delete
@data[1] = nil if @data
[98] Fix | Delete
end
[99] Fix | Delete
protected :_close
[100] Fix | Delete
[101] Fix | Delete
# Closes the file. If the optional flag is true, unlinks the file
[102] Fix | Delete
# after closing.
[103] Fix | Delete
#
[104] Fix | Delete
# If you don't explicitly unlink the temporary file, the removal
[105] Fix | Delete
# will be delayed until the object is finalized.
[106] Fix | Delete
def close(unlink_now=false)
[107] Fix | Delete
if unlink_now
[108] Fix | Delete
close!
[109] Fix | Delete
else
[110] Fix | Delete
_close
[111] Fix | Delete
end
[112] Fix | Delete
end
[113] Fix | Delete
[114] Fix | Delete
# Closes and unlinks the file.
[115] Fix | Delete
def close!
[116] Fix | Delete
_close
[117] Fix | Delete
@clean_proc.call
[118] Fix | Delete
ObjectSpace.undefine_finalizer(self)
[119] Fix | Delete
@data = @tmpname = nil
[120] Fix | Delete
end
[121] Fix | Delete
[122] Fix | Delete
# Unlinks the file. On UNIX-like systems, it is often a good idea
[123] Fix | Delete
# to unlink a temporary file immediately after creating and opening
[124] Fix | Delete
# it, because it leaves other programs zero chance to access the
[125] Fix | Delete
# file.
[126] Fix | Delete
def unlink
[127] Fix | Delete
# keep this order for thread safeness
[128] Fix | Delete
begin
[129] Fix | Delete
File.unlink(@tmpname) if File.exist?(@tmpname)
[130] Fix | Delete
@@cleanlist.delete(@tmpname)
[131] Fix | Delete
@data = @tmpname = nil
[132] Fix | Delete
ObjectSpace.undefine_finalizer(self)
[133] Fix | Delete
rescue Errno::EACCES
[134] Fix | Delete
# may not be able to unlink on Windows; just ignore
[135] Fix | Delete
end
[136] Fix | Delete
end
[137] Fix | Delete
alias delete unlink
[138] Fix | Delete
[139] Fix | Delete
# Returns the full path name of the temporary file.
[140] Fix | Delete
def path
[141] Fix | Delete
@tmpname
[142] Fix | Delete
end
[143] Fix | Delete
[144] Fix | Delete
# Returns the size of the temporary file. As a side effect, the IO
[145] Fix | Delete
# buffer is flushed before determining the size.
[146] Fix | Delete
def size
[147] Fix | Delete
if @tmpfile
[148] Fix | Delete
@tmpfile.flush
[149] Fix | Delete
@tmpfile.stat.size
[150] Fix | Delete
else
[151] Fix | Delete
0
[152] Fix | Delete
end
[153] Fix | Delete
end
[154] Fix | Delete
alias length size
[155] Fix | Delete
[156] Fix | Delete
class << self
[157] Fix | Delete
def callback(data) # :nodoc:
[158] Fix | Delete
pid = $$
[159] Fix | Delete
lambda{
[160] Fix | Delete
if pid == $$
[161] Fix | Delete
path, tmpfile, cleanlist = *data
[162] Fix | Delete
[163] Fix | Delete
print "removing ", path, "..." if $DEBUG
[164] Fix | Delete
[165] Fix | Delete
tmpfile.close if tmpfile
[166] Fix | Delete
[167] Fix | Delete
# keep this order for thread safeness
[168] Fix | Delete
File.unlink(path) if File.exist?(path)
[169] Fix | Delete
cleanlist.delete(path) if cleanlist
[170] Fix | Delete
[171] Fix | Delete
print "done\n" if $DEBUG
[172] Fix | Delete
end
[173] Fix | Delete
}
[174] Fix | Delete
end
[175] Fix | Delete
[176] Fix | Delete
# If no block is given, this is a synonym for new().
[177] Fix | Delete
#
[178] Fix | Delete
# If a block is given, it will be passed tempfile as an argument,
[179] Fix | Delete
# and the tempfile will automatically be closed when the block
[180] Fix | Delete
# terminates. In this case, open() returns nil.
[181] Fix | Delete
def open(*args)
[182] Fix | Delete
tempfile = new(*args)
[183] Fix | Delete
[184] Fix | Delete
if block_given?
[185] Fix | Delete
begin
[186] Fix | Delete
yield(tempfile)
[187] Fix | Delete
ensure
[188] Fix | Delete
tempfile.close
[189] Fix | Delete
end
[190] Fix | Delete
[191] Fix | Delete
nil
[192] Fix | Delete
else
[193] Fix | Delete
tempfile
[194] Fix | Delete
end
[195] Fix | Delete
end
[196] Fix | Delete
end
[197] Fix | Delete
end
[198] Fix | Delete
[199] Fix | Delete
if __FILE__ == $0
[200] Fix | Delete
# $DEBUG = true
[201] Fix | Delete
f = Tempfile.new("foo")
[202] Fix | Delete
f.print("foo\n")
[203] Fix | Delete
f.close
[204] Fix | Delete
f.open
[205] Fix | Delete
p f.gets # => "foo\n"
[206] Fix | Delete
f.close!
[207] Fix | Delete
end
[208] Fix | Delete
[209] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function