Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby27/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
# === Unlink after creation
[55] Fix | Delete
#
[56] Fix | Delete
# On POSIX systems, it's possible to unlink a file right after creating it,
[57] Fix | Delete
# and before closing it. This removes the filesystem entry without closing
[58] Fix | Delete
# the file handle, so it ensures that only the processes that already had
[59] Fix | Delete
# the file handle open can access the file's contents. It's strongly
[60] Fix | Delete
# recommended that you do this if you do not want any other processes to
[61] Fix | Delete
# be able to read from or write to the Tempfile, and you do not need to
[62] Fix | Delete
# know the Tempfile's filename either.
[63] Fix | Delete
#
[64] Fix | Delete
# For example, a practical use case for unlink-after-creation would be this:
[65] Fix | Delete
# you need a large byte buffer that's too large to comfortably fit in RAM,
[66] Fix | Delete
# e.g. when you're writing a web server and you want to buffer the client's
[67] Fix | Delete
# file upload data.
[68] Fix | Delete
#
[69] Fix | Delete
# Please refer to #unlink for more information and a code example.
[70] Fix | Delete
#
[71] Fix | Delete
# == Minor notes
[72] Fix | Delete
#
[73] Fix | Delete
# Tempfile's filename picking method is both thread-safe and inter-process-safe:
[74] Fix | Delete
# it guarantees that no other threads or processes will pick the same filename.
[75] Fix | Delete
#
[76] Fix | Delete
# Tempfile itself however may not be entirely thread-safe. If you access the
[77] Fix | Delete
# same Tempfile object from multiple threads then you should protect it with a
[78] Fix | Delete
# mutex.
[79] Fix | Delete
class Tempfile < DelegateClass(File)
[80] Fix | Delete
# Creates a temporary file with permissions 0600 (= only readable and
[81] Fix | Delete
# writable by the owner) and opens it with mode "w+".
[82] Fix | Delete
#
[83] Fix | Delete
# The +basename+ parameter is used to determine the name of the
[84] Fix | Delete
# temporary file. You can either pass a String or an Array with
[85] Fix | Delete
# 2 String elements. In the former form, the temporary file's base
[86] Fix | Delete
# name will begin with the given string. In the latter form,
[87] Fix | Delete
# the temporary file's base name will begin with the array's first
[88] Fix | Delete
# element, and end with the second element. For example:
[89] Fix | Delete
#
[90] Fix | Delete
# file = Tempfile.new('hello')
[91] Fix | Delete
# file.path # => something like: "/tmp/hello2843-8392-92849382--0"
[92] Fix | Delete
#
[93] Fix | Delete
# # Use the Array form to enforce an extension in the filename:
[94] Fix | Delete
# file = Tempfile.new(['hello', '.jpg'])
[95] Fix | Delete
# file.path # => something like: "/tmp/hello2843-8392-92849382--0.jpg"
[96] Fix | Delete
#
[97] Fix | Delete
# The temporary file will be placed in the directory as specified
[98] Fix | Delete
# by the +tmpdir+ parameter. By default, this is +Dir.tmpdir+.
[99] Fix | Delete
#
[100] Fix | Delete
# file = Tempfile.new('hello', '/home/aisaka')
[101] Fix | Delete
# file.path # => something like: "/home/aisaka/hello2843-8392-92849382--0"
[102] Fix | Delete
#
[103] Fix | Delete
# You can also pass an options hash. Under the hood, Tempfile creates
[104] Fix | Delete
# the temporary file using +File.open+. These options will be passed to
[105] Fix | Delete
# +File.open+. This is mostly useful for specifying encoding
[106] Fix | Delete
# options, e.g.:
[107] Fix | Delete
#
[108] Fix | Delete
# Tempfile.new('hello', '/home/aisaka', encoding: 'ascii-8bit')
[109] Fix | Delete
#
[110] Fix | Delete
# # You can also omit the 'tmpdir' parameter:
[111] Fix | Delete
# Tempfile.new('hello', encoding: 'ascii-8bit')
[112] Fix | Delete
#
[113] Fix | Delete
# Note: +mode+ keyword argument, as accepted by Tempfile, can only be
[114] Fix | Delete
# numeric, combination of the modes defined in File::Constants.
[115] Fix | Delete
#
[116] Fix | Delete
# === Exceptions
[117] Fix | Delete
#
[118] Fix | Delete
# If Tempfile.new cannot find a unique filename within a limited
[119] Fix | Delete
# number of tries, then it will raise an exception.
[120] Fix | Delete
def initialize(basename="", tmpdir=nil, mode: 0, **options)
[121] Fix | Delete
warn "Tempfile.new doesn't call the given block.", uplevel: 1 if block_given?
[122] Fix | Delete
[123] Fix | Delete
@unlinked = false
[124] Fix | Delete
@mode = mode|File::RDWR|File::CREAT|File::EXCL
[125] Fix | Delete
::Dir::Tmpname.create(basename, tmpdir, **options) do |tmpname, n, opts|
[126] Fix | Delete
opts[:perm] = 0600
[127] Fix | Delete
@tmpfile = File.open(tmpname, @mode, **opts)
[128] Fix | Delete
@opts = opts.freeze
[129] Fix | Delete
end
[130] Fix | Delete
ObjectSpace.define_finalizer(self, Remover.new(@tmpfile))
[131] Fix | Delete
[132] Fix | Delete
super(@tmpfile)
[133] Fix | Delete
end
[134] Fix | Delete
[135] Fix | Delete
# Opens or reopens the file with mode "r+".
[136] Fix | Delete
def open
[137] Fix | Delete
_close
[138] Fix | Delete
mode = @mode & ~(File::CREAT|File::EXCL)
[139] Fix | Delete
@tmpfile = File.open(@tmpfile.path, mode, **@opts)
[140] Fix | Delete
__setobj__(@tmpfile)
[141] Fix | Delete
end
[142] Fix | Delete
[143] Fix | Delete
def _close # :nodoc:
[144] Fix | Delete
@tmpfile.close
[145] Fix | Delete
end
[146] Fix | Delete
protected :_close
[147] Fix | Delete
[148] Fix | Delete
# Closes the file. If +unlink_now+ is true, then the file will be unlinked
[149] Fix | Delete
# (deleted) after closing. Of course, you can choose to later call #unlink
[150] Fix | Delete
# if you do not unlink it now.
[151] Fix | Delete
#
[152] Fix | Delete
# If you don't explicitly unlink the temporary file, the removal
[153] Fix | Delete
# will be delayed until the object is finalized.
[154] Fix | Delete
def close(unlink_now=false)
[155] Fix | Delete
_close
[156] Fix | Delete
unlink if unlink_now
[157] Fix | Delete
end
[158] Fix | Delete
[159] Fix | Delete
# Closes and unlinks (deletes) the file. Has the same effect as called
[160] Fix | Delete
# <tt>close(true)</tt>.
[161] Fix | Delete
def close!
[162] Fix | Delete
close(true)
[163] Fix | Delete
end
[164] Fix | Delete
[165] Fix | Delete
# Unlinks (deletes) the file from the filesystem. One should always unlink
[166] Fix | Delete
# the file after using it, as is explained in the "Explicit close" good
[167] Fix | Delete
# practice section in the Tempfile overview:
[168] Fix | Delete
#
[169] Fix | Delete
# file = Tempfile.new('foo')
[170] Fix | Delete
# begin
[171] Fix | Delete
# # ...do something with file...
[172] Fix | Delete
# ensure
[173] Fix | Delete
# file.close
[174] Fix | Delete
# file.unlink # deletes the temp file
[175] Fix | Delete
# end
[176] Fix | Delete
#
[177] Fix | Delete
# === Unlink-before-close
[178] Fix | Delete
#
[179] Fix | Delete
# On POSIX systems it's possible to unlink a file before closing it. This
[180] Fix | Delete
# practice is explained in detail in the Tempfile overview (section
[181] Fix | Delete
# "Unlink after creation"); please refer there for more information.
[182] Fix | Delete
#
[183] Fix | Delete
# However, unlink-before-close may not be supported on non-POSIX operating
[184] Fix | Delete
# systems. Microsoft Windows is the most notable case: unlinking a non-closed
[185] Fix | Delete
# file will result in an error, which this method will silently ignore. If
[186] Fix | Delete
# you want to practice unlink-before-close whenever possible, then you should
[187] Fix | Delete
# write code like this:
[188] Fix | Delete
#
[189] Fix | Delete
# file = Tempfile.new('foo')
[190] Fix | Delete
# file.unlink # On Windows this silently fails.
[191] Fix | Delete
# begin
[192] Fix | Delete
# # ... do something with file ...
[193] Fix | Delete
# ensure
[194] Fix | Delete
# file.close! # Closes the file handle. If the file wasn't unlinked
[195] Fix | Delete
# # because #unlink failed, then this method will attempt
[196] Fix | Delete
# # to do so again.
[197] Fix | Delete
# end
[198] Fix | Delete
def unlink
[199] Fix | Delete
return if @unlinked
[200] Fix | Delete
begin
[201] Fix | Delete
File.unlink(@tmpfile.path)
[202] Fix | Delete
rescue Errno::ENOENT
[203] Fix | Delete
rescue Errno::EACCES
[204] Fix | Delete
# may not be able to unlink on Windows; just ignore
[205] Fix | Delete
return
[206] Fix | Delete
end
[207] Fix | Delete
ObjectSpace.undefine_finalizer(self)
[208] Fix | Delete
@unlinked = true
[209] Fix | Delete
end
[210] Fix | Delete
alias delete unlink
[211] Fix | Delete
[212] Fix | Delete
# Returns the full path name of the temporary file.
[213] Fix | Delete
# This will be nil if #unlink has been called.
[214] Fix | Delete
def path
[215] Fix | Delete
@unlinked ? nil : @tmpfile.path
[216] Fix | Delete
end
[217] Fix | Delete
[218] Fix | Delete
# Returns the size of the temporary file. As a side effect, the IO
[219] Fix | Delete
# buffer is flushed before determining the size.
[220] Fix | Delete
def size
[221] Fix | Delete
if !@tmpfile.closed?
[222] Fix | Delete
@tmpfile.size # File#size calls rb_io_flush_raw()
[223] Fix | Delete
else
[224] Fix | Delete
File.size(@tmpfile.path)
[225] Fix | Delete
end
[226] Fix | Delete
end
[227] Fix | Delete
alias length size
[228] Fix | Delete
[229] Fix | Delete
# :stopdoc:
[230] Fix | Delete
def inspect
[231] Fix | Delete
if @tmpfile.closed?
[232] Fix | Delete
"#<#{self.class}:#{path} (closed)>"
[233] Fix | Delete
else
[234] Fix | Delete
"#<#{self.class}:#{path}>"
[235] Fix | Delete
end
[236] Fix | Delete
end
[237] Fix | Delete
[238] Fix | Delete
class Remover # :nodoc:
[239] Fix | Delete
def initialize(tmpfile)
[240] Fix | Delete
@pid = Process.pid
[241] Fix | Delete
@tmpfile = tmpfile
[242] Fix | Delete
end
[243] Fix | Delete
[244] Fix | Delete
def call(*args)
[245] Fix | Delete
return if @pid != Process.pid
[246] Fix | Delete
[247] Fix | Delete
$stderr.puts "removing #{@tmpfile.path}..." if $DEBUG
[248] Fix | Delete
[249] Fix | Delete
@tmpfile.close
[250] Fix | Delete
begin
[251] Fix | Delete
File.unlink(@tmpfile.path)
[252] Fix | Delete
rescue Errno::ENOENT
[253] Fix | Delete
end
[254] Fix | Delete
[255] Fix | Delete
$stderr.puts "done" if $DEBUG
[256] Fix | Delete
end
[257] Fix | Delete
end
[258] Fix | Delete
[259] Fix | Delete
class << self
[260] Fix | Delete
# :startdoc:
[261] Fix | Delete
[262] Fix | Delete
# Creates a new Tempfile.
[263] Fix | Delete
#
[264] Fix | Delete
# If no block is given, this is a synonym for Tempfile.new.
[265] Fix | Delete
#
[266] Fix | Delete
# If a block is given, then a Tempfile object will be constructed,
[267] Fix | Delete
# and the block is run with said object as argument. The Tempfile
[268] Fix | Delete
# object will be automatically closed after the block terminates.
[269] Fix | Delete
# The call returns the value of the block.
[270] Fix | Delete
#
[271] Fix | Delete
# In any case, all arguments (<code>*args</code>) will be passed to Tempfile.new.
[272] Fix | Delete
#
[273] Fix | Delete
# Tempfile.open('foo', '/home/temp') do |f|
[274] Fix | Delete
# # ... do something with f ...
[275] Fix | Delete
# end
[276] Fix | Delete
#
[277] Fix | Delete
# # Equivalent:
[278] Fix | Delete
# f = Tempfile.open('foo', '/home/temp')
[279] Fix | Delete
# begin
[280] Fix | Delete
# # ... do something with f ...
[281] Fix | Delete
# ensure
[282] Fix | Delete
# f.close
[283] Fix | Delete
# end
[284] Fix | Delete
def open(*args, **kw)
[285] Fix | Delete
tempfile = new(*args, **kw)
[286] Fix | Delete
[287] Fix | Delete
if block_given?
[288] Fix | Delete
begin
[289] Fix | Delete
yield(tempfile)
[290] Fix | Delete
ensure
[291] Fix | Delete
tempfile.close
[292] Fix | Delete
end
[293] Fix | Delete
else
[294] Fix | Delete
tempfile
[295] Fix | Delete
end
[296] Fix | Delete
end
[297] Fix | Delete
end
[298] Fix | Delete
end
[299] Fix | Delete
[300] Fix | Delete
# Creates a temporary file as usual File object (not Tempfile).
[301] Fix | Delete
# It doesn't use finalizer and delegation.
[302] Fix | Delete
#
[303] Fix | Delete
# If no block is given, this is similar to Tempfile.new except
[304] Fix | Delete
# creating File instead of Tempfile.
[305] Fix | Delete
# The created file is not removed automatically.
[306] Fix | Delete
# You should use File.unlink to remove it.
[307] Fix | Delete
#
[308] Fix | Delete
# If a block is given, then a File object will be constructed,
[309] Fix | Delete
# and the block is invoked with the object as the argument.
[310] Fix | Delete
# The File object will be automatically closed and
[311] Fix | Delete
# the temporary file is removed after the block terminates.
[312] Fix | Delete
# The call returns the value of the block.
[313] Fix | Delete
#
[314] Fix | Delete
# In any case, all arguments (+basename+, +tmpdir+, +mode+, and
[315] Fix | Delete
# <code>**options</code>) will be treated as Tempfile.new.
[316] Fix | Delete
#
[317] Fix | Delete
# Tempfile.create('foo', '/home/temp') do |f|
[318] Fix | Delete
# # ... do something with f ...
[319] Fix | Delete
# end
[320] Fix | Delete
#
[321] Fix | Delete
def Tempfile.create(basename="", tmpdir=nil, mode: 0, **options)
[322] Fix | Delete
tmpfile = nil
[323] Fix | Delete
Dir::Tmpname.create(basename, tmpdir, **options) do |tmpname, n, opts|
[324] Fix | Delete
mode |= File::RDWR|File::CREAT|File::EXCL
[325] Fix | Delete
opts[:perm] = 0600
[326] Fix | Delete
tmpfile = File.open(tmpname, mode, **opts)
[327] Fix | Delete
end
[328] Fix | Delete
if block_given?
[329] Fix | Delete
begin
[330] Fix | Delete
yield tmpfile
[331] Fix | Delete
ensure
[332] Fix | Delete
unless tmpfile.closed?
[333] Fix | Delete
if File.identical?(tmpfile, tmpfile.path)
[334] Fix | Delete
unlinked = File.unlink tmpfile.path rescue nil
[335] Fix | Delete
end
[336] Fix | Delete
tmpfile.close
[337] Fix | Delete
end
[338] Fix | Delete
unless unlinked
[339] Fix | Delete
begin
[340] Fix | Delete
File.unlink tmpfile.path
[341] Fix | Delete
rescue Errno::ENOENT
[342] Fix | Delete
end
[343] Fix | Delete
end
[344] Fix | Delete
end
[345] Fix | Delete
else
[346] Fix | Delete
tmpfile
[347] Fix | Delete
end
[348] Fix | Delete
end
[349] Fix | Delete
[350] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function