entrystream fixes and fmt

This commit is contained in:
Adam Hathcock
2025-10-29 08:41:05 +00:00
parent aca97c2c6c
commit bb53d1e1c6
8 changed files with 59 additions and 45 deletions

View File

@@ -64,6 +64,11 @@ public class EntryStream : Stream, IStreamStack
protected override void Dispose(bool disposing)
{
if (_isDisposed)
{
return;
}
_isDisposed = true;
if (!(_completed || _reader.Cancelled))
{
SkipEntry();
@@ -81,12 +86,6 @@ public class EntryStream : Stream, IStreamStack
lzmaStream.Flush(); //Lzma over reads. Knock it back
}
}
if (_isDisposed)
{
return;
}
_isDisposed = true;
#if DEBUG_STREAMS
this.DebugDispose(typeof(EntryStream));
#endif
@@ -97,6 +96,11 @@ public class EntryStream : Stream, IStreamStack
#if !NETFRAMEWORK && !NETSTANDARD2_0
public override async ValueTask DisposeAsync()
{
if (_isDisposed)
{
return;
}
_isDisposed = true;
if (!(_completed || _reader.Cancelled))
{
await SkipEntryAsync().ConfigureAwait(false);
@@ -115,10 +119,6 @@ public class EntryStream : Stream, IStreamStack
}
}
if (_isDisposed)
{
return;
}
_isDisposed = true;
#if DEBUG_STREAMS
this.DebugDispose(typeof(EntryStream));
@@ -204,4 +204,11 @@ public class EntryStream : Stream, IStreamStack
public override void Write(byte[] buffer, int offset, int count) =>
throw new NotSupportedException();
public override Task WriteAsync(
byte[] buffer,
int offset,
int count,
CancellationToken cancellationToken
) => throw new NotSupportedException();
}

View File

@@ -10,7 +10,12 @@ internal interface IRarUnpack
void DoUnpack(FileHeader fileHeader, Stream readStream, Stream writeStream);
void DoUnpack();
Task DoUnpackAsync(FileHeader fileHeader, Stream readStream, Stream writeStream, CancellationToken cancellationToken);
Task DoUnpackAsync(
FileHeader fileHeader,
Stream readStream,
Stream writeStream,
CancellationToken cancellationToken
);
Task DoUnpackAsync(CancellationToken cancellationToken);
// eg u/i pause/resume button

View File

@@ -126,7 +126,8 @@ internal class RarBLAKE2spStream : RarStream, IStreamStack
public static RarBLAKE2spStream Create(
IRarUnpack unpack,
FileHeader fileHeader,
MultiVolumeReadOnlyStream readStream)
MultiVolumeReadOnlyStream readStream
)
{
var stream = new RarBLAKE2spStream(unpack, fileHeader, readStream);
stream.Initialize();
@@ -136,7 +137,9 @@ internal class RarBLAKE2spStream : RarStream, IStreamStack
public static async Task<RarBLAKE2spStream> CreateAsync(
IRarUnpack unpack,
FileHeader fileHeader,
MultiVolumeReadOnlyStream readStream, CancellationToken cancellationToken = default)
MultiVolumeReadOnlyStream readStream,
CancellationToken cancellationToken = default
)
{
var stream = new RarBLAKE2spStream(unpack, fileHeader, readStream);
await stream.InitializeAsync(cancellationToken);

View File

@@ -51,7 +51,8 @@ internal class RarCrcStream : RarStream, IStreamStack
public static RarCrcStream Create(
IRarUnpack unpack,
FileHeader fileHeader,
MultiVolumeReadOnlyStream readStream)
MultiVolumeReadOnlyStream readStream
)
{
var stream = new RarCrcStream(unpack, fileHeader, readStream);
stream.Initialize();
@@ -61,7 +62,9 @@ internal class RarCrcStream : RarStream, IStreamStack
public static async Task<RarCrcStream> CreateAsync(
IRarUnpack unpack,
FileHeader fileHeader,
MultiVolumeReadOnlyStream readStream, CancellationToken cancellationToken = default)
MultiVolumeReadOnlyStream readStream,
CancellationToken cancellationToken = default
)
{
var stream = new RarCrcStream(unpack, fileHeader, readStream);
await stream.InitializeAsync(cancellationToken);
@@ -132,22 +135,31 @@ internal class RarCrcStream : RarStream, IStreamStack
System.Threading.CancellationToken cancellationToken = default
)
{
var result = await base.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
if (result != 0)
cancellationToken.ThrowIfCancellationRequested();
var array = System.Buffers.ArrayPool<byte>.Shared.Rent(buffer.Length);
try
{
currentCrc = RarCRC.CheckCrc(currentCrc, buffer.Span.ToArray(), 0, result);
}
else if (
!disableCRC
&& GetCrc() != BitConverter.ToUInt32(readStream.CurrentCrc, 0)
&& buffer.Length != 0
)
{
// NOTE: we use the last FileHeader in a multipart volume to check CRC
throw new InvalidFormatException("file crc mismatch");
}
var result = await base.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
if (result != 0)
{
currentCrc = RarCRC.CheckCrc(currentCrc, buffer.Span, 0, result);
}
else if (
!disableCRC
&& GetCrc() != BitConverter.ToUInt32(readStream.CurrentCrc, 0)
&& buffer.Length != 0
)
{
// NOTE: we use the last FileHeader in a multipart volume to check CRC
throw new InvalidFormatException("file crc mismatch");
}
return result;
return result;
}
finally
{
System.Buffers.ArrayPool<byte>.Shared.Return(array);
}
}
#endif
}

View File

@@ -236,7 +236,7 @@ internal sealed partial class Unpack : BitInput, IRarUnpack
break;
case 50: // rar 5.x compression
await Unpack5Async(fileHeader.IsSolid,cancellationToken).ConfigureAwait(false);
await Unpack5Async(fileHeader.IsSolid, cancellationToken).ConfigureAwait(false);
break;
default:

View File

@@ -1162,7 +1162,5 @@ internal partial class Unpack
Header.TablePresent = (BlockFlags & 0x80) != 0;
return true;
}
}
#endif

View File

@@ -359,4 +359,3 @@ public class RarCRCTest
Assert.Equal(startCrc, result);
}
}

View File

@@ -193,12 +193,7 @@ public class AsyncTests : TestBase
// Test async write with GZipStream
using (var fileStream = File.Create(compressedPath))
using (
var gzipStream = new GZipStream(
fileStream,
CompressionMode.Compress
)
)
using (var gzipStream = new GZipStream(fileStream, CompressionMode.Compress))
{
await gzipStream.WriteAsync(testData, 0, testData.Length);
await gzipStream.FlushAsync();
@@ -209,12 +204,7 @@ public class AsyncTests : TestBase
// Test async read with GZipStream
using (var fileStream = File.OpenRead(compressedPath))
using (
var gzipStream = new GZipStream(
fileStream,
CompressionMode.Decompress
)
)
using (var gzipStream = new GZipStream(fileStream, CompressionMode.Decompress))
{
var decompressed = new byte[testData.Length];
var totalRead = 0;