Sha256: 56120dd7c1805329407eb133954ee873b6bc9be424e2b8f5d3fff2dab28c8b36

Contents?: true

Size: 1.56 KB

Versions: 13

Compression:

Stored size: 1.56 KB

Contents

require "fileutils"

module VagrantPlugins
  module ProviderVirtualBox
    module Action
      # Cleans up the VirtualBox machine folder for any ".xml-prev"
      # files which VirtualBox may have left over. This is a bug in
      # VirtualBox. As soon as this is fixed, this middleware can and
      # will be removed.
      class CleanMachineFolder
        def initialize(app, env)
          @app = app
        end

        def call(env)
          machine_folder = env[:machine].provider.driver.read_machine_folder

          begin
            clean_machine_folder(machine_folder)
          rescue Errno::EPERM
            raise Vagrant::Errors::MachineFolderNotAccessible,
              name: env[:machine].name,
              path: machine_folder
          end

          @app.call(env)
        end

        def clean_machine_folder(machine_folder)
          folder = File.join(machine_folder, "*")

          # Small safeguard against potentially unwanted rm-rf, since the default
          # machine folder will typically always be greater than 10 characters long.
          # For users with it < 10, out of luck?
          return if folder.length < 10

          Dir[folder].each do |f|
            next unless File.directory?(f)

            keep = Dir["#{f}/**/*"].find do |d|
              # Find a file that doesn't have ".xml-prev" as the suffix,
              # which signals that we want to keep this folder
              File.file?(d) && !(File.basename(d) =~ /\.vbox-prev$/)
            end

            FileUtils.rm_rf(f) if !keep
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
vagrant-unbundled-2.3.6.0 plugins/providers/virtualbox/action/clean_machine_folder.rb
tamtam-vagrant-reload-1.2.1 vendor/cache/vagrant-2092df529ae7/plugins/providers/virtualbox/action/clean_machine_folder.rb
vagrant-unbundled-2.3.3.0 plugins/providers/virtualbox/action/clean_machine_folder.rb
vagrant-unbundled-2.3.2.0 plugins/providers/virtualbox/action/clean_machine_folder.rb
vagrant-unbundled-2.2.19.0 plugins/providers/virtualbox/action/clean_machine_folder.rb
vagrant-unbundled-2.2.18.0 plugins/providers/virtualbox/action/clean_machine_folder.rb
vagrant-unbundled-2.2.16.0 plugins/providers/virtualbox/action/clean_machine_folder.rb
vagrant-unbundled-2.2.14.0 plugins/providers/virtualbox/action/clean_machine_folder.rb
vagrant-aws-mkubenka-0.7.2.pre.24 vendor/bundle/ruby/2.7.0/bundler/gems/vagrant-22795b161bf6/plugins/providers/virtualbox/action/clean_machine_folder.rb
vagrant-unbundled-2.2.10.0 plugins/providers/virtualbox/action/clean_machine_folder.rb
vagrant-unbundled-2.2.9.0 plugins/providers/virtualbox/action/clean_machine_folder.rb
vagrant-unbundled-2.2.8.0 plugins/providers/virtualbox/action/clean_machine_folder.rb
vagrant-unbundled-2.2.7.0 plugins/providers/virtualbox/action/clean_machine_folder.rb