2013-11-22 01:38:17 +00:00
|
|
|
require "vagrant/util/platform"
|
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module ProviderVirtualBox
|
|
|
|
class SyncedFolder < Vagrant.plugin("2", :synced_folder)
|
2014-04-11 01:26:19 +00:00
|
|
|
def usable?(machine, raise_errors=false)
|
2013-11-22 01:38:17 +00:00
|
|
|
# These synced folders only work if the provider if VirtualBox
|
2014-04-11 01:26:19 +00:00
|
|
|
machine.provider_name == :virtualbox &&
|
|
|
|
machine.provider_config.functional_vboxsf
|
2013-11-22 01:38:17 +00:00
|
|
|
end
|
|
|
|
|
2014-04-16 17:12:59 +00:00
|
|
|
def enable(machine, folders, _opts)
|
|
|
|
# Export the shared folders to the VM
|
2013-11-22 01:38:17 +00:00
|
|
|
defs = []
|
|
|
|
folders.each do |id, data|
|
2014-04-16 18:59:08 +00:00
|
|
|
hostpath = data[:hostpath]
|
|
|
|
if !data[:hostpath_exact]
|
|
|
|
hostpath = Vagrant::Util::Platform.cygwin_windows_path(hostpath)
|
|
|
|
end
|
2013-11-22 01:38:17 +00:00
|
|
|
|
|
|
|
defs << {
|
2014-03-14 19:34:36 +00:00
|
|
|
name: os_friendly_id(id),
|
2013-11-23 01:16:21 +00:00
|
|
|
hostpath: hostpath.to_s,
|
2014-04-16 17:12:59 +00:00
|
|
|
transient: true,
|
2013-11-22 01:38:17 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
driver(machine).share_folders(defs)
|
|
|
|
|
|
|
|
# short guestpaths first, so we don't step on ourselves
|
|
|
|
folders = folders.sort_by do |id, data|
|
|
|
|
if data[:guestpath]
|
|
|
|
data[:guestpath].length
|
|
|
|
else
|
|
|
|
# A long enough path to just do this at the end.
|
|
|
|
10000
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Go through each folder and mount
|
2014-01-18 02:21:55 +00:00
|
|
|
machine.ui.output(I18n.t("vagrant.actions.vm.share_folders.mounting"))
|
2013-11-22 01:38:17 +00:00
|
|
|
folders.each do |id, data|
|
|
|
|
if data[:guestpath]
|
|
|
|
# Guest path specified, so mount the folder to specified point
|
2014-01-18 02:21:55 +00:00
|
|
|
machine.ui.detail(I18n.t("vagrant.actions.vm.share_folders.mounting_entry",
|
|
|
|
guestpath: data[:guestpath],
|
|
|
|
hostpath: data[:hostpath]))
|
2013-11-22 01:38:17 +00:00
|
|
|
|
|
|
|
# Dup the data so we can pass it to the guest API
|
|
|
|
data = data.dup
|
|
|
|
|
|
|
|
# Calculate the owner and group
|
|
|
|
ssh_info = machine.ssh_info
|
|
|
|
data[:owner] ||= ssh_info[:username]
|
|
|
|
data[:group] ||= ssh_info[:username]
|
|
|
|
|
|
|
|
# Mount the actual folder
|
|
|
|
machine.guest.capability(
|
2014-03-14 19:34:36 +00:00
|
|
|
:mount_virtualbox_shared_folder,
|
|
|
|
os_friendly_id(id), data[:guestpath], data)
|
2013-11-22 01:38:17 +00:00
|
|
|
else
|
|
|
|
# If no guest path is specified, then automounting is disabled
|
2014-01-18 02:21:55 +00:00
|
|
|
machine.ui.detail(I18n.t("vagrant.actions.vm.share_folders.nomount_entry",
|
|
|
|
:hostpath => data[:hostpath]))
|
2013-11-22 01:38:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-16 17:24:33 +00:00
|
|
|
def disable(machine, folders, _opts)
|
2014-04-16 21:59:23 +00:00
|
|
|
if machine.guest.capability?(:unmount_virtualbox_shared_folder)
|
|
|
|
folders.each do |id, data|
|
|
|
|
machine.guest.capability(
|
|
|
|
:unmount_virtualbox_shared_folder,
|
|
|
|
data[:guestpath], data)
|
|
|
|
end
|
|
|
|
end
|
2014-04-16 17:24:33 +00:00
|
|
|
|
|
|
|
# Remove the shared folders from the VM metadata
|
|
|
|
names = folders.map { |id, _data| os_friendly_id(id) }
|
|
|
|
driver(machine).unshare_folders(names)
|
|
|
|
end
|
|
|
|
|
2013-12-07 00:40:24 +00:00
|
|
|
def cleanup(machine, opts)
|
2013-12-05 21:39:23 +00:00
|
|
|
driver(machine).clear_shared_folders if machine.id && machine.id != ""
|
2013-12-04 11:22:01 +00:00
|
|
|
end
|
|
|
|
|
2013-11-22 01:38:17 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
# This is here so that we can stub it for tests
|
|
|
|
def driver(machine)
|
|
|
|
machine.provider.driver
|
|
|
|
end
|
2014-03-14 19:34:36 +00:00
|
|
|
|
|
|
|
def os_friendly_id(id)
|
|
|
|
id.gsub(/[\/]/,'_').sub(/^_/, '')
|
|
|
|
end
|
2013-11-22 01:38:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|