2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2016-10-07 00:41:59 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// 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
|
2016-10-07 00:41:59 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2017-12-22 07:05:08 +00:00
|
|
|
using System.Linq;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.CommonTypes.Structs;
|
|
|
|
|
using Aaru.Helpers;
|
|
|
|
|
using FileAttributes = Aaru.CommonTypes.Structs.FileAttributes;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2020-07-20 15:43:52 +01:00
|
|
|
namespace Aaru.Filesystems
|
2016-10-07 00:41:59 +01:00
|
|
|
{
|
2020-07-22 13:20:25 +01:00
|
|
|
public sealed partial class AppleDOS
|
2016-10-07 00:41:59 +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-10-07 00:41:59 +01:00
|
|
|
{
|
2017-12-26 08:17:28 +00:00
|
|
|
attributes = new FileAttributes();
|
2016-10-07 00:41:59 +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-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
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;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
attributes = FileAttributes.Extents;
|
2016-10-07 00:41:59 +01:00
|
|
|
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))
|
2017-12-24 02:37:41 +00:00
|
|
|
attributes |= FileAttributes.System;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
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-10-07 00:41:59 +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);
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(pathElements.Length != 1)
|
|
|
|
|
return Errno.NotSupported;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
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;
|
2016-10-07 00:41:59 +01:00
|
|
|
else
|
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
if(!_fileCache.TryGetValue(filename, out file))
|
2016-10-07 00:41:59 +01:00
|
|
|
{
|
2017-12-22 07:05:08 +00:00
|
|
|
Errno error = CacheFile(filename);
|
2016-10-07 00:41:59 +01:00
|
|
|
|
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;
|
2016-10-07 00:41:59 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(offset >= file.Length)
|
|
|
|
|
return Errno.InvalidArgument;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
if(size + offset >= file.Length)
|
|
|
|
|
size = file.Length - offset;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
buf = new byte[size];
|
|
|
|
|
|
|
|
|
|
Array.Copy(file, offset, buf, 0, size);
|
|
|
|
|
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
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-10-07 00:41:59 +01:00
|
|
|
{
|
2017-12-26 08:17:28 +00:00
|
|
|
stat = null;
|
2016-10-07 00:41:59 +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-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
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;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2017-12-26 08:17:28 +00:00
|
|
|
stat = new FileEntryInfo();
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2020-07-22 13:20:25 +01:00
|
|
|
_fileSizeCache.TryGetValue(filename, out int fileSize);
|
2017-12-26 08:17:28 +00:00
|
|
|
GetAttributes(path, out FileAttributes attrs);
|
2016-10-07 00:41:59 +01:00
|
|
|
|
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))
|
2016-10-07 00:41:59 +01:00
|
|
|
{
|
|
|
|
|
if(string.Compare(path, "$", StringComparison.InvariantCulture) == 0)
|
2020-07-20 21:11:32 +01:00
|
|
|
stat.Length = _catalogBlocks.Length;
|
2016-10-07 00:41:59 +01:00
|
|
|
else if(string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0)
|
2020-07-20 21:11:32 +01:00
|
|
|
stat.Length = _bootBlocks.Length;
|
2016-10-07 00:41:59 +01:00
|
|
|
else if(string.Compare(path, "$Vtoc", StringComparison.InvariantCulture) == 0)
|
2020-07-20 21:11:32 +01:00
|
|
|
stat.Length = _vtocBlocks.Length;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
stat.Blocks = stat.Length / _vtoc.bytesPerSector;
|
2016-10-07 00:41:59 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2016-10-07 00:41:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
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-10-07 00:41:59 +01:00
|
|
|
{
|
2017-12-26 08:17:28 +00:00
|
|
|
deviceBlock = 0;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2016-10-07 00:41:59 +01:00
|
|
|
// TODO: Not really important.
|
2020-07-20 21:11:32 +01:00
|
|
|
return !_mounted ? Errno.AccessDenied : Errno.NotImplemented;
|
2016-10-07 00:41:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
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;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
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;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
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);
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
// Read the track/sector list sector
|
2019-03-01 07:35:22 +00:00
|
|
|
TrackSectorList tsSector = Marshal.ByteArrayToStructureLittleEndian<TrackSectorList>(tsSectorB);
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
if(tsSector.sectorOffset > expectedBlock)
|
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
byte[] hole = new byte[(tsSector.sectorOffset - expectedBlock) * _vtoc.bytesPerSector];
|
2016-10-07 00:41:59 +01:00
|
|
|
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++;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
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;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
byte[] fileBlock = _device.ReadSector(blockLba);
|
2016-10-07 00:41:59 +01:00
|
|
|
fileMs.Write(fileBlock, 0, fileBlock.Length);
|
|
|
|
|
expectedBlock++;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
lba = (ulong)((tsSector.nextListTrack * _sectorsPerTrack) + tsSector.nextListSector);
|
2016-10-07 00:41:59 +01:00
|
|
|
}
|
|
|
|
|
|
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);
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
_fileCache.Add(filename, fileMs.ToArray());
|
|
|
|
|
_extentCache.Add(filename, tsListMs.ToArray());
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Errno CacheAllFiles()
|
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
_fileCache = new Dictionary<string, byte[]>();
|
|
|
|
|
_extentCache = new Dictionary<string, byte[]>();
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
foreach(Errno error in _catalogCache.Keys.Select(CacheFile).Where(error => error != Errno.NoError))
|
2017-12-24 02:37:41 +00:00
|
|
|
return error;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
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++;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
_bootBlocks = _device.ReadSectors(0, (uint)(tracksOnBoot * _sectorsPerTrack));
|
|
|
|
|
_usedSectors += (uint)(_bootBlocks.Length / _vtoc.bytesPerSector);
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|