Class Dir
In: lib/mcollective/monkey_patches.rb
Parent: Object

Methods

mktmpdir   tmpdir  

Public Class methods

[Source]

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

[Source]

     # File lib/mcollective/monkey_patches.rb, line 102
102:   def self.tmpdir
103:     tmp = '.'
104:     for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], '/tmp']
105:       if dir and stat = File.stat(dir) and stat.directory? and stat.writable?
106:         tmp = dir
107:         break
108:       end rescue nil
109:     end
110:     File.expand_path(tmp)
111:   end

[Validate]