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