Files

177 lines
7.5 KiB
C#
Raw Permalink Normal View History

2017-05-19 20:28:49 +01:00
// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
2016-07-31 20:55:58 +01:00
// ----------------------------------------------------------------------------
//
// Filename : Super.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : U.C.S.D. Pascal filesystem plugin.
2016-07-31 20:55:58 +01:00
//
// --[ Description ] ----------------------------------------------------------
//
// Handles mounting and umounting the U.C.S.D. Pascal filesystem.
2016-07-31 20:55:58 +01:00
//
// --[ 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-12-19 10:45:18 +00:00
// Copyright © 2011-2025 Natalia Portillo
2016-07-31 20:55:58 +01:00
// ****************************************************************************/
2016-07-31 20:55:58 +01:00
using System;
using System.Collections.Generic;
using Aaru.CommonTypes.AaruMetadata;
2021-09-16 04:42:14 +01:00
using Aaru.CommonTypes.Enums;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Interfaces;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
2020-02-29 18:03:35 +00:00
using Claunia.Encoding;
using Encoding = System.Text.Encoding;
2025-08-14 15:51:32 +01:00
using FileSystemInfo = Aaru.CommonTypes.Structs.FileSystemInfo;
using Partition = Aaru.CommonTypes.Partition;
namespace Aaru.Filesystems;
2022-03-06 13:29:38 +00:00
// Information from Call-A.P.P.L.E. Pascal Disk Directory Structure
public sealed partial class PascalPlugin
2016-07-31 20:55:58 +01:00
{
#region IReadOnlyFilesystem Members
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Mount(IMediaImage imagePlugin, Partition partition, Encoding encoding,
Dictionary<string, string> options, string @namespace)
2016-07-31 20:55:58 +01:00
{
_device = imagePlugin;
_encoding = encoding ?? new Apple2();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
options ??= GetDefaultOptions();
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
if(options.TryGetValue("debug", out string debugString)) bool.TryParse(debugString, out _debug);
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
if(_device.Info.Sectors < 3) return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
_multiplier = (uint)(imagePlugin.Info.SectorSize == 256 ? 2 : 1);
2022-03-06 13:29:38 +00:00
// Blocks 0 and 1 are boot code
ErrorNumber errno = _device.ReadSectors(_multiplier * 2, false, _multiplier, out _catalogBlocks, out _);
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return errno;
2022-03-06 13:29:38 +00:00
// On Apple //, it's little endian
// TODO: Fix
//BigEndianBitConverter.IsLittleEndian =
// multiplier == 2 ? !BitConverter.IsLittleEndian : BitConverter.IsLittleEndian;
2022-03-06 13:29:38 +00:00
_mountedVolEntry.FirstBlock = BigEndianBitConverter.ToInt16(_catalogBlocks, 0x00);
_mountedVolEntry.LastBlock = BigEndianBitConverter.ToInt16(_catalogBlocks, 0x02);
_mountedVolEntry.EntryType = (PascalFileKind)BigEndianBitConverter.ToInt16(_catalogBlocks, 0x04);
_mountedVolEntry.VolumeName = new byte[8];
Array.Copy(_catalogBlocks, 0x06, _mountedVolEntry.VolumeName, 0, 8);
_mountedVolEntry.Blocks = BigEndianBitConverter.ToInt16(_catalogBlocks, 0x0E);
_mountedVolEntry.Files = BigEndianBitConverter.ToInt16(_catalogBlocks, 0x10);
_mountedVolEntry.Dummy = BigEndianBitConverter.ToInt16(_catalogBlocks, 0x12);
_mountedVolEntry.LastBoot = BigEndianBitConverter.ToInt16(_catalogBlocks, 0x14);
_mountedVolEntry.Tail = BigEndianBitConverter.ToInt32(_catalogBlocks, 0x16);
2020-07-20 21:11:32 +01:00
if(_mountedVolEntry.FirstBlock != 0 ||
_mountedVolEntry.LastBlock <= _mountedVolEntry.FirstBlock ||
(ulong)_mountedVolEntry.LastBlock > _device.Info.Sectors / _multiplier - 2 ||
_mountedVolEntry.EntryType != PascalFileKind.Volume && _mountedVolEntry.EntryType != PascalFileKind.Secure ||
_mountedVolEntry.VolumeName[0] > 7 ||
_mountedVolEntry.Blocks < 0 ||
(ulong)_mountedVolEntry.Blocks != _device.Info.Sectors / _multiplier ||
2022-03-06 13:29:38 +00:00
_mountedVolEntry.Files < 0)
return ErrorNumber.InvalidArgument;
errno = _device.ReadSectors(_multiplier * 2,
false,
2022-03-07 07:36:44 +00:00
(uint)(_mountedVolEntry.LastBlock - _mountedVolEntry.FirstBlock - 2) * _multiplier,
out _catalogBlocks,
out _);
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return errno;
2020-02-29 18:03:35 +00:00
var offset = 26;
2024-05-01 04:39:38 +01:00
_fileEntries = [];
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
while(offset + 26 < _catalogBlocks.Length)
{
var entry = new PascalFileEntry
{
2022-03-06 13:29:38 +00:00
Filename = new byte[16],
FirstBlock = BigEndianBitConverter.ToInt16(_catalogBlocks, offset + 0x00),
LastBlock = BigEndianBitConverter.ToInt16(_catalogBlocks, offset + 0x02),
EntryType = (PascalFileKind)BigEndianBitConverter.ToInt16(_catalogBlocks, offset + 0x04),
LastBytes = BigEndianBitConverter.ToInt16(_catalogBlocks, offset + 0x16),
ModificationTime = BigEndianBitConverter.ToInt16(_catalogBlocks, offset + 0x18)
};
2022-03-06 13:29:38 +00:00
Array.Copy(_catalogBlocks, offset + 0x06, entry.Filename, 0, 16);
2024-05-01 04:05:22 +01:00
if(entry.Filename[0] <= 15 && entry.Filename[0] > 0) _fileEntries.Add(entry);
2022-03-06 13:29:38 +00:00
offset += 26;
}
errno = _device.ReadSectors(0, false, 2 * _multiplier, out _bootBlocks, out _);
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return errno;
Metadata = new FileSystem
{
Bootable = !ArrayHelpers.ArrayIsNullOrEmpty(_bootBlocks),
Clusters = (ulong)_mountedVolEntry.Blocks,
ClusterSize = _device.Info.SectorSize,
Files = (ulong)_mountedVolEntry.Files,
Type = FS_TYPE,
VolumeName = StringHandlers.PascalToString(_mountedVolEntry.VolumeName, _encoding)
2022-03-06 13:29:38 +00:00
};
_mounted = true;
return ErrorNumber.NoError;
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Unmount()
{
_mounted = false;
_fileEntries = null;
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber StatFs(out FileSystemInfo stat)
{
stat = new FileSystemInfo
{
2022-03-06 13:29:38 +00:00
Blocks = (ulong)_mountedVolEntry.Blocks,
FilenameLength = 16,
Files = (ulong)_mountedVolEntry.Files,
FreeBlocks = 0,
PluginId = Id,
Type = FS_TYPE
2022-03-06 13:29:38 +00:00
};
2022-03-07 07:36:44 +00:00
stat.FreeBlocks = (ulong)(_mountedVolEntry.Blocks - (_mountedVolEntry.LastBlock - _mountedVolEntry.FirstBlock));
2024-05-01 04:05:22 +01:00
foreach(PascalFileEntry entry in _fileEntries) stat.FreeBlocks -= (ulong)(entry.LastBlock - entry.FirstBlock);
2022-03-06 13:29:38 +00:00
return ErrorNumber.NotImplemented;
2016-07-31 20:55:58 +01:00
}
#endregion
2017-12-19 20:33:03 +00:00
}