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 : File.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
|
|
|
// Methods to handle files.
|
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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-12-31 23:08:23 +00:00
|
|
|
// Copyright © 2011-2021 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;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.CommonTypes.Structs;
|
2020-07-20 15:43:52 +01:00
|
|
|
using Aaru.Helpers;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2020-07-20 15:43:52 +01:00
|
|
|
namespace Aaru.Filesystems
|
2016-08-26 01:43:15 +01:00
|
|
|
{
|
2020-07-22 13:20:25 +01:00
|
|
|
public sealed partial class CPM
|
2016-08-26 01:43:15 +01:00
|
|
|
{
|
2021-08-17 13:56:05 +01:00
|
|
|
/// <inheritdoc />
|
2017-12-26 08:17:28 +00:00
|
|
|
public Errno GetAttributes(string path, out FileAttributes attributes)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
2017-12-26 08:17:28 +00:00
|
|
|
attributes = new FileAttributes();
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(!_mounted)
|
2020-02-29 18:03:35 +00:00
|
|
|
return Errno.AccessDenied;
|
|
|
|
|
|
|
|
|
|
string[] pathElements = path.Split(new[]
|
|
|
|
|
{
|
|
|
|
|
'/'
|
|
|
|
|
}, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
|
|
|
|
if(pathElements.Length != 1)
|
|
|
|
|
return Errno.NotSupported;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(string.IsNullOrEmpty(pathElements[0]) ||
|
|
|
|
|
string.Compare(pathElements[0], "/", StringComparison.OrdinalIgnoreCase) == 0)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
attributes = new FileAttributes();
|
|
|
|
|
attributes = FileAttributes.Directory;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-08-26 01:45:58 +01:00
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(!_statCache.TryGetValue(pathElements[0].ToUpperInvariant(), out FileEntryInfo fInfo))
|
2017-12-24 02:37:41 +00:00
|
|
|
return Errno.NoSuchFile;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
attributes = fInfo.Attributes;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
return Errno.NoError;
|
2016-08-26 01:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-22 08:43:22 +00:00
|
|
|
// TODO: Implementing this would require storing the interleaving
|
2021-08-17 13:56:05 +01:00
|
|
|
/// <inheritdoc />
|
2017-12-26 08:17:28 +00:00
|
|
|
public Errno MapBlock(string path, long fileBlock, out long deviceBlock)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
2017-12-26 08:17:28 +00:00
|
|
|
deviceBlock = 0;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
return !_mounted ? Errno.AccessDenied : Errno.NotImplemented;
|
2016-08-26 01:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
/// <inheritdoc />
|
2017-12-26 07:28:40 +00:00
|
|
|
public Errno Read(string path, long offset, long size, ref byte[] buf)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
if(!_mounted)
|
2020-02-29 18:03:35 +00:00
|
|
|
return Errno.AccessDenied;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
|
|
|
|
if(size == 0)
|
|
|
|
|
{
|
2021-08-17 18:21:12 +01:00
|
|
|
buf = Array.Empty<byte>();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-08-26 01:45:58 +01:00
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(offset < 0)
|
|
|
|
|
return Errno.InvalidArgument;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
string[] pathElements = path.Split(new[]
|
|
|
|
|
{
|
|
|
|
|
'/'
|
|
|
|
|
}, StringSplitOptions.RemoveEmptyEntries);
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(pathElements.Length != 1)
|
|
|
|
|
return Errno.NotSupported;
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(!_fileCache.TryGetValue(pathElements[0].ToUpperInvariant(), out byte[] file))
|
2020-02-29 18:03:35 +00:00
|
|
|
return Errno.NoSuchFile;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(offset >= file.Length)
|
|
|
|
|
return Errno.EINVAL;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(size + offset >= file.Length)
|
|
|
|
|
size = file.Length - offset;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
|
|
|
|
buf = new byte[size];
|
|
|
|
|
Array.Copy(file, offset, buf, 0, size);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-08-26 01:45:58 +01:00
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
/// <inheritdoc />
|
2017-12-26 08:17:28 +00:00
|
|
|
public Errno ReadLink(string path, out string dest)
|
2016-08-26 01:43:15 +01:00
|
|
|
{
|
2017-12-26 08:17:28 +00:00
|
|
|
dest = null;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
return !_mounted ? Errno.AccessDenied : Errno.NotSupported;
|
2016-08-26 01:43:15 +01:00
|
|
|
}
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
/// <inheritdoc />
|
2017-12-26 08:17:28 +00:00
|
|
|
public Errno Stat(string path, out FileEntryInfo stat)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
2017-12-26 08:17:28 +00:00
|
|
|
stat = null;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(!_mounted)
|
2020-02-29 18:03:35 +00:00
|
|
|
return Errno.AccessDenied;
|
|
|
|
|
|
|
|
|
|
string[] pathElements = path.Split(new[]
|
|
|
|
|
{
|
|
|
|
|
'/'
|
|
|
|
|
}, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
|
|
|
|
if(pathElements.Length != 1)
|
|
|
|
|
return Errno.NotSupported;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(!string.IsNullOrEmpty(path) &&
|
|
|
|
|
string.Compare(path, "/", StringComparison.OrdinalIgnoreCase) != 0)
|
2020-07-20 21:11:32 +01:00
|
|
|
return _statCache.TryGetValue(pathElements[0].ToUpperInvariant(), out stat) ? Errno.NoError
|
2017-12-26 08:17:28 +00:00
|
|
|
: Errno.NoSuchFile;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
stat = new FileEntryInfo
|
|
|
|
|
{
|
2020-07-20 04:34:16 +01:00
|
|
|
Attributes = FileAttributes.Directory,
|
|
|
|
|
BlockSize = XmlFsType.ClusterSize
|
2020-02-29 18:03:35 +00:00
|
|
|
};
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(_labelCreationDate != null)
|
|
|
|
|
stat.CreationTime = DateHandlers.CpmToDateTime(_labelCreationDate);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(_labelUpdateDate != null)
|
|
|
|
|
stat.StatusChangeTime = DateHandlers.CpmToDateTime(_labelUpdateDate);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2017-12-26 08:17:28 +00:00
|
|
|
return Errno.NoError;
|
2016-08-26 01:45:58 +01:00
|
|
|
}
|
2016-08-26 01:43:15 +01:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|