class Vagrant::Util::SafeChdir

Public Class Methods

safe_chdir(dir) { || ... } click to toggle source

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.

@param [String] dir Dir to change to temporarily

# 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