# Dir.mktmpdir

**URL:** <https://www.ruby-forum.com/t/dir-mktmpdir/112033>\
**Category:** Development\
**Created:** [August 14, 2007, 2:06pm UTC](https://www.ruby-forum.com/t/dir-mktmpdir/112033 "2007-08-14T14:06:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tanaka\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Tanaka\_A](https://www.ruby-forum.com/u/Tanaka_A)\
**Post date:** [August 14, 2007, 2:06pm UTC](https://www.ruby-forum.com/t/dir-mktmpdir/112033/1 "2007-08-14T14:06:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tanaka\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Tanaka\_A](https://www.ruby-forum.com/u/Tanaka_A)\
**Post date:** [August 15, 2007, 5:30pm UTC](https://www.ruby-forum.com/t/dir-mktmpdir/112033/2 "2007-08-15T17:30:03Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![Tanaka\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Tanaka\_A](https://www.ruby-forum.com/u/Tanaka_A)\
**Post date:** [August 15, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/dir-mktmpdir/112033/3 "2007-08-15T19:31:16Z")

</div>

In article [E1ILKnW-0003O8-57@x31](mailto:E1ILKnW-0003O8-57@x31),  
Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
