From abf606a6959318389446705e66573695b7851e32 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sat, 11 Apr 2026 00:23:41 +0100 Subject: [PATCH] [RBF] Add support for analyze command --- Aaru.Filesystems/RBF/Analyze.cs | 243 ++++++++++++++++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 Aaru.Filesystems/RBF/Analyze.cs diff --git a/Aaru.Filesystems/RBF/Analyze.cs b/Aaru.Filesystems/RBF/Analyze.cs new file mode 100644 index 000000000..aba520ef5 --- /dev/null +++ b/Aaru.Filesystems/RBF/Analyze.cs @@ -0,0 +1,243 @@ +// /*************************************************************************** +// Aaru Data Preservation Suite +// ---------------------------------------------------------------------------- +// +// Filename : Analyze.cs +// Author(s) : Natalia Portillo +// +// Component : Random Block File filesystem 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 RBF +{ +#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("/", + _rootDirectoryCache, + normalizedExtents, + files, + updateProgress, + pulseProgress); + } + finally + { + endProgress?.Invoke(); + } + } + +#endregion + + ErrorNumber TraverseDirectoryForAffectedSectors(string path, + Dictionary directoryEntries, + IReadOnlyList<(ulong Start, ulong End)> sectorExtents, + List files, UpdateProgressHandler updateProgress, + PulseProgressHandler pulseProgress) + { + pulseProgress?.Invoke(path); + + KeyValuePair[] orderedEntries = directoryEntries + .Where(static entry => + !string.IsNullOrWhiteSpace(entry.Key) && + entry.Key is not "." and not "..") + .OrderBy(static entry => entry.Key, + StringComparer.OrdinalIgnoreCase) + .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; + CachedDirectoryEntry entry = orderedEntries[i].Value; + + updateProgress?.Invoke(entryPath, i + 1, maximum); + + if(entry.IsDirectory) + { + ErrorNumber errno = ReadDirectoryContents(entry.Fd, + out Dictionary subDirectory); + + if(errno != ErrorNumber.NoError) return errno; + + errno = TraverseDirectoryForAffectedSectors(entryPath, + subDirectory, + sectorExtents, + files, + updateProgress, + pulseProgress); + + if(errno != ErrorNumber.NoError) return errno; + + continue; + } + + ErrorNumber overlapErrno = AddOverlappingFile(entryPath, entry, sectorExtents, files); + + if(overlapErrno != ErrorNumber.NoError) return overlapErrno; + } + + return ErrorNumber.NoError; + } + + ErrorNumber AddOverlappingFile(string path, CachedDirectoryEntry entry, + IReadOnlyList<(ulong Start, ulong End)> sectorExtents, List files) + { + if(entry.FileSize == 0) return ErrorNumber.NoError; + + List<(uint lsn, uint sectors)> segments = ParseSegmentList(entry.Fd.fd_seg); + + ErrorNumber errno = FindOverlappingExtents(segments, + entry.FileSize, + 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.FdLsn, + AffectedSectors = overlaps + }); + + return ErrorNumber.NoError; + } + + ErrorNumber FindOverlappingExtents(IReadOnlyList<(uint lsn, uint sectors)> segments, uint logicalSize, + IReadOnlyList<(ulong Start, ulong End)> sectorExtents, + out List<(ulong Start, ulong End)> overlaps) + { + overlaps = []; + + if(logicalSize == 0 || segments.Count == 0) return ErrorNumber.NoError; + + ulong remainingBytes = logicalSize; + ulong sectorSize = _sectorSize; + ulong sectorsPerLsn = (_lsnSize + _sectorSize - 1) / _sectorSize; + + if(sectorsPerLsn == 0) sectorsPerLsn = 1; + + for(var i = 0; i < segments.Count && remainingBytes > 0; i++) + { + if(segments[i].sectors == 0) continue; + + ulong segmentBytes = (ulong)segments[i].sectors * _lsnSize; + ulong bytesInRun = Math.Min(segmentBytes, remainingBytes); + + if(bytesInRun == 0) continue; + + ulong lsnsUsed = (bytesInRun + _lsnSize - 1) / _lsnSize; + + if(lsnsUsed == 0) + { + remainingBytes -= bytesInRun; + + continue; + } + + ulong startSector = _partitionStart + _idSectorLocation + segments[i].lsn; + ulong endSector = startSector + lsnsUsed - 1 + sectorsPerLsn - 1; + + AddExtentOverlaps(startSector, endSector, sectorExtents, overlaps); + remainingBytes -= bytesInRun; + } + + 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