Files
Aaru/Aaru.Filesystems/AppleDOS/File.cs

305 lines
9.9 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 : Apple DOS filesystem plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// Methods to handle files.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2021-09-16 04:42:14 +01:00
using Aaru.CommonTypes.Enums;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Structs;
using Aaru.Helpers;
using FileAttributes = Aaru.CommonTypes.Structs.FileAttributes;
namespace Aaru.Filesystems;
2022-03-06 13:29:38 +00:00
public sealed partial class AppleDOS
{
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();
2022-03-06 13:29:38 +00:00
if(!_mounted)
return ErrorNumber.AccessDenied;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
string[] pathElements = path.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(pathElements.Length != 1)
return ErrorNumber.NotSupported;
2022-03-06 13:29:38 +00:00
string filename = pathElements[0].ToUpperInvariant();
2022-03-06 13:29:38 +00:00
if(!_fileCache.ContainsKey(filename))
return ErrorNumber.NoSuchFile;
2022-03-06 13:29:38 +00:00
attributes = FileAttributes.Extents;
attributes |= FileAttributes.File;
2022-03-06 13:29:38 +00:00
if(_lockedFiles.Contains(filename))
attributes |= FileAttributes.ReadOnly;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(_debug && (string.Compare(path, "$", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$Vtoc", StringComparison.InvariantCulture) == 0))
attributes |= FileAttributes.System;
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
/// <inheritdoc />
public ErrorNumber Read(string path, long offset, long size, ref byte[] buf)
{
if(!_mounted)
return ErrorNumber.AccessDenied;
2022-03-06 13:29:38 +00:00
string[] pathElements = path.Split(new[]
{
2022-03-06 13:29:38 +00:00
'/'
}, StringSplitOptions.RemoveEmptyEntries);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(pathElements.Length != 1)
return ErrorNumber.NotSupported;
byte[] file;
string filename = pathElements[0].ToUpperInvariant();
if(filename.Length > 30)
return ErrorNumber.NameTooLong;
if(_debug && (string.Compare(path, "$", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$Vtoc", StringComparison.InvariantCulture) == 0))
if(string.Compare(path, "$", StringComparison.InvariantCulture) == 0)
file = _catalogBlocks;
else if(string.Compare(path, "$Vtoc", StringComparison.InvariantCulture) == 0)
file = _vtocBlocks;
else
2022-03-06 13:29:38 +00:00
file = _bootBlocks;
else
{
if(!_fileCache.TryGetValue(filename, out file))
{
2022-03-06 13:29:38 +00:00
ErrorNumber error = CacheFile(filename);
2022-03-06 13:29:38 +00:00
if(error != ErrorNumber.NoError)
return error;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(!_fileCache.TryGetValue(filename, out file))
return ErrorNumber.InvalidArgument;
}
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(offset >= file.Length)
return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
if(size + offset >= file.Length)
size = file.Length - offset;
2022-03-06 13:29:38 +00:00
buf = new byte[size];
2022-03-06 13:29:38 +00:00
Array.Copy(file, offset, buf, 0, size);
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Stat(string path, out FileEntryInfo stat)
{
stat = null;
2022-03-06 13:29:38 +00:00
if(!_mounted)
return ErrorNumber.AccessDenied;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
string[] pathElements = path.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(pathElements.Length != 1)
return ErrorNumber.NotSupported;
2022-03-06 13:29:38 +00:00
string filename = pathElements[0].ToUpperInvariant();
2022-03-06 13:29:38 +00:00
if(filename.Length > 30)
return ErrorNumber.NameTooLong;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(!_fileCache.ContainsKey(filename))
return ErrorNumber.NoSuchFile;
2022-03-06 13:29:38 +00:00
stat = new FileEntryInfo();
2022-03-06 13:29:38 +00:00
_fileSizeCache.TryGetValue(filename, out int fileSize);
GetAttributes(path, out FileAttributes attrs);
2022-03-06 13:29:38 +00:00
if(_debug && (string.Compare(path, "$", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$Vtoc", StringComparison.InvariantCulture) == 0))
{
if(string.Compare(path, "$", StringComparison.InvariantCulture) == 0)
stat.Length = _catalogBlocks.Length;
else if(string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0)
stat.Length = _bootBlocks.Length;
else if(string.Compare(path, "$Vtoc", StringComparison.InvariantCulture) == 0)
stat.Length = _vtocBlocks.Length;
stat.Blocks = stat.Length / _vtoc.bytesPerSector;
}
else
{
stat.Length = fileSize;
stat.Blocks = stat.Length / _vtoc.bytesPerSector;
}
2022-03-06 13:29:38 +00:00
stat.Attributes = attrs;
stat.BlockSize = _vtoc.bytesPerSector;
stat.Links = 1;
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber MapBlock(string path, long fileBlock, out long deviceBlock)
{
deviceBlock = 0;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
// TODO: Not really important.
return !_mounted ? ErrorNumber.AccessDenied : ErrorNumber.NotImplemented;
}
2022-03-06 13:29:38 +00:00
ErrorNumber CacheFile(string path)
{
string[] pathElements = path.Split(new[]
{
2022-03-06 13:29:38 +00:00
'/'
}, StringSplitOptions.RemoveEmptyEntries);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(pathElements.Length != 1)
return ErrorNumber.NotSupported;
2022-03-06 13:29:38 +00:00
string filename = pathElements[0].ToUpperInvariant();
2022-03-06 13:29:38 +00:00
if(filename.Length > 30)
return ErrorNumber.NameTooLong;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(!_catalogCache.TryGetValue(filename, out ushort ts))
return ErrorNumber.NoSuchFile;
ulong lba = (ulong)((((ts & 0xFF00) >> 8) * _sectorsPerTrack) + (ts & 0xFF));
2022-03-06 13:29:38 +00:00
var fileMs = new MemoryStream();
var tsListMs = new MemoryStream();
ushort expectedBlock = 0;
2022-03-06 13:29:38 +00:00
while(lba != 0)
{
_usedSectors++;
ErrorNumber errno = _device.ReadSector(lba, out byte[] tsSectorB);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
2022-03-06 13:29:38 +00:00
if(_debug)
tsListMs.Write(tsSectorB, 0, tsSectorB.Length);
2022-03-06 13:29:38 +00:00
// Read the track/sector list sector
TrackSectorList tsSector = Marshal.ByteArrayToStructureLittleEndian<TrackSectorList>(tsSectorB);
2022-03-06 13:29:38 +00:00
if(tsSector.sectorOffset > expectedBlock)
{
byte[] hole = new byte[(tsSector.sectorOffset - expectedBlock) * _vtoc.bytesPerSector];
2022-03-06 13:29:38 +00:00
fileMs.Write(hole, 0, hole.Length);
expectedBlock = tsSector.sectorOffset;
}
2022-03-06 13:29:38 +00:00
foreach(TrackSectorListEntry entry in tsSector.entries)
{
_track1UsedByFiles |= entry.track == 1;
_track2UsedByFiles |= entry.track == 2;
_usedSectors++;
2020-02-29 18:03:35 +00:00
ulong blockLba = (ulong)((entry.track * _sectorsPerTrack) + entry.sector);
2022-03-06 13:29:38 +00:00
if(blockLba == 0)
break;
2022-03-06 13:29:38 +00:00
errno = _device.ReadSector(blockLba, out byte[] fileBlock);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
2022-03-06 13:29:38 +00:00
fileMs.Write(fileBlock, 0, fileBlock.Length);
expectedBlock++;
}
lba = (ulong)((tsSector.nextListTrack * _sectorsPerTrack) + tsSector.nextListSector);
2022-03-06 13:29:38 +00:00
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(_fileCache.ContainsKey(filename))
_fileCache.Remove(filename);
2022-03-06 13:29:38 +00:00
if(_extentCache.ContainsKey(filename))
_extentCache.Remove(filename);
2022-03-06 13:29:38 +00:00
_fileCache.Add(filename, fileMs.ToArray());
_extentCache.Add(filename, tsListMs.ToArray());
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
ErrorNumber CacheAllFiles()
{
_fileCache = new Dictionary<string, byte[]>();
_extentCache = new Dictionary<string, byte[]>();
2022-03-07 07:36:44 +00:00
foreach(ErrorNumber error in _catalogCache.Keys.Select(CacheFile).Where(error => error != ErrorNumber.NoError))
2022-03-06 13:29:38 +00:00
return error;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
uint tracksOnBoot = 1;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(!_track1UsedByFiles)
tracksOnBoot++;
2022-03-06 13:29:38 +00:00
if(!_track2UsedByFiles)
tracksOnBoot++;
2022-03-06 13:29:38 +00:00
ErrorNumber errno = _device.ReadSectors(0, (uint)(tracksOnBoot * _sectorsPerTrack), out _bootBlocks);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
2022-03-06 13:29:38 +00:00
_usedSectors += (uint)(_bootBlocks.Length / _vtoc.bytesPerSector);
return ErrorNumber.NoError;
}
2017-12-19 20:33:03 +00:00
}