72: def self.mktmpdir(prefix_suffix=nil, tmpdir=nil)
73: case prefix_suffix
74: when nil
75: prefix = "d"
76: suffix = ""
77: when String
78: prefix = prefix_suffix
79: suffix = ""
80: when Array
81: prefix = prefix_suffix[0]
82: suffix = prefix_suffix[1]
83: else
84: raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}"
85: end
86: tmpdir ||= Dir.tmpdir
87: t = Time.now.strftime("%Y%m%d")
88: n = nil
89: begin
90: path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
91: path << "-#{n}" if n
92: path << suffix
93: Dir.mkdir(path, 0700)
94: rescue Errno::EEXIST
95: n ||= 0
96: n += 1
97: retry
98: end
99:
100: if block_given?
101: begin
102: yield path
103: ensure
104: FileUtils.remove_entry_secure path
105: end
106: else
107: path
108: end
109: end