Skip to content
Newer
Older
100644 144 lines (133 sloc) 3.69 KB
511dc44 initial import
Laurent Sansonetti authored
1 #
2 # tmpdir - retrieve temporary directory path
3 #
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
4 # $Id: tmpdir.rb 27667 2010-05-08 03:25:17Z nobu $
511dc44 initial import
Laurent Sansonetti authored
5 #
6
7 require 'fileutils'
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
8 begin
75f2ab2 require 'etc' not 'etc.so'
Laurent Sansonetti authored
9 require 'etc'
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
10 rescue LoadError
11 end
511dc44 initial import
Laurent Sansonetti authored
12
13 class Dir
14
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
15 @@systmpdir ||= defined?(Etc.systmpdir) ? Etc.systmpdir : '/tmp'
511dc44 initial import
Laurent Sansonetti authored
16
17 ##
18 # Returns the operating system's temporary file path.
19
20 def Dir::tmpdir
21 tmp = '.'
22 if $SAFE > 0
23 tmp = @@systmpdir
24 else
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
25 for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp']
26 if dir and stat = File.stat(dir) and stat.directory? and stat.writable?
511dc44 initial import
Laurent Sansonetti authored
27 tmp = dir
28 break
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
29 end rescue nil
511dc44 initial import
Laurent Sansonetti authored
30 end
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
31 File.expand_path(tmp)
511dc44 initial import
Laurent Sansonetti authored
32 end
33 end
34
35 # Dir.mktmpdir creates a temporary directory.
36 #
37 # The directory is created with 0700 permission.
38 #
39 # The prefix and suffix of the name of the directory is specified by
40 # the optional first argument, <i>prefix_suffix</i>.
41 # - If it is not specified or nil, "d" is used as the prefix and no suffix is used.
42 # - If it is a string, it is used as the prefix and no suffix is used.
43 # - If it is an array, first element is used as the prefix and second element is used as a suffix.
44 #
45 # Dir.mktmpdir {|dir| dir is ".../d..." }
46 # Dir.mktmpdir("foo") {|dir| dir is ".../foo..." }
47 # Dir.mktmpdir(["foo", "bar"]) {|dir| dir is ".../foo...bar" }
48 #
49 # The directory is created under Dir.tmpdir or
50 # the optional second argument <i>tmpdir</i> if non-nil value is given.
51 #
52 # Dir.mktmpdir {|dir| dir is "#{Dir.tmpdir}/d..." }
53 # Dir.mktmpdir(nil, "/var/tmp") {|dir| dir is "/var/tmp/d..." }
54 #
55 # If a block is given,
56 # it is yielded with the path of the directory.
57 # The directory and its contents are removed
58 # using FileUtils.remove_entry_secure before Dir.mktmpdir returns.
59 # The value of the block is returned.
60 #
61 # Dir.mktmpdir {|dir|
62 # # use the directory...
63 # open("#{dir}/foo", "w") { ... }
64 # }
65 #
66 # If a block is not given,
67 # The path of the directory is returned.
68 # In this case, Dir.mktmpdir doesn't remove the directory.
69 #
70 # dir = Dir.mktmpdir
71 # begin
72 # # use the directory...
73 # open("#{dir}/foo", "w") { ... }
74 # ensure
75 # # remove the directory.
76 # FileUtils.remove_entry_secure dir
77 # end
78 #
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
79 def Dir.mktmpdir(prefix_suffix=nil, *rest)
80 path = Tmpname.create(prefix_suffix || "d", *rest) {|n| mkdir(n, 0700)}
81 if block_given?
82 begin
83 yield path
84 ensure
85 FileUtils.remove_entry_secure path
86 end
511dc44 initial import
Laurent Sansonetti authored
87 else
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
88 path
511dc44 initial import
Laurent Sansonetti authored
89 end
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
90 end
91
92 module Tmpname # :nodoc:
93 module_function
94
95 def tmpdir
96 Dir.tmpdir
97 end
98
99 def make_tmpname(prefix_suffix, n)
100 case prefix_suffix
101 when String
102 prefix = prefix_suffix
103 suffix = ""
104 when Array
105 prefix = prefix_suffix[0]
106 suffix = prefix_suffix[1]
107 else
108 raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}"
109 end
110 t = Time.now.strftime("%Y%m%d")
111 path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
511dc44 initial import
Laurent Sansonetti authored
112 path << "-#{n}" if n
113 path << suffix
114 end
115
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
116 def create(basename, *rest)
117 if opts = Hash.try_convert(rest[-1])
118 opts = opts.dup if rest.pop.equal?(opts)
119 max_try = opts.delete(:max_try)
120 opts = [opts]
121 else
122 opts = []
123 end
124 tmpdir, = *rest
125 if $SAFE > 0 and tmpdir.tainted?
126 tmpdir = '/tmp'
127 else
128 tmpdir ||= tmpdir()
129 end
130 n = nil
511dc44 initial import
Laurent Sansonetti authored
131 begin
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
132 path = File.expand_path(make_tmpname(basename, n), tmpdir)
133 yield(path, n, *opts)
134 rescue Errno::EEXIST
135 n ||= 0
136 n += 1
137 retry if !max_try or n < max_try
138 raise "cannot generate temporary name using `#{basename}' under `#{tmpdir}'"
511dc44 initial import
Laurent Sansonetti authored
139 end
140 path
141 end
142 end
143 end
Something went wrong with that request. Please try again.