Files
Aaru/Aaru.Filesystems/FAT/Dir.cs

396 lines
15 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Dir.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Microsoft FAT filesystem plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// Methods to handle Microsoft FAT filesystem 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/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
namespace Aaru.Filesystems;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
2019-04-27 13:04:59 +01:00
using System.Text;
2021-09-16 04:42:14 +01:00
using Aaru.CommonTypes.Enums;
using Aaru.Console;
2020-02-27 00:33:26 +00:00
using Aaru.Helpers;
2022-03-06 13:29:38 +00:00
public sealed partial class FAT
{
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
/// <summary>Solves a symbolic link.</summary>
/// <param name="path">Link path.</param>
/// <param name="dest">Link destination.</param>
public ErrorNumber ReadLink(string path, out string dest)
{
2022-03-06 13:29:38 +00:00
dest = null;
return ErrorNumber.NotSupported;
}
/// <inheritdoc />
/// <summary>Lists contents from a directory.</summary>
/// <param name="path">Directory path.</param>
/// <param name="contents">Directory contents.</param>
public ErrorNumber ReadDir(string path, out List<string> contents)
{
contents = null;
if(!_mounted)
return ErrorNumber.AccessDenied;
if(string.IsNullOrWhiteSpace(path) ||
path == "/")
{
2022-03-06 13:29:38 +00:00
contents = _rootDirectoryCache.Keys.ToList();
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
string cutPath = path.StartsWith("/", StringComparison.Ordinal) ? path.Substring(1).ToLower(_cultureInfo)
: path.ToLower(_cultureInfo);
if(_directoryCache.TryGetValue(cutPath, out Dictionary<string, CompleteDirectoryEntry> currentDirectory))
2019-04-26 00:54:51 +01:00
{
2022-03-06 13:29:38 +00:00
contents = currentDirectory.Keys.ToList();
2019-04-26 00:54:51 +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
string[] pieces = cutPath.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
KeyValuePair<string, CompleteDirectoryEntry> entry =
_rootDirectoryCache.FirstOrDefault(t => t.Key.ToLower(_cultureInfo) == pieces[0]);
2022-03-06 13:29:38 +00:00
if(string.IsNullOrEmpty(entry.Key))
return ErrorNumber.NoSuchFile;
2022-03-06 13:29:38 +00:00
if(!entry.Value.Dirent.attributes.HasFlag(FatAttributes.Subdirectory))
return ErrorNumber.NotDirectory;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
string currentPath = pieces[0];
2022-03-06 13:29:38 +00:00
currentDirectory = _rootDirectoryCache;
2022-03-07 07:36:44 +00:00
for(var p = 0; p < pieces.Length; p++)
2022-03-06 13:29:38 +00: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.Dirent.attributes.HasFlag(FatAttributes.Subdirectory))
2021-09-16 04:42:14 +01:00
return ErrorNumber.NotDirectory;
2022-03-06 13:29:38 +00:00
currentPath = p == 0 ? pieces[0] : $"{currentPath}/{pieces[p]}";
uint currentCluster = entry.Value.Dirent.start_cluster;
if(_fat32)
currentCluster += (uint)(entry.Value.Dirent.ea_handle << 16);
2022-03-06 13:29:38 +00:00
if(_directoryCache.TryGetValue(currentPath, out currentDirectory))
continue;
2022-03-06 13:29:38 +00:00
// Reserved unallocated directory, seen in Atari ST
if(currentCluster == 0)
{
2022-03-06 13:29:38 +00:00
_directoryCache[currentPath] = new Dictionary<string, CompleteDirectoryEntry>();
contents = new List<string>();
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
uint[] clusters = GetClusters(currentCluster);
2022-03-06 13:29:38 +00:00
if(clusters is null)
return ErrorNumber.InvalidArgument;
2022-03-07 07:36:44 +00:00
var directoryBuffer = new byte[_bytesPerCluster * clusters.Length];
2022-03-07 07:36:44 +00:00
for(var i = 0; i < clusters.Length; i++)
2022-03-06 13:29:38 +00:00
{
2022-11-13 19:16:14 +00:00
ErrorNumber errno = _image.ReadSectors(_firstClusterSector + clusters[i] * _sectorsPerCluster, _sectorsPerCluster,
out byte[] buffer);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
2022-03-06 13:29:38 +00:00
Array.Copy(buffer, 0, directoryBuffer, i * _bytesPerCluster, _bytesPerCluster);
}
2022-03-06 13:29:38 +00:00
currentDirectory = new Dictionary<string, CompleteDirectoryEntry>();
byte[] lastLfnName = null;
byte lastLfnChecksum = 0;
2022-03-07 07:36:44 +00:00
for(var pos = 0; pos < directoryBuffer.Length; pos += Marshal.SizeOf<DirectoryEntry>())
2022-03-06 13:29:38 +00:00
{
DirectoryEntry dirent =
Marshal.ByteArrayToStructureLittleEndian<DirectoryEntry>(directoryBuffer, pos,
Marshal.SizeOf<DirectoryEntry>());
2022-03-06 13:29:38 +00:00
if(dirent.filename[0] == DIRENT_FINISHED)
break;
2022-03-06 13:29:38 +00:00
if(dirent.attributes.HasFlag(FatAttributes.LFN))
{
2022-03-06 13:29:38 +00:00
if(_namespace != Namespace.Lfn &&
_namespace != Namespace.Ecs)
continue;
2022-03-06 13:29:38 +00:00
LfnEntry lfnEntry =
Marshal.ByteArrayToStructureLittleEndian<LfnEntry>(directoryBuffer, pos,
Marshal.SizeOf<LfnEntry>());
2022-03-06 13:29:38 +00:00
int lfnSequence = lfnEntry.sequence & LFN_MASK;
2022-03-06 13:29:38 +00:00
if((lfnEntry.sequence & LFN_ERASED) > 0)
continue;
2022-03-06 13:29:38 +00:00
if((lfnEntry.sequence & LFN_LAST) > 0)
2019-04-27 13:04:59 +01:00
{
2022-03-06 13:29:38 +00:00
lastLfnName = new byte[lfnSequence * 26];
lastLfnChecksum = lfnEntry.checksum;
}
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
if(lastLfnName is null)
continue;
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
if(lfnEntry.checksum != lastLfnChecksum)
continue;
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
lfnSequence--;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
Array.Copy(lfnEntry.name1, 0, lastLfnName, lfnSequence * 26, 10);
2022-03-07 07:36:44 +00:00
Array.Copy(lfnEntry.name2, 0, lastLfnName, lfnSequence * 26 + 10, 12);
Array.Copy(lfnEntry.name3, 0, lastLfnName, lfnSequence * 26 + 22, 4);
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
continue;
}
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
// Not a correct entry
if(dirent.filename[0] < DIRENT_MIN &&
dirent.filename[0] != DIRENT_E5)
continue;
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
// Self
if(Encoding.GetString(dirent.filename).TrimEnd() == ".")
continue;
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
// Parent
if(Encoding.GetString(dirent.filename).TrimEnd() == "..")
continue;
2022-03-06 13:29:38 +00:00
// Deleted
if(dirent.filename[0] == DIRENT_DELETED)
continue;
2022-03-06 13:29:38 +00:00
string filename;
2022-03-06 13:29:38 +00:00
if(dirent.attributes.HasFlag(FatAttributes.VolumeLabel))
continue;
2022-03-06 13:29:38 +00:00
var completeEntry = new CompleteDirectoryEntry
{
Dirent = dirent
};
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(dirent.filename, dirent.extension);
2022-03-06 13:29:38 +00:00
if(calculatedLfnChecksum == lastLfnChecksum)
2020-02-29 18:03:35 +00:00
{
2022-03-06 13:29:38 +00:00
filename = StringHandlers.CToString(lastLfnName, Encoding.Unicode, true);
2019-04-28 11:57:54 +01:00
2022-03-06 13:29:38 +00:00
completeEntry.Lfn = filename;
lastLfnName = null;
lastLfnChecksum = 0;
}
}
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
if(dirent.filename[0] == DIRENT_E5)
dirent.filename[0] = DIRENT_DELETED;
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
string name = Encoding.GetString(dirent.filename).TrimEnd();
string extension = Encoding.GetString(dirent.extension).TrimEnd();
if(name == "" &&
extension == "")
{
AaruConsole.DebugWriteLine("FAT filesystem", "Found empty filename in {0}", path);
2019-04-27 13:04:59 +01:00
2022-03-06 13:29:38 +00:00
if(!_debug ||
2022-03-07 07:36:44 +00:00
dirent.size > 0 && dirent.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
2022-03-07 07:36:44 +00:00
for(var uniq = 0; uniq < 1000; uniq++)
{
2022-03-06 13:29:38 +00:00
extension = $"{uniq:D03}";
2022-03-06 13:29:38 +00:00
if(!currentDirectory.ContainsKey($"{name}.{extension}"))
break;
}
// If we couldn't find it, just skip over
if(currentDirectory.ContainsKey($"{name}.{extension}"))
continue;
}
2022-03-06 13:29:38 +00:00
if(_namespace == Namespace.Nt)
{
if(dirent.caseinfo.HasFlag(CaseInfo.LowerCaseExtension))
extension = extension.ToLower(CultureInfo.CurrentCulture);
2022-03-06 13:29:38 +00:00
if(dirent.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(_namespace == Namespace.Human)
{
HumanDirectoryEntry humanEntry =
Marshal.ByteArrayToStructureLittleEndian<HumanDirectoryEntry>(directoryBuffer, pos,
Marshal.SizeOf<HumanDirectoryEntry>());
2022-03-06 13:29:38 +00:00
completeEntry.HumanDirent = humanEntry;
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
if(extension != "")
2022-03-06 13:29:38 +00:00
filename = name + name2 + "." + extension;
2020-02-29 18:03:35 +00:00
else
2022-03-06 13:29:38 +00:00
filename = name + name2;
2022-03-06 13:29:38 +00:00
completeEntry.HumanName = filename;
}
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');
2022-03-06 13:29:38 +00:00
// Using array accessor ensures that repeated entries just get substituted.
// Repeated entries are not allowed but some bad implementations (e.g. FAT32.IFS)allow to create them
// when using spaces
completeEntry.Shortname = filename;
currentDirectory[completeEntry.ToString()] = completeEntry;
}
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)
{
2022-03-07 07:36:44 +00:00
var filesWithEas = currentDirectory.Where(t => t.Value.Dirent.ea_handle != 0).ToList();
2022-03-06 13:29:38 +00:00
foreach(KeyValuePair<string, CompleteDirectoryEntry> fileWithEa in filesWithEas)
{
2022-03-06 13:29:38 +00:00
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;
2022-03-07 07:36:44 +00:00
var longnameSize = BitConverter.ToUInt16(longnameEa, 2);
2022-03-06 13:29:38 +00:00
if(longnameSize + 4 > longnameEa.Length)
continue;
2022-03-07 07:36:44 +00:00
var 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;
currentDirectory.Remove(fileWithEa.Key);
currentDirectory[fileWithEa.Value.ToString()] = fileWithEa.Value;
2019-04-28 11:57:54 +01:00
}
2022-03-06 13:29:38 +00:00
}
// Check FAT32.IFS EAs
if(_fat32 || _debug)
{
2022-03-07 07:36:44 +00:00
var fat32EaSidecars = currentDirectory.Where(t => t.Key.EndsWith(FAT32_EA_TAIL, true, _cultureInfo)).
ToList();
2019-04-28 11:57:54 +01:00
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
if(!currentDirectory.
TryGetValue(sidecar.Key.Substring(0, sidecar.Key.Length - FAT32_EA_TAIL.Length),
out CompleteDirectoryEntry fileWithEa))
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)
currentDirectory.Remove(sidecar.Key);
}
}
2022-03-06 13:29:38 +00:00
_directoryCache.Add(currentPath, currentDirectory);
2019-04-26 00:54:51 +01:00
}
2022-03-06 13:29:38 +00:00
contents = currentDirectory?.Keys.ToList();
return ErrorNumber.NoError;
}
}