Do not create a tempfile when downloading box metadata

The only reason we were using Tempfile was to generate the path. This
commit switches to using `Dir::Tmpname.create`, which accomplishes the
same thing without the overhead of creating and removing a tempfile.
This commit is contained in:
Seth Vargo 2016-05-28 23:05:37 -04:00
parent fb60d34236
commit fb7c4033a9
No known key found for this signature in database
GPG Key ID: 905A90C2949E8787
1 changed files with 4 additions and 5 deletions

View File

@ -118,8 +118,7 @@ module Vagrant
# @param [Hash] download_options Options to pass to the downloader. # @param [Hash] download_options Options to pass to the downloader.
# @return [BoxMetadata] # @return [BoxMetadata]
def load_metadata(**download_options) def load_metadata(**download_options)
tf = Tempfile.new("vagrant") path = Dir::Tmpname.create("vagrant-load-metadata") {}
tf.close
url = @metadata_url url = @metadata_url
if File.file?(url) || url !~ /^[a-z0-9]+:.*$/i if File.file?(url) || url !~ /^[a-z0-9]+:.*$/i
@ -129,13 +128,13 @@ module Vagrant
end end
opts = { headers: ["Accept: application/json"] }.merge(download_options) opts = { headers: ["Accept: application/json"] }.merge(download_options)
Util::Downloader.new(url, tf.path, **opts).download! Util::Downloader.new(url, path, **opts).download!
BoxMetadata.new(File.open(tf.path, "r")) BoxMetadata.new(File.open(path, "r"))
rescue Errors::DownloaderError => e rescue Errors::DownloaderError => e
raise Errors::BoxMetadataDownloadError, raise Errors::BoxMetadataDownloadError,
message: e.extra_data[:message] message: e.extra_data[:message]
ensure ensure
tf.unlink if tf File.unlink(path) if File.file?(path)
end end
# Checks if the box has an update and returns the metadata, version, # Checks if the box has an update and returns the metadata, version,