From 1bf9c379c93b724825a820b7abdce5bcea546fe9 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sat, 11 Apr 2026 01:04:01 +0100 Subject: [PATCH] [PFS] Add support for analyze command --- Aaru.Filesystems/PFS/Analyze.cs | 257 ++++++++++++++++++++++++++++++++ 1 file changed, 257 insertions(+) create mode 100644 Aaru.Filesystems/PFS/Analyze.cs diff --git a/Aaru.Filesystems/PFS/Analyze.cs b/Aaru.Filesystems/PFS/Analyze.cs new file mode 100644 index 000000000..b618ac96c --- /dev/null +++ b/Aaru.Filesystems/PFS/Analyze.cs @@ -0,0 +1,257 @@ +// /*************************************************************************** +// Aaru Data Preservation Suite +// ---------------------------------------------------------------------------- +// +// Filename : Analyze.cs +// Author(s) : Natalia Portillo +// +// Component : Professional 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.Interfaces; +using Aaru.CommonTypes.Structs; + +namespace Aaru.Filesystems; + +/// +public sealed partial class PFS +{ +#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) return ErrorNumber.NoError; + + try + { + initProgress?.Invoke(); + + return TraverseDirectoryForAffectedSectors("/", + normalizedExtents, + files, + updateProgress, + pulseProgress); + } + finally + { + endProgress?.Invoke(); + } + } + +#endregion + + ErrorNumber TraverseDirectoryForAffectedSectors(string path, + IReadOnlyList<(ulong Start, ulong End)> sectorExtents, + List files, + UpdateProgressHandler updateProgress, + PulseProgressHandler pulseProgress) + { + pulseProgress?.Invoke(path); + + ErrorNumber errno = OpenDir(path, out IDirNode node); + + if(errno != ErrorNumber.NoError) return errno; + + try + { + List entries = []; + + while(true) + { + errno = ReadDir(node, out string entryName); + + if(errno != ErrorNumber.NoError) return errno; + if(entryName is null) break; + + entries.Add(entryName); + } + + string[] orderedEntries = entries.Where(static entry => !string.IsNullOrWhiteSpace(entry)) + .OrderBy(static entry => entry, StringComparer.OrdinalIgnoreCase) + .ToArray(); + long maximum = orderedEntries.Length > 0 ? orderedEntries.Length : 1; + + for(int i = 0; i < orderedEntries.Length; i++) + { + string entryPath = path == "/" ? "/" + orderedEntries[i] : path + "/" + orderedEntries[i]; + + updateProgress?.Invoke(entryPath, i + 1, maximum); + + errno = GetEntryForPath(entryPath, out DirEntryCacheItem entry); + + if(errno != ErrorNumber.NoError) return errno; + + if(entry.Type == EntryType.Directory || entry.Type == EntryType.HardLinkDir) + { + errno = TraverseDirectoryForAffectedSectors(entryPath, + sectorExtents, + files, + updateProgress, + pulseProgress); + + if(errno != ErrorNumber.NoError) return errno; + + continue; + } + + errno = AddOverlappingFile(entryPath, entry, sectorExtents, files); + + if(errno != ErrorNumber.NoError) return errno; + } + + return ErrorNumber.NoError; + } + finally + { + CloseDir(node); + } + } + + ErrorNumber AddOverlappingFile(string path, DirEntryCacheItem entry, + IReadOnlyList<(ulong Start, ulong End)> sectorExtents, + List files) + { + if(entry.Size == 0) return ErrorNumber.NoError; + + ErrorNumber errno = FindOverlappingExtents(entry, 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 = entry.Anode, + AffectedSectors = overlaps + }); + + return ErrorNumber.NoError; + } + + ErrorNumber FindOverlappingExtents(DirEntryCacheItem entry, + IReadOnlyList<(ulong Start, ulong End)> sectorExtents, + out List<(ulong Start, ulong End)> overlaps) + { + overlaps = []; + + ulong remainingBytes = entry.Size; + + ErrorNumber errno = GetAnode(entry.Anode, out Anode currentAnode); + + if(errno != ErrorNumber.NoError) return errno; + + while(remainingBytes > 0) + { + if(currentAnode.clustersize > 0) + { + ulong extentBytes = Math.Min((ulong)currentAnode.clustersize * _blockSize, remainingBytes); + AddByteRangeOverlaps((ulong)currentAnode.blocknr * _blockSize, extentBytes, sectorExtents, overlaps); + remainingBytes -= extentBytes; + } + + if(remainingBytes == 0 || currentAnode.next == ANODE_EOF) break; + + errno = GetAnode(currentAnode.next, out currentAnode); + + if(errno != ErrorNumber.NoError) return errno; + } + + overlaps = NormalizeAnalyzeExtents(overlaps); + + return ErrorNumber.NoError; + } + + void AddByteRangeOverlaps(ulong absoluteByteOffset, ulong lengthBytes, + IReadOnlyList<(ulong Start, ulong End)> sectorExtents, + List<(ulong Start, ulong End)> overlaps) + { + if(lengthBytes == 0) return; + + ulong sectorSize = _imagePlugin.Info.SectorSize; + ulong startSector = _partition.Start + absoluteByteOffset / sectorSize; + ulong offsetInSector = absoluteByteOffset % sectorSize; + ulong sectorCount = (lengthBytes + offsetInSector + sectorSize - 1) / sectorSize; + + if(sectorCount == 0) sectorCount = 1; + + AddExtentOverlaps(startSector, startSector + sectorCount - 1, sectorExtents, overlaps); + } + + 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) + { + List<(ulong Start, ulong End)> 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(int 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