File: tempdir.rb

package info (click to toggle)
vagrant 1.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,084 kB
  • sloc: ruby: 15,045; python: 230; sh: 66; makefile: 2; lisp: 1
file content (34 lines) | stat: -rw-r--r-- 697 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
require 'fileutils'
require 'tempfile'

# This class provides an easy way of creating a temporary
# directory and having it removed when the application exits.
#
# TODO: This class doesn't currently delete the temporary
# directory on exit.
class Tempdir
  attr_reader :path

  def initialize(basename="vagrant")
    @path = nil

    # Loop and attempt to create a temporary directory until
    # it succeeds.
    while @path.nil?
      file = Tempfile.new(basename)
      @path = file.path
      file.unlink

      begin
        Dir.mkdir(@path)
      rescue
        @path = nil
      end
    end
  end

  # This deletes the temporary directory.
  def unlink
    FileUtils.rm_rf(@path)
  end
end