Changed catalog algorithm to handle fragmented catalogs.

This commit is contained in:
2016-07-27 17:08:49 +01:00
parent 5915185f70
commit aa903865c5

View File

@@ -93,45 +93,67 @@ namespace DiscImageChef.Filesystems.LisaFS
if(catalogCache.TryGetValue(fileId, out catalog)) if(catalogCache.TryGetValue(fileId, out catalog))
return Errno.NoError; return Errno.NoError;
int count = 0; byte[] firstCatalogBlock = null;
// Catalogs don't have extents files so we need to traverse all disk searching pieces (tend to be non fragmented and non expandable)
for(ulong i = 0; i < device.GetSectors(); i++) for(ulong i = 0; i < device.GetSectors(); i++)
{ {
byte[] tag = device.ReadSectorTag((ulong)i, SectorTagType.AppleSectorTag); byte[] tag = device.ReadSectorTag(i, SectorTagType.AppleSectorTag);
UInt16 id = BigEndianBitConverter.ToUInt16(tag, 0x04); UInt16 id = BigEndianBitConverter.ToUInt16(tag, 0x04);
UInt16 pos = BigEndianBitConverter.ToUInt16(tag, 0x06);
if(id == fileId) if(id == fileId && pos == 0)
count++; {
firstCatalogBlock = device.ReadSectors(i, 4);
break;
}
// Extents file found, it's not a catalog
if(id == -fileId) if(id == -fileId)
return Errno.NotDirectory; return Errno.NotDirectory;
} }
if(count == 0) if(firstCatalogBlock == null)
return Errno.NoSuchFile; return Errno.NoSuchFile;
byte[] buf = new byte[count * device.GetSectorSize()]; ulong prevCatalogPointer;
prevCatalogPointer = BigEndianBitConverter.ToUInt32(firstCatalogBlock, 0x7F6);
// This can be enhanced to follow linked tags. However on some disks a linked tag cuts a file, better not let it do with a catalog while(prevCatalogPointer != 0xFFFFFFFF)
for(ulong i = 0; i < device.GetSectors(); i++)
{ {
byte[] tag = device.ReadSectorTag((ulong)i, SectorTagType.AppleSectorTag); byte[] tag = device.ReadSectorTag(prevCatalogPointer + mddf.mddf_block, SectorTagType.AppleSectorTag);
UInt16 id = BigEndianBitConverter.ToUInt16(tag, 0x04); UInt16 id = BigEndianBitConverter.ToUInt16(tag, 0x04);
if(id == fileId) if(id != fileId)
{ return Errno.InvalidArgument;
UInt16 pos = BigEndianBitConverter.ToUInt16(tag, 0x06);
byte[] sector = device.ReadSector(i); firstCatalogBlock = device.ReadSectors(prevCatalogPointer + mddf.mddf_block, 4);
Array.Copy(sector, 0, buf, sector.Length * pos, sector.Length); prevCatalogPointer = BigEndianBitConverter.ToUInt32(firstCatalogBlock, 0x7F6);
}
} }
int offset = 0; ulong nextCatalogPointer;
nextCatalogPointer = BigEndianBitConverter.ToUInt32(firstCatalogBlock, 0x7FA);
List<byte[]> catalogBlocks = new List<byte[]>();
catalogBlocks.Add(firstCatalogBlock);
while(nextCatalogPointer != 0xFFFFFFFF)
{
byte[] tag = device.ReadSectorTag(nextCatalogPointer + mddf.mddf_block, SectorTagType.AppleSectorTag);
UInt16 id = BigEndianBitConverter.ToUInt16(tag, 0x04);
if(id != fileId)
return Errno.InvalidArgument;
byte[] nextCatalogBlock = device.ReadSectors(nextCatalogPointer + mddf.mddf_block, 4);
nextCatalogPointer = BigEndianBitConverter.ToUInt32(nextCatalogBlock, 0x7FA);
catalogBlocks.Add(nextCatalogBlock);
}
catalog = new List<CatalogEntry>(); catalog = new List<CatalogEntry>();
foreach(byte[] buf in catalogBlocks)
{
int offset = 0;
while((offset + 64) <= buf.Length) while((offset + 64) <= buf.Length)
{ {
if(buf[offset + 0x24] == 0x08) if(buf[offset + 0x24] == 0x08)
@@ -174,6 +196,7 @@ namespace DiscImageChef.Filesystems.LisaFS
else else
break; break;
} }
}
catalogCache.Add(fileId, catalog); catalogCache.Add(fileId, catalog);
return Errno.NoError; return Errno.NoError;