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

156 lines
5.8 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/>.
//
// ----------------------------------------------------------------------------
2020-12-31 23:08:23 +00:00
// Copyright © 2011-2021 Natalia Portillo
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
2020-07-20 15:43:52 +01:00
namespace Aaru.Filesystems
2019-04-06 11:03:43 +01:00
{
2020-07-22 13:20:25 +01:00
public sealed partial class XboxFatPlugin
2019-04-06 11:03:43 +01:00
{
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-16 04:42:14 +01:00
public ErrorNumber ReadDir(string path, out List<string> contents)
{
contents = null;
2020-07-20 21:11:32 +01:00
if(!_mounted)
2021-09-16 04:42:14 +01:00
return ErrorNumber.AccessDenied;
2020-02-29 18:03:35 +00:00
if(string.IsNullOrWhiteSpace(path) ||
path == "/")
{
2020-07-20 21:11:32 +01:00
contents = _rootDirectory.Keys.ToList();
2020-02-29 18:03:35 +00:00
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoError;
}
2020-07-22 13:20:25 +01:00
string cutPath = path.StartsWith('/') ? path.Substring(1).ToLower(_cultureInfo)
2020-07-20 21:11:32 +01:00
: path.ToLower(_cultureInfo);
2020-07-20 21:11:32 +01:00
if(_directoryCache.TryGetValue(cutPath, out Dictionary<string, DirectoryEntry> currentDirectory))
{
contents = currentDirectory.Keys.ToList();
2020-02-29 18:03:35 +00:00
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoError;
}
2020-02-29 18:03:35 +00:00
string[] pieces = cutPath.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
KeyValuePair<string, DirectoryEntry> entry =
2020-07-20 21:11:32 +01:00
_rootDirectory.FirstOrDefault(t => t.Key.ToLower(_cultureInfo) == pieces[0]);
2020-02-29 18:03:35 +00:00
if(string.IsNullOrEmpty(entry.Key))
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoSuchFile;
2020-02-29 18:03:35 +00:00
if(!entry.Value.attributes.HasFlag(Attributes.Directory))
2021-09-16 04:42:14 +01:00
return ErrorNumber.NotDirectory;
string currentPath = pieces[0];
2020-07-20 21:11:32 +01:00
currentDirectory = _rootDirectory;
for(int p = 0; p < pieces.Length; p++)
{
2020-07-20 21:11:32 +01:00
entry = currentDirectory.FirstOrDefault(t => t.Key.ToLower(_cultureInfo) == pieces[p]);
2020-02-29 18:03:35 +00:00
if(string.IsNullOrEmpty(entry.Key))
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoSuchFile;
2020-02-29 18:03:35 +00:00
if(!entry.Value.attributes.HasFlag(Attributes.Directory))
2021-09-16 04:42:14 +01:00
return ErrorNumber.NotDirectory;
currentPath = p == 0 ? pieces[0] : $"{currentPath}/{pieces[p]}";
uint currentCluster = entry.Value.firstCluster;
2020-07-20 21:11:32 +01:00
if(_directoryCache.TryGetValue(currentPath, out currentDirectory))
2020-02-29 18:03:35 +00:00
continue;
uint[] clusters = GetClusters(currentCluster);
2020-02-29 18:03:35 +00:00
if(clusters is null)
2021-09-16 04:42:14 +01:00
return ErrorNumber.InvalidArgument;
2020-07-20 21:11:32 +01:00
byte[] directoryBuffer = new byte[_bytesPerCluster * clusters.Length];
for(int i = 0; i < clusters.Length; i++)
{
2020-02-29 18:03:35 +00:00
byte[] buffer =
2020-07-20 21:11:32 +01:00
_imagePlugin.ReadSectors(_firstClusterSector + ((clusters[i] - 1) * _sectorsPerCluster),
_sectorsPerCluster);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
Array.Copy(buffer, 0, directoryBuffer, i * _bytesPerCluster, _bytesPerCluster);
}
currentDirectory = new Dictionary<string, DirectoryEntry>();
int pos = 0;
2020-02-29 18:03:35 +00:00
while(pos < directoryBuffer.Length)
{
2020-07-20 21:11:32 +01:00
DirectoryEntry dirent = _littleEndian
2020-02-29 18:03:35 +00:00
? Marshal.
2021-08-17 21:23:10 +01:00
ByteArrayToStructureLittleEndian<
DirectoryEntry>(directoryBuffer, pos,
Marshal.SizeOf<DirectoryEntry>())
: Marshal.ByteArrayToStructureBigEndian<DirectoryEntry>(directoryBuffer,
pos, Marshal.SizeOf<DirectoryEntry>());
pos += Marshal.SizeOf<DirectoryEntry>();
2020-02-29 18:03:35 +00:00
if(dirent.filenameSize == UNUSED_DIRENTRY ||
dirent.filenameSize == FINISHED_DIRENTRY)
break;
2020-02-29 18:03:35 +00:00
if(dirent.filenameSize == DELETED_DIRENTRY ||
dirent.filenameSize > MAX_FILENAME)
continue;
string filename = Encoding.GetString(dirent.filename, 0, dirent.filenameSize);
currentDirectory.Add(filename, dirent);
}
2020-07-20 21:11:32 +01:00
_directoryCache.Add(currentPath, currentDirectory);
}
contents = currentDirectory?.Keys.ToList();
2020-02-29 18:03:35 +00:00
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoError;
}
2019-04-06 11:03:43 +01:00
}
}