Dir.mktmpdir

e$B%F%s%]%i%j%G%#%l%/%H%j$r:n$k%a%=%C%I$rMQ0U$9$k$H$$$$$H;W$&$se(B
e$B$G$9$,!"$I$&$G$7$g$&e(B?

tmpdir.rb e$B$Ge(B Dir.mktmpdir e$B$H$$$&$N$rDj5A$9$k$N$O$I$&$G$7$g$&e(B
e$B$+!#e(B

  • permission e$B$Oe(B 0700 e$B$K$9$ke(B
  • e$B%V%m%C%/$D$-$G8F$s$@$H$-$O8e$G>C$9e(B
    ** e$B$=$N$H$-$A$c$s$He(B FileUtils.remove_entry_secure e$B$r;H$&e(B

Index: lib/tmpdir.rb

— lib/tmpdir.rb (e$B%j%S%8%g%se(B 12928)
+++ lib/tmpdir.rb (e$B:n6H%3%T!<e(B)
@@ -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

e$B$J$*!"<BAu$7$F5$$,$D$-$^$7$?$,!"8=:_$Oe(B SEGV e$B$9$k$h$&$G$9!#e(B
1.8 e$B$J$iF0$-$^$9!#e(B

% ./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

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:31416] Dir.mktmpdir”
on Tue, 14 Aug 2007 21:05:30 +0900, Tanaka A. [email protected]
writes:

|tmpdir.rb e$B$Ge(B Dir.mktmpdir e$B$H$$$&$N$rDj5A$9$k$N$O$I$&$G$7$g$&e(B
|e$B$+!#e(B
|
|* permission e$B$Oe(B 0700 e$B$K$9$ke(B
|* e$B%V%m%C%/$D$-$G8F$s$@$H$-$O8e$G>C$9e(B
|** e$B$=$N$H$-$A$c$s$He(B FileUtils.remove_entry_secure e$B$r;H$&e(B

e$B$$$$$s$8$c$J$$$G$7$g$&$+!#%3%_%C%H$7$F$/$@$5$$!#e(B

In article E1ILKnW-0003O8-57@x31,
Yukihiro M. [email protected] writes:

|tmpdir.rb e$B$Ge(B Dir.mktmpdir e$B$H$$$&$N$rDj5A$9$k$N$O$I$&$G$7$g$&e(B
|e$B$+!#e(B
|
|* permission e$B$Oe(B 0700 e$B$K$9$ke(B
|* e$B%V%m%C%/$D$-$G8F$s$@$H$-$O8e$G>C$9e(B
|** e$B$=$N$H$-$A$c$s$He(B FileUtils.remove_entry_secure e$B$r;H$&e(B

e$B$$$$$s$8$c$J$$$G$7$g$&$+!#%3%_%C%H$7$F$/$@$5$$!#e(B

e$B%3%_%C%H$7$^$7$?!#e(B

e$B$U$H;W$$$D$$$F!“e(BDir.tmpdir e$B$8$c$J$$%G%#%l%/%H%j$r;XDj$9$k>Je(B
e$BN,2DG=0z?t$r2C$($F$”$j$^$9!#e(B