예제 #1
0
    def _sftp_open_exclusive(self, abspath, mode=None):
        """Open a remote path exclusively.

        SFTP supports O_EXCL (SFTP_FLAG_EXCL), which fails if
        the file already exists. However it does not expose this
        at the higher level of SFTPClient.open(), so we have to
        sneak away with it.

        WARNING: This breaks the SFTPClient abstraction, so it
        could easily break against an updated version of paramiko.

        :param abspath: The remote absolute path where the file should be opened
        :param mode: The mode permissions bits for the new file
        """
        # TODO: jam 20060816 Paramiko >= 1.6.2 (probably earlier) supports
        #       using the 'x' flag to indicate SFTP_FLAG_EXCL.
        #       However, there is no way to set the permission mode at open
        #       time using the sftp_client.file() functionality.
        path = self._get_sftp()._adjust_cwd(abspath)
        # mutter('sftp abspath %s => %s', abspath, path)
        attr = SFTPAttributes()
        if mode is not None:
            attr.st_mode = mode
        omode = (SFTP_FLAG_WRITE | SFTP_FLAG_CREATE
                | SFTP_FLAG_TRUNC | SFTP_FLAG_EXCL)
        try:
            t, msg = self._get_sftp()._request(CMD_OPEN, path, omode, attr)
            if t != CMD_HANDLE:
                raise TransportError('Expected an SFTP handle')
            handle = msg.get_string()
            return SFTPFile(self._get_sftp(), handle, 'wb', -1)
        except (paramiko.SSHException, IOError), e:
            self._translate_io_exception(e, abspath, ': unable to open',
                failure_exc=FileExists)
예제 #2
0
    def chmod(self, mode):
        """
        Change the mode (permissions) of this file.  The permissions are
        unix-style and identical to those used by Python's `os.chmod`
        function.

        :param int mode: new permissions
        """
        self.sftp._log(DEBUG, 'chmod(%s, %r)' % (hexlify(self.handle), mode))
        attr = SFTPAttributes()
        attr.st_mode = mode
        self.sftp._request(CMD_FSETSTAT, self.handle, attr)
예제 #3
0
 def truncate(self, size):
     """
     Change the size of this file.  This usually extends
     or shrinks the size of the file, just like the C{truncate()} method on
     python file objects.
     
     @param size: the new size of the file
     @type size: int or long
     """
     self.sftp._log(DEBUG, 'truncate(%s, %r)' % (hexlify(self.handle), size))
     attr = SFTPAttributes()
     attr.st_size = size
     self.sftp._request(CMD_FSETSTAT, self.handle, attr)
예제 #4
0
파일: sftp_file.py 프로젝트: OmniDB/OmniDB
    def truncate(self, size):
        """
        Change the size of this file.  This usually extends
        or shrinks the size of the file, just like the ``truncate()`` method on
        Python file objects.

        :param size: the new size of the file
        """
        self.sftp._log(
            DEBUG, "truncate({}, {!r})".format(hexlify(self.handle), size)
        )
        attr = SFTPAttributes()
        attr.st_size = size
        self.sftp._request(CMD_FSETSTAT, self.handle, attr)
예제 #5
0
    def truncate(self, path, size):
        """
        Change the size of the file specified by ``path``.  This usually
        extends or shrinks the size of the file, just like the `~file.truncate`
        method on Python file objects.

        :param str path: path of the file to modify
        :param int size: the new size of the file
        """
        path = self._adjust_cwd(path)
        self._log(DEBUG, 'truncate({!r}, {!r})'.format(path, size))
        attr = SFTPAttributes()
        attr.st_size = size
        self._request(CMD_SETSTAT, path, attr)
예제 #6
0
    def chown(self, uid, gid):
        """
        Change the owner (``uid``) and group (``gid``) of this file.  As with
        Python's `os.chown` function, you must pass both arguments, so if you
        only want to change one, use `stat` first to retrieve the current
        owner and group.

        :param int uid: new owner's uid
        :param int gid: new group id
        """
        self.sftp._log(DEBUG, 'chown(%s, %r, %r)' % (hexlify(self.handle), uid, gid))
        attr = SFTPAttributes()
        attr.st_uid, attr.st_gid = uid, gid
        self.sftp._request(CMD_FSETSTAT, self.handle, attr)
예제 #7
0
    def chmod(self, path, mode):
        """
        Change the mode (permissions) of a file.  The permissions are
        unix-style and identical to those used by Python's `os.chmod`
        function.

        :param str path: path of the file to change the permissions of
        :param int mode: new permissions
        """
        path = self._adjust_cwd(path)
        self._log(DEBUG, 'chmod(%r, %r)' % (path, mode))
        attr = SFTPAttributes()
        attr.st_mode = mode
        self._request(CMD_SETSTAT, path, attr)
예제 #8
0
    def mkdir(self, path, mode=o777):
        """
        Create a folder (directory) named ``path`` with numeric mode ``mode``.
        The default mode is 0777 (octal).  On some systems, mode is ignored.
        Where it is used, the current umask value is first masked out.

        :param str path: name of the folder to create
        :param int mode: permissions (posix-style) for the newly-created folder
        """
        path = self._adjust_cwd(path)
        self._log(DEBUG, 'mkdir(%r, %r)' % (path, mode))
        attr = SFTPAttributes()
        attr.st_mode = mode
        self._request(CMD_MKDIR, path, attr)
예제 #9
0
    def mkdir(self, path, mode=0777):
        """
        Create a folder (directory) named C{path} with numeric mode C{mode}.
        The default mode is 0777 (octal).  On some systems, mode is ignored.
        Where it is used, the current umask value is first masked out.

        @param path: name of the folder to create.
        @type path: string
        @param mode: permissions (posix-style) for the newly-created folder.
        @type mode: int
        """
        path = self._adjust_cwd(path)
        attr = SFTPAttributes()
        attr.st_mode = mode
        self._request(CMD_MKDIR, path, attr)
예제 #10
0
    def chmod(self, path, mode):
        """
        Change the mode (permissions) of a file.  The permissions are
        unix-style and identical to those used by python's C{os.chmod}
        function.

        @param path: path of the file to change the permissions of.
        @type path: string
        @param mode: new permissions.
        @type mode: int
        """
        path = self._adjust_cwd(path)
        attr = SFTPAttributes()
        attr.st_mode = mode
        self._request(CMD_SETSTAT, path, attr)
예제 #11
0
    def chown(self, path, uid, gid):
        """
        Change the owner (``uid``) and group (``gid``) of a file.  As with
        Python's `os.chown` function, you must pass both arguments, so if you
        only want to change one, use `stat` first to retrieve the current
        owner and group.

        :param str path: path of the file to change the owner and group of
        :param int uid: new owner's uid
        :param int gid: new group id
        """
        path = self._adjust_cwd(path)
        self._log(DEBUG, 'chown(%r, %r, %r)' % (path, uid, gid))
        attr = SFTPAttributes()
        attr.st_uid, attr.st_gid = uid, gid
        self._request(CMD_SETSTAT, path, attr)
예제 #12
0
    def stat(self, path):
        """
        Retrieve information about a file on the remote system.  The return
        value is an object whose attributes correspond to the attributes of
        python's C{stat} structure as returned by C{os.stat}, except that it
        contains fewer fields.  An SFTP server may return as much or as little
        info as it wants, so the results may vary from server to server.

        Unlike a python C{stat} object, the result may not be accessed as a
        tuple.  This is mostly due to the author's slack factor.

        The fields supported are: C{st_mode}, C{st_size}, C{st_uid}, C{st_gid},
        C{st_atime}, and C{st_mtime}.

        @param path: the filename to stat
        @type path: str
        @return: an object containing attributes about the given file
        @rtype: SFTPAttributes
        """
        path = self._adjust_cwd(path)
        self._log(DEBUG, 'stat(%r)' % path)
        t, msg = self._request(CMD_STAT, path)
        if t != CMD_ATTRS:
            raise SFTPError('Expected attributes')
        return SFTPAttributes._from_msg(msg)
예제 #13
0
    def stat(self, path):
        """
        Retrieve information about a file on the remote system.  The return
        value is an object whose attributes correspond to the attributes of
        Python's ``stat`` structure as returned by ``os.stat``, except that it
        contains fewer fields.  An SFTP server may return as much or as little
        info as it wants, so the results may vary from server to server.

        Unlike a Python `python:stat` object, the result may not be accessed as
        a tuple.  This is mostly due to the author's slack factor.

        The fields supported are: ``st_mode``, ``st_size``, ``st_uid``,
        ``st_gid``, ``st_atime``, and ``st_mtime``.

        :param str path: the filename to stat
        :return:
            an `.SFTPAttributes` object containing attributes about the given
            file
        """
        path = self._adjust_cwd(path)
        self._log(DEBUG, 'stat({!r})'.format(path))
        t, msg = self._request(CMD_STAT, path)
        if t != CMD_ATTRS:
            raise SFTPError('Expected attributes')
        return SFTPAttributes._from_msg(msg)
예제 #14
0
    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.

        @param path: path to list (defaults to C{'.'})
        @type path: str
        @return: list of attributes
        @rtype: list of L{SFTPAttributes}
        
        @since: 1.2
        """
        path = self._adjust_cwd(path)
        t, msg = self._request(CMD_OPENDIR, path)
        if t != CMD_HANDLE:
            raise SFTPError('Expected handle')
        handle = msg.get_string()
        filelist = []
        while True:
            try:
                t, msg = self._request(CMD_READDIR, handle)
            except EOFError, e:
                # done with handle
                break
            if t != CMD_NAME:
                raise SFTPError('Expected name response')
            count = msg.get_int()
            for i in range(count):
                filename = _to_unicode(msg.get_string())
                longname = _to_unicode(msg.get_string())
                attr = SFTPAttributes._from_msg(msg, filename)
                if (filename != '.') and (filename != '..'):
                    filelist.append(attr)
예제 #15
0
    def chown(self, uid, gid):
        """
        Change the owner (C{uid}) and group (C{gid}) of this file.  As with
        python's C{os.chown} function, you must pass both arguments, so if you
        only want to change one, use L{stat} first to retrieve the current
        owner and group.

        @param uid: new owner's uid
        @type uid: int
        @param gid: new group id
        @type gid: int
        """
        self.sftp._log(DEBUG, 'chown(%s, %r, %r)' % (hexlify(self.handle), uid, gid))
        attr = SFTPAttributes()
        attr.st_uid, attr.st_gid = uid, gid
        self.sftp._request(CMD_FSETSTAT, self.handle, attr)
예제 #16
0
    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
        @type size: int or long
        """
        path = self._adjust_cwd(path)
        self._log(DEBUG, 'truncate(%r, %r)' % (path, size))
        attr = SFTPAttributes()
        attr.st_size = size
        self._request(CMD_SETSTAT, path, attr)
예제 #17
0
    def chown(self, path, uid, gid):
        """
        Change the owner (C{uid}) and group (C{gid}) of a file.  As with
        python's C{os.chown} function, you must pass both arguments, so if you
        only want to change one, use L{stat} first to retrieve the current
        owner and group.

        @param path: path of the file to change the owner and group of.
        @type path: string
        @param uid: new owner's uid
        @type uid: int
        @param gid: new group id
        @type gid: int
        """
        path = self._adjust_cwd(path)
        attr = SFTPAttributes()
        attr.st_uid, attr.st_gid = uid, gid
        self._request(CMD_SETSTAT, path, attr)
예제 #18
0
    def utime(self, times):
        """
        Set the access and modified times of this file.  If
        C{times} is C{None}, then the file's access and modified times are set
        to the current time.  Otherwise, C{times} must be a 2-tuple of numbers,
        of the form C{(atime, mtime)}, which is used to set the access and
        modified times, respectively.  This bizarre API is mimicked from python
        for the sake of consistency -- I apologize.

        @param times: C{None} or a tuple of (access time, modified time) in
            standard internet epoch time (seconds since 01 January 1970 GMT)
        @type times: tuple(int)
        """
        if times is None:
            times = (time.time(), time.time())
        self.sftp._log(DEBUG, 'utime(%s, %r)' % (hexlify(self.handle), times))
        attr = SFTPAttributes()
        attr.st_atime, attr.st_mtime = times
        self.sftp._request(CMD_FSETSTAT, self.handle, attr)
예제 #19
0
    def utime(self, path, times):
        """
        Set the access and modified times of the file specified by C{path}.  If
        C{times} is C{None}, then the file's access and modified times are set
        to the current time.  Otherwise, C{times} must be a 2-tuple of numbers,
        of the form C{(atime, mtime)}, which is used to set the access and
        modified times, respectively.  This bizarre API is mimicked from python
        for the sake of consistency -- I apologize.

        @param path: path of the file to modify.
        @type path: string
        @param times: C{None} or a tuple of (access time, modified time) in
            standard internet epoch time (seconds since 01 January 1970 GMT).
        @type times: tuple of int
        """
        path = self._adjust_cwd(path)
        if times is None:
            times = (time.time(), time.time())
        attr = SFTPAttributes()
        attr.st_atime, attr.st_mtime = times
        self._request(CMD_SETSTAT, path, attr)
예제 #20
0
    def utime(self, path, times):
        """
        Set the access and modified times of the file specified by C{path}.  If
        C{times} is C{None}, then the file's access and modified times are set
        to the current time.  Otherwise, C{times} must be a 2-tuple of numbers,
        of the form C{(atime, mtime)}, which is used to set the access and
        modified times, respectively.  This bizarre API is mimicked from python
        for the sake of consistency -- I apologize.

        @param path: path of the file to modify.
        @type path: string
        @param times: C{None} or a tuple of (access time, modified time) in
            standard internet epoch time (seconds since 01 January 1970 GMT).
        @type times: tuple of int
        """
        path = self._adjust_cwd(path)
        if times is None:
            times = (time.time(), time.time())
        attr = SFTPAttributes()
        attr.st_atime, attr.st_mtime = times
        self._request(CMD_SETSTAT, path, attr)
예제 #21
0
    def put(self, localpath, remotepath, callback=None, confirm=True):
        """
        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
        @type remotepath: str
        @param callback: optional callback function that accepts the bytes
            transferred so far and the total bytes to be transferred
            (since 1.7.4)
        @type callback: function(int, int)
        @param confirm: whether to do a stat() on the file afterwards to
            confirm the file size (since 1.7.7)
        @type confirm: bool

        @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
        fl = file(localpath, 'rb')
        try:
            fr = self.file(remotepath, 'wb')
            fr.set_pipelined(True)
            size = 0
            try:
                while True:
                    data = fl.read(32768)
                    if len(data) == 0:
                        break
                    fr.write(data)
                    size += len(data)
                    if callback is not None:
                        callback(size, file_size)
            finally:
                fr.close()
        finally:
            fl.close()
        if confirm:
            s = self.stat(remotepath)
            if s.st_size != size:
                raise IOError('size mismatch in put!  %d != %d' %
                              (s.st_size, size))
        else:
            s = SFTPAttributes()
        return s
예제 #22
0
    def utime(self, path, times):
        """
        Set the access and modified times of the file specified by ``path``.
        If ``times`` is ``None``, then the file's access and modified times
        are set to the current time.  Otherwise, ``times`` must be a 2-tuple
        of numbers, of the form ``(atime, mtime)``, which is used to set the
        access and modified times, respectively.  This bizarre API is mimicked
        from Python for the sake of consistency -- I apologize.

        :param str path: path of the file to modify
        :param tuple times:
            ``None`` or a tuple of (access time, modified time) in standard
            internet epoch time (seconds since 01 January 1970 GMT)
        """
        path = self._adjust_cwd(path)
        if times is None:
            times = (time.time(), time.time())
        self._log(DEBUG, 'utime(%r, %r)' % (path, times))
        attr = SFTPAttributes()
        attr.st_atime, attr.st_mtime = times
        self._request(CMD_SETSTAT, path, attr)
예제 #23
0
    def stat(self):
        """
        Retrieve information about this file from the remote system.  This is
        exactly like `.SFTPClient.stat`, except that it operates on an
        already-open file.

        :return: an `.SFTPAttributes` object containing attributes about this file.
        """
        t, msg = self.sftp._request(CMD_FSTAT, self.handle)
        if t != CMD_ATTRS:
            raise SFTPError('Expected attributes')
        return SFTPAttributes._from_msg(msg)
예제 #24
0
    def open(self, filename, mode='r', bufsize=-1):
        """
        Open a file on the remote server.  The arguments are the same as for
        Python's built-in `python:file` (aka `python:open`).  A file-like
        object is returned, which closely mimics the behavior of a normal
        Python file object, including the ability to be used as a context
        manager.

        The mode indicates how the file is to be opened: ``'r'`` for reading,
        ``'w'`` for writing (truncating an existing file), ``'a'`` for
        appending, ``'r+'`` for reading/writing, ``'w+'`` for reading/writing
        (truncating an existing file), ``'a+'`` for reading/appending.  The
        Python ``'b'`` flag is ignored, since SSH treats all files as binary.
        The ``'U'`` flag is supported in a compatible way.

        Since 1.5.2, an ``'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
        ``O_EXCL`` flag in posix.

        The file will be buffered in standard Python style by default, but
        can be altered with the ``bufsize`` parameter.  ``0`` turns off
        buffering, ``1`` uses line buffering, and any number greater than 1
        (``>1``) uses that specific buffer size.

        :param str filename: name of the file to open
        :param str mode: mode (Python-style) to open in
        :param int bufsize: desired buffering (-1 = default buffer size)
        :return: an `.SFTPFile` object representing the open file

        :raises IOError: if the file could not be opened.
        """
        filename = self._adjust_cwd(filename)
        self._log(DEBUG, 'open(%r, %r)' % (filename, mode))
        imode = 0
        if ('r' in mode) or ('+' in mode):
            imode |= SFTP_FLAG_READ
        if ('w' in mode) or ('+' in mode) or ('a' in mode):
            imode |= SFTP_FLAG_WRITE
        if 'w' in mode:
            imode |= SFTP_FLAG_CREATE | SFTP_FLAG_TRUNC
        if 'a' in mode:
            imode |= SFTP_FLAG_CREATE | SFTP_FLAG_APPEND
        if 'x' in mode:
            imode |= SFTP_FLAG_CREATE | SFTP_FLAG_EXCL
        attrblock = SFTPAttributes()
        t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
        if t != CMD_HANDLE:
            raise SFTPError('Expected handle')
        handle = msg.get_binary()
        self._log(DEBUG,
                  'open(%r, %r) -> %s' % (filename, mode, hexlify(handle)))
        return SFTPFile(self, handle, mode, bufsize)
예제 #25
0
    def file(self, filename, mode='r', bufsize=-1):
        """
        Open a file on the remote server.  The arguments are the same as for
        python's built-in C{file} (aka C{open}).  A file-like object is
        returned, which closely mimics the behavior of a normal python file
        object.

        The mode indicates how the file is to be opened: C{'r'} for reading,
        C{'w'} for writing (truncating an existing file), C{'a'} for appending,
        C{'r+'} for reading/writing, C{'w+'} for reading/writing (truncating an
        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
        C{O_EXCL} flag in posix.

        The file will be buffered in standard python style by default, but
        can be altered with the C{bufsize} parameter.  C{0} turns off
        buffering, C{1} uses line buffering, and any number greater than 1
        (C{>1}) uses that specific buffer size.

        @param filename: name of the file to open.
        @type filename: string
        @param mode: mode (python-style) to open in.
        @type mode: string
        @param bufsize: desired buffering (-1 = default buffer size)
        @type bufsize: int
        @return: a file object representing the open file.
        @rtype: SFTPFile

        @raise IOError: if the file could not be opened.
        """
        filename = self._adjust_cwd(filename)
        imode = 0
        if ('r' in mode) or ('+' in mode):
            imode |= SFTP_FLAG_READ
        if ('w' in mode) or ('+' in mode) or ('a' in mode):
            imode |= SFTP_FLAG_WRITE
        if ('w' in mode):
            imode |= SFTP_FLAG_CREATE | SFTP_FLAG_TRUNC
        if ('a' in mode):
            imode |= SFTP_FLAG_CREATE | SFTP_FLAG_APPEND
        if ('x' in mode):
            imode |= SFTP_FLAG_CREATE | SFTP_FLAG_EXCL
        attrblock = SFTPAttributes()
        t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
        if t != CMD_HANDLE:
            raise SFTPError('Expected handle')
        handle = msg.get_string()
        return SFTPFile(self, handle, mode, bufsize)
예제 #26
0
파일: sftp_file.py 프로젝트: OmniDB/OmniDB
    def utime(self, times):
        """
        Set the access and modified times of this file.  If
        ``times`` is ``None``, then the file's access and modified times are
        set to the current time.  Otherwise, ``times`` must be a 2-tuple of
        numbers, of the form ``(atime, mtime)``, which is used to set the
        access and modified times, respectively.  This bizarre API is mimicked
        from Python for the sake of consistency -- I apologize.

        :param tuple times:
            ``None`` or a tuple of (access time, modified time) in standard
            internet epoch time (seconds since 01 January 1970 GMT)
        """
        if times is None:
            times = (time.time(), time.time())
        self.sftp._log(
            DEBUG, "utime({}, {!r})".format(hexlify(self.handle), times)
        )
        attr = SFTPAttributes()
        attr.st_atime, attr.st_mtime = times
        self.sftp._request(CMD_FSETSTAT, self.handle, attr)
예제 #27
0
    def stat(self):
        """
        Retrieve information about this file from the remote system.  This is
        exactly like `.SFTPClient.stat`, except that it operates on an
        already-open file.

        :return: an `.SFTPAttributes` object containing attributes about this file.
        """
        t, msg = self.sftp._request(CMD_FSTAT, self.handle)
        if t != CMD_ATTRS:
            raise SFTPError('Expected attributes')
        return SFTPAttributes._from_msg(msg)
예제 #28
0
    def utime(self, path, times):
        """
        Set the access and modified times of the file specified by ``path``.  If
        ``times`` is ``None``, then the file's access and modified times are set
        to the current time.  Otherwise, ``times`` must be a 2-tuple of numbers,
        of the form ``(atime, mtime)``, which is used to set the access and
        modified times, respectively.  This bizarre API is mimicked from Python
        for the sake of consistency -- I apologize.

        :param str path: path of the file to modify
        :param tuple times:
            ``None`` or a tuple of (access time, modified time) in standard
            internet epoch time (seconds since 01 January 1970 GMT)
        """
        path = self._adjust_cwd(path)
        if times is None:
            times = (time.time(), time.time())
        self._log(DEBUG, 'utime(%r, %r)' % (path, times))
        attr = SFTPAttributes()
        attr.st_atime, attr.st_mtime = times
        self._request(CMD_SETSTAT, path, attr)
예제 #29
0
    def stat(self):
        if self.content_provider.get(self.path) is None:
            return SFTP_NO_SUCH_FILE

        mtime = calendar.timegm(datetime.now().timetuple())

        sftp_attrs = SFTPAttributes()
        sftp_attrs.st_size = self.content_provider.get_size(self.path)
        sftp_attrs.st_uid = 0
        sftp_attrs.st_gid = 0
        sftp_attrs.st_mode = (stat.S_IRWXO
                              | stat.S_IRWXG
                              | stat.S_IRWXU
                              | (stat.S_IFDIR if self.content_provider.is_dir(
                                  self.path) else stat.S_IFREG))
        sftp_attrs.st_atime = mtime
        sftp_attrs.st_mtime = mtime
        sftp_attrs.filename = posixpath.basename(self.path)
        return sftp_attrs
예제 #30
0
    def putfo(self, fl, remotepath, file_size=0, callback=None, confirm=True):
        """
        Copy the contents of an open file object (``fl``) to the SFTP server as
        ``remotepath``. Any exception raised by operations will be passed
        through.

        The SFTP operations use pipelining for speed.

        :param file fl: opened file or file-like object to copy
        :param str remotepath: the destination path on the SFTP server
        :param int file_size:
            optional size parameter passed to callback. If none is specified,
            size defaults to 0
        :param callable callback:
            optional callback function (form: ``func(int, int)``) that accepts
            the bytes transferred so far and the total bytes to be transferred
            (since 1.7.4)
        :param bool confirm:
            whether to do a stat() on the file afterwards to confirm the file
            size (since 1.7.7)

        :return:
            an `.SFTPAttributes` object containing attributes about the given
            file.

        .. versionadded:: 1.4
        .. versionchanged:: 1.7.4
            Began returning rich attribute objects.
        """
        fr = self.file(remotepath, 'wb')
        fr.set_pipelined(True)
        size = 0
        try:
            while True:
                data = fl.read(32768)
                fr.write(data)
                size += len(data)
                if callback is not None:
                    callback(size, file_size)
                if len(data) == 0:
                    break
        finally:
            fr.close()
        if confirm:
            s = self.stat(remotepath)
            if s.st_size != size:
                raise IOError('size mismatch in put!  %d != %d' % (s.st_size, size))
        else:
            s = SFTPAttributes()
        return s
예제 #31
0
    def putfo(self, fl, remotepath, file_size=0, callback=None, confirm=True):
        """
        Copy the contents of an open file object (``fl``) to the SFTP server as
        ``remotepath``. Any exception raised by operations will be passed
        through.

        The SFTP operations use pipelining for speed.

        :param fl: opened file or file-like object to copy
        :param str remotepath: the destination path on the SFTP server
        :param int file_size:
            optional size parameter passed to callback. If none is specified,
            size defaults to 0
        :param callable callback:
            optional callback function (form: ``func(int, int)``) that accepts
            the bytes transferred so far and the total bytes to be transferred
            (since 1.7.4)
        :param bool confirm:
            whether to do a stat() on the file afterwards to confirm the file
            size (since 1.7.7)

        :return:
            an `.SFTPAttributes` object containing attributes about the given
            file.

        .. versionadded:: 1.10
        """
        with self.file(remotepath, 'wb') as fr:
            fr.set_pipelined(True)
            size = self._transfer_with_callback(reader=fl,
                                                writer=fr,
                                                file_size=file_size,
                                                callback=callback)
        if confirm:
            try:
                s = self.stat(remotepath)
            except IOError as e:
                if e.errno == SFTP_NO_SUCH_FILE:
                    raise IOError(
                        SFTP_NO_SUCH_FILE, "STAT failed after successful PUT,"
                        " possibly because the server deleted or moved the file."
                        " STAT is skipped if confirm=false. Original error: " +
                        str(e))
                raise
            if s.st_size != size:
                raise IOError('size mismatch in put!  {} != {}'.format(
                    s.st_size, size))
        else:
            s = SFTPAttributes()
        return s
예제 #32
0
    def putfo(self, fl, remotepath, file_size=0, callback=None, confirm=True):
        """
        Copy the contents of an open file object (C{fl}) to the SFTP server as
        C{remotepath}. Any exception raised by operations will be passed through.

        The SFTP operations use pipelining for speed.

        @param fl: opened file or file-like object to copy
        @type localpath: object
        @param remotepath: the destination path on the SFTP server
        @type remotepath: str
        @param file_size: optional size parameter passed to callback. If none is
            specified, size defaults to 0
        @type file_size: int
        @param callback: optional callback function that accepts the bytes
            transferred so far and the total bytes to be transferred
            (since 1.7.4)
        @type callback: function(int, int)
        @param confirm: whether to do a stat() on the file afterwards to
            confirm the file size (since 1.7.7)
        @type confirm: bool

        @return: an object containing attributes about the given file
            (since 1.7.4)
        @rtype: SFTPAttributes

        @since: 1.4
        """
        fr = self.file(remotepath, 'wb')
        fr.set_pipelined(True)
        size = 0
        try:
            while True:
                data = fl.read(32768)
                fr.write(data)
                size += len(data)
                if callback is not None:
                    callback(size, file_size)
                if len(data) == 0:
                    break
        finally:
            fr.close()
        if confirm:
            s = self.stat(remotepath)
            if s.st_size != size:
                raise IOError('size mismatch in put!  %d != %d' %
                              (s.st_size, size))
        else:
            s = SFTPAttributes()
        return s
예제 #33
0
    def _sftp_open_exclusive(self, abspath, mode=None):
        """Open a remote path exclusively.

        SFTP supports O_EXCL (SFTP_FLAG_EXCL), which fails if
        the file already exists. However it does not expose this
        at the higher level of SFTPClient.open(), so we have to
        sneak away with it.

        WARNING: This breaks the SFTPClient abstraction, so it
        could easily break against an updated version of paramiko.

        :param abspath: The remote absolute path where the file should be opened
        :param mode: The mode permissions bits for the new file
        """
        # TODO: jam 20060816 Paramiko >= 1.6.2 (probably earlier) supports
        #       using the 'x' flag to indicate SFTP_FLAG_EXCL.
        #       However, there is no way to set the permission mode at open
        #       time using the sftp_client.file() functionality.
        path = self._get_sftp()._adjust_cwd(abspath)
        # mutter('sftp abspath %s => %s', abspath, path)
        attr = SFTPAttributes()
        if mode is not None:
            attr.st_mode = mode
        omode = (SFTP_FLAG_WRITE | SFTP_FLAG_CREATE
                 | SFTP_FLAG_TRUNC | SFTP_FLAG_EXCL)
        try:
            t, msg = self._get_sftp()._request(CMD_OPEN, path, omode, attr)
            if t != CMD_HANDLE:
                raise TransportError('Expected an SFTP handle')
            handle = msg.get_string()
            return SFTPFile(self._get_sftp(), handle, 'wb', -1)
        except (paramiko.SSHException, IOError), e:
            self._translate_io_exception(e,
                                         abspath,
                                         ': unable to open',
                                         failure_exc=FileExists)
예제 #34
0
    def lstat(self, path):
        """
        Retrieve information about a file on the remote system, without
        following symbolic links (shortcuts).  This otherwise behaves exactly
        the same as L{stat}.

        @param path: the filename to stat.
        @type path: string
        @return: an object containing attributes about the given file.
        @rtype: SFTPAttributes
        """
        path = self._adjust_cwd(path)
        t, msg = self._request(CMD_LSTAT, path)
        if t != CMD_ATTRS:
            raise SFTPError('Expected attributes')
        return SFTPAttributes._from_msg(msg)
예제 #35
0
    def lstat(self, path):
        """
        Retrieve information about a file on the remote system, without
        following symbolic links (shortcuts).  This otherwise behaves exactly
        the same as L{stat}.

        @param path: the filename to stat.
        @type path: string
        @return: an object containing attributes about the given file.
        @rtype: SFTPAttributes
        """
        path = self._adjust_cwd(path)
        t, msg = self._request(CMD_LSTAT, path)
        if t != CMD_ATTRS:
            raise SFTPError('Expected attributes')
        return SFTPAttributes._from_msg(msg)
예제 #36
0
    def listdir_attr(self, path=".", encoding="utf-8", path_encoding='utf-8'):
        """
        Return a list containing `.SFTPAttributes` objects corresponding to
        files in the given ``path``.  The list is in arbitrary order.  It does
        not include the special entries ``'.'`` and ``'..'`` even if they are
        present in the folder.

        The returned `.SFTPAttributes` objects will each have an additional
        field: ``longname``, which may contain a formatted string of the file's
        attributes, in unix format.  The content of this string will probably
        depend on the SFTP server implementation.
        
        :param str encoding: the byte decode format (defauls to ```'utf-8'```),used to decode filename(under the path) byte ,eg: ```'中文目录'```
        :param str encoding: 设置服务器中返回byte的解码格式,当服务器返回内容中有使用其他格式编码的内容时(如目录下包含有中文)需要设置此参数,参数值建议为服务器使用的编码格式,如: 'GB180303'编码的中文,此时建议使用方式: listdir_attr(path='/test', encoding='GB18030')
        :param str path: path to list (defaults to ``'.'``)
        :param str path_encoding: the path parameter encode format (defauls to ```'utf-8'```),eg: listdir_attr(path='中文目录'.encode('GB18030'), path_encoding='GB18030')
        :param str path_encoding: 当需要查看的路径path在服务器上的编码格式不是utf-8时需要设置此参数,参数值为服务器所使用的编码格式,如需要查看的路径是'/test/中文路径',而服务器使用的编码格式是GB18030, 此时就建议如下使用方式: listdir_attr(path="/test/中文路径".encode("GB18030"), path_encoding='GB18030')
        :return: list of `.SFTPAttributes` objects

        .. versionadded:: 1.2
        """
        if path == '.' and self.getcwd() is not None:
            path = self.getcwd().encode(path_encoding)
        path = self._adjust_cwd(path)
        self._log(DEBUG, "listdir({!r})".format(path))
        t, msg = self._request(CMD_OPENDIR, path)
        if t != CMD_HANDLE:
            raise SFTPError("Expected handle")
        handle = msg.get_binary()
        filelist = []
        while True:
            try:
                t, msg = self._request(CMD_READDIR, handle)
            except EOFError:
                # done with handle
                break
            if t != CMD_NAME:
                raise SFTPError("Expected name response")
            count = msg.get_int()
            for i in range(count):
                filename = msg.get_text(encoding)
                longname = msg.get_text(encoding)
                attr = SFTPAttributes._from_msg(msg, filename, longname)
                if (filename != ".") and (filename != ".."):
                    filelist.append(attr)
        self._request(CMD_CLOSE, handle)
        return filelist
예제 #37
0
    def lstat(self, path):
        """
        Retrieve information about a file on the remote system, without
        following symbolic links (shortcuts).  This otherwise behaves exactly
        the same as `stat`.

        :param str path: the filename to stat
        :return:
            an `.SFTPAttributes` object containing attributes about the given
            file
        """
        path = self._adjust_cwd(path)
        self._log(DEBUG, 'lstat(%r)' % path)
        t, msg = self._request(CMD_LSTAT, path)
        if t != CMD_ATTRS:
            raise SFTPError('Expected attributes')
        return SFTPAttributes._from_msg(msg)
예제 #38
0
    def lstat(self, path):
        """
        Retrieve information about a file on the remote system, without
        following symbolic links (shortcuts).  This otherwise behaves exactly
        the same as `stat`.

        :param str path: the filename to stat
        :return:
            an `.SFTPAttributes` object containing attributes about the given
            file
        """
        path = self._adjust_cwd(path)
        self._log(DEBUG, 'lstat(%r)' % path)
        t, msg = self._request(CMD_LSTAT, path)
        if t != CMD_ATTRS:
            raise SFTPError('Expected attributes')
        return SFTPAttributes._from_msg(msg)
예제 #39
0
    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
        depend on the SFTP server implementation.

        @param path: path to list (defaults to C{'.'})
        @type path: str
        @return: list of attributes
        @rtype: list of L{SFTPAttributes}

        @since: 1.2
        """
        path = self._adjust_cwd(path)
        self._log(DEBUG, 'listdir(%r)' % path)
        t, msg = self._request(CMD_OPENDIR, path)
        if t != CMD_HANDLE:
            raise SFTPError('Expected handle')
        handle = msg.get_binary()
        filelist = []
        while True:
            try:
                t, msg = self._request(CMD_READDIR, handle)
            except EOFError:
                # done with handle
                break
            if t != CMD_NAME:
                raise SFTPError('Expected name response')
            count = msg.get_int()
            for i in range(count):
                filename = msg.get_text()
                longname = msg.get_text()
                attr = SFTPAttributes._from_msg(msg, filename, longname)
                if (filename != '.') and (filename != '..'):
                    filelist.append(attr)
        self._request(CMD_CLOSE, handle)
        return filelist
예제 #40
0
    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
        depend on the SFTP server implementation.

        @param path: path to list (defaults to C{'.'})
        @type path: str
        @return: list of attributes
        @rtype: list of L{SFTPAttributes}

        @since: 1.2
        """
        path = self._adjust_cwd(path)
        self._log(DEBUG, 'listdir(%r)' % path)
        t, msg = self._request(CMD_OPENDIR, path)
        if t != CMD_HANDLE:
            raise SFTPError('Expected handle')
        handle = msg.get_binary()
        filelist = []
        while True:
            try:
                t, msg = self._request(CMD_READDIR, handle)
            except EOFError:
                # done with handle
                break
            if t != CMD_NAME:
                raise SFTPError('Expected name response')
            count = msg.get_int()
            for i in range(count):
                filename = msg.get_text()
                longname = msg.get_text()
                attr = SFTPAttributes._from_msg(msg, filename, longname)
                if (filename != '.') and (filename != '..'):
                    filelist.append(attr)
        self._request(CMD_CLOSE, handle)
        return filelist
예제 #41
0
def put(sftp, localpath, remotepath, callback=None, confirm=True):

    file_size = os.stat(localpath).st_size
    fl = file(localpath, 'rb')
    try:
        size = 0
        fr = sftp.file(remotepath, 'ab')
        s = sftp.stat(remotepath)
        frSize = s.st_size
        print frSize, 'frSizefrSizefrSize'
        fr.set_pipelined(True)
        size = frSize
        print size, ' frSize'
        fl.seek(size)
        fr.seek(size)
        try:
            while True:
                data = fl.read(32768)
                if len(data) == 0:
                    break
                fr.write(data)
                size += len(data)
                if callback is not None:
                    callback(size, file_size)
        finally:
            print size, ' frfrsize'
            fr.seek(size)
            fr.close()
    finally:
        print size, '  flflsize'
        fl.seek(size)
        fl.close()
    if confirm:
        s = sftp.stat(remotepath)
        if s.st_size != size:
            return False
        elif file_size == s.st_size:
            return True
        else:
            return False
    else:
        s = SFTPAttributes()
    return s
예제 #42
0
    def open(self, filename, mode="r"):
        """
        Open a remote file, ``filename``, on the server for reading
        or writing.

        Args:
            filename (str): name of remote file to open
            mode (str): mode to open file in
        """
        filename = self.encode_path(filename)
        pflags = 0
        if "r" in mode:
            pflags |= SFTP_FLAG_READ
        if "w" in mode:
            pflags |= SFTP_FLAG_WRITE | SFTP_FLAG_CREATE | SFTP_FLAG_TRUNC
        attrs = SFTPAttributes()
        resp_type, msg = self._blocking_request(CMD_OPEN, filename, pflags, attrs)
        if resp_type != CMD_HANDLE:
            raise SFTPError("Expected remote file handle")
        return msg.get_binary()
예제 #43
0
    def listdir_attr(self, path='.'):
        """
        Return a list containing `.SFTPAttributes` objects corresponding to
        files in the given ``path``.  The list is in arbitrary order.  It does
        not include the special entries ``'.'`` and ``'..'`` even if they are
        present in the folder.

        The returned `.SFTPAttributes` objects will each have an additional
        field: ``longname``, which may contain a formatted string of the file's
        attributes, in unix format.  The content of this string will probably
        depend on the SFTP server implementation.

        :param str path: path to list (defaults to ``'.'``)
        :return: list of `.SFTPAttributes` objects

        .. versionadded:: 1.2
        """
        path = self._adjust_cwd(path)
        self._log(DEBUG, 'listdir({!r})'.format(path))
        t, msg = self._request(CMD_OPENDIR, path)
        if t != CMD_HANDLE:
            raise SFTPError('Expected handle')
        handle = msg.get_binary()
        filelist = []
        while True:
            try:
                t, msg = self._request(CMD_READDIR, handle)
            except EOFError:
                # done with handle
                break
            if t != CMD_NAME:
                raise SFTPError('Expected name response')
            count = msg.get_int()
            for i in range(count):
                filename = msg.get_text()
                longname = msg.get_text()
                attr = SFTPAttributes._from_msg(msg, filename, longname)
                if (filename != '.') and (filename != '..'):
                    filelist.append(attr)
        self._request(CMD_CLOSE, handle)
        return filelist
예제 #44
0
    def listdir_attr(self, path="."):
        """
        Return a list containing `.SFTPAttributes` objects corresponding to
        files in the given ``path``.  The list is in arbitrary order.  It does
        not include the special entries ``'.'`` and ``'..'`` even if they are
        present in the folder.

        The returned `.SFTPAttributes` objects will each have an additional
        field: ``longname``, which may contain a formatted string of the file's
        attributes, in unix format.  The content of this string will probably
        depend on the SFTP server implementation.

        :param str path: path to list (defaults to ``'.'``)
        :return: list of `.SFTPAttributes` objects

        .. versionadded:: 1.2
        """
        path = self._adjust_cwd(path)
        self._log(DEBUG, "listdir({!r})".format(path))
        t, msg = self._request(CMD_OPENDIR, path)
        if t != CMD_HANDLE:
            raise SFTPError("Expected handle")
        handle = msg.get_binary()
        filelist = []
        while True:
            try:
                t, msg = self._request(CMD_READDIR, handle)
            except EOFError:
                # done with handle
                break
            if t != CMD_NAME:
                raise SFTPError("Expected name response")
            count = msg.get_int()
            for i in range(count):
                filename = msg.get_text()
                longname = msg.get_text()
                attr = SFTPAttributes._from_msg(msg, filename, longname)
                if (filename != ".") and (filename != ".."):
                    filelist.append(attr)
        self._request(CMD_CLOSE, handle)
        return filelist
예제 #45
0
    def stat(self):
        if self.content_provider.get(self.path) is None:
            return SFTP_NO_SUCH_FILE

        mtime = calendar.timegm(datetime.now().timetuple())

        sftp_attrs = SFTPAttributes()
        sftp_attrs.st_size = self.content_provider.get_size(self.path)
        sftp_attrs.st_uid = 0
        sftp_attrs.st_gid = 0
        sftp_attrs.st_mode = (
            stat.S_IRWXO |
            stat.S_IRWXG |
            stat.S_IRWXU |
            (
                stat.S_IFDIR
                if self.content_provider.is_dir(self.path)
                else stat.S_IFREG
            )
        )
        sftp_attrs.st_atime = mtime
        sftp_attrs.st_mtime = mtime
        sftp_attrs.filename = os.path.basename(self.path)
        return sftp_attrs
예제 #46
0
    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.

        @param path: path to list (defaults to C{'.'})
        @type path: str
        @return: list of attributes
        @rtype: list of L{SFTPAttributes}
        
        @since: 1.2
        """
        path = self._adjust_cwd(path)
        self._log(DEBUG, 'listdir(%r)' % path)
        t, msg = self._request(CMD_OPENDIR, path)
        if t != CMD_HANDLE:
            raise SFTPError('Expected handle')
        handle = msg.get_string()
        filelist = []
        while True:
            try:
                t, msg = self._request(CMD_READDIR, handle)
            except EOFError, e:
                # done with handle
                break
            if t != CMD_NAME:
                raise SFTPError('Expected name response')
            count = msg.get_int()
            for i in range(count):
                filename = _to_unicode(msg.get_string())
                longname = _to_unicode(msg.get_string())
                attr = SFTPAttributes._from_msg(msg, filename)
                if (filename != '.') and (filename != '..'):
                    filelist.append(attr)
예제 #47
0
    def stat(self, path):
        """
        Retrieve information about a file on the remote system.  The return
        value is an object whose attributes correspond to the attributes of
        python's C{stat} structure as returned by C{os.stat}, except that it
        contains fewer fields.  An SFTP server may return as much or as little
        info as it wants, so the results may vary from server to server.

        Unlike a python C{stat} object, the result may not be accessed as a
        tuple.  This is mostly due to the author's slack factor.

        The fields supported are: C{st_mode}, C{st_size}, C{st_uid}, C{st_gid},
        C{st_atime}, and C{st_mtime}.

        @param path: the filename to stat.
        @type path: string
        @return: an object containing attributes about the given file.
        @rtype: SFTPAttributes
        """
        path = self._adjust_cwd(path)
        t, msg = self._request(CMD_STAT, path)
        if t != CMD_ATTRS:
            raise SFTPError('Expected attributes')
        return SFTPAttributes._from_msg(msg)
예제 #48
0
    def listdir_iter(self, path='.', read_aheads=50):
        """
        Generator version of `.listdir_attr`.

        See the API docs for `.listdir_attr` for overall details.

        This function adds one more kwarg on top of `.listdir_attr`:
        ``read_aheads``, an integer controlling how many
        ``SSH_FXP_READDIR`` requests are made to the server. The default of 50
        should suffice for most file listings as each request/response cycle
        may contain multiple files (dependent on server implementation.)

        .. versionadded:: 1.15
        """
        path = self._adjust_cwd(path)
        self._log(DEBUG, 'listdir(%r)' % path)
        t, msg = self._request(CMD_OPENDIR, path)

        if t != CMD_HANDLE:
            raise SFTPError('Expected handle')

        handle = msg.get_string()

        nums = list()
        while True:
            try:
                # Send out a bunch of readdir requests so that we can read the
                # responses later on Section 6.7 of the SSH file transfer RFC
                # explains this
                # http://filezilla-project.org/specs/draft-ietf-secsh-filexfer-02.txt
                for i in range(read_aheads):
                    num = self._async_request(type(None), CMD_READDIR, handle)
                    nums.append(num)

                # For each of our sent requests
                # Read and parse the corresponding packets
                # If we're at the end of our queued requests, then fire off
                # some more requests
                # Exit the loop when we've reached the end of the directory
                # handle
                for num in nums:
                    t, pkt_data = self._read_packet()
                    msg = Message(pkt_data)
                    new_num = msg.get_int()
                    if num == new_num:
                        if t == CMD_STATUS:
                            self._convert_status(msg)
                    count = msg.get_int()
                    for i in range(count):
                        filename = msg.get_text()
                        longname = msg.get_text()
                        attr = SFTPAttributes._from_msg(
                            msg, filename, longname)
                        if (filename != '.') and (filename != '..'):
                            yield attr

                # If we've hit the end of our queued requests, reset nums.
                nums = list()

            except EOFError:
                self._request(CMD_CLOSE, handle)
                return
예제 #49
0
 def test_sftp_attributes_empty_str(self, sftp):
     sftp_attributes = SFTPAttributes()
     assert str(sftp_attributes) == "?---------   1 0        0               0 (unknown date) ?"
예제 #50
0
파일: test_sftp.py 프로젝트: xiar/idic
 def test_sftp_attributes_empty_str(self):
     sftp_attributes = SFTPAttributes()
     self.assertEqual(str(sftp_attributes), "?---------   1 0        0               0 (unknown date) ?")
예제 #51
0
 def _process_open(self, request_number, msg):
     path = msg.get_text()
     flags = self._convert_pflags(msg.get_int())
     attr = SFTPAttributes._from_msg(msg)
     self._send_handle_response(request_number,
                                self.server.open(path, flags, attr))
예제 #52
0
    def listdir_iter(self, path='.', read_ahead_requests=50):
        """
        Generator yielding L{SFTPAttributes} objects corresponding to
        files in the given C{path}.  Files are yielded 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
        depend on the SFTP server implementation.

        @param path: path to list (defaults to C{'.'})
        @type path: str
        @return: Yields L{SFTPAttributes}
        @rtype: L{SFTPAttributes}

        @since: 1.9
        """
        path = self._adjust_cwd(path)
        self._log(DEBUG, 'listdir(%r)' % path)
        t, msg = self._request(CMD_OPENDIR, path)

        if t != CMD_HANDLE:
            raise SFTPError('Expected handle')

        handle = msg.get_string()

        nums = list()
        while True:
            try:
                # Send out a bunch of readdir requests so that we can read the responses later on
                # Section 6.7 of the SSH file transfer RFC explains this
                # http://filezilla-project.org/specs/draft-ietf-secsh-filexfer-02.txt
                for i in range(read_ahead_requests):
                    num = self._async_request(type(None), CMD_READDIR, handle)
                    nums.append(num)


                # For each of our sent requests
                # Read and parse the corresponding packets
                # If we're at the end of our queued requests, then fire off some more requests
                # Exit the loop when we've reached the end of the directory handle
                for num in nums:
                    t, pkt_data = self._read_packet()
                    msg = Message(pkt_data)
                    new_num = msg.get_int()
                    if num == new_num:
                        if t == CMD_STATUS:
                            self._convert_status(msg)
                    count = msg.get_int()
                    for i in range(count):
                        filename = msg.get_string()
                        longname = msg.get_string()
                        attr = SFTPAttributes._from_msg(msg, filename, longname)
                        if (filename != '.') and (filename != '..'):
                            yield attr

                # If we've hit the end of our queued requests, reset nums.
                nums = list()

            except EOFError:
                self._request(CMD_CLOSE, handle)
                return
예제 #53
0
 def _process(self, t, request_number, msg):
     self._log(DEBUG, 'Request: %s' % CMD_NAMES[t])
     if t == CMD_OPEN:
         path = msg.get_text()
         flags = self._convert_pflags(msg.get_int())
         attr = SFTPAttributes._from_msg(msg)
         self._send_handle_response(request_number, self.server.open(path, flags, attr))
     elif t == CMD_CLOSE:
         handle = msg.get_binary()
         if handle in self.folder_table:
             del self.folder_table[handle]
             self._send_status(request_number, SFTP_OK)
             return
         if handle in self.file_table:
             self.file_table[handle].close()
             del self.file_table[handle]
             self._send_status(request_number, SFTP_OK)
             return
         self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle')
     elif t == CMD_READ:
         handle = msg.get_binary()
         offset = msg.get_int64()
         length = msg.get_int()
         if handle not in self.file_table:
             self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle')
             return
         data = self.file_table[handle].read(offset, length)
         if isinstance(data, (bytes_types, string_types)):
             if len(data) == 0:
                 self._send_status(request_number, SFTP_EOF)
             else:
                 self._response(request_number, CMD_DATA, data)
         else:
             self._send_status(request_number, data)
     elif t == CMD_WRITE:
         handle = msg.get_binary()
         offset = msg.get_int64()
         data = msg.get_binary()
         if handle not in self.file_table:
             self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle')
             return
         self._send_status(request_number, self.file_table[handle].write(offset, data))
     elif t == CMD_REMOVE:
         path = msg.get_text()
         self._send_status(request_number, self.server.remove(path))
     elif t == CMD_RENAME:
         oldpath = msg.get_text()
         newpath = msg.get_text()
         self._send_status(request_number, self.server.rename(oldpath, newpath))
     elif t == CMD_MKDIR:
         path = msg.get_text()
         attr = SFTPAttributes._from_msg(msg)
         self._send_status(request_number, self.server.mkdir(path, attr))
     elif t == CMD_RMDIR:
         path = msg.get_text()
         self._send_status(request_number, self.server.rmdir(path))
     elif t == CMD_OPENDIR:
         path = msg.get_text()
         self._open_folder(request_number, path)
         return
     elif t == CMD_READDIR:
         handle = msg.get_binary()
         if handle not in self.folder_table:
             self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle')
             return
         folder = self.folder_table[handle]
         self._read_folder(request_number, folder)
     elif t == CMD_STAT:
         path = msg.get_text()
         resp = self.server.stat(path)
         if issubclass(type(resp), SFTPAttributes):
             self._response(request_number, CMD_ATTRS, resp)
         else:
             self._send_status(request_number, resp)
     elif t == CMD_LSTAT:
         path = msg.get_text()
         resp = self.server.lstat(path)
         if issubclass(type(resp), SFTPAttributes):
             self._response(request_number, CMD_ATTRS, resp)
         else:
             self._send_status(request_number, resp)
     elif t == CMD_FSTAT:
         handle = msg.get_binary()
         if handle not in self.file_table:
             self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle')
             return
         resp = self.file_table[handle].stat()
         if issubclass(type(resp), SFTPAttributes):
             self._response(request_number, CMD_ATTRS, resp)
         else:
             self._send_status(request_number, resp)
     elif t == CMD_SETSTAT:
         path = msg.get_text()
         attr = SFTPAttributes._from_msg(msg)
         self._send_status(request_number, self.server.chattr(path, attr))
     elif t == CMD_FSETSTAT:
         handle = msg.get_binary()
         attr = SFTPAttributes._from_msg(msg)
         if handle not in self.file_table:
             self._response(request_number, SFTP_BAD_MESSAGE, 'Invalid handle')
             return
         self._send_status(request_number, self.file_table[handle].chattr(attr))
     elif t == CMD_READLINK:
         path = msg.get_text()
         resp = self.server.readlink(path)
         if isinstance(resp, (bytes_types, string_types)):
             self._response(request_number, CMD_NAME, 1, resp, '', SFTPAttributes())
         else:
             self._send_status(request_number, resp)
     elif t == CMD_SYMLINK:
         # the sftp 2 draft is incorrect here!  path always follows target_path
         target_path = msg.get_text()
         path = msg.get_text()
         self._send_status(request_number, self.server.symlink(target_path, path))
     elif t == CMD_REALPATH:
         path = msg.get_text()
         rpath = self.server.canonicalize(path)
         self._response(request_number, CMD_NAME, 1, rpath, '', SFTPAttributes())
     elif t == CMD_EXTENDED:
         tag = msg.get_text()
         if tag == 'check-file':
             self._check_file(request_number, msg)
         else:
             self._send_status(request_number, SFTP_OP_UNSUPPORTED)
     else:
         self._send_status(request_number, SFTP_OP_UNSUPPORTED)
예제 #54
0
 def _process_realpath(self, request_number, msg):
     path = msg.get_text()
     rpath = self.server.canonicalize(path)
     self._response(request_number, CMD_NAME, 1, rpath, '',
                    SFTPAttributes())
예제 #55
0
파일: paramiko.py 프로젝트: jsonar/mock
 def listdir_attr(self, path='.'):
     ret = []
     for filename in os.listdir(path):
         child = os.lstat(filename)
         ret.append(SFTPAttributes.from_stat(child, filename))
     return ret
예제 #56
0
 def stat(self):
     try:
         return SFTPAttributes.from_stat(os.fstat(self.readfile.fileno()))
     except OSError as e:
         return SFTPServer.convert_errno(e.errno)
예제 #57
0
    def listdir_iter(self, path='.', read_aheads=50):
        """
        Generator version of `.listdir_attr`.

        See the API docs for `.listdir_attr` for overall details.

        This function adds one more kwarg on top of `.listdir_attr`:
        ``read_aheads``, an integer controlling how many
        ``SSH_FXP_READDIR`` requests are made to the server. The default of 50
        should suffice for most file listings as each request/response cycle
        may contain multiple files (dependant on server implementation.)

        .. versionadded:: 1.15
        """
        path = self._adjust_cwd(path)
        self._log(DEBUG, 'listdir(%r)' % path)
        t, msg = self._request(CMD_OPENDIR, path)

        if t != CMD_HANDLE:
            raise SFTPError('Expected handle')

        handle = msg.get_string()

        nums = list()
        while True:
            try:
                # Send out a bunch of readdir requests so that we can read the
                # responses later on Section 6.7 of the SSH file transfer RFC
                # explains this
                # http://filezilla-project.org/specs/draft-ietf-secsh-filexfer-02.txt
                for i in range(read_aheads):
                    num = self._async_request(type(None), CMD_READDIR, handle)
                    nums.append(num)


                # For each of our sent requests
                # Read and parse the corresponding packets
                # If we're at the end of our queued requests, then fire off
                # some more requests
                # Exit the loop when we've reached the end of the directory
                # handle
                for num in nums:
                    t, pkt_data = self._read_packet()
                    msg = Message(pkt_data)
                    new_num = msg.get_int()
                    if num == new_num:
                        if t == CMD_STATUS:
                            self._convert_status(msg)
                    count = msg.get_int()
                    for i in range(count):
                        filename = msg.get_text()
                        longname = msg.get_text()
                        attr = SFTPAttributes._from_msg(
                            msg, filename, longname)
                        if (filename != '.') and (filename != '..'):
                            yield attr

                # If we've hit the end of our queued requests, reset nums.
                nums = list()

            except EOFError:
                self._request(CMD_CLOSE, handle)
                return
예제 #58
0
 def _process(self, t, request_number, msg):
     self._log(DEBUG, 'Request: %s' % CMD_NAMES[t])
     if t == CMD_OPEN:
         path = msg.get_text()
         flags = self._convert_pflags(msg.get_int())
         attr = SFTPAttributes._from_msg(msg)
         self._send_handle_response(request_number,
                                    self.server.open(path, flags, attr))
     elif t == CMD_CLOSE:
         handle = msg.get_binary()
         if handle in self.folder_table:
             del self.folder_table[handle]
             self._send_status(request_number, SFTP_OK)
             return
         if handle in self.file_table:
             self.file_table[handle].close()
             del self.file_table[handle]
             self._send_status(request_number, SFTP_OK)
             return
         self._send_status(request_number, SFTP_BAD_MESSAGE,
                           'Invalid handle')
     elif t == CMD_READ:
         handle = msg.get_binary()
         offset = msg.get_int64()
         length = msg.get_int()
         if handle not in self.file_table:
             self._send_status(request_number, SFTP_BAD_MESSAGE,
                               'Invalid handle')
             return
         data = self.file_table[handle].read(offset, length)
         if isinstance(data, (bytes_types, string_types)):
             if len(data) == 0:
                 self._send_status(request_number, SFTP_EOF)
             else:
                 self._response(request_number, CMD_DATA, data)
         else:
             self._send_status(request_number, data)
     elif t == CMD_WRITE:
         handle = msg.get_binary()
         offset = msg.get_int64()
         data = msg.get_binary()
         if handle not in self.file_table:
             self._send_status(request_number, SFTP_BAD_MESSAGE,
                               'Invalid handle')
             return
         self._send_status(request_number,
                           self.file_table[handle].write(offset, data))
     elif t == CMD_REMOVE:
         path = msg.get_text()
         self._send_status(request_number, self.server.remove(path))
     elif t == CMD_RENAME:
         oldpath = msg.get_text()
         newpath = msg.get_text()
         self._send_status(request_number,
                           self.server.rename(oldpath, newpath))
     elif t == CMD_MKDIR:
         path = msg.get_text()
         attr = SFTPAttributes._from_msg(msg)
         self._send_status(request_number, self.server.mkdir(path, attr))
     elif t == CMD_RMDIR:
         path = msg.get_text()
         self._send_status(request_number, self.server.rmdir(path))
     elif t == CMD_OPENDIR:
         path = msg.get_text()
         self._open_folder(request_number, path)
         return
     elif t == CMD_READDIR:
         handle = msg.get_binary()
         if handle not in self.folder_table:
             self._send_status(request_number, SFTP_BAD_MESSAGE,
                               'Invalid handle')
             return
         folder = self.folder_table[handle]
         self._read_folder(request_number, folder)
     elif t == CMD_STAT:
         path = msg.get_text()
         resp = self.server.stat(path)
         if issubclass(type(resp), SFTPAttributes):
             self._response(request_number, CMD_ATTRS, resp)
         else:
             self._send_status(request_number, resp)
     elif t == CMD_LSTAT:
         path = msg.get_text()
         resp = self.server.lstat(path)
         if issubclass(type(resp), SFTPAttributes):
             self._response(request_number, CMD_ATTRS, resp)
         else:
             self._send_status(request_number, resp)
     elif t == CMD_FSTAT:
         handle = msg.get_binary()
         if handle not in self.file_table:
             self._send_status(request_number, SFTP_BAD_MESSAGE,
                               'Invalid handle')
             return
         resp = self.file_table[handle].stat()
         if issubclass(type(resp), SFTPAttributes):
             self._response(request_number, CMD_ATTRS, resp)
         else:
             self._send_status(request_number, resp)
     elif t == CMD_SETSTAT:
         path = msg.get_text()
         attr = SFTPAttributes._from_msg(msg)
         self._send_status(request_number, self.server.chattr(path, attr))
     elif t == CMD_FSETSTAT:
         handle = msg.get_binary()
         attr = SFTPAttributes._from_msg(msg)
         if handle not in self.file_table:
             self._response(request_number, SFTP_BAD_MESSAGE,
                            'Invalid handle')
             return
         self._send_status(request_number,
                           self.file_table[handle].chattr(attr))
     elif t == CMD_READLINK:
         path = msg.get_text()
         resp = self.server.readlink(path)
         if isinstance(resp, (bytes_types, string_types)):
             self._response(request_number, CMD_NAME, 1, resp, '',
                            SFTPAttributes())
         else:
             self._send_status(request_number, resp)
     elif t == CMD_SYMLINK:
         # the sftp 2 draft is incorrect here!  path always follows target_path
         target_path = msg.get_text()
         path = msg.get_text()
         self._send_status(request_number,
                           self.server.symlink(target_path, path))
     elif t == CMD_REALPATH:
         path = msg.get_text()
         rpath = self.server.canonicalize(path)
         self._response(request_number, CMD_NAME, 1, rpath, '',
                        SFTPAttributes())
     elif t == CMD_EXTENDED:
         tag = msg.get_text()
         if tag == 'check-file':
             self._check_file(request_number, msg)
         else:
             self._send_status(request_number, SFTP_OP_UNSUPPORTED)
     else:
         self._send_status(request_number, SFTP_OP_UNSUPPORTED)
예제 #59
0
 def _process_setstat(self, request_number, msg):
     path = msg.get_text()
     attr = SFTPAttributes._from_msg(msg)
     self._send_status(request_number, self.server.chattr(path, attr))