2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2016-08-26 01:43:15 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : Super.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
2016-08-26 01:45:58 +01:00
|
|
|
// Component : CP/M filesystem plugin.
|
2016-08-26 01:43:15 +01:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
2016-08-26 01:45:58 +01:00
|
|
|
// Handles mounting and umounting the CP/M filesystem.
|
|
|
|
|
// Caches the whole volume on mounting (shouldn't be a problem, maximum
|
|
|
|
|
// volume size for CP/M is 8 MiB).
|
2016-08-26 01:43:15 +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-08-26 01:43:15 +01:00
|
|
|
// ****************************************************************************/
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2016-08-26 01:43:15 +01:00
|
|
|
using System;
|
2016-08-26 01:45:58 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2017-12-21 07:08:26 +00:00
|
|
|
using System.Linq;
|
2016-08-26 01:45:58 +01:00
|
|
|
using System.Text;
|
2022-12-15 22:21:07 +00:00
|
|
|
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;
|
|
|
|
|
using Aaru.CommonTypes.Structs;
|
|
|
|
|
using Aaru.Helpers;
|
2025-08-17 05:50:25 +01:00
|
|
|
using Aaru.Logging;
|
2020-02-27 00:33:26 +00:00
|
|
|
using FileAttributes = Aaru.CommonTypes.Structs.FileAttributes;
|
|
|
|
|
using FileSystemInfo = Aaru.CommonTypes.Structs.FileSystemInfo;
|
2022-12-15 22:21:07 +00:00
|
|
|
using Partition = Aaru.CommonTypes.Partition;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
namespace Aaru.Filesystems;
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
public sealed partial class CPM
|
2016-08-26 01:43:15 +01:00
|
|
|
{
|
2023-10-03 23:22:08 +01:00
|
|
|
#region IReadOnlyFilesystem Members
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
2023-10-03 23:22:08 +01:00
|
|
|
public ErrorNumber Mount(IMediaImage imagePlugin, Partition partition, Encoding encoding,
|
|
|
|
|
Dictionary<string, string> options, string @namespace)
|
2016-08-26 01:43:15 +01:00
|
|
|
{
|
2022-12-17 23:17:18 +00:00
|
|
|
_device = imagePlugin;
|
|
|
|
|
_encoding = encoding ?? Encoding.GetEncoding("IBM437");
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
// As the identification is so complex, just call Identify() and relay on its findings
|
2023-10-04 17:34:40 +01:00
|
|
|
if(!Identify(_device, partition) || !_cpmFound || _workingDefinition == null || _dpb == null)
|
2022-03-06 13:29:38 +00:00
|
|
|
return ErrorNumber.InvalidArgument;
|
|
|
|
|
|
|
|
|
|
// Build the software interleaving sector mask
|
|
|
|
|
if(_workingDefinition.sides == 1)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
_sectorMask = new int[_workingDefinition.side1.sectorIds.Length];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2025-10-22 14:28:58 +01:00
|
|
|
for(var m = 0; m < _sectorMask.Length; m++)
|
2022-03-06 13:29:38 +00:00
|
|
|
_sectorMask[m] = _workingDefinition.side1.sectorIds[m] - _workingDefinition.side1.sectorIds[0];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Head changes after every track
|
2025-11-24 20:27:29 +00:00
|
|
|
if(string.Equals(_workingDefinition.order, "SIDES", StringComparison.InvariantCultureIgnoreCase))
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
_sectorMask = new int[_workingDefinition.side1.sectorIds.Length +
|
|
|
|
|
_workingDefinition.side2.sectorIds.Length];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2025-10-22 14:28:58 +01:00
|
|
|
for(var m = 0; m < _workingDefinition.side1.sectorIds.Length; m++)
|
2022-03-06 13:29:38 +00:00
|
|
|
_sectorMask[m] = _workingDefinition.side1.sectorIds[m] - _workingDefinition.side1.sectorIds[0];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Skip first track (first side)
|
2025-10-22 14:28:58 +01:00
|
|
|
for(var m = 0; m < _workingDefinition.side2.sectorIds.Length; m++)
|
2023-10-03 23:22:08 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
_sectorMask[m + _workingDefinition.side1.sectorIds.Length] =
|
2023-10-04 17:34:40 +01:00
|
|
|
_workingDefinition.side2.sectorIds[m] -
|
|
|
|
|
_workingDefinition.side2.sectorIds[0] +
|
2022-03-06 13:29:38 +00:00
|
|
|
_workingDefinition.side1.sectorIds.Length;
|
2023-10-03 23:22:08 +01:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Head changes after whole side
|
2025-11-24 20:27:29 +00:00
|
|
|
else if(string.Equals(_workingDefinition.order, "CYLINDERS", StringComparison.InvariantCultureIgnoreCase))
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2025-10-22 14:28:58 +01:00
|
|
|
for(var m = 0; m < _workingDefinition.side1.sectorIds.Length; m++)
|
2022-03-06 13:29:38 +00:00
|
|
|
_sectorMask[m] = _workingDefinition.side1.sectorIds[m] - _workingDefinition.side1.sectorIds[0];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Skip first track (first side) and first track (second side)
|
2025-10-22 14:28:58 +01:00
|
|
|
for(var m = 0; m < _workingDefinition.side1.sectorIds.Length; m++)
|
2023-10-03 23:22:08 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
_sectorMask[m + _workingDefinition.side1.sectorIds.Length] =
|
2023-10-04 17:34:40 +01:00
|
|
|
_workingDefinition.side1.sectorIds[m] -
|
|
|
|
|
_workingDefinition.side1.sectorIds[0] +
|
|
|
|
|
_workingDefinition.side1.sectorIds.Length +
|
|
|
|
|
_workingDefinition.side2.sectorIds.Length;
|
2023-10-03 23:22:08 +01:00
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// TODO: Implement CYLINDERS ordering
|
2025-08-17 06:11:22 +01:00
|
|
|
AaruLogging.Debug(MODULE_NAME, Localization.CYLINDERS_ordering_not_yet_implemented);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return ErrorNumber.NotImplemented;
|
2016-08-26 01:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// TODO: Implement COLUMBIA ordering
|
2025-11-24 20:27:29 +00:00
|
|
|
else if(string.Equals(_workingDefinition.order, "COLUMBIA", StringComparison.InvariantCultureIgnoreCase))
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
2025-08-17 06:11:22 +01:00
|
|
|
AaruLogging.Debug(MODULE_NAME,
|
2025-10-22 14:28:58 +01:00
|
|
|
Localization
|
|
|
|
|
.Dont_know_how_to_handle_COLUMBIA_ordering_not_proceeding_with_this_definition);
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return ErrorNumber.NotImplemented;
|
|
|
|
|
}
|
2021-09-19 21:16:47 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// TODO: Implement EAGLE ordering
|
2025-11-24 20:27:29 +00:00
|
|
|
else if(string.Equals(_workingDefinition.order, "EAGLE", StringComparison.InvariantCultureIgnoreCase))
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2025-08-17 06:11:22 +01:00
|
|
|
AaruLogging.Debug(MODULE_NAME,
|
2025-10-22 14:28:58 +01:00
|
|
|
Localization
|
|
|
|
|
.Don_know_how_to_handle_EAGLE_ordering_not_proceeding_with_this_definition);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return ErrorNumber.NotImplemented;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-08-17 06:11:22 +01:00
|
|
|
AaruLogging.Debug(MODULE_NAME,
|
2025-10-22 14:28:58 +01:00
|
|
|
Localization.Unknown_order_type_0_not_proceeding_with_this_definition,
|
|
|
|
|
_workingDefinition.order);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return ErrorNumber.NotSupported;
|
2016-08-26 01:45:58 +01:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Deinterleave whole volume
|
|
|
|
|
Dictionary<ulong, byte[]> deinterleavedSectors = new();
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2025-11-24 20:27:29 +00:00
|
|
|
if(_workingDefinition.sides == 1 ||
|
|
|
|
|
string.Equals(_workingDefinition.order, "SIDES", StringComparison.InvariantCultureIgnoreCase))
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2025-08-17 06:11:22 +01:00
|
|
|
AaruLogging.Debug(MODULE_NAME, Localization.Deinterleaving_whole_volume);
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2025-10-22 14:28:58 +01:00
|
|
|
for(var p = 0; p <= (int)(partition.End - partition.Start); p++)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
ErrorNumber errno =
|
2024-05-01 04:05:22 +01:00
|
|
|
_device.ReadSector((ulong)((int)partition.Start +
|
|
|
|
|
p / _sectorMask.Length * _sectorMask.Length +
|
|
|
|
|
_sectorMask[p % _sectorMask.Length]),
|
2025-10-23 03:07:43 +01:00
|
|
|
false,
|
2025-10-22 14:28:58 +01:00
|
|
|
out byte[] readSector,
|
|
|
|
|
out _);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(errno != ErrorNumber.NoError) return errno;
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(_workingDefinition.complement)
|
2025-11-24 20:27:29 +00:00
|
|
|
{
|
|
|
|
|
for(var b = 0; b < readSector.Length; b++) readSector[b] = (byte)(~readSector[b] & 0xFF);
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
deinterleavedSectors.Add((ulong)p, readSector);
|
2016-08-26 01:45:58 +01:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
int blockSize = 128 << _dpb.bsh;
|
|
|
|
|
var blockMs = new MemoryStream();
|
|
|
|
|
ulong blockNo = 0;
|
2025-10-22 14:28:58 +01:00
|
|
|
var sectorsPerBlock = 0;
|
2022-03-06 13:29:38 +00:00
|
|
|
Dictionary<ulong, byte[]> allocationBlocks = new();
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2025-08-17 06:11:22 +01:00
|
|
|
AaruLogging.Debug(MODULE_NAME, Localization.Creating_allocation_blocks);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// For each volume sector
|
|
|
|
|
for(ulong a = 0; a < (ulong)deinterleavedSectors.Count; a++)
|
|
|
|
|
{
|
|
|
|
|
deinterleavedSectors.TryGetValue(a, out byte[] sector);
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// May it happen? Just in case, CP/M blocks are smaller than physical sectors
|
|
|
|
|
if(sector.Length > blockSize)
|
2023-10-03 23:22:08 +01:00
|
|
|
{
|
2025-10-22 14:28:58 +01:00
|
|
|
for(var i = 0; i < sector.Length / blockSize; i++)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2025-10-22 14:28:58 +01:00
|
|
|
var tmp = new byte[blockSize];
|
2022-03-06 13:29:38 +00:00
|
|
|
Array.Copy(sector, blockSize * i, tmp, 0, blockSize);
|
|
|
|
|
allocationBlocks.Add(blockNo++, tmp);
|
|
|
|
|
}
|
2023-10-03 23:22:08 +01:00
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// CP/M blocks are larger than physical sectors
|
|
|
|
|
else if(sector.Length < blockSize)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
blockMs.Write(sector, 0, sector.Length);
|
|
|
|
|
sectorsPerBlock++;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(sectorsPerBlock != blockSize / sector.Length) continue;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
allocationBlocks.Add(blockNo++, blockMs.ToArray());
|
|
|
|
|
sectorsPerBlock = 0;
|
|
|
|
|
blockMs = new MemoryStream();
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// CP/M blocks are same size than physical sectors
|
|
|
|
|
else
|
|
|
|
|
allocationBlocks.Add(blockNo++, sector);
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2025-08-17 06:11:22 +01:00
|
|
|
AaruLogging.Debug(MODULE_NAME, Localization.Reading_directory);
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
int dirOff;
|
|
|
|
|
int dirSectors = (_dpb.drm + 1) * 32 / _workingDefinition.bytesPerSector;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(_workingDefinition.sofs > 0)
|
|
|
|
|
dirOff = _workingDefinition.sofs;
|
|
|
|
|
else
|
|
|
|
|
dirOff = _workingDefinition.ofs * _workingDefinition.sectorsPerTrack;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Read the whole directory blocks
|
|
|
|
|
var dirMs = new MemoryStream();
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2025-10-22 14:28:58 +01:00
|
|
|
for(var d = 0; d < dirSectors; d++)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
|
|
|
|
deinterleavedSectors.TryGetValue((ulong)(d + dirOff), out byte[] sector);
|
|
|
|
|
dirMs.Write(sector, 0, sector.Length);
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
byte[] directory = dirMs.ToArray();
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(directory == null) return ErrorNumber.InvalidArgument;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2025-10-22 14:28:58 +01:00
|
|
|
var dirCnt = 0;
|
2022-03-06 13:29:38 +00:00
|
|
|
string file1 = null;
|
|
|
|
|
string file2 = null;
|
|
|
|
|
string file3 = null;
|
2017-12-27 23:55:59 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
Dictionary<string, Dictionary<int, List<ushort>>> fileExtents = new();
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
_statCache = new Dictionary<string, FileEntryInfo>();
|
|
|
|
|
_cpmStat = new FileSystemInfo();
|
2025-10-22 14:28:58 +01:00
|
|
|
var atime = false;
|
2024-05-01 04:39:38 +01:00
|
|
|
_dirList = [];
|
2022-03-06 13:29:38 +00:00
|
|
|
_labelCreationDate = null;
|
|
|
|
|
_labelUpdateDate = null;
|
|
|
|
|
_passwordCache = new Dictionary<string, byte[]>();
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2025-08-17 06:11:22 +01:00
|
|
|
AaruLogging.Debug(MODULE_NAME, Localization.Traversing_directory);
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// For each directory entry
|
2025-10-22 14:28:58 +01:00
|
|
|
for(var dOff = 0; dOff < directory.Length; dOff += 32)
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2023-10-03 23:22:08 +01:00
|
|
|
{
|
2022-11-13 19:38:03 +00:00
|
|
|
switch(directory[dOff] & 0x7F)
|
|
|
|
|
{
|
|
|
|
|
// Describes a file (does not support PDOS entries with user >= 16, because they're identical to password entries
|
|
|
|
|
case < 0x10 when allocationBlocks.Count > 256:
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
|
|
|
|
DirectoryEntry16 entry =
|
|
|
|
|
Marshal.ByteArrayToStructureLittleEndian<DirectoryEntry16>(directory, dOff, 32);
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
bool hidden = (entry.statusUser & 0x80) == 0x80;
|
|
|
|
|
bool rdOnly = (entry.filename[0] & 0x80) == 0x80 || (entry.extension[0] & 0x80) == 0x80;
|
|
|
|
|
bool system = (entry.filename[1] & 0x80) == 0x80 || (entry.extension[2] & 0x80) == 0x80;
|
2021-06-03 11:54:19 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
//bool backed = (entry.filename[3] & 0x80) == 0x80 || (entry.extension[3] & 0x80) == 0x80;
|
|
|
|
|
int user = entry.statusUser & 0x0F;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2025-10-22 14:28:58 +01:00
|
|
|
var validEntry = true;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2025-10-22 14:28:58 +01:00
|
|
|
for(var i = 0; i < 8; i++)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
|
|
|
|
entry.filename[i] &= 0x7F;
|
|
|
|
|
validEntry &= entry.filename[i] >= 0x20;
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2025-10-22 14:28:58 +01:00
|
|
|
for(var i = 0; i < 3; i++)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
|
|
|
|
entry.extension[i] &= 0x7F;
|
|
|
|
|
validEntry &= entry.extension[i] >= 0x20;
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!validEntry) continue;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
string filename = Encoding.ASCII.GetString(entry.filename).Trim();
|
|
|
|
|
string extension = Encoding.ASCII.GetString(entry.extension).Trim();
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// If user is != 0, append user to name to have identical filenames
|
2024-05-01 04:05:22 +01:00
|
|
|
if(user > 0) filename = $"{user:X1}:{filename}";
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!string.IsNullOrEmpty(extension)) filename = filename + "." + extension;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
filename = filename.Replace('/', '\u2215');
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2023-10-03 23:22:08 +01:00
|
|
|
int entryNo = (32 * entry.extentCounter + entry.extentCounterHigh) / (_dpb.exm + 1);
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Do we have a stat for the file already?
|
|
|
|
|
if(_statCache.TryGetValue(filename, out FileEntryInfo fInfo))
|
|
|
|
|
_statCache.Remove(filename);
|
|
|
|
|
else
|
2023-10-03 23:22:08 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
fInfo = new FileEntryInfo
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
Attributes = new FileAttributes()
|
|
|
|
|
};
|
2023-10-03 23:22:08 +01:00
|
|
|
}
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// And any extent?
|
|
|
|
|
if(fileExtents.TryGetValue(filename, out Dictionary<int, List<ushort>> extentBlocks))
|
|
|
|
|
fileExtents.Remove(filename);
|
|
|
|
|
else
|
|
|
|
|
extentBlocks = new Dictionary<int, List<ushort>>();
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Do we already have this extent? Should never happen
|
|
|
|
|
if(extentBlocks.TryGetValue(entryNo, out List<ushort> blocks))
|
|
|
|
|
extentBlocks.Remove(entryNo);
|
|
|
|
|
else
|
2024-05-01 04:39:38 +01:00
|
|
|
blocks = [];
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Attributes
|
2024-05-01 04:05:22 +01:00
|
|
|
if(hidden) fInfo.Attributes |= FileAttributes.Hidden;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(rdOnly) fInfo.Attributes |= FileAttributes.ReadOnly;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(system) fInfo.Attributes |= FileAttributes.System;
|
2021-06-03 11:54:19 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Supposedly there is a value in the directory entry telling how many blocks are designated in
|
|
|
|
|
// this entry. However some implementations tend to do whatever they wish, but none will ever
|
|
|
|
|
// allocate block 0 for a file because that's where the directory resides.
|
|
|
|
|
// There is also a field telling how many bytes are used in the last block, but its meaning is
|
|
|
|
|
// non-standard so we must ignore it.
|
2025-11-24 19:00:58 +00:00
|
|
|
blocks.AddRange(entry.allocations.Where(blk => !blocks.Contains(blk) && blk != 0));
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Save the file
|
|
|
|
|
fInfo.UID = (ulong)user;
|
|
|
|
|
extentBlocks.Add(entryNo, blocks);
|
|
|
|
|
fileExtents.Add(filename, extentBlocks);
|
|
|
|
|
_statCache.Add(filename, fInfo);
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Add the file to the directory listing
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!_dirList.Contains(filename)) _dirList.Add(filename);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Count entries 3 by 3 for timestamps
|
|
|
|
|
switch(dirCnt % 3)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
file1 = filename;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
file2 = filename;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
file3 = filename;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
2016-08-26 01:45:58 +01:00
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
dirCnt++;
|
2022-11-13 19:38:03 +00:00
|
|
|
|
|
|
|
|
break;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2022-11-13 19:38:03 +00:00
|
|
|
case < 0x10:
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
DirectoryEntry entry =
|
|
|
|
|
Marshal.ByteArrayToStructureLittleEndian<DirectoryEntry>(directory, dOff, 32);
|
|
|
|
|
|
|
|
|
|
bool hidden = (entry.statusUser & 0x80) == 0x80;
|
|
|
|
|
bool rdOnly = (entry.filename[0] & 0x80) == 0x80 || (entry.extension[0] & 0x80) == 0x80;
|
|
|
|
|
bool system = (entry.filename[1] & 0x80) == 0x80 || (entry.extension[2] & 0x80) == 0x80;
|
|
|
|
|
|
|
|
|
|
//bool backed = (entry.filename[3] & 0x80) == 0x80 || (entry.extension[3] & 0x80) == 0x80;
|
|
|
|
|
int user = entry.statusUser & 0x0F;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2025-10-22 14:28:58 +01:00
|
|
|
var validEntry = true;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2025-10-22 14:28:58 +01:00
|
|
|
for(var i = 0; i < 8; i++)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
entry.filename[i] &= 0x7F;
|
2022-03-06 13:29:38 +00:00
|
|
|
validEntry &= entry.filename[i] >= 0x20;
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2025-10-22 14:28:58 +01:00
|
|
|
for(var i = 0; i < 3; i++)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
entry.extension[i] &= 0x7F;
|
2022-03-06 13:29:38 +00:00
|
|
|
validEntry &= entry.extension[i] >= 0x20;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!validEntry) continue;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2017-12-27 23:55:59 +00:00
|
|
|
string filename = Encoding.ASCII.GetString(entry.filename).Trim();
|
2016-08-26 01:45:58 +01:00
|
|
|
string extension = Encoding.ASCII.GetString(entry.extension).Trim();
|
|
|
|
|
|
|
|
|
|
// If user is != 0, append user to name to have identical filenames
|
2024-05-01 04:05:22 +01:00
|
|
|
if(user > 0) filename = $"{user:X1}:{filename}";
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!string.IsNullOrEmpty(extension)) filename = filename + "." + extension;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2021-06-03 11:54:19 +01:00
|
|
|
filename = filename.Replace('/', '\u2215');
|
|
|
|
|
|
2023-10-03 23:22:08 +01:00
|
|
|
int entryNo = (32 * entry.extentCounterHigh + entry.extentCounter) / (_dpb.exm + 1);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
// Do we have a stat for the file already?
|
|
|
|
|
if(_statCache.TryGetValue(filename, out FileEntryInfo fInfo))
|
|
|
|
|
_statCache.Remove(filename);
|
|
|
|
|
else
|
2023-10-03 23:22:08 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
fInfo = new FileEntryInfo
|
|
|
|
|
{
|
|
|
|
|
Attributes = new FileAttributes()
|
|
|
|
|
};
|
2023-10-03 23:22:08 +01:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
// And any extent?
|
|
|
|
|
if(fileExtents.TryGetValue(filename, out Dictionary<int, List<ushort>> extentBlocks))
|
|
|
|
|
fileExtents.Remove(filename);
|
|
|
|
|
else
|
|
|
|
|
extentBlocks = new Dictionary<int, List<ushort>>();
|
|
|
|
|
|
|
|
|
|
// Do we already have this extent? Should never happen
|
|
|
|
|
if(extentBlocks.TryGetValue(entryNo, out List<ushort> blocks))
|
|
|
|
|
extentBlocks.Remove(entryNo);
|
|
|
|
|
else
|
2024-05-01 04:39:38 +01:00
|
|
|
blocks = [];
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
// Attributes
|
2024-05-01 04:05:22 +01:00
|
|
|
if(hidden) fInfo.Attributes |= FileAttributes.Hidden;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(rdOnly) fInfo.Attributes |= FileAttributes.ReadOnly;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(system) fInfo.Attributes |= FileAttributes.System;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Supposedly there is a value in the directory entry telling how many blocks are designated in
|
|
|
|
|
// this entry. However some implementations tend to do whatever they wish, but none will ever
|
|
|
|
|
// allocate block 0 for a file because that's where the directory resides.
|
|
|
|
|
// There is also a field telling how many bytes are used in the last block, but its meaning is
|
|
|
|
|
// non-standard so we must ignore it.
|
|
|
|
|
foreach(ushort blk in entry.allocations.Where(blk => !blocks.Contains(blk) && blk != 0))
|
|
|
|
|
blocks.Add(blk);
|
|
|
|
|
|
|
|
|
|
// Save the file
|
|
|
|
|
fInfo.UID = (ulong)user;
|
|
|
|
|
extentBlocks.Add(entryNo, blocks);
|
|
|
|
|
fileExtents.Add(filename, extentBlocks);
|
|
|
|
|
_statCache.Add(filename, fInfo);
|
|
|
|
|
|
|
|
|
|
// Add the file to the directory listing
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!_dirList.Contains(filename)) _dirList.Add(filename);
|
2016-08-26 01:45:58 +01:00
|
|
|
|
|
|
|
|
// Count entries 3 by 3 for timestamps
|
|
|
|
|
switch(dirCnt % 3)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
file1 = filename;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-08-26 01:45:58 +01:00
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
file2 = filename;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-08-26 01:45:58 +01:00
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
file3 = filename;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-08-26 01:45:58 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2016-08-26 01:45:58 +01:00
|
|
|
dirCnt++;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
break;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
// A password entry (or a file entry in PDOS, but this does not handle that case)
|
|
|
|
|
case >= 0x10 and < 0x20:
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-11-13 19:38:03 +00:00
|
|
|
PasswordEntry entry = Marshal.ByteArrayToStructureLittleEndian<PasswordEntry>(directory, dOff, 32);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
int user = entry.userNumber & 0x0F;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2025-10-22 14:28:58 +01:00
|
|
|
for(var i = 0; i < 8; i++) entry.filename[i] &= 0x7F;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2025-10-22 14:28:58 +01:00
|
|
|
for(var i = 0; i < 3; i++) entry.extension[i] &= 0x7F;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
string filename = Encoding.ASCII.GetString(entry.filename).Trim();
|
|
|
|
|
string extension = Encoding.ASCII.GetString(entry.extension).Trim();
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
// If user is != 0, append user to name to have identical filenames
|
2024-05-01 04:05:22 +01:00
|
|
|
if(user > 0) filename = $"{user:X1}:{filename}";
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!string.IsNullOrEmpty(extension)) filename = filename + "." + extension;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
filename = filename.Replace('/', '\u2215');
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
// Do not repeat passwords
|
2024-05-01 04:05:22 +01:00
|
|
|
if(_passwordCache.ContainsKey(filename)) _passwordCache.Remove(filename);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
// Copy whole password entry
|
2025-10-22 14:28:58 +01:00
|
|
|
var tmp = new byte[32];
|
2022-11-13 19:38:03 +00:00
|
|
|
Array.Copy(directory, dOff, tmp, 0, 32);
|
|
|
|
|
_passwordCache.Add(filename, tmp);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
// Count entries 3 by 3 for timestamps
|
|
|
|
|
switch(dirCnt % 3)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
file1 = filename;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
file2 = filename;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
file3 = filename;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
dirCnt++;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
// Volume label and password entry. Volume password is ignored.
|
|
|
|
|
default:
|
|
|
|
|
switch(directory[dOff] & 0x7F)
|
|
|
|
|
{
|
|
|
|
|
case 0x20:
|
|
|
|
|
LabelEntry labelEntry =
|
|
|
|
|
Marshal.ByteArrayToStructureLittleEndian<LabelEntry>(directory, dOff, 32);
|
|
|
|
|
|
|
|
|
|
// The volume label defines if one of the fields in CP/M 3 timestamp is a creation or an
|
|
|
|
|
// access time
|
|
|
|
|
atime |= (labelEntry.flags & 0x40) == 0x40;
|
|
|
|
|
|
|
|
|
|
_label = Encoding.ASCII.GetString(directory, dOff + 1, 11).Trim();
|
|
|
|
|
_labelCreationDate = new byte[4];
|
|
|
|
|
_labelUpdateDate = new byte[4];
|
|
|
|
|
Array.Copy(directory, dOff + 24, _labelCreationDate, 0, 4);
|
2023-10-03 23:22:08 +01:00
|
|
|
Array.Copy(directory, dOff + 28, _labelUpdateDate, 0, 4);
|
2022-11-13 19:38:03 +00:00
|
|
|
|
|
|
|
|
// Count entries 3 by 3 for timestamps
|
|
|
|
|
switch(dirCnt % 3)
|
2017-12-21 04:43:29 +00:00
|
|
|
{
|
2022-11-13 19:38:03 +00:00
|
|
|
case 0:
|
|
|
|
|
file1 = null;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
file2 = null;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
file3 = null;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
break;
|
2017-12-24 02:37:41 +00:00
|
|
|
}
|
2017-12-27 23:55:59 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
dirCnt++;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
break;
|
|
|
|
|
case 0x21:
|
|
|
|
|
if(directory[dOff + 10] == 0x00 &&
|
|
|
|
|
directory[dOff + 20] == 0x00 &&
|
|
|
|
|
directory[dOff + 30] == 0x00 &&
|
|
|
|
|
directory[dOff + 31] == 0x00)
|
|
|
|
|
{
|
|
|
|
|
DateEntry dateEntry =
|
|
|
|
|
Marshal.ByteArrayToStructureLittleEndian<DateEntry>(directory, dOff, 32);
|
|
|
|
|
|
|
|
|
|
FileEntryInfo fInfo;
|
|
|
|
|
|
|
|
|
|
// Entry contains timestamps for last 3 entries, whatever the kind they are.
|
|
|
|
|
if(!string.IsNullOrEmpty(file1))
|
|
|
|
|
{
|
|
|
|
|
if(_statCache.TryGetValue(file1, out fInfo))
|
|
|
|
|
_statCache.Remove(file1);
|
|
|
|
|
else
|
|
|
|
|
fInfo = new FileEntryInfo();
|
|
|
|
|
|
|
|
|
|
if(atime)
|
|
|
|
|
fInfo.AccessTime = DateHandlers.CpmToDateTime(dateEntry.date1);
|
|
|
|
|
else
|
|
|
|
|
fInfo.CreationTime = DateHandlers.CpmToDateTime(dateEntry.date1);
|
|
|
|
|
|
|
|
|
|
fInfo.LastWriteTime = DateHandlers.CpmToDateTime(dateEntry.date2);
|
|
|
|
|
|
|
|
|
|
_statCache.Add(file1, fInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!string.IsNullOrEmpty(file2))
|
|
|
|
|
{
|
|
|
|
|
if(_statCache.TryGetValue(file2, out fInfo))
|
|
|
|
|
_statCache.Remove(file2);
|
|
|
|
|
else
|
|
|
|
|
fInfo = new FileEntryInfo();
|
|
|
|
|
|
|
|
|
|
if(atime)
|
|
|
|
|
fInfo.AccessTime = DateHandlers.CpmToDateTime(dateEntry.date3);
|
|
|
|
|
else
|
|
|
|
|
fInfo.CreationTime = DateHandlers.CpmToDateTime(dateEntry.date3);
|
|
|
|
|
|
|
|
|
|
fInfo.LastWriteTime = DateHandlers.CpmToDateTime(dateEntry.date4);
|
|
|
|
|
|
|
|
|
|
_statCache.Add(file2, fInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!string.IsNullOrEmpty(file3))
|
|
|
|
|
{
|
|
|
|
|
if(_statCache.TryGetValue(file3, out fInfo))
|
|
|
|
|
_statCache.Remove(file3);
|
|
|
|
|
else
|
|
|
|
|
fInfo = new FileEntryInfo();
|
|
|
|
|
|
|
|
|
|
if(atime)
|
|
|
|
|
fInfo.AccessTime = DateHandlers.CpmToDateTime(dateEntry.date5);
|
|
|
|
|
else
|
|
|
|
|
fInfo.CreationTime = DateHandlers.CpmToDateTime(dateEntry.date5);
|
|
|
|
|
|
|
|
|
|
fInfo.LastWriteTime = DateHandlers.CpmToDateTime(dateEntry.date6);
|
|
|
|
|
|
|
|
|
|
_statCache.Add(file3, fInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file1 = null;
|
|
|
|
|
file2 = null;
|
|
|
|
|
file3 = null;
|
|
|
|
|
dirCnt = 0;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
// However, if this byte is 0, timestamp is in Z80DOS or DOS+ format
|
|
|
|
|
else if(directory[dOff + 1] == 0x00)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-11-13 19:38:03 +00:00
|
|
|
TrdPartyDateEntry trdPartyDateEntry =
|
|
|
|
|
Marshal.ByteArrayToStructureLittleEndian<TrdPartyDateEntry>(directory, dOff, 32);
|
|
|
|
|
|
|
|
|
|
FileEntryInfo fInfo;
|
|
|
|
|
|
|
|
|
|
// Entry contains timestamps for last 3 entries, whatever the kind they are.
|
|
|
|
|
if(!string.IsNullOrEmpty(file1))
|
|
|
|
|
{
|
|
|
|
|
if(_statCache.TryGetValue(file1, out fInfo))
|
|
|
|
|
_statCache.Remove(file1);
|
|
|
|
|
else
|
|
|
|
|
fInfo = new FileEntryInfo();
|
|
|
|
|
|
2025-10-22 14:28:58 +01:00
|
|
|
var ctime = new byte[4];
|
2022-11-13 19:38:03 +00:00
|
|
|
ctime[0] = trdPartyDateEntry.create1[0];
|
|
|
|
|
ctime[1] = trdPartyDateEntry.create1[1];
|
|
|
|
|
|
|
|
|
|
fInfo.AccessTime = DateHandlers.CpmToDateTime(trdPartyDateEntry.access1);
|
|
|
|
|
fInfo.CreationTime = DateHandlers.CpmToDateTime(ctime);
|
|
|
|
|
fInfo.LastWriteTime = DateHandlers.CpmToDateTime(trdPartyDateEntry.modify1);
|
|
|
|
|
|
|
|
|
|
_statCache.Add(file1, fInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!string.IsNullOrEmpty(file2))
|
|
|
|
|
{
|
|
|
|
|
if(_statCache.TryGetValue(file2, out fInfo))
|
|
|
|
|
_statCache.Remove(file2);
|
|
|
|
|
else
|
|
|
|
|
fInfo = new FileEntryInfo();
|
|
|
|
|
|
2025-10-22 14:28:58 +01:00
|
|
|
var ctime = new byte[4];
|
2022-11-13 19:38:03 +00:00
|
|
|
ctime[0] = trdPartyDateEntry.create2[0];
|
|
|
|
|
ctime[1] = trdPartyDateEntry.create2[1];
|
|
|
|
|
|
|
|
|
|
fInfo.AccessTime = DateHandlers.CpmToDateTime(trdPartyDateEntry.access2);
|
|
|
|
|
fInfo.CreationTime = DateHandlers.CpmToDateTime(ctime);
|
|
|
|
|
fInfo.LastWriteTime = DateHandlers.CpmToDateTime(trdPartyDateEntry.modify2);
|
|
|
|
|
|
|
|
|
|
_statCache.Add(file2, fInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!string.IsNullOrEmpty(file3))
|
|
|
|
|
{
|
|
|
|
|
if(_statCache.TryGetValue(file1, out fInfo))
|
|
|
|
|
_statCache.Remove(file3);
|
|
|
|
|
else
|
|
|
|
|
fInfo = new FileEntryInfo();
|
|
|
|
|
|
2025-10-22 14:28:58 +01:00
|
|
|
var ctime = new byte[4];
|
2022-11-13 19:38:03 +00:00
|
|
|
ctime[0] = trdPartyDateEntry.create3[0];
|
|
|
|
|
ctime[1] = trdPartyDateEntry.create3[1];
|
|
|
|
|
|
|
|
|
|
fInfo.AccessTime = DateHandlers.CpmToDateTime(trdPartyDateEntry.access3);
|
|
|
|
|
fInfo.CreationTime = DateHandlers.CpmToDateTime(ctime);
|
|
|
|
|
fInfo.LastWriteTime = DateHandlers.CpmToDateTime(trdPartyDateEntry.modify3);
|
|
|
|
|
|
|
|
|
|
_statCache.Add(file3, fInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file1 = null;
|
|
|
|
|
file2 = null;
|
|
|
|
|
file3 = null;
|
|
|
|
|
dirCnt = 0;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2023-10-03 23:22:08 +01:00
|
|
|
}
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Cache all files. As CP/M maximum volume size is 8 Mib
|
|
|
|
|
// this should not be a problem
|
2025-08-17 06:11:22 +01:00
|
|
|
AaruLogging.Debug(MODULE_NAME, "Reading files.");
|
2022-03-06 13:29:38 +00:00
|
|
|
long usedBlocks = 0;
|
|
|
|
|
_fileCache = new Dictionary<string, byte[]>();
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
foreach(string filename in _dirList)
|
|
|
|
|
{
|
|
|
|
|
var fileMs = new MemoryStream();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(_statCache.TryGetValue(filename, out FileEntryInfo fInfo)) _statCache.Remove(filename);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
fInfo.Blocks = 0;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(fileExtents.TryGetValue(filename, out Dictionary<int, List<ushort>> extents))
|
2023-10-03 23:22:08 +01:00
|
|
|
{
|
2025-10-22 14:28:58 +01:00
|
|
|
for(var ex = 0; ex < extents.Count; ex++)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!extents.TryGetValue(ex, out List<ushort> alBlks)) continue;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
foreach(ushort alBlk in alBlks)
|
|
|
|
|
{
|
|
|
|
|
allocationBlocks.TryGetValue(alBlk, out byte[] blk);
|
|
|
|
|
fileMs.Write(blk, 0, blk.Length);
|
|
|
|
|
fInfo.Blocks++;
|
|
|
|
|
}
|
2016-08-26 01:45:58 +01:00
|
|
|
}
|
2023-10-03 23:22:08 +01:00
|
|
|
}
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// If you insist to call CP/M "extent based"
|
|
|
|
|
fInfo.Attributes |= FileAttributes.Extents;
|
|
|
|
|
fInfo.BlockSize = blockSize;
|
|
|
|
|
fInfo.Length = fileMs.Length;
|
|
|
|
|
_cpmStat.Files++;
|
|
|
|
|
usedBlocks += fInfo.Blocks;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
_statCache.Add(filename, fInfo);
|
|
|
|
|
_fileCache.Add(filename, fileMs.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_decodedPasswordCache = new Dictionary<string, byte[]>();
|
2017-12-27 23:55:59 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// For each stored password, store a decoded version of it
|
2024-05-06 04:34:43 +01:00
|
|
|
foreach(KeyValuePair<string, byte[]> kvp in _passwordCache)
|
2023-10-03 23:22:08 +01:00
|
|
|
{
|
2025-10-22 14:28:58 +01:00
|
|
|
var tmp = new byte[8];
|
2024-05-06 04:34:43 +01:00
|
|
|
Array.Copy(kvp.Value, 16, tmp, 0, 8);
|
2017-12-27 23:55:59 +00:00
|
|
|
|
2025-10-22 14:28:58 +01:00
|
|
|
for(var t = 0; t < 8; t++) tmp[t] ^= kvp.Value[13];
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2024-05-06 04:34:43 +01:00
|
|
|
_decodedPasswordCache.Add(kvp.Key, tmp);
|
2023-10-03 23:22:08 +01:00
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Generate statfs.
|
|
|
|
|
_cpmStat.Blocks = (ulong)(_dpb.dsm + 1);
|
|
|
|
|
_cpmStat.FilenameLength = 11;
|
|
|
|
|
_cpmStat.Files = (ulong)_fileCache.Count;
|
|
|
|
|
_cpmStat.FreeBlocks = _cpmStat.Blocks - (ulong)usedBlocks;
|
|
|
|
|
_cpmStat.PluginId = Id;
|
2022-11-28 02:59:53 +00:00
|
|
|
_cpmStat.Type = FS_TYPE;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
// Generate XML info
|
2022-12-15 22:21:07 +00:00
|
|
|
Metadata = new FileSystem
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-12-15 22:21:07 +00:00
|
|
|
Clusters = _cpmStat.Blocks,
|
|
|
|
|
ClusterSize = (uint)blockSize,
|
|
|
|
|
Files = (ulong)_fileCache.Count,
|
|
|
|
|
FreeClusters = _cpmStat.FreeBlocks,
|
|
|
|
|
Type = FS_TYPE
|
2022-03-06 13:29:38 +00:00
|
|
|
};
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(_labelCreationDate != null) Metadata.CreationDate = DateHandlers.CpmToDateTime(_labelCreationDate);
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(_labelUpdateDate != null) Metadata.ModificationDate = DateHandlers.CpmToDateTime(_labelUpdateDate);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!string.IsNullOrEmpty(_label)) Metadata.VolumeName = _label;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
_mounted = true;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ErrorNumber StatFs(out FileSystemInfo stat)
|
|
|
|
|
{
|
|
|
|
|
stat = null;
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!_mounted) return ErrorNumber.AccessDenied;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
stat = _cpmStat;
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ErrorNumber Unmount()
|
|
|
|
|
{
|
|
|
|
|
_mounted = false;
|
|
|
|
|
_definitions = null;
|
|
|
|
|
_cpmFound = false;
|
|
|
|
|
_workingDefinition = null;
|
|
|
|
|
_dpb = null;
|
|
|
|
|
_sectorMask = null;
|
|
|
|
|
_label = null;
|
|
|
|
|
_thirdPartyTimestamps = false;
|
|
|
|
|
_standardTimestamps = false;
|
|
|
|
|
_labelCreationDate = null;
|
|
|
|
|
_labelUpdateDate = null;
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
2016-08-26 01:43:15 +01:00
|
|
|
}
|
2023-10-03 23:22:08 +01:00
|
|
|
|
|
|
|
|
#endregion
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|