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