43
45
File.expand_path(tmp)
48
# Dir.mktmpdir creates a temporary directory.
50
# The directory is created with 0700 permission.
52
# The prefix and suffix of the name of the directory is specified by
53
# the optional first argument, <i>prefix_suffix</i>.
54
# - If it is not specified or nil, "d" is used as the prefix and no suffix is used.
55
# - If it is a string, it is used as the prefix and no suffix is used.
56
# - If it is an array, first element is used as the prefix and second element is used as a suffix.
58
# Dir.mktmpdir {|dir| dir is ".../d..." }
59
# Dir.mktmpdir("foo") {|dir| dir is ".../foo..." }
60
# Dir.mktmpdir(["foo", "bar"]) {|dir| path is ".../foo...bar" }
62
# The directory is created under Dir.tmpdir or
63
# the optional second argument <i>tmpdir</i> if non-nil value is given.
65
# Dir.mktmpdir {|dir| dir is "#{Dir.tmpdir}/d..." }
66
# Dir.mktmpdir(nil, "/var/tmp") {|dir| dir is "/var/tmp/d..." }
68
# If a block is given,
69
# it is yielded with the path of the directory.
70
# The directory and its contents are removed
71
# using FileUtils.remove_entry_secure before Dir.mktmpdir returns.
72
# The value of the block is returned.
75
# # use the directory...
76
# open("#{dir}/foo", "w") { ... }
79
# If a block is not given,
80
# The path of the directory is returned.
81
# In this case, Dir.mktmpdir doesn't remove the directory.
85
# # use the directory...
86
# open("#{dir}/foo", "w") { ... }
88
# # remove the directory.
89
# FileUtils.remove_entry_secure dir
92
def Dir.mktmpdir(prefix_suffix=nil, tmpdir=nil)
98
prefix = prefix_suffix
101
prefix = prefix_suffix[0]
102
suffix = prefix_suffix[1]
104
raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}"
106
tmpdir ||= Dir.tmpdir
107
t = Time.now.strftime("%Y%m%d")
110
path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
113
Dir.mkdir(path, 0700)
124
FileUtils.remove_entry_secure path