Files
Aaru/Aaru.Filesystems/FAT/Super.cs

1003 lines
39 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Super.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Microsoft FAT filesystem plugin.
//
// --[ 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-12-03 16:07:10 +00:00
// Copyright © 2011-2023 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.Globalization;
2019-04-26 00:54:51 +01:00
using System.IO;
using System.Linq;
2019-04-27 02:38:48 +01:00
using System.Runtime.InteropServices;
using System.Text;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Structs;
using Aaru.Console;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
2019-04-26 00:54:51 +01:00
using Schemas;
2020-02-27 00:33:26 +00:00
using FileSystemInfo = Aaru.CommonTypes.Structs.FileSystemInfo;
using Marshal = Aaru.Helpers.Marshal;
namespace Aaru.Filesystems;
2022-03-06 13:29:38 +00:00
public sealed partial class FAT
{
2022-03-06 13:29:38 +00:00
uint _fatEntriesPerSector;
IMediaImage _image;
/// <inheritdoc />
public ErrorNumber Mount(IMediaImage imagePlugin, Partition partition, Encoding encoding,
Dictionary<string, string> options, string @namespace)
{
2022-03-06 13:29:38 +00:00
XmlFsType = new FileSystemType();
options ??= GetDefaultOptions();
2019-04-26 19:57:19 +01:00
2022-03-06 13:29:38 +00:00
if(options.TryGetValue("debug", out string debugString))
bool.TryParse(debugString, out _debug);
// Default namespace
@namespace ??= "ecs";
switch(@namespace.ToLowerInvariant())
2019-04-26 00:54:51 +01:00
{
2022-03-06 13:29:38 +00:00
case "dos":
_namespace = Namespace.Dos;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
break;
case "nt":
_namespace = Namespace.Nt;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
break;
case "os2":
_namespace = Namespace.Os2;
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
break;
case "ecs":
_namespace = Namespace.Ecs;
2022-03-06 13:29:38 +00:00
break;
case "lfn":
_namespace = Namespace.Lfn;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
break;
case "human":
_namespace = Namespace.Human;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
break;
default: return ErrorNumber.InvalidArgument;
}
2020-02-29 18:03:35 +00:00
AaruConsole.DebugWriteLine("FAT plugin", Localization.Reading_BPB);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
uint sectorsPerBpb = imagePlugin.Info.SectorSize < 512 ? 512 / imagePlugin.Info.SectorSize : 1;
2020-02-29 18:03:35 +00:00
ErrorNumber errno = imagePlugin.ReadSectors(0 + partition.Start, sectorsPerBpb, out byte[] bpbSector);
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
BpbKind bpbKind = DetectBpbKind(bpbSector, imagePlugin, partition, out BiosParameterBlockEbpb fakeBpb,
out HumanParameterBlock humanBpb, out AtariParameterBlock atariBpb,
out byte minBootNearJump, out bool andosOemCorrect, out bool bootable);
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
_fat12 = false;
_fat16 = false;
_fat32 = false;
_useFirstFat = true;
XmlFsType.Bootable = bootable;
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
_statfs = new FileSystemInfo
{
FilenameLength = 11,
Files = 0, // Requires traversing all directories
FreeFiles = 0,
PluginId = Id,
FreeBlocks = 0 // Requires traversing the FAT
};
2022-03-06 13:29:38 +00:00
// This is needed because for FAT16, GEMDOS increases bytes per sector count instead of using big_sectors field.
uint sectorsPerRealSector = 1;
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
// This is needed because some OSes don't put volume label as first entry in the root directory
uint sectorsForRootDirectory = 0;
uint rootDirectoryCluster = 0;
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
Encoding = encoding ?? (bpbKind == BpbKind.Human ? Encoding.GetEncoding("shift_jis")
: Encoding.GetEncoding("IBM437"));
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
switch(bpbKind)
{
case BpbKind.DecRainbow:
case BpbKind.Hardcoded:
case BpbKind.Msx:
case BpbKind.Apricot:
_fat12 = true;
break;
case BpbKind.ShortFat32:
case BpbKind.LongFat32:
2019-04-26 23:06:05 +01:00
{
2022-03-06 13:29:38 +00:00
_fat32 = true;
2019-04-26 23:06:05 +01:00
2022-03-07 07:36:44 +00:00
Fat32ParameterBlock fat32Bpb = Marshal.ByteArrayToStructureLittleEndian<Fat32ParameterBlock>(bpbSector);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
Fat32ParameterBlockShort shortFat32Bpb =
Marshal.ByteArrayToStructureLittleEndian<Fat32ParameterBlockShort>(bpbSector);
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
rootDirectoryCluster = fat32Bpb.root_cluster;
2022-03-06 13:29:38 +00:00
// This is to support FAT partitions on hybrid ISO/USB images
if(imagePlugin.Info.XmlMediaType == XmlMediaType.OpticalDisc)
2019-04-26 00:54:51 +01:00
{
2022-03-06 13:29:38 +00:00
fat32Bpb.bps *= 4;
fat32Bpb.spc /= 4;
fat32Bpb.big_spfat /= 4;
fat32Bpb.hsectors /= 4;
fat32Bpb.sptrk /= 4;
}
2020-02-29 18:03:35 +00:00
XmlFsType.Type = fat32Bpb.version != 0 ? FS_TYPE_FAT_PLUS : FS_TYPE_FAT32;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(fat32Bpb.oem_name != null &&
(fat32Bpb.oem_name[5] != 0x49 || fat32Bpb.oem_name[6] != 0x48 || fat32Bpb.oem_name[7] != 0x43))
XmlFsType.SystemIdentifier = StringHandlers.CToString(fat32Bpb.oem_name);
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
_sectorsPerCluster = fat32Bpb.spc;
XmlFsType.ClusterSize = (uint)(fat32Bpb.bps * fat32Bpb.spc);
_reservedSectors = fat32Bpb.rsectors;
2019-04-26 19:57:19 +01:00
if(fat32Bpb is { big_sectors: 0, signature: 0x28 })
2022-03-06 13:29:38 +00:00
XmlFsType.Clusters = shortFat32Bpb.huge_sectors / shortFat32Bpb.spc;
else if(fat32Bpb.sectors == 0)
XmlFsType.Clusters = fat32Bpb.big_sectors / fat32Bpb.spc;
else
XmlFsType.Clusters = (ulong)(fat32Bpb.sectors / fat32Bpb.spc);
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
_sectorsPerFat = fat32Bpb.big_spfat;
XmlFsType.VolumeSerial = $"{fat32Bpb.serial_no:X8}";
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
_statfs.Id = new FileSystemId
{
IsInt = true,
Serial32 = fat32Bpb.serial_no
};
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
if((fat32Bpb.flags & 0xF8) == 0x00)
if((fat32Bpb.flags & 0x01) == 0x01)
XmlFsType.Dirty = true;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if((fat32Bpb.mirror_flags & 0x80) == 0x80)
_useFirstFat = (fat32Bpb.mirror_flags & 0xF) != 1;
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
if(fat32Bpb.signature == 0x29)
{
XmlFsType.VolumeName = StringHandlers.SpacePaddedToString(fat32Bpb.volume_label, Encoding);
XmlFsType.VolumeName = XmlFsType.VolumeName?.Replace("\0", "");
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
// Check that jumps to a correct boot code position and has boot signature set.
// This will mean that the volume will boot, even if just to say "this is not bootable change disk"......
XmlFsType.Bootable =
(fat32Bpb.jump[0] == 0xEB && fat32Bpb.jump[1] >= minBootNearJump && fat32Bpb.jump[1] < 0x80) ||
(fat32Bpb.jump[0] == 0xE9 && fat32Bpb.jump.Length >= 3 &&
BitConverter.ToUInt16(fat32Bpb.jump, 1) >= minBootNearJump &&
BitConverter.ToUInt16(fat32Bpb.jump, 1) <= 0x1FC);
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
sectorsPerRealSector = fat32Bpb.bps / imagePlugin.Info.SectorSize;
_sectorsPerCluster *= sectorsPerRealSector;
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
// First root directory sector
_firstClusterSector =
((ulong)((fat32Bpb.big_spfat * fat32Bpb.fats_no) + fat32Bpb.rsectors) * sectorsPerRealSector) -
(2 * _sectorsPerCluster);
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
if(fat32Bpb.fsinfo_sector + partition.Start <= partition.End)
{
2022-03-07 07:36:44 +00:00
errno = imagePlugin.ReadSector(fat32Bpb.fsinfo_sector + partition.Start, out byte[] fsinfoSector);
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
FsInfoSector fsInfo = Marshal.ByteArrayToStructureLittleEndian<FsInfoSector>(fsinfoSector);
2019-04-26 00:54:51 +01:00
if(fsInfo is { signature1: FSINFO_SIGNATURE1, signature2 : FSINFO_SIGNATURE2 }
and { signature3 : FSINFO_SIGNATURE3, free_clusters: < 0xFFFFFFFF })
{
XmlFsType.FreeClusters = fsInfo.free_clusters;
XmlFsType.FreeClustersSpecified = true;
}
2022-03-06 13:29:38 +00:00
}
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
break;
}
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
// Some fields could overflow fake BPB, those will be handled below
case BpbKind.Atari:
{
ushort sum = 0;
2019-04-26 00:54:51 +01:00
for(int i = 0; i < bpbSector.Length; i += 2)
2022-03-06 13:29:38 +00:00
sum += BigEndianBitConverter.ToUInt16(bpbSector, i);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
// TODO: Check this
if(sum == 0x1234)
XmlFsType.Bootable = true;
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
// BGM changes the bytes per sector instead of changing the sectors per cluster. Why?! WHY!?
uint ratio = fakeBpb.bps / imagePlugin.Info.SectorSize;
fakeBpb.bps = (ushort)imagePlugin.Info.SectorSize;
fakeBpb.spc = (byte)(fakeBpb.spc * ratio);
fakeBpb.rsectors = (ushort)(fakeBpb.rsectors * ratio);
fakeBpb.big_sectors = fakeBpb.sectors * ratio;
fakeBpb.sectors = 0;
fakeBpb.spfat = (ushort)(fakeBpb.spfat * ratio);
fakeBpb.sptrk = (ushort)(fakeBpb.sptrk * ratio);
break;
}
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
case BpbKind.Human:
// If not debug set Human68k namespace and ShiftJIS codepage as defaults
if(!_debug)
_namespace = Namespace.Human;
2022-03-06 13:29:38 +00:00
XmlFsType.Bootable = true;
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
break;
}
2022-03-06 13:29:38 +00:00
ulong firstRootSector = 0;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(!_fat32)
{
// This is to support FAT partitions on hybrid ISO/USB images
if(imagePlugin.Info.XmlMediaType == XmlMediaType.OpticalDisc)
{
fakeBpb.bps *= 4;
fakeBpb.spc /= 4;
fakeBpb.spfat /= 4;
fakeBpb.hsectors /= 4;
fakeBpb.sptrk /= 4;
fakeBpb.rsectors /= 4;
if(fakeBpb.spc == 0)
fakeBpb.spc = 1;
2019-04-26 00:54:51 +01:00
}
2022-03-06 13:29:38 +00:00
ulong clusters;
2022-03-06 13:29:38 +00:00
if(bpbKind != BpbKind.Human)
2019-04-26 00:54:51 +01:00
{
int reservedSectors = fakeBpb.rsectors + (fakeBpb.fats_no * fakeBpb.spfat) +
(fakeBpb.root_ent * 32 / fakeBpb.bps);
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
if(fakeBpb.sectors == 0)
clusters = (ulong)(fakeBpb.spc == 0 ? fakeBpb.big_sectors - reservedSectors
: (fakeBpb.big_sectors - reservedSectors) / fakeBpb.spc);
else
2022-03-06 13:29:38 +00:00
clusters = (ulong)(fakeBpb.spc == 0 ? fakeBpb.sectors - reservedSectors
: (fakeBpb.sectors - reservedSectors) / fakeBpb.spc);
}
else
clusters = humanBpb.clusters == 0 ? humanBpb.big_clusters : humanBpb.clusters;
2022-03-06 13:29:38 +00:00
// This will walk all the FAT entries and check if they're valid FAT12 or FAT16 entries.
// If the whole table is valid in both senses, it considers the type entry in the BPB.
// BeOS is known to set the type as FAT16 but treat it as FAT12.
if(!_fat12 &&
!_fat16)
{
if(clusters < 4089)
2019-04-26 00:54:51 +01:00
{
ushort[] fat12 = new ushort[clusters + 1];
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
_reservedSectors = fakeBpb.rsectors;
sectorsPerRealSector = fakeBpb.bps / imagePlugin.Info.SectorSize;
_fatFirstSector = partition.Start + (_reservedSectors * sectorsPerRealSector);
2022-03-06 13:29:38 +00:00
errno = imagePlugin.ReadSectors(_fatFirstSector, fakeBpb.spfat, out byte[] fatBytes);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
int pos = 0;
for(int i = 0; i + 3 < fatBytes.Length && pos < fat12.Length; i += 3)
2022-03-06 13:29:38 +00:00
{
fat12[pos++] = (ushort)(((fatBytes[i + 1] & 0xF) << 8) + fatBytes[i + 0]);
2022-03-06 13:29:38 +00:00
if(pos >= fat12.Length)
break;
2022-03-06 13:29:38 +00:00
fat12[pos++] = (ushort)(((fatBytes[i + 1] & 0xF0) >> 4) + (fatBytes[i + 2] << 4));
}
2022-03-06 13:29:38 +00:00
bool fat12Valid = fat12[0] >= FAT12_RESERVED && fat12[1] >= FAT12_RESERVED;
2022-11-15 01:35:06 +00:00
if(fat12.Any(entry => entry < FAT12_RESERVED && entry > clusters))
2022-03-06 13:29:38 +00:00
fat12Valid = false;
2022-03-06 13:29:38 +00:00
ushort[] fat16 = MemoryMarshal.Cast<byte, ushort>(fatBytes).ToArray();
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
bool fat16Valid = fat16[0] >= FAT16_RESERVED && fat16[1] >= 0x3FF0;
2022-11-15 01:35:06 +00:00
if(fat16.Any(entry => entry < FAT16_RESERVED && entry > clusters))
2022-03-06 13:29:38 +00:00
fat16Valid = false;
_fat12 = fat12Valid;
_fat16 = fat16Valid;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
// Check BPB type
if(_fat12 == _fat16 &&
fakeBpb.fs_type != null)
2019-04-26 00:54:51 +01:00
{
2022-03-06 13:29:38 +00:00
_fat12 = Encoding.ASCII.GetString(fakeBpb.fs_type) == "FAT12 ";
_fat16 = Encoding.ASCII.GetString(fakeBpb.fs_type) == "FAT16 ";
2019-04-26 00:54:51 +01:00
}
2022-03-06 13:29:38 +00:00
// Still undecided (fs_type is null or is not FAT1[2|6])
if(_fat12 == _fat16)
2019-04-26 23:06:05 +01:00
{
2022-03-06 13:29:38 +00:00
_fat12 = true;
_fat16 = false;
2019-04-26 23:06:05 +01:00
}
2019-04-26 00:54:51 +01:00
}
2022-03-06 13:29:38 +00:00
else
_fat16 = true;
}
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
if(_fat12)
XmlFsType.Type = FS_TYPE_FAT12;
2022-03-06 13:29:38 +00:00
else if(_fat16)
XmlFsType.Type = FS_TYPE_FAT16;
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
if(bpbKind == BpbKind.Atari)
{
if(atariBpb.serial_no[0] != 0x49 ||
atariBpb.serial_no[1] != 0x48 ||
atariBpb.serial_no[2] != 0x43)
2019-04-26 00:54:51 +01:00
{
2022-11-13 19:38:03 +00:00
XmlFsType.VolumeSerial = $"{atariBpb.serial_no[0]:X2}{atariBpb.serial_no[1]:X2}{
atariBpb.serial_no[2]:X2}";
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
_statfs.Id = new FileSystemId
{
2022-03-06 13:29:38 +00:00
IsInt = true,
Serial32 = (uint)((atariBpb.serial_no[0] << 16) + (atariBpb.serial_no[1] << 8) +
atariBpb.serial_no[2])
};
2019-04-26 00:54:51 +01:00
}
2022-03-06 13:29:38 +00:00
XmlFsType.SystemIdentifier = StringHandlers.CToString(atariBpb.oem_name);
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
if(string.IsNullOrEmpty(XmlFsType.SystemIdentifier))
XmlFsType.SystemIdentifier = null;
}
else if(fakeBpb.oem_name != null)
{
if(fakeBpb.oem_name[5] != 0x49 ||
fakeBpb.oem_name[6] != 0x48 ||
fakeBpb.oem_name[7] != 0x43)
2022-11-13 19:59:24 +00:00
XmlFsType.SystemIdentifier = fakeBpb.oem_name[0] switch
{
// Later versions of Windows create a DOS 3 BPB without OEM name on 8 sectors/track floppies
// OEM ID should be ASCII, otherwise ignore it
>= 0x20 and <= 0x7F when fakeBpb.oem_name[1] >= 0x20 && fakeBpb.oem_name[1] <= 0x7F &&
fakeBpb.oem_name[2] >= 0x20 && fakeBpb.oem_name[2] <= 0x7F &&
fakeBpb.oem_name[3] >= 0x20 && fakeBpb.oem_name[3] <= 0x7F &&
fakeBpb.oem_name[4] >= 0x20 && fakeBpb.oem_name[4] <= 0x7F &&
fakeBpb.oem_name[5] >= 0x20 && fakeBpb.oem_name[5] <= 0x7F &&
fakeBpb.oem_name[6] >= 0x20 && fakeBpb.oem_name[6] <= 0x7F &&
fakeBpb.oem_name[7] >= 0x20 &&
fakeBpb.oem_name[7] <= 0x7F =>
StringHandlers.CToString(fakeBpb.oem_name),
< 0x20 when fakeBpb.oem_name[1] >= 0x20 && fakeBpb.oem_name[1] <= 0x7F &&
fakeBpb.oem_name[2] >= 0x20 && fakeBpb.oem_name[2] <= 0x7F &&
fakeBpb.oem_name[3] >= 0x20 && fakeBpb.oem_name[3] <= 0x7F &&
fakeBpb.oem_name[4] >= 0x20 && fakeBpb.oem_name[4] <= 0x7F &&
fakeBpb.oem_name[5] >= 0x20 && fakeBpb.oem_name[5] <= 0x7F &&
fakeBpb.oem_name[6] >= 0x20 && fakeBpb.oem_name[6] <= 0x7F &&
fakeBpb.oem_name[7] >= 0x20 &&
fakeBpb.oem_name[7] <= 0x7F => StringHandlers.CToString(fakeBpb.oem_name, Encoding,
start: 1),
_ => XmlFsType.SystemIdentifier
};
2020-02-29 18:03:35 +00:00
2022-03-16 11:47:00 +00:00
if(fakeBpb.signature is 0x28 or 0x29)
2022-03-06 13:29:38 +00:00
{
XmlFsType.VolumeSerial = $"{fakeBpb.serial_no:X8}";
2022-03-06 13:29:38 +00:00
_statfs.Id = new FileSystemId
{
IsInt = true,
Serial32 = fakeBpb.serial_no
};
}
2019-04-26 00:54:51 +01:00
}
2022-03-06 13:29:38 +00:00
XmlFsType.Clusters = clusters;
_sectorsPerCluster = fakeBpb.spc;
XmlFsType.ClusterSize = (uint)(fakeBpb.bps * fakeBpb.spc);
_reservedSectors = fakeBpb.rsectors;
_sectorsPerFat = fakeBpb.spfat;
2019-04-26 00:54:51 +01:00
2022-03-16 11:47:00 +00:00
if(fakeBpb.signature is 0x28 or 0x29 || andosOemCorrect)
2022-03-06 13:29:38 +00:00
{
if((fakeBpb.flags & 0xF8) == 0x00)
if((fakeBpb.flags & 0x01) == 0x01)
XmlFsType.Dirty = true;
2019-04-26 19:57:19 +01:00
2022-03-06 13:29:38 +00:00
if(fakeBpb.signature == 0x29 || andosOemCorrect)
{
XmlFsType.VolumeName = StringHandlers.SpacePaddedToString(fakeBpb.volume_label, Encoding);
XmlFsType.VolumeName = XmlFsType.VolumeName?.Replace("\0", "");
}
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
// Workaround that PCExchange jumps into "FAT16 "...
if(XmlFsType.SystemIdentifier == "PCX 2.0 ")
fakeBpb.jump[1] += 8;
// Check that jumps to a correct boot code position and has boot signature set.
// This will mean that the volume will boot, even if just to say "this is not bootable change disk"......
if(XmlFsType.Bootable == false &&
fakeBpb.jump != null)
XmlFsType.Bootable |=
(fakeBpb.jump[0] == 0xEB && fakeBpb.jump[1] >= minBootNearJump && fakeBpb.jump[1] < 0x80) ||
(fakeBpb.jump[0] == 0xE9 && fakeBpb.jump.Length >= 3 &&
BitConverter.ToUInt16(fakeBpb.jump, 1) >= minBootNearJump &&
BitConverter.ToUInt16(fakeBpb.jump, 1) <= 0x1FC);
2022-03-06 13:29:38 +00:00
// First root directory sector
firstRootSector = ((ulong)((fakeBpb.spfat * fakeBpb.fats_no) + fakeBpb.rsectors) * sectorsPerRealSector) +
2022-03-07 07:36:44 +00:00
partition.Start;
2022-03-06 13:29:38 +00:00
sectorsForRootDirectory = (uint)(fakeBpb.root_ent * 32 / imagePlugin.Info.SectorSize);
sectorsPerRealSector = fakeBpb.bps / imagePlugin.Info.SectorSize;
_sectorsPerCluster *= sectorsPerRealSector;
}
2019-04-26 19:57:19 +01:00
2022-03-06 13:29:38 +00:00
_firstClusterSector += partition.Start;
2022-03-06 13:29:38 +00:00
_image = imagePlugin;
2022-03-06 13:29:38 +00:00
if(_fat32)
_fatEntriesPerSector = imagePlugin.Info.SectorSize / 4;
else if(_fat16)
_fatEntriesPerSector = imagePlugin.Info.SectorSize / 2;
else
_fatEntriesPerSector = imagePlugin.Info.SectorSize * 2 / 3;
2019-04-26 19:57:19 +01:00
_fatFirstSector = partition.Start + (_reservedSectors * sectorsPerRealSector);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
_rootDirectoryCache = new Dictionary<string, CompleteDirectoryEntry>();
byte[] rootDirectory;
2022-03-06 13:29:38 +00:00
if(!_fat32)
{
_firstClusterSector = firstRootSector + sectorsForRootDirectory - (_sectorsPerCluster * 2);
2022-03-06 13:29:38 +00:00
errno = imagePlugin.ReadSectors(firstRootSector, sectorsForRootDirectory, out rootDirectory);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
if(bpbKind == BpbKind.DecRainbow)
2019-04-26 19:57:19 +01:00
{
2022-03-06 13:29:38 +00:00
var rootMs = new MemoryStream();
2019-04-26 00:54:51 +01:00
2022-03-18 01:32:22 +00:00
foreach(ulong rootSector in new ulong[]
2022-03-06 13:29:38 +00:00
{
0x17, 0x19, 0x1B, 0x1D, 0x1E, 0x20
})
2019-04-26 19:57:19 +01:00
{
2022-03-06 13:29:38 +00:00
errno = imagePlugin.ReadSector(rootSector, out byte[] tmp);
if(errno != ErrorNumber.NoError)
return errno;
2022-03-06 13:29:38 +00:00
rootMs.Write(tmp, 0, tmp.Length);
2019-04-26 19:57:19 +01:00
}
2019-04-26 19:57:19 +01:00
rootDirectory = rootMs.ToArray();
}
2022-03-06 13:29:38 +00:00
}
else
{
if(rootDirectoryCluster == 0)
2021-09-16 04:42:14 +01:00
return ErrorNumber.InvalidArgument;
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
var rootMs = new MemoryStream();
uint[] rootDirectoryClusters = GetClusters(rootDirectoryCluster);
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
foreach(uint cluster in rootDirectoryClusters)
2019-04-26 19:57:19 +01:00
{
errno = imagePlugin.ReadSectors(_firstClusterSector + (cluster * _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;
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
rootMs.Write(buffer, 0, buffer.Length);
}
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
rootDirectory = rootMs.ToArray();
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
// OS/2 FAT32.IFS uses LFN instead of .LONGNAME
if(_namespace == Namespace.Os2)
_namespace = Namespace.Lfn;
}
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
if(rootDirectory is null)
return ErrorNumber.InvalidArgument;
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
byte[] lastLfnName = null;
byte lastLfnChecksum = 0;
2019-04-27 13:04:59 +01:00
for(int i = 0; i < rootDirectory.Length; i += Marshal.SizeOf<DirectoryEntry>())
2022-03-06 13:29:38 +00:00
{
DirectoryEntry entry =
Marshal.ByteArrayToStructureLittleEndian<DirectoryEntry>(rootDirectory, i,
Marshal.SizeOf<DirectoryEntry>());
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(entry.filename[0] == DIRENT_FINISHED)
break;
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
if(entry.attributes.HasFlag(FatAttributes.LFN))
{
if(_namespace != Namespace.Lfn &&
_namespace != Namespace.Ecs)
continue;
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
LfnEntry lfnEntry =
2022-03-07 07:36:44 +00:00
Marshal.ByteArrayToStructureLittleEndian<LfnEntry>(rootDirectory, i, Marshal.SizeOf<LfnEntry>());
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
int lfnSequence = lfnEntry.sequence & LFN_MASK;
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
if((lfnEntry.sequence & LFN_ERASED) > 0)
2020-02-29 18:03:35 +00:00
continue;
2022-03-06 13:29:38 +00:00
if((lfnEntry.sequence & LFN_LAST) > 0)
{
lastLfnName = new byte[lfnSequence * 26];
lastLfnChecksum = lfnEntry.checksum;
}
2022-03-06 13:29:38 +00:00
if(lastLfnName is null)
2020-02-29 18:03:35 +00:00
continue;
2022-03-06 13:29:38 +00:00
if(lfnEntry.checksum != lastLfnChecksum)
2020-02-29 18:03:35 +00:00
continue;
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
lfnSequence--;
2019-04-26 19:57:19 +01:00
2022-03-06 13:29:38 +00:00
Array.Copy(lfnEntry.name1, 0, lastLfnName, lfnSequence * 26, 10);
Array.Copy(lfnEntry.name2, 0, lastLfnName, (lfnSequence * 26) + 10, 12);
Array.Copy(lfnEntry.name3, 0, lastLfnName, (lfnSequence * 26) + 22, 4);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
continue;
}
2019-04-26 19:57:19 +01:00
2022-03-06 13:29:38 +00:00
// Not a correct entry
if(entry.filename[0] < DIRENT_MIN &&
entry.filename[0] != DIRENT_E5)
continue;
2022-03-06 13:29:38 +00:00
// Self
if(Encoding.GetString(entry.filename).TrimEnd() == ".")
continue;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
// Parent
if(Encoding.GetString(entry.filename).TrimEnd() == "..")
continue;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
// Deleted
if(entry.filename[0] == DIRENT_DELETED)
continue;
2019-04-26 19:57:19 +01:00
2022-03-06 13:29:38 +00:00
string filename;
2022-03-06 13:29:38 +00:00
if(entry.attributes.HasFlag(FatAttributes.VolumeLabel))
{
byte[] fullname = new byte[11];
2022-03-06 13:29:38 +00:00
Array.Copy(entry.filename, 0, fullname, 0, 8);
Array.Copy(entry.extension, 0, fullname, 8, 3);
string volname = Encoding.GetString(fullname).Trim();
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
if(!string.IsNullOrEmpty(volname))
2022-03-07 07:36:44 +00:00
XmlFsType.VolumeName = entry.caseinfo.HasFlag(CaseInfo.AllLowerCase) && _namespace == Namespace.Nt
? volname.ToLower() : volname;
2022-03-06 13:29:38 +00:00
XmlFsType.VolumeName = XmlFsType.VolumeName?.Replace("\0", "");
2019-04-28 11:57:54 +01:00
if(entry is { ctime: > 0, cdate: > 0 })
2019-04-27 13:04:59 +01:00
{
2022-03-06 13:29:38 +00:00
XmlFsType.CreationDate = DateHandlers.DosToDateTime(entry.cdate, entry.ctime);
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
if(entry.ctime_ms > 0)
XmlFsType.CreationDate = XmlFsType.CreationDate.AddMilliseconds(entry.ctime_ms * 10);
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
XmlFsType.CreationDateSpecified = true;
2019-04-27 13:04:59 +01:00
}
if(entry is { mtime: > 0, mdate: > 0 })
{
2022-03-06 13:29:38 +00:00
XmlFsType.ModificationDate = DateHandlers.DosToDateTime(entry.mdate, entry.mtime);
XmlFsType.ModificationDateSpecified = true;
}
2022-03-06 13:29:38 +00:00
continue;
}
2022-03-06 13:29:38 +00:00
var completeEntry = new CompleteDirectoryEntry
{
Dirent = entry
};
2022-03-16 11:47:00 +00:00
if(_namespace is Namespace.Lfn or Namespace.Ecs &&
2022-03-06 13:29:38 +00:00
lastLfnName != null)
{
byte calculatedLfnChecksum = LfnChecksum(entry.filename, entry.extension);
2022-03-06 13:29:38 +00:00
if(calculatedLfnChecksum == lastLfnChecksum)
{
filename = StringHandlers.CToString(lastLfnName, Encoding.Unicode, true);
2022-03-06 13:29:38 +00:00
completeEntry.Lfn = filename;
lastLfnName = null;
lastLfnChecksum = 0;
}
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(entry.filename[0] == DIRENT_E5)
entry.filename[0] = DIRENT_DELETED;
2022-03-06 13:29:38 +00:00
string name = Encoding.GetString(entry.filename).TrimEnd();
string extension = Encoding.GetString(entry.extension).TrimEnd();
2019-04-28 11:57:54 +01:00
2022-03-06 13:29:38 +00:00
if(_namespace == Namespace.Nt)
{
if(entry.caseinfo.HasFlag(CaseInfo.LowerCaseExtension))
extension = extension.ToLower(CultureInfo.CurrentCulture);
if(entry.caseinfo.HasFlag(CaseInfo.LowerCaseBasename))
name = name.ToLower(CultureInfo.CurrentCulture);
}
2022-03-06 13:29:38 +00:00
if(extension != "")
filename = name + "." + extension;
else
filename = name;
2022-03-06 13:29:38 +00:00
if(name == "" &&
extension == "")
{
AaruConsole.DebugWriteLine("FAT filesystem", Localization.Found_empty_filename_in_root_directory);
2022-03-06 13:29:38 +00:00
if(!_debug ||
entry is { size: > 0, start_cluster: 0 })
2022-03-06 13:29:38 +00:00
continue; // Skip invalid name
2022-03-06 13:29:38 +00:00
// If debug, add it
name = ":{EMPTYNAME}:";
2022-03-06 13:29:38 +00:00
// Try to create a unique filename with an extension from 000 to 999
for(int uniq = 0; uniq < 1000; uniq++)
2019-04-27 16:04:07 +01:00
{
2022-03-06 13:29:38 +00:00
extension = $"{uniq:D03}";
2019-04-27 16:04:07 +01:00
2022-03-06 13:29:38 +00:00
if(!_rootDirectoryCache.ContainsKey($"{name}.{extension}"))
break;
2019-04-27 16:04:07 +01:00
}
2022-03-06 13:29:38 +00:00
// If we couldn't find it, just skip over
if(_rootDirectoryCache.ContainsKey($"{name}.{extension}"))
continue;
2019-04-26 19:57:19 +01:00
}
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
// Atari ST allows slash AND colon so cannot simply substitute one for the other like in Mac filesystems
filename = filename.Replace('/', '\u2215');
2019-04-26 23:06:05 +01:00
2022-03-06 13:29:38 +00:00
completeEntry.Shortname = filename;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(_namespace == Namespace.Human)
{
HumanDirectoryEntry humanEntry =
Marshal.ByteArrayToStructureLittleEndian<HumanDirectoryEntry>(rootDirectory, i,
Marshal.SizeOf<HumanDirectoryEntry>());
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
completeEntry.HumanDirent = humanEntry;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
name = StringHandlers.CToString(humanEntry.name1, Encoding).TrimEnd();
extension = StringHandlers.CToString(humanEntry.extension, Encoding).TrimEnd();
string name2 = StringHandlers.CToString(humanEntry.name2, Encoding).TrimEnd();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(extension != "")
filename = name + name2 + "." + extension;
else
filename = name + name2;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
completeEntry.HumanName = filename;
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(!_fat32 &&
filename == "EA DATA. SF")
{
_eaDirEntry = entry;
lastLfnName = null;
lastLfnChecksum = 0;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(_debug)
_rootDirectoryCache[completeEntry.ToString()] = completeEntry;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
continue;
2019-04-26 23:06:05 +01:00
}
2022-03-06 13:29:38 +00:00
_rootDirectoryCache[completeEntry.ToString()] = completeEntry;
lastLfnName = null;
lastLfnChecksum = 0;
}
2022-03-06 13:29:38 +00:00
XmlFsType.VolumeName = XmlFsType.VolumeName?.Trim();
_statfs.Blocks = XmlFsType.Clusters;
2022-03-06 13:29:38 +00:00
switch(bpbKind)
{
case BpbKind.ShortFat32:
case BpbKind.LongFat32:
_statfs.Type = XmlFsType.Type == FS_TYPE_FAT_PLUS ? FS_TYPE_FAT_PLUS : FS_TYPE_FAT32;
2022-03-06 13:29:38 +00:00
break;
default:
_statfs.Type = _fat16 ? FS_TYPE_FAT16 : FS_TYPE_FAT12;
2022-03-06 13:29:38 +00:00
break;
}
2019-04-27 02:26:31 +01:00
2022-03-06 13:29:38 +00:00
_bytesPerCluster = _sectorsPerCluster * imagePlugin.Info.SectorSize;
// The first 2 FAT entries do not count as allocation clusters in FAT12 and FAT16
ushort[] firstFatEntries = new ushort[_statfs.Blocks + 2];
ushort[] secondFatEntries = new ushort[_statfs.Blocks + 2];
bool firstFatValid = true;
bool secondFatValid = true;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(_fat12)
{
AaruConsole.DebugWriteLine("FAT plugin", Localization.Reading_FAT12);
2022-03-06 13:29:38 +00:00
errno = imagePlugin.ReadSectors(_fatFirstSector, _sectorsPerFat, out byte[] fatBytes);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
int pos = 0;
for(int i = 0; i + 3 < fatBytes.Length && pos < firstFatEntries.Length; i += 3)
2022-03-06 13:29:38 +00:00
{
firstFatEntries[pos++] = (ushort)(((fatBytes[i + 1] & 0xF) << 8) + fatBytes[i + 0]);
2022-03-06 13:29:38 +00:00
if(pos >= firstFatEntries.Length)
break;
2022-03-06 13:29:38 +00:00
firstFatEntries[pos++] = (ushort)(((fatBytes[i + 1] & 0xF0) >> 4) + (fatBytes[i + 2] << 4));
}
2022-03-06 13:29:38 +00:00
errno = imagePlugin.ReadSectors(_fatFirstSector + _sectorsPerFat, _sectorsPerFat, out fatBytes);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
_fatEntries = new ushort[_statfs.Blocks + 2];
2022-03-06 13:29:38 +00:00
pos = 0;
for(int i = 0; i + 3 < fatBytes.Length && pos < secondFatEntries.Length; i += 3)
2022-03-06 13:29:38 +00:00
{
secondFatEntries[pos++] = (ushort)(((fatBytes[i + 1] & 0xF) << 8) + fatBytes[i + 0]);
2022-03-06 13:29:38 +00:00
if(pos >= secondFatEntries.Length)
break;
2022-03-06 13:29:38 +00:00
secondFatEntries[pos++] = (ushort)(((fatBytes[i + 1] & 0xF0) >> 4) + (fatBytes[i + 2] << 4));
}
if(firstFatEntries.Any(entry => entry < FAT12_RESERVED && entry > _statfs.Blocks + 2))
2022-03-06 13:29:38 +00:00
firstFatValid = false;
if(secondFatEntries.Any(entry => entry < FAT12_RESERVED && entry > _statfs.Blocks + 2))
2022-03-06 13:29:38 +00:00
secondFatValid = false;
2022-03-06 13:29:38 +00:00
if(firstFatValid == secondFatValid)
_fatEntries = _useFirstFat ? firstFatEntries : secondFatEntries;
else if(firstFatValid)
_fatEntries = firstFatEntries;
else
_fatEntries = secondFatEntries;
}
else if(_fat16)
{
AaruConsole.DebugWriteLine("FAT plugin", Localization.Reading_FAT16);
2022-03-06 13:29:38 +00:00
errno = imagePlugin.ReadSectors(_fatFirstSector, _sectorsPerFat, out byte[] fatBytes);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
2019-04-27 02:38:48 +01:00
AaruConsole.DebugWriteLine("FAT plugin", Localization.Casting_FAT);
2022-03-06 13:29:38 +00:00
firstFatEntries = MemoryMarshal.Cast<byte, ushort>(fatBytes).ToArray();
2022-03-06 13:29:38 +00:00
errno = imagePlugin.ReadSectors(_fatFirstSector + _sectorsPerFat, _sectorsPerFat, out fatBytes);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
AaruConsole.DebugWriteLine("FAT plugin", Localization.Casting_FAT);
2022-03-06 13:29:38 +00:00
secondFatEntries = MemoryMarshal.Cast<byte, ushort>(fatBytes).ToArray();
if(firstFatEntries.Any(entry => entry < FAT16_RESERVED && entry > _statfs.Blocks + 2))
2022-03-06 13:29:38 +00:00
firstFatValid = false;
if(secondFatEntries.Any(entry => entry < FAT16_RESERVED && entry > _statfs.Blocks + 2))
2022-03-06 13:29:38 +00:00
secondFatValid = false;
if(firstFatValid == secondFatValid)
_fatEntries = _useFirstFat ? firstFatEntries : secondFatEntries;
else if(firstFatValid)
_fatEntries = firstFatEntries;
else
_fatEntries = secondFatEntries;
}
2022-03-06 13:29:38 +00:00
// TODO: Check how this affects international filenames
_cultureInfo = new CultureInfo("en-US", false);
_directoryCache = new Dictionary<string, Dictionary<string, CompleteDirectoryEntry>>();
2019-04-27 16:44:43 +01:00
2022-03-06 13:29:38 +00:00
// Check it is really an OS/2 EA file
if(_eaDirEntry.start_cluster != 0)
{
CacheEaData();
ushort eamagic = BitConverter.ToUInt16(_cachedEaData, 0);
2022-03-06 13:29:38 +00:00
if(eamagic != EADATA_MAGIC)
{
_eaDirEntry = new DirectoryEntry();
_cachedEaData = null;
2019-04-27 16:44:43 +01:00
}
2022-03-06 13:29:38 +00:00
else
2020-07-20 21:11:32 +01:00
_eaCache = new Dictionary<string, Dictionary<string, byte[]>>();
2022-03-06 13:29:38 +00:00
}
else if(_fat32)
_eaCache = new Dictionary<string, Dictionary<string, byte[]>>();
2019-04-27 16:44:43 +01:00
2022-03-06 13:29:38 +00:00
// Check OS/2 .LONGNAME
2022-03-16 11:47:00 +00:00
if(_eaCache != null &&
_namespace is Namespace.Os2 or Namespace.Ecs &&
2022-03-06 13:29:38 +00:00
!_fat32)
{
List<KeyValuePair<string, CompleteDirectoryEntry>> rootFilesWithEas =
_rootDirectoryCache.Where(t => t.Value.Dirent.ea_handle != 0).ToList();
2022-03-06 13:29:38 +00:00
foreach(KeyValuePair<string, CompleteDirectoryEntry> fileWithEa in rootFilesWithEas)
{
Dictionary<string, byte[]> eas = GetEas(fileWithEa.Value.Dirent.ea_handle);
2022-03-06 13:29:38 +00:00
if(eas is null)
continue;
2022-03-06 13:29:38 +00:00
if(!eas.TryGetValue("com.microsoft.os2.longname", out byte[] longnameEa))
continue;
2022-03-06 13:29:38 +00:00
if(BitConverter.ToUInt16(longnameEa, 0) != EAT_ASCII)
continue;
ushort longnameSize = BitConverter.ToUInt16(longnameEa, 2);
2022-03-06 13:29:38 +00:00
if(longnameSize + 4 > longnameEa.Length)
continue;
byte[] longnameBytes = new byte[longnameSize];
2022-03-06 13:29:38 +00:00
Array.Copy(longnameEa, 4, longnameBytes, 0, longnameSize);
2022-03-06 13:29:38 +00:00
string longname = StringHandlers.CToString(longnameBytes, Encoding);
2022-03-06 13:29:38 +00:00
if(string.IsNullOrWhiteSpace(longname))
continue;
2022-03-06 13:29:38 +00:00
// Forward slash is allowed in .LONGNAME, so change it to visually similar division slash
longname = longname.Replace('/', '\u2215');
2022-03-06 13:29:38 +00:00
fileWithEa.Value.Longname = longname;
_rootDirectoryCache.Remove(fileWithEa.Key);
_rootDirectoryCache[fileWithEa.Value.ToString()] = fileWithEa.Value;
2019-04-28 11:57:54 +01:00
}
2022-03-06 13:29:38 +00:00
}
2019-04-28 11:57:54 +01:00
2022-03-06 13:29:38 +00:00
// Check FAT32.IFS EAs
if(_fat32 || _debug)
{
List<KeyValuePair<string, CompleteDirectoryEntry>> fat32EaSidecars =
_rootDirectoryCache.Where(t => t.Key.EndsWith(FAT32_EA_TAIL, true, _cultureInfo)).ToList();
2022-03-06 13:29:38 +00:00
foreach(KeyValuePair<string, CompleteDirectoryEntry> sidecar in fat32EaSidecars)
2019-04-28 11:57:54 +01:00
{
2022-03-06 13:29:38 +00:00
// No real file this sidecar accompanies
2022-11-14 01:15:06 +00:00
if(!_rootDirectoryCache.TryGetValue(sidecar.Key[..^FAT32_EA_TAIL.Length],
2022-03-07 07:36:44 +00:00
out CompleteDirectoryEntry fileWithEa))
2022-03-06 13:29:38 +00:00
continue;
2019-04-28 11:57:54 +01:00
2022-03-06 13:29:38 +00:00
// If not in debug mode we will consider the lack of EA bitflags to mean the EAs are corrupted or not real
if(!_debug)
if(!fileWithEa.Dirent.caseinfo.HasFlag(CaseInfo.NormalEaOld) &&
!fileWithEa.Dirent.caseinfo.HasFlag(CaseInfo.CriticalEa) &&
!fileWithEa.Dirent.caseinfo.HasFlag(CaseInfo.NormalEa) &&
!fileWithEa.Dirent.caseinfo.HasFlag(CaseInfo.CriticalEa))
2020-02-29 18:03:35 +00:00
continue;
2019-04-28 11:57:54 +01:00
2022-03-06 13:29:38 +00:00
fileWithEa.Fat32Ea = sidecar.Value.Dirent;
2019-04-28 11:57:54 +01:00
2022-03-06 13:29:38 +00:00
if(!_debug)
_rootDirectoryCache.Remove(sidecar.Key);
}
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
_mounted = true;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(string.IsNullOrWhiteSpace(XmlFsType.VolumeName))
XmlFsType.VolumeName = null;
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Unmount()
{
if(!_mounted)
return ErrorNumber.AccessDenied;
2019-04-26 00:54:51 +01:00
2022-03-06 13:29:38 +00:00
_mounted = false;
_fatEntries = null;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber StatFs(out FileSystemInfo stat)
{
stat = null;
2020-02-29 18:03:35 +00:00
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
stat = _statfs.ShallowCopy();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
}