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

150 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/>.
//
// ----------------------------------------------------------------------------
2020-01-03 17:51:30 +00:00
// Copyright © 2011-2020 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Structs;
using Aaru.Helpers;
2020-02-27 00:33:26 +00:00
namespace Aaru.Filesystems.AppleDOS
{
public partial class AppleDOS
{
2020-07-20 05:20:18 +01:00
/// <inheritdoc />
2020-02-29 18:03:35 +00:00
/// <summary>Solves a symbolic link.</summary>
/// <param name="path">Link path.</param>
/// <param name="dest">Link destination.</param>
public Errno ReadLink(string path, out string dest)
{
dest = null;
2020-02-29 18:03:35 +00:00
return !mounted ? Errno.AccessDenied : Errno.NotSupported;
}
2020-07-20 05:20:18 +01:00
/// <inheritdoc />
2020-02-29 18:03:35 +00:00
/// <summary>Lists contents from a directory.</summary>
/// <param name="path">Directory path.</param>
/// <param name="contents">Directory contents.</param>
public Errno ReadDir(string path, out List<string> contents)
{
contents = null;
2020-02-29 18:03:35 +00:00
if(!mounted)
return Errno.AccessDenied;
if(!string.IsNullOrEmpty(path) &&
string.Compare(path, "/", StringComparison.OrdinalIgnoreCase) != 0)
return Errno.NotSupported;
contents = catalogCache.Keys.ToList();
if(debug)
{
contents.Add("$");
contents.Add("$Boot");
contents.Add("$Vtoc");
}
contents.Sort();
2020-02-29 18:03:35 +00:00
return Errno.NoError;
}
Errno ReadCatalog()
{
2020-02-29 18:03:35 +00:00
var catalogMs = new MemoryStream();
ulong lba = (ulong)((vtoc.catalogTrack * sectorsPerTrack) + vtoc.catalogSector);
totalFileEntries = 0;
2018-06-22 08:08:38 +01:00
catalogCache = new Dictionary<string, ushort>();
fileTypeCache = new Dictionary<string, byte>();
fileSizeCache = new Dictionary<string, int>();
lockedFiles = new List<string>();
2020-02-29 18:03:35 +00:00
if(lba == 0 ||
lba > device.Info.Sectors)
return Errno.InvalidArgument;
while(lba != 0)
{
usedSectors++;
byte[] catSectorB = device.ReadSector(lba);
totalFileEntries += 7;
2020-02-29 18:03:35 +00:00
if(debug)
catalogMs.Write(catSectorB, 0, catSectorB.Length);
// Read the catalog sector
CatalogSector catSector = Marshal.ByteArrayToStructureLittleEndian<CatalogSector>(catSectorB);
foreach(FileEntry entry in catSector.entries.Where(entry => entry.extentTrack > 0))
{
track1UsedByFiles |= entry.extentTrack == 1;
track2UsedByFiles |= entry.extentTrack == 2;
byte[] filenameB = new byte[30];
2018-06-22 08:08:38 +01:00
ushort ts = (ushort)((entry.extentTrack << 8) | entry.extentSector);
// Apple DOS has high byte set over ASCII.
2020-02-29 18:03:35 +00:00
for(int i = 0; i < 30; i++)
filenameB[i] = (byte)(entry.filename[i] & 0x7F);
2017-12-26 08:01:40 +00:00
string filename = StringHandlers.SpacePaddedToString(filenameB, Encoding);
2020-02-29 18:03:35 +00:00
if(!catalogCache.ContainsKey(filename))
catalogCache.Add(filename, ts);
if(!fileTypeCache.ContainsKey(filename))
fileTypeCache.Add(filename, (byte)(entry.typeAndFlags & 0x7F));
if(!fileSizeCache.ContainsKey(filename))
fileSizeCache.Add(filename, entry.length * vtoc.bytesPerSector);
2020-02-29 18:03:35 +00:00
if((entry.typeAndFlags & 0x80) == 0x80 &&
!lockedFiles.Contains(filename))
lockedFiles.Add(filename);
}
2020-02-29 18:03:35 +00:00
lba = (ulong)((catSector.trackOfNext * sectorsPerTrack) + catSector.sectorOfNext);
2020-02-29 18:03:35 +00:00
if(lba > device.Info.Sectors)
break;
}
2020-02-29 18:03:35 +00:00
if(debug)
catalogBlocks = catalogMs.ToArray();
return Errno.NoError;
}
}
2017-12-19 20:33:03 +00:00
}