Skip to content

Commit bcc8def

Browse files
committed
No longer track write position separately.
Reading from the SftpFileStream will not also affect the position at which a subsequent write will be performed. The position at which a write is performed in the server file is now always the current position minus the position in the buffer.
1 parent 99fd7b4 commit bcc8def

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

src/Renci.SshNet/Sftp/SftpFileStream.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public class SftpFileStream : Stream
2828
private bool _canRead;
2929
private bool _canSeek;
3030
private bool _canWrite;
31-
private ulong _serverFilePosition;
3231

3332
private readonly object _lock = new object();
3433

@@ -262,7 +261,6 @@ internal SftpFileStream(ISftpSession session, string path, FileMode mode, FileAc
262261
{
263262
var attributes = _session.RequestFStat(_handle, false);
264263
_position = attributes.Size;
265-
_serverFilePosition = (ulong) attributes.Size;
266264
}
267265
}
268266

@@ -347,9 +345,6 @@ public override int Read(byte[] buffer, int offset, int count)
347345

348346
var data = _session.RequestRead(_handle, (ulong) _position, (uint) _readBufferSize);
349347

350-
// TODO: don't we need to take into account the number of bytes read (data.Length) ?
351-
_serverFilePosition = (ulong) _position;
352-
353348
if (data.Length == 0)
354349
{
355350
break;
@@ -422,11 +417,11 @@ public override int ReadByte()
422417
if (_bufferPosition >= _bufferLen)
423418
{
424419
_bufferPosition = 0;
420+
_bufferLen = 0;
425421

426422
var data = _session.RequestRead(_handle, (ulong) _position, (uint) _readBufferSize);
427423

428424
_bufferLen = data.Length;
429-
_serverFilePosition = (ulong) _position;
430425

431426
if (_bufferLen == 0)
432427
{
@@ -501,7 +496,6 @@ public override long Seek(long offset, SeekOrigin origin)
501496
throw new EndOfStreamException("End of stream.");
502497
}
503498
_position = newPosn;
504-
_serverFilePosition = (ulong)newPosn;
505499
}
506500
else
507501
{
@@ -645,8 +639,7 @@ public override void Write(byte[] buffer, int offset, int count)
645639
{
646640
using (var wait = new AutoResetEvent(false))
647641
{
648-
_session.RequestWrite(_handle, _serverFilePosition, buffer, offset, tempLen, wait);
649-
_serverFilePosition += (ulong) tempLen;
642+
_session.RequestWrite(_handle, (ulong) _position, buffer, offset, tempLen, wait);
650643
}
651644
}
652645
else
@@ -668,8 +661,7 @@ public override void Write(byte[] buffer, int offset, int count)
668661
{
669662
using (var wait = new AutoResetEvent(false))
670663
{
671-
_session.RequestWrite(_handle, _serverFilePosition, _writeBuffer, 0, _bufferPosition, wait);
672-
_serverFilePosition += (ulong) _bufferPosition;
664+
_session.RequestWrite(_handle, (ulong) (_position - _bufferPosition), _writeBuffer, 0, _bufferPosition, wait);
673665
}
674666

675667
_bufferPosition = 0;
@@ -699,8 +691,7 @@ public override void WriteByte(byte value)
699691
{
700692
using (var wait = new AutoResetEvent(false))
701693
{
702-
_session.RequestWrite(_handle, _serverFilePosition, _writeBuffer, 0, _bufferPosition, wait);
703-
_serverFilePosition += (ulong) _bufferPosition;
694+
_session.RequestWrite(_handle, (ulong) (_position - _bufferPosition), _writeBuffer, 0, _bufferPosition, wait);
704695
}
705696

706697
_bufferPosition = 0;
@@ -779,8 +770,7 @@ private void FlushWriteBuffer()
779770
{
780771
using (var wait = new AutoResetEvent(false))
781772
{
782-
_session.RequestWrite(_handle, _serverFilePosition, _writeBuffer, 0, _bufferPosition, wait);
783-
_serverFilePosition += (ulong) _bufferPosition;
773+
_session.RequestWrite(_handle, (ulong) (_position - _bufferPosition), _writeBuffer, 0, _bufferPosition, wait);
784774
}
785775

786776
_bufferPosition = 0;

0 commit comments

Comments
 (0)