Files
Aaru/Aaru.Filesystems/Reiser4.cs

165 lines
5.8 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Reiser4.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Reiser4 filesystem plugin
//
// --[ Description ] ----------------------------------------------------------
//
// Identifies the Reiser4 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.Linq;
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;
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 of the Reiser v4 filesystem</summary>
public sealed class Reiser4 : IFilesystem
{
2022-03-06 13:29:38 +00:00
const uint REISER4_SUPER_OFFSET = 0x10000;
const string FS_TYPE = "reiser4";
2022-03-06 13:29:38 +00:00
readonly byte[] _magic =
{
0x52, 0x65, 0x49, 0x73, 0x45, 0x72, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public FileSystemType XmlFsType { get; private set; }
/// <inheritdoc />
public Encoding Encoding { get; private set; }
/// <inheritdoc />
public string Name => Localization.Reiser4_Name;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public Guid Id => new("301F2D00-E8D5-4F04-934E-81DFB21D15BA");
/// <inheritdoc />
public string Author => Authors.NataliaPortillo;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool Identify(IMediaImage imagePlugin, Partition partition)
{
if(imagePlugin.Info.SectorSize < 512)
return false;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
uint sbAddr = REISER4_SUPER_OFFSET / imagePlugin.Info.SectorSize;
2022-03-06 13:29:38 +00:00
if(sbAddr == 0)
sbAddr = 1;
uint sbSize = (uint)(Marshal.SizeOf<Superblock>() / imagePlugin.Info.SectorSize);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(Marshal.SizeOf<Superblock>() % imagePlugin.Info.SectorSize != 0)
sbSize++;
2017-07-23 19:58:11 +01:00
2022-03-06 13:29:38 +00:00
if(partition.Start + sbAddr + sbSize >= partition.End)
return false;
2022-03-06 13:29:38 +00:00
ErrorNumber errno = imagePlugin.ReadSectors(partition.Start + sbAddr, sbSize, out byte[] sector);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return false;
2022-03-06 13:29:38 +00:00
if(sector.Length < Marshal.SizeOf<Superblock>())
return false;
2022-03-06 13:29:38 +00:00
Superblock reiserSb = Marshal.ByteArrayToStructureLittleEndian<Superblock>(sector);
2022-03-06 13:29:38 +00:00
return _magic.SequenceEqual(reiserSb.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
{
Encoding = encoding ?? Encoding.GetEncoding("iso-8859-15");
information = "";
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(imagePlugin.Info.SectorSize < 512)
return;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
uint sbAddr = REISER4_SUPER_OFFSET / imagePlugin.Info.SectorSize;
2022-03-06 13:29:38 +00:00
if(sbAddr == 0)
sbAddr = 1;
2020-02-29 18:03:35 +00:00
uint sbSize = (uint)(Marshal.SizeOf<Superblock>() / imagePlugin.Info.SectorSize);
2022-03-06 13:29:38 +00:00
if(Marshal.SizeOf<Superblock>() % imagePlugin.Info.SectorSize != 0)
sbSize++;
2022-03-06 13:29:38 +00:00
ErrorNumber errno = imagePlugin.ReadSectors(partition.Start + sbAddr, sbSize, out byte[] sector);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return;
2022-03-06 13:29:38 +00:00
if(sector.Length < Marshal.SizeOf<Superblock>())
return;
2022-03-06 13:29:38 +00:00
Superblock reiserSb = Marshal.ByteArrayToStructureLittleEndian<Superblock>(sector);
2022-03-06 13:29:38 +00:00
if(!_magic.SequenceEqual(reiserSb.magic))
return;
2022-03-06 13:29:38 +00:00
var sb = new StringBuilder();
sb.AppendLine(Localization.Reiser_4_filesystem);
sb.AppendFormat(Localization._0_bytes_per_block, reiserSb.blocksize).AppendLine();
sb.AppendFormat(Localization.Volume_disk_format_0, reiserSb.diskformat).AppendLine();
sb.AppendFormat(Localization.Volume_UUID_0, reiserSb.uuid).AppendLine();
sb.AppendFormat(Localization.Volume_name_0, StringHandlers.CToString(reiserSb.label, Encoding)).AppendLine();
2022-03-06 13:29:38 +00:00
information = sb.ToString();
2022-03-06 13:29:38 +00:00
XmlFsType = new FileSystemType
{
Type = FS_TYPE,
2022-03-06 13:29:38 +00:00
ClusterSize = reiserSb.blocksize,
Clusters = (partition.End - partition.Start) * imagePlugin.Info.SectorSize / reiserSb.blocksize,
VolumeName = StringHandlers.CToString(reiserSb.label, Encoding),
VolumeSerial = reiserSb.uuid.ToString()
};
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct Superblock
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public readonly byte[] magic;
public readonly ushort diskformat;
public readonly ushort blocksize;
public readonly Guid uuid;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public readonly byte[] label;
}
}