Enabled fragmenting SFTP requests to LIBSSH2_SFTP_PACKET_MAXLEN.

(Thanks to elifantu@mail.ru for the patch)
This commit is contained in:
Mikhail Gusarov 2007-04-01 08:04:32 +00:00
parent 56608a799f
commit e7b3a2efc5

View File

@ -668,14 +668,29 @@ static ssize_t _libssh2_sftp_read(LIBSSH2_SFTP_HANDLE *handle,
unsigned char *packet, *s, *data;
unsigned char read_responses[2] = { SSH_FXP_DATA, SSH_FXP_STATUS };
size_t bytes_read = 0;
size_t bytes_requested = 0;
size_t total_read = 0;
_libssh2_debug(session, LIBSSH2_DBG_SFTP, "Reading %lu bytes from SFTP handle", (unsigned long)buffer_maxlen);
s = packet = LIBSSH2_ALLOC(session, packet_len);
packet = LIBSSH2_ALLOC(session, packet_len);
if (!packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for FXP_CLOSE packet", 0);
return -1;
}
while (total_read < buffer_maxlen) {
s = packet;
/* If buffer_maxlen bytes will be requested, server may return
* all with one packet. But libssh2 have packet length
* limit. So we request data by pieces. */
bytes_requested = buffer_maxlen - total_read;
if (bytes_requested > LIBSSH2_SFTP_PACKET_MAXLEN-10) /* packet_type(1)+request_id(4)+data_length(4)+end_of_line_flag(1) */
bytes_requested = LIBSSH2_SFTP_PACKET_MAXLEN-10;
#ifdef LIBSSH2_DEBUG_SFTP
_libssh2_debug(session, LIBSSH2_DBG_SFTP, "Requesting %lu bytes from SFTP handle", (unsigned long)bytes_requested);
#endif
libssh2_htonu32(s, packet_len - 4);
s += 4;
*(s++) = SSH_FXP_READ;
@ -701,7 +716,6 @@ static ssize_t _libssh2_sftp_read(LIBSSH2_SFTP_HANDLE *handle,
LIBSSH2_FREE(session, packet);
return -1;
}
LIBSSH2_FREE(session, packet);
if (libssh2_sftp_packet_requirev(sftp, 2, read_responses, request_id,
&data, &data_len)) {
@ -713,10 +727,11 @@ static ssize_t _libssh2_sftp_read(LIBSSH2_SFTP_HANDLE *handle,
switch (data[0]) {
case SSH_FXP_STATUS: {
int retcode = libssh2_ntohu32(data + 5);
LIBSSH2_FREE(session, packet);
LIBSSH2_FREE(session, data);
if (retcode == LIBSSH2_FX_EOF) {
return 0;
return total_read;
} else {
sftp->last_errno = retcode;
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, "SFTP Protocol Error", 0);
@ -729,16 +744,21 @@ static ssize_t _libssh2_sftp_read(LIBSSH2_SFTP_HANDLE *handle,
if (bytes_read > (data_len - 9)) {
return -1;
}
#ifdef LIBSSH2_DEBUG_SFTP
_libssh2_debug(session, LIBSSH2_DBG_SFTP,
"%lu bytes returned",
(unsigned long)bytes_read);
memcpy(buffer, data + 9, bytes_read);
#endif
memcpy(buffer + total_read, data + 9, bytes_read);
handle->u.file.offset += bytes_read;
total_read += bytes_read;
LIBSSH2_FREE(session, data);
return bytes_read;
}
}
return -1;
LIBSSH2_FREE(session, packet);
return total_read;
}
/* {{{ libssh2_sftp_read
* Read from an SFTP file handle blocking