diff --git a/DiscImageChef.Filesystems/ChangeLog b/DiscImageChef.Filesystems/ChangeLog index 31c0df8cf..ccfee1605 100644 --- a/DiscImageChef.Filesystems/ChangeLog +++ b/DiscImageChef.Filesystems/ChangeLog @@ -1,3 +1,13 @@ +2016-07-31 Natalia Portillo + + * Dir.cs: + * File.cs: + * Super.cs: + * Xattr.cs: + * Consts.cs: + * LisaFS.cs: + * Structs.cs: Added support for subcatalogs. + 2016-07-29 Natalia Portillo * DiscImageChef.Filesystems.csproj: Bump to version 3.1.0. diff --git a/DiscImageChef.Filesystems/LisaFS/Consts.cs b/DiscImageChef.Filesystems/LisaFS/Consts.cs index 9a7cae88a..c9086ba91 100644 --- a/DiscImageChef.Filesystems/LisaFS/Consts.cs +++ b/DiscImageChef.Filesystems/LisaFS/Consts.cs @@ -80,7 +80,7 @@ namespace DiscImageChef.Filesystems.LisaFS /// const ushort FILEID_SRECORD = 0x0003; /// The root catalog - const ushort FILEID_ROOTCATALOG = 0x0004; + const ushort FILEID_CATALOG = 0x0004; const short FILEID_BOOT_SIGNED = -21846; const short FILEID_LOADER_SIGNED = -17477; /// @@ -89,6 +89,9 @@ namespace DiscImageChef.Filesystems.LisaFS const ushort FILEID_ERASED = 0x7FFF; const ushort FILEID_MAX = FILEID_ERASED; + /// Root directory ID + const short DIRID_ROOT = 0; + enum FileType : byte { /// diff --git a/DiscImageChef.Filesystems/LisaFS/Dir.cs b/DiscImageChef.Filesystems/LisaFS/Dir.cs index ba5702130..3ee24f38d 100644 --- a/DiscImageChef.Filesystems/LisaFS/Dir.cs +++ b/DiscImageChef.Filesystems/LisaFS/Dir.cs @@ -65,17 +65,16 @@ namespace DiscImageChef.Filesystems.LisaFS if(!isDir) return Errno.NotDirectory; - List catalog; - ReadCatalog(fileId, out catalog); + /*List catalog; + error = ReadCatalog(fileId, out catalog); + if(error != Errno.NoError) + return error;*/ - // Do same trick as Mac OS X, replace filesystem '/' with ':' - // Maybe as ':' is the path separator in Lisa OS I should do that - foreach(CatalogEntry entry in catalog) - contents.Add(GetString(entry.filename).Replace('/', ':')); + ReadDir(fileId, ref contents); // On debug add system files as readable files // Syntax similar to NTFS - if(debug && fileId == FILEID_ROOTCATALOG) + if(debug && fileId == DIRID_ROOT) { contents.Add("$MDDF"); contents.Add("$Boot"); @@ -89,41 +88,37 @@ namespace DiscImageChef.Filesystems.LisaFS return Errno.NoError; } - /// - /// Lists contents from a catalog. - /// - /// Catalog id. - /// Catalog contents. - Errno ReadCatalog(short fileId, out List catalog) + Errno ReadDir(short dirId, ref List contents) { - catalog = null; + contents = new List(); + foreach(CatalogEntry entry in catalogCache) + { + if(entry.parentID == dirId) + // Do same trick as Mac OS X, replace filesystem '/' with ':' + // Maybe as '-' is the path separator in Lisa OS I should do that + contents.Add(GetString(entry.filename).Replace('/', ':')); + } + return Errno.NoError; + } + + /// + /// Reads, interprets and caches the Catalog File + /// + Errno ReadCatalog() + { if(!mounted) return Errno.AccessDenied; - if(fileId < 4) - return Errno.InvalidArgument; - - if(catalogCache.TryGetValue(fileId, out catalog)) - return Errno.NoError; + catalogCache = new List(); Errno error; // Do differently for V1 and V2 if(mddf.fsversion == LisaFSv2 || mddf.fsversion == LisaFSv1) { - // V1 and V2 can only contain the root catalog - if(fileId != FILEID_ROOTCATALOG) - { - ExtentFile ext; - // Check if it's a file to return correct error - error = ReadExtentsFile(fileId, out ext); - if(error == Errno.NoError) - return Errno.NotDirectory; - } - byte[] buf; - error = ReadFile(4, out buf); + error = ReadFile((short)FILEID_CATALOG, out buf); int offset = 0; List catalogV2 = new List(); @@ -149,8 +144,6 @@ namespace DiscImageChef.Filesystems.LisaFS catalogV2.Add(entV2); } - catalog = new List(); - // Convert entries to V3 format foreach(CatalogEntryV2 entV2 in catalogV2) { @@ -167,11 +160,10 @@ namespace DiscImageChef.Filesystems.LisaFS entV3.dtc = ext.dtc; entV3.dtm = ext.dtm; - catalog.Add(entV3); + catalogCache.Add(entV3); } } - catalogCache.Add(fileId, catalog); return Errno.NoError; } @@ -185,15 +177,11 @@ namespace DiscImageChef.Filesystems.LisaFS Tag catTag; DecodeTag(device.ReadSectorTag(i, SectorTagType.AppleSectorTag), out catTag); - if(catTag.fileID == fileId && catTag.relBlock == 0) + if(catTag.fileID == FILEID_CATALOG && catTag.relBlock == 0) { firstCatalogBlock = device.ReadSectors(i, 4); break; } - - // Found Extents File for a catalog, not allowable in V3, at least for root catalog - if(catTag.fileID == -fileId) - return Errno.NotDirectory; } // Catalog not found @@ -209,7 +197,7 @@ namespace DiscImageChef.Filesystems.LisaFS Tag prevTag; DecodeTag(device.ReadSectorTag(prevCatalogPointer + mddf.mddf_block + volumePrefix, SectorTagType.AppleSectorTag), out prevTag); - if(prevTag.fileID != fileId) + if(prevTag.fileID != FILEID_CATALOG) return Errno.InvalidArgument; firstCatalogBlock = device.ReadSectors(prevCatalogPointer + mddf.mddf_block + volumePrefix, 4); @@ -228,7 +216,7 @@ namespace DiscImageChef.Filesystems.LisaFS Tag nextTag; DecodeTag(device.ReadSectorTag(nextCatalogPointer + mddf.mddf_block + volumePrefix, SectorTagType.AppleSectorTag), out nextTag); - if(nextTag.fileID != fileId) + if(nextTag.fileID != FILEID_CATALOG) return Errno.InvalidArgument; byte[] nextCatalogBlock = device.ReadSectors(nextCatalogPointer + mddf.mddf_block + volumePrefix, 4); @@ -236,8 +224,6 @@ namespace DiscImageChef.Filesystems.LisaFS catalogBlocks.Add(nextCatalogBlock); } - catalog = new List(); - // Foreach catalog block foreach(byte[] buf in catalogBlocks) { @@ -260,7 +246,7 @@ namespace DiscImageChef.Filesystems.LisaFS { CatalogEntry entry = new CatalogEntry(); entry.marker = buf[offset]; - entry.zero = BigEndianBitConverter.ToUInt16(buf, offset + 0x01); + entry.parentID = 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]; @@ -274,27 +260,75 @@ namespace DiscImageChef.Filesystems.LisaFS 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) { if(!fileSizeCache.ContainsKey(entry.fileID)) { - catalog.Add(entry); + catalogCache.Add(entry); fileSizeCache.Add(entry.fileID, entry.length); } } offset += 64; } + // Subdirectory entry + else if(buf[offset + 0x24] == 0x01 && buf[offset] == 0x24) + { + CatalogEntry entry = new CatalogEntry(); + entry.marker = buf[offset]; + entry.parentID = 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 = 0; + entry.wasted = 0; + entry.tail = null; + + if(!directoryDTCCache.ContainsKey(entry.fileID)) + directoryDTCCache.Add(entry.fileID, DateHandlers.LisaToDateTime(entry.dtc)); + + catalogCache.Add(entry); + + offset += 48; + } else break; } } - catalogCache.Add(fileId, catalog); + return Errno.NoError; + } + + Errno StatDir(short dirId, out FileEntryInfo stat) + { + stat = null; + + if(!mounted) + return Errno.AccessDenied; + + stat = new FileEntryInfo(); + stat.Attributes = new FileAttributes(); + DateTime tmp = new DateTime(); + + directoryDTCCache.TryGetValue(dirId, out tmp); + stat.CreationTime = tmp; + stat.Inode = FILEID_CATALOG; + stat.Mode = 0x16D; + stat.Links = 0; + stat.UID = 0; + stat.GID = 0; + stat.DeviceNo = 0; + stat.Length = 0; + stat.BlockSize = mddf.datasize; + stat.Blocks = 0; + return Errno.NoError; } } -} - +} \ No newline at end of file diff --git a/DiscImageChef.Filesystems/LisaFS/File.cs b/DiscImageChef.Filesystems/LisaFS/File.cs index 6b1f37af0..eb22d7cad 100644 --- a/DiscImageChef.Filesystems/LisaFS/File.cs +++ b/DiscImageChef.Filesystems/LisaFS/File.cs @@ -42,11 +42,18 @@ namespace DiscImageChef.Filesystems.LisaFS public override Errno GetAttributes(string path, ref FileAttributes attributes) { short fileId; - Errno error = LookupFileId(path, out fileId); + bool isDir; + Errno error = LookupFileId(path, out fileId, out isDir); if(error != Errno.NoError) return error; - return GetAttributes(fileId, ref attributes); + if(!isDir) + return GetAttributes(fileId, ref attributes); + + attributes = new FileAttributes(); + attributes = FileAttributes.Directory; + + return Errno.NoError; } public override Errno Read(string path, long offset, long size, ref byte[] buf) @@ -76,7 +83,7 @@ namespace DiscImageChef.Filesystems.LisaFS case (short)FILEID_MDDF: case (short)FILEID_BITMAP: case (short)FILEID_SRECORD: - case (short)FILEID_ROOTCATALOG: + case (short)FILEID_CATALOG: error = ReadSystemFile(fileId, out tmp); break; default: @@ -104,11 +111,12 @@ namespace DiscImageChef.Filesystems.LisaFS public override Errno Stat(string path, ref FileEntryInfo stat) { short fileId; - Errno error = LookupFileId(path, out fileId); + bool isDir; + Errno error = LookupFileId(path, out fileId, out isDir); if(error != Errno.NoError) return error; - return Stat(fileId, out stat); + return isDir ? StatDir(fileId, out stat) : Stat(fileId, out stat); } Errno GetAttributes(short fileId, ref FileAttributes attributes) @@ -116,23 +124,18 @@ namespace DiscImageChef.Filesystems.LisaFS if(!mounted) return Errno.AccessDenied; - if(fileId <= 4) + if(fileId < 4) { - if(!debug || fileId == 0) + if(!debug) return Errno.NoSuchFile; - else - { - attributes = new FileAttributes(); - attributes = FileAttributes.System; - attributes |= FileAttributes.Hidden; - if(fileId == 4) - attributes |= FileAttributes.Directory; - else - attributes |= FileAttributes.File; + attributes = new FileAttributes(); + attributes = FileAttributes.System; + attributes |= FileAttributes.Hidden; - return Errno.NoError; - } + attributes |= FileAttributes.File; + + return Errno.NoError; } ExtentFile extFile; @@ -159,7 +162,6 @@ namespace DiscImageChef.Filesystems.LisaFS break; default: attributes |= FileAttributes.File; - // Subcatalogs use extents? attributes |= FileAttributes.Extents; break; } @@ -435,12 +437,6 @@ namespace DiscImageChef.Filesystems.LisaFS return Errno.NoError; } - Errno LookupFileId(string path, out short fileId) - { - bool temp; - return LookupFileId(path, out fileId, out temp); - } - Errno LookupFileId(string path, out short fileId, out bool isDir) { fileId = 0; @@ -453,16 +449,16 @@ namespace DiscImageChef.Filesystems.LisaFS if(pathElements.Length == 0) { - fileId = (short)FILEID_ROOTCATALOG; + fileId = DIRID_ROOT; isDir = true; return Errno.NoError; } - // TODO: Subcatalogs - if(pathElements.Length > 1) - return Errno.NotImplemented; + // Only V3 supports subdirectories + if(pathElements.Length > 1 && mddf.fsversion != LisaFSv3) + return Errno.NotSupported; - if(debug) + if(debug && pathElements.Length == 1) { if(string.Compare(pathElements[0], "$MDDF", StringComparison.InvariantCulture) == 0) { @@ -496,29 +492,35 @@ namespace DiscImageChef.Filesystems.LisaFS if(string.Compare(pathElements[0], "$", StringComparison.InvariantCulture) == 0) { - fileId = (short)FILEID_ROOTCATALOG; + fileId = DIRID_ROOT; isDir = true; return Errno.NoError; } } - List catalog; - - Errno error = ReadCatalog((short)FILEID_ROOTCATALOG, out catalog); - if(error != Errno.NoError) - return error; - - string wantedFilename = pathElements[0].Replace(':', '/'); - - foreach(CatalogEntry entry in catalog) + for(int lvl = 0; lvl < pathElements.Length; lvl++) { - string filename = GetString(entry.filename); - // Should they be case sensitive? - if(string.Compare(wantedFilename, filename, StringComparison.InvariantCultureIgnoreCase) == 0) + string wantedFilename = pathElements[0].Replace(':', '/'); + + foreach(CatalogEntry entry in catalogCache) { - fileId = entry.fileID; - isDir |= entry.fileType != 0x03; - return Errno.NoError; + string filename = GetString(entry.filename); + + // LisaOS is case insensitive + if(string.Compare(wantedFilename, filename, StringComparison.InvariantCultureIgnoreCase) == 0 + && entry.parentID == fileId) + { + fileId = entry.fileID; + isDir = entry.fileType == 0x01; + + // Not last path element, and it's not a directory + if(lvl != pathElements.Length - 1 && !isDir) + return Errno.NotDirectory; + + // Arrived last path element + if(lvl == pathElements.Length - 1) + return Errno.NoError; + } } } diff --git a/DiscImageChef.Filesystems/LisaFS/LisaFS.cs b/DiscImageChef.Filesystems/LisaFS/LisaFS.cs index 838f2600f..e14c55493 100644 --- a/DiscImageChef.Filesystems/LisaFS/LisaFS.cs +++ b/DiscImageChef.Filesystems/LisaFS/LisaFS.cs @@ -58,11 +58,13 @@ namespace DiscImageChef.Filesystems.LisaFS /// Caches user files files Dictionary fileCache; /// Caches catalogs - Dictionary> catalogCache; + List catalogCache; /// Caches file size Dictionary fileSizeCache; /// Lists Extents Files already printed in debug mode to not repeat them List printedExtents; + /// Caches the creation times for subdirectories as to not have to traverse the Catalog File on each stat + Dictionary directoryDTCCache; #endregion Caches public LisaFS() diff --git a/DiscImageChef.Filesystems/LisaFS/Structs.cs b/DiscImageChef.Filesystems/LisaFS/Structs.cs index e70071cee..e980147e6 100644 --- a/DiscImageChef.Filesystems/LisaFS/Structs.cs +++ b/DiscImageChef.Filesystems/LisaFS/Structs.cs @@ -259,14 +259,15 @@ namespace DiscImageChef.Filesystems.LisaFS { /// 0x00, seems to be 0x24 when the entry is valid public byte marker; - /// 0x01, must be zero otherwise LisaOS gives an error - public ushort zero; + /// 0x01, parent directory ID for this file, 0 for root directory + public ushort parentID; /// 0x03, filename, 32-bytes, null-padded public byte[] filename; /// 0x23, null-termination public byte terminator; /// /// At 0x24 + /// 0x01 here for subdirectories, entries 48 bytes long /// 0x03 here for entries 64 bytes long /// 0x08 here for entries 78 bytes long /// This is incomplete, may fail, mostly works... diff --git a/DiscImageChef.Filesystems/LisaFS/Super.cs b/DiscImageChef.Filesystems/LisaFS/Super.cs index 3daa67546..8e4e09ee0 100644 --- a/DiscImageChef.Filesystems/LisaFS/Super.cs +++ b/DiscImageChef.Filesystems/LisaFS/Super.cs @@ -213,7 +213,7 @@ namespace DiscImageChef.Filesystems.LisaFS extentCache = new Dictionary(); systemFileCache = new Dictionary(); fileCache = new Dictionary(); - catalogCache = new Dictionary>(); + //catalogCache = new Dictionary>(); fileSizeCache = new Dictionary(); Errno error; @@ -234,13 +234,15 @@ namespace DiscImageChef.Filesystems.LisaFS return error; } - // Read the root catalog - List tempCat; - error = ReadCatalog((short)FILEID_ROOTCATALOG, out tempCat); + directoryDTCCache = new Dictionary(); + directoryDTCCache.Add(DIRID_ROOT, mddf.dtcc); + + // Read the Catalog File + error = ReadCatalog(); if(error != Errno.NoError) { - DicConsole.DebugWriteLine("LisaFS plugin", "Cannot read root catalog, error {0}", error.ToString()); + DicConsole.DebugWriteLine("LisaFS plugin", "Cannot read Catalog File, error {0}", error.ToString()); mounted = false; return error; } diff --git a/DiscImageChef.Filesystems/LisaFS/Xattr.cs b/DiscImageChef.Filesystems/LisaFS/Xattr.cs index b4b00ae88..ea973d05a 100644 --- a/DiscImageChef.Filesystems/LisaFS/Xattr.cs +++ b/DiscImageChef.Filesystems/LisaFS/Xattr.cs @@ -48,11 +48,12 @@ namespace DiscImageChef.Filesystems.LisaFS public override Errno ListXAttr(string path, ref List xattrs) { short fileId; - Errno error = LookupFileId(path, out fileId); + bool isDir; + Errno error = LookupFileId(path, out fileId, out isDir); if(error != Errno.NoError) return error; - return ListXAttr(fileId, ref xattrs); + return isDir ? Errno.InvalidArgument : ListXAttr(fileId, ref xattrs); } /// @@ -65,11 +66,12 @@ namespace DiscImageChef.Filesystems.LisaFS public override Errno GetXattr(string path, string xattr, ref byte[] buf) { short fileId; - Errno error = LookupFileId(path, out fileId); + bool isDir; + Errno error = LookupFileId(path, out fileId, out isDir); if(error != Errno.NoError) return error; - return GetXattr(fileId, xattr, out buf); + return isDir ? Errno.InvalidArgument : GetXattr(fileId, xattr, out buf); } ///