Permalink
Browse files

Experimental Dir.mktmpdir transplant from future Ruby

  • Loading branch information...
1 parent 7bc91c0 commit 27d51add4a2388dab969c4cd4620142b6c5c843e @jhs jhs committed with steveyen Mar 7, 2011
Showing with 42 additions and 0 deletions.
  1. +42 −0 tasks/lib.rb
View
@@ -43,6 +43,48 @@
require File.dirname(__FILE__) + '/distros'
+# This is the Ruby v1.9.1 Dir.mktmpdir.
+if ! Dir.respond_to? :mktmpdir
+ def Dir.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, 0700)
+ 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
+end
+
def package_dep opts
# Unfortunately the dependency must be defined after the OS is detected,
# Even if this task is a no-op, if other tasks depend on it, they will re-run

0 comments on commit 27d51ad

Please sign in to comment.