Files
Aaru/Aaru.Filesystems/CPM/File.cs

180 lines
5.8 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 : File.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : CP/M filesystem plugin.
//
// --[ 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
// ****************************************************************************/
using System;
2021-09-16 04:42:14 +01:00
using Aaru.CommonTypes.Enums;
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;
namespace Aaru.Filesystems;
2022-03-06 13:29:38 +00:00
public sealed partial class CPM
{
#region IReadOnlyFilesystem Members
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber GetAttributes(string path, out FileAttributes attributes)
{
2022-03-06 13:29:38 +00:00
attributes = new FileAttributes();
2024-05-01 04:05:22 +01:00
if(!_mounted) return ErrorNumber.AccessDenied;
2022-03-06 13:29:38 +00:00
string[] pathElements = path.Split(new[]
2024-05-01 04:05:22 +01:00
{
'/'
},
StringSplitOptions.RemoveEmptyEntries);
2024-05-01 04:05:22 +01:00
if(pathElements.Length != 1) return ErrorNumber.NotSupported;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(string.IsNullOrEmpty(pathElements[0]) ||
string.Compare(pathElements[0], "/", StringComparison.OrdinalIgnoreCase) == 0)
{
attributes = new FileAttributes();
attributes = FileAttributes.Directory;
return ErrorNumber.NoError;
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(!_statCache.TryGetValue(pathElements[0].ToUpperInvariant(), out FileEntryInfo fInfo))
return ErrorNumber.NoSuchFile;
2022-03-06 13:29:38 +00:00
attributes = fInfo.Attributes;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
/// <inheritdoc />
public ErrorNumber OpenFile(string path, out IFileNode node)
{
node = null;
2024-05-01 04:05:22 +01:00
if(!_mounted) return ErrorNumber.AccessDenied;
string[] pathElements = path.Split(new[]
2024-05-01 04:05:22 +01:00
{
'/'
},
StringSplitOptions.RemoveEmptyEntries);
2024-05-01 04:05:22 +01:00
if(pathElements.Length != 1) return ErrorNumber.NotSupported;
2024-05-01 04:05:22 +01:00
if(!_fileCache.TryGetValue(pathElements[0].ToUpperInvariant(), out byte[] file)) return ErrorNumber.NoSuchFile;
node = new CpmFileNode
{
Path = path,
Length = file.Length,
Offset = 0,
2023-10-05 01:52:48 +01:00
Cache = file
};
return ErrorNumber.NoError;
}
/// <inheritdoc />
public ErrorNumber CloseFile(IFileNode node)
{
2024-05-01 04:05:22 +01:00
if(!_mounted) return ErrorNumber.AccessDenied;
2024-05-01 04:05:22 +01:00
if(node is not CpmFileNode mynode) return ErrorNumber.InvalidArgument;
2023-10-05 01:52:48 +01:00
mynode.Cache = null;
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadFile(IFileNode node, long length, byte[] buffer, out long read)
2022-03-06 13:29:38 +00:00
{
read = 0;
2024-05-01 04:05:22 +01:00
if(!_mounted) return ErrorNumber.AccessDenied;
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 CpmFileNode mynode) return ErrorNumber.InvalidArgument;
read = length;
2024-05-01 04:05:22 +01:00
if(length + mynode.Offset >= mynode.Length) read = mynode.Length - mynode.Offset;
2020-02-29 18:03:35 +00:00
2023-10-05 01:52:48 +01:00
Array.Copy(mynode.Cache, mynode.Offset, buffer, 0, read);
mynode.Offset += read;
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadLink(string path, out string dest)
{
dest = null;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return !_mounted ? ErrorNumber.AccessDenied : ErrorNumber.NotSupported;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Stat(string path, out FileEntryInfo stat)
{
stat = null;
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
if(!_mounted) return ErrorNumber.AccessDenied;
string[] pathElements = path.Split(new[]
2024-05-01 04:05:22 +01:00
{
'/'
},
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;
2020-02-29 18:03:35 +00:00
if(!string.IsNullOrEmpty(path) && string.Compare(path, "/", StringComparison.OrdinalIgnoreCase) != 0)
{
return _statCache.TryGetValue(pathElements[0].ToUpperInvariant(), out stat)
? ErrorNumber.NoError
2022-03-06 13:29:38 +00:00
: ErrorNumber.NoSuchFile;
}
2022-03-06 13:29:38 +00:00
stat = new FileEntryInfo
{
Attributes = FileAttributes.Directory,
BlockSize = Metadata.ClusterSize
2022-03-06 13:29:38 +00:00
};
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
if(_labelCreationDate != null) stat.CreationTime = DateHandlers.CpmToDateTime(_labelCreationDate);
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
if(_labelUpdateDate != null) stat.StatusChangeTime = DateHandlers.CpmToDateTime(_labelUpdateDate);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
#endregion
2017-12-19 20:33:03 +00:00
}