Skip to content

Commit

Permalink
Merge pull request #832 from adamhathcock/remove-ignored-nulls
Browse files Browse the repository at this point in the history
Remove ignored nulls
  • Loading branch information
adamhathcock committed Apr 23, 2024
2 parents 900190c + 095b5f7 commit d26f020
Show file tree
Hide file tree
Showing 75 changed files with 455 additions and 442 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"csharpier": {
"version": "0.27.3",
"version": "0.28.1",
"commands": [
"dotnet-csharpier"
]
Expand Down
50 changes: 23 additions & 27 deletions src/SharpCompress/Archives/AbstractArchive.cs
Expand Up @@ -12,39 +12,35 @@ public abstract class AbstractArchive<TEntry, TVolume> : IArchive, IArchiveExtra
where TEntry : IArchiveEntry
where TVolume : IVolume
{
private readonly LazyReadOnlyCollection<TVolume> lazyVolumes;
private readonly LazyReadOnlyCollection<TEntry> lazyEntries;
private readonly LazyReadOnlyCollection<TVolume> _lazyVolumes;
private readonly LazyReadOnlyCollection<TEntry> _lazyEntries;
private bool _disposed;
private readonly SourceStream? _sourceStream;

public event EventHandler<ArchiveExtractionEventArgs<IArchiveEntry>>? EntryExtractionBegin;
public event EventHandler<ArchiveExtractionEventArgs<IArchiveEntry>>? EntryExtractionEnd;

public event EventHandler<CompressedBytesReadEventArgs>? CompressedBytesRead;
public event EventHandler<FilePartExtractionBeginEventArgs>? FilePartExtractionBegin;

protected ReaderOptions ReaderOptions { get; }

private bool disposed;
protected SourceStream SrcStream;

internal AbstractArchive(ArchiveType type, SourceStream srcStream)
internal AbstractArchive(ArchiveType type, SourceStream sourceStream)
{
Type = type;
ReaderOptions = srcStream.ReaderOptions;
SrcStream = srcStream;
lazyVolumes = new LazyReadOnlyCollection<TVolume>(LoadVolumes(SrcStream));
lazyEntries = new LazyReadOnlyCollection<TEntry>(LoadEntries(Volumes));
ReaderOptions = sourceStream.ReaderOptions;
_sourceStream = sourceStream;
_lazyVolumes = new LazyReadOnlyCollection<TVolume>(LoadVolumes(_sourceStream));
_lazyEntries = new LazyReadOnlyCollection<TEntry>(LoadEntries(Volumes));
}

#nullable disable
internal AbstractArchive(ArchiveType type)
{
Type = type;
lazyVolumes = new LazyReadOnlyCollection<TVolume>(Enumerable.Empty<TVolume>());
lazyEntries = new LazyReadOnlyCollection<TEntry>(Enumerable.Empty<TEntry>());
ReaderOptions = new();
_lazyVolumes = new LazyReadOnlyCollection<TVolume>(Enumerable.Empty<TVolume>());
_lazyEntries = new LazyReadOnlyCollection<TEntry>(Enumerable.Empty<TEntry>());
}

#nullable enable

public ArchiveType Type { get; }

void IArchiveExtractionListener.FireEntryExtractionBegin(IArchiveEntry entry) =>
Expand All @@ -65,12 +61,12 @@ private static Stream CheckStreams(Stream stream)
/// <summary>
/// Returns an ReadOnlyCollection of all the RarArchiveEntries across the one or many parts of the RarArchive.
/// </summary>
public virtual ICollection<TEntry> Entries => lazyEntries;
public virtual ICollection<TEntry> Entries => _lazyEntries;

/// <summary>
/// Returns an ReadOnlyCollection of all the RarArchiveVolumes across the one or many parts of the RarArchive.
/// </summary>
public ICollection<TVolume> Volumes => lazyVolumes;
public ICollection<TVolume> Volumes => _lazyVolumes;

/// <summary>
/// The total size of the files compressed in the archive.
Expand All @@ -84,29 +80,29 @@ private static Stream CheckStreams(Stream stream)
public virtual long TotalUncompressSize =>
Entries.Aggregate(0L, (total, cf) => total + cf.Size);

protected abstract IEnumerable<TVolume> LoadVolumes(SourceStream srcStream);
protected abstract IEnumerable<TVolume> LoadVolumes(SourceStream sourceStream);
protected abstract IEnumerable<TEntry> LoadEntries(IEnumerable<TVolume> volumes);

IEnumerable<IArchiveEntry> IArchive.Entries => Entries.Cast<IArchiveEntry>();

IEnumerable<IVolume> IArchive.Volumes => lazyVolumes.Cast<IVolume>();
IEnumerable<IVolume> IArchive.Volumes => _lazyVolumes.Cast<IVolume>();

public virtual void Dispose()
{
if (!disposed)
if (!_disposed)
{
lazyVolumes.ForEach(v => v.Dispose());
lazyEntries.GetLoaded().Cast<Entry>().ForEach(x => x.Close());
SrcStream?.Dispose();
_lazyVolumes.ForEach(v => v.Dispose());
_lazyEntries.GetLoaded().Cast<Entry>().ForEach(x => x.Close());
_sourceStream?.Dispose();

disposed = true;
_disposed = true;
}
}

void IArchiveExtractionListener.EnsureEntriesLoaded()
{
lazyEntries.EnsureFullyLoaded();
lazyVolumes.EnsureFullyLoaded();
_lazyEntries.EnsureFullyLoaded();
_lazyVolumes.EnsureFullyLoaded();
}

void IExtractionListener.FireCompressedBytesRead(
Expand Down
8 changes: 6 additions & 2 deletions src/SharpCompress/Archives/AbstractWritableArchive.cs
Expand Up @@ -41,8 +41,8 @@ public void Dispose()
internal AbstractWritableArchive(ArchiveType type)
: base(type) { }

internal AbstractWritableArchive(ArchiveType type, SourceStream srcStream)
: base(type, srcStream) { }
internal AbstractWritableArchive(ArchiveType type, SourceStream sourceStream)
: base(type, sourceStream) { }

public override ICollection<TEntry> Entries
{
Expand Down Expand Up @@ -120,6 +120,10 @@ private bool DoesKeyMatchExisting(string key)
{
foreach (var path in Entries.Select(x => x.Key))
{
if (path is null)
{
continue;
}
var p = path.Replace('/', '\\');
if (p.Length > 0 && p[0] == '\\')
{
Expand Down
22 changes: 12 additions & 10 deletions src/SharpCompress/Archives/GZip/GZipArchive.cs
Expand Up @@ -90,7 +90,7 @@ public static GZipArchive Open(Stream stream, ReaderOptions? readerOptions = nul
{
stream.CheckNotNull(nameof(stream));
return new GZipArchive(
new SourceStream(stream, i => null, readerOptions ?? new ReaderOptions())
new SourceStream(stream, _ => null, readerOptions ?? new ReaderOptions())
);
}

Expand All @@ -99,16 +99,14 @@ public static GZipArchive Open(Stream stream, ReaderOptions? readerOptions = nul
/// <summary>
/// Constructor with a SourceStream able to handle FileInfo and Streams.
/// </summary>
/// <param name="srcStream"></param>
/// <param name="options"></param>
internal GZipArchive(SourceStream srcStream)
: base(ArchiveType.Tar, srcStream) { }
/// <param name="sourceStream"></param>
private GZipArchive(SourceStream sourceStream)
: base(ArchiveType.Tar, sourceStream) { }

protected override IEnumerable<GZipVolume> LoadVolumes(SourceStream srcStream)
protected override IEnumerable<GZipVolume> LoadVolumes(SourceStream sourceStream)
{
srcStream.LoadAllParts();
var idx = 0;
return srcStream.Streams.Select(a => new GZipVolume(a, ReaderOptions, idx++));
sourceStream.LoadAllParts();
return sourceStream.Streams.Select(a => new GZipVolume(a, ReaderOptions, 0));
}

public static bool IsGZipFile(string filePath) => IsGZipFile(new FileInfo(filePath));
Expand Down Expand Up @@ -184,7 +182,11 @@ IEnumerable<GZipArchiveEntry> newEntries
foreach (var entry in oldEntries.Concat(newEntries).Where(x => !x.IsDirectory))
{
using var entryStream = entry.OpenEntryStream();
writer.Write(entry.Key, entryStream, entry.LastModifiedTime);
writer.Write(
entry.Key.NotNull("Entry Key is null"),
entryStream,
entry.LastModifiedTime
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs
Expand Up @@ -6,7 +6,7 @@ namespace SharpCompress.Archives.GZip;

public class GZipArchiveEntry : GZipEntry, IArchiveEntry
{
internal GZipArchiveEntry(GZipArchive archive, GZipFilePart part)
internal GZipArchiveEntry(GZipArchive archive, GZipFilePart? part)
: base(part) => Archive = archive;

public virtual Stream OpenEntryStream()
Expand Down
4 changes: 1 addition & 3 deletions src/SharpCompress/Archives/GZip/GZipWritableArchiveEntry.cs
@@ -1,5 +1,3 @@
#nullable disable

using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -32,7 +30,7 @@ bool closeStream

public override long Crc => 0;

public override string Key { get; }
public override string? Key { get; }

public override long CompressedSize => 0;

Expand Down
6 changes: 1 addition & 5 deletions src/SharpCompress/Archives/IArchiveEntryExtensions.cs
Expand Up @@ -17,15 +17,11 @@ public static void WriteTo(this IArchiveEntry archiveEntry, Stream streamToWrite
streamListener.EnsureEntriesLoaded();
streamListener.FireEntryExtractionBegin(archiveEntry);
streamListener.FireFilePartExtractionBegin(
archiveEntry.Key,
archiveEntry.Key ?? "Key",
archiveEntry.Size,
archiveEntry.CompressedSize
);
var entryStream = archiveEntry.OpenEntryStream();
if (entryStream is null)
{
return;
}
using (entryStream)
{
using Stream s = new ListeningStream(streamListener, entryStream);
Expand Down
3 changes: 1 addition & 2 deletions src/SharpCompress/Archives/IArchiveExtensions.cs
Expand Up @@ -3,7 +3,6 @@
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using SharpCompress.Common;

namespace SharpCompress.Archives;
Expand Down Expand Up @@ -59,7 +58,7 @@ public static class IArchiveExtensions
}

// Create each directory
var path = Path.Combine(destination, entry.Key);
var path = Path.Combine(destination, entry.Key.NotNull("Entry Key is null"));
if (Path.GetDirectoryName(path) is { } directory && seenDirectories.Add(path))
{
Directory.CreateDirectory(directory);
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/Rar/FileInfoRarArchiveVolume.cs
Expand Up @@ -13,7 +13,7 @@ namespace SharpCompress.Archives.Rar;
/// </summary>
internal class FileInfoRarArchiveVolume : RarVolume
{
internal FileInfoRarArchiveVolume(FileInfo fileInfo, ReaderOptions options, int index = 0)
internal FileInfoRarArchiveVolume(FileInfo fileInfo, ReaderOptions options, int index)
: base(StreamingMode.Seekable, fileInfo.OpenRead(), FixOptions(options), index)
{
FileInfo = fileInfo;
Expand Down
32 changes: 15 additions & 17 deletions src/SharpCompress/Archives/Rar/RarArchive.cs
Expand Up @@ -21,35 +21,33 @@ public class RarArchive : AbstractArchive<RarArchiveEntry, RarVolume>
/// <summary>
/// Constructor with a SourceStream able to handle FileInfo and Streams.
/// </summary>
/// <param name="srcStream"></param>
/// <param name="options"></param>
internal RarArchive(SourceStream srcStream)
: base(ArchiveType.Rar, srcStream) { }
/// <param name="sourceStream"></param>
private RarArchive(SourceStream sourceStream)
: base(ArchiveType.Rar, sourceStream) { }

protected override IEnumerable<RarArchiveEntry> LoadEntries(IEnumerable<RarVolume> volumes) =>
RarArchiveEntryFactory.GetEntries(this, volumes, ReaderOptions);

protected override IEnumerable<RarVolume> LoadVolumes(SourceStream srcStream)
protected override IEnumerable<RarVolume> LoadVolumes(SourceStream sourceStream)
{
SrcStream.LoadAllParts(); //request all streams
var streams = SrcStream.Streams.ToArray();
var idx = 0;
sourceStream.LoadAllParts(); //request all streams
var streams = sourceStream.Streams.ToArray();
var i = 0;
if (streams.Length > 1 && IsRarFile(streams[1], ReaderOptions)) //test part 2 - true = multipart not split
{
SrcStream.IsVolumes = true;
sourceStream.IsVolumes = true;
streams[1].Position = 0;
SrcStream.Position = 0;
sourceStream.Position = 0;

return srcStream.Streams.Select(a => new StreamRarArchiveVolume(
return sourceStream.Streams.Select(a => new StreamRarArchiveVolume(
a,
ReaderOptions,
idx++
i++
));
}
else //split mode or single file
{
return new StreamRarArchiveVolume(SrcStream, ReaderOptions, idx++).AsEnumerable();
}

//split mode or single file
return new StreamRarArchiveVolume(sourceStream, ReaderOptions, i++).AsEnumerable();
}

protected override IReader CreateReaderForSolidExtraction()
Expand Down Expand Up @@ -108,7 +106,7 @@ public static RarArchive Open(FileInfo fileInfo, ReaderOptions? options = null)
public static RarArchive Open(Stream stream, ReaderOptions? options = null)
{
stream.CheckNotNull(nameof(stream));
return new RarArchive(new SourceStream(stream, i => null, options ?? new ReaderOptions()));
return new RarArchive(new SourceStream(stream, _ => null, options ?? new ReaderOptions()));
}

/// <summary>
Expand Down
20 changes: 10 additions & 10 deletions src/SharpCompress/Archives/Rar/SeekableFilePart.cs
Expand Up @@ -6,8 +6,8 @@ namespace SharpCompress.Archives.Rar;

internal class SeekableFilePart : RarFilePart
{
private readonly Stream stream;
private readonly string? password;
private readonly Stream _stream;
private readonly string? _password;

internal SeekableFilePart(
MarkHeader mh,
Expand All @@ -18,27 +18,27 @@ internal class SeekableFilePart : RarFilePart
)
: base(mh, fh, index)
{
this.stream = stream;
this.password = password;
_stream = stream;
_password = password;
}

internal override Stream GetCompressedStream()
{
stream.Position = FileHeader.DataStartPosition;
_stream.Position = FileHeader.DataStartPosition;

if (FileHeader.R4Salt != null)
{
var cryptKey = new CryptKey3(password!);
return new RarCryptoWrapper(stream, FileHeader.R4Salt, cryptKey);
var cryptKey = new CryptKey3(_password!);
return new RarCryptoWrapper(_stream, FileHeader.R4Salt, cryptKey);
}

if (FileHeader.Rar5CryptoInfo != null)
{
var cryptKey = new CryptKey5(password!, FileHeader.Rar5CryptoInfo);
return new RarCryptoWrapper(stream, FileHeader.Rar5CryptoInfo.Salt, cryptKey);
var cryptKey = new CryptKey5(_password!, FileHeader.Rar5CryptoInfo);
return new RarCryptoWrapper(_stream, FileHeader.Rar5CryptoInfo.Salt, cryptKey);
}

return stream;
return _stream;
}

internal override string FilePartName => "Unknown Stream - File Entry: " + FileHeader.FileName;
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/Rar/StreamRarArchiveVolume.cs
Expand Up @@ -9,7 +9,7 @@ namespace SharpCompress.Archives.Rar;

internal class StreamRarArchiveVolume : RarVolume
{
internal StreamRarArchiveVolume(Stream stream, ReaderOptions options, int index = 0)
internal StreamRarArchiveVolume(Stream stream, ReaderOptions options, int index)
: base(StreamingMode.Seekable, stream, options, index) { }

internal override IEnumerable<RarFilePart> ReadFileParts() => GetVolumeFileParts();
Expand Down

0 comments on commit d26f020

Please sign in to comment.