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