| 1 | From: Antonio Terceiro <[email protected]>
|
| 2 | Date: Wed, 22 Oct 2014 09:40:14 -0200
|
| 3 | Subject: Use a private temporary directory that is cleanup up on exit
|
| 4 |
|
| 5 | This avoids vagrant from cluttering $TMPDIR with dozens of even hundreds
|
| 6 | of temporary files (~4 per vagrant invocation).
|
| 7 | ---
|
| 8 | lib/vagrant/box.rb | 3 ++-
|
| 9 | lib/vagrant/util.rb | 1 +
|
| 10 | lib/vagrant/util/caps.rb | 2 +-
|
| 11 | lib/vagrant/util/platform.rb | 2 +-
|
| 12 | lib/vagrant/util/tempfile.rb | 39 +++++++++++++++++++++++++++++++++++++++
|
| 13 | 5 files changed, 44 insertions(+), 3 deletions(-)
|
| 14 | create mode 100644 lib/vagrant/util/tempfile.rb
|
| 15 |
|
| 16 | diff --git a/lib/vagrant/box.rb b/lib/vagrant/box.rb
|
| 17 | index 90dc69d..4ee79b9 100644
|
| 18 | |
| 19 | |
| 20 | @@ -12,6 +12,7 @@ require "vagrant/util/downloader"
|
| 21 | require "vagrant/util/platform"
|
| 22 | require "vagrant/util/safe_chdir"
|
| 23 | require "vagrant/util/subprocess"
|
| 24 | +# require "vagrant/util/tempfile"
|
| 25 |
|
| 26 | module Vagrant
|
| 27 | # Represents a "box," which is a package Vagrant environment that is used
|
| 28 | @@ -153,7 +154,7 @@ module Vagrant
|
| 29 | # @param [Hash] download_options Options to pass to the downloader.
|
| 30 | # @return [BoxMetadata]
|
| 31 | def load_metadata(download_options={})
|
| 32 | - tf = Tempfile.new("vagrant-load-metadata")
|
| 33 | + tf = Util::Tempfile.new("vagrant-load-metadata")
|
| 34 | tf.close
|
| 35 |
|
| 36 | url = @metadata_url
|
| 37 | diff --git a/lib/vagrant/util.rb b/lib/vagrant/util.rb
|
| 38 | index 4b3e0ff..36eb671 100644
|
| 39 | |
| 40 | |
| 41 | @@ -57,6 +57,7 @@ module Vagrant
|
| 42 | autoload :SilenceWarnings, 'vagrant/util/silence_warnings'
|
| 43 | autoload :SSH, 'vagrant/util/ssh'
|
| 44 | autoload :StackedProcRunner, 'vagrant/util/stacked_proc_runner'
|
| 45 | + autoload :Tempfile, 'vagrant/util/tempfile'
|
| 46 | autoload :StringBlockEditor, 'vagrant/util/string_block_editor'
|
| 47 | autoload :Subprocess, 'vagrant/util/subprocess'
|
| 48 | autoload :TemplateRenderer, 'vagrant/util/template_renderer'
|
| 49 | diff --git a/lib/vagrant/util/caps.rb b/lib/vagrant/util/caps.rb
|
| 50 | index 310add3..55afc49 100644
|
| 51 | |
| 52 | |
| 53 | @@ -31,7 +31,7 @@ module Vagrant
|
| 54 |
|
| 55 | def ensure_output_iso(file_destination)
|
| 56 | if file_destination.nil?
|
| 57 | - tmpfile = Tempfile.new(["vagrant", ".iso"])
|
| 58 | + tmpfile = Util::Tempfile.new(["vagrant", ".iso"])
|
| 59 | file_destination = Pathname.new(tmpfile.path)
|
| 60 | tmpfile.close
|
| 61 | tmpfile.unlink
|
| 62 | diff --git a/lib/vagrant/util/platform.rb b/lib/vagrant/util/platform.rb
|
| 63 | index c8658e1..0421c70 100644
|
| 64 | |
| 65 | |
| 66 | @@ -388,7 +388,7 @@ module Vagrant
|
| 67 |
|
| 68 | if wsl?
|
| 69 | # Mark our filesystem with a temporary file having an unique name.
|
| 70 | - marker = Tempfile.new(Time.now.to_i.to_s)
|
| 71 | + marker = Util::Tempfile.new(Time.now.to_i.to_s)
|
| 72 | logger = Log4r::Logger.new("vagrant::util::platform::wsl")
|
| 73 |
|
| 74 | # Check for lxrun installation first
|
| 75 | diff --git a/lib/vagrant/util/tempfile.rb b/lib/vagrant/util/tempfile.rb
|
| 76 | new file mode 100644
|
| 77 | index 0000000..0cbbb53
|
| 78 | |
| 79 | |
| 80 | @@ -0,0 +1,39 @@
|
| 81 | +require 'fileutils'
|
| 82 | +require 'tmpdir'
|
| 83 | +
|
| 84 | +module Vagrant
|
| 85 | + module Util
|
| 86 | + class Tempfile < ::Tempfile
|
| 87 | +
|
| 88 | + def initialize(basename)
|
| 89 | + super(basename, private_tmpdir)
|
| 90 | + end
|
| 91 | +
|
| 92 | + def private_tmpdir
|
| 93 | + self.class.private_tmpdir
|
| 94 | + end
|
| 95 | +
|
| 96 | + def self.private_tmpdir
|
| 97 | + @private_tmpdir ||=
|
| 98 | + begin
|
| 99 | + user = Etc.getpwuid.name
|
| 100 | + pid = Process.pid
|
| 101 | + tmpdir = File.join(Dir.tmpdir, "vagrant-#{user}-#{pid}")
|
| 102 | + FileUtils.mkdir_p(tmpdir)
|
| 103 | + FileUtils.chmod(0700, tmpdir)
|
| 104 | + tmpdir
|
| 105 | + end
|
| 106 | + end
|
| 107 | +
|
| 108 | + def self.mktmpdir(prefix_suffix)
|
| 109 | + Dir.mktmpdir(prefix_suffix, private_tmpdir)
|
| 110 | + end
|
| 111 | +
|
| 112 | +
|
| 113 | + end
|
| 114 | + end
|
| 115 | +end
|
| 116 | +
|
| 117 | +at_exit do
|
| 118 | + FileUtils.rm_rf(Vagrant::Util::Tempfile.private_tmpdir)
|
| 119 | +end
|