Skip to content

Commit

Permalink
Change remote platform management in SSHFS
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Larralde committed Oct 2, 2017
1 parent 665804c commit 23a3f9f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 49 deletions.
16 changes: 0 additions & 16 deletions fs/sshfs/enums.py

This file was deleted.

52 changes: 19 additions & 33 deletions fs/sshfs/sshfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import io
import os
import pwd
import grp
import stat
import socket

Expand All @@ -25,7 +23,6 @@
from ..mode import Mode

from .error_tools import convert_sshfs_errors
from .enums import Platform


class _SSHFileWrapper(RawWrapper):
Expand Down Expand Up @@ -330,27 +327,26 @@ def _guess_platform(self):
"""
uname_sys = self._exec_command("uname -s")
sysinfo = self._exec_command("sysinfo")

if sysinfo is not None and sysinfo:
return Platform.Windows

elif uname_sys is not None:
if uname_sys.endswith(b"BSD") or uname_sys == b"DragonFly":
return Platform.BSD
if uname_sys is not None:
if uname_sys == b"FreeBSD":
return "freebsd"
elif uname_sys == b"Darwin":
return Platform.Darwin
return "darwin"
elif uname_sys == b"Linux":
return Platform.Linux

return Platform._Unknown
return "linux"
elif uname_sys.startswith(b"CYGWIN"):
return "cygwin"
elif sysinfo is not None and sysinfo:
return "win32"
return "unknown"

def _guess_locale(self):
"""Guess the locale of the remote server.
Returns:
str: the guessed locale.
"""
if self.platform in Platform.Unix:
if self.platform in ("linux", "darwin", "freebsd"):
locale = self._exec_command('locale charmap')
if locale is not None:
return locale.decode('ascii').lower()
Expand Down Expand Up @@ -388,8 +384,7 @@ def _make_details_from_stat(self, stat_result):
}

details['created'] = getattr(stat_result, 'st_birthtime', None)
ctime_key = 'created' if self.platform is Platform.Windows \
else 'metadata_changed'
ctime_key = 'created' if self.platform=="win32" else 'metadata_changed'
details[ctime_key] = getattr(stat_result, 'st_ctime', None)
return details

Expand All @@ -401,22 +396,13 @@ def _make_access_from_stat(self, stat_result):
access['gid'] = stat_result.st_gid
access['uid'] = stat_result.st_uid

if self.platform in Platform.Unix:

targets = [
{'db': 'group', 'id': access['gid'], 'len': 4, 'key': 'group',
'name': lambda g: grp.struct_group(g).gr_name},
{'db': 'passwd', 'id': access['uid'], 'len': 7, 'key': 'user',
'name': lambda g: pwd.struct_passwd(g).pw_name},
]

for target in targets:
getent = self._exec_command(
'getent {db} {id}'.format(**target)).split(b':')
if len(getent) < target['len']:
getent += [b''] * (target['len'] - len(getent))
access[target['key']] = \
target['name'](getent).decode(self.locale or 'utf-8')
if self.platform in ("linux", "darwin", "freebsd"):
def entry_name(db, _id):
entry = self._exec_command('getent {} {}'.format(db, _id))
name = next(iter(entry.split(b':')))
return name.decode(self.locale or 'utf-8')
access['group'] = entry_name('group', access['gid'])
access['user'] = entry_name('passwd', access['uid'])

return access

Expand Down

0 comments on commit 23a3f9f

Please sign in to comment.