From 8f42dbff2161516eab59749505c00d8272241095 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Tue, 8 Oct 2019 16:40:34 -0700 Subject: [PATCH] Add a resolve_host_path capability for bsd and darwin hosts For bsd the resolve_host_path capability is a no-op. For darwin hosts, if firmlinks are defined on the system, paths will be properly resolved to real path. --- plugins/hosts/bsd/cap/path.rb | 11 ++++++ plugins/hosts/bsd/plugin.rb | 5 +++ plugins/hosts/darwin/cap/path.rb | 58 ++++++++++++++++++++++++++++++++ plugins/hosts/darwin/plugin.rb | 5 +++ 4 files changed, 79 insertions(+) create mode 100644 plugins/hosts/bsd/cap/path.rb create mode 100644 plugins/hosts/darwin/cap/path.rb diff --git a/plugins/hosts/bsd/cap/path.rb b/plugins/hosts/bsd/cap/path.rb new file mode 100644 index 000000000..25ca3cd15 --- /dev/null +++ b/plugins/hosts/bsd/cap/path.rb @@ -0,0 +1,11 @@ +module VagrantPlugins + module HostBSD + module Cap + class Path + def self.resolve_host_path(env, path) + path + end + end + end + end +end diff --git a/plugins/hosts/bsd/plugin.rb b/plugins/hosts/bsd/plugin.rb index ce79ede31..107157675 100644 --- a/plugins/hosts/bsd/plugin.rb +++ b/plugins/hosts/bsd/plugin.rb @@ -36,6 +36,11 @@ module VagrantPlugins Cap::NFS end + host_capability("bsd", "resolve_host_path") do + require_relative "cap/path" + Cap::Path + end + host_capability("bsd", "set_ssh_key_permissions") do require_relative "cap/ssh" Cap::SSH diff --git a/plugins/hosts/darwin/cap/path.rb b/plugins/hosts/darwin/cap/path.rb new file mode 100644 index 000000000..c39d63319 --- /dev/null +++ b/plugins/hosts/darwin/cap/path.rb @@ -0,0 +1,58 @@ +module VagrantPlugins + module HostDarwin + module Cap + class Path + @@logger = Log4r::Logger.new("vagrant::host::darwin::path") + + FIRMLINK_DEFS = "/usr/share/firmlinks".freeze + FIRMLINK_DATA_PATH = "/System/Volumes/Data".freeze + + # Resolve the given host path to the actual + # usable system path by detecting firmlinks + # if available on the current system + # + # @param [String] path Host system path + # @return [String] resolved path + def self.resolve_host_path(env, path) + path = File.expand_path(path) + firmlink = firmlink_map.detect do |mount_path, data_path| + path.start_with?(mount_path) + end + return path if firmlink.nil? + current_prefix, new_suffix = firmlink + new_prefix = File.join(FIRMLINK_DATA_PATH, new_suffix) + new_path = path.sub(current_prefix, new_prefix) + @@logger.debug("Resolved given path `#{path}` to `#{new_path}`") + new_path + end + + # Generate mapping of firmlinks if available on the host + # + # @return [Hash] + def self.firmlink_map + if !@firmlink_map + return @firmlink_map = {} if !File.exist?(FIRMLINK_DEFS) + begin + @firmlink_map = Hash[ + File.readlines(FIRMLINK_DEFS).map { |d| + d.strip.split(/\s+/, 2) + } + ] + rescue => err + @@logger.warn("Failed to parse firmlink definitions: #{err}") + @firmlink_map = {} + end + end + @firmlink_map + end + + # @private + # Reset the cached values for capability. This is not considered a public + # API and should only be used for testing. + def self.reset! + instance_variables.each(&method(:remove_instance_variable)) + end + end + end + end +end diff --git a/plugins/hosts/darwin/plugin.rb b/plugins/hosts/darwin/plugin.rb index 7b08af3f1..b6c535c9b 100644 --- a/plugins/hosts/darwin/plugin.rb +++ b/plugins/hosts/darwin/plugin.rb @@ -16,6 +16,11 @@ module VagrantPlugins Cap::ProviderInstallVirtualBox end + host_capability("darwin", "resolve_host_path") do + require_relative "cap/path" + Cap::Path + end + host_capability("darwin", "rdp_client") do require_relative "cap/rdp" Cap::RDP