Class: Vagrant::Util::SafeChdir

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/util/safe_chdir.rb

Constant Summary collapse

@@chdir_lock =
Mutex.new

Class Method Summary collapse

Class Method Details

.safe_chdir(dir) ⇒ Object

Safely changes directory of this process by putting a lock around it so that it is thread safe. This will yield a block and when the block exits it changes back to the original directory.

Parameters:

  • dir (String)

    Dir to change to temporarily


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/vagrant/util/safe_chdir.rb', line 13

def self.safe_chdir(dir)
  lock = @@chdir_lock

  begin
    @@chdir_lock.synchronize {}
  rescue ThreadError
    # If we already hold the lock, just create a new lock so we
    # definitely don't block and don't get an error.
    lock = Mutex.new
  end

  lock.synchronize do
    Dir.chdir(dir) do
      return yield
    end
  end
end