Repository navigation
Conversation
When File.WriteTo is given another *File from the same Client, and the server advertises the "copy-data" extension (OpenSSH 9.0 and later), ask the server to copy the data directly from the source's current offset to the destination's current offset, instead of reading it to the client and writing it back. Both offsets advance by the bytes copied, so io.Copy(dst, src) and Seek work as before. If the server lacks the extension, the source is not a regular file, or the files are the same or from different clients, WriteTo falls back to the existing read and write path.
puellanivis
left a comment
There was a problem hiding this comment.
😟 hm… not actually looking good now that I see the PR…
This extension is just a bad idea for fitting into Golang in general. Too many assumptions being made that we cannot rely upon.
| var fileStat *FileStat | ||
| if f.c.useFstat { | ||
| fileStat, err = f.c.fstat(f.handle) | ||
| } else { | ||
| fileStat, err = f.c.stat(f.path) | ||
| } |
There was a problem hiding this comment.
Fun fact: we usually don’t want to be doing this anymore. There are a lot of issues with trying to find the file length without certain servers deleting the file as completely read.
Of course, these servers are unlikely to support the copy-data extension in the first place, right?
| dst.mu.Lock() | ||
| defer dst.mu.Unlock() |
There was a problem hiding this comment.
We’re atempting here to grab a new lock while holding another lock. This is always an great opportunity for creating a deadlock. Worse, as designed, there’s no way to keep clients from trying to lock the files in the opposite order.
As such, we should probably record this file’s handle to a local variable, unlock, grab the dst lock, record its handle to a local variable, then unlock. Now, we’re not holding either lock. So, we can now lock them in the sorted order of their handles. (Of course, if either is empty, then we would return os.ErrClosed rather than attemptiung to relock at all.)
Once we’ve released both locks and then relocked in a deterministic order, then need to again recheck to make sure each is not closed handle == "" while holding both locks.
| return 0, false, nil | ||
| } | ||
|
|
||
| // copy-data treats a length of zero as "until EOF", which would leave us not knowing how much was copied. |
There was a problem hiding this comment.
Fun fact: even if we know the file length, and request copying that much data, we still won’t know how many bytes were actually copied.
As an example, another process could trunc the file in between when we did our stat and when the copy-data request was sent/finishes.
Reading the openssh code, if it hits an EOF before the full amount of requested length has been read, then it should still return an SSH_FX_OK. But we would then erroneously report that we copied the whole set of data.
😐 This is actually a failure of the RFC design. There’s simply no way to get the information of how many bytes were actually copied back to the client. … this might make this a non-starter for this API, unless we implement a WriteToLimited(dst *File, n int64) error receiver method, that one will note: does not report back how many bytes were read/written. It should have been defined an SSH_FXP_EXTENDED_REPLY packet that reports back a success with how many bytes were copied.
Per review: do not guess the length with a stat, since some servers remove a file once it has been read, and copy-data cannot report how many bytes were actually copied. Instead add WriteToLimited(dst *File, n int64) error. The caller chooses the length and offsets (via Seek), zero is never sent (it would mean "until EOF"), and the two file locks are taken in a fixed order by handle, rechecking for a close, so opposite-direction calls cannot deadlock. WriteTo is unchanged.
puellanivis
left a comment
There was a problem hiding this comment.
Go ahead and push any updates from this round you would like to propose. But I’m going to put further reviews on ice for a bit, while I consider how we might want to shove this square peg into a round hole.
The design of this is just… so incompatible with what we already have.
| if n == 0 { | ||
| // A length of zero means "until EOF" to copy-data, so never send it. | ||
| return nil | ||
| } |
There was a problem hiding this comment.
Sending a ReadDataLength == 0 means copy until EOF, but that’s not the semantics we have for n here.
Here, it means to copy zero bytes, and as such, is immediately successful as a no-op.
|
|
||
| first, second := f, dst | ||
| if dstHandle < srcHandle { | ||
| first, second = dst, f |
There was a problem hiding this comment.
prefer explicitly swapping the values:
| first, second = dst, f | |
| first, second = second, first |
| second.mu.Lock() | ||
| defer second.mu.Unlock() | ||
|
|
||
| // A file may have been closed while we were not holding its lock. |
There was a problem hiding this comment.
We’re not testing if it’s closed (that is f.handle == ""), we’re testing a wider sanity check: "making sure that the handle hasn’t changed (for example, when closed)".
Either we should test for being closed (against ""), or we should provide a proper error message of the actual problem: the file handler changed underneath us.
| f.offset += n | ||
| dst.offset += n |
There was a problem hiding this comment.
🤔 I’m not sure we should be doing this. If we knew the length read/written, then this would be justifiable. But we don’t actually know how much was written. So, we’re just making a guess here. This is the whole reason we cannot return written int64!
| require.NoError(t, err) | ||
|
|
||
| var wg sync.WaitGroup | ||
| for i := 0; i < 50; i++ { |
There was a problem hiding this comment.
| for i := 0; i < 50; i++ { | |
| for i := range 50 { |
| require.NoError(t, err) | ||
| defer dst.Close() | ||
|
|
||
| err = src.WriteToLimited(dst, 1) |
There was a problem hiding this comment.
This seems to be making the assumption that our server implementation will never implement the copy-data extension.
Weirdly, because of the bad design of the extension, it would actually be kind of trivial for us to implement it, the same as the OpenSSH sftp-server…
And you can bet contributors to this package will be interested in providing an implementation soon-ish. We have plenty of not just users of our client in this package, but users of our servers as well… (no matter how ill-advised I keep insisting that is).
| // the server may stop at EOF and still report success. | ||
| // Callers that need to be sure should check the size of dst afterwards. | ||
| // | ||
| // f and dst must be different files opened by the same Client. |
There was a problem hiding this comment.
f and dst do not have to be separate files, they have to be separate handles (in our use case).
As mentioned in the standard:
The server SHOULD allow 'read-from-handle' and 'write-to-handle' to be the same handle as long as the range of data is not overlapping.
In our case, we need separate offsets, even if it’s the same file… although, it seems, that sftp-server does not allow this: https://github.com/openssh/openssh-portable/blob/fa4b2a82be7df9768272cfabc2d78e4a12c2b3c0/sftp-server.c#L1651-L1655
However, we shouldn’t be making that decision. The standard says servers should allow even the two handles to be the same… On our side, the only reason we don’t want f == dst is because the two files will be sharing the same offset… oh, and they also share the same mutex… so, that would deadlock.
It should say something like f and dst must be different [File] structs, opened by the same [Client].
| // If the server does not advertise "copy-data", WriteToLimited sends nothing | ||
| // and returns a *StatusError with the code SSH_FX_OP_UNSUPPORTED. |
There was a problem hiding this comment.
I’ve had things misunderstand this statement.
- This should be a separate paragraph. It doesn’t relate to the previous “must be different Files”.
- “It returns an error if the server did not advertise "copy-data" support, and no request will be sent.”
| // On success, the offsets of both f and dst are advanced by n. | ||
| // | ||
| // The server only reports success or failure, not how many bytes it copied. | ||
| // If f is shorter than expected, for example because it is truncated while the copy runs, |
There was a problem hiding this comment.
Parentheticals should be parenthesized.
| // If f is shorter than expected, for example because it is truncated while the copy runs, | |
| // If f is shorter than expected (for example: because it is truncated while the copy runs), |
| // f and dst must be different files opened by the same Client. | ||
| // If the server does not advertise "copy-data", WriteToLimited sends nothing | ||
| // and returns a *StatusError with the code SSH_FX_OP_UNSUPPORTED. | ||
| func (f *File) WriteToLimited(dst *File, n int64) error { |
There was a problem hiding this comment.
🤔 with all the feedback from this round, I’m starting to think we just implement this as func (f *File) CopyData(dst *File, at, n int64) error…
Then we can specially consider where src == dst and avoid the deadlock from that, while keeping potential support to copy from the same handle to the same handle—but elsewhere. And leave it up to the server to either accept or refuse the operation.
…and docs - handle f == dst by locking once and leaving the decision to the server - report a changed handle separately from a closed one after locking - docs: File structs rather than files, unsupported case in its own paragraph - tests: same-file case, range-over-int loop, note on the unsupported test
|
Thanks for taking another look, and no problem putting this on ice. I pushed the updates from this round:
Two things I was not sure about:
If it still doesn't fit the package, that's completely fine and I can close this. |
Closes #707.
When
File.WriteTois given another*Filefrom the sameClientand the server advertises the OpenSSHcopy-dataextension (OpenSSH 9.0+), this asks the server to copy the data directly instead of reading it to the client and writing it back. Soio.Copy(dst, src)between two remote files on the same server no longer moves the data over the network twice.Seek. Both offsets advance by the bytes copied.WriteTocan return an accurate byte count.WriteToLimitedout to keep this small and can follow up with it.Tested with
go test -race -integrationand-integration -testserver(OpenSSH 9.6 sftp-server for the copy-data path, the Go server for the fallback).This is groundwork for server-side copy in rclone's sftp backend (rclone/rclone#6624).
I've reviewed and tested the change and I'm happy to make any changes you need.