Enabled fragmenting SFTP requests to LIBSSH2_SFTP_PACKET_MAXLEN.
(Thanks to elifantu@mail.ru for the patch)
Этот коммит содержится в:
родитель
56608a799f
Коммит
e7b3a2efc5
128
src/sftp.c
128
src/sftp.c
@ -668,77 +668,97 @@ static ssize_t _libssh2_sftp_read(LIBSSH2_SFTP_HANDLE *handle,
|
|||||||
unsigned char *packet, *s, *data;
|
unsigned char *packet, *s, *data;
|
||||||
unsigned char read_responses[2] = { SSH_FXP_DATA, SSH_FXP_STATUS };
|
unsigned char read_responses[2] = { SSH_FXP_DATA, SSH_FXP_STATUS };
|
||||||
size_t bytes_read = 0;
|
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);
|
_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) {
|
if (!packet) {
|
||||||
libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for FXP_CLOSE packet", 0);
|
libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for FXP_CLOSE packet", 0);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
libssh2_htonu32(s, packet_len - 4);
|
while (total_read < buffer_maxlen) {
|
||||||
s += 4;
|
s = packet;
|
||||||
*(s++) = SSH_FXP_READ;
|
/* If buffer_maxlen bytes will be requested, server may return
|
||||||
request_id = sftp->request_id++;
|
* all with one packet. But libssh2 have packet length
|
||||||
libssh2_htonu32(s, request_id);
|
* limit. So we request data by pieces. */
|
||||||
s += 4;
|
bytes_requested = buffer_maxlen - total_read;
|
||||||
libssh2_htonu32(s, handle->handle_len);
|
if (bytes_requested > LIBSSH2_SFTP_PACKET_MAXLEN-10) /* packet_type(1)+request_id(4)+data_length(4)+end_of_line_flag(1) */
|
||||||
s += 4;
|
bytes_requested = LIBSSH2_SFTP_PACKET_MAXLEN-10;
|
||||||
|
|
||||||
memcpy(s, handle->handle, handle->handle_len);
|
#ifdef LIBSSH2_DEBUG_SFTP
|
||||||
s += handle->handle_len;
|
_libssh2_debug(session, LIBSSH2_DBG_SFTP, "Requesting %lu bytes from SFTP handle", (unsigned long)bytes_requested);
|
||||||
|
#endif
|
||||||
|
|
||||||
libssh2_htonu64(s, handle->u.file.offset);
|
libssh2_htonu32(s, packet_len - 4);
|
||||||
s += 8;
|
s += 4;
|
||||||
|
*(s++) = SSH_FXP_READ;
|
||||||
|
request_id = sftp->request_id++;
|
||||||
|
libssh2_htonu32(s, request_id);
|
||||||
|
s += 4;
|
||||||
|
libssh2_htonu32(s, handle->handle_len);
|
||||||
|
s += 4;
|
||||||
|
|
||||||
libssh2_htonu32(s, buffer_maxlen);
|
memcpy(s, handle->handle, handle->handle_len);
|
||||||
s += 4;
|
s += handle->handle_len;
|
||||||
|
|
||||||
if (packet_len != libssh2_channel_write(channel, (char *)packet,
|
libssh2_htonu64(s, handle->u.file.offset);
|
||||||
packet_len)) {
|
s += 8;
|
||||||
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
|
|
||||||
"Unable to send FXP_READ command", 0);
|
|
||||||
LIBSSH2_FREE(session, packet);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
LIBSSH2_FREE(session, packet);
|
|
||||||
|
|
||||||
if (libssh2_sftp_packet_requirev(sftp, 2, read_responses, request_id,
|
libssh2_htonu32(s, buffer_maxlen);
|
||||||
&data, &data_len)) {
|
s += 4;
|
||||||
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
|
|
||||||
"Timeout waiting for status message", 0);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (data[0]) {
|
if (packet_len != libssh2_channel_write(channel, (char *)packet,
|
||||||
case SSH_FXP_STATUS: {
|
packet_len)) {
|
||||||
int retcode = libssh2_ntohu32(data + 5);
|
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
|
||||||
LIBSSH2_FREE(session, data);
|
"Unable to send FXP_READ command", 0);
|
||||||
|
LIBSSH2_FREE(session, packet);
|
||||||
if (retcode == LIBSSH2_FX_EOF) {
|
return -1;
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
sftp->last_errno = retcode;
|
|
||||||
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, "SFTP Protocol Error", 0);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case SSH_FXP_DATA:
|
if (libssh2_sftp_packet_requirev(sftp, 2, read_responses, request_id,
|
||||||
bytes_read = libssh2_ntohu32(data + 5);
|
&data, &data_len)) {
|
||||||
if (bytes_read > (data_len - 9)) {
|
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
|
||||||
return -1;
|
"Timeout waiting for status message", 0);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 total_read;
|
||||||
|
} else {
|
||||||
|
sftp->last_errno = retcode;
|
||||||
|
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, "SFTP Protocol Error", 0);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_libssh2_debug(session, LIBSSH2_DBG_SFTP,
|
|
||||||
"%lu bytes returned",
|
case SSH_FXP_DATA:
|
||||||
(unsigned long)bytes_read);
|
bytes_read = libssh2_ntohu32(data + 5);
|
||||||
memcpy(buffer, data + 9, bytes_read);
|
if (bytes_read > (data_len - 9)) {
|
||||||
handle->u.file.offset += bytes_read;
|
return -1;
|
||||||
LIBSSH2_FREE(session, data);
|
}
|
||||||
return bytes_read;
|
#ifdef LIBSSH2_DEBUG_SFTP
|
||||||
|
_libssh2_debug(session, LIBSSH2_DBG_SFTP,
|
||||||
|
"%lu bytes returned",
|
||||||
|
(unsigned long)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 -1;
|
LIBSSH2_FREE(session, packet);
|
||||||
|
return total_read;
|
||||||
}
|
}
|
||||||
/* {{{ libssh2_sftp_read
|
/* {{{ libssh2_sftp_read
|
||||||
* Read from an SFTP file handle blocking
|
* Read from an SFTP file handle blocking
|
||||||
|
Загрузка…
x
Ссылка в новой задаче
Block a user