Files
Aaru/Aaru.Filesystems/FATX/Dir.cs

155 lines
5.5 KiB
C#
Raw Normal View History

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.
//
// --[ Description ] ----------------------------------------------------------
//
// Methods to show the FATX directories.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
2019-04-06 11:03:43 +01:00
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
namespace Aaru.Filesystems;
2019-04-06 11:03:43 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
2021-09-16 04:42:14 +01:00
using Aaru.CommonTypes.Enums;
2020-02-27 00:33:26 +00:00
using Aaru.Helpers;
2019-04-06 11:03:43 +01:00
2022-03-06 13:29:38 +00:00
public sealed partial class XboxFatPlugin
2019-04-06 11:03:43 +01:00
{
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadDir(string path, out List<string> contents)
2019-04-06 11:03:43 +01:00
{
2022-03-06 13:29:38 +00:00
contents = null;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(!_mounted)
return ErrorNumber.AccessDenied;
2022-03-06 13:29:38 +00:00
if(string.IsNullOrWhiteSpace(path) ||
path == "/")
{
contents = _rootDirectory.Keys.ToList();
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-07 07:36:44 +00:00
string cutPath = path.StartsWith('/') ? path.Substring(1).ToLower(_cultureInfo) : path.ToLower(_cultureInfo);
2022-03-06 13:29:38 +00:00
if(_directoryCache.TryGetValue(cutPath, out Dictionary<string, DirectoryEntry> currentDirectory))
{
contents = currentDirectory.Keys.ToList();
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
string[] pieces = cutPath.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
2022-03-06 13:29:38 +00:00
KeyValuePair<string, DirectoryEntry> entry =
_rootDirectory.FirstOrDefault(t => t.Key.ToLower(_cultureInfo) == pieces[0]);
2022-03-06 13:29:38 +00:00
if(string.IsNullOrEmpty(entry.Key))
return ErrorNumber.NoSuchFile;
2022-03-06 13:29:38 +00:00
if(!entry.Value.attributes.HasFlag(Attributes.Directory))
return ErrorNumber.NotDirectory;
2022-03-06 13:29:38 +00:00
string currentPath = pieces[0];
2022-03-06 13:29:38 +00:00
currentDirectory = _rootDirectory;
2022-03-07 07:36:44 +00:00
for(var p = 0; p < pieces.Length; p++)
2022-03-06 13:29:38 +00:00
{
entry = currentDirectory.FirstOrDefault(t => t.Key.ToLower(_cultureInfo) == pieces[p]);
2022-03-06 13:29:38 +00:00
if(string.IsNullOrEmpty(entry.Key))
return ErrorNumber.NoSuchFile;
2022-03-06 13:29:38 +00:00
if(!entry.Value.attributes.HasFlag(Attributes.Directory))
return ErrorNumber.NotDirectory;
2022-03-06 13:29:38 +00:00
currentPath = p == 0 ? pieces[0] : $"{currentPath}/{pieces[p]}";
uint currentCluster = entry.Value.firstCluster;
2022-03-06 13:29:38 +00:00
if(_directoryCache.TryGetValue(currentPath, out currentDirectory))
continue;
2022-03-06 13:29:38 +00:00
uint[] clusters = GetClusters(currentCluster);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(clusters is null)
return ErrorNumber.InvalidArgument;
2022-03-07 07:36:44 +00:00
var directoryBuffer = new byte[_bytesPerCluster * clusters.Length];
2022-03-07 07:36:44 +00:00
for(var i = 0; i < clusters.Length; i++)
2022-03-06 13:29:38 +00:00
{
ErrorNumber errno =
2022-03-07 07:36:44 +00:00
_imagePlugin.ReadSectors(_firstClusterSector + (clusters[i] - 1) * _sectorsPerCluster,
2022-03-06 13:29:38 +00:00
_sectorsPerCluster, out byte[] buffer);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
2022-03-06 13:29:38 +00:00
Array.Copy(buffer, 0, directoryBuffer, i * _bytesPerCluster, _bytesPerCluster);
}
2022-03-06 13:29:38 +00:00
currentDirectory = new Dictionary<string, DirectoryEntry>();
2022-03-07 07:36:44 +00:00
var pos = 0;
2022-03-06 13:29:38 +00:00
while(pos < directoryBuffer.Length)
{
DirectoryEntry dirent = _littleEndian
2022-03-07 07:36:44 +00:00
? Marshal.ByteArrayToStructureLittleEndian<DirectoryEntry>(directoryBuffer,
pos, Marshal.SizeOf<DirectoryEntry>())
2022-03-06 13:29:38 +00:00
: Marshal.ByteArrayToStructureBigEndian<DirectoryEntry>(directoryBuffer,
pos, Marshal.SizeOf<DirectoryEntry>());
pos += Marshal.SizeOf<DirectoryEntry>();
if(dirent.filenameSize == UNUSED_DIRENTRY ||
dirent.filenameSize == FINISHED_DIRENTRY)
break;
if(dirent.filenameSize == DELETED_DIRENTRY ||
dirent.filenameSize > MAX_FILENAME)
continue;
2022-03-06 13:29:38 +00:00
string filename = Encoding.GetString(dirent.filename, 0, dirent.filenameSize);
2022-03-06 13:29:38 +00:00
currentDirectory.Add(filename, dirent);
}
2022-03-06 13:29:38 +00:00
_directoryCache.Add(currentPath, currentDirectory);
}
2022-03-06 13:29:38 +00:00
contents = currentDirectory?.Keys.ToList();
return ErrorNumber.NoError;
2019-04-06 11:03:43 +01:00
}
}