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 : File.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : FATX filesystem plugin.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Methods to handle files.
|
|
|
|
|
//
|
|
|
|
|
// --[ 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;
|
2019-04-07 13:31:27 +01:00
|
|
|
using System.Collections.Generic;
|
2019-04-14 11:30:16 +01:00
|
|
|
using System.IO;
|
2019-04-10 20:25:16 +01:00
|
|
|
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.CommonTypes.Structs;
|
2020-07-20 15:43:52 +01:00
|
|
|
using Aaru.Helpers;
|
2020-02-27 00:33:26 +00:00
|
|
|
using FileAttributes = Aaru.CommonTypes.Structs.FileAttributes;
|
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 MapBlock(string path, long fileBlock, out long deviceBlock)
|
2019-04-07 11:41:35 +01:00
|
|
|
{
|
|
|
|
|
deviceBlock = 0;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(!_mounted)
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.AccessDenied;
|
2019-04-06 11:03:43 +01:00
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
ErrorNumber err = Stat(path, out FileEntryInfo stat);
|
2019-04-14 11:38:20 +01:00
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
if(err != ErrorNumber.NoError)
|
2020-02-29 18:03:35 +00:00
|
|
|
return err;
|
2019-04-14 11:38:20 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(stat.Attributes.HasFlag(FileAttributes.Directory) &&
|
2020-07-20 21:11:32 +01:00
|
|
|
!_debug)
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.IsDirectory;
|
2019-04-15 01:01:38 +01:00
|
|
|
|
2019-04-14 11:38:20 +01:00
|
|
|
uint[] clusters = GetClusters((uint)stat.Inode);
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(fileBlock >= clusters.Length)
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.InvalidArgument;
|
2019-04-14 11:38:20 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
deviceBlock = (long)(_firstClusterSector + ((clusters[fileBlock] - 1) * _sectorsPerCluster));
|
2019-04-14 11:38:20 +01:00
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.NoError;
|
2019-04-07 11:41:35 +01:00
|
|
|
}
|
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 GetAttributes(string path, out FileAttributes attributes)
|
2019-04-07 11:41:35 +01:00
|
|
|
{
|
|
|
|
|
attributes = new FileAttributes();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(!_mounted)
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.AccessDenied;
|
2019-04-06 11:03:43 +01:00
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
ErrorNumber err = Stat(path, out FileEntryInfo stat);
|
2019-04-10 20:34:32 +01:00
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
if(err != ErrorNumber.NoError)
|
2020-02-29 18:03:35 +00:00
|
|
|
return err;
|
2019-04-10 20:34:32 +01:00
|
|
|
|
|
|
|
|
attributes = stat.Attributes;
|
|
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.NoError;
|
2019-04-07 11:41:35 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
/// <inheritdoc />
|
2021-09-16 04:42:14 +01:00
|
|
|
public ErrorNumber Read(string path, long offset, long size, ref byte[] buf)
|
2019-04-07 11:41:35 +01:00
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
if(!_mounted)
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.AccessDenied;
|
2019-04-07 11:41:35 +01:00
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
ErrorNumber err = Stat(path, out FileEntryInfo stat);
|
2019-04-14 11:30:16 +01:00
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
if(err != ErrorNumber.NoError)
|
2020-02-29 18:03:35 +00:00
|
|
|
return err;
|
2019-04-14 11:30:16 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(stat.Attributes.HasFlag(FileAttributes.Directory) &&
|
2020-07-20 21:11:32 +01:00
|
|
|
!_debug)
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.IsDirectory;
|
2019-04-14 11:57:05 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(offset >= stat.Length)
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.InvalidArgument;
|
2019-04-14 11:30:16 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(size + offset >= stat.Length)
|
|
|
|
|
size = stat.Length - offset;
|
2019-04-14 11:30:16 +01:00
|
|
|
|
|
|
|
|
uint[] clusters = GetClusters((uint)stat.Inode);
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
long firstCluster = offset / _bytesPerCluster;
|
|
|
|
|
long offsetInCluster = offset % _bytesPerCluster;
|
|
|
|
|
long sizeInClusters = (size + offsetInCluster) / _bytesPerCluster;
|
2019-04-14 11:30:16 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if((size + offsetInCluster) % _bytesPerCluster > 0)
|
2020-02-29 18:03:35 +00:00
|
|
|
sizeInClusters++;
|
|
|
|
|
|
|
|
|
|
var ms = new MemoryStream();
|
2019-04-14 11:30:16 +01:00
|
|
|
|
|
|
|
|
for(int i = 0; i < sizeInClusters; i++)
|
|
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
if(i + firstCluster >= clusters.Length)
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.InvalidArgument;
|
2019-04-14 11:30:16 +01:00
|
|
|
|
|
|
|
|
byte[] buffer =
|
2020-07-20 21:11:32 +01:00
|
|
|
_imagePlugin.
|
|
|
|
|
ReadSectors(_firstClusterSector + ((clusters[i + firstCluster] - 1) * _sectorsPerCluster),
|
|
|
|
|
_sectorsPerCluster);
|
2019-04-14 11:30:16 +01:00
|
|
|
|
|
|
|
|
ms.Write(buffer, 0, buffer.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ms.Position = offsetInCluster;
|
|
|
|
|
buf = new byte[size];
|
|
|
|
|
ms.Read(buf, 0, (int)size);
|
|
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.NoError;
|
2019-04-07 11:41:35 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
/// <inheritdoc />
|
2021-09-16 04:42:14 +01:00
|
|
|
public ErrorNumber Stat(string path, out FileEntryInfo stat)
|
2019-04-07 11:41:35 +01:00
|
|
|
{
|
|
|
|
|
stat = null;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(!_mounted)
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.AccessDenied;
|
2019-04-07 11:41:35 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(_debug && (string.IsNullOrEmpty(path) || path == "$" || path == "/"))
|
2019-04-10 20:25:16 +01:00
|
|
|
{
|
|
|
|
|
stat = new FileEntryInfo
|
|
|
|
|
{
|
|
|
|
|
Attributes = FileAttributes.Directory | FileAttributes.System | FileAttributes.Hidden,
|
2020-07-20 21:11:32 +01:00
|
|
|
Blocks = GetClusters(_superblock.rootDirectoryCluster).Length,
|
|
|
|
|
BlockSize = _bytesPerCluster,
|
|
|
|
|
Length = GetClusters(_superblock.rootDirectoryCluster).Length * _bytesPerCluster,
|
|
|
|
|
Inode = _superblock.rootDirectoryCluster,
|
2020-07-20 04:34:16 +01:00
|
|
|
Links = 1
|
2019-04-10 20:25:16 +01:00
|
|
|
};
|
|
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.NoError;
|
2019-04-10 20:25:16 +01:00
|
|
|
}
|
|
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
ErrorNumber err = GetFileEntry(path, out DirectoryEntry entry);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
if(err != ErrorNumber.NoError)
|
2020-02-29 18:03:35 +00:00
|
|
|
return err;
|
2019-04-10 20:25:16 +01:00
|
|
|
|
|
|
|
|
stat = new FileEntryInfo
|
|
|
|
|
{
|
2020-07-20 04:34:16 +01:00
|
|
|
Attributes = new FileAttributes(),
|
2020-07-20 21:11:32 +01:00
|
|
|
Blocks = entry.length / _bytesPerCluster,
|
|
|
|
|
BlockSize = _bytesPerCluster,
|
2020-07-20 04:34:16 +01:00
|
|
|
Length = entry.length,
|
|
|
|
|
Inode = entry.firstCluster,
|
|
|
|
|
Links = 1,
|
2020-07-20 21:11:32 +01:00
|
|
|
CreationTime = _littleEndian
|
2020-02-29 18:03:35 +00:00
|
|
|
? DateHandlers.DosToDateTime(entry.creationDate, entry.creationTime).AddYears(20)
|
|
|
|
|
: DateHandlers.DosToDateTime(entry.creationTime, entry.creationDate),
|
2020-07-20 21:11:32 +01:00
|
|
|
AccessTime = _littleEndian
|
2020-02-29 18:03:35 +00:00
|
|
|
? DateHandlers.DosToDateTime(entry.lastAccessDate, entry.lastAccessTime).AddYears(20)
|
|
|
|
|
: DateHandlers.DosToDateTime(entry.lastAccessTime, entry.lastAccessDate),
|
2020-07-20 21:11:32 +01:00
|
|
|
LastWriteTime = _littleEndian
|
2020-02-29 18:03:35 +00:00
|
|
|
? DateHandlers.DosToDateTime(entry.lastWrittenDate, entry.lastWrittenTime).
|
|
|
|
|
AddYears(20)
|
2019-04-15 22:00:54 +01:00
|
|
|
: DateHandlers.DosToDateTime(entry.lastWrittenTime, entry.lastWrittenDate)
|
2019-04-10 20:25:16 +01:00
|
|
|
};
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(entry.length % _bytesPerCluster > 0)
|
2020-02-29 18:03:35 +00:00
|
|
|
stat.Blocks++;
|
2019-04-14 11:31:48 +01:00
|
|
|
|
2019-04-10 20:25:16 +01:00
|
|
|
if(entry.attributes.HasFlag(Attributes.Directory))
|
|
|
|
|
{
|
|
|
|
|
stat.Attributes |= FileAttributes.Directory;
|
|
|
|
|
stat.Blocks = GetClusters(entry.firstCluster).Length;
|
|
|
|
|
stat.Length = stat.Blocks * stat.BlockSize;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(entry.attributes.HasFlag(Attributes.ReadOnly))
|
|
|
|
|
stat.Attributes |= FileAttributes.ReadOnly;
|
|
|
|
|
|
|
|
|
|
if(entry.attributes.HasFlag(Attributes.Hidden))
|
|
|
|
|
stat.Attributes |= FileAttributes.Hidden;
|
|
|
|
|
|
|
|
|
|
if(entry.attributes.HasFlag(Attributes.System))
|
|
|
|
|
stat.Attributes |= FileAttributes.System;
|
|
|
|
|
|
|
|
|
|
if(entry.attributes.HasFlag(Attributes.Archive))
|
|
|
|
|
stat.Attributes |= FileAttributes.Archive;
|
2019-04-10 20:25:16 +01:00
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.NoError;
|
2019-04-07 11:41:35 +01:00
|
|
|
}
|
2019-04-07 13:31:27 +01:00
|
|
|
|
|
|
|
|
uint[] GetClusters(uint startCluster)
|
|
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
if(startCluster == 0)
|
|
|
|
|
return null;
|
2019-04-07 13:31:27 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(_fat16 is null)
|
2019-04-07 13:31:27 +01:00
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
if(startCluster >= _fat32.Length)
|
2020-02-29 18:03:35 +00:00
|
|
|
return null;
|
2019-04-07 13:31:27 +01:00
|
|
|
}
|
2020-07-20 21:11:32 +01:00
|
|
|
else if(startCluster >= _fat16.Length)
|
2020-02-29 18:03:35 +00:00
|
|
|
return null;
|
2019-04-07 13:31:27 +01:00
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
List<uint> clusters = new();
|
2019-04-07 13:31:27 +01:00
|
|
|
|
|
|
|
|
uint nextCluster = startCluster;
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(_fat16 is null)
|
2020-02-29 18:03:35 +00:00
|
|
|
while((nextCluster & FAT32_MASK) > 0 &&
|
2021-06-02 21:15:33 +01:00
|
|
|
(nextCluster & FAT32_MASK) <= FAT32_RESERVED)
|
2019-04-07 13:31:27 +01:00
|
|
|
{
|
|
|
|
|
clusters.Add(nextCluster);
|
2020-07-20 21:11:32 +01:00
|
|
|
nextCluster = _fat32[nextCluster];
|
2019-04-07 13:31:27 +01:00
|
|
|
}
|
|
|
|
|
else
|
2020-02-29 18:03:35 +00:00
|
|
|
while(nextCluster > 0 &&
|
2021-06-02 21:15:33 +01:00
|
|
|
nextCluster <= FAT16_RESERVED)
|
2019-04-07 13:31:27 +01:00
|
|
|
{
|
|
|
|
|
clusters.Add(nextCluster);
|
2020-07-20 21:11:32 +01:00
|
|
|
nextCluster = _fat16[nextCluster];
|
2019-04-07 13:31:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return clusters.ToArray();
|
|
|
|
|
}
|
2019-04-10 20:25:16 +01:00
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
ErrorNumber GetFileEntry(string path, out DirectoryEntry entry)
|
2019-04-10 20:25:16 +01:00
|
|
|
{
|
|
|
|
|
entry = new DirectoryEntry();
|
|
|
|
|
|
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);
|
2019-04-10 20:25:16 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
string[] pieces = cutPath.Split(new[]
|
|
|
|
|
{
|
|
|
|
|
'/'
|
|
|
|
|
}, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
|
|
|
|
if(pieces.Length == 0)
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.InvalidArgument;
|
2019-04-10 20:25:16 +01:00
|
|
|
|
|
|
|
|
string parentPath = string.Join("/", pieces, 0, pieces.Length - 1);
|
|
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
ErrorNumber err = ReadDir(parentPath, out _);
|
2019-04-10 20:25:16 +01:00
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
if(err != ErrorNumber.NoError)
|
2020-02-29 18:03:35 +00:00
|
|
|
return err;
|
2019-04-10 20:25:16 +01:00
|
|
|
|
|
|
|
|
Dictionary<string, DirectoryEntry> parent;
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(pieces.Length == 1)
|
2020-07-20 21:11:32 +01:00
|
|
|
parent = _rootDirectory;
|
|
|
|
|
else if(!_directoryCache.TryGetValue(parentPath, out parent))
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.InvalidArgument;
|
2019-04-10 20:25:16 +01:00
|
|
|
|
|
|
|
|
KeyValuePair<string, DirectoryEntry> dirent =
|
2020-07-20 21:11:32 +01:00
|
|
|
parent.FirstOrDefault(t => t.Key.ToLower(_cultureInfo) == pieces[^1]);
|
2019-04-10 20:25:16 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(string.IsNullOrEmpty(dirent.Key))
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.NoSuchFile;
|
2019-04-10 20:25:16 +01:00
|
|
|
|
|
|
|
|
entry = dirent.Value;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.NoError;
|
2019-04-10 20:25:16 +01:00
|
|
|
}
|
2019-04-06 11:03:43 +01:00
|
|
|
}
|
|
|
|
|
}
|