#158: libssh2_sftp_write returns incorrect value
--------------------+-------------------------------------------------------
Reporter: mstrsn | Owner: bagder
Type: defect | Status: assigned
Priority: normal | Milestone:
Component: SFTP | Version: 1.2.4
Keywords: | Blocking:
Blockedby: |
--------------------+-------------------------------------------------------
Comment(by mstrsn):
Please ignore my previous patch suggestion. While it did allow my program
to complete, the file sent is corrupted. I now see that my problem is also
related to bug #126. I agree with dseanressell's suggestion that
_libssh2_channel_write should return channel->write_bufwrote, not wrote.
The SSH server that I am communicating with is very limited and the local
window will often become very small, sometimes reaching zero length. The
following two patch fixes the problem for me:
{{{
>diff -u channel.c.orig channel.c
--- channel.c.orig Fri Feb 19 16:30:03 2010
+++ channel.c Fri Feb 19 16:45:45 2010
@@ -2001,7 +2001,8 @@
/* Deduct the amount that has already been sent, and set buf
accordingly. */
buflen -= channel->write_bufwrote;
buf += channel->write_bufwrote;
-
+ wrote = channel->write_bufwrote;
+
while (buflen > 0) {
if (channel->write_state == libssh2_NB_state_allocated) {
channel->write_bufwrite = buflen;
@@ -2024,7 +2025,10 @@
if(channel->local.window_size <= 0) {
/* there's no more room for data so we stop sending now
*/
- break;
+ rc = PACKET_EAGAIN;
+ libssh2_error(session, rc,
+ "Unable to send channel data", 0);
+ return rc;
}
/* Don't exceed the remote end's limits */
}}}
{{{
gedms3gmg:dhm>diff -u sftp.c.orig sftp.c
--- sftp.c.orig Fri Feb 19 10:20:09 2010
+++ sftp.c Fri Feb 19 16:52:57 2010
@@ -1460,6 +1460,7 @@
}
if (sftp->write_state == libssh2_NB_state_created) {
+ again:
rc = _libssh2_channel_write(channel, 0, (char
*)sftp->write_packet,
packet_len);
if(rc < 0) {
@@ -1467,11 +1468,14 @@
return rc;
}
else if(0 == rc) {
+ if(channel->local.window_size == 0) {
+ goto again;
+ }
/* nothing sent is an error */
return LIBSSH2_ERROR_SOCKET_SEND;
}
else if (packet_len != rc) {
- return rc;
+ goto again;
}
LIBSSH2_FREE(session, sftp->write_packet);
sftp->write_packet = NULL;
}}}
-- Ticket URL: <http://libssh2.stuge.se/ticket/158#comment:3> libssh2 <http://libssh2.stuge.se/> C library for writing portable SSH2 clients _______________________________________________ libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-develReceived on 2010-02-23