Files
Aaru/Aaru.Filesystems/AppleDOS/Dir.cs

170 lines
5.4 KiB
C#
Raw Normal View History

2017-05-19 20:28:49 +01:00
// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Dir.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Apple DOS filesystem plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// Methods to handle Apple DOS filesystem catalog (aka directory).
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2024-05-01 04:17:32 +01:00
// Copyright © 2011-2024 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2021-09-16 04:42:14 +01:00
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
2020-02-27 00:33:26 +00:00
using Aaru.Helpers;
namespace Aaru.Filesystems;
2022-03-06 13:29:38 +00:00
public sealed partial class AppleDOS
{
#region IReadOnlyFilesystem Members
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadLink(string path, out string dest)
{
2022-03-06 13:29:38 +00:00
dest = null;
2022-03-06 13:29:38 +00:00
return !_mounted ? ErrorNumber.AccessDenied : ErrorNumber.NotSupported;
}
2020-02-29 18:03:35 +00:00
/// <inheritdoc />
public ErrorNumber OpenDir(string path, out IDirNode node)
{
node = null;
2024-05-01 04:05:22 +01:00
if(!_mounted) return ErrorNumber.AccessDenied;
if(!string.IsNullOrEmpty(path) && string.Compare(path, "/", StringComparison.OrdinalIgnoreCase) != 0)
return ErrorNumber.NotSupported;
var contents = _catalogCache.Keys.ToList();
if(_debug)
{
contents.Add("$");
contents.Add("$Boot");
contents.Add("$Vtoc");
}
contents.Sort();
node = new AppleDosDirNode
{
2023-10-05 01:52:48 +01:00
Path = path,
Position = 0,
Contents = contents.ToArray()
};
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadDir(IDirNode node, out string filename)
2022-03-06 13:29:38 +00:00
{
filename = null;
2024-05-01 04:05:22 +01:00
if(!_mounted) return ErrorNumber.AccessDenied;
2024-05-01 04:05:22 +01:00
if(node is not AppleDosDirNode mynode) return ErrorNumber.InvalidArgument;
2024-05-01 04:05:22 +01:00
if(mynode.Position < 0) return ErrorNumber.InvalidArgument;
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
if(mynode.Position >= mynode.Contents.Length) return ErrorNumber.NoError;
2023-10-05 01:52:48 +01:00
filename = mynode.Contents[mynode.Position++];
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
/// <inheritdoc />
public ErrorNumber CloseDir(IDirNode node)
{
2024-05-01 04:05:22 +01:00
if(node is not AppleDosDirNode mynode) return ErrorNumber.InvalidArgument;
2023-10-05 01:52:48 +01:00
mynode.Position = -1;
mynode.Contents = null;
return ErrorNumber.NoError;
}
#endregion
2022-03-06 13:29:38 +00:00
ErrorNumber ReadCatalog()
{
var catalogMs = new MemoryStream();
var lba = (ulong)(_vtoc.catalogTrack * _sectorsPerTrack + _vtoc.catalogSector);
2022-03-06 13:29:38 +00:00
_totalFileEntries = 0;
_catalogCache = new Dictionary<string, ushort>();
_fileTypeCache = new Dictionary<string, byte>();
2024-05-01 04:39:38 +01:00
_lockedFiles = [];
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(lba == 0 || lba > _device.Info.Sectors) return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
while(lba != 0)
{
_usedSectors++;
ErrorNumber errno = _device.ReadSector(lba, out byte[] catSectorB);
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return errno;
2022-03-06 13:29:38 +00:00
_totalFileEntries += 7;
2024-05-01 04:05:22 +01:00
if(_debug) catalogMs.Write(catSectorB, 0, catSectorB.Length);
2022-03-06 13:29:38 +00:00
// Read the catalog sector
CatalogSector catSector = Marshal.ByteArrayToStructureLittleEndian<CatalogSector>(catSectorB);
2022-03-06 13:29:38 +00:00
foreach(FileEntry entry in catSector.entries.Where(entry => entry.extentTrack > 0))
{
_track1UsedByFiles |= entry.extentTrack == 1;
_track2UsedByFiles |= entry.extentTrack == 2;
var filenameB = new byte[30];
var ts = (ushort)(entry.extentTrack << 8 | entry.extentSector);
2022-03-06 13:29:38 +00:00
// Apple DOS has high byte set over ASCII.
2024-05-01 04:05:22 +01:00
for(var i = 0; i < 30; i++) filenameB[i] = (byte)(entry.filename[i] & 0x7F);
string filename = StringHandlers.SpacePaddedToString(filenameB, _encoding);
2023-10-04 09:48:09 +01:00
_catalogCache.TryAdd(filename, ts);
_fileTypeCache.TryAdd(filename, (byte)(entry.typeAndFlags & 0x7F));
2024-05-01 04:05:22 +01:00
if((entry.typeAndFlags & 0x80) == 0x80 && !_lockedFiles.Contains(filename)) _lockedFiles.Add(filename);
}
lba = (ulong)(catSector.trackOfNext * _sectorsPerTrack + catSector.sectorOfNext);
2024-05-01 04:05:22 +01:00
if(lba > _device.Info.Sectors) break;
}
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(_debug) _catalogBlocks = catalogMs.ToArray();
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2017-12-19 20:33:03 +00:00
}