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