Move common data structures for MFS, HFS and ISO9660 (Apple Extensions) to common class.

This commit is contained in:
2020-02-19 03:37:51 +00:00
parent 177031d7ac
commit 999657502b
14 changed files with 459 additions and 351 deletions

View File

@@ -46,18 +46,27 @@ namespace DiscImageChef.Filesystems.AppleMFS
{
deviceBlock = new long();
if(!mounted) return Errno.AccessDenied;
if(!mounted)
return Errno.AccessDenied;
string[] pathElements = path.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
if(pathElements.Length != 1) return Errno.NotSupported;
string[] pathElements = path.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
if(pathElements.Length != 1)
return Errno.NotSupported;
path = pathElements[0];
if(!filenameToId.TryGetValue(path.ToLowerInvariant(), out uint fileId)) return Errno.NoSuchFile;
if(!filenameToId.TryGetValue(path.ToLowerInvariant(), out uint fileId))
return Errno.NoSuchFile;
if(!idToEntry.TryGetValue(fileId, out MFS_FileEntry entry)) return Errno.NoSuchFile;
if(!idToEntry.TryGetValue(fileId, out MFS_FileEntry entry))
return Errno.NoSuchFile;
if(fileBlock > entry.flPyLen / volMDB.drAlBlkSiz) return Errno.InvalidArgument;
if(fileBlock > entry.flPyLen / volMDB.drAlBlkSiz)
return Errno.InvalidArgument;
uint nextBlock = entry.flStBlk;
long relBlock = 0;
@@ -66,11 +75,14 @@ namespace DiscImageChef.Filesystems.AppleMFS
{
if(relBlock == fileBlock)
{
deviceBlock = (nextBlock - 2) * sectorsPerBlock + volMDB.drAlBlSt + (long)partitionStart;
deviceBlock = ((nextBlock - 2) * sectorsPerBlock) + volMDB.drAlBlSt + (long)partitionStart;
return Errno.NoError;
}
if(blockMap[nextBlock] == BMAP_FREE || blockMap[nextBlock] == BMAP_LAST) break;
if(blockMap[nextBlock] == BMAP_FREE ||
blockMap[nextBlock] == BMAP_LAST)
break;
nextBlock = blockMap[nextBlock];
relBlock++;
@@ -82,32 +94,60 @@ namespace DiscImageChef.Filesystems.AppleMFS
public Errno GetAttributes(string path, out FileAttributes attributes)
{
attributes = new FileAttributes();
if(!mounted) return Errno.AccessDenied;
string[] pathElements = path.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
if(pathElements.Length != 1) return Errno.NotSupported;
if(!mounted)
return Errno.AccessDenied;
string[] pathElements = path.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
if(pathElements.Length != 1)
return Errno.NotSupported;
path = pathElements[0];
if(!filenameToId.TryGetValue(path.ToLowerInvariant(), out uint fileId)) return Errno.NoSuchFile;
if(!filenameToId.TryGetValue(path.ToLowerInvariant(), out uint fileId))
return Errno.NoSuchFile;
if(!idToEntry.TryGetValue(fileId, out MFS_FileEntry entry)) return Errno.NoSuchFile;
if(!idToEntry.TryGetValue(fileId, out MFS_FileEntry entry))
return Errno.NoSuchFile;
MFS_FinderFlags fdFlags = (MFS_FinderFlags)BigEndianBitConverter.ToUInt16(entry.flUsrWds, 0x08);
if(entry.flUsrWds.fdFlags.HasFlag(AppleCommon.FinderFlags.kIsAlias))
attributes |= FileAttributes.Alias;
if(fdFlags.HasFlag(MFS_FinderFlags.kIsAlias)) attributes |= FileAttributes.Alias;
if(fdFlags.HasFlag(MFS_FinderFlags.kHasBundle)) attributes |= FileAttributes.Bundle;
if(fdFlags.HasFlag(MFS_FinderFlags.kHasBeenInited)) attributes |= FileAttributes.HasBeenInited;
if(fdFlags.HasFlag(MFS_FinderFlags.kHasCustomIcon)) attributes |= FileAttributes.HasCustomIcon;
if(fdFlags.HasFlag(MFS_FinderFlags.kHasNoINITs)) attributes |= FileAttributes.HasNoINITs;
if(fdFlags.HasFlag(MFS_FinderFlags.kIsInvisible)) attributes |= FileAttributes.Hidden;
if(entry.flFlags.HasFlag(MFS_FileFlags.Locked)) attributes |= FileAttributes.Immutable;
if(fdFlags.HasFlag(MFS_FinderFlags.kIsOnDesk)) attributes |= FileAttributes.IsOnDesk;
if(fdFlags.HasFlag(MFS_FinderFlags.kIsShared)) attributes |= FileAttributes.Shared;
if(fdFlags.HasFlag(MFS_FinderFlags.kIsStationery)) attributes |= FileAttributes.Stationery;
if(entry.flUsrWds.fdFlags.HasFlag(AppleCommon.FinderFlags.kHasBundle))
attributes |= FileAttributes.Bundle;
if(!attributes.HasFlag(FileAttributes.Alias) && !attributes.HasFlag(FileAttributes.Bundle) &&
!attributes.HasFlag(FileAttributes.Stationery)) attributes |= FileAttributes.File;
if(entry.flUsrWds.fdFlags.HasFlag(AppleCommon.FinderFlags.kHasBeenInited))
attributes |= FileAttributes.HasBeenInited;
if(entry.flUsrWds.fdFlags.HasFlag(AppleCommon.FinderFlags.kHasCustomIcon))
attributes |= FileAttributes.HasCustomIcon;
if(entry.flUsrWds.fdFlags.HasFlag(AppleCommon.FinderFlags.kHasNoINITs))
attributes |= FileAttributes.HasNoINITs;
if(entry.flUsrWds.fdFlags.HasFlag(AppleCommon.FinderFlags.kIsInvisible))
attributes |= FileAttributes.Hidden;
if(entry.flFlags.HasFlag(MFS_FileFlags.Locked))
attributes |= FileAttributes.Immutable;
if(entry.flUsrWds.fdFlags.HasFlag(AppleCommon.FinderFlags.kIsOnDesk))
attributes |= FileAttributes.IsOnDesk;
if(entry.flUsrWds.fdFlags.HasFlag(AppleCommon.FinderFlags.kIsShared))
attributes |= FileAttributes.Shared;
if(entry.flUsrWds.fdFlags.HasFlag(AppleCommon.FinderFlags.kIsStationery))
attributes |= FileAttributes.Stationery;
if(!attributes.HasFlag(FileAttributes.Alias) &&
!attributes.HasFlag(FileAttributes.Bundle) &&
!attributes.HasFlag(FileAttributes.Stationery))
attributes |= FileAttributes.File;
attributes |= FileAttributes.BlockUnits;
@@ -116,33 +156,40 @@ namespace DiscImageChef.Filesystems.AppleMFS
public Errno Read(string path, long offset, long size, ref byte[] buf)
{
if(!mounted) return Errno.AccessDenied;
if(!mounted)
return Errno.AccessDenied;
byte[] file;
Errno error = Errno.NoError;
if(debug && string.Compare(path, "$", StringComparison.InvariantCulture) == 0) file = directoryBlocks;
else if(debug && string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0 &&
bootBlocks != null)
if(debug && string.Compare(path, "$", StringComparison.InvariantCulture) == 0)
file = directoryBlocks;
else if(debug &&
string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0 &&
bootBlocks != null)
file = bootBlocks;
else if(debug && string.Compare(path, "$Bitmap", StringComparison.InvariantCulture) == 0)
file = blockMapBytes;
else if(debug && string.Compare(path, "$MDB", StringComparison.InvariantCulture) == 0) file = mdbBlocks;
file = blockMapBytes;
else if(debug && string.Compare(path, "$MDB", StringComparison.InvariantCulture) == 0)
file = mdbBlocks;
else
error =
ReadFile(path, out file, false, false);
error = ReadFile(path, out file, false, false);
if(error != Errno.NoError) return error;
if(error != Errno.NoError)
return error;
if(size == 0)
{
buf = new byte[0];
return Errno.NoError;
}
if(offset >= file.Length) return Errno.InvalidArgument;
if(offset >= file.Length)
return Errno.InvalidArgument;
if(size + offset >= file.Length) size = file.Length - offset;
if(size + offset >= file.Length)
size = file.Length - offset;
buf = new byte[size];
@@ -154,18 +201,25 @@ namespace DiscImageChef.Filesystems.AppleMFS
public Errno Stat(string path, out FileEntryInfo stat)
{
stat = null;
if(!mounted) return Errno.AccessDenied;
string[] pathElements = path.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
if(pathElements.Length != 1) return Errno.NotSupported;
if(!mounted)
return Errno.AccessDenied;
string[] pathElements = path.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
if(pathElements.Length != 1)
return Errno.NotSupported;
path = pathElements[0];
if(debug)
if(string.Compare(path, "$", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0 ||
if(string.Compare(path, "$", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$Bitmap", StringComparison.InvariantCulture) == 0 ||
string.Compare(path, "$MDB", StringComparison.InvariantCulture) == 0)
string.Compare(path, "$MDB", StringComparison.InvariantCulture) == 0)
{
stat = new FileEntryInfo
{
@@ -174,46 +228,50 @@ namespace DiscImageChef.Filesystems.AppleMFS
if(string.Compare(path, "$", StringComparison.InvariantCulture) == 0)
{
stat.Blocks = directoryBlocks.Length / stat.BlockSize + directoryBlocks.Length % stat.BlockSize;
stat.Blocks = (directoryBlocks.Length / stat.BlockSize) +
(directoryBlocks.Length % stat.BlockSize);
stat.Length = directoryBlocks.Length;
}
else if(string.Compare(path, "$Bitmap", StringComparison.InvariantCulture) == 0)
{
stat.Blocks = blockMapBytes.Length / stat.BlockSize + blockMapBytes.Length % stat.BlockSize;
stat.Blocks = (blockMapBytes.Length / stat.BlockSize) + (blockMapBytes.Length % stat.BlockSize);
stat.Length = blockMapBytes.Length;
}
else if(string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0 && bootBlocks != null)
else if(string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0 &&
bootBlocks != null)
{
stat.Blocks = bootBlocks.Length / stat.BlockSize + bootBlocks.Length % stat.BlockSize;
stat.Blocks = (bootBlocks.Length / stat.BlockSize) + (bootBlocks.Length % stat.BlockSize);
stat.Length = bootBlocks.Length;
}
else if(string.Compare(path, "$MDB", StringComparison.InvariantCulture) == 0)
{
stat.Blocks = mdbBlocks.Length / stat.BlockSize + mdbBlocks.Length % stat.BlockSize;
stat.Blocks = (mdbBlocks.Length / stat.BlockSize) + (mdbBlocks.Length % stat.BlockSize);
stat.Length = mdbBlocks.Length;
}
else return Errno.InvalidArgument;
else
return Errno.InvalidArgument;
return Errno.NoError;
}
if(!filenameToId.TryGetValue(path.ToLowerInvariant(), out uint fileId)) return Errno.NoSuchFile;
if(!filenameToId.TryGetValue(path.ToLowerInvariant(), out uint fileId))
return Errno.NoSuchFile;
if(!idToEntry.TryGetValue(fileId, out MFS_FileEntry entry)) return Errno.NoSuchFile;
if(!idToEntry.TryGetValue(fileId, out MFS_FileEntry entry))
return Errno.NoSuchFile;
Errno error = GetAttributes(path, out FileAttributes attr);
if(error != Errno.NoError) return error;
if(error != Errno.NoError)
return error;
stat = new FileEntryInfo
{
Attributes = attr,
Blocks = entry.flLgLen / volMDB.drAlBlkSiz,
Attributes = attr, Blocks = entry.flLgLen / volMDB.drAlBlkSiz,
BlockSize = volMDB.drAlBlkSiz,
CreationTime = DateHandlers.MacToDateTime(entry.flCrDat),
Inode = entry.flFlNum,
LastWriteTime = DateHandlers.MacToDateTime(entry.flMdDat),
Length = entry.flPyLen,
Links = 1
CreationTime = DateHandlers.MacToDateTime(entry.flCrDat), Inode = entry.flFlNum,
LastWriteTime = DateHandlers.MacToDateTime(entry.flMdDat), Length = entry.flPyLen, Links = 1
};
return Errno.NoError;
@@ -222,6 +280,7 @@ namespace DiscImageChef.Filesystems.AppleMFS
public Errno ReadLink(string path, out string dest)
{
dest = null;
return Errno.NotImplemented;
}
@@ -229,16 +288,24 @@ namespace DiscImageChef.Filesystems.AppleMFS
{
buf = null;
if(!mounted) return Errno.AccessDenied;
if(!mounted)
return Errno.AccessDenied;
string[] pathElements = path.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
if(pathElements.Length != 1) return Errno.NotSupported;
string[] pathElements = path.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
if(pathElements.Length != 1)
return Errno.NotSupported;
path = pathElements[0];
if(!filenameToId.TryGetValue(path.ToLowerInvariant(), out uint fileId)) return Errno.NoSuchFile;
if(!filenameToId.TryGetValue(path.ToLowerInvariant(), out uint fileId))
return Errno.NoSuchFile;
if(!idToEntry.TryGetValue(fileId, out MFS_FileEntry entry)) return Errno.NoSuchFile;
if(!idToEntry.TryGetValue(fileId, out MFS_FileEntry entry))
return Errno.NoSuchFile;
uint nextBlock;
@@ -247,6 +314,7 @@ namespace DiscImageChef.Filesystems.AppleMFS
if(entry.flRPyLen == 0)
{
buf = new byte[0];
return Errno.NoError;
}
@@ -257,17 +325,19 @@ namespace DiscImageChef.Filesystems.AppleMFS
if(entry.flPyLen == 0)
{
buf = new byte[0];
return Errno.NoError;
}
nextBlock = entry.flStBlk;
}
MemoryStream ms = new MemoryStream();
var ms = new MemoryStream();
do
{
byte[] sectors;
if(tags)
sectors =
device.ReadSectorsTag((ulong)((nextBlock - 2) * sectorsPerBlock) + volMDB.drAlBlSt + partitionStart,
@@ -282,18 +352,20 @@ namespace DiscImageChef.Filesystems.AppleMFS
if(blockMap[nextBlock] == BMAP_FREE)
{
DicConsole.ErrorWriteLine("File truncated at block {0}", nextBlock);
break;
}
nextBlock = blockMap[nextBlock];
}
while(nextBlock > BMAP_LAST);
} while(nextBlock > BMAP_LAST);
if(tags) buf = ms.ToArray();
if(tags)
buf = ms.ToArray();
else
{
if(resourceFork)
if(ms.Length < entry.flRLgLen) buf = ms.ToArray();
if(ms.Length < entry.flRLgLen)
buf = ms.ToArray();
else
{
buf = new byte[entry.flRLgLen];
@@ -301,7 +373,8 @@ namespace DiscImageChef.Filesystems.AppleMFS
}
else
{
if(ms.Length < entry.flLgLen) buf = ms.ToArray();
if(ms.Length < entry.flLgLen)
buf = ms.ToArray();
else
{
buf = new byte[entry.flLgLen];