~ubuntu-branches/ubuntu/intrepid/ruby1.9/intrepid-updates

« back to all changes in this revision

Viewing changes to lib/tmpdir.rb

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-09-04 16:01:17 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20070904160117-i15zckg2nhxe9fyw
Tags: 1.9.0+20070830-2ubuntu1
* Sync from Debian; remaining changes:
  - Add -g to CFLAGS.
* Fixes build failure on ia64.
* Fixes build failure with gcc-4.2 on lpia.
* Robustify check for target_os, fixing build failure on lpia.
* Set Ubuntu maintainer address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#
2
2
# tmpdir - retrieve temporary directory path
3
3
#
4
 
# $Id: tmpdir.rb 11708 2007-02-12 23:01:19Z shyouhei $
 
4
# $Id: tmpdir.rb 13129 2007-08-21 12:15:34Z akr $
5
5
#
6
6
 
 
7
require 'fileutils'
 
8
 
7
9
class Dir
8
10
 
9
11
  @@systmpdir = '/tmp'
42
44
    end
43
45
    File.expand_path(tmp)
44
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| path 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
45
130
end