2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2016-10-07 00:41:59 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// 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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2018-12-29 17:34:38 +00:00
|
|
|
// Copyright © 2011-2019 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;
|
2018-06-25 19:08:16 +01:00
|
|
|
using DiscImageChef.CommonTypes.Structs;
|
2019-03-01 07:35:22 +00:00
|
|
|
using DiscImageChef.Helpers;
|
2018-06-25 19:08:16 +01:00
|
|
|
using FileAttributes = DiscImageChef.CommonTypes.Structs.FileAttributes;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
namespace DiscImageChef.Filesystems.AppleDOS
|
|
|
|
|
{
|
2017-12-21 16:27:09 +00:00
|
|
|
public partial class AppleDOS
|
2016-10-07 00:41:59 +01:00
|
|
|
{
|
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();
|
2017-12-19 20:33:03 +00:00
|
|
|
if(!mounted) return Errno.AccessDenied;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
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;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
string filename = pathElements[0].ToUpperInvariant();
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(!fileCache.ContainsKey(filename)) 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;
|
2017-12-19 20:33:03 +00:00
|
|
|
if(lockedFiles.Contains(filename)) attributes |= FileAttributes.ReadOnly;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
if(debug && (string.Compare(path, "$", StringComparison.InvariantCulture) == 0 ||
|
2017-12-19 20:33:03 +00:00
|
|
|
string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0 ||
|
2017-12-24 02:37:41 +00:00
|
|
|
string.Compare(path, "$Vtoc", StringComparison.InvariantCulture) == 0))
|
|
|
|
|
attributes |= FileAttributes.System;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
if(!mounted) return Errno.AccessDenied;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
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;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
byte[] file;
|
|
|
|
|
string filename = pathElements[0].ToUpperInvariant();
|
2017-12-19 20:33:03 +00:00
|
|
|
if(filename.Length > 30) return Errno.NameTooLong;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
if(debug && (string.Compare(path, "$", StringComparison.InvariantCulture) == 0 ||
|
2017-12-19 20:33:03 +00:00
|
|
|
string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0 ||
|
|
|
|
|
string.Compare(path, "$Vtoc", StringComparison.InvariantCulture) == 0))
|
2019-04-23 01:38:33 +01:00
|
|
|
if(string.Compare(path, "$", StringComparison.InvariantCulture) == 0) file = catalogBlocks;
|
2017-12-24 02:37:41 +00:00
|
|
|
else if(string.Compare(path, "$Vtoc", StringComparison.InvariantCulture) == 0) file = vtocBlocks;
|
2018-06-22 08:08:38 +01:00
|
|
|
else file = bootBlocks;
|
2016-10-07 00:41:59 +01:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(!fileCache.TryGetValue(filename, out file))
|
|
|
|
|
{
|
2017-12-22 07:05:08 +00:00
|
|
|
Errno error = CacheFile(filename);
|
2017-12-19 20:33:03 +00:00
|
|
|
if(error != Errno.NoError) return error;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(!fileCache.TryGetValue(filename, out file)) return Errno.InvalidArgument;
|
2016-10-07 00:41:59 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(offset >= file.Length) return Errno.InvalidArgument;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2017-12-19 20:33:03 +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;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2017-12-19 20:33:03 +00:00
|
|
|
if(!mounted) return Errno.AccessDenied;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
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;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
string filename = pathElements[0].ToUpperInvariant();
|
2017-12-19 20:33:03 +00:00
|
|
|
if(filename.Length > 30) return Errno.NameTooLong;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(!fileCache.ContainsKey(filename)) 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
|
|
|
|
2017-12-22 07:05:08 +00: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
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
if(debug && (string.Compare(path, "$", StringComparison.InvariantCulture) == 0 ||
|
2017-12-19 20:33:03 +00:00
|
|
|
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)
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stat.Attributes = attrs;
|
2018-06-22 08:08:38 +01:00
|
|
|
stat.BlockSize = vtoc.bytesPerSector;
|
|
|
|
|
stat.Links = 1;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2016-10-07 00:41:59 +01:00
|
|
|
// TODO: Not really important.
|
2017-12-22 07:05:08 +00:00
|
|
|
return !mounted ? Errno.AccessDenied : Errno.NotImplemented;
|
2016-10-07 00:41:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Errno CacheFile(string path)
|
|
|
|
|
{
|
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;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
string filename = pathElements[0].ToUpperInvariant();
|
2017-12-19 20:33:03 +00:00
|
|
|
if(filename.Length > 30) return Errno.NameTooLong;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2017-12-22 07:05:08 +00:00
|
|
|
if(!catalogCache.TryGetValue(filename, out ushort ts)) return Errno.NoSuchFile;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
ulong lba = (ulong)(((ts & 0xFF00) >> 8) * sectorsPerTrack + (ts & 0xFF));
|
|
|
|
|
MemoryStream fileMs = new MemoryStream();
|
|
|
|
|
MemoryStream tsListMs = new MemoryStream();
|
|
|
|
|
ushort expectedBlock = 0;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
while(lba != 0)
|
|
|
|
|
{
|
|
|
|
|
usedSectors++;
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] tsSectorB = device.ReadSector(lba);
|
|
|
|
|
if(debug) 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)
|
|
|
|
|
{
|
|
|
|
|
byte[] hole = new byte[(tsSector.sectorOffset - expectedBlock) * vtoc.bytesPerSector];
|
|
|
|
|
fileMs.Write(hole, 0, hole.Length);
|
|
|
|
|
expectedBlock = tsSector.sectorOffset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach(TrackSectorListEntry entry in tsSector.entries)
|
|
|
|
|
{
|
|
|
|
|
track1UsedByFiles |= entry.track == 1;
|
|
|
|
|
track2UsedByFiles |= entry.track == 2;
|
|
|
|
|
usedSectors++;
|
|
|
|
|
|
2017-12-20 17:26:28 +00:00
|
|
|
ulong blockLba = (ulong)(entry.track * sectorsPerTrack + entry.sector);
|
2017-12-19 20:33:03 +00:00
|
|
|
if(blockLba == 0) break;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
byte[] fileBlock = device.ReadSector(blockLba);
|
|
|
|
|
fileMs.Write(fileBlock, 0, fileBlock.Length);
|
|
|
|
|
expectedBlock++;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 17:26:28 +00:00
|
|
|
lba = (ulong)(tsSector.nextListTrack * sectorsPerTrack + tsSector.nextListSector);
|
2016-10-07 00:41:59 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(fileCache.ContainsKey(filename)) fileCache.Remove(filename);
|
|
|
|
|
if(extentCache.ContainsKey(filename)) extentCache.Remove(filename);
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
fileCache.Add(filename, fileMs.ToArray());
|
|
|
|
|
extentCache.Add(filename, tsListMs.ToArray());
|
|
|
|
|
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Errno CacheAllFiles()
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
fileCache = new Dictionary<string, byte[]>();
|
2016-10-07 00:41:59 +01:00
|
|
|
extentCache = new Dictionary<string, byte[]>();
|
|
|
|
|
|
2017-12-22 07:05:08 +00: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;
|
2017-12-19 20:33:03 +00:00
|
|
|
if(!track1UsedByFiles) tracksOnBoot++;
|
|
|
|
|
if(!track2UsedByFiles) tracksOnBoot++;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
bootBlocks = device.ReadSectors(0, (uint)(tracksOnBoot * sectorsPerTrack));
|
2019-04-23 01:38:33 +01:00
|
|
|
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
|
|
|
}
|