Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby27/share/ruby
File: tmpdir.rb
# frozen_string_literal: true
[0] Fix | Delete
#
[1] Fix | Delete
# tmpdir - retrieve temporary directory path
[2] Fix | Delete
#
[3] Fix | Delete
# $Id$
[4] Fix | Delete
#
[5] Fix | Delete
[6] Fix | Delete
require 'fileutils'
[7] Fix | Delete
begin
[8] Fix | Delete
require 'etc.so'
[9] Fix | Delete
rescue LoadError # rescue LoadError for miniruby
[10] Fix | Delete
end
[11] Fix | Delete
[12] Fix | Delete
class Dir
[13] Fix | Delete
[14] Fix | Delete
@@systmpdir ||= defined?(Etc.systmpdir) ? Etc.systmpdir : '/tmp'
[15] Fix | Delete
[16] Fix | Delete
##
[17] Fix | Delete
# Returns the operating system's temporary file path.
[18] Fix | Delete
[19] Fix | Delete
def self.tmpdir
[20] Fix | Delete
tmp = nil
[21] Fix | Delete
[ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp', '.'].each do |dir|
[22] Fix | Delete
next if !dir
[23] Fix | Delete
dir = File.expand_path(dir)
[24] Fix | Delete
if stat = File.stat(dir) and stat.directory? and stat.writable? and
[25] Fix | Delete
(!stat.world_writable? or stat.sticky?)
[26] Fix | Delete
tmp = dir
[27] Fix | Delete
break
[28] Fix | Delete
end rescue nil
[29] Fix | Delete
end
[30] Fix | Delete
raise ArgumentError, "could not find a temporary directory" unless tmp
[31] Fix | Delete
tmp
[32] Fix | Delete
end
[33] Fix | Delete
[34] Fix | Delete
# Dir.mktmpdir creates a temporary directory.
[35] Fix | Delete
#
[36] Fix | Delete
# The directory is created with 0700 permission.
[37] Fix | Delete
# Application should not change the permission to make the temporary directory accessible from other users.
[38] Fix | Delete
#
[39] Fix | Delete
# The prefix and suffix of the name of the directory is specified by
[40] Fix | Delete
# the optional first argument, <i>prefix_suffix</i>.
[41] Fix | Delete
# - If it is not specified or nil, "d" is used as the prefix and no suffix is used.
[42] Fix | Delete
# - If it is a string, it is used as the prefix and no suffix is used.
[43] Fix | Delete
# - If it is an array, first element is used as the prefix and second element is used as a suffix.
[44] Fix | Delete
#
[45] Fix | Delete
# Dir.mktmpdir {|dir| dir is ".../d..." }
[46] Fix | Delete
# Dir.mktmpdir("foo") {|dir| dir is ".../foo..." }
[47] Fix | Delete
# Dir.mktmpdir(["foo", "bar"]) {|dir| dir is ".../foo...bar" }
[48] Fix | Delete
#
[49] Fix | Delete
# The directory is created under Dir.tmpdir or
[50] Fix | Delete
# the optional second argument <i>tmpdir</i> if non-nil value is given.
[51] Fix | Delete
#
[52] Fix | Delete
# Dir.mktmpdir {|dir| dir is "#{Dir.tmpdir}/d..." }
[53] Fix | Delete
# Dir.mktmpdir(nil, "/var/tmp") {|dir| dir is "/var/tmp/d..." }
[54] Fix | Delete
#
[55] Fix | Delete
# If a block is given,
[56] Fix | Delete
# it is yielded with the path of the directory.
[57] Fix | Delete
# The directory and its contents are removed
[58] Fix | Delete
# using FileUtils.remove_entry before Dir.mktmpdir returns.
[59] Fix | Delete
# The value of the block is returned.
[60] Fix | Delete
#
[61] Fix | Delete
# Dir.mktmpdir {|dir|
[62] Fix | Delete
# # use the directory...
[63] Fix | Delete
# open("#{dir}/foo", "w") { ... }
[64] Fix | Delete
# }
[65] Fix | Delete
#
[66] Fix | Delete
# If a block is not given,
[67] Fix | Delete
# The path of the directory is returned.
[68] Fix | Delete
# In this case, Dir.mktmpdir doesn't remove the directory.
[69] Fix | Delete
#
[70] Fix | Delete
# dir = Dir.mktmpdir
[71] Fix | Delete
# begin
[72] Fix | Delete
# # use the directory...
[73] Fix | Delete
# open("#{dir}/foo", "w") { ... }
[74] Fix | Delete
# ensure
[75] Fix | Delete
# # remove the directory.
[76] Fix | Delete
# FileUtils.remove_entry dir
[77] Fix | Delete
# end
[78] Fix | Delete
#
[79] Fix | Delete
def self.mktmpdir(prefix_suffix=nil, *rest, **options)
[80] Fix | Delete
base = nil
[81] Fix | Delete
path = Tmpname.create(prefix_suffix || "d", *rest, **options) {|path, _, _, d|
[82] Fix | Delete
base = d
[83] Fix | Delete
mkdir(path, 0700)
[84] Fix | Delete
}
[85] Fix | Delete
if block_given?
[86] Fix | Delete
begin
[87] Fix | Delete
yield path.dup
[88] Fix | Delete
ensure
[89] Fix | Delete
unless base
[90] Fix | Delete
stat = File.stat(File.dirname(path))
[91] Fix | Delete
if stat.world_writable? and !stat.sticky?
[92] Fix | Delete
raise ArgumentError, "parent directory is world writable but not sticky"
[93] Fix | Delete
end
[94] Fix | Delete
end
[95] Fix | Delete
FileUtils.remove_entry path
[96] Fix | Delete
end
[97] Fix | Delete
else
[98] Fix | Delete
path
[99] Fix | Delete
end
[100] Fix | Delete
end
[101] Fix | Delete
[102] Fix | Delete
module Tmpname # :nodoc:
[103] Fix | Delete
module_function
[104] Fix | Delete
[105] Fix | Delete
def tmpdir
[106] Fix | Delete
Dir.tmpdir
[107] Fix | Delete
end
[108] Fix | Delete
[109] Fix | Delete
UNUSABLE_CHARS = "^,-.0-9A-Z_a-z~"
[110] Fix | Delete
[111] Fix | Delete
def create(basename, tmpdir=nil, max_try: nil, **opts)
[112] Fix | Delete
origdir = tmpdir
[113] Fix | Delete
tmpdir ||= tmpdir()
[114] Fix | Delete
n = nil
[115] Fix | Delete
prefix, suffix = basename
[116] Fix | Delete
prefix = (String.try_convert(prefix) or
[117] Fix | Delete
raise ArgumentError, "unexpected prefix: #{prefix.inspect}")
[118] Fix | Delete
prefix = prefix.delete(UNUSABLE_CHARS)
[119] Fix | Delete
suffix &&= (String.try_convert(suffix) or
[120] Fix | Delete
raise ArgumentError, "unexpected suffix: #{suffix.inspect}")
[121] Fix | Delete
suffix &&= suffix.delete(UNUSABLE_CHARS)
[122] Fix | Delete
begin
[123] Fix | Delete
t = Time.now.strftime("%Y%m%d")
[124] Fix | Delete
path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"\
[125] Fix | Delete
"#{n ? %[-#{n}] : ''}#{suffix||''}"
[126] Fix | Delete
path = File.join(tmpdir, path)
[127] Fix | Delete
yield(path, n, opts, origdir)
[128] Fix | Delete
rescue Errno::EEXIST
[129] Fix | Delete
n ||= 0
[130] Fix | Delete
n += 1
[131] Fix | Delete
retry if !max_try or n < max_try
[132] Fix | Delete
raise "cannot generate temporary name using `#{basename}' under `#{tmpdir}'"
[133] Fix | Delete
end
[134] Fix | Delete
path
[135] Fix | Delete
end
[136] Fix | Delete
end
[137] Fix | Delete
end
[138] Fix | Delete
[139] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function