bug 411099: chdir() isn't saving the cwd in a normalized way. added test.

This commit is contained in:
Robey Pointer 2009-10-31 16:06:53 -07:00
parent 7da1f2c4a3
commit 931f71e627
2 changed files with 69 additions and 53 deletions

View File

@ -105,7 +105,7 @@ class SFTPClient (BaseSFTP):
chan.invoke_subsystem('sftp') chan.invoke_subsystem('sftp')
return cls(chan) return cls(chan)
from_transport = classmethod(from_transport) from_transport = classmethod(from_transport)
def _log(self, level, msg, *args): def _log(self, level, msg, *args):
if isinstance(msg, list): if isinstance(msg, list):
for m in msg: for m in msg:
@ -116,20 +116,20 @@ class SFTPClient (BaseSFTP):
def close(self): def close(self):
""" """
Close the SFTP session and its underlying channel. Close the SFTP session and its underlying channel.
@since: 1.4 @since: 1.4
""" """
self._log(INFO, 'sftp session closed.') self._log(INFO, 'sftp session closed.')
self.sock.close() self.sock.close()
def get_channel(self): def get_channel(self):
""" """
Return the underlying L{Channel} object for this SFTP session. This Return the underlying L{Channel} object for this SFTP session. This
might be useful for doing things like setting a timeout on the channel. might be useful for doing things like setting a timeout on the channel.
@return: the SSH channel @return: the SSH channel
@rtype: L{Channel} @rtype: L{Channel}
@since: 1.7.1 @since: 1.7.1
""" """
return self.sock return self.sock
@ -148,14 +148,14 @@ class SFTPClient (BaseSFTP):
@rtype: list of str @rtype: list of str
""" """
return [f.filename for f in self.listdir_attr(path)] return [f.filename for f in self.listdir_attr(path)]
def listdir_attr(self, path='.'): def listdir_attr(self, path='.'):
""" """
Return a list containing L{SFTPAttributes} objects corresponding to Return a list containing L{SFTPAttributes} objects corresponding to
files in the given C{path}. The list is in arbitrary order. It does files in the given C{path}. The list is in arbitrary order. It does
not include the special entries C{'.'} and C{'..'} even if they are not include the special entries C{'.'} and C{'..'} even if they are
present in the folder. present in the folder.
The returned L{SFTPAttributes} objects will each have an additional The returned L{SFTPAttributes} objects will each have an additional
field: C{longname}, which may contain a formatted string of the file's field: C{longname}, which may contain a formatted string of the file's
attributes, in unix format. The content of this string will probably attributes, in unix format. The content of this string will probably
@ -165,7 +165,7 @@ class SFTPClient (BaseSFTP):
@type path: str @type path: str
@return: list of attributes @return: list of attributes
@rtype: list of L{SFTPAttributes} @rtype: list of L{SFTPAttributes}
@since: 1.2 @since: 1.2
""" """
path = self._adjust_cwd(path) path = self._adjust_cwd(path)
@ -206,7 +206,7 @@ class SFTPClient (BaseSFTP):
existing file), C{'a+'} for reading/appending. The python C{'b'} flag existing file), C{'a+'} for reading/appending. The python C{'b'} flag
is ignored, since SSH treats all files as binary. The C{'U'} flag is is ignored, since SSH treats all files as binary. The C{'U'} flag is
supported in a compatible way. supported in a compatible way.
Since 1.5.2, an C{'x'} flag indicates that the operation should only Since 1.5.2, an C{'x'} flag indicates that the operation should only
succeed if the file was created and did not previously exist. This has succeed if the file was created and did not previously exist. This has
no direct mapping to python's file flags, but is commonly known as the no direct mapping to python's file flags, but is commonly known as the
@ -276,7 +276,7 @@ class SFTPClient (BaseSFTP):
@type oldpath: str @type oldpath: str
@param newpath: new name for the file or folder @param newpath: new name for the file or folder
@type newpath: str @type newpath: str
@raise IOError: if C{newpath} is a folder, or something else goes @raise IOError: if C{newpath} is a folder, or something else goes
wrong wrong
""" """
@ -389,7 +389,7 @@ class SFTPClient (BaseSFTP):
attr = SFTPAttributes() attr = SFTPAttributes()
attr.st_mode = mode attr.st_mode = mode
self._request(CMD_SETSTAT, path, attr) self._request(CMD_SETSTAT, path, attr)
def chown(self, path, uid, gid): def chown(self, path, uid, gid):
""" """
Change the owner (C{uid}) and group (C{gid}) of a file. As with Change the owner (C{uid}) and group (C{gid}) of a file. As with
@ -438,7 +438,7 @@ class SFTPClient (BaseSFTP):
Change the size of the file specified by C{path}. This usually extends Change the size of the file specified by C{path}. This usually extends
or shrinks the size of the file, just like the C{truncate()} method on or shrinks the size of the file, just like the C{truncate()} method on
python file objects. python file objects.
@param path: path of the file to modify @param path: path of the file to modify
@type path: str @type path: str
@param size: the new size of the file @param size: the new size of the file
@ -484,7 +484,7 @@ class SFTPClient (BaseSFTP):
@type path: str @type path: str
@return: normalized form of the given path @return: normalized form of the given path
@rtype: str @rtype: str
@raise IOError: if the path can't be resolved on the server @raise IOError: if the path can't be resolved on the server
""" """
path = self._adjust_cwd(path) path = self._adjust_cwd(path)
@ -496,47 +496,51 @@ class SFTPClient (BaseSFTP):
if count != 1: if count != 1:
raise SFTPError('Realpath returned %d results' % count) raise SFTPError('Realpath returned %d results' % count)
return _to_unicode(msg.get_string()) return _to_unicode(msg.get_string())
def chdir(self, path): def chdir(self, path):
""" """
Change the "current directory" of this SFTP session. Since SFTP Change the "current directory" of this SFTP session. Since SFTP
doesn't really have the concept of a current working directory, this doesn't really have the concept of a current working directory, this
is emulated by paramiko. Once you use this method to set a working is emulated by paramiko. Once you use this method to set a working
directory, all operations on this SFTPClient object will be relative directory, all operations on this SFTPClient object will be relative
to that path. to that path. You can pass in C{None} to stop using a current working
directory.
@param path: new current working directory @param path: new current working directory
@type path: str @type path: str
@raise IOError: if the requested path doesn't exist on the server @raise IOError: if the requested path doesn't exist on the server
@since: 1.4 @since: 1.4
""" """
if path is None:
self._cwd = None
return
if not stat.S_ISDIR(self.stat(path).st_mode): if not stat.S_ISDIR(self.stat(path).st_mode):
raise SFTPError(errno.ENOTDIR, "%s: %s" % (os.strerror(errno.ENOTDIR), path)) raise SFTPError(errno.ENOTDIR, "%s: %s" % (os.strerror(errno.ENOTDIR), path))
self._cwd = self.normalize(path) self._cwd = self.normalize(path).encode('utf-8')
def getcwd(self): def getcwd(self):
""" """
Return the "current working directory" for this SFTP session, as Return the "current working directory" for this SFTP session, as
emulated by paramiko. If no directory has been set with L{chdir}, emulated by paramiko. If no directory has been set with L{chdir},
this method will return C{None}. this method will return C{None}.
@return: the current working directory on the server, or C{None} @return: the current working directory on the server, or C{None}
@rtype: str @rtype: str
@since: 1.4 @since: 1.4
""" """
return self._cwd return self._cwd
def put(self, localpath, remotepath, callback=None): def put(self, localpath, remotepath, callback=None):
""" """
Copy a local file (C{localpath}) to the SFTP server as C{remotepath}. Copy a local file (C{localpath}) to the SFTP server as C{remotepath}.
Any exception raised by operations will be passed through. This Any exception raised by operations will be passed through. This
method is primarily provided as a convenience. method is primarily provided as a convenience.
The SFTP operations use pipelining for speed. The SFTP operations use pipelining for speed.
@param localpath: the local file to copy @param localpath: the local file to copy
@type localpath: str @type localpath: str
@param remotepath: the destination path on the SFTP server @param remotepath: the destination path on the SFTP server
@ -548,7 +552,7 @@ class SFTPClient (BaseSFTP):
@return: an object containing attributes about the given file @return: an object containing attributes about the given file
(since 1.7.4) (since 1.7.4)
@rtype: SFTPAttributes @rtype: SFTPAttributes
@since: 1.4 @since: 1.4
""" """
file_size = os.stat(localpath).st_size file_size = os.stat(localpath).st_size
@ -574,13 +578,13 @@ class SFTPClient (BaseSFTP):
if s.st_size != size: if s.st_size != size:
raise IOError('size mismatch in put! %d != %d' % (s.st_size, size)) raise IOError('size mismatch in put! %d != %d' % (s.st_size, size))
return s return s
def get(self, remotepath, localpath, callback=None): def get(self, remotepath, localpath, callback=None):
""" """
Copy a remote file (C{remotepath}) from the SFTP server to the local Copy a remote file (C{remotepath}) from the SFTP server to the local
host as C{localpath}. Any exception raised by operations will be host as C{localpath}. Any exception raised by operations will be
passed through. This method is primarily provided as a convenience. passed through. This method is primarily provided as a convenience.
@param remotepath: the remote file to copy @param remotepath: the remote file to copy
@type remotepath: str @type remotepath: str
@param localpath: the destination path on the local host @param localpath: the destination path on the local host
@ -589,7 +593,7 @@ class SFTPClient (BaseSFTP):
transferred so far and the total bytes to be transferred transferred so far and the total bytes to be transferred
(since 1.7.4) (since 1.7.4)
@type callback: function(int, int) @type callback: function(int, int)
@since: 1.4 @since: 1.4
""" """
fr = self.file(remotepath, 'rb') fr = self.file(remotepath, 'rb')
@ -622,7 +626,7 @@ class SFTPClient (BaseSFTP):
def _request(self, t, *arg): def _request(self, t, *arg):
num = self._async_request(type(None), t, *arg) num = self._async_request(type(None), t, *arg)
return self._read_response(num) return self._read_response(num)
def _async_request(self, fileobj, t, *arg): def _async_request(self, fileobj, t, *arg):
# this method may be called from other threads (prefetch) # this method may be called from other threads (prefetch)
self._lock.acquire() self._lock.acquire()
@ -699,7 +703,7 @@ class SFTPClient (BaseSFTP):
raise IOError(errno.EACCES, text) raise IOError(errno.EACCES, text)
else: else:
raise IOError(text) raise IOError(text)
def _adjust_cwd(self, path): def _adjust_cwd(self, path):
""" """
Return an adjusted path if we're emulating a "current working Return an adjusted path if we're emulating a "current working

View File

@ -80,7 +80,7 @@ class SFTPTest (unittest.TestCase):
def init(hostname, username, keyfile, passwd): def init(hostname, username, keyfile, passwd):
global sftp, tc global sftp, tc
t = paramiko.Transport(hostname) t = paramiko.Transport(hostname)
tc = t tc = t
try: try:
@ -136,7 +136,7 @@ class SFTPTest (unittest.TestCase):
global g_big_file_test global g_big_file_test
g_big_file_test = onoff g_big_file_test = onoff
set_big_file_test = staticmethod(set_big_file_test) set_big_file_test = staticmethod(set_big_file_test)
def setUp(self): def setUp(self):
global FOLDER global FOLDER
for i in xrange(1000): for i in xrange(1000):
@ -206,7 +206,7 @@ class SFTPTest (unittest.TestCase):
f.close() f.close()
finally: finally:
sftp.remove(FOLDER + '/append.txt') sftp.remove(FOLDER + '/append.txt')
def test_5_rename(self): def test_5_rename(self):
""" """
verify that renaming a file works. verify that renaming a file works.
@ -309,7 +309,7 @@ class SFTPTest (unittest.TestCase):
self.assertEqual(stat.st_atime, atime) self.assertEqual(stat.st_atime, atime)
# can't really test chown, since we'd have to know a valid uid. # can't really test chown, since we'd have to know a valid uid.
sftp.truncate(FOLDER + '/special', 512) sftp.truncate(FOLDER + '/special', 512)
stat = sftp.stat(FOLDER + '/special') stat = sftp.stat(FOLDER + '/special')
self.assertEqual(stat.st_size, 512) self.assertEqual(stat.st_size, 512)
@ -325,7 +325,7 @@ class SFTPTest (unittest.TestCase):
try: try:
f.write('x' * 1024) f.write('x' * 1024)
f.close() f.close()
f = sftp.open(FOLDER + '/special', 'r+') f = sftp.open(FOLDER + '/special', 'r+')
stat = f.stat() stat = f.stat()
f.chmod((stat.st_mode & ~0777) | 0600) f.chmod((stat.st_mode & ~0777) | 0600)
@ -348,16 +348,16 @@ class SFTPTest (unittest.TestCase):
self.assertEqual(stat.st_mtime, mtime) self.assertEqual(stat.st_mtime, mtime)
if sys.platform not in ('win32', 'cygwin'): if sys.platform not in ('win32', 'cygwin'):
self.assertEqual(stat.st_atime, atime) self.assertEqual(stat.st_atime, atime)
# can't really test chown, since we'd have to know a valid uid. # can't really test chown, since we'd have to know a valid uid.
f.truncate(512) f.truncate(512)
stat = f.stat() stat = f.stat()
self.assertEqual(stat.st_size, 512) self.assertEqual(stat.st_size, 512)
f.close() f.close()
finally: finally:
sftp.remove(FOLDER + '/special') sftp.remove(FOLDER + '/special')
def test_A_readline_seek(self): def test_A_readline_seek(self):
""" """
create a text file and write a bunch of text into it. then count the lines create a text file and write a bunch of text into it. then count the lines
@ -510,7 +510,7 @@ class SFTPTest (unittest.TestCase):
self.assert_(False, 'no exception removing nonexistent subfolder') self.assert_(False, 'no exception removing nonexistent subfolder')
except IOError: except IOError:
pass pass
def test_G_chdir(self): def test_G_chdir(self):
""" """
verify that chdir/getcwd work. verify that chdir/getcwd work.
@ -524,7 +524,7 @@ class SFTPTest (unittest.TestCase):
sftp.mkdir('beta') sftp.mkdir('beta')
self.assertEquals(root + FOLDER + '/alpha', sftp.getcwd()) self.assertEquals(root + FOLDER + '/alpha', sftp.getcwd())
self.assertEquals(['beta'], sftp.listdir('.')) self.assertEquals(['beta'], sftp.listdir('.'))
sftp.chdir('beta') sftp.chdir('beta')
f = sftp.open('fish', 'w') f = sftp.open('fish', 'w')
f.write('hello\n') f.write('hello\n')
@ -554,7 +554,7 @@ class SFTPTest (unittest.TestCase):
""" """
import os, warnings import os, warnings
warnings.filterwarnings('ignore', 'tempnam.*') warnings.filterwarnings('ignore', 'tempnam.*')
localname = os.tempnam() localname = os.tempnam()
text = 'All I wanted was a plastic bunny rabbit.\n' text = 'All I wanted was a plastic bunny rabbit.\n'
f = open(localname, 'wb') f = open(localname, 'wb')
@ -564,22 +564,22 @@ class SFTPTest (unittest.TestCase):
def progress_callback(x, y): def progress_callback(x, y):
saved_progress.append((x, y)) saved_progress.append((x, y))
sftp.put(localname, FOLDER + '/bunny.txt', progress_callback) sftp.put(localname, FOLDER + '/bunny.txt', progress_callback)
f = sftp.open(FOLDER + '/bunny.txt', 'r') f = sftp.open(FOLDER + '/bunny.txt', 'r')
self.assertEquals(text, f.read(128)) self.assertEquals(text, f.read(128))
f.close() f.close()
self.assertEquals((41, 41), saved_progress[-1]) self.assertEquals((41, 41), saved_progress[-1])
os.unlink(localname) os.unlink(localname)
localname = os.tempnam() localname = os.tempnam()
saved_progress = [] saved_progress = []
sftp.get(FOLDER + '/bunny.txt', localname, progress_callback) sftp.get(FOLDER + '/bunny.txt', localname, progress_callback)
f = open(localname, 'rb') f = open(localname, 'rb')
self.assertEquals(text, f.read(128)) self.assertEquals(text, f.read(128))
f.close() f.close()
self.assertEquals((41, 41), saved_progress[-1]) self.assertEquals((41, 41), saved_progress[-1])
os.unlink(localname) os.unlink(localname)
sftp.unlink(FOLDER + '/bunny.txt') sftp.unlink(FOLDER + '/bunny.txt')
@ -592,7 +592,7 @@ class SFTPTest (unittest.TestCase):
f = sftp.open(FOLDER + '/kitty.txt', 'w') f = sftp.open(FOLDER + '/kitty.txt', 'w')
f.write('here kitty kitty' * 64) f.write('here kitty kitty' * 64)
f.close() f.close()
try: try:
f = sftp.open(FOLDER + '/kitty.txt', 'r') f = sftp.open(FOLDER + '/kitty.txt', 'r')
sum = f.check('sha1') sum = f.check('sha1')
@ -612,7 +612,7 @@ class SFTPTest (unittest.TestCase):
""" """
f = sftp.open(FOLDER + '/unusual.txt', 'wx') f = sftp.open(FOLDER + '/unusual.txt', 'wx')
f.close() f.close()
try: try:
try: try:
f = sftp.open(FOLDER + '/unusual.txt', 'wx') f = sftp.open(FOLDER + '/unusual.txt', 'wx')
@ -621,7 +621,7 @@ class SFTPTest (unittest.TestCase):
pass pass
finally: finally:
sftp.unlink(FOLDER + '/unusual.txt') sftp.unlink(FOLDER + '/unusual.txt')
def test_K_utf8(self): def test_K_utf8(self):
""" """
verify that unicode strings are encoded into utf8 correctly. verify that unicode strings are encoded into utf8 correctly.
@ -629,7 +629,7 @@ class SFTPTest (unittest.TestCase):
f = sftp.open(FOLDER + '/something', 'w') f = sftp.open(FOLDER + '/something', 'w')
f.write('okay') f.write('okay')
f.close() f.close()
try: try:
sftp.rename(FOLDER + '/something', FOLDER + u'/\u00fcnic\u00f8de') sftp.rename(FOLDER + '/something', FOLDER + u'/\u00fcnic\u00f8de')
sftp.open(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65', 'r') sftp.open(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65', 'r')
@ -637,7 +637,19 @@ class SFTPTest (unittest.TestCase):
self.fail('exception ' + e) self.fail('exception ' + e)
sftp.unlink(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65') sftp.unlink(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65')
def test_L_bad_readv(self): def test_L_utf8_chdir(self):
sftp.mkdir(FOLDER + u'\u00fcnic\u00f8de')
try:
sftp.chdir(FOLDER + u'\u00fcnic\u00f8de')
f = sftp.open('something', 'w')
f.write('okay')
f.close()
sftp.unlink('something')
finally:
sftp.chdir(None)
sftp.rmdir(FOLDER + u'\u00fcnic\u00f8de')
def test_M_bad_readv(self):
""" """
verify that readv at the end of the file doesn't essplode. verify that readv at the end of the file doesn't essplode.
""" """
@ -647,7 +659,7 @@ class SFTPTest (unittest.TestCase):
f = sftp.open(FOLDER + '/zero', 'r') f = sftp.open(FOLDER + '/zero', 'r')
data = f.readv([(0, 12)]) data = f.readv([(0, 12)])
f.close() f.close()
f = sftp.open(FOLDER + '/zero', 'r') f = sftp.open(FOLDER + '/zero', 'r')
f.prefetch() f.prefetch()
data = f.read(100) data = f.read(100)
@ -658,7 +670,7 @@ class SFTPTest (unittest.TestCase):
def XXX_test_M_seek_append(self): def XXX_test_M_seek_append(self):
""" """
verify that seek does't affect writes during append. verify that seek does't affect writes during append.
does not work except through paramiko. :( openssh fails. does not work except through paramiko. :( openssh fails.
""" """
f = sftp.open(FOLDER + '/append.txt', 'a') f = sftp.open(FOLDER + '/append.txt', 'a')