From 4d968c22b56bf045dca6abd0da891b6db9724c9f Mon Sep 17 00:00:00 2001 From: Mika Pflüger Date: Tue, 6 Jan 2015 20:14:34 +0100 Subject: Add method for "posix-rename@openssh.com" extension for SFTP client. Based on work of Warren Turkal --- paramiko/sftp_client.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 89840eaa..2979544f 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -33,7 +33,7 @@ from paramiko.sftp import BaseSFTP, CMD_OPENDIR, CMD_HANDLE, SFTPError, CMD_READ CMD_NAME, CMD_CLOSE, SFTP_FLAG_READ, SFTP_FLAG_WRITE, SFTP_FLAG_CREATE, \ SFTP_FLAG_TRUNC, SFTP_FLAG_APPEND, SFTP_FLAG_EXCL, CMD_OPEN, CMD_REMOVE, \ CMD_RENAME, CMD_MKDIR, CMD_RMDIR, CMD_STAT, CMD_ATTRS, CMD_LSTAT, \ - CMD_SYMLINK, CMD_SETSTAT, CMD_READLINK, CMD_REALPATH, CMD_STATUS, SFTP_OK, \ + CMD_SYMLINK, CMD_SETSTAT, CMD_READLINK, CMD_REALPATH, CMD_STATUS, CMD_EXTENDED, SFTP_OK, \ SFTP_EOF, SFTP_NO_SUCH_FILE, SFTP_PERMISSION_DENIED from paramiko.sftp_attr import SFTPAttributes @@ -354,7 +354,7 @@ class SFTPClient(BaseSFTP, ClosingContextManager): Rename a file or folder from ``oldpath`` to ``newpath``. :param str oldpath: existing name of the file or folder - :param str newpath: new name for the file or folder + :param str newpath: new name for the file or folder, must not exist already :raises IOError: if ``newpath`` is a folder, or something else goes wrong @@ -364,6 +364,23 @@ class SFTPClient(BaseSFTP, ClosingContextManager): self._log(DEBUG, 'rename(%r, %r)' % (oldpath, newpath)) self._request(CMD_RENAME, oldpath, newpath) + def posix_rename(self, oldpath, newpath): + """ + Rename a file or folder from ``oldpath`` to ``newpath``, following + posix conventions. + + :param str oldpath: existing name of the file or folder + :param str newpath: new name for the file or folder, will be + overwritten if it already exists + + :raises IOError: if ``newpath`` is a folder, posix-rename is not + supported by the server or something else goes wrong + """ + oldpath = self._adjust_cwd(oldpath) + newpath = self._adjust_cwd(newpath) + self._log(DEBUG, 'posix_rename(%r, %r)' % (oldpath, newpath)) + self._request(CMD_EXTENDED, "posix-rename@openssh.com", oldpath, newpath) + def mkdir(self, path, mode=o777): """ Create a folder (directory) named ``path`` with numeric mode ``mode``. -- cgit v1.2.3