diff --git a/lib/vagrant-sshfs/cap/host/darwin/sshfs_reverse_mount.rb b/lib/vagrant-sshfs/cap/host/darwin/sshfs_reverse_mount.rb index f58033f..b711138 100644 --- a/lib/vagrant-sshfs/cap/host/darwin/sshfs_reverse_mount.rb +++ b/lib/vagrant-sshfs/cap/host/darwin/sshfs_reverse_mount.rb @@ -1,3 +1,4 @@ +require "etc" require "log4r" require "vagrant/util/retryable" require "tempfile" @@ -88,9 +89,35 @@ def self.sshfs_mount(machine, opts) ssh_opts_append = opts[:ssh_opts_append].to_s # provided by user + # Support for user provided mount_options, owner, group + # https://github.com/hashicorp/vagrant/blob/2c3397c46851ef29a3589bf3214a3eee12da8484/website/content/docs/synced-folders/basic_usage.mdx#options + mount_options = opts.fetch(:mount_options, []) + if (opts.has_key?(:owner) and opts[:owner]) or + (opts.has_key?(:group) and opts[:group]) + # Identify the uid + if opts.has_key?(:owner) and opts[:owner] + mount_uid = Etc::getpwnam(opts[:owner]).uid + else + mount_uid = Etc::getpwnam(Etc.getlogin).uid + end + # Identify the gid. If a group was provided use that otherwise use + # the group detected with the detected user id. + if opts.has_key?(:group) and opts[:group] + mount_gid = Etc::getgrnam(opts[:group]).gid + else + mount_gid = Etc::getpwnam(Etc.getlogin).gid + end + # Add them to the mount options + mount_options.append("uid=#{mount_uid}") + mount_options.append("gid=#{mount_gid}") + end + # SSHFS executable options sshfs_opts = opts[:sshfs_opts] sshfs_opts_append = opts[:sshfs_opts_append].to_s # provided by user + if not mount_options.empty?() + sshfs_opts_append+= ' -o ' + mount_options.join(',') + ' ' + end username = machine.ssh_info[:username] host = machine.ssh_info[:host] diff --git a/lib/vagrant-sshfs/cap/host/linux/sshfs_reverse_mount.rb b/lib/vagrant-sshfs/cap/host/linux/sshfs_reverse_mount.rb index 55230b0..367fdb9 100644 --- a/lib/vagrant-sshfs/cap/host/linux/sshfs_reverse_mount.rb +++ b/lib/vagrant-sshfs/cap/host/linux/sshfs_reverse_mount.rb @@ -1,3 +1,4 @@ +require "etc" require "log4r" require "vagrant/util/retryable" require "tempfile" @@ -92,33 +93,18 @@ def self.sshfs_mount(machine, opts) mount_options = opts.fetch(:mount_options, []) if (opts.has_key?(:owner) and opts[:owner]) or (opts.has_key?(:group) and opts[:group]) - # Find the `id` command on the system and set the error class - id_command = Vagrant::Util::Which.which('id') - getent_command = Vagrant::Util::Which.which('getent') - error_class = VagrantPlugins::SyncedFolderSSHFS::Errors::SSHFSFindUIDGIDFailed # Identify the uid - cmd = "#{id_command} -u #{opts[:owner]}" - result = Vagrant::Util::Subprocess.execute(*cmd.split()) - if result.exit_code != 0 - raise error_class, command: cmd, stdout: result.stdout, stderr: result.stderr + if opts.has_key?(:owner) and opts[:owner] + mount_uid = Etc::getpwnam(opts[:owner]).uid + else + mount_uid = Etc::getpwnam(Etc.getlogin).uid end - mount_uid = result.stdout.chomp # Identify the gid. If a group was provided use that otherwise use # the group detected with the detected user id. if opts.has_key?(:group) and opts[:group] - cmd = "#{getent_command} group #{opts[:group]}" - result = Vagrant::Util::Subprocess.execute(*cmd.split()) - if result.exit_code != 0 - raise error_class, command: cmd, stdout: result.stdout, stderr: result.stderr - end - mount_gid = result.stdout.split(':').at(2).to_s.chomp + mount_gid = Etc::getgrnam(opts[:group]).gid else - cmd = "#{id_command} -g #{mount_uid}" - result = Vagrant::Util::Subprocess.execute(*cmd.split()) - if result.exit_code != 0 - raise error_class, command: cmd, stdout: result.stdout, stderr: result.stderr - end - mount_gid = result.stdout.chomp + mount_gid = Etc::getpwnam(Etc.getlogin).gid end # Add them to the mount options mount_options.append("uid=#{mount_uid}") diff --git a/lib/vagrant-sshfs/errors.rb b/lib/vagrant-sshfs/errors.rb index 2c17aba..48d4909 100644 --- a/lib/vagrant-sshfs/errors.rb +++ b/lib/vagrant-sshfs/errors.rb @@ -37,10 +37,6 @@ class SSHFSExeNotAvailable < SSHFSError class SSHFSGetAbsolutePathFailed < SSHFSError error_key(:get_absolute_path_failed) end - - class SSHFSFindUIDGIDFailed < SSHFSError - error_key(:find_uid_gid_failed) - end end end end diff --git a/locales/synced_folder_sshfs.yml b/locales/synced_folder_sshfs.yml index 8b2a471..0f631ba 100644 --- a/locales/synced_folder_sshfs.yml +++ b/locales/synced_folder_sshfs.yml @@ -92,18 +92,4 @@ en: Stderr from the command: - %{stderr} - find_uid_gid_failed: |- - Attempting to find the UID/GID failed. - - The command and output are: - - %{command} - - Stdout from the command: - - %{stdout} - - Stderr from the command: - %{stderr}