Give a unique, prefixed name to all tempfiles

This commit basically grepped the code base for all uses of Dir.mktmpdir
and Tempfile.new/open and ensures the value is unique within the
code base and also prefixed with `vagrant-`.

Previously, most invocations of these commands simply used "vagrant",
thus making them indistinguishable when trying to identify leaks.
This commit is contained in:
Seth Vargo 2016-05-28 23:11:14 -04:00
parent fb7c4033a9
commit 3d2390fc94
No known key found for this signature in database
GPG Key ID: 905A90C2949E8787
5 changed files with 14 additions and 17 deletions

View File

@ -56,7 +56,7 @@ module Vagrant
# Setup the "local" Bundler configuration. We need to set BUNDLE_PATH # Setup the "local" Bundler configuration. We need to set BUNDLE_PATH
# because the existence of this actually suppresses `sudo`. # because the existence of this actually suppresses `sudo`.
@appconfigpath = Dir.mktmpdir @appconfigpath = Dir.mktmpdir("vagrant-bundle-app-config")
File.open(File.join(@appconfigpath, "config"), "w+") do |f| File.open(File.join(@appconfigpath, "config"), "w+") do |f|
f.write("BUNDLE_PATH: \"#{bundle_path}\"") f.write("BUNDLE_PATH: \"#{bundle_path}\"")
end end
@ -254,14 +254,13 @@ module Vagrant
def with_isolated_gem def with_isolated_gem
raise Errors::BundlerDisabled if !@enabled raise Errors::BundlerDisabled if !@enabled
tmp_gemfile = tempfile("vagrant-gemfile") tmp_gemfile = Dir::Tmpname.create("vagrant-gemfile") {}
tmp_gemfile.close
# Remove bundler settings so that Bundler isn't loaded when building # Remove bundler settings so that Bundler isn't loaded when building
# native extensions because it causes all sorts of problems. # native extensions because it causes all sorts of problems.
old_rubyopt = ENV["RUBYOPT"] old_rubyopt = ENV["RUBYOPT"]
old_gemfile = ENV["BUNDLE_GEMFILE"] old_gemfile = ENV["BUNDLE_GEMFILE"]
ENV["BUNDLE_GEMFILE"] = tmp_gemfile.path ENV["BUNDLE_GEMFILE"] = tmp_gemfile
ENV["RUBYOPT"] = (ENV["RUBYOPT"] || "").gsub(/-rbundler\/setup\s*/, "") ENV["RUBYOPT"] = (ENV["RUBYOPT"] || "").gsub(/-rbundler\/setup\s*/, "")
# Set the GEM_HOME so gems are installed only to our local gem dir # Set the GEM_HOME so gems are installed only to our local gem dir
@ -291,7 +290,7 @@ module Vagrant
return yield return yield
end end
ensure ensure
tmp_gemfile.unlink rescue nil File.unlink(tmp_gemfile) if File.file?(tmp_gemfile)
ENV["BUNDLE_GEMFILE"] = old_gemfile ENV["BUNDLE_GEMFILE"] = old_gemfile
ENV["GEM_HOME"] = @gem_home ENV["GEM_HOME"] = @gem_home

View File

@ -125,7 +125,7 @@ module Vagrant
# directory runs a different filesystem than the root directory. # directory runs a different filesystem than the root directory.
# However, this works in many cases. # However, this works in many cases.
def fs_case_sensitive? def fs_case_sensitive?
Dir.mktmpdir("vagrant") do |tmp_dir| Dir.mktmpdir("vagrant-fs-case-sensitive") do |tmp_dir|
tmp_file = File.join(tmp_dir, "FILE") tmp_file = File.join(tmp_dir, "FILE")
File.open(tmp_file, "w") do |f| File.open(tmp_file, "w") do |f|
f.write("foo") f.write("foo")

View File

@ -16,8 +16,7 @@ module VagrantPlugins
SHA256SUM = "62f933115498e51ddf5f2dab47dc1eebb42eb78ea1a7665cb91c53edacc847c6".freeze SHA256SUM = "62f933115498e51ddf5f2dab47dc1eebb42eb78ea1a7665cb91c53edacc847c6".freeze
def self.provider_install_virtualbox(env) def self.provider_install_virtualbox(env)
tf = Tempfile.new("vagrant") path = Dir::Tmpname.create("vagrant-provider-install-virtualbox") {}
tf.close
# Prefixed UI for prettiness # Prefixed UI for prettiness
ui = Vagrant::UI::Prefixed.new(env.ui, "") ui = Vagrant::UI::Prefixed.new(env.ui, "")
@ -28,11 +27,11 @@ module VagrantPlugins
version: VERSION)) version: VERSION))
ui.detail(I18n.t( ui.detail(I18n.t(
"vagrant.hosts.darwin.virtualbox_install_detail")) "vagrant.hosts.darwin.virtualbox_install_detail"))
dl = Vagrant::Util::Downloader.new(URL, tf.path, ui: ui) dl = Vagrant::Util::Downloader.new(URL, path, ui: ui)
dl.download! dl.download!
# Validate that the file checksum matches # Validate that the file checksum matches
actual = FileChecksum.new(tf.path, Digest::SHA2).checksum actual = FileChecksum.new(path, Digest::SHA2).checksum
if actual != SHA256SUM if actual != SHA256SUM
raise Vagrant::Errors::ProviderChecksumMismatch, raise Vagrant::Errors::ProviderChecksumMismatch,
provider: "virtualbox", provider: "virtualbox",
@ -46,7 +45,7 @@ module VagrantPlugins
ui.detail(I18n.t( ui.detail(I18n.t(
"vagrant.hosts.darwin.virtualbox_install_install_detail")) "vagrant.hosts.darwin.virtualbox_install_install_detail"))
script = File.expand_path("../../scripts/install_virtualbox.sh", __FILE__) script = File.expand_path("../../scripts/install_virtualbox.sh", __FILE__)
result = Vagrant::Util::Subprocess.execute("bash", script, tf.path) result = Vagrant::Util::Subprocess.execute("bash", script, path)
if result.exit_code != 0 if result.exit_code != 0
raise Vagrant::Errors::ProviderInstallFailed, raise Vagrant::Errors::ProviderInstallFailed,
provider: "virtualbox", provider: "virtualbox",

View File

@ -17,8 +17,7 @@ module VagrantPlugins
SHA256SUM = "3e5ed8fe4ada6eef8dfb4fe6fd79fcab4b242acf799f7d3ab4a17b43838b1e04".freeze SHA256SUM = "3e5ed8fe4ada6eef8dfb4fe6fd79fcab4b242acf799f7d3ab4a17b43838b1e04".freeze
def self.provider_install_virtualbox(env) def self.provider_install_virtualbox(env)
tf = Tempfile.new("vagrant") path = Dir::Tmpname.create("vagrant-provider-install-virtualbox") {}
tf.close
# Prefixed UI for prettiness # Prefixed UI for prettiness
ui = Vagrant::UI::Prefixed.new(env.ui, "") ui = Vagrant::UI::Prefixed.new(env.ui, "")
@ -29,11 +28,11 @@ module VagrantPlugins
version: VERSION)) version: VERSION))
ui.detail(I18n.t( ui.detail(I18n.t(
"vagrant.hosts.windows.virtualbox_install_detail")) "vagrant.hosts.windows.virtualbox_install_detail"))
dl = Vagrant::Util::Downloader.new(URL, tf.path, ui: ui) dl = Vagrant::Util::Downloader.new(URL, path, ui: ui)
dl.download! dl.download!
# Validate that the file checksum matches # Validate that the file checksum matches
actual = FileChecksum.new(tf.path, Digest::SHA2).checksum actual = FileChecksum.new(path, Digest::SHA2).checksum
if actual != SHA256SUM if actual != SHA256SUM
raise Vagrant::Errors::ProviderChecksumMismatch, raise Vagrant::Errors::ProviderChecksumMismatch,
provider: "virtualbox", provider: "virtualbox",
@ -47,7 +46,7 @@ module VagrantPlugins
ui.detail(I18n.t( ui.detail(I18n.t(
"vagrant.hosts.windows.virtualbox_install_install_detail")) "vagrant.hosts.windows.virtualbox_install_install_detail"))
script = File.expand_path("../../scripts/install_virtualbox.ps1", __FILE__) script = File.expand_path("../../scripts/install_virtualbox.ps1", __FILE__)
result = Vagrant::Util::PowerShell.execute(script, tf.path) result = Vagrant::Util::PowerShell.execute(script, path)
if result.exit_code != 0 if result.exit_code != 0
raise Vagrant::Errors::ProviderInstallFailed, raise Vagrant::Errors::ProviderInstallFailed,
provider: "virtualbox", provider: "virtualbox",

View File

@ -14,7 +14,7 @@ module VagrantPlugins
def call(env) def call(env)
env["package.output"] ||= "package.box" env["package.output"] ||= "package.box"
env["package.directory"] ||= Dir.mktmpdir("package-", env[:tmp_path]) env["package.directory"] ||= Dir.mktmpdir("vagrant-package-", env[:tmp_path])
# Match up a couple environmental variables so that the other parts of # Match up a couple environmental variables so that the other parts of
# Vagrant will do the right thing. # Vagrant will do the right thing.