pkg / sftp

SFTP support for the go.crypto/ssh package
BSD 2-Clause "Simplified" License
1.52k stars 380 forks source link

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

Closed drakkan closed 4 years ago

drakkan commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

drakkan commented 4 years ago

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:

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?