Skip to content
This repository
Newer
Older
100644 130 lines (122 sloc) 3.476 kb
511dc44a » Laurent Sansonetti
2008-02-25 initial import
1 #
2 # tmpdir - retrieve temporary directory path
3 #
4 # $Id: tmpdir.rb 13762 2007-10-24 06:03:48Z akr $
5 #
6
7 require 'fileutils'
8
9 class Dir
10
11 @@systmpdir = '/tmp'
12
13 begin
14 require 'Win32API'
15 max_pathlen = 260
16 windir = ' '*(max_pathlen+1)
17 begin
18 getdir = Win32API.new('kernel32', 'GetSystemWindowsDirectory', 'PL', 'L')
19 rescue RuntimeError
20 getdir = Win32API.new('kernel32', 'GetWindowsDirectory', 'PL', 'L')
21 end
22 len = getdir.call(windir, windir.size)
23 windir = File.expand_path(windir[0, len])
24 temp = File.join(windir, 'temp')
25 @@systmpdir = temp if File.directory?(temp) and File.writable?(temp)
26 rescue LoadError
27 end
28
29 ##
30 # Returns the operating system's temporary file path.
31
32 def Dir::tmpdir
33 tmp = '.'
34 if $SAFE > 0
35 tmp = @@systmpdir
36 else
37 for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'],
38 ENV['USERPROFILE'], @@systmpdir, '/tmp']
39 if dir and File.directory?(dir) and File.writable?(dir)
40 tmp = dir
41 break
42 end
43 end
44 end
45 File.expand_path(tmp)
46 end
47
48 # Dir.mktmpdir creates a temporary directory.
49 #
50 # The directory is created with 0700 permission.
51 #
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.
57 #
58 # Dir.mktmpdir {|dir| dir is ".../d..." }
59 # Dir.mktmpdir("foo") {|dir| dir is ".../foo..." }
60 # Dir.mktmpdir(["foo", "bar"]) {|dir| dir is ".../foo...bar" }
61 #
62 # The directory is created under Dir.tmpdir or
63 # the optional second argument <i>tmpdir</i> if non-nil value is given.
64 #
65 # Dir.mktmpdir {|dir| dir is "#{Dir.tmpdir}/d..." }
66 # Dir.mktmpdir(nil, "/var/tmp") {|dir| dir is "/var/tmp/d..." }
67 #
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.
73 #
74 # Dir.mktmpdir {|dir|
75 # # use the directory...
76 # open("#{dir}/foo", "w") { ... }
77 # }
78 #
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.
82 #
83 # dir = Dir.mktmpdir
84 # begin
85 # # use the directory...
86 # open("#{dir}/foo", "w") { ... }
87 # ensure
88 # # remove the directory.
89 # FileUtils.remove_entry_secure dir
90 # end
91 #
92 def Dir.mktmpdir(prefix_suffix=nil, tmpdir=nil)
93 case prefix_suffix
94 when nil
95 prefix = "d"
96 suffix = ""
97 when String
98 prefix = prefix_suffix
99 suffix = ""
100 when Array
101 prefix = prefix_suffix[0]
102 suffix = prefix_suffix[1]
103 else
104 raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}"
105 end
106 tmpdir ||= Dir.tmpdir
107 t = Time.now.strftime("%Y%m%d")
108 n = nil
109 begin
110 path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
111 path << "-#{n}" if n
112 path << suffix
113 Dir.mkdir(path, 0700)
114 rescue Errno::EEXIST
115 n ||= 0
116 n += 1
117 retry
118 end
119
120 if block_given?
121 begin
122 yield path
123 ensure
124 FileUtils.remove_entry_secure path
125 end
126 else
127 path
128 end
129 end
130 end
Something went wrong with that request. Please try again.