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

170 lines
6.8 KiB
C#
Raw Normal View History

2017-05-19 20:28:49 +01:00
// /***************************************************************************
2016-07-31 20:55:58 +01:00
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// 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/>.
//
// ----------------------------------------------------------------------------
2018-12-29 17:34:38 +00:00
// Copyright © 2011-2019 Natalia Portillo
2016-07-31 20:55:58 +01:00
// ****************************************************************************/
2016-07-31 20:55:58 +01:00
using System;
using System.Linq;
using DiscImageChef.CommonTypes.Structs;
2016-07-31 20:55:58 +01:00
namespace DiscImageChef.Filesystems.UCSDPascal
{
// Information from Call-A.P.P.L.E. Pascal Disk Directory Structure
public partial class PascalPlugin
2016-07-31 20:55:58 +01:00
{
public Errno MapBlock(string path, long fileBlock, out long deviceBlock)
{
deviceBlock = 0;
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();
2017-12-19 20:33:03 +00:00
if(!mounted) return Errno.AccessDenied;
2017-12-21 14:30:38 +00:00
string[] pathElements = path.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
2017-12-19 20:33:03 +00:00
if(pathElements.Length != 1) return Errno.NotSupported;
Errno error = GetFileEntry(path, out _);
if(error != Errno.NoError) return error;
attributes = FileAttributes.File;
return error;
}
public Errno Read(string path, long offset, long size, ref byte[] buf)
{
2017-12-19 20:33:03 +00:00
if(!mounted) return Errno.AccessDenied;
2017-12-21 14:30:38 +00:00
string[] pathElements = path.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
2017-12-19 20:33:03 +00:00
if(pathElements.Length != 1) return Errno.NotSupported;
byte[] file;
if(debug && (string.Compare(path, "$", StringComparison.InvariantCulture) == 0 ||
2017-12-19 20:33:03 +00:00
string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0))
2018-06-22 08:08:38 +01:00
file = string.Compare(path, "$", StringComparison.InvariantCulture) == 0 ? catalogBlocks : bootBlocks;
else
{
Errno error = GetFileEntry(path, out PascalFileEntry entry);
2017-12-19 20:33:03 +00:00
if(error != Errno.NoError) return error;
2018-06-22 08:08:38 +01:00
byte[] tmp = device.ReadSectors((ulong)entry.FirstBlock * multiplier,
(uint)(entry.LastBlock - entry.FirstBlock) * multiplier);
file = new byte[(entry.LastBlock - entry.FirstBlock - 1) * device.Info.SectorSize * multiplier +
entry.LastBytes];
Array.Copy(tmp, 0, file, 0, file.Length);
}
2017-12-19 20:33:03 +00:00
if(offset >= file.Length) return Errno.EINVAL;
2017-12-19 20:33:03 +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;
2017-12-21 14:30:38 +00:00
string[] pathElements = path.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
2017-12-19 20:33:03 +00:00
if(pathElements.Length != 1) return Errno.NotSupported;
if(debug)
if(string.Compare(path, "$", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0)
{
stat = new FileEntryInfo
{
Attributes = FileAttributes.System,
BlockSize = device.Info.SectorSize * multiplier,
Links = 1
};
if(string.Compare(path, "$", StringComparison.InvariantCulture) == 0)
{
stat.Blocks = catalogBlocks.Length / stat.BlockSize + catalogBlocks.Length % stat.BlockSize;
stat.Length = catalogBlocks.Length;
}
else
{
stat.Blocks = bootBlocks.Length / stat.BlockSize + catalogBlocks.Length % stat.BlockSize;
stat.Length = bootBlocks.Length;
}
return Errno.NoError;
}
Errno error = GetFileEntry(path, out PascalFileEntry entry);
2017-12-19 20:33:03 +00:00
if(error != Errno.NoError) return error;
stat = new FileEntryInfo
{
Attributes = FileAttributes.File,
Blocks = entry.LastBlock - entry.FirstBlock,
BlockSize = device.Info.SectorSize * multiplier,
LastWriteTimeUtc = DateHandlers.UcsdPascalToDateTime(entry.ModificationTime),
2018-06-22 08:08:38 +01:00
Length = (entry.LastBlock - entry.FirstBlock) * device.Info.SectorSize * multiplier +
2018-12-31 13:17:27 +00:00
entry.LastBytes,
Links = 1
};
return Errno.NoError;
}
Errno GetFileEntry(string path, out PascalFileEntry entry)
{
entry = new PascalFileEntry();
foreach(PascalFileEntry ent in fileEntries.Where(ent =>
string.Compare(path,
StringHandlers
.PascalToString(ent.Filename,
Encoding),
StringComparison
.InvariantCultureIgnoreCase) == 0))
{
entry = ent;
return Errno.NoError;
}
return Errno.NoSuchFile;
2016-07-31 20:55:58 +01:00
}
}
2017-12-19 20:33:03 +00:00
}