From 0e62c129fdf85f2da889f49a0186c01ed154ec9a Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Fri, 10 Apr 2026 23:57:31 +0100 Subject: [PATCH] [HPFS] Add support for analyze command --- Aaru.Filesystems/HPFS/Analyze.cs | 252 +++++++++++++++++++++++++++++++ 1 file changed, 252 insertions(+) create mode 100644 Aaru.Filesystems/HPFS/Analyze.cs diff --git a/Aaru.Filesystems/HPFS/Analyze.cs b/Aaru.Filesystems/HPFS/Analyze.cs new file mode 100644 index 000000000..c32d8db70 --- /dev/null +++ b/Aaru.Filesystems/HPFS/Analyze.cs @@ -0,0 +1,252 @@ +// /*************************************************************************** +// Aaru Data Preservation Suite +// ---------------------------------------------------------------------------- +// +// Filename : Analyze.cs +// Author(s) : Natalia Portillo +// +// Component : OS/2 High Performance File System plugin. +// +// --[ License ] -------------------------------------------------------------- +// +// This library is free software; you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation; either version 2.1 of the +// License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, see . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2026 Natalia Portillo +// ****************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Linq; +using Aaru.CommonTypes; +using Aaru.CommonTypes.Enums; +using Aaru.CommonTypes.Structs; + +namespace Aaru.Filesystems; + +/// +public sealed partial class HPFS +{ +#region IReadOnlyFilesystem Members + + /// + public ErrorNumber GetFilesWithAffectedSectors(IEnumerable<(ulong Start, ulong End)> sectorExtents, + out List files, + InitProgressHandler initProgress = null, + UpdateProgressHandler updateProgress = null, + PulseProgressHandler pulseProgress = null, + EndProgressHandler endProgress = null) + { + files = []; + + if(!_mounted) return ErrorNumber.AccessDenied; + if(sectorExtents is null) return ErrorNumber.InvalidArgument; + + List<(ulong Start, ulong End)> normalizedExtents = NormalizeAnalyzeExtents(sectorExtents); + + if(normalizedExtents.Count == 0 || _rootDirectoryCache is null || _rootDirectoryCache.Count == 0) + return ErrorNumber.NoError; + + try + { + initProgress?.Invoke(); + + return TraverseDirectoryForAffectedSectors("/", + _rootFnode, + normalizedExtents, + files, + updateProgress, + pulseProgress); + } + finally + { + endProgress?.Invoke(); + } + } + +#endregion + + ErrorNumber TraverseDirectoryForAffectedSectors(string path, uint directoryFnode, + IReadOnlyList<(ulong Start, ulong End)> sectorExtents, + List files, UpdateProgressHandler updateProgress, + PulseProgressHandler pulseProgress) + { + pulseProgress?.Invoke(path); + + ErrorNumber errno = GetDirectoryEntries(path, directoryFnode, out Dictionary directoryEntries); + + if(errno != ErrorNumber.NoError) return errno; + + KeyValuePair[] orderedEntries = directoryEntries + .Where(static entry => !string.IsNullOrWhiteSpace(entry.Key)) + .OrderBy(static entry => entry.Key, StringComparer.Ordinal) + .ToArray(); + + long maximum = orderedEntries.Length > 0 ? orderedEntries.Length : 1; + + for(var i = 0; i < orderedEntries.Length; i++) + { + string entryPath = path == "/" ? "/" + orderedEntries[i].Key : path + "/" + orderedEntries[i].Key; + + updateProgress?.Invoke(entryPath, i + 1, maximum); + + errno = ReadFNode(orderedEntries[i].Value, out FNode fnode); + + if(errno != ErrorNumber.NoError) return errno; + + if(fnode.IsDirectory) + { + errno = TraverseDirectoryForAffectedSectors(entryPath, + orderedEntries[i].Value, + sectorExtents, + files, + updateProgress, + pulseProgress); + + if(errno != ErrorNumber.NoError) return errno; + + continue; + } + + errno = AddOverlappingFile(entryPath, orderedEntries[i].Value, fnode, sectorExtents, files); + + if(errno != ErrorNumber.NoError) return errno; + } + + return ErrorNumber.NoError; + } + + ErrorNumber GetDirectoryEntries(string path, uint directoryFnode, out Dictionary directoryEntries) + { + if(path == "/") + { + directoryEntries = new Dictionary(_rootDirectoryCache, StringComparer.Ordinal); + + return ErrorNumber.NoError; + } + + return ReadDirectoryEntries(directoryFnode, out directoryEntries); + } + + ErrorNumber AddOverlappingFile(string path, uint fnodeSector, in FNode fnode, + IReadOnlyList<(ulong Start, ulong End)> sectorExtents, List files) + { + if(fnode.file_size == 0) return ErrorNumber.NoError; + + ErrorNumber errno = FindOverlappingExtents(fnode, + fnode.file_size, + sectorExtents, + out List<(ulong Start, ulong End)> overlaps); + + if(errno != ErrorNumber.NoError) return errno; + + if(overlaps.Count == 0) return ErrorNumber.NoError; + + files.Add(new FileSectorInfo + { + Path = path, + Inode = fnodeSector, + AffectedSectors = overlaps + }); + + return ErrorNumber.NoError; + } + + ErrorNumber FindOverlappingExtents(in FNode fnode, uint logicalSize, + IReadOnlyList<(ulong Start, ulong End)> sectorExtents, + out List<(ulong Start, ulong End)> overlaps) + { + overlaps = []; + + if(logicalSize == 0) return ErrorNumber.NoError; + + ulong remainingBytes = logicalSize; + ulong sectorSize = _image.Info.SectorSize; + uint fileSector = 0; + uint totalSectors = (logicalSize + 511) / 512; + + while(fileSector < totalSectors && remainingBytes > 0) + { + ErrorNumber errno = BPlusLookup(fnode.btree, + fnode.btree_data, + fileSector, + out uint diskSector, + out uint runLength); + + if(errno != ErrorNumber.NoError) return errno; + + if(runLength == 0) return ErrorNumber.InvalidArgument; + + ulong bytesInRun = Math.Min(remainingBytes, (ulong)runLength * 512); + ulong byteOffset = (ulong)diskSector * _bytesPerSector; + ulong startSector = _partition.Start + byteOffset / sectorSize; + ulong offsetInSector = byteOffset % sectorSize; + ulong sectorsInExtent = (bytesInRun + offsetInSector + sectorSize - 1) / sectorSize; + + if(sectorsInExtent == 0) sectorsInExtent = 1; + + AddExtentOverlaps(startSector, startSector + sectorsInExtent - 1, sectorExtents, overlaps); + + remainingBytes -= bytesInRun; + fileSector += (uint)((bytesInRun + 511) / 512); + } + + overlaps = NormalizeAnalyzeExtents(overlaps); + + return ErrorNumber.NoError; + } + + static void AddExtentOverlaps(ulong startSector, ulong endSector, + IReadOnlyList<(ulong Start, ulong End)> sectorExtents, + List<(ulong Start, ulong End)> overlaps) + { + foreach((ulong Start, ulong End) requestedExtent in sectorExtents) + { + if(requestedExtent.End < startSector || requestedExtent.Start > endSector) continue; + + overlaps.Add((Math.Max(startSector, requestedExtent.Start), Math.Min(endSector, requestedExtent.End))); + } + } + + static List<(ulong Start, ulong End)> NormalizeAnalyzeExtents(IEnumerable<(ulong Start, ulong End)> extents) + { + var orderedExtents = extents.Where(static extent => extent.End >= extent.Start) + .OrderBy(static extent => extent.Start) + .ThenBy(static extent => extent.End) + .ToList(); + + List<(ulong Start, ulong End)> normalizedExtents = []; + + if(orderedExtents.Count == 0) return normalizedExtents; + + (ulong Start, ulong End) currentExtent = orderedExtents[0]; + + for(var i = 1; i < orderedExtents.Count; i++) + { + if(orderedExtents[i].Start <= currentExtent.End + 1) + { + currentExtent.End = Math.Max(currentExtent.End, orderedExtents[i].End); + + continue; + } + + normalizedExtents.Add(currentExtent); + currentExtent = orderedExtents[i]; + } + + normalizedExtents.Add(currentExtent); + + return normalizedExtents; + } +} \ No newline at end of file