some fixes for win32: potential fix for lockup during failed tests; don't try chmod/chown or symlinks; fix canonicalize to convert dos-style path separators to '/'; open local files in binary mode; close a file before erasing it
This commit is contained in:
Robey Pointer 2006-04-06 11:18:21 -07:00
parent d7b28ac257
commit ccc1046a5c
3 changed files with 31 additions and 10 deletions

View File

@ -96,6 +96,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler):
def finish_subsystem(self):
self.server.session_ended()
super(SFTPServer, self).finish_subsystem()
# close any file handles that were left open (so we can return them to the OS quickly)
for f in self.file_table.itervalues():
f.close()
@ -141,6 +142,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler):
@param attr: attributes to change.
@type attr: L{SFTPAttributes}
"""
if sys.platform != 'win32':
# mode operations are meaningless on win32
if attr._flags & attr.FLAG_PERMISSIONS:
os.chmod(filename, attr.st_mode)
if attr._flags & attr.FLAG_UIDGID:

View File

@ -271,9 +271,13 @@ class SFTPServerInterface (object):
The default implementation returns C{os.path.normpath('/' + path)}.
"""
if os.path.isabs(path):
return os.path.normpath(path)
out = os.path.normpath(path)
else:
return os.path.normpath('/' + path)
out = os.path.normpath('/' + path)
if sys.platform == 'win32':
# on windows, normalize backslashes to sftp/posix format
out = out.replace('\\', '/')
return out
def readlink(self, path):
"""

View File

@ -289,7 +289,11 @@ class SFTPTest (unittest.TestCase):
stat = sftp.stat(FOLDER + '/special')
sftp.chmod(FOLDER + '/special', (stat.st_mode & ~0777) | 0600)
stat = sftp.stat(FOLDER + '/special')
self.assertEqual(stat.st_mode & 0777, 0600)
expected_mode = 0600
if sys.platform == 'win32':
# chmod not really functional on windows
expected_mode = 0666
self.assertEqual(stat.st_mode & 0777, expected_mode)
self.assertEqual(stat.st_size, 1024)
mtime = stat.st_mtime - 3600
@ -321,7 +325,12 @@ class SFTPTest (unittest.TestCase):
stat = f.stat()
f.chmod((stat.st_mode & ~0777) | 0600)
stat = f.stat()
self.assertEqual(stat.st_mode & 0777, 0600)
expected_mode = 0600
if sys.platform == 'win32':
# chmod not really functional on windows
expected_mode = 0666
self.assertEqual(stat.st_mode & 0777, expected_mode)
self.assertEqual(stat.st_size, 1024)
mtime = stat.st_mtime - 3600
@ -394,6 +403,10 @@ class SFTPTest (unittest.TestCase):
"""
create a symlink and then check that lstat doesn't follow it.
"""
if not hasattr(os, "symlink"):
# skip symlink tests on windows
return
f = sftp.open(FOLDER + '/original.txt', 'w')
try:
f.write('original\n')
@ -535,7 +548,7 @@ class SFTPTest (unittest.TestCase):
localname = os.tempnam()
text = 'All I wanted was a plastic bunny rabbit.\n'
f = open(localname, 'w')
f = open(localname, 'wb')
f.write(text)
f.close()
sftp.put(localname, FOLDER + '/bunny.txt')
@ -548,7 +561,7 @@ class SFTPTest (unittest.TestCase):
localname = os.tempnam()
sftp.get(FOLDER + '/bunny.txt', localname)
f = open(localname, 'r')
f = open(localname, 'rb')
self.assertEquals(text, f.read(128))
f.close()
@ -574,6 +587,7 @@ class SFTPTest (unittest.TestCase):
sum = f.check('md5', 0, 0, 510)
self.assertEquals('EB3B45B8CD55A0707D99B177544A319F373183D241432BB2157AB9E46358C4AC90370B5CADE5D90336FC1716F90B36D6',
paramiko.util.hexify(sum))
f.close()
finally:
sftp.unlink(FOLDER + '/kitty.txt')