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

190 lines
5.8 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/>.
//
// ----------------------------------------------------------------------------
2022-12-03 16:07:10 +00:00
// Copyright © 2011-2023 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
{
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;
if(!_mounted)
return ErrorNumber.AccessDenied;
if(!string.IsNullOrEmpty(path) &&
string.Compare(path, "/", StringComparison.OrdinalIgnoreCase) != 0)
return ErrorNumber.NotSupported;
List<string> contents = _catalogCache.Keys.ToList();
if(_debug)
{
contents.Add("$");
contents.Add("$Boot");
contents.Add("$Vtoc");
}
contents.Sort();
node = new AppleDosDirNode
{
Path = path,
_position = 0,
_contents = contents.ToArray()
};
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadDir(string path, out List<string> contents)
{
contents = null;
2022-03-06 13:29:38 +00:00
if(!_mounted)
return ErrorNumber.AccessDenied;
2022-03-06 13:29:38 +00:00
if(!string.IsNullOrEmpty(path) &&
string.Compare(path, "/", StringComparison.OrdinalIgnoreCase) != 0)
return ErrorNumber.NotSupported;
2022-03-06 13:29:38 +00:00
contents = _catalogCache.Keys.ToList();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(_debug)
{
contents.Add("$");
contents.Add("$Boot");
contents.Add("$Vtoc");
}
2022-03-06 13:29:38 +00:00
contents.Sort();
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
/// <inheritdoc />
public ErrorNumber CloseDir(IDirNode node)
{
if(node is not AppleDosDirNode mynode)
return ErrorNumber.InvalidArgument;
mynode._position = -1;
mynode._contents = null;
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
ErrorNumber ReadCatalog()
{
var catalogMs = new MemoryStream();
ulong 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>();
_lockedFiles = new List<string>();
if(lba == 0 ||
lba > _device.Info.Sectors)
return ErrorNumber.InvalidArgument;
while(lba != 0)
{
_usedSectors++;
ErrorNumber errno = _device.ReadSector(lba, out byte[] catSectorB);
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
_totalFileEntries += 7;
2022-03-06 13:29:38 +00: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;
byte[] filenameB = new byte[30];
ushort ts = (ushort)((entry.extentTrack << 8) | entry.extentSector);
2022-03-06 13:29:38 +00:00
// Apple DOS has high byte set over ASCII.
for(int i = 0; i < 30; i++)
2022-03-06 13:29:38 +00:00
filenameB[i] = (byte)(entry.filename[i] & 0x7F);
string filename = StringHandlers.SpacePaddedToString(filenameB, _encoding);
2022-03-06 13:29:38 +00:00
if(!_catalogCache.ContainsKey(filename))
_catalogCache.Add(filename, ts);
2022-03-06 13:29:38 +00:00
if(!_fileTypeCache.ContainsKey(filename))
_fileTypeCache.Add(filename, (byte)(entry.typeAndFlags & 0x7F));
2022-03-06 13:29:38 +00:00
if((entry.typeAndFlags & 0x80) == 0x80 &&
!_lockedFiles.Contains(filename))
_lockedFiles.Add(filename);
}
lba = (ulong)((catSector.trackOfNext * _sectorsPerTrack) + catSector.sectorOfNext);
2022-03-06 13:29:38 +00:00
if(lba > _device.Info.Sectors)
break;
}
2022-03-06 13:29:38 +00:00
if(_debug)
_catalogBlocks = catalogMs.ToArray();
return ErrorNumber.NoError;
}
2017-12-19 20:33:03 +00:00
}