From 3becc0833cd2e58b3caf4a40f931120f896e1c84 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Wed, 21 Dec 2022 20:03:24 +0000 Subject: [PATCH] Replace ReadDir method with one that uses IDirNode. --- Aaru.Core/Sidecar/Files.cs | 12 +- Aaru.Filesystems/AppleDOS/Dir.cs | 23 +- Aaru.Filesystems/AppleMFS/Dir.cs | 25 +- Aaru.Filesystems/CPM/Dir.cs | 18 +- Aaru.Filesystems/FAT/Dir.cs | 344 +----------------- Aaru.Filesystems/FAT/File.cs | 4 +- Aaru.Filesystems/FATX/Dir.cs | 108 +----- Aaru.Filesystems/FATX/File.cs | 4 +- Aaru.Filesystems/ISO9660/Dir.cs | 125 ++----- Aaru.Filesystems/ISO9660/File.cs | 4 +- Aaru.Filesystems/LisaFS/Dir.cs | 36 +- Aaru.Filesystems/Opera/Dir.cs | 69 +--- Aaru.Filesystems/Opera/File.cs | 4 +- Aaru.Filesystems/UCSDPascal/Dir.cs | 21 +- .../Panels/SubdirectoryViewModel.cs | 7 +- .../Filesystems/ReadOnlyFilesystemTest.cs | 19 +- Aaru.Tests/Issues/FsExtractHashIssueTest.cs | 7 +- Aaru.Tests/Issues/FsExtractIssueTest.cs | 7 +- Aaru/Commands/Filesystem/ExtractFiles.cs | 7 +- Aaru/Commands/Filesystem/Ls.cs | 30 +- 20 files changed, 184 insertions(+), 690 deletions(-) diff --git a/Aaru.Core/Sidecar/Files.cs b/Aaru.Core/Sidecar/Files.cs index 629e713ad..010ead3d7 100644 --- a/Aaru.Core/Sidecar/Files.cs +++ b/Aaru.Core/Sidecar/Files.cs @@ -47,7 +47,7 @@ public sealed partial class Sidecar { var contents = new FilesystemContents(); - ErrorNumber ret = filesystem.ReadDir("/", out List dirents); + ErrorNumber ret = filesystem.OpenDir("/", out IDirNode node); if(ret != ErrorNumber.NoError) return null; @@ -55,7 +55,8 @@ public sealed partial class Sidecar List directories = new(); List files = new(); - foreach(string dirent in dirents) + while(filesystem.ReadDir(node, out string dirent) == ErrorNumber.NoError && + dirent is not null) { ret = filesystem.Stat(dirent, out FileEntryInfo stat); @@ -76,6 +77,8 @@ public sealed partial class Sidecar files.Add(SidecarFile(filesystem, "", dirent, stat)); } + filesystem.CloseDir(node); + if(files.Count > 0) contents.Files = files.OrderBy(f => f.Name).ToList(); @@ -104,7 +107,7 @@ public sealed partial class Sidecar StatusChangeTime = stat.StatusChangeTimeUtc }; - ErrorNumber ret = filesystem.ReadDir(path + "/" + filename, out List dirents); + ErrorNumber ret = filesystem.OpenDir(path + "/" + filename, out IDirNode node); if(ret != ErrorNumber.NoError) return null; @@ -112,7 +115,8 @@ public sealed partial class Sidecar List directories = new(); List files = new(); - foreach(string dirent in dirents) + while(filesystem.ReadDir(node, out string dirent) == ErrorNumber.NoError && + dirent is not null) { ret = filesystem.Stat(path + "/" + filename + "/" + dirent, out FileEntryInfo entryStat); diff --git a/Aaru.Filesystems/AppleDOS/Dir.cs b/Aaru.Filesystems/AppleDOS/Dir.cs index 6934a644b..e7c5a3638 100644 --- a/Aaru.Filesystems/AppleDOS/Dir.cs +++ b/Aaru.Filesystems/AppleDOS/Dir.cs @@ -84,27 +84,23 @@ public sealed partial class AppleDOS } /// - public ErrorNumber ReadDir(string path, out List contents) + public ErrorNumber ReadDir(IDirNode node, out string filename) { - contents = null; + filename = null; if(!_mounted) return ErrorNumber.AccessDenied; - if(!string.IsNullOrEmpty(path) && - string.Compare(path, "/", StringComparison.OrdinalIgnoreCase) != 0) - return ErrorNumber.NotSupported; + if(node is not AppleDosDirNode mynode) + return ErrorNumber.InvalidArgument; - contents = _catalogCache.Keys.ToList(); + if(mynode._position < 0) + return ErrorNumber.InvalidArgument; - if(_debug) - { - contents.Add("$"); - contents.Add("$Boot"); - contents.Add("$Vtoc"); - } + if(mynode._position >= mynode._contents.Length) + return ErrorNumber.NoError; - contents.Sort(); + filename = mynode._contents[mynode._position++]; return ErrorNumber.NoError; } @@ -121,7 +117,6 @@ public sealed partial class AppleDOS return ErrorNumber.NoError; } - ErrorNumber ReadCatalog() { var catalogMs = new MemoryStream(); diff --git a/Aaru.Filesystems/AppleMFS/Dir.cs b/Aaru.Filesystems/AppleMFS/Dir.cs index 7f190af87..4ca0e1d20 100644 --- a/Aaru.Filesystems/AppleMFS/Dir.cs +++ b/Aaru.Filesystems/AppleMFS/Dir.cs @@ -76,30 +76,23 @@ public sealed partial class AppleMFS } /// - public ErrorNumber ReadDir(string path, out List contents) + public ErrorNumber ReadDir(IDirNode node, out string filename) { - contents = null; + filename = null; if(!_mounted) return ErrorNumber.AccessDenied; - if(!string.IsNullOrEmpty(path) && - string.Compare(path, "/", StringComparison.OrdinalIgnoreCase) != 0) - return ErrorNumber.NotSupported; + if(node is not AppleMfsDirNode mynode) + return ErrorNumber.InvalidArgument; - contents = _idToFilename.Select(kvp => kvp.Value).ToList(); + if(mynode._position < 0) + return ErrorNumber.InvalidArgument; - if(_debug) - { - contents.Add("$"); - contents.Add("$Bitmap"); - contents.Add("$MDB"); + if(mynode._position >= mynode._contents.Length) + return ErrorNumber.NoError; - if(_bootBlocks != null) - contents.Add("$Boot"); - } - - contents.Sort(); + filename = mynode._contents[mynode._position++]; return ErrorNumber.NoError; } diff --git a/Aaru.Filesystems/CPM/Dir.cs b/Aaru.Filesystems/CPM/Dir.cs index 6912c65f6..9d3ef7ffc 100644 --- a/Aaru.Filesystems/CPM/Dir.cs +++ b/Aaru.Filesystems/CPM/Dir.cs @@ -31,7 +31,6 @@ // ****************************************************************************/ using System; -using System.Collections.Generic; using System.Text; using Aaru.CommonTypes.Enums; using Aaru.CommonTypes.Interfaces; @@ -64,18 +63,23 @@ public sealed partial class CPM } /// - public ErrorNumber ReadDir(string path, out List contents) + public ErrorNumber ReadDir(IDirNode node, out string filename) { - contents = null; + filename = null; if(!_mounted) return ErrorNumber.AccessDenied; - if(!string.IsNullOrEmpty(path) && - string.Compare(path, "/", StringComparison.OrdinalIgnoreCase) != 0) - return ErrorNumber.NotSupported; + if(node is not CpmDirNode mynode) + return ErrorNumber.InvalidArgument; - contents = new List(_dirList); + if(mynode._position < 0) + return ErrorNumber.InvalidArgument; + + if(mynode._position >= mynode._contents.Length) + return ErrorNumber.NoError; + + filename = mynode._contents[mynode._position++]; return ErrorNumber.NoError; } diff --git a/Aaru.Filesystems/FAT/Dir.cs b/Aaru.Filesystems/FAT/Dir.cs index d2cac7781..205725c27 100644 --- a/Aaru.Filesystems/FAT/Dir.cs +++ b/Aaru.Filesystems/FAT/Dir.cs @@ -413,341 +413,35 @@ public sealed partial class FAT } /// - /// Lists contents from a directory. - /// Directory path. - /// Directory contents. - public ErrorNumber ReadDir(string path, out List contents) + public ErrorNumber ReadDir(IDirNode node, out string filename) { - contents = null; + filename = null; if(!_mounted) return ErrorNumber.AccessDenied; - if(string.IsNullOrWhiteSpace(path) || - path == "/") - { - contents = _rootDirectoryCache.Keys.ToList(); + if(node is not FatDirNode mynode) + return ErrorNumber.InvalidArgument; + if(mynode._position < 0) + return ErrorNumber.InvalidArgument; + + if(mynode._position >= mynode._entries.Length) return ErrorNumber.NoError; - } - string cutPath = path.StartsWith("/", StringComparison.Ordinal) ? path[1..].ToLower(_cultureInfo) - : path.ToLower(_cultureInfo); + CompleteDirectoryEntry entry = mynode._entries[mynode._position]; - if(_directoryCache.TryGetValue(cutPath, out Dictionary currentDirectory)) + filename = _namespace switch { - contents = currentDirectory.Keys.ToList(); - - return ErrorNumber.NoError; - } - - string[] pieces = cutPath.Split(new[] - { - '/' - }, StringSplitOptions.RemoveEmptyEntries); - - KeyValuePair entry = - _rootDirectoryCache.FirstOrDefault(t => t.Key.ToLower(_cultureInfo) == pieces[0]); - - if(string.IsNullOrEmpty(entry.Key)) - return ErrorNumber.NoSuchFile; - - if(!entry.Value.Dirent.attributes.HasFlag(FatAttributes.Subdirectory)) - return ErrorNumber.NotDirectory; - - string currentPath = pieces[0]; - - currentDirectory = _rootDirectoryCache; - - for(int p = 0; p < pieces.Length; p++) - { - entry = currentDirectory.FirstOrDefault(t => t.Key.ToLower(_cultureInfo) == pieces[p]); - - if(string.IsNullOrEmpty(entry.Key)) - return ErrorNumber.NoSuchFile; - - if(!entry.Value.Dirent.attributes.HasFlag(FatAttributes.Subdirectory)) - return ErrorNumber.NotDirectory; - - currentPath = p == 0 ? pieces[0] : $"{currentPath}/{pieces[p]}"; - uint currentCluster = entry.Value.Dirent.start_cluster; - - if(_fat32) - currentCluster += (uint)(entry.Value.Dirent.ea_handle << 16); - - if(_directoryCache.TryGetValue(currentPath, out currentDirectory)) - continue; - - // Reserved unallocated directory, seen in Atari ST - if(currentCluster == 0) - { - _directoryCache[currentPath] = new Dictionary(); - contents = new List(); - - return ErrorNumber.NoError; - } - - uint[] clusters = GetClusters(currentCluster); - - if(clusters is null) - return ErrorNumber.InvalidArgument; - - byte[] directoryBuffer = new byte[_bytesPerCluster * clusters.Length]; - - for(int i = 0; i < clusters.Length; i++) - { - ErrorNumber errno = _image.ReadSectors(_firstClusterSector + (clusters[i] * _sectorsPerCluster), - _sectorsPerCluster, out byte[] buffer); - - if(errno != ErrorNumber.NoError) - return errno; - - Array.Copy(buffer, 0, directoryBuffer, i * _bytesPerCluster, _bytesPerCluster); - } - - currentDirectory = new Dictionary(); - byte[] lastLfnName = null; - byte lastLfnChecksum = 0; - - for(int pos = 0; pos < directoryBuffer.Length; pos += Marshal.SizeOf()) - { - DirectoryEntry dirent = - Marshal.ByteArrayToStructureLittleEndian(directoryBuffer, pos, - Marshal.SizeOf()); - - if(dirent.filename[0] == DIRENT_FINISHED) - break; - - if(dirent.attributes.HasFlag(FatAttributes.LFN)) - { - if(_namespace != Namespace.Lfn && - _namespace != Namespace.Ecs) - continue; - - LfnEntry lfnEntry = - Marshal.ByteArrayToStructureLittleEndian(directoryBuffer, pos, - Marshal.SizeOf()); - - int lfnSequence = lfnEntry.sequence & LFN_MASK; - - if((lfnEntry.sequence & LFN_ERASED) > 0) - continue; - - if((lfnEntry.sequence & LFN_LAST) > 0) - { - lastLfnName = new byte[lfnSequence * 26]; - lastLfnChecksum = lfnEntry.checksum; - } - - if(lastLfnName is null) - continue; - - if(lfnEntry.checksum != lastLfnChecksum) - continue; - - lfnSequence--; - - Array.Copy(lfnEntry.name1, 0, lastLfnName, lfnSequence * 26, 10); - Array.Copy(lfnEntry.name2, 0, lastLfnName, (lfnSequence * 26) + 10, 12); - Array.Copy(lfnEntry.name3, 0, lastLfnName, (lfnSequence * 26) + 22, 4); - - continue; - } - - // Not a correct entry - if(dirent.filename[0] < DIRENT_MIN && - dirent.filename[0] != DIRENT_E5) - continue; - - // Self - if(_encoding.GetString(dirent.filename).TrimEnd() == ".") - continue; - - // Parent - if(_encoding.GetString(dirent.filename).TrimEnd() == "..") - continue; - - // Deleted - if(dirent.filename[0] == DIRENT_DELETED) - continue; - - string filename; - - if(dirent.attributes.HasFlag(FatAttributes.VolumeLabel)) - continue; - - var completeEntry = new CompleteDirectoryEntry - { - Dirent = dirent - }; - - if(_namespace is Namespace.Lfn or Namespace.Ecs && - lastLfnName != null) - { - byte calculatedLfnChecksum = LfnChecksum(dirent.filename, dirent.extension); - - if(calculatedLfnChecksum == lastLfnChecksum) - { - filename = StringHandlers.CToString(lastLfnName, Encoding.Unicode, true); - - completeEntry.Lfn = filename; - lastLfnName = null; - lastLfnChecksum = 0; - } - } - - if(dirent.filename[0] == DIRENT_E5) - dirent.filename[0] = DIRENT_DELETED; - - string name = _encoding.GetString(dirent.filename).TrimEnd(); - string extension = _encoding.GetString(dirent.extension).TrimEnd(); - - if(name == "" && - extension == "") - { - AaruConsole.DebugWriteLine("FAT filesystem", Localization.Found_empty_filename_in_0, path); - - if(!_debug || - dirent is { size: > 0, start_cluster: 0 }) - continue; // Skip invalid name - - // If debug, add it - name = ":{EMPTYNAME}:"; - - // Try to create a unique filename with an extension from 000 to 999 - for(int uniq = 0; uniq < 1000; uniq++) - { - extension = $"{uniq:D03}"; - - if(!currentDirectory.ContainsKey($"{name}.{extension}")) - break; - } - - // If we couldn't find it, just skip over - if(currentDirectory.ContainsKey($"{name}.{extension}")) - continue; - } - - if(_namespace == Namespace.Nt) - { - if(dirent.caseinfo.HasFlag(CaseInfo.LowerCaseExtension)) - extension = extension.ToLower(CultureInfo.CurrentCulture); - - if(dirent.caseinfo.HasFlag(CaseInfo.LowerCaseBasename)) - name = name.ToLower(CultureInfo.CurrentCulture); - } - - if(extension != "") - filename = name + "." + extension; - else - filename = name; - - if(_namespace == Namespace.Human) - { - HumanDirectoryEntry humanEntry = - Marshal.ByteArrayToStructureLittleEndian(directoryBuffer, pos, - Marshal.SizeOf()); - - completeEntry.HumanDirent = humanEntry; - - name = StringHandlers.CToString(humanEntry.name1, _encoding).TrimEnd(); - extension = StringHandlers.CToString(humanEntry.extension, _encoding).TrimEnd(); - string name2 = StringHandlers.CToString(humanEntry.name2, _encoding).TrimEnd(); - - if(extension != "") - filename = name + name2 + "." + extension; - else - filename = name + name2; - - completeEntry.HumanName = filename; - } - - // Atari ST allows slash AND colon so cannot simply substitute one for the other like in Mac filesystems - filename = filename.Replace('/', '\u2215'); - - // Using array accessor ensures that repeated entries just get substituted. - // Repeated entries are not allowed but some bad implementations (e.g. FAT32.IFS)allow to create them - // when using spaces - completeEntry.Shortname = filename; - currentDirectory[completeEntry.ToString()] = completeEntry; - } - - // Check OS/2 .LONGNAME - if(_eaCache != null && - _namespace is Namespace.Os2 or Namespace.Ecs && - !_fat32) - { - List> filesWithEas = - currentDirectory.Where(t => t.Value.Dirent.ea_handle != 0).ToList(); - - foreach(KeyValuePair fileWithEa in filesWithEas) - { - Dictionary eas = GetEas(fileWithEa.Value.Dirent.ea_handle); - - if(eas is null) - continue; - - if(!eas.TryGetValue("com.microsoft.os2.longname", out byte[] longnameEa)) - continue; - - if(BitConverter.ToUInt16(longnameEa, 0) != EAT_ASCII) - continue; - - ushort longnameSize = BitConverter.ToUInt16(longnameEa, 2); - - if(longnameSize + 4 > longnameEa.Length) - continue; - - byte[] longnameBytes = new byte[longnameSize]; - - Array.Copy(longnameEa, 4, longnameBytes, 0, longnameSize); - - string longname = StringHandlers.CToString(longnameBytes, _encoding); - - if(string.IsNullOrWhiteSpace(longname)) - continue; - - // Forward slash is allowed in .LONGNAME, so change it to visually similar division slash - longname = longname.Replace('/', '\u2215'); - - fileWithEa.Value.Longname = longname; - currentDirectory.Remove(fileWithEa.Key); - currentDirectory[fileWithEa.Value.ToString()] = fileWithEa.Value; - } - } - - // Check FAT32.IFS EAs - if(_fat32 || _debug) - { - List> fat32EaSidecars = - currentDirectory.Where(t => t.Key.EndsWith(FAT32_EA_TAIL, true, _cultureInfo)).ToList(); - - foreach(KeyValuePair sidecar in fat32EaSidecars) - { - // No real file this sidecar accompanies - if(!currentDirectory.TryGetValue(sidecar.Key[..^FAT32_EA_TAIL.Length], - out CompleteDirectoryEntry fileWithEa)) - continue; - - // If not in debug mode we will consider the lack of EA bitflags to mean the EAs are corrupted or not real - if(!_debug) - if(!fileWithEa.Dirent.caseinfo.HasFlag(CaseInfo.NormalEaOld) && - !fileWithEa.Dirent.caseinfo.HasFlag(CaseInfo.CriticalEa) && - !fileWithEa.Dirent.caseinfo.HasFlag(CaseInfo.NormalEa) && - !fileWithEa.Dirent.caseinfo.HasFlag(CaseInfo.CriticalEa)) - continue; - - fileWithEa.Fat32Ea = sidecar.Value.Dirent; - - if(!_debug) - currentDirectory.Remove(sidecar.Key); - } - } - - _directoryCache.Add(currentPath, currentDirectory); - } - - contents = currentDirectory?.Keys.ToList(); + Namespace.Ecs when entry.Longname is not null => entry.Longname, + Namespace.Ecs when entry.Longname is null && entry.Lfn is not null => entry.Lfn, + Namespace.Lfn when entry.Lfn is not null => entry.Lfn, + Namespace.Human when entry.HumanName is not null => entry.HumanName, + Namespace.Os2 when entry.Longname is not null => entry.Longname, + _ => entry.Shortname + }; + + mynode._position++; return ErrorNumber.NoError; } diff --git a/Aaru.Filesystems/FAT/File.cs b/Aaru.Filesystems/FAT/File.cs index bfba0fcf4..7c3e9c79f 100644 --- a/Aaru.Filesystems/FAT/File.cs +++ b/Aaru.Filesystems/FAT/File.cs @@ -313,10 +313,12 @@ public sealed partial class FAT if(!_directoryCache.TryGetValue(parentPath, out _)) { - ErrorNumber err = ReadDir(parentPath, out _); + ErrorNumber err = OpenDir(parentPath, out IDirNode node); if(err != ErrorNumber.NoError) return err; + + CloseDir(node); } Dictionary parent; diff --git a/Aaru.Filesystems/FATX/Dir.cs b/Aaru.Filesystems/FATX/Dir.cs index 7b87e2fc8..2d3e863fc 100644 --- a/Aaru.Filesystems/FATX/Dir.cs +++ b/Aaru.Filesystems/FATX/Dir.cs @@ -167,112 +167,26 @@ public sealed partial class XboxFatPlugin } /// - public ErrorNumber ReadDir(string path, out List contents) + public ErrorNumber ReadDir(IDirNode node, out string filename) { - contents = null; + filename = null; if(!_mounted) return ErrorNumber.AccessDenied; - if(string.IsNullOrWhiteSpace(path) || - path == "/") - { - contents = _rootDirectory.Keys.ToList(); + if(node is not FatxDirNode mynode) + return ErrorNumber.InvalidArgument; + if(mynode._position < 0) + return ErrorNumber.InvalidArgument; + + if(mynode._position >= mynode._entries.Length) return ErrorNumber.NoError; - } - string cutPath = path.StartsWith('/') ? path[1..].ToLower(_cultureInfo) : path.ToLower(_cultureInfo); + filename = _encoding.GetString(mynode._entries[mynode._position].filename, 0, + mynode._entries[mynode._position].filenameSize); - if(_directoryCache.TryGetValue(cutPath, out Dictionary currentDirectory)) - { - contents = currentDirectory.Keys.ToList(); - - return ErrorNumber.NoError; - } - - string[] pieces = cutPath.Split(new[] - { - '/' - }, StringSplitOptions.RemoveEmptyEntries); - - KeyValuePair entry = - _rootDirectory.FirstOrDefault(t => t.Key.ToLower(_cultureInfo) == pieces[0]); - - if(string.IsNullOrEmpty(entry.Key)) - return ErrorNumber.NoSuchFile; - - if(!entry.Value.attributes.HasFlag(Attributes.Directory)) - return ErrorNumber.NotDirectory; - - string currentPath = pieces[0]; - - currentDirectory = _rootDirectory; - - for(int p = 0; p < pieces.Length; p++) - { - entry = currentDirectory.FirstOrDefault(t => t.Key.ToLower(_cultureInfo) == pieces[p]); - - if(string.IsNullOrEmpty(entry.Key)) - return ErrorNumber.NoSuchFile; - - if(!entry.Value.attributes.HasFlag(Attributes.Directory)) - return ErrorNumber.NotDirectory; - - currentPath = p == 0 ? pieces[0] : $"{currentPath}/{pieces[p]}"; - uint currentCluster = entry.Value.firstCluster; - - if(_directoryCache.TryGetValue(currentPath, out currentDirectory)) - continue; - - uint[] clusters = GetClusters(currentCluster); - - if(clusters is null) - return ErrorNumber.InvalidArgument; - - byte[] directoryBuffer = new byte[_bytesPerCluster * clusters.Length]; - - for(int i = 0; i < clusters.Length; i++) - { - ErrorNumber errno = - _imagePlugin.ReadSectors(_firstClusterSector + ((clusters[i] - 1) * _sectorsPerCluster), - _sectorsPerCluster, out byte[] buffer); - - if(errno != ErrorNumber.NoError) - return errno; - - Array.Copy(buffer, 0, directoryBuffer, i * _bytesPerCluster, _bytesPerCluster); - } - - currentDirectory = new Dictionary(); - - int pos = 0; - - while(pos < directoryBuffer.Length) - { - DirectoryEntry dirent = _littleEndian - ? Marshal.ByteArrayToStructureLittleEndian(directoryBuffer, - pos, Marshal.SizeOf()) - : Marshal.ByteArrayToStructureBigEndian(directoryBuffer, - pos, Marshal.SizeOf()); - - pos += Marshal.SizeOf(); - - if(dirent.filenameSize is UNUSED_DIRENTRY or FINISHED_DIRENTRY) - break; - - if(dirent.filenameSize is DELETED_DIRENTRY or > MAX_FILENAME) - continue; - - string filename = _encoding.GetString(dirent.filename, 0, dirent.filenameSize); - - currentDirectory.Add(filename, dirent); - } - - _directoryCache.Add(currentPath, currentDirectory); - } - - contents = currentDirectory?.Keys.ToList(); + mynode._position++; return ErrorNumber.NoError; } diff --git a/Aaru.Filesystems/FATX/File.cs b/Aaru.Filesystems/FATX/File.cs index d443bbd92..a52450edf 100644 --- a/Aaru.Filesystems/FATX/File.cs +++ b/Aaru.Filesystems/FATX/File.cs @@ -276,11 +276,13 @@ public sealed partial class XboxFatPlugin string parentPath = string.Join("/", pieces, 0, pieces.Length - 1); - ErrorNumber err = ReadDir(parentPath, out _); + ErrorNumber err = OpenDir(parentPath, out IDirNode node); if(err != ErrorNumber.NoError) return err; + CloseDir(node); + Dictionary parent; if(pieces.Length == 1) diff --git a/Aaru.Filesystems/ISO9660/Dir.cs b/Aaru.Filesystems/ISO9660/Dir.cs index f11637e49..6f9027cb8 100644 --- a/Aaru.Filesystems/ISO9660/Dir.cs +++ b/Aaru.Filesystems/ISO9660/Dir.cs @@ -150,89 +150,41 @@ public sealed partial class ISO9660 } /// - public ErrorNumber ReadDir(string path, out List contents) + public ErrorNumber ReadDir(IDirNode node, out string filename) { - contents = null; + filename = null; if(!_mounted) return ErrorNumber.AccessDenied; - if(string.IsNullOrWhiteSpace(path) || - path == "/") - { - contents = GetFilenames(_rootDirectoryCache); + if(node is not Iso9660DirNode mynode) + return ErrorNumber.InvalidArgument; + if(mynode._position < 0) + return ErrorNumber.InvalidArgument; + + if(mynode._position >= mynode._entries.Length) return ErrorNumber.NoError; + + switch(_namespace) + { + case Namespace.Normal: + filename = mynode._entries[mynode._position].Filename.EndsWith(";1", StringComparison.Ordinal) + ? mynode._entries[mynode._position].Filename[..^2] + : mynode._entries[mynode._position].Filename; + + break; + case Namespace.Vms: + case Namespace.Joliet: + case Namespace.Rrip: + case Namespace.Romeo: + filename = mynode._entries[mynode._position].Filename; + + break; + default: return ErrorNumber.InvalidArgument; } - string cutPath = path.StartsWith("/", StringComparison.Ordinal) - ? path[1..].ToLower(CultureInfo.CurrentUICulture) - : path.ToLower(CultureInfo.CurrentUICulture); - - if(_directoryCache.TryGetValue(cutPath, out Dictionary currentDirectory)) - { - contents = currentDirectory.Keys.ToList(); - - return ErrorNumber.NoError; - } - - string[] pieces = cutPath.Split(new[] - { - '/' - }, StringSplitOptions.RemoveEmptyEntries); - - KeyValuePair entry = - _rootDirectoryCache.FirstOrDefault(t => t.Key.ToLower(CultureInfo.CurrentUICulture) == pieces[0]); - - if(string.IsNullOrEmpty(entry.Key)) - return ErrorNumber.NoSuchFile; - - if(!entry.Value.Flags.HasFlag(FileFlags.Directory)) - return ErrorNumber.NotDirectory; - - string currentPath = pieces[0]; - - currentDirectory = _rootDirectoryCache; - - for(int p = 0; p < pieces.Length; p++) - { - entry = currentDirectory.FirstOrDefault(t => t.Key.ToLower(CultureInfo.CurrentUICulture) == pieces[p]); - - if(string.IsNullOrEmpty(entry.Key)) - return ErrorNumber.NoSuchFile; - - if(!entry.Value.Flags.HasFlag(FileFlags.Directory)) - return ErrorNumber.NotDirectory; - - currentPath = p == 0 ? pieces[0] : $"{currentPath}/{pieces[p]}"; - - if(_directoryCache.TryGetValue(currentPath, out currentDirectory)) - continue; - - if(entry.Value.Extents.Count == 0) - return ErrorNumber.InvalidArgument; - - currentDirectory = _cdi - ? DecodeCdiDirectory(entry.Value.Extents[0].extent + entry.Value.XattrLength, - entry.Value.Extents[0].size) - : _highSierra - ? DecodeHighSierraDirectory(entry.Value.Extents[0].extent + entry.Value.XattrLength, - entry.Value.Extents[0].size) - : DecodeIsoDirectory(entry.Value.Extents[0].extent + entry.Value.XattrLength, - entry.Value.Extents[0].size); - - if(_usePathTable) - foreach(DecodedDirectoryEntry subDirectory in _cdi - ? GetSubdirsFromCdiPathTable(currentPath) - : _highSierra - ? GetSubdirsFromHighSierraPathTable(currentPath) - : GetSubdirsFromIsoPathTable(currentPath)) - currentDirectory[subDirectory.Filename] = subDirectory; - - _directoryCache.Add(currentPath, currentDirectory); - } - - contents = GetFilenames(currentDirectory); + mynode._position++; return ErrorNumber.NoError; } @@ -249,31 +201,6 @@ public sealed partial class ISO9660 return ErrorNumber.NoError; } - List GetFilenames(Dictionary dirents) - { - List contents = new(); - - foreach(DecodedDirectoryEntry entry in dirents.Values) - switch(_namespace) - { - case Namespace.Normal: - contents.Add(entry.Filename.EndsWith(";1", StringComparison.Ordinal) ? entry.Filename[..^2] - : entry.Filename); - - break; - case Namespace.Vms: - case Namespace.Joliet: - case Namespace.Rrip: - case Namespace.Romeo: - contents.Add(entry.Filename); - - break; - default: throw new ArgumentOutOfRangeException(); - } - - return contents; - } - Dictionary DecodeCdiDirectory(ulong start, uint size) { Dictionary entries = new(); diff --git a/Aaru.Filesystems/ISO9660/File.cs b/Aaru.Filesystems/ISO9660/File.cs index 077687d64..59265fed2 100644 --- a/Aaru.Filesystems/ISO9660/File.cs +++ b/Aaru.Filesystems/ISO9660/File.cs @@ -481,10 +481,12 @@ public sealed partial class ISO9660 if(!_directoryCache.TryGetValue(parentPath, out _)) { - ErrorNumber err = ReadDir(parentPath, out _); + ErrorNumber err = OpenDir(parentPath, out IDirNode node); if(err != ErrorNumber.NoError) return err; + + CloseDir(node); } Dictionary parent; diff --git a/Aaru.Filesystems/LisaFS/Dir.cs b/Aaru.Filesystems/LisaFS/Dir.cs index 450964f7b..8bd1f77cc 100644 --- a/Aaru.Filesystems/LisaFS/Dir.cs +++ b/Aaru.Filesystems/LisaFS/Dir.cs @@ -96,37 +96,23 @@ public sealed partial class LisaFS } /// - public ErrorNumber ReadDir(string path, out List contents) + public ErrorNumber ReadDir(IDirNode node, out string filename) { - contents = null; - ErrorNumber error = LookupFileId(path, out short fileId, out bool isDir); + filename = null; - if(error != ErrorNumber.NoError) - return error; + if(!_mounted) + return ErrorNumber.AccessDenied; - if(!isDir) - return ErrorNumber.NotDirectory; + if(node is not LisaDirNode mynode) + return ErrorNumber.InvalidArgument; - /*List catalog; - error = ReadCatalog(fileId, out catalog); - if(error != ErrorNumber.NoError) - return error;*/ + if(mynode._position < 0) + return ErrorNumber.InvalidArgument; - ReadDir(fileId, out contents); + if(mynode._position >= mynode._contents.Length) + return ErrorNumber.NoError; - // On debug add system files as readable files - // Syntax similar to NTFS - if(_debug && fileId == DIRID_ROOT) - { - contents.Add("$MDDF"); - contents.Add("$Boot"); - contents.Add("$Loader"); - contents.Add("$Bitmap"); - contents.Add("$S-Record"); - contents.Add("$"); - } - - contents.Sort(); + filename = mynode._contents[mynode._position++]; return ErrorNumber.NoError; } diff --git a/Aaru.Filesystems/Opera/Dir.cs b/Aaru.Filesystems/Opera/Dir.cs index 106da67cc..6f450e19d 100644 --- a/Aaru.Filesystems/Opera/Dir.cs +++ b/Aaru.Filesystems/Opera/Dir.cs @@ -130,74 +130,23 @@ public sealed partial class OperaFS } /// - public ErrorNumber ReadDir(string path, out List contents) + public ErrorNumber ReadDir(IDirNode node, out string filename) { - contents = null; + filename = null; if(!_mounted) return ErrorNumber.AccessDenied; - if(string.IsNullOrWhiteSpace(path) || - path == "/") - { - contents = _rootDirectoryCache.Keys.ToList(); + if(node is not OperaDirNode mynode) + return ErrorNumber.InvalidArgument; + if(mynode._position < 0) + return ErrorNumber.InvalidArgument; + + if(mynode._position >= mynode._contents.Length) return ErrorNumber.NoError; - } - string cutPath = path.StartsWith("/", StringComparison.Ordinal) - ? path[1..].ToLower(CultureInfo.CurrentUICulture) - : path.ToLower(CultureInfo.CurrentUICulture); - - if(_directoryCache.TryGetValue(cutPath, out Dictionary currentDirectory)) - { - contents = currentDirectory.Keys.ToList(); - - return ErrorNumber.NoError; - } - - string[] pieces = cutPath.Split(new[] - { - '/' - }, StringSplitOptions.RemoveEmptyEntries); - - KeyValuePair entry = - _rootDirectoryCache.FirstOrDefault(t => t.Key.ToLower(CultureInfo.CurrentUICulture) == pieces[0]); - - if(string.IsNullOrEmpty(entry.Key)) - return ErrorNumber.NoSuchFile; - - if((entry.Value.Entry.flags & FLAGS_MASK) != (int)FileFlags.Directory) - return ErrorNumber.NotDirectory; - - string currentPath = pieces[0]; - - currentDirectory = _rootDirectoryCache; - - for(int p = 0; p < pieces.Length; p++) - { - entry = currentDirectory.FirstOrDefault(t => t.Key.ToLower(CultureInfo.CurrentUICulture) == pieces[p]); - - if(string.IsNullOrEmpty(entry.Key)) - return ErrorNumber.NoSuchFile; - - if((entry.Value.Entry.flags & FLAGS_MASK) != (int)FileFlags.Directory) - return ErrorNumber.NotDirectory; - - currentPath = p == 0 ? pieces[0] : $"{currentPath}/{pieces[p]}"; - - if(_directoryCache.TryGetValue(currentPath, out currentDirectory)) - continue; - - if(entry.Value.Pointers.Length < 1) - return ErrorNumber.InvalidArgument; - - currentDirectory = DecodeDirectory((int)entry.Value.Pointers[0]); - - _directoryCache.Add(currentPath, currentDirectory); - } - - contents = currentDirectory?.Keys.ToList(); + filename = mynode._contents[mynode._position++]; return ErrorNumber.NoError; } diff --git a/Aaru.Filesystems/Opera/File.cs b/Aaru.Filesystems/Opera/File.cs index 73f0ea51c..f86f930d5 100644 --- a/Aaru.Filesystems/Opera/File.cs +++ b/Aaru.Filesystems/Opera/File.cs @@ -212,10 +212,12 @@ public sealed partial class OperaFS if(!_directoryCache.TryGetValue(parentPath, out _)) { - ErrorNumber err = ReadDir(parentPath, out _); + ErrorNumber err = OpenDir(parentPath, out IDirNode node); if(err != ErrorNumber.NoError) return err; + + CloseDir(node); } Dictionary parent; diff --git a/Aaru.Filesystems/UCSDPascal/Dir.cs b/Aaru.Filesystems/UCSDPascal/Dir.cs index 82c58aae0..f75a7e246 100644 --- a/Aaru.Filesystems/UCSDPascal/Dir.cs +++ b/Aaru.Filesystems/UCSDPascal/Dir.cs @@ -76,26 +76,23 @@ public sealed partial class PascalPlugin } /// - public ErrorNumber ReadDir(string path, out List contents) + public ErrorNumber ReadDir(IDirNode node, out string filename) { - contents = null; + filename = null; if(!_mounted) return ErrorNumber.AccessDenied; - if(!string.IsNullOrEmpty(path) && - string.Compare(path, "/", StringComparison.OrdinalIgnoreCase) != 0) - return ErrorNumber.NotSupported; + if(node is not PascalDirNode mynode) + return ErrorNumber.InvalidArgument; - contents = _fileEntries.Select(ent => StringHandlers.PascalToString(ent.Filename, _encoding)).ToList(); + if(mynode._position < 0) + return ErrorNumber.InvalidArgument; - if(_debug) - { - contents.Add("$"); - contents.Add("$Boot"); - } + if(mynode._position >= mynode._contents.Length) + return ErrorNumber.NoError; - contents.Sort(); + filename = mynode._contents[mynode._position++]; return ErrorNumber.NoError; } diff --git a/Aaru.Gui/ViewModels/Panels/SubdirectoryViewModel.cs b/Aaru.Gui/ViewModels/Panels/SubdirectoryViewModel.cs index 8de390560..b557494c0 100644 --- a/Aaru.Gui/ViewModels/Panels/SubdirectoryViewModel.cs +++ b/Aaru.Gui/ViewModels/Panels/SubdirectoryViewModel.cs @@ -67,7 +67,7 @@ public sealed class SubdirectoryViewModel _model = model; _view = view; - ErrorNumber errno = model.Plugin.ReadDir(model.Path, out List dirents); + ErrorNumber errno = model.Plugin.OpenDir(model.Path, out IDirNode node); if(errno != ErrorNumber.NoError) { @@ -80,7 +80,8 @@ public sealed class SubdirectoryViewModel return; } - foreach(string dirent in dirents) + while(model.Plugin.ReadDir(node, out string dirent) == ErrorNumber.NoError && + dirent is not null) { errno = model.Plugin.Stat(model.Path + "/" + dirent, out FileEntryInfo stat); @@ -112,6 +113,8 @@ public sealed class SubdirectoryViewModel Stat = stat }); } + + model.Plugin.CloseDir(node); } public ObservableCollection Entries { get; } diff --git a/Aaru.Tests/Filesystems/ReadOnlyFilesystemTest.cs b/Aaru.Tests/Filesystems/ReadOnlyFilesystemTest.cs index 298d612aa..e5010e382 100644 --- a/Aaru.Tests/Filesystems/ReadOnlyFilesystemTest.cs +++ b/Aaru.Tests/Filesystems/ReadOnlyFilesystemTest.cs @@ -267,12 +267,13 @@ public abstract class ReadOnlyFilesystemTest : FilesystemTest path = ""; Dictionary children = new(); - fs.ReadDir(path, out List contents); + ErrorNumber ret = fs.OpenDir(path, out IDirNode node); - if(contents is null) + if(ret != ErrorNumber.NoError) return children; - foreach(string child in contents) + while(fs.ReadDir(node, out string child) == ErrorNumber.NoError && + child is not null) { string childPath = $"{path}/{child}"; fs.Stat(childPath, out FileEntryInfo stat); @@ -303,6 +304,8 @@ public abstract class ReadOnlyFilesystemTest : FilesystemTest children[child] = data; } + fs.CloseDir(node); + return children; } @@ -351,7 +354,7 @@ public abstract class ReadOnlyFilesystemTest : FilesystemTest { currentDepth++; nextLevels = new List(); - ErrorNumber ret = fs.ReadDir(path, out List contents); + ErrorNumber ret = fs.OpenDir(path, out IDirNode node); // Directory is not readable, probably filled the volume, just ignore it if(ret == ErrorNumber.InvalidArgument) @@ -364,6 +367,14 @@ public abstract class ReadOnlyFilesystemTest : FilesystemTest if(ret != ErrorNumber.NoError) return; + List contents = new(); + + while(fs.ReadDir(node, out string filename) == ErrorNumber.NoError && + filename is not null) + contents.Add(filename); + + fs.CloseDir(node); + if(children.Count == 0 && contents.Count == 0) return; diff --git a/Aaru.Tests/Issues/FsExtractHashIssueTest.cs b/Aaru.Tests/Issues/FsExtractHashIssueTest.cs index f13f67728..dfb09aab2 100644 --- a/Aaru.Tests/Issues/FsExtractHashIssueTest.cs +++ b/Aaru.Tests/Issues/FsExtractHashIssueTest.cs @@ -162,12 +162,13 @@ public abstract class FsExtractHashIssueTest if(path.StartsWith('/')) path = path[1..]; - ErrorNumber error = fs.ReadDir(path, out List directory); + ErrorNumber error = fs.OpenDir(path, out IDirNode node); Assert.AreEqual(ErrorNumber.NoError, error, string.Format(Localization.Error_0_reading_root_directory_0, error.ToString())); - foreach(string entry in directory) + while(fs.ReadDir(node, out string entry) == ErrorNumber.NoError && + entry is not null) { error = fs.Stat(path + "/" + entry, out FileEntryInfo stat); @@ -273,5 +274,7 @@ public abstract class FsExtractHashIssueTest Assert.AreEqual(fileData.Md5, calculatedMd5, string.Format(Localization.Invalid_checksum_for_file_0, path + "/" + entry)); } + + fs.CloseDir(node); } } \ No newline at end of file diff --git a/Aaru.Tests/Issues/FsExtractIssueTest.cs b/Aaru.Tests/Issues/FsExtractIssueTest.cs index 717c653af..98eaf15cd 100644 --- a/Aaru.Tests/Issues/FsExtractIssueTest.cs +++ b/Aaru.Tests/Issues/FsExtractIssueTest.cs @@ -129,12 +129,13 @@ public abstract class FsExtractIssueTest if(path.StartsWith('/')) path = path[1..]; - ErrorNumber error = fs.ReadDir(path, out List directory); + ErrorNumber error = fs.OpenDir(path, out IDirNode node); Assert.AreEqual(ErrorNumber.NoError, error, string.Format(Localization.Error_0_reading_root_directory_0, error.ToString())); - foreach(string entry in directory) + while(fs.ReadDir(node, out string entry) == ErrorNumber.NoError && + entry is not null) { error = fs.Stat(path + "/" + entry, out FileEntryInfo stat); @@ -183,5 +184,7 @@ public abstract class FsExtractIssueTest string.Format(Localization.Error_0_reading_file_1, readBytes, stat.Length, path + "/" + entry)); } + + fs.CloseDir(node); } } \ No newline at end of file diff --git a/Aaru/Commands/Filesystem/ExtractFiles.cs b/Aaru/Commands/Filesystem/ExtractFiles.cs index d48d9b15b..86864ea32 100644 --- a/Aaru/Commands/Filesystem/ExtractFiles.cs +++ b/Aaru/Commands/Filesystem/ExtractFiles.cs @@ -394,7 +394,7 @@ sealed class ExtractFilesCommand : Command if(path.StartsWith('/')) path = path[1..]; - ErrorNumber error = fs.ReadDir(path, out List directory); + ErrorNumber error = fs.OpenDir(path, out IDirNode node); if(error != ErrorNumber.NoError) { @@ -403,7 +403,8 @@ sealed class ExtractFilesCommand : Command return; } - foreach(string entry in directory) + while(fs.ReadDir(node, out string entry) == ErrorNumber.NoError && + entry is not null) { FileEntryInfo stat = new(); @@ -679,5 +680,7 @@ sealed class ExtractFilesCommand : Command else AaruConsole.ErrorWriteLine(UI.Error_reading_file_0, Markup.Escape(entry)); } + + fs.CloseDir(node); } } \ No newline at end of file diff --git a/Aaru/Commands/Filesystem/Ls.cs b/Aaru/Commands/Filesystem/Ls.cs index 006523960..7552f94ea 100644 --- a/Aaru/Commands/Filesystem/Ls.cs +++ b/Aaru/Commands/Filesystem/Ls.cs @@ -353,8 +353,8 @@ sealed class LsCommand : Command static void ListFilesInDir(string path, [NotNull] IReadOnlyFilesystem fs, bool longFormat) { - ErrorNumber error = ErrorNumber.InvalidArgument; - List directory = new(); + ErrorNumber error = ErrorNumber.InvalidArgument; + IDirNode node = null; if(path.StartsWith('/')) path = path[1..]; @@ -365,7 +365,7 @@ sealed class LsCommand : Command Core.Spectre.ProgressSingleSpinner(ctx => { ctx.AddTask(UI.Reading_directory).IsIndeterminate(); - error = fs.ReadDir(path, out directory); + error = fs.OpenDir(path, out node); }); if(error != ErrorNumber.NoError) @@ -377,20 +377,20 @@ sealed class LsCommand : Command Dictionary stats = new(); - AnsiConsole.Progress().AutoClear(true).HideCompleted(true). - Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn()).Start(ctx => - { - ProgressTask task = ctx.AddTask(UI.Retrieving_file_information); - task.MaxValue = directory.Count; + Core.Spectre.ProgressSingleSpinner(ctx => + { + ctx.AddTask(UI.Retrieving_file_information).IsIndeterminate(); - foreach(string entry in directory) - { - task.Increment(1); - fs.Stat(path + "/" + entry, out FileEntryInfo stat); + while(fs.ReadDir(node, out string entry) == ErrorNumber.NoError && + entry is not null) + { + fs.Stat(path + "/" + entry, out FileEntryInfo stat); - stats.Add(entry, stat); - } - }); + stats.Add(entry, stat); + } + + fs.CloseDir(node); + }); foreach(KeyValuePair entry in stats.OrderBy(e => e.Value?.Attributes.HasFlag(FileAttributes.Directory) == false))