Preemptive FileTypes cleanup

This commit is contained in:
Matt Nadareski
2025-01-04 20:24:56 -05:00
parent f5e2d8a11c
commit c449f34410
11 changed files with 29 additions and 119 deletions

View File

@@ -60,11 +60,8 @@ namespace SabreTools.FileTypes.Archives
/// <summary>
/// Create a new TorrentGZipArchive from the given file
/// </summary>
/// <param name="filename">Name of the file to use as an archive</param>
/// <param name="read">True for opening file as read, false for opening file as write</param>
/// <param name="getHashes">True if hashes for this file should be calculated, false otherwise (default)</param>
public GZipArchive(string filename, bool getHashes = false)
: base(filename, getHashes)
public GZipArchive(string filename)
: base(filename)
{
}

View File

@@ -31,10 +31,8 @@ namespace SabreTools.FileTypes.Archives
/// Create a new TorrentRARArchive from the given file
/// </summary>
/// <param name="filename">Name of the file to use as an archive</param>
/// <param name="read">True for opening file as read, false for opening file as write</param>
/// <param name="getHashes">True if hashes for this file should be calculated, false otherwise (default)</param>
public RarArchive(string filename, bool getHashes = false)
: base(filename, getHashes)
public RarArchive(string filename)
: base(filename)
{
}

View File

@@ -38,9 +38,8 @@ namespace SabreTools.FileTypes.Archives
/// </summary>
/// <param name="filename">Name of the file to use as an archive</param>
/// <param name="read">True for opening file as read, false for opening file as write</param>
/// <param name="getHashes">True if hashes for this file should be calculated, false otherwise (default)</param>
public SevenZipArchive(string filename, bool getHashes = false)
: base(filename, getHashes)
public SevenZipArchive(string filename)
: base(filename)
{
}

View File

@@ -36,10 +36,8 @@ namespace SabreTools.FileTypes.Archives
/// Create a new Tape archive from the given file
/// </summary>
/// <param name="filename">Name of the file to use as an archive</param>
/// <param name="read">True for opening file as read, false for opening file as write</param>
/// <param name="getHashes">True if hashes for this file should be calculated, false otherwise (default)</param>
public TapeArchive(string filename, bool getHashes = false)
: base(filename, getHashes)
public TapeArchive(string filename)
: base(filename)
{
}

View File

@@ -46,10 +46,8 @@ namespace SabreTools.FileTypes.Archives
/// Create a new TorrentGZipArchive from the given file
/// </summary>
/// <param name="filename">Name of the file to use as an archive</param>
/// <param name="read">True for opening file as read, false for opening file as write</param>
/// <param name="getHashes">True if hashes for this file should be calculated, false otherwise (default)</param>
public XZArchive(string filename, bool getHashes = false)
: base(filename, getHashes)
public XZArchive(string filename)
: base(filename)
{
}

View File

@@ -44,10 +44,8 @@ namespace SabreTools.FileTypes.Archives
/// Create a new TorrentZipArchive from the given file
/// </summary>
/// <param name="filename">Name of the file to use as an archive</param>
/// <param name="read">True for opening file as read, false for opening file as write</param>
/// <param name="getHashes">True if hashes for this file should be calculated, false otherwise (default)</param>
public ZipArchive(string filename, bool getHashes = false)
: base(filename, getHashes)
public ZipArchive(string filename)
: base(filename)
{
}

View File

@@ -34,12 +34,11 @@ namespace SabreTools.FileTypes
}
/// <summary>
/// Create a new Archive from the given file
/// Create a new BaseArchive from the given file
/// </summary>
/// <param name="filename">Name of the file to use as an archive</param>
/// <param name="getHashes">True if hashes for this file should be calculated, false otherwise (default)</param>
public BaseArchive(string filename, bool getHashes = false)
: base(filename, getHashes)
/// <param name="filename">Name of the file to use</param>
public BaseArchive(string filename)
: base(filename)
{
}

View File

@@ -14,25 +14,16 @@ namespace SabreTools.FileTypes
protected static readonly byte[] SevenZipSignature = [0x37, 0x7a, 0xbc, 0xaf, 0x27, 0x1c];
protected static readonly byte[] AaruFormatSignature = [0x41, 0x41, 0x52, 0x55, 0x46, 0x52, 0x4d, 0x54];
protected static readonly byte[] BZ2Signature = [0x42, 0x5a, 0x68];
protected static readonly byte[] CabinetSignature = [0x4d, 0x53, 0x43, 0x46];
protected static readonly byte[] CHDSignature = [0x4d, 0x43, 0x6f, 0x6d, 0x70, 0x72, 0x48, 0x44];
protected static readonly byte[] ELFSignature = [0x7f, 0x45, 0x4c, 0x46];
protected static readonly byte[] FreeArcSignature = [0x41, 0x72, 0x43, 0x01];
protected static readonly byte[] GzSignature = [0x1f, 0x8b, 0x08];
protected static readonly byte[] PESignature = [0x4d, 0x5a];
protected static readonly byte[] RarSignature = [0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x00];
protected static readonly byte[] RarFiveSignature = [0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x01, 0x00];
protected static readonly byte[] TarSignature = [0x75, 0x73, 0x74, 0x61, 0x72, 0x20, 0x20, 0x00];
protected static readonly byte[] TarZeroSignature = [0x75, 0x73, 0x74, 0x61, 0x72, 0x00, 0x30, 0x30];
protected static readonly byte[] XZSignature = [0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00, 0x00];
protected static readonly byte[] ZipSignature = [0x50, 0x4b, 0x03, 0x04];
protected static readonly byte[] ZipSignatureEmpty = [0x50, 0x4b, 0x05, 0x06];
protected static readonly byte[] ZipSignatureSpanned = [0x50, 0x4b, 0x07, 0x08];
#endregion
// TODO: Get all of these values automatically so there is no public "set"
#region Fields
/// <summary>
@@ -110,56 +101,9 @@ namespace SabreTools.FileTypes
/// Create a new BaseFile from the given file
/// </summary>
/// <param name="filename">Name of the file to use</param>
/// <param name="getHashes">True if hashes for this file should be calculated (default), false otherwise</param>
public BaseFile(string filename, bool getHashes = true)
public BaseFile(string filename)
{
Filename = filename;
if (getHashes)
{
BaseFile? temp = GetInfo(Filename, hashes: AvailableHashTypes);
if (temp != null)
{
Parent = temp.Parent;
Date = temp.Date;
CRC = temp.CRC;
MD5 = temp.MD5;
SHA1 = temp.SHA1;
SHA256 = temp.SHA256;
SHA384 = temp.SHA384;
SHA512 = temp.SHA512;
SpamSum = temp.SpamSum;
}
}
}
/// <summary>
/// Create a new BaseFile from the given file
/// </summary>
/// <param name="filename">Name of the file to use</param>
/// <param name="stream">Stream to populate information from</param>
/// <param name="getHashes">True if hashes for this file should be calculated (default), false otherwise</param>
public BaseFile(string filename, Stream stream, bool getHashes = true)
{
Filename = filename;
if (getHashes)
{
BaseFile temp = GetInfo(stream, hashes: AvailableHashTypes);
if (temp != null)
{
Parent = temp.Parent;
Date = temp.Date;
CRC = temp.CRC;
MD5 = temp.MD5;
SHA1 = temp.SHA1;
SHA256 = temp.SHA256;
SHA384 = temp.SHA384;
SHA512 = temp.SHA512;
SpamSum = temp.SpamSum;
}
}
}
#endregion
@@ -221,9 +165,9 @@ namespace SabreTools.FileTypes
{
outFileType = FileType.XZArchive;
}
else if (magic.StartsWith(ZipSignature)
|| magic.StartsWith(ZipSignatureEmpty)
|| magic.StartsWith(ZipSignatureSpanned))
else if (magic.StartsWith(Models.PKZIP.Constants.LocalFileHeaderSignatureBytes)
|| magic.StartsWith(Models.PKZIP.Constants.EndOfCentralDirectoryRecordSignatureBytes)
|| magic.StartsWith(Models.PKZIP.Constants.DataDescriptorSignatureBytes))
{
outFileType = FileType.ZipArchive;
}

View File

@@ -3,10 +3,6 @@ using SabreTools.Models.CHD;
namespace SabreTools.FileTypes.CHD
{
/// <summary>
/// This is code adapted from chd.h and chd.cpp in MAME
/// Additional archival code from https://github.com/rtissera/libchdr/blob/master/src/chd.h
/// </summary>
public class CHDFile : BaseFile
{
#region Private instance variables

View File

@@ -1,17 +0,0 @@
namespace SabreTools.FileTypes.CHD
{
internal static class Constants
{
public const string SignatureString = "MComprHD";
#region Header Sizes
public const int HeaderV1Size = 76;
public const int HeaderV2Size = 80;
public const int HeaderV3Size = 120;
public const int HeaderV4Size = 108;
public const int HeaderV5Size = 124;
#endregion
}
}

View File

@@ -31,14 +31,14 @@ namespace SabreTools.FileTypes
/// <summary>
/// Flag specific to Folder to omit Machine name from output path
/// </summary>
private readonly bool _writeToParent = false;
private readonly bool _writeToParent;
#endregion
#region Constructors
/// <summary>
/// Create a new folder with no base file
/// Create a new Folder with no base file
/// </summary>
/// <param name="writeToParent">True to write directly to parent, false otherwise</param>
public Folder(bool writeToParent = false)
@@ -49,14 +49,14 @@ namespace SabreTools.FileTypes
}
/// <summary>
/// Create a new folder from the given file
/// Create a new Folder from the given file
/// </summary>
/// <param name="filename">Name of the file to use as an archive</param>
/// <param name="read">True for opening file as read, false for opening file as write</param>
/// <param name="getHashes">True if hashes for this file should be calculated, false otherwise (default)</param>
public Folder(string filename, bool getHashes = false)
: base(filename, getHashes)
/// <param name="filename">Name of the folder to use</param>
/// <param name="writeToParent">True to write directly to parent, false otherwise</param>
public Folder(string filename, bool writeToParent = false)
: base(filename)
{
_writeToParent = writeToParent;
logger = new Logger(this);
}
@@ -271,7 +271,7 @@ namespace SabreTools.FileTypes
foreach (string dir in Directory.EnumerateDirectories(Filename, "*", SearchOption.TopDirectoryOnly))
#endif
{
Folder fl = new(dir);
var fl = new Folder(dir);
_children.Add(fl);
}
}