2019-04-06 11:03:43 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2019-04-06 11:03:43 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : Dir.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : FATX 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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2024-12-19 10:45:18 +00:00
|
|
|
// Copyright © 2011-2025 Natalia Portillo
|
2019-04-06 11:03:43 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-04-10 19:15:04 +01:00
|
|
|
using System.Linq;
|
2021-09-16 04:42:14 +01:00
|
|
|
using Aaru.CommonTypes.Enums;
|
2022-12-21 19:10:51 +00:00
|
|
|
using Aaru.CommonTypes.Interfaces;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.Helpers;
|
2019-04-06 11:03:43 +01:00
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
namespace Aaru.Filesystems;
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
public sealed partial class XboxFatPlugin
|
2019-04-06 11:03:43 +01:00
|
|
|
{
|
2023-10-03 23:22:08 +01:00
|
|
|
#region IReadOnlyFilesystem Members
|
|
|
|
|
|
2022-12-21 19:10:51 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ErrorNumber OpenDir(string path, out IDirNode node)
|
|
|
|
|
{
|
|
|
|
|
node = null;
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!_mounted) return ErrorNumber.AccessDenied;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
2023-10-04 17:34:40 +01:00
|
|
|
if(string.IsNullOrWhiteSpace(path) || path == "/")
|
2022-12-21 19:10:51 +00:00
|
|
|
{
|
|
|
|
|
node = new FatxDirNode
|
|
|
|
|
{
|
2023-10-05 01:52:48 +01:00
|
|
|
Path = path,
|
|
|
|
|
Position = 0,
|
|
|
|
|
Entries = _rootDirectory.Values.ToArray()
|
2022-12-21 19:10:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string cutPath = path.StartsWith('/') ? path[1..].ToLower(_cultureInfo) : path.ToLower(_cultureInfo);
|
|
|
|
|
|
|
|
|
|
if(_directoryCache.TryGetValue(cutPath, out Dictionary<string, DirectoryEntry> currentDirectory))
|
|
|
|
|
{
|
|
|
|
|
node = new FatxDirNode
|
|
|
|
|
{
|
2023-10-05 01:52:48 +01:00
|
|
|
Path = path,
|
|
|
|
|
Position = 0,
|
|
|
|
|
Entries = currentDirectory.Values.ToArray()
|
2022-12-21 19:10:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-24 03:08:01 +00:00
|
|
|
string[] pieces = cutPath.Split(['/'], StringSplitOptions.RemoveEmptyEntries);
|
2022-12-21 19:10:51 +00:00
|
|
|
|
|
|
|
|
KeyValuePair<string, DirectoryEntry> entry =
|
|
|
|
|
_rootDirectory.FirstOrDefault(t => t.Key.ToLower(_cultureInfo) == pieces[0]);
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(string.IsNullOrEmpty(entry.Key)) return ErrorNumber.NoSuchFile;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!entry.Value.attributes.HasFlag(Attributes.Directory)) return ErrorNumber.NotDirectory;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
|
|
|
|
string currentPath = pieces[0];
|
|
|
|
|
|
|
|
|
|
currentDirectory = _rootDirectory;
|
|
|
|
|
|
2023-10-03 23:22:08 +01:00
|
|
|
for(var p = 0; p < pieces.Length; p++)
|
2022-12-21 19:10:51 +00:00
|
|
|
{
|
|
|
|
|
entry = currentDirectory.FirstOrDefault(t => t.Key.ToLower(_cultureInfo) == pieces[p]);
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(string.IsNullOrEmpty(entry.Key)) return ErrorNumber.NoSuchFile;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!entry.Value.attributes.HasFlag(Attributes.Directory)) return ErrorNumber.NotDirectory;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
|
|
|
|
currentPath = p == 0 ? pieces[0] : $"{currentPath}/{pieces[p]}";
|
|
|
|
|
uint currentCluster = entry.Value.firstCluster;
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(_directoryCache.TryGetValue(currentPath, out currentDirectory)) continue;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
|
|
|
|
uint[] clusters = GetClusters(currentCluster);
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(clusters is null) return ErrorNumber.InvalidArgument;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
2023-10-03 23:22:08 +01:00
|
|
|
var directoryBuffer = new byte[_bytesPerCluster * clusters.Length];
|
2022-12-21 19:10:51 +00:00
|
|
|
|
2023-10-03 23:22:08 +01:00
|
|
|
for(var i = 0; i < clusters.Length; i++)
|
2022-12-21 19:10:51 +00:00
|
|
|
{
|
|
|
|
|
ErrorNumber errno =
|
2023-10-03 23:22:08 +01:00
|
|
|
_imagePlugin.ReadSectors(_firstClusterSector + (clusters[i] - 1) * _sectorsPerCluster,
|
2025-10-23 03:07:43 +01:00
|
|
|
false,
|
2024-05-01 04:05:22 +01:00
|
|
|
_sectorsPerCluster,
|
2025-10-22 14:28:58 +01:00
|
|
|
out byte[] buffer,
|
|
|
|
|
out _);
|
2022-12-21 19:10:51 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(errno != ErrorNumber.NoError) return errno;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
|
|
|
|
Array.Copy(buffer, 0, directoryBuffer, i * _bytesPerCluster, _bytesPerCluster);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentDirectory = new Dictionary<string, DirectoryEntry>();
|
|
|
|
|
|
2023-10-03 23:22:08 +01:00
|
|
|
var pos = 0;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
|
|
|
|
while(pos < directoryBuffer.Length)
|
|
|
|
|
{
|
|
|
|
|
DirectoryEntry dirent = _littleEndian
|
|
|
|
|
? Marshal.ByteArrayToStructureLittleEndian<DirectoryEntry>(directoryBuffer,
|
2024-05-01 04:05:22 +01:00
|
|
|
pos,
|
|
|
|
|
Marshal.SizeOf<DirectoryEntry>())
|
2025-10-21 11:43:05 +01:00
|
|
|
: Marshal.ByteArrayToStructureBigEndian<DirectoryEntry>(directoryBuffer,
|
2024-05-01 04:05:22 +01:00
|
|
|
pos,
|
|
|
|
|
Marshal.SizeOf<DirectoryEntry>());
|
2022-12-21 19:10:51 +00:00
|
|
|
|
|
|
|
|
pos += Marshal.SizeOf<DirectoryEntry>();
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(dirent.filenameSize is UNUSED_DIRENTRY or FINISHED_DIRENTRY) break;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(dirent.filenameSize is DELETED_DIRENTRY or > MAX_FILENAME) continue;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
|
|
|
|
string filename = _encoding.GetString(dirent.filename, 0, dirent.filenameSize);
|
|
|
|
|
|
|
|
|
|
currentDirectory.Add(filename, dirent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_directoryCache.Add(currentPath, currentDirectory);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(currentDirectory is null) return ErrorNumber.NoSuchFile;
|
2022-12-21 19:10:51 +00:00
|
|
|
|
|
|
|
|
node = new FatxDirNode
|
|
|
|
|
{
|
2023-10-05 01:52:48 +01:00
|
|
|
Path = path,
|
|
|
|
|
Position = 0,
|
|
|
|
|
Entries = currentDirectory.Values.ToArray()
|
2022-12-21 19:10:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
2022-12-21 20:03:24 +00:00
|
|
|
public ErrorNumber ReadDir(IDirNode node, out string filename)
|
2019-04-06 11:03:43 +01:00
|
|
|
{
|
2022-12-21 20:03:24 +00:00
|
|
|
filename = null;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!_mounted) return ErrorNumber.AccessDenied;
|
2019-04-10 19:15:04 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(node is not FatxDirNode mynode) return ErrorNumber.InvalidArgument;
|
2019-04-10 19:15:04 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(mynode.Position < 0) return ErrorNumber.InvalidArgument;
|
2019-04-10 19:15:04 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(mynode.Position >= mynode.Entries.Length) return ErrorNumber.NoError;
|
2019-04-10 19:15:04 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
filename = _encoding.GetString(mynode.Entries[mynode.Position].filename,
|
|
|
|
|
0,
|
2023-10-05 01:52:48 +01:00
|
|
|
mynode.Entries[mynode.Position].filenameSize);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2023-10-05 01:52:48 +01:00
|
|
|
mynode.Position++;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
2019-04-06 11:03:43 +01:00
|
|
|
}
|
2022-12-21 19:24:06 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ErrorNumber CloseDir(IDirNode node)
|
|
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(node is not FatxDirNode mynode) return ErrorNumber.InvalidArgument;
|
2022-12-21 19:24:06 +00:00
|
|
|
|
2023-10-05 01:52:48 +01:00
|
|
|
mynode.Position = -1;
|
|
|
|
|
mynode.Entries = null;
|
2022-12-21 19:24:06 +00:00
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
2023-10-03 23:22:08 +01:00
|
|
|
|
|
|
|
|
#endregion
|
2019-04-06 11:03:43 +01:00
|
|
|
}
|