Skip to content

wait for entire buffer to be written before returning when calling sftp_write #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 5, 2018

Conversation

rasmunk
Copy link
Contributor

@rasmunk rasmunk commented Jun 4, 2018

libssh2 sftp_write does not guarantee that the entire buffer is written.
Sends up to 32K packets which are individually acked.
https://www.libssh2.org/libssh2_sftp_write.html

@pkittenis
Copy link
Member

Hi there,

Thanks for the interest and PR!

The change seems good to make, have left some comments.

Will also need to rebuild the and commit the C sources with Cython for changes to be applied and tested either locally or on CI. CI does not install Cython, it only builds from C files.

See contribution guide.

@@ -270,7 +270,12 @@ cdef class SFTPHandle:
cdef char *cbuf = buf
cdef ssize_t rc
with nogil:
rc = c_sftp.libssh2_sftp_write(self._handle, cbuf, _size)
while _size > 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change if _size is 0 rc will be undefined and will cause a crash. Can skip call to write and just return 0 in that case.

@@ -270,7 +270,16 @@ cdef class SFTPHandle:
cdef char *cbuf = buf
cdef ssize_t rc
with nogil:
rc = c_sftp.libssh2_sftp_write(self._handle, cbuf, _size)
while _size >= 0:
if _size == 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better to just set cdef ssize_t rc = 0 by default and let it fall through when size > 0 fails rather than have to do conditional checks. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that does make more sense. Cheaper in general, will update it

@pkittenis
Copy link
Member

CI errors are from docker login which does not work for PRs - tests all pass. Will make a separate change for that.

rc = c_sftp.libssh2_sftp_write(self._handle, cbuf, _size)
while _size > 0:
rc = c_sftp.libssh2_sftp_write(self._handle, cbuf, _size)
if rc < 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate on this? why not?
Would think that you would keep writing the content of 'cbuf' until it is empty

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake actually :)

@pkittenis pkittenis merged commit a8e0d5d into ParallelSSH:master Jun 5, 2018
@pkittenis
Copy link
Member

Looks good, thanks for the PR!

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