www.libssh2.org | Daily snapshots | Mailing list archive | Docs | Examples

Archive Index This month's Index

Subject: libssh2_sftp_write with count=32768

libssh2_sftp_write with count=32768

From: Michael Harris <michael.harris_at_ericsson.com>
Date: Sat, 27 Feb 2010 12:08:14 +0800

Hi,

I discovered what I think might be a bug in sftp.c; I thought I would canvas it here before putting it into the bug tracker to make sure it is not just a misunderstanding on my part.

The API for libssh2_sftp_write says that if the return value is positive, it is the number of bytes actually written. If I feed it a write of exactly 32768 bytes it returns 32768 - but the data is not actually written (which you can see by calling libssh2_sftp_tell afterwards - you always get back 0).

I think the problem is in the following part of sftp.c (lines 1462 .. 1475):

1462 if (sftp->write_state == libssh2_NB_state_created) {
1463 rc = _libssh2_channel_write(channel, 0, (char *)sftp->write_packet,
1464 packet_len);
1465 if(rc < 0) {
1466 /* error */
1467 return rc;
1468 }
1469 else if(0 == rc) {
1470 /* nothing sent is an error */
1471 return LIBSSH2_ERROR_SOCKET_SEND;
1472 }
1473 else if (packet_len != rc) {
1474 return rc;
1475 }

My packet_len is 32768+handle_len(4)+25 = 32797, and _libssh2_channel_write can only write 32768 bytes at a time, so rc is 32768 (the actual amount of the packet written so far). Line 1474 returns the amount of the packet written so far - and the calling application things it has managed to write it's whole buffer, but in fact the returned value represents the amount of the packet sent so far - this is the bug I think.

But anyway, until the whole packet is sent, we probably can't conclude that any of the file is written anyway? By my reading of the sftp spec, I can't see that there is any scope for the result of an sftp write to be anything less than the full amount requested. It is not until you have received back SSH_FXP_STATUS that you know of the write worked at all.

So maybe line 1474 should return EAGAIN instead? That way if in blocking mode it sftp_write will be repeatedly called, and if not then the calling application can handle calling again when it wants to.

Unless of course I misunderstood the way the API should be used!

BR // Mike

MICHAEL HARRIS

Ericsson Australia Pty Ltd
www.ericsson.com

This Communication is Confidential. We only send and receive email on the basis of the terms set out at www.ericsson.com/email_disclaimer

_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2010-02-27

the libssh2 team