From 3d2390fc947fdc7987ebf14723a33c35fe98d1e9 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Sat, 28 May 2016 23:11:14 -0400 Subject: [PATCH] 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. --- lib/vagrant/bundler.rb | 9 ++++----- lib/vagrant/util/platform.rb | 2 +- plugins/hosts/darwin/cap/provider_install_virtualbox.rb | 9 ++++----- plugins/hosts/windows/cap/provider_install_virtualbox.rb | 9 ++++----- .../providers/virtualbox/action/package_setup_folders.rb | 2 +- 5 files changed, 14 insertions(+), 17 deletions(-) diff --git a/lib/vagrant/bundler.rb b/lib/vagrant/bundler.rb index 853807ebe..13fe174d1 100644 --- a/lib/vagrant/bundler.rb +++ b/lib/vagrant/bundler.rb @@ -56,7 +56,7 @@ module Vagrant # Setup the "local" Bundler configuration. We need to set BUNDLE_PATH # 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| f.write("BUNDLE_PATH: \"#{bundle_path}\"") end @@ -254,14 +254,13 @@ module Vagrant def with_isolated_gem raise Errors::BundlerDisabled if !@enabled - tmp_gemfile = tempfile("vagrant-gemfile") - tmp_gemfile.close + tmp_gemfile = Dir::Tmpname.create("vagrant-gemfile") {} # Remove bundler settings so that Bundler isn't loaded when building # native extensions because it causes all sorts of problems. old_rubyopt = ENV["RUBYOPT"] old_gemfile = ENV["BUNDLE_GEMFILE"] - ENV["BUNDLE_GEMFILE"] = tmp_gemfile.path + ENV["BUNDLE_GEMFILE"] = tmp_gemfile ENV["RUBYOPT"] = (ENV["RUBYOPT"] || "").gsub(/-rbundler\/setup\s*/, "") # Set the GEM_HOME so gems are installed only to our local gem dir @@ -291,7 +290,7 @@ module Vagrant return yield end ensure - tmp_gemfile.unlink rescue nil + File.unlink(tmp_gemfile) if File.file?(tmp_gemfile) ENV["BUNDLE_GEMFILE"] = old_gemfile ENV["GEM_HOME"] = @gem_home diff --git a/lib/vagrant/util/platform.rb b/lib/vagrant/util/platform.rb index 0aba82150..e210e0f3b 100644 --- a/lib/vagrant/util/platform.rb +++ b/lib/vagrant/util/platform.rb @@ -125,7 +125,7 @@ module Vagrant # directory runs a different filesystem than the root directory. # However, this works in many cases. 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") File.open(tmp_file, "w") do |f| f.write("foo") diff --git a/plugins/hosts/darwin/cap/provider_install_virtualbox.rb b/plugins/hosts/darwin/cap/provider_install_virtualbox.rb index 3eba42ac1..88a7eefa5 100644 --- a/plugins/hosts/darwin/cap/provider_install_virtualbox.rb +++ b/plugins/hosts/darwin/cap/provider_install_virtualbox.rb @@ -16,8 +16,7 @@ module VagrantPlugins SHA256SUM = "62f933115498e51ddf5f2dab47dc1eebb42eb78ea1a7665cb91c53edacc847c6".freeze def self.provider_install_virtualbox(env) - tf = Tempfile.new("vagrant") - tf.close + path = Dir::Tmpname.create("vagrant-provider-install-virtualbox") {} # Prefixed UI for prettiness ui = Vagrant::UI::Prefixed.new(env.ui, "") @@ -28,11 +27,11 @@ module VagrantPlugins version: VERSION)) ui.detail(I18n.t( "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! # Validate that the file checksum matches - actual = FileChecksum.new(tf.path, Digest::SHA2).checksum + actual = FileChecksum.new(path, Digest::SHA2).checksum if actual != SHA256SUM raise Vagrant::Errors::ProviderChecksumMismatch, provider: "virtualbox", @@ -46,7 +45,7 @@ module VagrantPlugins ui.detail(I18n.t( "vagrant.hosts.darwin.virtualbox_install_install_detail")) 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 raise Vagrant::Errors::ProviderInstallFailed, provider: "virtualbox", diff --git a/plugins/hosts/windows/cap/provider_install_virtualbox.rb b/plugins/hosts/windows/cap/provider_install_virtualbox.rb index 4611990e4..0aaf649e9 100644 --- a/plugins/hosts/windows/cap/provider_install_virtualbox.rb +++ b/plugins/hosts/windows/cap/provider_install_virtualbox.rb @@ -17,8 +17,7 @@ module VagrantPlugins SHA256SUM = "3e5ed8fe4ada6eef8dfb4fe6fd79fcab4b242acf799f7d3ab4a17b43838b1e04".freeze def self.provider_install_virtualbox(env) - tf = Tempfile.new("vagrant") - tf.close + path = Dir::Tmpname.create("vagrant-provider-install-virtualbox") {} # Prefixed UI for prettiness ui = Vagrant::UI::Prefixed.new(env.ui, "") @@ -29,11 +28,11 @@ module VagrantPlugins version: VERSION)) ui.detail(I18n.t( "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! # Validate that the file checksum matches - actual = FileChecksum.new(tf.path, Digest::SHA2).checksum + actual = FileChecksum.new(path, Digest::SHA2).checksum if actual != SHA256SUM raise Vagrant::Errors::ProviderChecksumMismatch, provider: "virtualbox", @@ -47,7 +46,7 @@ module VagrantPlugins ui.detail(I18n.t( "vagrant.hosts.windows.virtualbox_install_install_detail")) 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 raise Vagrant::Errors::ProviderInstallFailed, provider: "virtualbox", diff --git a/plugins/providers/virtualbox/action/package_setup_folders.rb b/plugins/providers/virtualbox/action/package_setup_folders.rb index a045122b7..26a6503a5 100644 --- a/plugins/providers/virtualbox/action/package_setup_folders.rb +++ b/plugins/providers/virtualbox/action/package_setup_folders.rb @@ -14,7 +14,7 @@ module VagrantPlugins def call(env) 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 # Vagrant will do the right thing.