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