From 032a314c5025d2d87e5ea11c58162e698fff0168 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 8 Sep 2009 13:38:01 +0000 Subject: * lib/tempfile.rb, lib/tmpdir.rb (Tmpname): extracted new module. [ruby-dev:39197] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24795 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/tmpdir.rb | 89 +++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 59 insertions(+), 30 deletions(-) (limited to 'lib/tmpdir.rb') diff --git a/lib/tmpdir.rb b/lib/tmpdir.rb index 0d247bdd54..0aa9cf5e3b 100644 --- a/lib/tmpdir.rb +++ b/lib/tmpdir.rb @@ -10,8 +10,12 @@ class Dir @@systmpdir = '/tmp' - begin - require 'Win32API' + if /mswin|mingw|cygwin/ =~ RUBY_PLATFORM and + begin + require 'Win32API' + true + rescue LoadError + end CSIDL_LOCAL_APPDATA = 0x001c max_pathlen = 260 windir = "\0"*(max_pathlen+1) @@ -30,7 +34,6 @@ class Dir windir.force_encoding(Dir.pwd.encoding) temp = File.expand_path('temp', windir.untaint) @@systmpdir = temp if File.directory?(temp) and File.writable?(temp) - rescue LoadError end ## @@ -95,41 +98,67 @@ class Dir # FileUtils.remove_entry_secure dir # end # - 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] + def Dir.mktmpdir(prefix_suffix=nil, *rest) + path = Tmpname.create(prefix_suffix || "d", *rest) {|n| mkdir(n, 0700)} + if block_given? + begin + yield path + ensure + FileUtils.remove_entry_secure path + end else - raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}" + path end - tmpdir ||= Dir.tmpdir - t = Time.now.strftime("%Y%m%d") - n = nil - begin - path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}" + end + + module Tmpname + module_function + + def tmpdir + Dir.tmpdir + end + + def make_tmpname(prefix_suffix, n) + case prefix_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 + t = Time.now.strftime("%Y%m%d") + path = "#{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? + def create(basename, *rest) + if opts = Hash.try_convert(rest[-1]) + opts = opts.dup if rest.pop.equal?(opts) + max_try = opts.delete(:max_try) + opts = [opts] + else + opts = [] + end + tmpdir, = *rest + if $SAFE > 0 and tmpdir.tainted? + tmpdir = '/tmp' + else + tmpdir ||= tmpdir() + end + n = nil begin - yield path - ensure - FileUtils.remove_entry_secure path + path = File.expand_path(make_tmpname(basename, n), tmpdir) + yield(path, n, opts) + rescue Errno::EEXIST + n ||= 0 + n += 1 + retry if !max_try or n < max_try + raise "cannot generate temporary name using `#{basename}' under `#{tmpdir}'" end - else path end end -- cgit v1.2.3