[pascal] Added filesystem consistency checks.

This commit is contained in:
2026-02-06 15:41:18 +00:00
parent 61e7470305
commit 1a93768665

View File

@@ -35,6 +35,7 @@ using Aaru.CommonTypes.AaruMetadata;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.Helpers;
using Aaru.Logging;
using Claunia.Encoding;
using Encoding = System.Text.Encoding;
using FileSystemInfo = Aaru.CommonTypes.Structs.FileSystemInfo;
@@ -124,6 +125,9 @@ public sealed partial class PascalPlugin
offset += entrySize;
}
// Validate filesystem consistency
ValidateFilesystem();
errno = _device.ReadSectors(0, false, 2 * _multiplier, out _bootBlocks, out _);
if(errno != ErrorNumber.NoError) return errno;
@@ -169,6 +173,141 @@ public sealed partial class PascalPlugin
return volEntry.Files >= 0;
}
/// <summary>Validates filesystem consistency and logs any issues found</summary>
void ValidateFilesystem()
{
var errorCount = 0;
// Check 1: File count matches volume header
if(_fileEntries.Count != _mountedVolEntry.Files)
{
AaruLogging.Error("File count mismatch: volume header says {0} files, but found {1} directory entries",
_mountedVolEntry.Files,
_fileEntries.Count);
errorCount++;
}
if(_fileEntries.Count == 0)
{
if(errorCount > 0) AaruLogging.Error("Filesystem validation completed with {0} issue(s)", errorCount);
return;
}
// Check 2: Files are in block order
for(var i = 1; i < _fileEntries.Count; i++)
{
if(_fileEntries[i - 1].LastBlock > _fileEntries[i].FirstBlock)
{
AaruLogging.Error("Directory entries not in block order: '{0}' (ends at block {1}) comes before '{2}' (starts at block {3})",
StringHandlers.PascalToString(_fileEntries[i - 1].Filename, _encoding),
_fileEntries[i - 1].LastBlock,
StringHandlers.PascalToString(_fileEntries[i].Filename, _encoding),
_fileEntries[i].FirstBlock);
errorCount++;
break; // Only report once
}
}
// Check 3: No block overlaps - first file must start after directory
int expectedFirstBlock = _mountedVolEntry.LastBlock;
foreach(PascalFileEntry entry in _fileEntries)
{
string fileName = StringHandlers.PascalToString(entry.Filename, _encoding);
// File starts before expected position (overlap with previous file or directory)
if(entry.FirstBlock < expectedFirstBlock)
{
AaruLogging.Error("File '{0}' starts at block {1}, but expected block {2} or later (block overlap detected)",
fileName,
entry.FirstBlock,
expectedFirstBlock);
errorCount++;
}
// File ends before it starts (invalid)
if(entry.LastBlock <= entry.FirstBlock)
{
AaruLogging.Error("File '{0}' has invalid block range: FirstBlock={1}, LastBlock={2}",
fileName,
entry.FirstBlock,
entry.LastBlock);
errorCount++;
continue;
}
// File extends beyond end of volume
if(entry.LastBlock > _mountedVolEntry.Blocks)
{
AaruLogging.Error("File '{0}' extends beyond end of volume: LastBlock={1}, VolumeBlocks={2}",
fileName,
entry.LastBlock,
_mountedVolEntry.Blocks);
errorCount++;
}
// Validate LastBytes field (should be 1-512)
if(entry.LastBytes <= 0 || entry.LastBytes > 512)
{
AaruLogging.Error("File '{0}' has invalid LastBytes value: {1} (expected 1-512)",
fileName,
entry.LastBytes);
errorCount++;
}
// Validate file type
if(entry.EntryType < PascalFileKind.Bad || entry.EntryType > PascalFileKind.Secure)
{
AaruLogging.Error("File '{0}' has invalid file type: {1}", fileName, (int)entry.EntryType);
errorCount++;
}
// Validate filename length
if(entry.Filename[0] == 0 || entry.Filename[0] > 15)
{
AaruLogging.Error("File '{0}' has invalid filename length: {1} (expected 1-15)",
fileName,
entry.Filename[0]);
errorCount++;
}
// Update expected first block for next file
expectedFirstBlock = entry.LastBlock;
}
// Check 4: Last file doesn't extend beyond volume
if(_fileEntries.Count > 0)
{
PascalFileEntry lastEntry = _fileEntries[^1];
if(lastEntry.LastBlock > _mountedVolEntry.Blocks)
{
AaruLogging.Error("Last file '{0}' extends beyond volume: ends at block {1}, volume has {2} blocks",
StringHandlers.PascalToString(lastEntry.Filename, _encoding),
lastEntry.LastBlock,
_mountedVolEntry.Blocks);
errorCount++;
}
}
if(errorCount > 0)
AaruLogging.Error("Filesystem validation completed with {0} issue(s)", errorCount);
else
AaruLogging.Debug(MODULE_NAME, "Filesystem validation passed");
}
/// <inheritdoc />
public ErrorNumber Unmount()
{