Class: Dir

Inherits:
Object
  • Object
show all
Defined in:
lib/mcollective/monkey_patches.rb

Class Method Summary collapse

Class Method Details

.mktmpdir(prefix_suffix = nil, tmpdir = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mcollective/monkey_patches.rb', line 79

def self.mktmpdir(prefix_suffix=nil, tmpdir=nil)
  case prefix_suffix
  when nil
    prefix = "d"
    suffix = ""
  when String
    prefix = prefix_suffix
    suffix = ""
  when Array
    prefix = prefix_suffix[0]
    suffix = prefix_suffix[1]
  else
    raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}"
  end
  tmpdir ||= Dir.tmpdir
  t = Time.now.strftime("%Y%m%d")
  n = nil
  begin
    path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
    path << "-#{n}" if n
    path << suffix
    Dir.mkdir(path, 0o700)
  rescue Errno::EEXIST
    n ||= 0
    n += 1
    retry
  end

  if block_given?
    begin
      yield path
    ensure
      FileUtils.remove_entry_secure path
    end
  else
    path
  end
end

.tmpdirObject



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/mcollective/monkey_patches.rb', line 120

def self.tmpdir
  tmp = "."
  [ENV["TMPDIR"], ENV["TMP"], ENV["TEMP"], "/tmp"].each do |dir|
    if dir && (stat = File.stat(dir)) && stat.directory? && stat.writable?
      tmp = dir
      break
    end rescue nil
  end

  File.expand_path(tmp)
end