Files
Aaru/Aaru.Filesystems/AppleMFS/File.cs

407 lines
13 KiB
C#
Raw Normal View History

2017-05-19 20:28:49 +01:00
// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : File.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Apple Macintosh File System 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/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/
using System;
using System.IO;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Structs;
using Aaru.Console;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
2020-02-27 00:33:26 +00:00
using FileAttributes = Aaru.CommonTypes.Structs.FileAttributes;
2022-03-06 13:29:38 +00:00
namespace Aaru.Filesystems;
// Information from Inside Macintosh Volume II
public sealed partial class AppleMFS
{
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber MapBlock(string path, long fileBlock, out long deviceBlock)
{
2022-03-06 13:29:38 +00:00
deviceBlock = new long();
2022-03-06 13:29:38 +00:00
if(!_mounted)
return ErrorNumber.AccessDenied;
2022-03-06 13:29:38 +00:00
string[] pathElements = path.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
2022-03-06 13:29:38 +00:00
if(pathElements.Length != 1)
return ErrorNumber.NotSupported;
2022-03-06 13:29:38 +00:00
path = pathElements[0];
2022-03-06 13:29:38 +00:00
if(!_filenameToId.TryGetValue(path.ToLowerInvariant(), out uint fileId))
return ErrorNumber.NoSuchFile;
2022-03-06 13:29:38 +00:00
if(!_idToEntry.TryGetValue(fileId, out FileEntry entry))
return ErrorNumber.NoSuchFile;
2022-03-06 13:29:38 +00:00
if(fileBlock > entry.flPyLen / _volMdb.drAlBlkSiz)
return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
uint nextBlock = entry.flStBlk;
long relBlock = 0;
2022-03-06 13:29:38 +00:00
while(true)
{
if(relBlock == fileBlock)
{
2022-03-06 13:29:38 +00:00
deviceBlock = ((nextBlock - 2) * _sectorsPerBlock) + _volMdb.drAlBlSt + (long)_partitionStart;
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
if(_blockMap[nextBlock] == BMAP_FREE ||
_blockMap[nextBlock] == BMAP_LAST)
break;
nextBlock = _blockMap[nextBlock];
relBlock++;
}
2022-03-06 13:29:38 +00:00
return ErrorNumber.InOutError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber GetAttributes(string path, out FileAttributes attributes)
{
attributes = new FileAttributes();
2022-03-06 13:29:38 +00:00
if(!_mounted)
return ErrorNumber.AccessDenied;
2022-03-06 13:29:38 +00:00
string[] pathElements = path.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
2022-03-06 13:29:38 +00:00
if(pathElements.Length != 1)
return ErrorNumber.NotSupported;
2022-03-06 13:29:38 +00:00
path = pathElements[0];
2022-03-06 13:29:38 +00:00
if(!_filenameToId.TryGetValue(path.ToLowerInvariant(), out uint fileId))
return ErrorNumber.NoSuchFile;
2022-03-06 13:29:38 +00:00
if(!_idToEntry.TryGetValue(fileId, out FileEntry entry))
return ErrorNumber.NoSuchFile;
2022-03-06 13:29:38 +00:00
if(entry.flUsrWds.fdFlags.HasFlag(AppleCommon.FinderFlags.kIsAlias))
attributes |= FileAttributes.Alias;
2022-03-06 13:29:38 +00:00
if(entry.flUsrWds.fdFlags.HasFlag(AppleCommon.FinderFlags.kHasBundle))
attributes |= FileAttributes.Bundle;
2022-03-06 13:29:38 +00:00
if(entry.flUsrWds.fdFlags.HasFlag(AppleCommon.FinderFlags.kHasBeenInited))
attributes |= FileAttributes.HasBeenInited;
2022-03-06 13:29:38 +00:00
if(entry.flUsrWds.fdFlags.HasFlag(AppleCommon.FinderFlags.kHasCustomIcon))
attributes |= FileAttributes.HasCustomIcon;
2022-03-06 13:29:38 +00:00
if(entry.flUsrWds.fdFlags.HasFlag(AppleCommon.FinderFlags.kHasNoINITs))
attributes |= FileAttributes.HasNoINITs;
2022-03-06 13:29:38 +00:00
if(entry.flUsrWds.fdFlags.HasFlag(AppleCommon.FinderFlags.kIsInvisible))
attributes |= FileAttributes.Hidden;
2022-03-06 13:29:38 +00:00
if(entry.flFlags.HasFlag(FileFlags.Locked))
attributes |= FileAttributes.Immutable;
2022-03-06 13:29:38 +00:00
if(entry.flUsrWds.fdFlags.HasFlag(AppleCommon.FinderFlags.kIsOnDesk))
attributes |= FileAttributes.IsOnDesk;
2022-03-06 13:29:38 +00:00
if(entry.flUsrWds.fdFlags.HasFlag(AppleCommon.FinderFlags.kIsShared))
attributes |= FileAttributes.Shared;
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
if(entry.flUsrWds.fdFlags.HasFlag(AppleCommon.FinderFlags.kIsStationery))
attributes |= FileAttributes.Stationery;
2022-03-06 13:29:38 +00:00
if(!attributes.HasFlag(FileAttributes.Alias) &&
!attributes.HasFlag(FileAttributes.Bundle) &&
!attributes.HasFlag(FileAttributes.Stationery))
attributes |= FileAttributes.File;
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
attributes |= FileAttributes.BlockUnits;
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Read(string path, long offset, long size, ref byte[] buf)
{
if(!_mounted)
return ErrorNumber.AccessDenied;
byte[] file;
ErrorNumber error = ErrorNumber.NoError;
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;
else
error = ReadFile(path, out file, false, false);
if(error != ErrorNumber.NoError)
return error;
if(size == 0)
{
2022-03-06 13:29:38 +00:00
buf = Array.Empty<byte>();
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
if(offset >= file.Length)
return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
if(size + offset >= file.Length)
size = file.Length - offset;
2022-03-06 13:29:38 +00:00
buf = new byte[size];
2022-03-06 13:29:38 +00:00
Array.Copy(file, offset, buf, 0, size);
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Stat(string path, out FileEntryInfo stat)
{
stat = null;
2022-03-06 13:29:38 +00:00
if(!_mounted)
return ErrorNumber.AccessDenied;
2022-03-06 13:29:38 +00:00
string[] pathElements = path.Split(new[]
{
2022-03-06 13:29:38 +00:00
'/'
}, StringSplitOptions.RemoveEmptyEntries);
if(pathElements.Length != 1)
return ErrorNumber.NotSupported;
2022-03-06 13:29:38 +00:00
path = pathElements[0];
2022-03-06 13:29:38 +00:00
if(_debug)
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)
{
2022-03-06 13:29:38 +00:00
stat = new FileEntryInfo
{
BlockSize = _device.Info.SectorSize,
Inode = 0,
Links = 1,
Attributes = FileAttributes.System
};
2022-03-06 13:29:38 +00:00
if(string.Compare(path, "$", StringComparison.InvariantCulture) == 0)
{
stat.Blocks = (_directoryBlocks.Length / stat.BlockSize) +
(_directoryBlocks.Length % stat.BlockSize);
2022-03-06 13:29:38 +00:00
stat.Length = _directoryBlocks.Length;
}
else if(string.Compare(path, "$Bitmap", StringComparison.InvariantCulture) == 0)
{
stat.Blocks = (_blockMapBytes.Length / stat.BlockSize) +
(_blockMapBytes.Length % stat.BlockSize);
2022-03-06 13:29:38 +00:00
stat.Length = _blockMapBytes.Length;
}
else if(string.Compare(path, "$Boot", StringComparison.InvariantCulture) == 0 &&
_bootBlocks != null)
{
2022-03-06 13:29:38 +00:00
stat.Blocks = (_bootBlocks.Length / stat.BlockSize) + (_bootBlocks.Length % stat.BlockSize);
stat.Length = _bootBlocks.Length;
}
2022-03-06 13:29:38 +00:00
else if(string.Compare(path, "$MDB", StringComparison.InvariantCulture) == 0)
{
stat.Blocks = (_mdbBlocks.Length / stat.BlockSize) + (_mdbBlocks.Length % stat.BlockSize);
stat.Length = _mdbBlocks.Length;
}
else
return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
if(!_filenameToId.TryGetValue(path.ToLowerInvariant(), out uint fileId))
return ErrorNumber.NoSuchFile;
2022-03-06 13:29:38 +00:00
if(!_idToEntry.TryGetValue(fileId, out FileEntry entry))
return ErrorNumber.NoSuchFile;
2022-03-06 13:29:38 +00:00
ErrorNumber error = GetAttributes(path, out FileAttributes attr);
2022-03-06 13:29:38 +00:00
if(error != ErrorNumber.NoError)
return error;
2022-03-06 13:29:38 +00:00
stat = new FileEntryInfo
{
2022-03-06 13:29:38 +00:00
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
};
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadLink(string path, out string dest)
{
dest = null;
2022-03-06 13:29:38 +00:00
return ErrorNumber.NotImplemented;
}
2022-03-06 13:29:38 +00:00
ErrorNumber ReadFile(string path, out byte[] buf, bool resourceFork, bool tags)
{
buf = null;
2022-03-06 13:29:38 +00:00
if(!_mounted)
return ErrorNumber.AccessDenied;
string[] pathElements = path.Split(new[]
{
'/'
}, StringSplitOptions.RemoveEmptyEntries);
2022-03-06 13:29:38 +00:00
if(pathElements.Length != 1)
return ErrorNumber.NotSupported;
2022-03-06 13:29:38 +00:00
path = pathElements[0];
2022-03-06 13:29:38 +00:00
if(!_filenameToId.TryGetValue(path.ToLowerInvariant(), out uint fileId))
return ErrorNumber.NoSuchFile;
2022-03-06 13:29:38 +00:00
if(!_idToEntry.TryGetValue(fileId, out FileEntry entry))
return ErrorNumber.NoSuchFile;
2022-03-06 13:29:38 +00:00
uint nextBlock;
2022-03-06 13:29:38 +00:00
if(resourceFork)
{
if(entry.flRPyLen == 0)
{
2022-03-06 13:29:38 +00:00
buf = Array.Empty<byte>();
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
nextBlock = entry.flRStBlk;
}
else
{
if(entry.flPyLen == 0)
{
buf = Array.Empty<byte>();
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
nextBlock = entry.flStBlk;
}
2022-03-06 13:29:38 +00:00
var ms = new MemoryStream();
2022-03-06 13:29:38 +00:00
do
{
byte[] sectors;
ErrorNumber errno;
2022-03-06 13:29:38 +00:00
errno =
tags
? _device.
ReadSectorsTag((ulong)((nextBlock - 2) * _sectorsPerBlock) + _volMdb.drAlBlSt + _partitionStart,
(uint)_sectorsPerBlock, SectorTagType.AppleSectorTag, out sectors)
: _device.
ReadSectors((ulong)((nextBlock - 2) * _sectorsPerBlock) + _volMdb.drAlBlSt + _partitionStart,
(uint)_sectorsPerBlock, out sectors);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
2022-03-06 13:29:38 +00:00
ms.Write(sectors, 0, sectors.Length);
2022-03-06 13:29:38 +00:00
if(_blockMap[nextBlock] == BMAP_FREE)
{
AaruConsole.ErrorWriteLine("File truncated at block {0}", nextBlock);
break;
}
2022-03-06 13:29:38 +00:00
nextBlock = _blockMap[nextBlock];
} while(nextBlock > BMAP_LAST);
2022-03-06 13:29:38 +00:00
if(tags)
buf = ms.ToArray();
else
{
if(resourceFork)
if(ms.Length < entry.flRLgLen)
buf = ms.ToArray();
else
{
buf = new byte[entry.flRLgLen];
Array.Copy(ms.ToArray(), 0, buf, 0, buf.Length);
}
else
{
2022-03-06 13:29:38 +00:00
if(ms.Length < entry.flLgLen)
buf = ms.ToArray();
else
{
2022-03-06 13:29:38 +00:00
buf = new byte[entry.flLgLen];
Array.Copy(ms.ToArray(), 0, buf, 0, buf.Length);
}
}
}
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2017-12-19 20:33:03 +00:00
}