Skip to content

Commit

Permalink
sshfs_reverse_mount: use ruby Etc lib for owner/group
Browse files Browse the repository at this point in the history
When I started to go in and add owner/group support for
Darwin reverse sshfs mounting I noticed Mac OS doesn't have
`getent`. Let's use the builtin ruby library here instead
which should work cross platform.
  • Loading branch information
dustymabe committed Jun 20, 2022
1 parent bf72f0f commit 51be8cb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 39 deletions.
27 changes: 27 additions & 0 deletions lib/vagrant-sshfs/cap/host/darwin/sshfs_reverse_mount.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "etc"
require "log4r"
require "vagrant/util/retryable"
require "tempfile"
Expand Down Expand Up @@ -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]
Expand Down
28 changes: 7 additions & 21 deletions lib/vagrant-sshfs/cap/host/linux/sshfs_reverse_mount.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "etc"
require "log4r"
require "vagrant/util/retryable"
require "tempfile"
Expand Down Expand Up @@ -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}")
Expand Down
4 changes: 0 additions & 4 deletions lib/vagrant-sshfs/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 0 additions & 14 deletions locales/synced_folder_sshfs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}

0 comments on commit 51be8cb

Please sign in to comment.