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