Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby22/lib64/ruby/2.2.0
File: tempfile.rb
#
[0] Fix | Delete
# tempfile - manipulates temporary files
[1] Fix | Delete
#
[2] Fix | Delete
# $Id: tempfile.rb 47656 2014-09-21 01:40:21Z nobu $
[3] Fix | Delete
#
[4] Fix | Delete
[5] Fix | Delete
require 'delegate'
[6] Fix | Delete
require 'tmpdir'
[7] Fix | Delete
[8] Fix | Delete
# A utility class for managing temporary files. When you create a Tempfile
[9] Fix | Delete
# object, it will create a temporary file with a unique filename. A Tempfile
[10] Fix | Delete
# objects behaves just like a File object, and you can perform all the usual
[11] Fix | Delete
# file operations on it: reading data, writing data, changing its permissions,
[12] Fix | Delete
# etc. So although this class does not explicitly document all instance methods
[13] Fix | Delete
# supported by File, you can in fact call any File instance method on a
[14] Fix | Delete
# Tempfile object.
[15] Fix | Delete
#
[16] Fix | Delete
# == Synopsis
[17] Fix | Delete
#
[18] Fix | Delete
# require 'tempfile'
[19] Fix | Delete
#
[20] Fix | Delete
# file = Tempfile.new('foo')
[21] Fix | Delete
# file.path # => A unique filename in the OS's temp directory,
[22] Fix | Delete
# # e.g.: "/tmp/foo.24722.0"
[23] Fix | Delete
# # This filename contains 'foo' in its basename.
[24] Fix | Delete
# file.write("hello world")
[25] Fix | Delete
# file.rewind
[26] Fix | Delete
# file.read # => "hello world"
[27] Fix | Delete
# file.close
[28] Fix | Delete
# file.unlink # deletes the temp file
[29] Fix | Delete
#
[30] Fix | Delete
# == Good practices
[31] Fix | Delete
#
[32] Fix | Delete
# === Explicit close
[33] Fix | Delete
#
[34] Fix | Delete
# When a Tempfile object is garbage collected, or when the Ruby interpreter
[35] Fix | Delete
# exits, its associated temporary file is automatically deleted. This means
[36] Fix | Delete
# that's it's unnecessary to explicitly delete a Tempfile after use, though
[37] Fix | Delete
# it's good practice to do so: not explicitly deleting unused Tempfiles can
[38] Fix | Delete
# potentially leave behind large amounts of tempfiles on the filesystem
[39] Fix | Delete
# until they're garbage collected. The existence of these temp files can make
[40] Fix | Delete
# it harder to determine a new Tempfile filename.
[41] Fix | Delete
#
[42] Fix | Delete
# Therefore, one should always call #unlink or close in an ensure block, like
[43] Fix | Delete
# this:
[44] Fix | Delete
#
[45] Fix | Delete
# file = Tempfile.new('foo')
[46] Fix | Delete
# begin
[47] Fix | Delete
# ...do something with file...
[48] Fix | Delete
# ensure
[49] Fix | Delete
# file.close
[50] Fix | Delete
# file.unlink # deletes the temp file
[51] Fix | Delete
# end
[52] Fix | Delete
#
[53] Fix | Delete
# === Unlink after creation
[54] Fix | Delete
#
[55] Fix | Delete
# On POSIX systems, it's possible to unlink a file right after creating it,
[56] Fix | Delete
# and before closing it. This removes the filesystem entry without closing
[57] Fix | Delete
# the file handle, so it ensures that only the processes that already had
[58] Fix | Delete
# the file handle open can access the file's contents. It's strongly
[59] Fix | Delete
# recommended that you do this if you do not want any other processes to
[60] Fix | Delete
# be able to read from or write to the Tempfile, and you do not need to
[61] Fix | Delete
# know the Tempfile's filename either.
[62] Fix | Delete
#
[63] Fix | Delete
# For example, a practical use case for unlink-after-creation would be this:
[64] Fix | Delete
# you need a large byte buffer that's too large to comfortably fit in RAM,
[65] Fix | Delete
# e.g. when you're writing a web server and you want to buffer the client's
[66] Fix | Delete
# file upload data.
[67] Fix | Delete
#
[68] Fix | Delete
# Please refer to #unlink for more information and a code example.
[69] Fix | Delete
#
[70] Fix | Delete
# == Minor notes
[71] Fix | Delete
#
[72] Fix | Delete
# Tempfile's filename picking method is both thread-safe and inter-process-safe:
[73] Fix | Delete
# it guarantees that no other threads or processes will pick the same filename.
[74] Fix | Delete
#
[75] Fix | Delete
# Tempfile itself however may not be entirely thread-safe. If you access the
[76] Fix | Delete
# same Tempfile object from multiple threads then you should protect it with a
[77] Fix | Delete
# mutex.
[78] Fix | Delete
class Tempfile < DelegateClass(File)
[79] Fix | Delete
# call-seq:
[80] Fix | Delete
# new(basename, [tmpdir = Dir.tmpdir], [options])
[81] Fix | Delete
#
[82] Fix | Delete
# Creates a temporary file with permissions 0600 (= only readable and
[83] Fix | Delete
# writable by the owner) and opens it with mode "w+".
[84] Fix | Delete
#
[85] Fix | Delete
# The +basename+ parameter is used to determine the name of the
[86] Fix | Delete
# temporary file. You can either pass a String or an Array with
[87] Fix | Delete
# 2 String elements. In the former form, the temporary file's base
[88] Fix | Delete
# name will begin with the given string. In the latter form,
[89] Fix | Delete
# the temporary file's base name will begin with the array's first
[90] Fix | Delete
# element, and end with the second element. For example:
[91] Fix | Delete
#
[92] Fix | Delete
# file = Tempfile.new('hello')
[93] Fix | Delete
# file.path # => something like: "/tmp/hello2843-8392-92849382--0"
[94] Fix | Delete
#
[95] Fix | Delete
# # Use the Array form to enforce an extension in the filename:
[96] Fix | Delete
# file = Tempfile.new(['hello', '.jpg'])
[97] Fix | Delete
# file.path # => something like: "/tmp/hello2843-8392-92849382--0.jpg"
[98] Fix | Delete
#
[99] Fix | Delete
# The temporary file will be placed in the directory as specified
[100] Fix | Delete
# by the +tmpdir+ parameter. By default, this is +Dir.tmpdir+.
[101] Fix | Delete
# When $SAFE > 0 and the given +tmpdir+ is tainted, it uses
[102] Fix | Delete
# '/tmp' as the temporary directory. Please note that ENV values
[103] Fix | Delete
# are tainted by default, and +Dir.tmpdir+'s return value might
[104] Fix | Delete
# come from environment variables (e.g. <tt>$TMPDIR</tt>).
[105] Fix | Delete
#
[106] Fix | Delete
# file = Tempfile.new('hello', '/home/aisaka')
[107] Fix | Delete
# file.path # => something like: "/home/aisaka/hello2843-8392-92849382--0"
[108] Fix | Delete
#
[109] Fix | Delete
# You can also pass an options hash. Under the hood, Tempfile creates
[110] Fix | Delete
# the temporary file using +File.open+. These options will be passed to
[111] Fix | Delete
# +File.open+. This is mostly useful for specifying encoding
[112] Fix | Delete
# options, e.g.:
[113] Fix | Delete
#
[114] Fix | Delete
# Tempfile.new('hello', '/home/aisaka', :encoding => 'ascii-8bit')
[115] Fix | Delete
#
[116] Fix | Delete
# # You can also omit the 'tmpdir' parameter:
[117] Fix | Delete
# Tempfile.new('hello', :encoding => 'ascii-8bit')
[118] Fix | Delete
#
[119] Fix | Delete
# === Exceptions
[120] Fix | Delete
#
[121] Fix | Delete
# If Tempfile.new cannot find a unique filename within a limited
[122] Fix | Delete
# number of tries, then it will raise an exception.
[123] Fix | Delete
def initialize(basename, tmpdir=nil, mode: 0, **options)
[124] Fix | Delete
if block_given?
[125] Fix | Delete
warn "Tempfile.new doesn't call the given block."
[126] Fix | Delete
end
[127] Fix | Delete
@data = []
[128] Fix | Delete
@clean_proc = Remover.new(@data)
[129] Fix | Delete
ObjectSpace.define_finalizer(self, @clean_proc)
[130] Fix | Delete
[131] Fix | Delete
::Dir::Tmpname.create(basename, tmpdir, options) do |tmpname, n, opts|
[132] Fix | Delete
mode |= File::RDWR|File::CREAT|File::EXCL
[133] Fix | Delete
opts[:perm] = 0600
[134] Fix | Delete
@data[1] = @tmpfile = File.open(tmpname, mode, opts)
[135] Fix | Delete
@data[0] = @tmpname = tmpname
[136] Fix | Delete
@mode = mode & ~(File::CREAT|File::EXCL)
[137] Fix | Delete
opts.freeze
[138] Fix | Delete
@opts = opts
[139] Fix | Delete
end
[140] Fix | Delete
[141] Fix | Delete
super(@tmpfile)
[142] Fix | Delete
end
[143] Fix | Delete
[144] Fix | Delete
# Opens or reopens the file with mode "r+".
[145] Fix | Delete
def open
[146] Fix | Delete
@tmpfile.close if @tmpfile
[147] Fix | Delete
@tmpfile = File.open(@tmpname, @mode, @opts)
[148] Fix | Delete
@data[1] = @tmpfile
[149] Fix | Delete
__setobj__(@tmpfile)
[150] Fix | Delete
end
[151] Fix | Delete
[152] Fix | Delete
def _close # :nodoc:
[153] Fix | Delete
begin
[154] Fix | Delete
@tmpfile.close if @tmpfile
[155] Fix | Delete
ensure
[156] Fix | Delete
@tmpfile = nil
[157] Fix | Delete
@data[1] = nil if @data
[158] Fix | Delete
end
[159] Fix | Delete
end
[160] Fix | Delete
protected :_close
[161] Fix | Delete
[162] Fix | Delete
# Closes the file. If +unlink_now+ is true, then the file will be unlinked
[163] Fix | Delete
# (deleted) after closing. Of course, you can choose to later call #unlink
[164] Fix | Delete
# if you do not unlink it now.
[165] Fix | Delete
#
[166] Fix | Delete
# If you don't explicitly unlink the temporary file, the removal
[167] Fix | Delete
# will be delayed until the object is finalized.
[168] Fix | Delete
def close(unlink_now=false)
[169] Fix | Delete
if unlink_now
[170] Fix | Delete
close!
[171] Fix | Delete
else
[172] Fix | Delete
_close
[173] Fix | Delete
end
[174] Fix | Delete
end
[175] Fix | Delete
[176] Fix | Delete
# Closes and unlinks (deletes) the file. Has the same effect as called
[177] Fix | Delete
# <tt>close(true)</tt>.
[178] Fix | Delete
def close!
[179] Fix | Delete
_close
[180] Fix | Delete
unlink
[181] Fix | Delete
end
[182] Fix | Delete
[183] Fix | Delete
# Unlinks (deletes) the file from the filesystem. One should always unlink
[184] Fix | Delete
# the file after using it, as is explained in the "Explicit close" good
[185] Fix | Delete
# practice section in the Tempfile overview:
[186] Fix | Delete
#
[187] Fix | Delete
# file = Tempfile.new('foo')
[188] Fix | Delete
# begin
[189] Fix | Delete
# ...do something with file...
[190] Fix | Delete
# ensure
[191] Fix | Delete
# file.close
[192] Fix | Delete
# file.unlink # deletes the temp file
[193] Fix | Delete
# end
[194] Fix | Delete
#
[195] Fix | Delete
# === Unlink-before-close
[196] Fix | Delete
#
[197] Fix | Delete
# On POSIX systems it's possible to unlink a file before closing it. This
[198] Fix | Delete
# practice is explained in detail in the Tempfile overview (section
[199] Fix | Delete
# "Unlink after creation"); please refer there for more information.
[200] Fix | Delete
#
[201] Fix | Delete
# However, unlink-before-close may not be supported on non-POSIX operating
[202] Fix | Delete
# systems. Microsoft Windows is the most notable case: unlinking a non-closed
[203] Fix | Delete
# file will result in an error, which this method will silently ignore. If
[204] Fix | Delete
# you want to practice unlink-before-close whenever possible, then you should
[205] Fix | Delete
# write code like this:
[206] Fix | Delete
#
[207] Fix | Delete
# file = Tempfile.new('foo')
[208] Fix | Delete
# file.unlink # On Windows this silently fails.
[209] Fix | Delete
# begin
[210] Fix | Delete
# ... do something with file ...
[211] Fix | Delete
# ensure
[212] Fix | Delete
# file.close! # Closes the file handle. If the file wasn't unlinked
[213] Fix | Delete
# # because #unlink failed, then this method will attempt
[214] Fix | Delete
# # to do so again.
[215] Fix | Delete
# end
[216] Fix | Delete
def unlink
[217] Fix | Delete
return unless @tmpname
[218] Fix | Delete
begin
[219] Fix | Delete
File.unlink(@tmpname)
[220] Fix | Delete
rescue Errno::ENOENT
[221] Fix | Delete
rescue Errno::EACCES
[222] Fix | Delete
# may not be able to unlink on Windows; just ignore
[223] Fix | Delete
return
[224] Fix | Delete
end
[225] Fix | Delete
# remove tmpname from remover
[226] Fix | Delete
@data[0] = @data[1] = nil
[227] Fix | Delete
@tmpname = nil
[228] Fix | Delete
ObjectSpace.undefine_finalizer(self)
[229] Fix | Delete
end
[230] Fix | Delete
alias delete unlink
[231] Fix | Delete
[232] Fix | Delete
# Returns the full path name of the temporary file.
[233] Fix | Delete
# This will be nil if #unlink has been called.
[234] Fix | Delete
def path
[235] Fix | Delete
@tmpname
[236] Fix | Delete
end
[237] Fix | Delete
[238] Fix | Delete
# Returns the size of the temporary file. As a side effect, the IO
[239] Fix | Delete
# buffer is flushed before determining the size.
[240] Fix | Delete
def size
[241] Fix | Delete
if @tmpfile
[242] Fix | Delete
@tmpfile.flush
[243] Fix | Delete
@tmpfile.stat.size
[244] Fix | Delete
elsif @tmpname
[245] Fix | Delete
File.size(@tmpname)
[246] Fix | Delete
else
[247] Fix | Delete
0
[248] Fix | Delete
end
[249] Fix | Delete
end
[250] Fix | Delete
alias length size
[251] Fix | Delete
[252] Fix | Delete
# :stopdoc:
[253] Fix | Delete
def inspect
[254] Fix | Delete
if closed?
[255] Fix | Delete
"#<#{self.class}:#{path} (closed)>"
[256] Fix | Delete
else
[257] Fix | Delete
"#<#{self.class}:#{path}>"
[258] Fix | Delete
end
[259] Fix | Delete
end
[260] Fix | Delete
[261] Fix | Delete
class Remover
[262] Fix | Delete
def initialize(data)
[263] Fix | Delete
@pid = $$
[264] Fix | Delete
@data = data
[265] Fix | Delete
end
[266] Fix | Delete
[267] Fix | Delete
def call(*args)
[268] Fix | Delete
return if @pid != $$
[269] Fix | Delete
[270] Fix | Delete
path, tmpfile = @data
[271] Fix | Delete
[272] Fix | Delete
STDERR.print "removing ", path, "..." if $DEBUG
[273] Fix | Delete
[274] Fix | Delete
tmpfile.close if tmpfile
[275] Fix | Delete
[276] Fix | Delete
if path
[277] Fix | Delete
begin
[278] Fix | Delete
File.unlink(path)
[279] Fix | Delete
rescue Errno::ENOENT
[280] Fix | Delete
end
[281] Fix | Delete
end
[282] Fix | Delete
[283] Fix | Delete
STDERR.print "done\n" if $DEBUG
[284] Fix | Delete
end
[285] Fix | Delete
end
[286] Fix | Delete
[287] Fix | Delete
class << self
[288] Fix | Delete
# :startdoc:
[289] Fix | Delete
[290] Fix | Delete
# Creates a new Tempfile.
[291] Fix | Delete
#
[292] Fix | Delete
# If no block is given, this is a synonym for Tempfile.new.
[293] Fix | Delete
#
[294] Fix | Delete
# If a block is given, then a Tempfile object will be constructed,
[295] Fix | Delete
# and the block is run with said object as argument. The Tempfile
[296] Fix | Delete
# object will be automatically closed after the block terminates.
[297] Fix | Delete
# The call returns the value of the block.
[298] Fix | Delete
#
[299] Fix | Delete
# In any case, all arguments (+*args+) will be passed to Tempfile.new.
[300] Fix | Delete
#
[301] Fix | Delete
# Tempfile.open('foo', '/home/temp') do |f|
[302] Fix | Delete
# ... do something with f ...
[303] Fix | Delete
# end
[304] Fix | Delete
#
[305] Fix | Delete
# # Equivalent:
[306] Fix | Delete
# f = Tempfile.open('foo', '/home/temp')
[307] Fix | Delete
# begin
[308] Fix | Delete
# ... do something with f ...
[309] Fix | Delete
# ensure
[310] Fix | Delete
# f.close
[311] Fix | Delete
# end
[312] Fix | Delete
def open(*args)
[313] Fix | Delete
tempfile = new(*args)
[314] Fix | Delete
[315] Fix | Delete
if block_given?
[316] Fix | Delete
begin
[317] Fix | Delete
yield(tempfile)
[318] Fix | Delete
ensure
[319] Fix | Delete
tempfile.close
[320] Fix | Delete
end
[321] Fix | Delete
else
[322] Fix | Delete
tempfile
[323] Fix | Delete
end
[324] Fix | Delete
end
[325] Fix | Delete
end
[326] Fix | Delete
end
[327] Fix | Delete
[328] Fix | Delete
# Creates a temporally file as usual File object (not Tempfile).
[329] Fix | Delete
# It don't use finalizer and delegation.
[330] Fix | Delete
#
[331] Fix | Delete
# If no block is given, this is similar to Tempfile.new except
[332] Fix | Delete
# creating File instead of Tempfile.
[333] Fix | Delete
# The created file is not removed automatically.
[334] Fix | Delete
# You should use File.unlink to remove it.
[335] Fix | Delete
#
[336] Fix | Delete
# If a block is given, then a File object will be constructed,
[337] Fix | Delete
# and the block is invoked with the object as the argument.
[338] Fix | Delete
# The File object will be automatically closed and
[339] Fix | Delete
# the temporally file is removed after the block terminates.
[340] Fix | Delete
# The call returns the value of the block.
[341] Fix | Delete
#
[342] Fix | Delete
# In any case, all arguments (+*args+) will be treated as Tempfile.new.
[343] Fix | Delete
#
[344] Fix | Delete
# Tempfile.create('foo', '/home/temp') do |f|
[345] Fix | Delete
# ... do something with f ...
[346] Fix | Delete
# end
[347] Fix | Delete
#
[348] Fix | Delete
def Tempfile.create(basename, tmpdir=nil, mode: 0, **options)
[349] Fix | Delete
tmpfile = nil
[350] Fix | Delete
Dir::Tmpname.create(basename, tmpdir, options) do |tmpname, n, opts|
[351] Fix | Delete
mode |= File::RDWR|File::CREAT|File::EXCL
[352] Fix | Delete
opts[:perm] = 0600
[353] Fix | Delete
tmpfile = File.open(tmpname, mode, opts)
[354] Fix | Delete
end
[355] Fix | Delete
if block_given?
[356] Fix | Delete
begin
[357] Fix | Delete
yield tmpfile
[358] Fix | Delete
ensure
[359] Fix | Delete
tmpfile.close if !tmpfile.closed?
[360] Fix | Delete
File.unlink tmpfile
[361] Fix | Delete
end
[362] Fix | Delete
else
[363] Fix | Delete
tmpfile
[364] Fix | Delete
end
[365] Fix | Delete
end
[366] Fix | Delete
[367] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function