From 2c7a50356982c4d46ffc12f7446fa0385d3d55c5 Mon Sep 17 00:00:00 2001 From: "O.S" Date: Mon, 27 Jul 2015 16:08:45 +0200 Subject: [PATCH] scp_recv_e method added. It returns additionally the file size in bytes. Receiving binary files was not possible without knowing the amount of data to be received. --- libssh2/session.py | 14 ++++++++++++++ src/session.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/libssh2/session.py b/libssh2/session.py index 93d958e..9565958 100644 --- a/libssh2/session.py +++ b/libssh2/session.py @@ -154,6 +154,20 @@ def scp_recv(self, remote_path): """ return Channel(self._session.scp_recv(remote_path)) + def scp_recv_e(self, remote_path): + """ + --writeme-- + Gets a remote file via SCP Protocol. Returns a tuple of Channel and file size in bytes. + + @param remote_path: absolute path of remote file to transfer + @type remote_path: str + + @return: new channel opened and sb.st_size + @rtype: (L{Channel}, int) + """ + ch,sz=self._session.scp_recv_e(remote_path) + return Channel(ch),sz + def scp_send(self, path, mode, size): """ Sends a file to remote host via SCP protocol. diff --git a/src/session.c b/src/session.c index a4c7941..0749e32 100644 --- a/src/session.c +++ b/src/session.c @@ -21,6 +21,7 @@ #include #define PYLIBSSH2_MODULE #include "pylibssh2.h" +#include /* {{{ PYLIBSSH2_Session_set_banner */ @@ -634,6 +635,42 @@ PYLIBSSH2_Session_scp_recv(PYLIBSSH2_SESSION *self, PyObject *args) } /* }}} */ +/* {{{ PYLIBSSH2_Session_scp_recv_e + */ +static char PYLIBSSH2_Session_scp_recv_e_doc[] = "\n\ +scp_recv_e(remote_path) -> Tuple of libssh2.Channel and stat.st_size\n\ +\n\ +Requests a remote file via SCP protocol.\n\ +\n\ +@param remote_path: absolute path of remote file to transfer\n\ +@type remote_path: str\n\ +\n\ +@return new channel opened and size of the requested file in bytes\n\ +@rtype (libssh2.Channel, int)"; + +static PyObject * +PYLIBSSH2_Session_scp_recv_e(PYLIBSSH2_SESSION *self, PyObject *args) +{ + char *path; + LIBSSH2_CHANNEL *channel; + + if (!PyArg_ParseTuple(args, "s:scp_recv_e", &path)) { + return NULL; + } + + struct stat sb; + + channel = libssh2_scp_recv(self->session, path, &sb); + if (channel == NULL) { + /* CLEAN: PYLIBSSH2_CHANNEL_SCP_RECV_ERROR_MSG */ + PyErr_SetString(PYLIBSSH2_Error, "SCP receive error."); + return NULL; + } + + return (PyObject *)Py_BuildValue("(N,l)",PYLIBSSH2_Channel_New(channel, 1),sb.st_size); +} +/* }}} */ + /* {{{ PYLIBSSH2_Session_scp_send */ static char PYLIBSSH2_Session_scp_send_doc[] = "\n\ @@ -1052,6 +1089,7 @@ static PyMethodDef PYLIBSSH2_Session_methods[] = ADD_METHOD(session_method_pref), ADD_METHOD(open_session), ADD_METHOD(scp_recv), + ADD_METHOD(scp_recv_e), ADD_METHOD(scp_send), ADD_METHOD(sftp_init), ADD_METHOD(direct_tcpip),