Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/libraries/System.IO.FileSystem/tests/FileStream/Seek.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ namespace System.IO.Tests
{
public class FileStream_Seek : FileSystemTest
{
[Fact]
public void SeekAppendModifyThrows()
[Theory]
[InlineData(0)]
[InlineData(10)]
public void SeekAppendModifyThrows(int bufferSize)
{
string fileName = GetTestFilePath();
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
fs.Write(TestBuffer, 0, TestBuffer.Length);
}

using (FileStream fs = new FileStream(fileName, FileMode.Append))
using (FileStream fs = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.Read, bufferSize))
{
long length = fs.Length;
Assert.Throws<IOException>(() => fs.Seek(length - 1, SeekOrigin.Begin));
Expand All @@ -33,6 +35,9 @@ public void SeekAppendModifyThrows()
Assert.Throws<IOException>(() => fs.Seek(-length, SeekOrigin.End));
Assert.Equal(length, fs.Position);

Assert.Throws<IOException>(() => fs.Position = length - 1);
Assert.Equal(length, fs.Position);

fs.Write(TestBuffer);
Assert.Equal(length, fs.Seek(length, SeekOrigin.Begin));
}
Expand Down
30 changes: 28 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/IO/FileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,17 @@ public override long Position
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
}
else if (!CanSeek)
{
if(_strategy.IsClosed)
{
ThrowHelper.ThrowObjectDisposedException_FileClosed();
}

ThrowHelper.ThrowNotSupportedException_UnseekableStream();
}

_strategy.Seek(value, SeekOrigin.Begin);
_strategy.Position = value;
}
}

Expand Down Expand Up @@ -575,7 +584,24 @@ public override void EndWrite(IAsyncResult asyncResult)

public override bool CanSeek => _strategy.CanSeek;

public override long Seek(long offset, SeekOrigin origin) => _strategy.Seek(offset, origin);
public override long Seek(long offset, SeekOrigin origin)
{
if (origin < SeekOrigin.Begin || origin > SeekOrigin.End)
{
throw new ArgumentException(SR.Argument_InvalidSeekOrigin, nameof(origin));
}
else if (!CanSeek)
{
if (_strategy.IsClosed)
{
ThrowHelper.ThrowObjectDisposedException_FileClosed();
}

ThrowHelper.ThrowNotSupportedException_UnseekableStream();
}

return _strategy.Seek(offset, origin);
}

internal Task BaseFlushAsync(CancellationToken cancellationToken)
=> base.FlushAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,7 @@ public override long Position
}
set
{
if (_writePos > 0)
{
FlushWrite();
}

_readPos = 0;
_readLen = 0;

_strategy.Position = value;
Seek(value, SeekOrigin.Begin);
}
}

Expand Down Expand Up @@ -904,9 +896,6 @@ public override void CopyTo(Stream destination, int bufferSize)

public override long Seek(long offset, SeekOrigin origin)
{
EnsureNotClosed();
EnsureCanSeek();

// If we have bytes in the write buffer, flush them out, seek and be done.
if (_writePos > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ internal void OnIncompleteOperation(int expectedBytesTransferred, int actualByte
public sealed override long Position
{
get => _filePosition;
set => _filePosition = value;
set => Seek(value, SeekOrigin.Begin);
}

internal sealed override string Name => _fileHandle.Path ?? SR.IO_UnknownFileName;
Expand Down Expand Up @@ -144,11 +144,6 @@ internal sealed override void Flush(bool flushToDisk)

public sealed override long Seek(long offset, SeekOrigin origin)
{
if (origin < SeekOrigin.Begin || origin > SeekOrigin.End)
throw new ArgumentException(SR.Argument_InvalidSeekOrigin, nameof(origin));
if (_fileHandle.IsClosed) ThrowHelper.ThrowObjectDisposedException_FileClosed();
if (!CanSeek) ThrowHelper.ThrowNotSupportedException_UnseekableStream();

long oldPos = _filePosition;
long pos = origin switch
{
Expand Down