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 : File.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
2016-07-31 20:56:53 +01:00
|
|
|
// Component : U.C.S.D. Pascal filesystem plugin.
|
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:56:53 +01:00
|
|
|
|
2016-07-31 20:55:58 +01:00
|
|
|
using System;
|
2017-12-21 07:08:26 +00:00
|
|
|
using System.Linq;
|
2021-09-16 04:42:14 +01:00
|
|
|
using Aaru.CommonTypes.Enums;
|
2022-12-19 00:26:55 +00:00
|
|
|
using Aaru.CommonTypes.Interfaces;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.CommonTypes.Structs;
|
2020-07-20 15:43:52 +01:00
|
|
|
using Aaru.Helpers;
|
2025-08-14 15:51:32 +01:00
|
|
|
using FileAttributes = Aaru.CommonTypes.Structs.FileAttributes;
|
2016-07-31 20:56:53 +01:00
|
|
|
|
2022-12-14 20:56:08 +00:00
|
|
|
namespace Aaru.Filesystems;
|
2022-11-15 15:58:43 +00:00
|
|
|
|
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
|
|
|
{
|
2025-10-22 14:28:58 +01:00
|
|
|
ErrorNumber GetFileEntry(string path, out PascalFileEntry entry)
|
|
|
|
|
{
|
|
|
|
|
entry = new PascalFileEntry();
|
|
|
|
|
|
2025-11-24 20:27:29 +00:00
|
|
|
foreach(PascalFileEntry ent in _fileEntries.Where(ent => string.Equals(path,
|
|
|
|
|
StringHandlers
|
|
|
|
|
.PascalToString(ent.Filename,
|
|
|
|
|
_encoding),
|
|
|
|
|
StringComparison
|
|
|
|
|
.InvariantCultureIgnoreCase)))
|
2025-10-22 14:28:58 +01:00
|
|
|
{
|
|
|
|
|
entry = ent;
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoSuchFile;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-03 23:22:08 +01:00
|
|
|
#region IReadOnlyFilesystem Members
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ErrorNumber GetAttributes(string path, out FileAttributes attributes)
|
|
|
|
|
{
|
|
|
|
|
attributes = new FileAttributes();
|
2016-07-31 20:56:53 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!_mounted) return ErrorNumber.AccessDenied;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2025-11-24 03:08:01 +00:00
|
|
|
string[] pathElements = path.Split(['/'], StringSplitOptions.RemoveEmptyEntries);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(pathElements.Length != 1) return ErrorNumber.NotSupported;
|
2016-07-31 20:56:53 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
ErrorNumber error = GetFileEntry(path, out _);
|
2016-07-31 20:56:53 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(error != ErrorNumber.NoError) return error;
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
attributes = FileAttributes.File;
|
2016-07-31 20:56:53 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 00:26:55 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ErrorNumber OpenFile(string path, out IFileNode node)
|
|
|
|
|
{
|
|
|
|
|
node = null;
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!_mounted) return ErrorNumber.AccessDenied;
|
2022-12-19 00:26:55 +00:00
|
|
|
|
2025-11-24 03:08:01 +00:00
|
|
|
string[] pathElements = path.Split(['/'], StringSplitOptions.RemoveEmptyEntries);
|
2022-12-19 00:26:55 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(pathElements.Length != 1) return ErrorNumber.NotSupported;
|
2022-12-19 00:26:55 +00:00
|
|
|
|
|
|
|
|
byte[] file;
|
|
|
|
|
|
2023-10-04 17:34:40 +01:00
|
|
|
if(_debug &&
|
2025-11-24 20:27:29 +00:00
|
|
|
(string.Equals(path, "$", StringComparison.InvariantCulture) ||
|
|
|
|
|
string.Equals(path, "$Boot", StringComparison.InvariantCulture)))
|
|
|
|
|
file = string.Equals(path, "$", StringComparison.InvariantCulture) ? _catalogBlocks : _bootBlocks;
|
2022-12-19 00:26:55 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ErrorNumber error = GetFileEntry(path, out PascalFileEntry entry);
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(error != ErrorNumber.NoError) return error;
|
2022-12-19 00:26:55 +00:00
|
|
|
|
2025-10-23 03:07:43 +01:00
|
|
|
error = _device.ReadSectors((ulong)entry.FirstBlock * _multiplier,
|
|
|
|
|
false,
|
2024-05-01 04:05:22 +01:00
|
|
|
(uint)(entry.LastBlock - entry.FirstBlock) * _multiplier,
|
2025-10-22 14:28:58 +01:00
|
|
|
out byte[] tmp,
|
|
|
|
|
out _);
|
2022-12-19 00:26:55 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(error != ErrorNumber.NoError) return error;
|
2022-12-19 00:26:55 +00:00
|
|
|
|
2023-10-03 23:22:08 +01:00
|
|
|
file = new byte[(entry.LastBlock - entry.FirstBlock - 1) * _device.Info.SectorSize * _multiplier +
|
2022-12-19 00:26:55 +00:00
|
|
|
entry.LastBytes];
|
|
|
|
|
|
|
|
|
|
Array.Copy(tmp, 0, file, 0, file.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node = new PascalFileNode
|
|
|
|
|
{
|
|
|
|
|
Path = path,
|
|
|
|
|
Length = file.Length,
|
|
|
|
|
Offset = 0,
|
2023-10-05 01:52:48 +01:00
|
|
|
Cache = file
|
2022-12-19 00:26:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ErrorNumber CloseFile(IFileNode node)
|
|
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!_mounted) return ErrorNumber.AccessDenied;
|
2022-12-19 00:26:55 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(node is not PascalFileNode mynode) return ErrorNumber.InvalidArgument;
|
2022-12-19 00:26:55 +00:00
|
|
|
|
2023-10-05 01:52:48 +01:00
|
|
|
mynode.Cache = null;
|
2022-12-19 00:26:55 +00:00
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
2022-12-19 11:03:51 +00:00
|
|
|
public ErrorNumber ReadFile(IFileNode node, long length, byte[] buffer, out long read)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-12-19 11:03:51 +00:00
|
|
|
read = 0;
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!_mounted) return ErrorNumber.AccessDenied;
|
2016-07-31 20:56:53 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(buffer is null || buffer.Length < length) return ErrorNumber.InvalidArgument;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(node is not PascalFileNode mynode) return ErrorNumber.InvalidArgument;
|
2016-07-31 20:56:53 +01:00
|
|
|
|
2022-12-19 11:03:51 +00:00
|
|
|
read = length;
|
2016-07-31 20:56:53 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(length + mynode.Offset >= mynode.Length) read = mynode.Length - mynode.Offset;
|
2016-07-31 20:56:53 +01:00
|
|
|
|
2023-10-05 01:52:48 +01:00
|
|
|
Array.Copy(mynode.Cache, mynode.Offset, buffer, 0, read);
|
2022-12-19 11:03:51 +00:00
|
|
|
mynode.Offset += read;
|
2016-07-31 20:56:53 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
2016-07-31 20:56:53 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ErrorNumber Stat(string path, out FileEntryInfo stat)
|
|
|
|
|
{
|
|
|
|
|
stat = null;
|
2016-07-31 20:56:53 +01:00
|
|
|
|
2025-11-24 03:08:01 +00:00
|
|
|
string[] pathElements = path.Split(['/'], StringSplitOptions.RemoveEmptyEntries);
|
2016-07-31 20:56:53 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(pathElements.Length != 1) return ErrorNumber.NotSupported;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
if(_debug)
|
2023-10-03 23:22:08 +01:00
|
|
|
{
|
2025-11-24 20:27:29 +00:00
|
|
|
if(string.Equals(path, "$", StringComparison.InvariantCulture) ||
|
|
|
|
|
string.Equals(path, "$Boot", StringComparison.InvariantCulture))
|
2020-02-29 18:03:35 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
stat = new FileEntryInfo
|
|
|
|
|
{
|
|
|
|
|
Attributes = FileAttributes.System,
|
|
|
|
|
BlockSize = _device.Info.SectorSize * _multiplier,
|
|
|
|
|
Links = 1
|
|
|
|
|
};
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2025-11-24 20:27:29 +00:00
|
|
|
if(string.Equals(path, "$", StringComparison.InvariantCulture))
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2023-10-03 23:22:08 +01:00
|
|
|
stat.Blocks = _catalogBlocks.Length / stat.BlockSize + _catalogBlocks.Length % stat.BlockSize;
|
2016-07-31 20:56:53 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
stat.Length = _catalogBlocks.Length;
|
|
|
|
|
}
|
|
|
|
|
else
|
2016-07-31 22:12:48 +01:00
|
|
|
{
|
2023-10-03 23:22:08 +01:00
|
|
|
stat.Blocks = _bootBlocks.Length / stat.BlockSize + _catalogBlocks.Length % stat.BlockSize;
|
2022-03-06 13:29:38 +00:00
|
|
|
stat.Length = _bootBlocks.Length;
|
2016-07-31 22:12:48 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
2023-10-03 23:22:08 +01:00
|
|
|
}
|
2016-07-31 20:56:53 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
ErrorNumber error = GetFileEntry(path, out PascalFileEntry entry);
|
2016-07-31 20:56:53 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(error != ErrorNumber.NoError) return error;
|
2016-07-31 20:56:53 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
stat = new FileEntryInfo
|
2016-07-31 20:56:53 +01:00
|
|
|
{
|
2022-03-07 07:36:44 +00:00
|
|
|
Attributes = FileAttributes.File,
|
|
|
|
|
Blocks = entry.LastBlock - entry.FirstBlock,
|
|
|
|
|
BlockSize = _device.Info.SectorSize * _multiplier,
|
2022-03-06 13:29:38 +00:00
|
|
|
LastWriteTimeUtc = DateHandlers.UcsdPascalToDateTime(entry.ModificationTime),
|
2023-10-03 23:22:08 +01:00
|
|
|
Length = (entry.LastBlock - entry.FirstBlock) * _device.Info.SectorSize * _multiplier + entry.LastBytes,
|
2022-03-06 13:29:38 +00:00
|
|
|
Links = 1
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2023-10-03 23:22:08 +01:00
|
|
|
#endregion
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|