Box adding is done in a helper that ensures temporary files are cleaned
This commit is contained in:
parent
f203c29fbb
commit
998122e076
|
@ -96,8 +96,7 @@ module Vagrant
|
|||
|
||||
# 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)
|
||||
temp_dir = Pathname.new(Dir.mktmpdir(TEMP_PREFIX))
|
||||
|
||||
with_temp_dir do |temp_dir|
|
||||
# Extract the box into a temporary directory.
|
||||
@logger.debug("Unpacking box into temporary directory: #{temp_dir}")
|
||||
result = Util::Subprocess.execute(
|
||||
|
@ -110,9 +109,13 @@ module Vagrant
|
|||
temp_dir = v1_upgrade(temp_dir)
|
||||
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
|
||||
# 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
|
||||
# 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
|
||||
# an issue where on Windows cross-device directory copies are
|
||||
# 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)
|
||||
@logger.debug("Moving: #{f} => #{destination}")
|
||||
FileUtils.mv(f, destination)
|
||||
end
|
||||
|
||||
# Remove the temporary directory
|
||||
temp_dir.rmtree
|
||||
end
|
||||
end
|
||||
|
||||
# Return the box
|
||||
find(name, provider)
|
||||
|
@ -324,5 +326,19 @@ module Vagrant
|
|||
# Return the temporary directory
|
||||
temp_dir
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue