Files
Aaru/Aaru.Filesystems/UCSDPascal/File.cs

200 lines
7.2 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
2016-07-31 20:55:58 +01:00
// ----------------------------------------------------------------------------
//
// Filename : File.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 ] ----------------------------------------------------------
//
// Methods to handle files.
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/>.
//
// ----------------------------------------------------------------------------
2020-01-03 17:51:30 +00:00
// Copyright © 2011-2020 Natalia Portillo
2016-07-31 20:55:58 +01:00
// ****************************************************************************/
2016-07-31 20:55:58 +01:00
using System;
using System.Linq;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Structs;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
2020-02-27 00:33:26 +00:00
namespace Aaru.Filesystems.UCSDPascal
2016-07-31 20:55:58 +01:00
{
// Information from Call-A.P.P.L.E. Pascal Disk Directory Structure
2020-07-22 13:20:25 +01:00
public sealed partial class PascalPlugin
2016-07-31 20:55:58 +01:00
{
public Errno MapBlock(string path, long fileBlock, out long deviceBlock)
{
deviceBlock = 0;
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
return !_mounted ? Errno.AccessDenied : Errno.NotImplemented;
}
public Errno GetAttributes(string path, out FileAttributes attributes)
2016-07-31 20:55:58 +01:00
{
attributes = new FileAttributes();
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;
Errno error = GetFileEntry(path, out _);
2020-02-29 18:03:35 +00:00
if(error != Errno.NoError)
return error;
attributes = FileAttributes.File;
return error;
}
public Errno Read(string path, long offset, long size, ref byte[] buf)
{
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);
2020-02-29 18:03:35 +00:00
if(pathElements.Length != 1)
return Errno.NotSupported;
byte[] file;
2020-07-20 21:11:32 +01:00
if(_debug && (string.Compare(path, "$", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0))
file = string.Compare(path, "$", StringComparison.InvariantCulture) == 0 ? _catalogBlocks : _bootBlocks;
else
{
Errno error = GetFileEntry(path, out PascalFileEntry entry);
2020-02-29 18:03:35 +00:00
if(error != Errno.NoError)
return error;
2020-07-20 21:11:32 +01:00
byte[] tmp = _device.ReadSectors((ulong)entry.FirstBlock * _multiplier,
(uint)(entry.LastBlock - entry.FirstBlock) * _multiplier);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
file = new byte[((entry.LastBlock - entry.FirstBlock - 1) * _device.Info.SectorSize * _multiplier) +
entry.LastBytes];
2020-02-29 18:03:35 +00:00
Array.Copy(tmp, 0, file, 0, file.Length);
}
2020-02-29 18:03:35 +00:00
if(offset >= file.Length)
return Errno.EINVAL;
2020-02-29 18:03:35 +00:00
if(size + offset >= file.Length)
size = file.Length - offset;
buf = new byte[size];
Array.Copy(file, offset, buf, 0, size);
return Errno.NoError;
}
public Errno Stat(string path, out FileEntryInfo stat)
{
stat = null;
2020-02-29 18:03:35 +00:00
string[] pathElements = path.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
if(pathElements.Length != 1)
return Errno.NotSupported;
2020-07-20 21:11:32 +01:00
if(_debug)
2020-02-29 18:03:35 +00:00
if(string.Compare(path, "$", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0)
{
stat = new FileEntryInfo
{
2020-07-20 04:34:16 +01:00
Attributes = FileAttributes.System,
2020-07-20 21:11:32 +01:00
BlockSize = _device.Info.SectorSize * _multiplier,
2020-07-20 04:34:16 +01:00
Links = 1
};
if(string.Compare(path, "$", StringComparison.InvariantCulture) == 0)
{
2020-07-20 21:11:32 +01:00
stat.Blocks = (_catalogBlocks.Length / stat.BlockSize) +
(_catalogBlocks.Length % stat.BlockSize);
stat.Length = _catalogBlocks.Length;
}
else
{
2020-07-20 21:11:32 +01:00
stat.Blocks = (_bootBlocks.Length / stat.BlockSize) + (_catalogBlocks.Length % stat.BlockSize);
stat.Length = _bootBlocks.Length;
}
return Errno.NoError;
}
Errno error = GetFileEntry(path, out PascalFileEntry entry);
2020-02-29 18:03:35 +00:00
if(error != Errno.NoError)
return error;
stat = new FileEntryInfo
{
2020-07-20 21:11:32 +01:00
Attributes = FileAttributes.File,
Blocks = entry.LastBlock - entry.FirstBlock,
BlockSize = _device.Info.SectorSize * _multiplier,
LastWriteTimeUtc = DateHandlers.UcsdPascalToDateTime(entry.ModificationTime),
2020-07-20 21:11:32 +01:00
Length = ((entry.LastBlock - entry.FirstBlock) * _device.Info.SectorSize * _multiplier) +
entry.LastBytes,
Links = 1
};
return Errno.NoError;
}
Errno GetFileEntry(string path, out PascalFileEntry entry)
{
entry = new PascalFileEntry();
2020-07-20 21:11:32 +01:00
foreach(PascalFileEntry ent in _fileEntries.Where(ent =>
string.Compare(path,
StringHandlers.
PascalToString(ent.Filename,
Encoding),
StringComparison.
InvariantCultureIgnoreCase) == 0))
{
entry = ent;
2020-02-29 18:03:35 +00:00
return Errno.NoError;
}
return Errno.NoSuchFile;
2016-07-31 20:55:58 +01:00
}
}
2017-12-19 20:33:03 +00:00
}