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

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