From: Tanaka Akira Date: 2007-08-14T21:05:30+09:00 Subject: [ruby-dev:31416] Dir.mktmpdir テンポラリディレクトリを作るメソッドを用意するといいと思うん ですが、どうでしょう? tmpdir.rb で Dir.mktmpdir というのを定義するのはどうでしょう か。 * permission は 0700 にする * ブロックつきで呼んだときは後で消す ** そのときちゃんと FileUtils.remove_entry_secure を使う Index: lib/tmpdir.rb =================================================================== --- lib/tmpdir.rb (リビジョン 12928) +++ lib/tmpdir.rb (作業コピー) @@ -4,6 +4,8 @@ # $Id$ # +require 'fileutils' + class Dir @@systmpdir = '/tmp' @@ -42,4 +44,60 @@ end File.expand_path(tmp) end + + # Dir.mktmpdir creates a temporary directory. + # + # The directory is created with 0700 permission + # under Dir.tmpdir. + # The name of the directory is prefixed + # with prefix argument. + # If prefix is not given, + # the prefix "d" is used. + # + # If a block is given, + # it is yielded with the path of the directory. + # The directory is removed before Dir.mktmpdir returns. + # + # Dir.mktmpdir {|dir| + # # use the directory... + # open("#{dir}/foo", "w") { ... } + # } + # + # If a block is not given, + # The path of the directory is returned. + # In this case, Dir.mktmpdir doesn't remove the directory. + # + # dir = Dir.mktmpdir + # begin + # # use the directory... + # open("#{dir}/foo", "w") { ... } + # ensure + # # remove the directory. + # FileUtils.remove_entry_secure dir + # end + # + def Dir.mktmpdir(prefix="d") + 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 + 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 なお、実装して気がつきましたが、現在は SEGV するようです。 1.8 なら動きます。 % ./ruby -Ilib -rtmpdir -e 'Dir.mktmpdir {|x| p x }' "/tmp/d20070814-6062-p2zi10" /home/akr/ruby/yarvo0/ruby/lib/fileutils.rb:696: -- control frame ---------- c:0006 p:---- s:0027 b:0027 l:000026 d:000026 CFUNC :euid c:0005 p:0213 s:0024 b:0024 l:000023 d:000023 METHOD /home/akr/ruby/yarvo0/ruby/lib/fileutils.rb:696 c:0004 p:0150 s:0014 b:0013 l:000012 d:000012 METHOD /home/akr/ruby/yarvo0/ruby/lib/tmpdir.rb:97 c:0003 p:0013 s:0005 b:0005 l:000004 d:000004 TOP -e:1 c:0002 p:---- s:0003 b:0003 l:000002 d:000002 FINISH :inherited c:0001 p:---- s:0001 b:-001 l:000000 d:000000 ------ --------------------------- DBG> : "/home/akr/ruby/yarvo0/ruby/lib/fileutils.rb:696:in DBG> : `remove_entry_secure'" DBG> : "/home/akr/ruby/yarvo0/ruby/lib/tmpdir.rb:97:in DBG> : `mktmpdir'" DBG> : "-e:1:in `
'" -- backtrace of native function call (Use addr2line) -- 0x80ec513 0x8105e96 0x8105f2d 0x80bccb5 0xffffe420 0x8073a49 0x80952a1 0x80e98fd 0x80eb5bb 0x80ea47d 0x80e5bb8 0x80e7f55 0x80e86f1 0x805a1c5 0x805a258 0x805716e 0x80570c9 0x80570ef 0x80570ef 0x80570ef 0x80570ef 0x80570ef 0x80570ef 0x80570ef 0x80570ef 0x80570ef 0x80570ef 0x8057128 0xb7e2e974 0x8057001 ------------------------------------------------------- [BUG] Segmentation fault ruby 1.9.0 (2007-08-14) [i686-linux] -- [田中 哲][たなか あきら][Tanaka Akira]