Skip to content
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

Request server: allow to open a file for both reading and writing #374

Closed
drakkan opened this issue Aug 20, 2020 · 3 comments · Fixed by #377
Closed

Request server: allow to open a file for both reading and writing #374

drakkan opened this issue Aug 20, 2020 · 3 comments · Fixed by #377

Comments

@drakkan
Copy link
Collaborator

drakkan commented Aug 20, 2020

Hi,

based on the SFTP specs opening a file for both reading and writing is allowed

SSH_FXF_READ
      Open the file for reading.

SSH_FXF_WRITE
      Open the file for writing.  If both this and SSH_FXF_READ are
      specified, the file is opened for both reading and writing.

pkg/sftp currently does not support this mode. I think this is the reason for this issue.

Here are the OpenSSH logs when opening a file using sshfs-win:

ago 20 16:29:10 p1 sftp-server[9254]: debug3: request 168: open flags 3
ago 20 16:29:10 p1 sftp-server[9254]: open "/tmp/a/Nuovo documento di testo (2).txt" flags READ,WRITE mode 00
ago 20 16:29:10 p1 sftp-server[9254]: debug1: request 168: sent handle handle 2
ago 20 16:29:10 p1 sftp-server[9254]: debug3: request 169: lstat
ago 20 16:29:10 p1 sftp-server[9254]: lstat name "/tmp/a/Nuovo documento di testo (2).txt"
ago 20 16:29:10 p1 sftp-server[9254]: debug1: request 169: sent attrib have 0xf
ago 20 16:29:10 p1 sftp-server[9254]: debug1: request 170: write "/tmp/a/Nuovo documento di testo (2).txt" (handle 2) off 0 len 15
ago 20 16:29:10 p1 sftp-server[9254]: debug3: request 170: sent status 0
ago 20 16:29:10 p1 sftp-server[9254]: sent status Success
ago 20 16:29:10 p1 sftp-server[9254]: debug1: request 171: fstat "/tmp/a/Nuovo documento di testo (2).txt" (handle 2)
ago 20 16:29:10 p1 sftp-server[9254]: debug1: request 171: sent attrib have 0xf
ago 20 16:29:10 p1 sftp-server[9254]: debug1: request 172: fsetstat handle 2
ago 20 16:29:10 p1 sftp-server[9254]: set "/tmp/a/Nuovo documento di testo (2).txt" size 15
ago 20 16:29:10 p1 sftp-server[9254]: debug3: request 172: sent status 0
ago 20 16:29:10 p1 sftp-server[9254]: sent status Success
ago 20 16:29:10 p1 sftp-server[9254]: debug1: request 173: fstat "/tmp/a/Nuovo documento di testo (2).txt" (handle 2)
ago 20 16:29:10 p1 sftp-server[9254]: debug1: request 173: sent attrib have 0xf
ago 20 16:29:10 p1 sftp-server[9254]: debug1: request 174: read "/tmp/a/Nuovo documento di testo (2).txt" (handle 2) off 0 len 4096
ago 20 16:29:10 p1 sftp-server[9254]: debug1: request 174: sent data len 15
ago 20 16:29:10 p1 sftp-server[9254]: debug3: request 175: close handle 2
ago 20 16:29:10 p1 sftp-server[9254]: close "/tmp/a/Nuovo documento di testo (2).txt" bytes read 15 written 15

as you can see OpenSSH uses the same handle for both reads and writes.

How do you suggest to support this mode in pkg/sftp? Is ok a new request method, for example "PutGet" mapped to a new interface, for example Filereadwrite that returns both an io.ReaderAt and an io.WriterAt? Do you have other suggestions? Thank you

@drakkan
Copy link
Collaborator Author

drakkan commented Aug 20, 2020

Proof of concept here on top of #373

The user who reported the issue has confirmed that it works.

Please let me know if you prefer an alternative approach, thank you

@puellanivis
Copy link
Collaborator

I would kind of rather see a more generic implementation of OpenFile rather than special casing Yet Another Narrow-Scope File Object Opener.

@drakkan
Copy link
Collaborator Author

drakkan commented Aug 21, 2020

I would kind of rather see a more generic implementation of OpenFile rather than special casing Yet Another Narrow-Scope File Object Opener.

The current code calls the user defined interfaces based on request method:

  • Get method is mapped to Fileread
  • Put method is mapped to Filewrite

see here

since this is probably a backward incompatible change, do you want to replace the "Put" and "Get" method with an "OpenFile" method and then make different things based on packet type like this?

drakkan added a commit to drakkan/sftp that referenced this issue Aug 30, 2020
add an optional interface to FileWriter to allow to open a file for both
writing and reading

Fixes pkg#374
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 a pull request may close this issue.

2 participants