[ext] Implement directory operations.

This commit is contained in:
2026-02-03 21:32:02 +00:00
parent ee42c155fa
commit f30a9d883c
3 changed files with 183 additions and 9 deletions

View File

@@ -28,7 +28,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.Helpers;
using Aaru.Logging;
@@ -38,6 +40,138 @@ namespace Aaru.Filesystems;
/// <inheritdoc />
public sealed partial class extFS
{
/// <inheritdoc />
public ErrorNumber OpenDir(string path, out IDirNode node)
{
node = null;
if(!_mounted) return ErrorNumber.AccessDenied;
// Normalize path
string normalizedPath = path ?? "/";
if(normalizedPath is "" or ".") normalizedPath = "/";
// Root directory
if(normalizedPath == "/" || string.Equals(normalizedPath, "/", StringComparison.OrdinalIgnoreCase))
{
if(_rootDirectoryCache.Count == 0) return ErrorNumber.NoSuchFile;
node = new ExtDirNode
{
Path = "/",
Position = 0,
Entries = _rootDirectoryCache.Keys.ToArray()
};
return ErrorNumber.NoError;
}
// Subdirectory traversal
string pathWithoutLeadingSlash = normalizedPath.StartsWith("/", StringComparison.Ordinal)
? normalizedPath[1..]
: normalizedPath;
string[] pathComponents = pathWithoutLeadingSlash.Split(['/', '\\'], StringSplitOptions.RemoveEmptyEntries)
.Where(static c => c != "." && c != "..")
.ToArray();
if(pathComponents.Length == 0) return ErrorNumber.InvalidArgument;
AaruLogging.Debug(MODULE_NAME, "Traversing path with {0} components", pathComponents.Length);
// Start from root directory
Dictionary<string, uint> currentEntries = _rootDirectoryCache;
// Traverse each path component
foreach(string component in pathComponents)
{
AaruLogging.Debug(MODULE_NAME, "Navigating to component '{0}'", component);
// Find the component in current directory
if(!currentEntries.TryGetValue(component, out uint childInodeNum))
{
AaruLogging.Debug(MODULE_NAME, "Component '{0}' not found in directory", component);
return ErrorNumber.NoSuchFile;
}
// Read the child inode
ErrorNumber errno = ReadInode(childInodeNum, out ext_inode childInode);
if(errno != ErrorNumber.NoError)
{
AaruLogging.Debug(MODULE_NAME, "Error reading child inode: {0}", errno);
return errno;
}
// Check if it's a directory (S_IFDIR = 0x4000)
if((childInode.i_mode & 0xF000) != 0x4000)
{
AaruLogging.Debug(MODULE_NAME, "Component '{0}' is not a directory", component);
return ErrorNumber.NotDirectory;
}
// Read directory entries from child inode
errno = ReadDirectoryEntries(childInode, out Dictionary<string, uint> childEntries);
if(errno != ErrorNumber.NoError)
{
AaruLogging.Debug(MODULE_NAME, "Error reading child directory entries: {0}", errno);
return errno;
}
currentEntries = childEntries;
}
// Create directory node with found entries
node = new ExtDirNode
{
Path = normalizedPath,
Position = 0,
Entries = currentEntries.Keys.ToArray()
};
AaruLogging.Debug(MODULE_NAME,
"Successfully opened directory '{0}' with {1} entries",
normalizedPath,
currentEntries.Count);
return ErrorNumber.NoError;
}
/// <inheritdoc />
public ErrorNumber CloseDir(IDirNode node)
{
if(node is not ExtDirNode extDirNode) return ErrorNumber.InvalidArgument;
extDirNode.Position = -1;
extDirNode.Entries = null;
return ErrorNumber.NoError;
}
/// <inheritdoc />
public ErrorNumber ReadDir(IDirNode node, out string filename)
{
filename = null;
if(!_mounted) return ErrorNumber.AccessDenied;
if(node is not ExtDirNode extDirNode) return ErrorNumber.InvalidArgument;
if(extDirNode.Position < 0) return ErrorNumber.InvalidArgument;
if(extDirNode.Position >= extDirNode.Entries.Length) return ErrorNumber.NoError;
filename = extDirNode.Entries[extDirNode.Position++];
return ErrorNumber.NoError;
}
/// <summary>Reads directory entries from an inode's data blocks</summary>
/// <param name="inode">The directory inode</param>
/// <param name="entries">Dictionary of filename to inode number</param>

View File

@@ -0,0 +1,49 @@
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Internal.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Linux extended 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2026 Natalia Portillo
// ****************************************************************************/
using Aaru.CommonTypes.Interfaces;
namespace Aaru.Filesystems;
// Information from the Linux kernel
/// <inheritdoc />
public sealed partial class extFS
{
/// <summary>Directory node for enumerating directory contents</summary>
sealed class ExtDirNode : IDirNode
{
/// <summary>Current position in the directory enumeration (entry index)</summary>
internal int Position { get; set; }
/// <summary>Array of directory entry names in this directory</summary>
internal string[] Entries { get; set; }
/// <inheritdoc />
public string Path { get; init; }
}
}

View File

@@ -74,13 +74,4 @@ public sealed partial class extFS
/// <inheritdoc />
public ErrorNumber ReadFile(IFileNode node, long length, byte[] buffer, out long read) =>
throw new NotImplementedException();
/// <inheritdoc />
public ErrorNumber OpenDir(string path, out IDirNode node) => throw new NotImplementedException();
/// <inheritdoc />
public ErrorNumber CloseDir(IDirNode node) => throw new NotImplementedException();
/// <inheritdoc />
public ErrorNumber ReadDir(IDirNode node, out string filename) => throw new NotImplementedException();
}