-
-
Notifications
You must be signed in to change notification settings - Fork 940
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
Sftp file stream should consider negative offsets in Seek #1020
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
#if FEATURE_TAP | ||
using System.Threading.Tasks; | ||
#endif | ||
using System.Reflection; | ||
|
||
namespace Renci.SshNet.Sftp | ||
{ | ||
|
@@ -283,8 +284,8 @@ internal SftpFileStream(ISftpSession session, string path, FileMode mode, FileAc | |
// that ensures we always receive or send the max. number of bytes in a single SSH_FXP_READ | ||
// or SSH_FXP_WRITE message | ||
|
||
_readBufferSize = (int) session.CalculateOptimalReadLength((uint) bufferSize); | ||
_writeBufferSize = (int) session.CalculateOptimalWriteLength((uint) bufferSize, _handle); | ||
_readBufferSize = (int)session.CalculateOptimalReadLength((uint)bufferSize); | ||
_writeBufferSize = (int)session.CalculateOptimalWriteLength((uint)bufferSize, _handle); | ||
|
||
if (mode == FileMode.Append) | ||
{ | ||
|
@@ -381,7 +382,7 @@ internal static async Task<SftpFileStream> OpenAsync(ISftpSession session, strin | |
await session.RequestCloseAsync(handle, cancellationToken).ConfigureAwait(false); | ||
} | ||
catch | ||
{ | ||
{ | ||
// The original exception is presumably more informative, so we just ignore this one. | ||
} | ||
throw; | ||
|
@@ -509,7 +510,7 @@ public override int Read(byte[] buffer, int offset, int count) | |
var bytesAvailableInBuffer = _bufferLen - _bufferPosition; | ||
if (bytesAvailableInBuffer <= 0) | ||
{ | ||
var data = _session.RequestRead(_handle, (ulong) _position, (uint) _readBufferSize); | ||
var data = _session.RequestRead(_handle, (ulong)_position, (uint)_readBufferSize); | ||
|
||
if (data.Length == 0) | ||
{ | ||
|
@@ -726,7 +727,7 @@ public override int ReadByte() | |
// Read more data into the internal buffer if necessary. | ||
if (_bufferPosition >= _bufferLen) | ||
{ | ||
var data = _session.RequestRead(_handle, (ulong) _position, (uint) _readBufferSize); | ||
var data = _session.RequestRead(_handle, (ulong)_position, (uint)_readBufferSize); | ||
if (data.Length == 0) | ||
{ | ||
// We've reached EOF. | ||
|
@@ -783,6 +784,8 @@ public override long Seek(long offset, SeekOrigin origin) | |
return _position; | ||
} | ||
|
||
var attributes = (SftpFileAttributes)null; | ||
|
||
// The behaviour depends upon the read/write mode. | ||
if (_bufferOwnedByWrite) | ||
{ | ||
|
@@ -798,15 +801,16 @@ public override long Seek(long offset, SeekOrigin origin) | |
newPosn = _position + offset; | ||
break; | ||
case SeekOrigin.End: | ||
var attributes = _session.RequestFStat(_handle, false); | ||
newPosn = attributes.Size - offset; | ||
attributes = _session.RequestFStat(_handle, false); | ||
newPosn = attributes.Size + offset; | ||
break; | ||
} | ||
|
||
if (newPosn == -1) | ||
if (newPosn == -1 || (attributes != null && newPosn > attributes.Size)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seeking past the end of file when writing is valid, it's a way to extend the file size. |
||
{ | ||
throw new EndOfStreamException("End of stream."); | ||
} | ||
|
||
_position = newPosn; | ||
} | ||
else | ||
|
@@ -829,7 +833,7 @@ public override long Seek(long offset, SeekOrigin origin) | |
if (newPosn >= (_position - _bufferPosition) && | ||
newPosn < (_position - _bufferPosition + _bufferLen)) | ||
{ | ||
_bufferPosition = (int) (newPosn - (_position - _bufferPosition)); | ||
_bufferPosition = (int)(newPosn - (_position - _bufferPosition)); | ||
_position = newPosn; | ||
return _position; | ||
} | ||
|
@@ -849,12 +853,16 @@ public override long Seek(long offset, SeekOrigin origin) | |
newPosn = _position + offset; | ||
break; | ||
case SeekOrigin.End: | ||
var attributes = _session.RequestFStat(_handle, false); | ||
newPosn = attributes.Size - offset; | ||
if (attributes == null) | ||
{ | ||
attributes = _session.RequestFStat(_handle, false); | ||
} | ||
|
||
newPosn = attributes.Size + offset; | ||
break; | ||
} | ||
|
||
if (newPosn < 0) | ||
if (newPosn < 0 || (attributes != null && newPosn > attributes.Size)) | ||
{ | ||
throw new EndOfStreamException(); | ||
} | ||
|
@@ -974,7 +982,7 @@ public override void Write(byte[] buffer, int offset, int count) | |
{ | ||
using (var wait = new AutoResetEvent(false)) | ||
{ | ||
_session.RequestWrite(_handle, (ulong) _position, buffer, offset, tempLen, wait); | ||
_session.RequestWrite(_handle, (ulong)_position, buffer, offset, tempLen, wait); | ||
} | ||
} | ||
else | ||
|
@@ -996,7 +1004,7 @@ public override void Write(byte[] buffer, int offset, int count) | |
{ | ||
using (var wait = new AutoResetEvent(false)) | ||
{ | ||
_session.RequestWrite(_handle, (ulong) (_position - _bufferPosition), GetOrCreateWriteBuffer(), 0, _bufferPosition, wait); | ||
_session.RequestWrite(_handle, (ulong)(_position - _bufferPosition), GetOrCreateWriteBuffer(), 0, _bufferPosition, wait); | ||
} | ||
|
||
_bufferPosition = 0; | ||
|
@@ -1106,7 +1114,7 @@ public override void WriteByte(byte value) | |
{ | ||
using (var wait = new AutoResetEvent(false)) | ||
{ | ||
_session.RequestWrite(_handle, (ulong) (_position - _bufferPosition), writeBuffer, 0, _bufferPosition, wait); | ||
_session.RequestWrite(_handle, (ulong)(_position - _bufferPosition), writeBuffer, 0, _bufferPosition, wait); | ||
} | ||
|
||
_bufferPosition = 0; | ||
|
@@ -1192,7 +1200,7 @@ private void FlushWriteBuffer() | |
{ | ||
using (var wait = new AutoResetEvent(false)) | ||
{ | ||
_session.RequestWrite(_handle, (ulong) (_position - _bufferPosition), _writeBuffer, 0, _bufferPosition, wait); | ||
_session.RequestWrite(_handle, (ulong)(_position - _bufferPosition), _writeBuffer, 0, _bufferPosition, wait); | ||
} | ||
|
||
_bufferPosition = 0; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's this for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dummy change, will remove it