Files
Aaru/Aaru.Filesystems/Fossil.cs

197 lines
7.8 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Fossil.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Fossil filesystem plugin
//
// --[ Description ] ----------------------------------------------------------
//
// Identifies the Fossil filesystem and shows information.
//
// --[ 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-12-03 16:07:10 +00:00
// Copyright © 2011-2023 Natalia Portillo
// ****************************************************************************/
using System;
using System.Runtime.InteropServices;
using System.Text;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Interfaces;
using Aaru.Console;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
2017-12-21 14:30:38 +00:00
using Schemas;
2020-02-27 00:33:26 +00:00
using Marshal = Aaru.Helpers.Marshal;
namespace Aaru.Filesystems;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
/// <summary>Implements detection for the Plan-9 Fossil on-disk filesystem</summary>
public sealed class Fossil : IFilesystem
{
2022-03-06 13:29:38 +00:00
const uint FOSSIL_HDR_MAGIC = 0x3776AE89;
const uint FOSSIL_SB_MAGIC = 0x2340A3B1;
// Fossil header starts at 128KiB
const ulong HEADER_POS = 128 * 1024;
const string FS_TYPE = "fossil";
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public FileSystemType XmlFsType { get; private set; }
/// <inheritdoc />
2022-03-06 13:29:38 +00:00
public Encoding Encoding { get; private set; }
/// <inheritdoc />
public string Name => Localization.Fossil_Name;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public Guid Id => new("932BF104-43F6-494F-973C-45EF58A51DA9");
/// <inheritdoc />
public string Author => Authors.NataliaPortillo;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool Identify(IMediaImage imagePlugin, Partition partition)
{
2022-03-06 13:29:38 +00:00
ulong hdrSector = HEADER_POS / imagePlugin.Info.SectorSize;
2022-03-06 13:29:38 +00:00
if(partition.Start + hdrSector > imagePlugin.Info.Sectors)
return false;
2022-03-06 13:29:38 +00:00
ErrorNumber errno = imagePlugin.ReadSector(partition.Start + hdrSector, out byte[] sector);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return false;
2022-03-06 13:29:38 +00:00
Header hdr = Marshal.ByteArrayToStructureBigEndian<Header>(sector);
AaruConsole.DebugWriteLine("Fossil plugin", Localization.magic_at_0_expected_1, hdr.magic, FOSSIL_HDR_MAGIC);
2022-03-06 13:29:38 +00:00
return hdr.magic == FOSSIL_HDR_MAGIC;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
2022-03-07 07:36:44 +00:00
public void GetInformation(IMediaImage imagePlugin, Partition partition, out string information, Encoding encoding)
2022-03-06 13:29:38 +00:00
{
// Technically everything on Plan 9 from Bell Labs is in UTF-8
Encoding = Encoding.UTF8;
information = "";
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(imagePlugin.Info.SectorSize < 512)
return;
2022-03-06 13:29:38 +00:00
ulong hdrSector = HEADER_POS / imagePlugin.Info.SectorSize;
2022-03-06 13:29:38 +00:00
ErrorNumber errno = imagePlugin.ReadSector(partition.Start + hdrSector, out byte[] sector);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return;
2022-03-06 13:29:38 +00:00
Header hdr = Marshal.ByteArrayToStructureBigEndian<Header>(sector);
AaruConsole.DebugWriteLine("Fossil plugin", Localization.magic_at_0_expected_1, hdr.magic, FOSSIL_HDR_MAGIC);
2022-03-06 13:29:38 +00:00
var sb = new StringBuilder();
sb.AppendLine(Localization.Fossil_filesystem);
sb.AppendFormat(Localization.Filesystem_version_0, hdr.version).AppendLine();
sb.AppendFormat(Localization._0_bytes_per_block, hdr.blockSize).AppendLine();
sb.AppendFormat(Localization.Superblock_resides_in_block_0, hdr.super).AppendLine();
sb.AppendFormat(Localization.Labels_resides_in_block_0, hdr.label).AppendLine();
sb.AppendFormat(Localization.Data_starts_at_block_0, hdr.data).AppendLine();
sb.AppendFormat(Localization.Volume_has_0_blocks, hdr.end).AppendLine();
ulong sbLocation = (hdr.super * (hdr.blockSize / imagePlugin.Info.SectorSize)) + partition.Start;
2022-03-06 13:29:38 +00:00
XmlFsType = new FileSystemType
{
Type = FS_TYPE,
2022-03-06 13:29:38 +00:00
ClusterSize = hdr.blockSize,
Clusters = hdr.end
};
if(sbLocation <= partition.End)
{
2022-03-17 23:54:41 +00:00
imagePlugin.ReadSector(sbLocation, out sector);
2022-03-06 13:29:38 +00:00
SuperBlock fsb = Marshal.ByteArrayToStructureBigEndian<SuperBlock>(sector);
AaruConsole.DebugWriteLine("Fossil plugin", Localization.magic_0_expected_1, fsb.magic, FOSSIL_SB_MAGIC);
2022-03-06 13:29:38 +00:00
if(fsb.magic == FOSSIL_SB_MAGIC)
{
sb.AppendFormat(Localization.Epoch_low_0, fsb.epochLow).AppendLine();
sb.AppendFormat(Localization.Epoch_high_0, fsb.epochHigh).AppendLine();
sb.AppendFormat(Localization.Next_QID_0, fsb.qid).AppendLine();
sb.AppendFormat(Localization.Active_root_block_0, fsb.active).AppendLine();
sb.AppendFormat(Localization.Next_root_block_0, fsb.next).AppendLine();
sb.AppendFormat(Localization.Current_root_block_0, fsb.current).AppendLine();
sb.AppendFormat(Localization.Volume_label_0, StringHandlers.CToString(fsb.name, Encoding)).AppendLine();
2022-03-06 13:29:38 +00:00
XmlFsType.VolumeName = StringHandlers.CToString(fsb.name, Encoding);
}
}
2022-03-06 13:29:38 +00:00
information = sb.ToString();
}
2022-03-06 13:29:38 +00:00
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct Header
{
/// <summary>Magic number</summary>
public readonly uint magic;
/// <summary>Header version</summary>
public readonly ushort version;
/// <summary>Block size</summary>
public readonly ushort blockSize;
/// <summary>Block containing superblock</summary>
public readonly uint super;
/// <summary>Block containing labels</summary>
public readonly uint label;
/// <summary>Where do data blocks start</summary>
public readonly uint data;
/// <summary>How many data blocks does it have</summary>
public readonly uint end;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct SuperBlock
{
/// <summary>Magic number</summary>
public readonly uint magic;
/// <summary>Header version</summary>
public readonly ushort version;
/// <summary>file system low epoch</summary>
public readonly uint epochLow;
/// <summary>file system high(active) epoch</summary>
public readonly uint epochHigh;
/// <summary>next qid to allocate</summary>
public readonly ulong qid;
/// <summary>data block number: root of active file system</summary>
public readonly int active;
/// <summary>data block number: root of next file system to archive</summary>
public readonly int next;
/// <summary>data block number: root of file system currently being archived</summary>
public readonly int current;
/// <summary>Venti score of last successful archive</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public readonly byte[] last;
/// <summary>name of file system(just a comment)</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public readonly byte[] name;
}
}