Skip to content

Conversation

@spencerjanssen
Copy link

I ran into this issue working with an SFTP server which always reports an inaccurate file size. However, I think this code is problematic in other scenarios such as the file being truncated mid-read.

In particular when the reported size of the remote file changes between
sftpFstat and completion of the read loop.
@portnov
Copy link
Owner

portnov commented Apr 3, 2025

Could you please elaborate a bit what the patch does and why. I currently do not have some test suite for this code, so I have to rely on basic logic understanding :)

@spencerjanssen
Copy link
Author

So the old code:

  1. Gets the reported size of the file, N
  2. Reads chunks of the file, in a loop, terminating once N bytes have been read

The problem is that: the size of the file may change in the course of our loop. If the file is truncated the code gets stuck in an infinite loop where each read gets 0 bytes and we never reach N.

The new code doesn't rely on the reported size of the file. Instead, it reads chunks in a loop terminating once sftp_read returns 0 bytes -- which is the way libssh2 signals end of file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants