2016-07-21 17:36:51 +01:00
|
|
|
|
// /***************************************************************************
|
|
|
|
|
|
// The Disc Image Chef
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : Dir.cs
|
2016-07-28 18:13:49 +01:00
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
2016-07-21 17:36:51 +01:00
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
|
// Component : Apple Lisa filesystem plugin.
|
2016-07-21 17:36:51 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
|
// Methods to handle Apple Lisa filesystem catalogs (aka directories).
|
2016-07-21 17:36:51 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
|
// 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
|
2016-07-21 17:36:51 +01:00
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
|
// 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.
|
2016-07-21 17:36:51 +01:00
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
|
// 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/>.
|
2016-07-21 17:36:51 +01:00
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2016-07-28 18:13:49 +01:00
|
|
|
|
// Copyright © 2011-2016 Natalia Portillo
|
2016-07-21 17:36:51 +01:00
|
|
|
|
// ****************************************************************************/
|
2016-07-28 18:13:49 +01:00
|
|
|
|
|
2016-07-21 17:36:51 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
using DiscImageChef.ImagePlugins;
|
2016-07-21 17:36:51 +01:00
|
|
|
|
|
|
|
|
|
|
namespace DiscImageChef.Filesystems.LisaFS
|
|
|
|
|
|
{
|
|
|
|
|
|
partial class LisaFS : Filesystem
|
|
|
|
|
|
{
|
2016-07-29 02:22:24 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Solves a symbolic link.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="path">Link path.</param>
|
|
|
|
|
|
/// <param name="dest">Link destination.</param>
|
2016-07-21 17:36:51 +01:00
|
|
|
|
public override Errno ReadLink(string path, ref string dest)
|
|
|
|
|
|
{
|
2016-07-21 18:39:38 +01:00
|
|
|
|
// LisaFS does not support symbolic links (afaik)
|
|
|
|
|
|
return Errno.NotSupported;
|
2016-07-21 17:36:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-29 02:22:24 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Lists contents from a directory.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="path">Directory path.</param>
|
|
|
|
|
|
/// <param name="contents">Directory contents.</param>
|
2016-07-21 17:36:51 +01:00
|
|
|
|
public override Errno ReadDir(string path, ref List<string> contents)
|
|
|
|
|
|
{
|
2016-07-28 22:25:26 +01:00
|
|
|
|
short fileId;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
bool isDir;
|
|
|
|
|
|
Errno error = LookupFileId(path, out fileId, out isDir);
|
|
|
|
|
|
if(error != Errno.NoError)
|
|
|
|
|
|
return error;
|
|
|
|
|
|
|
|
|
|
|
|
if(!isDir)
|
|
|
|
|
|
return Errno.NotDirectory;
|
|
|
|
|
|
|
|
|
|
|
|
List<CatalogEntry> catalog;
|
|
|
|
|
|
ReadCatalog(fileId, out catalog);
|
|
|
|
|
|
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// Do same trick as Mac OS X, replace filesystem '/' with ':'
|
|
|
|
|
|
// Maybe as ':' is the path separator in Lisa OS I should do that
|
2016-07-22 02:18:53 +01:00
|
|
|
|
foreach(CatalogEntry entry in catalog)
|
2016-07-28 23:08:22 +01:00
|
|
|
|
contents.Add(GetString(entry.filename).Replace('/', ':'));
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// On debug add system files as readable files
|
|
|
|
|
|
// Syntax similar to NTFS
|
|
|
|
|
|
if(debug && fileId == FILEID_ROOTCATALOG)
|
2016-07-22 02:18:53 +01:00
|
|
|
|
{
|
|
|
|
|
|
contents.Add("$MDDF");
|
|
|
|
|
|
contents.Add("$Boot");
|
|
|
|
|
|
contents.Add("$Loader");
|
|
|
|
|
|
contents.Add("$Bitmap");
|
|
|
|
|
|
contents.Add("$S-Record");
|
|
|
|
|
|
contents.Add("$");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
contents.Sort();
|
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-29 02:22:24 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Lists contents from a catalog.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="fileId">Catalog id.</param>
|
|
|
|
|
|
/// <param name="catalog">Catalog contents.</param>
|
2016-07-28 22:25:26 +01:00
|
|
|
|
Errno ReadCatalog(short fileId, out List<CatalogEntry> catalog)
|
2016-07-22 02:18:53 +01:00
|
|
|
|
{
|
|
|
|
|
|
catalog = null;
|
|
|
|
|
|
|
|
|
|
|
|
if(!mounted)
|
|
|
|
|
|
return Errno.AccessDenied;
|
|
|
|
|
|
|
|
|
|
|
|
if(fileId < 4)
|
|
|
|
|
|
return Errno.InvalidArgument;
|
|
|
|
|
|
|
|
|
|
|
|
if(catalogCache.TryGetValue(fileId, out catalog))
|
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
|
|
2016-07-28 04:12:49 +01:00
|
|
|
|
Errno error;
|
|
|
|
|
|
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// Do differently for V1 and V2
|
2016-07-28 05:34:23 +01:00
|
|
|
|
if(mddf.fsversion == LisaFSv2 || mddf.fsversion == LisaFSv1)
|
2016-07-28 04:12:49 +01:00
|
|
|
|
{
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// V1 and V2 can only contain the root catalog
|
|
|
|
|
|
if(fileId != FILEID_ROOTCATALOG)
|
2016-07-28 04:12:49 +01:00
|
|
|
|
{
|
|
|
|
|
|
ExtentFile ext;
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// Check if it's a file to return correct error
|
2016-07-28 04:12:49 +01:00
|
|
|
|
error = ReadExtentsFile(fileId, out ext);
|
|
|
|
|
|
if(error == Errno.NoError)
|
|
|
|
|
|
return Errno.NotDirectory;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
byte[] buf;
|
|
|
|
|
|
error = ReadFile(4, out buf);
|
|
|
|
|
|
|
|
|
|
|
|
int offset = 0;
|
|
|
|
|
|
List<CatalogEntryV2> catalogV2 = new List<CatalogEntryV2>();
|
|
|
|
|
|
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// For each entry on the catalog
|
2016-07-28 04:12:49 +01:00
|
|
|
|
while(offset + 54 < buf.Length)
|
|
|
|
|
|
{
|
|
|
|
|
|
CatalogEntryV2 entV2 = new CatalogEntryV2();
|
|
|
|
|
|
entV2.filenameLen = buf[offset];
|
|
|
|
|
|
entV2.filename = new byte[E_NAME];
|
|
|
|
|
|
Array.Copy(buf, offset + 0x01, entV2.filename, 0, E_NAME);
|
|
|
|
|
|
entV2.unknown1 = buf[offset + 0x21];
|
|
|
|
|
|
entV2.fileType = buf[offset + 0x22];
|
|
|
|
|
|
entV2.unknown2 = buf[offset + 0x23];
|
|
|
|
|
|
entV2.fileID = BigEndianBitConverter.ToInt16(buf, offset + 0x24);
|
|
|
|
|
|
entV2.unknown3 = new byte[16];
|
|
|
|
|
|
Array.Copy(buf, offset + 0x26, entV2.unknown3, 0, 16);
|
|
|
|
|
|
|
|
|
|
|
|
offset += 54;
|
|
|
|
|
|
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// Check that the entry is correct, not empty or garbage
|
2016-07-28 04:12:49 +01:00
|
|
|
|
if(entV2.filenameLen != 0 && entV2.filenameLen <= E_NAME && entV2.fileType != 0 && entV2.fileID > 0)
|
|
|
|
|
|
catalogV2.Add(entV2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
catalog = new List<CatalogEntry>();
|
|
|
|
|
|
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// Convert entries to V3 format
|
2016-07-28 04:12:49 +01:00
|
|
|
|
foreach(CatalogEntryV2 entV2 in catalogV2)
|
|
|
|
|
|
{
|
|
|
|
|
|
ExtentFile ext;
|
|
|
|
|
|
error = ReadExtentsFile(entV2.fileID, out ext);
|
|
|
|
|
|
if(error == Errno.NoError)
|
|
|
|
|
|
{
|
|
|
|
|
|
CatalogEntry entV3 = new CatalogEntry();
|
|
|
|
|
|
entV3.fileID = entV2.fileID;
|
|
|
|
|
|
entV3.filename = new byte[32];
|
|
|
|
|
|
Array.Copy(entV2.filename, 0, entV3.filename, 0, entV2.filenameLen);
|
|
|
|
|
|
entV3.fileType = entV2.fileType;
|
|
|
|
|
|
entV3.length = (int)srecords[entV2.fileID].filesize;
|
|
|
|
|
|
entV3.dtc = ext.dtc;
|
|
|
|
|
|
entV3.dtm = ext.dtm;
|
|
|
|
|
|
|
|
|
|
|
|
catalog.Add(entV3);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
catalogCache.Add(fileId, catalog);
|
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-27 17:08:49 +01:00
|
|
|
|
byte[] firstCatalogBlock = null;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// Search for the first sector describing the catalog
|
|
|
|
|
|
// While root catalog is not stored in S-Records, probably rest are? (unchecked)
|
|
|
|
|
|
// If root catalog is not pointed in MDDF (unchecked) maybe it's always following S-Records File?
|
2016-07-22 02:18:53 +01:00
|
|
|
|
for(ulong i = 0; i < device.GetSectors(); i++)
|
|
|
|
|
|
{
|
2016-07-27 22:13:47 +01:00
|
|
|
|
Tag catTag;
|
|
|
|
|
|
DecodeTag(device.ReadSectorTag(i, SectorTagType.AppleSectorTag), out catTag);
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
2016-07-27 22:13:47 +01:00
|
|
|
|
if(catTag.fileID == fileId && catTag.relBlock == 0)
|
2016-07-27 17:08:49 +01:00
|
|
|
|
{
|
|
|
|
|
|
firstCatalogBlock = device.ReadSectors(i, 4);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// Found Extents File for a catalog, not allowable in V3, at least for root catalog
|
2016-07-27 22:13:47 +01:00
|
|
|
|
if(catTag.fileID == -fileId)
|
2016-07-22 02:18:53 +01:00
|
|
|
|
return Errno.NotDirectory;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// Catalog not found
|
2016-07-27 17:08:49 +01:00
|
|
|
|
if(firstCatalogBlock == null)
|
2016-07-22 02:18:53 +01:00
|
|
|
|
return Errno.NoSuchFile;
|
|
|
|
|
|
|
2016-07-27 17:08:49 +01:00
|
|
|
|
ulong prevCatalogPointer;
|
|
|
|
|
|
prevCatalogPointer = BigEndianBitConverter.ToUInt32(firstCatalogBlock, 0x7F6);
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// Traverse double-linked list until first catalog block
|
2016-07-27 17:08:49 +01:00
|
|
|
|
while(prevCatalogPointer != 0xFFFFFFFF)
|
2016-07-22 02:18:53 +01:00
|
|
|
|
{
|
2016-07-27 22:13:47 +01:00
|
|
|
|
Tag prevTag;
|
|
|
|
|
|
DecodeTag(device.ReadSectorTag(prevCatalogPointer + mddf.mddf_block + volumePrefix, SectorTagType.AppleSectorTag), out prevTag);
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
2016-07-27 22:13:47 +01:00
|
|
|
|
if(prevTag.fileID != fileId)
|
2016-07-27 17:08:49 +01:00
|
|
|
|
return Errno.InvalidArgument;
|
|
|
|
|
|
|
2016-07-27 22:13:47 +01:00
|
|
|
|
firstCatalogBlock = device.ReadSectors(prevCatalogPointer + mddf.mddf_block + volumePrefix, 4);
|
2016-07-27 17:08:49 +01:00
|
|
|
|
prevCatalogPointer = BigEndianBitConverter.ToUInt32(firstCatalogBlock, 0x7F6);
|
2016-07-22 02:18:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-27 17:08:49 +01:00
|
|
|
|
ulong nextCatalogPointer;
|
|
|
|
|
|
nextCatalogPointer = BigEndianBitConverter.ToUInt32(firstCatalogBlock, 0x7FA);
|
|
|
|
|
|
|
|
|
|
|
|
List<byte[]> catalogBlocks = new List<byte[]>();
|
|
|
|
|
|
catalogBlocks.Add(firstCatalogBlock);
|
|
|
|
|
|
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// Traverse double-linked list to read full catalog
|
2016-07-27 17:08:49 +01:00
|
|
|
|
while(nextCatalogPointer != 0xFFFFFFFF)
|
|
|
|
|
|
{
|
2016-07-27 22:13:47 +01:00
|
|
|
|
Tag nextTag;
|
|
|
|
|
|
DecodeTag(device.ReadSectorTag(nextCatalogPointer + mddf.mddf_block + volumePrefix, SectorTagType.AppleSectorTag), out nextTag);
|
2016-07-27 17:08:49 +01:00
|
|
|
|
|
2016-07-27 22:13:47 +01:00
|
|
|
|
if(nextTag.fileID != fileId)
|
2016-07-27 17:08:49 +01:00
|
|
|
|
return Errno.InvalidArgument;
|
|
|
|
|
|
|
2016-07-27 22:13:47 +01:00
|
|
|
|
byte[] nextCatalogBlock = device.ReadSectors(nextCatalogPointer + mddf.mddf_block + volumePrefix, 4);
|
2016-07-27 17:08:49 +01:00
|
|
|
|
nextCatalogPointer = BigEndianBitConverter.ToUInt32(nextCatalogBlock, 0x7FA);
|
|
|
|
|
|
catalogBlocks.Add(nextCatalogBlock);
|
|
|
|
|
|
}
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
|
|
|
|
|
catalog = new List<CatalogEntry>();
|
|
|
|
|
|
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// Foreach catalog block
|
2016-07-27 17:08:49 +01:00
|
|
|
|
foreach(byte[] buf in catalogBlocks)
|
2016-07-22 02:18:53 +01:00
|
|
|
|
{
|
2016-07-27 17:08:49 +01:00
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// Traverse all entries
|
2016-07-27 17:08:49 +01:00
|
|
|
|
while((offset + 64) <= buf.Length)
|
2016-07-22 02:18:53 +01:00
|
|
|
|
{
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// Catalog block header
|
2016-07-27 17:08:49 +01:00
|
|
|
|
if(buf[offset + 0x24] == 0x08)
|
|
|
|
|
|
offset += 78;
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// Maybe just garbage? Found in more than 1 disk
|
2016-07-27 17:08:49 +01:00
|
|
|
|
else if(buf[offset + 0x24] == 0x7C)
|
|
|
|
|
|
offset += 50;
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// Apparently reserved to indicate end of catalog?
|
2016-07-27 17:08:49 +01:00
|
|
|
|
else if(buf[offset + 0x24] == 0xFF)
|
|
|
|
|
|
break;
|
2016-07-29 02:22:24 +01:00
|
|
|
|
// Normal entry
|
2016-07-27 17:08:49 +01:00
|
|
|
|
else if(buf[offset + 0x24] == 0x03 && buf[offset] == 0x24)
|
2016-07-27 13:33:41 +01:00
|
|
|
|
{
|
2016-07-27 17:08:49 +01:00
|
|
|
|
CatalogEntry entry = new CatalogEntry();
|
|
|
|
|
|
entry.marker = buf[offset];
|
|
|
|
|
|
entry.zero = BigEndianBitConverter.ToUInt16(buf, offset + 0x01);
|
|
|
|
|
|
entry.filename = new byte[E_NAME];
|
|
|
|
|
|
Array.Copy(buf, offset + 0x03, entry.filename, 0, E_NAME);
|
|
|
|
|
|
entry.terminator = buf[offset + 0x23];
|
|
|
|
|
|
entry.fileType = buf[offset + 0x24];
|
|
|
|
|
|
entry.unknown = buf[offset + 0x25];
|
|
|
|
|
|
entry.fileID = BigEndianBitConverter.ToInt16(buf, offset + 0x26);
|
|
|
|
|
|
entry.dtc = BigEndianBitConverter.ToUInt32(buf, offset + 0x28);
|
|
|
|
|
|
entry.dtm = BigEndianBitConverter.ToUInt32(buf, offset + 0x2C);
|
|
|
|
|
|
entry.length = BigEndianBitConverter.ToInt32(buf, offset + 0x30);
|
|
|
|
|
|
entry.wasted = BigEndianBitConverter.ToInt32(buf, offset + 0x34);
|
|
|
|
|
|
entry.tail = new byte[8];
|
|
|
|
|
|
Array.Copy(buf, offset + 0x38, entry.tail, 0, 8);
|
|
|
|
|
|
|
|
|
|
|
|
// This is as Pascal Workshop does, if there is no extents file it simply ignores it.
|
|
|
|
|
|
ExtentFile ext;
|
|
|
|
|
|
if(ReadExtentsFile(entry.fileID, out ext) == Errno.NoError)
|
2016-07-27 13:33:41 +01:00
|
|
|
|
{
|
2016-07-27 17:08:49 +01:00
|
|
|
|
if(!fileSizeCache.ContainsKey(entry.fileID))
|
|
|
|
|
|
{
|
|
|
|
|
|
catalog.Add(entry);
|
|
|
|
|
|
fileSizeCache.Add(entry.fileID, entry.length);
|
|
|
|
|
|
}
|
2016-07-27 13:33:41 +01:00
|
|
|
|
}
|
2016-07-22 02:18:53 +01:00
|
|
|
|
|
2016-07-27 17:08:49 +01:00
|
|
|
|
offset += 64;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
break;
|
2016-07-22 02:18:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
catalogCache.Add(fileId, catalog);
|
|
|
|
|
|
return Errno.NoError;
|
2016-07-21 17:36:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|