Box adding is done in a helper that ensures temporary files are cleaned

This commit is contained in:
Mitchell Hashimoto 2013-04-07 14:43:52 -07:00
parent f203c29fbb
commit 998122e076
1 changed files with 74 additions and 58 deletions

View File

@ -96,8 +96,7 @@ module Vagrant
# Create a temporary directory since we're not sure at this point if # Create a temporary directory since we're not sure at this point if
# the box we're unpackaging already exists (if no provider was given) # the box we're unpackaging already exists (if no provider was given)
temp_dir = Pathname.new(Dir.mktmpdir(TEMP_PREFIX)) with_temp_dir do |temp_dir|
# Extract the box into a temporary directory. # Extract the box into a temporary directory.
@logger.debug("Unpacking box into temporary directory: #{temp_dir}") @logger.debug("Unpacking box into temporary directory: #{temp_dir}")
result = Util::Subprocess.execute( result = Util::Subprocess.execute(
@ -110,9 +109,13 @@ module Vagrant
temp_dir = v1_upgrade(temp_dir) temp_dir = v1_upgrade(temp_dir)
end end
# We re-wrap ourselves in the safety net in case we upgraded.
# If we didn't upgrade, then this is still safe because the
# helper will only delete the directory if it exists
with_temp_dir(temp_dir) do |final_temp_dir|
# Get an instance of the box we just added before it is finalized # Get an instance of the box we just added before it is finalized
# in the system so we can inspect and use its metadata. # in the system so we can inspect and use its metadata.
box = Box.new(name, provider, temp_dir) box = Box.new(name, provider, final_temp_dir)
# Get the provider, since we'll need that to at the least add it # Get the provider, since we'll need that to at the least add it
# to the system or check that it matches what is given to us. # to the system or check that it matches what is given to us.
@ -150,14 +153,13 @@ module Vagrant
# Go through each child and copy them one-by-one. This avoids # Go through each child and copy them one-by-one. This avoids
# an issue where on Windows cross-device directory copies are # an issue where on Windows cross-device directory copies are
# failing for some reason. [GH-1424] # failing for some reason. [GH-1424]
temp_dir.children(true).each do |f| final_temp_dir.children(true).each do |f|
destination = final_dir.join(f.basename) destination = final_dir.join(f.basename)
@logger.debug("Moving: #{f} => #{destination}") @logger.debug("Moving: #{f} => #{destination}")
FileUtils.mv(f, destination) FileUtils.mv(f, destination)
end end
end
# Remove the temporary directory end
temp_dir.rmtree
# Return the box # Return the box
find(name, provider) find(name, provider)
@ -324,5 +326,19 @@ module Vagrant
# Return the temporary directory # Return the temporary directory
temp_dir temp_dir
end end
# This is a helper that makes sure that our temporary directories
# are cleaned up no matter what.
#
# @param [String] dir Path to a temporary directory
# @return [Object] The result of whatever the yield is
def with_temp_dir(dir=nil)
dir ||= Dir.mktmpdir("vagrant-")
dir = Pathname.new(dir)
yield dir
ensure
dir.rmtree if dir.exist?
end
end end
end end