23
23
from paramiko import ServerInterface, SFTPServerInterface, SFTPServer, SFTPAttributes, \
24
24
SFTPHandle, SFTP_OK, AUTH_SUCCESSFUL, OPEN_SUCCEEDED
26
27
from bzrlib.osutils import pathjoin
27
28
from bzrlib.trace import mutter
65
66
def __init__(self, server, root, home=None):
66
67
SFTPServerInterface.__init__(self, server)
68
# All paths are actually relative to 'root'.
69
# this is like implementing chroot().
72
# XXX: if 'home' is None, shouldn't it
73
# be set to '', since it should
74
# be relative to 'root'?
69
75
self.home = self.root
77
assert home.startswith(self.root), \
78
"home must be a subdirectory of root (%s vs %s)" \
71
80
self.home = home[len(self.root):]
72
if (len(self.home) > 0) and (self.home[0] == '/'):
81
if self.home.startswith('/'):
73
82
self.home = self.home[1:]
74
83
server._test_case.log('sftpserver - new connection')
76
85
def _realpath(self, path):
77
return self.root + self.canonicalize(path)
86
if sys.platform == 'win32':
87
# Win32 sftp paths end up looking like
88
# sftp://host@foo/h:/foo/bar
89
# which gets translated here to:
91
# Local paths stay 'foo/bar', though.
92
# Also, win32 needs to use the Unicode APIs.
93
thispath = path.decode('utf8')
94
if path.startswith('/'):
96
realpath = os.path.normpath(thispath[1:])
98
realpath = os.path.normpath(os.path.join(self.home, thispath))
100
realpath = self.root + self.canonicalize(path)
79
103
def canonicalize(self, path):
80
104
if os.path.isabs(path):
96
120
# TODO: win32 incorrectly lists paths with non-ascii if path is not
97
121
# unicode. However on Linux the server should only deal with
98
122
# bytestreams and posix.listdir does the right thing
99
flist = os.listdir(path)
123
if sys.platform == 'win32':
124
flist = [f.encode('utf8') for f in os.listdir(path)]
126
flist = os.listdir(path)
100
127
for fname in flist:
101
128
attr = SFTPAttributes.from_stat(os.stat(pathjoin(path, fname)))
102
129
attr.filename = fname