Files
Aaru/Aaru.Filesystems/AppleDOS/Super.cs

164 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 : Super.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Apple DOS filesystem plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// Handles mounting and umounting the Apple DOS filesystem.
//
// --[ 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-12-31 23:08:23 +00:00
// Copyright © 2011-2021 Natalia Portillo
// ****************************************************************************/
using System.Collections.Generic;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
2021-09-16 04:42:14 +01:00
using Aaru.CommonTypes.Enums;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Structs;
using Aaru.Console;
using Aaru.Helpers;
2020-02-29 18:03:35 +00:00
using Claunia.Encoding;
2017-12-21 14:30:38 +00:00
using Schemas;
using Encoding = System.Text.Encoding;
2020-07-20 15:43:52 +01:00
namespace Aaru.Filesystems
{
2020-07-22 13:20:25 +01:00
public sealed partial class AppleDOS
{
2020-07-20 05:20:18 +01:00
/// <inheritdoc />
2021-09-16 04:42:14 +01:00
public ErrorNumber Mount(IMediaImage imagePlugin, Partition partition, Encoding encoding,
Dictionary<string, string> options, string @namespace)
{
2020-07-20 21:11:32 +01:00
_device = imagePlugin;
_start = partition.Start;
Encoding = encoding ?? new Apple2();
2017-12-26 08:01:40 +00:00
2020-07-20 21:11:32 +01:00
if(_device.Info.Sectors != 455 &&
_device.Info.Sectors != 560)
{
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Apple DOS plugin", "Incorrect device size.");
2020-02-29 18:03:35 +00:00
2021-09-16 04:42:14 +01:00
return ErrorNumber.InOutError;
}
2020-07-20 21:11:32 +01:00
if(_start > 0)
{
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Apple DOS plugin", "Partitions are not supported.");
2020-02-29 18:03:35 +00:00
2021-09-16 04:42:14 +01:00
return ErrorNumber.InOutError;
}
2020-07-20 21:11:32 +01:00
if(_device.Info.SectorSize != 256)
{
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Apple DOS plugin", "Incorrect sector size.");
2020-02-29 18:03:35 +00:00
2021-09-16 04:42:14 +01:00
return ErrorNumber.InOutError;
}
2020-07-20 21:11:32 +01:00
_sectorsPerTrack = _device.Info.Sectors == 455 ? 13 : 16;
// Read the VTOC
ErrorNumber error = _device.ReadSector((ulong)(17 * _sectorsPerTrack), out _vtocBlocks);
if(error != ErrorNumber.NoError)
return error;
_vtoc = Marshal.ByteArrayToStructureLittleEndian<Vtoc>(_vtocBlocks);
2020-07-20 21:11:32 +01:00
_track1UsedByFiles = false;
_track2UsedByFiles = false;
_usedSectors = 1;
error = ReadCatalog();
2020-02-29 18:03:35 +00:00
2021-09-16 04:42:14 +01:00
if(error != ErrorNumber.NoError)
{
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Apple DOS plugin", "Unable to read catalog.");
2020-02-29 18:03:35 +00:00
return error;
}
error = CacheAllFiles();
2020-02-29 18:03:35 +00:00
2021-09-16 04:42:14 +01:00
if(error != ErrorNumber.NoError)
{
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Apple DOS plugin", "Unable cache all files.");
2020-02-29 18:03:35 +00:00
return error;
}
// Create XML metadata for mounted filesystem
2017-12-26 08:01:40 +00:00
XmlFsType = new FileSystemType
{
2020-07-20 04:34:16 +01:00
Bootable = true,
2020-07-20 21:11:32 +01:00
Clusters = _device.Info.Sectors,
ClusterSize = _vtoc.bytesPerSector,
Files = (ulong)_catalogCache.Count,
2020-07-20 04:34:16 +01:00
FilesSpecified = true,
FreeClustersSpecified = true,
Type = "Apple DOS"
};
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
XmlFsType.FreeClusters = XmlFsType.Clusters - _usedSectors;
2020-07-22 13:20:25 +01:00
options ??= GetDefaultOptions();
2020-02-29 18:03:35 +00:00
if(options.TryGetValue("debug", out string debugString))
2020-07-20 21:11:32 +01:00
bool.TryParse(debugString, out _debug);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
_mounted = true;
2020-02-29 18:03:35 +00:00
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoError;
}
2020-07-20 05:20:18 +01:00
/// <inheritdoc />
2021-09-16 04:42:14 +01:00
public ErrorNumber Unmount()
{
2020-07-20 21:11:32 +01:00
_mounted = false;
_extentCache = null;
_fileCache = null;
_catalogCache = null;
_fileSizeCache = null;
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoError;
}
2020-07-20 05:20:18 +01:00
/// <inheritdoc />
2021-09-16 04:42:14 +01:00
public ErrorNumber StatFs(out FileSystemInfo stat)
{
stat = new FileSystemInfo
{
2020-07-20 21:11:32 +01:00
Blocks = _device.Info.Sectors,
2020-07-20 04:34:16 +01:00
FilenameLength = 30,
2020-07-20 21:11:32 +01:00
Files = (ulong)_catalogCache.Count,
2020-07-20 04:34:16 +01:00
PluginId = Id,
Type = "Apple DOS"
};
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
stat.FreeFiles = _totalFileEntries - stat.Files;
stat.FreeBlocks = stat.Blocks - _usedSectors;
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoError;
}
}
2017-12-19 20:33:03 +00:00
}