Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
TarReader should dispose underlying stream if leaveOpen is false (#79899
)
  • Loading branch information
jozkee committed Jan 12, 2023
commit 08e8e19fa64e1a63657877f82f479575de1b6e67
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public TarReader(Stream archiveStream, bool leaveOpen = false)
}

/// <summary>
/// Disposes the current <see cref="TarReader"/> instance, and disposes the non-null <see cref="TarEntry.DataStream"/> instances of all the entries that were read from the archive.
/// Disposes the current <see cref="TarReader"/> instance, closes the archive stream, and disposes the non-null <see cref="TarEntry.DataStream"/> instances of all the entries that were read from the archive if the <c>leaveOpen</c> argument was set to <see langword="false"/> in the constructor.
/// </summary>
/// <remarks>The <see cref="TarEntry.DataStream"/> property of any entry can be replaced with a new stream. If the user decides to replace it on a <see cref="TarEntry"/> instance that was obtained using a <see cref="TarReader"/>, the underlying stream gets disposed immediately, freeing the <see cref="TarReader"/> of origin from the responsibility of having to dispose it.</remarks>
public void Dispose()
Expand All @@ -57,11 +57,16 @@ public void Dispose()
{
_isDisposed = true;

if (!_leaveOpen && _dataStreamsToDispose?.Count > 0)
if (!_leaveOpen)
{
foreach (Stream s in _dataStreamsToDispose)
_archiveStream.Dispose();

if (_dataStreamsToDispose?.Count > 0)
{
s.Dispose();
foreach (Stream s in _dataStreamsToDispose)
{
s.Dispose();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public void TarReader_LeaveOpen_False()
}
}

Assert.Throws<ObjectDisposedException>(() => ms.ReadByte());

Assert.True(dataStreams.Any());
foreach (Stream ds in dataStreams)
{
Expand All @@ -62,6 +64,8 @@ public void TarReader_LeaveOpen_True()
}
}

ms.ReadByte(); // Should not throw

Assert.True(dataStreams.Any());
foreach (Stream ds in dataStreams)
{
Expand Down