Files
Aaru/Aaru.Filesystems/FAT/File.cs

348 lines
11 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : File.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Microsoft FAT 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/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/
using System;
2019-04-26 19:56:58 +01:00
using System.Collections.Generic;
2019-04-27 00:56:49 +01:00
using System.IO;
2019-04-27 00:45:36 +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;
namespace Aaru.Filesystems;
2022-03-06 13:29:38 +00:00
public sealed partial class FAT
{
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber MapBlock(string path, long fileBlock, out long deviceBlock)
{
2022-03-06 13:29:38 +00:00
deviceBlock = 0;
2019-04-27 00:53:43 +01:00
2022-03-06 13:29:38 +00:00
if(!_mounted)
return ErrorNumber.AccessDenied;
2019-04-27 00:53:43 +01:00
2022-03-06 13:29:38 +00:00
ErrorNumber err = Stat(path, out FileEntryInfo stat);
2019-04-27 00:53:43 +01:00
2022-03-06 13:29:38 +00:00
if(err != ErrorNumber.NoError)
return err;
2019-04-27 00:53:43 +01:00
2022-03-06 13:29:38 +00:00
if(stat.Attributes.HasFlag(FileAttributes.Directory) &&
!_debug)
return ErrorNumber.IsDirectory;
2022-03-06 13:29:38 +00:00
uint[] clusters = GetClusters((uint)stat.Inode);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(fileBlock >= clusters.Length)
return ErrorNumber.InvalidArgument;
deviceBlock = (long)(_firstClusterSector + (clusters[fileBlock] * _sectorsPerCluster));
2019-04-27 00:53:09 +01:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber GetAttributes(string path, out FileAttributes attributes)
{
attributes = new FileAttributes();
2022-03-06 13:29:38 +00:00
if(!_mounted)
return ErrorNumber.AccessDenied;
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
ErrorNumber err = Stat(path, out FileEntryInfo stat);
2019-04-27 00:56:49 +01:00
2022-03-06 13:29:38 +00:00
if(err != ErrorNumber.NoError)
return err;
2019-04-27 00:56:49 +01:00
2022-03-06 13:29:38 +00:00
attributes = stat.Attributes;
2019-04-27 00:56:49 +01:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Read(string path, long offset, long size, ref byte[] buf)
{
if(!_mounted)
return ErrorNumber.AccessDenied;
2019-04-27 00:56:49 +01:00
2022-03-06 13:29:38 +00:00
ErrorNumber err = Stat(path, out FileEntryInfo stat);
2019-04-27 00:56:49 +01:00
2022-03-06 13:29:38 +00:00
if(err != ErrorNumber.NoError)
return err;
2019-04-27 00:56:49 +01:00
2022-03-06 13:29:38 +00:00
if(stat.Attributes.HasFlag(FileAttributes.Directory) &&
!_debug)
return ErrorNumber.IsDirectory;
2021-06-02 21:15:33 +01:00
2022-03-06 13:29:38 +00:00
if(size == 0)
{
buf = Array.Empty<byte>();
2019-04-27 00:56:49 +01:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(offset >= stat.Length)
return ErrorNumber.InvalidArgument;
2019-04-27 00:56:49 +01:00
2022-03-06 13:29:38 +00:00
if(size + offset >= stat.Length)
size = stat.Length - offset;
2019-04-27 00:56:49 +01:00
2022-03-06 13:29:38 +00:00
uint[] clusters = GetClusters((uint)stat.Inode);
2022-03-06 13:29:38 +00:00
if(clusters is null)
return ErrorNumber.InvalidArgument;
2019-04-27 00:56:49 +01:00
2022-03-06 13:29:38 +00:00
long firstCluster = offset / _bytesPerCluster;
long offsetInCluster = offset % _bytesPerCluster;
long sizeInClusters = (size + offsetInCluster) / _bytesPerCluster;
2019-04-27 00:56:49 +01:00
2022-03-06 13:29:38 +00:00
if((size + offsetInCluster) % _bytesPerCluster > 0)
sizeInClusters++;
2019-04-27 00:56:49 +01:00
2022-03-06 13:29:38 +00:00
var ms = new MemoryStream();
2019-04-26 00:54:51 +01:00
for(int i = 0; i < sizeInClusters; i++)
2019-04-26 00:54:51 +01:00
{
2022-03-06 13:29:38 +00:00
if(i + firstCluster >= clusters.Length)
return ErrorNumber.InvalidArgument;
2020-02-29 18:03:35 +00:00
ErrorNumber errno =
_image.ReadSectors(_firstClusterSector + (clusters[i + firstCluster] * _sectorsPerCluster),
_sectorsPerCluster, out byte[] buffer);
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
ms.Write(buffer, 0, buffer.Length);
}
2019-04-27 00:52:40 +01:00
2022-03-06 13:29:38 +00:00
ms.Position = offsetInCluster;
buf = new byte[size];
ms.EnsureRead(buf, 0, (int)size);
2019-04-28 11:57:54 +01:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Stat(string path, out FileEntryInfo stat)
{
stat = null;
2019-04-27 00:52:40 +01:00
2022-03-06 13:29:38 +00:00
if(!_mounted)
return ErrorNumber.AccessDenied;
2019-04-27 00:52:40 +01:00
2022-03-06 13:29:38 +00:00
ErrorNumber err = GetFileEntry(path, out CompleteDirectoryEntry completeEntry);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(err != ErrorNumber.NoError)
return err;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
DirectoryEntry entry = completeEntry.Dirent;
2019-04-27 00:52:40 +01:00
2022-03-06 13:29:38 +00:00
stat = new FileEntryInfo
{
Attributes = new FileAttributes(),
Blocks = entry.size / _bytesPerCluster,
BlockSize = _bytesPerCluster,
Length = entry.size,
Inode = (ulong)(_fat32 ? (entry.ea_handle << 16) + entry.start_cluster : entry.start_cluster),
Links = 1
};
if(entry.cdate > 0 ||
entry.ctime > 0)
stat.CreationTime = DateHandlers.DosToDateTime(entry.cdate, entry.ctime);
if(_namespace != Namespace.Human)
{
if(entry.mdate > 0 ||
entry.mtime > 0)
stat.LastWriteTime = DateHandlers.DosToDateTime(entry.mdate, entry.mtime);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(entry.ctime_ms > 0)
stat.CreationTime = stat.CreationTime?.AddMilliseconds(entry.ctime_ms * 10);
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(entry.size % _bytesPerCluster > 0)
stat.Blocks++;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(entry.attributes.HasFlag(FatAttributes.Subdirectory))
{
stat.Attributes |= FileAttributes.Directory;
2020-02-29 18:03:35 +00:00
if((_fat32 && entry.ea_handle << 16 > 0) ||
2022-03-06 13:29:38 +00:00
entry.start_cluster > 0)
2022-03-07 07:36:44 +00:00
stat.Blocks = _fat32 ? GetClusters((uint)((entry.ea_handle << 16) + entry.start_cluster))?.Length ?? 0
: GetClusters(entry.start_cluster)?.Length ?? 0;
2019-04-27 00:52:40 +01:00
2022-03-06 13:29:38 +00:00
stat.Length = stat.Blocks * stat.BlockSize;
2019-04-26 00:54:51 +01:00
}
2019-04-26 19:56:58 +01:00
2022-03-06 13:29:38 +00:00
if(entry.attributes.HasFlag(FatAttributes.ReadOnly))
stat.Attributes |= FileAttributes.ReadOnly;
2019-04-26 19:56:58 +01:00
2022-03-06 13:29:38 +00:00
if(entry.attributes.HasFlag(FatAttributes.Hidden))
stat.Attributes |= FileAttributes.Hidden;
2019-04-26 19:56:58 +01:00
2022-03-06 13:29:38 +00:00
if(entry.attributes.HasFlag(FatAttributes.System))
stat.Attributes |= FileAttributes.System;
2019-04-26 19:56:58 +01:00
2022-03-06 13:29:38 +00:00
if(entry.attributes.HasFlag(FatAttributes.Archive))
stat.Attributes |= FileAttributes.Archive;
2019-04-26 19:56:58 +01:00
2022-03-06 13:29:38 +00:00
if(entry.attributes.HasFlag(FatAttributes.Device))
stat.Attributes |= FileAttributes.Device;
2020-07-20 21:11:32 +01:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2019-04-26 19:56:58 +01:00
2022-03-06 13:29:38 +00:00
uint[] GetClusters(uint startCluster)
{
if(startCluster == 0)
return Array.Empty<uint>();
2022-03-06 13:29:38 +00:00
if(startCluster >= XmlFsType.Clusters)
return null;
2019-04-26 19:56:58 +01:00
2022-03-06 13:29:38 +00:00
List<uint> clusters = new();
2019-04-26 19:56:58 +01:00
2022-03-06 13:29:38 +00:00
uint nextCluster = startCluster;
ulong nextSector = (nextCluster / _fatEntriesPerSector) + _fatFirstSector + (_useFirstFat ? 0 : _sectorsPerFat);
int nextEntry = (int)(nextCluster % _fatEntriesPerSector);
2019-04-26 19:56:58 +01:00
2022-03-06 13:29:38 +00:00
ulong currentSector = nextSector;
ErrorNumber errno = _image.ReadSector(currentSector, out byte[] fatData);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return null;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(_fat32)
while((nextCluster & FAT32_MASK) > 0 &&
(nextCluster & FAT32_MASK) <= FAT32_RESERVED)
{
clusters.Add(nextCluster);
2022-03-06 13:29:38 +00:00
if(currentSector != nextSector)
2019-04-27 02:26:31 +01:00
{
2022-03-06 13:29:38 +00:00
errno = _image.ReadSector(nextSector, out fatData);
if(errno != ErrorNumber.NoError)
return null;
2022-03-06 13:29:38 +00:00
currentSector = nextSector;
2019-04-27 02:26:31 +01:00
}
2019-04-26 19:56:58 +01:00
2022-03-06 13:29:38 +00:00
nextCluster = BitConverter.ToUInt32(fatData, nextEntry * 4);
2019-04-27 00:45:36 +01:00
nextSector = (nextCluster / _fatEntriesPerSector) + _fatFirstSector +
(_useFirstFat ? 0 : _sectorsPerFat);
2019-04-27 00:45:36 +01:00
2022-03-06 13:29:38 +00:00
nextEntry = (int)(nextCluster % _fatEntriesPerSector);
}
else if(_fat16)
while(nextCluster is > 0 and <= FAT16_RESERVED)
2020-02-29 18:03:35 +00:00
{
2022-03-06 13:29:38 +00:00
if(nextCluster > _fatEntries.Length)
return null;
2019-04-27 00:45:36 +01:00
2022-03-06 13:29:38 +00:00
clusters.Add(nextCluster);
nextCluster = _fatEntries[nextCluster];
}
else
while(nextCluster is > 0 and <= FAT12_RESERVED)
{
2022-03-06 13:29:38 +00:00
if(nextCluster > _fatEntries.Length)
return null;
2019-04-27 00:45:36 +01:00
2022-03-06 13:29:38 +00:00
clusters.Add(nextCluster);
nextCluster = _fatEntries[nextCluster];
}
2019-04-27 00:45:36 +01:00
2022-03-06 13:29:38 +00:00
return clusters.ToArray();
}
2019-04-27 00:45:36 +01:00
2022-03-06 13:29:38 +00:00
ErrorNumber GetFileEntry(string path, out CompleteDirectoryEntry entry)
{
entry = null;
2019-04-27 00:45:36 +01:00
2022-11-14 01:15:06 +00:00
string cutPath = path.StartsWith('/') ? path[1..].ToLower(_cultureInfo) : path.ToLower(_cultureInfo);
2019-04-27 00:45:36 +01:00
2022-03-06 13:29:38 +00:00
string[] pieces = cutPath.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
2019-04-27 00:45:36 +01:00
2022-03-06 13:29:38 +00:00
if(pieces.Length == 0)
return ErrorNumber.InvalidArgument;
2020-02-29 18:03:35 +00:00
string parentPath = string.Join("/", pieces, 0, pieces.Length - 1);
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
if(!_directoryCache.TryGetValue(parentPath, out _))
2019-04-27 13:04:59 +01:00
{
2022-03-06 13:29:38 +00:00
ErrorNumber err = ReadDir(parentPath, out _);
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
if(err != ErrorNumber.NoError)
return err;
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
Dictionary<string, CompleteDirectoryEntry> parent;
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
if(pieces.Length == 1)
parent = _rootDirectoryCache;
else if(!_directoryCache.TryGetValue(parentPath, out parent))
return ErrorNumber.InvalidArgument;
KeyValuePair<string, CompleteDirectoryEntry> dirent =
parent.FirstOrDefault(t => t.Key.ToLower(_cultureInfo) == pieces[^1]);
if(string.IsNullOrEmpty(dirent.Key))
return ErrorNumber.NoSuchFile;
entry = dirent.Value;
return ErrorNumber.NoError;
}
2022-11-15 01:35:06 +00:00
static byte LfnChecksum(byte[] name, byte[] extension)
2022-03-06 13:29:38 +00:00
{
byte sum = 0;
for(int i = 0; i < 8; i++)
2022-03-06 13:29:38 +00:00
sum = (byte)(((sum & 1) << 7) + (sum >> 1) + name[i]);
for(int i = 0; i < 3; i++)
2022-03-06 13:29:38 +00:00
sum = (byte)(((sum & 1) << 7) + (sum >> 1) + extension[i]);
return sum;
}
}