Skip to content

Commit

Permalink
bug 411099: chdir() isn't saving the cwd in a normalized way. added t…
Browse files Browse the repository at this point in the history
…est.
  • Loading branch information
Robey Pointer committed Oct 31, 2009
1 parent 7da1f2c commit 931f71e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 53 deletions.
66 changes: 35 additions & 31 deletions paramiko/sftp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def from_transport(cls, t):
chan.invoke_subsystem('sftp')
return cls(chan)
from_transport = classmethod(from_transport)

def _log(self, level, msg, *args):
if isinstance(msg, list):
for m in msg:
Expand All @@ -116,20 +116,20 @@ def _log(self, level, msg, *args):
def close(self):
"""
Close the SFTP session and its underlying channel.
@since: 1.4
"""
self._log(INFO, 'sftp session closed.')
self.sock.close()

def get_channel(self):
"""
Return the underlying L{Channel} object for this SFTP session. This
might be useful for doing things like setting a timeout on the channel.
@return: the SSH channel
@rtype: L{Channel}
@since: 1.7.1
"""
return self.sock
Expand All @@ -148,14 +148,14 @@ def listdir(self, path='.'):
@rtype: list of str
"""
return [f.filename for f in self.listdir_attr(path)]

def listdir_attr(self, path='.'):
"""
Return a list containing L{SFTPAttributes} objects corresponding to
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
present in the folder.
The returned L{SFTPAttributes} objects will each have an additional
field: C{longname}, which may contain a formatted string of the file's
attributes, in unix format. The content of this string will probably
Expand All @@ -165,7 +165,7 @@ def listdir_attr(self, path='.'):
@type path: str
@return: list of attributes
@rtype: list of L{SFTPAttributes}
@since: 1.2
"""
path = self._adjust_cwd(path)
Expand Down Expand Up @@ -206,7 +206,7 @@ def open(self, filename, mode='r', bufsize=-1):
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
supported in a compatible way.
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
no direct mapping to python's file flags, but is commonly known as the
Expand Down Expand Up @@ -276,7 +276,7 @@ def rename(self, oldpath, newpath):
@type oldpath: str
@param newpath: new name for the file or folder
@type newpath: str
@raise IOError: if C{newpath} is a folder, or something else goes
wrong
"""
Expand Down Expand Up @@ -389,7 +389,7 @@ def chmod(self, path, mode):
attr = SFTPAttributes()
attr.st_mode = mode
self._request(CMD_SETSTAT, path, attr)

def chown(self, path, uid, gid):
"""
Change the owner (C{uid}) and group (C{gid}) of a file. As with
Expand Down Expand Up @@ -438,7 +438,7 @@ def truncate(self, path, size):
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
python file objects.
@param path: path of the file to modify
@type path: str
@param size: the new size of the file
Expand Down Expand Up @@ -484,7 +484,7 @@ def normalize(self, path):
@type path: str
@return: normalized form of the given path
@rtype: str
@raise IOError: if the path can't be resolved on the server
"""
path = self._adjust_cwd(path)
Expand All @@ -496,47 +496,51 @@ def normalize(self, path):
if count != 1:
raise SFTPError('Realpath returned %d results' % count)
return _to_unicode(msg.get_string())

def chdir(self, path):
"""
Change the "current directory" of this SFTP session. Since SFTP
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
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
@type path: str
@raise IOError: if the requested path doesn't exist on the server
@since: 1.4
"""
if path is None:
self._cwd = None
return
if not stat.S_ISDIR(self.stat(path).st_mode):
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):
"""
Return the "current working directory" for this SFTP session, as
emulated by paramiko. If no directory has been set with L{chdir},
this method will return C{None}.
@return: the current working directory on the server, or C{None}
@rtype: str
@since: 1.4
"""
return self._cwd

def put(self, localpath, remotepath, callback=None):
"""
Copy a local file (C{localpath}) to the SFTP server as C{remotepath}.
Any exception raised by operations will be passed through. This
method is primarily provided as a convenience.
The SFTP operations use pipelining for speed.
@param localpath: the local file to copy
@type localpath: str
@param remotepath: the destination path on the SFTP server
Expand All @@ -548,7 +552,7 @@ def put(self, localpath, remotepath, callback=None):
@return: an object containing attributes about the given file
(since 1.7.4)
@rtype: SFTPAttributes
@since: 1.4
"""
file_size = os.stat(localpath).st_size
Expand All @@ -574,13 +578,13 @@ def put(self, localpath, remotepath, callback=None):
if s.st_size != size:
raise IOError('size mismatch in put! %d != %d' % (s.st_size, size))
return s

def get(self, remotepath, localpath, callback=None):
"""
Copy a remote file (C{remotepath}) from the SFTP server to the local
host as C{localpath}. Any exception raised by operations will be
passed through. This method is primarily provided as a convenience.
@param remotepath: the remote file to copy
@type remotepath: str
@param localpath: the destination path on the local host
Expand All @@ -589,7 +593,7 @@ def get(self, remotepath, localpath, callback=None):
transferred so far and the total bytes to be transferred
(since 1.7.4)
@type callback: function(int, int)
@since: 1.4
"""
fr = self.file(remotepath, 'rb')
Expand Down Expand Up @@ -622,7 +626,7 @@ def get(self, remotepath, localpath, callback=None):
def _request(self, t, *arg):
num = self._async_request(type(None), t, *arg)
return self._read_response(num)

def _async_request(self, fileobj, t, *arg):
# this method may be called from other threads (prefetch)
self._lock.acquire()
Expand Down Expand Up @@ -699,7 +703,7 @@ def _convert_status(self, msg):
raise IOError(errno.EACCES, text)
else:
raise IOError(text)

def _adjust_cwd(self, path):
"""
Return an adjusted path if we're emulating a "current working
Expand Down
Loading

0 comments on commit 931f71e

Please sign in to comment.