Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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