Subject: Re: libssh2_sftp_read reads a number of bytes smaller than both the file size and the specified buffer size

Re: libssh2_sftp_read reads a number of bytes smaller than both the file size and the specified buffer size

From: Adam Craig <>
Date: Tue, 3 Apr 2012 17:51:57 -0700

On 4/3/12, Alexander Lamaison <> wrote:
> On 4 April 2012 00:12, Adam Craig <> wrote:
>> Mr. Stenberg,
>> On 4/3/12, Daniel Stenberg <> wrote:
>>> On Tue, 3 Apr 2012, Adam Craig wrote:
>>>> If I upload a file of 200K all in one call to libssh2_sftp_write and it
>>>> returns 30K when it actually uploaded all 200K, what does the 30K return
>>>> value mean?
>>> It means 30K was sent off and that you need to call it again with the
>>> remaining 170K. In a very similar fashion to how write() and similar
>>> function
>>> calls work.
>> This is what I had originally interpreted it to mean, but, when I
>> tested it, even if my application did not call libssh2_sftp_write
>> again with the remaining 170K, the whole 200K still show up on the
>> server, even though the function only returned 30K on the first call.
>> That is, the amount of data it reported sending was less than what
>> actually showed up on the server.
> That's ok. Just keep calling libssh2_sftp_write until the total of
> all the calls adds up to the amount you wanted to write. What's
> happening here is that libssh2 did 'write ahead' meaning it sent of a
> large number of packets to the server but didn't wait for them all to
> be acknowleged before returning. In this case it looks like the first
> packet (usually around 30K big) was acknowleged and while it returned
> the others arrived at the server. You still have to keep calling
> libssh2_sftp_write so that libssh2 can collect (and eventually flush)
> all the acknowledgements.
> In short, just forget about what has actually arrived at the server
> and pretend libssh2_sftp_write is telling the truth.

Thank you. That makes sense. I will do as you suggest. This would be a
good thing to explain in the documentation for the function.

That still does not explain why the file downloads in 2K chunks, but
that might have something to do with the server. I will try a
different server-side program as soon as I can
 set up one.


>>>> Does the documentation explain this somewhere?
>>>> All te information on this I could find was
>>>> Actual number of bytes written or negative on failure.
>>> Based on your questions, I figure we need to expand this to explain it
>>> with
>>> more detail!
> We do! In particular, I think the documentation needs to stop
> referring people to POSIX read/write and instead spell out exactly
> what their behaviour is.
> Alex
> --
> Swish - Easy SFTP for Windows Explorer (
> _______________________________________________
> libssh2-devel
Received on 2012-04-04