Files
Aaru/Aaru.Filesystems/Reiser4.cs

160 lines
5.9 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/>.
//
// ----------------------------------------------------------------------------
2020-12-31 23:08:23 +00:00
// Copyright © 2011-2021 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.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;
2020-02-27 00:33:26 +00:00
namespace Aaru.Filesystems
{
/// <inheritdoc />
2021-08-17 13:56:05 +01:00
/// <summary>
/// Implements detection of the Reiser v4 filesystem
/// </summary>
2020-07-22 13:20:25 +01:00
public sealed class Reiser4 : IFilesystem
{
const uint REISER4_SUPER_OFFSET = 0x10000;
2020-07-20 21:11:32 +01:00
readonly byte[] _magic =
2018-08-29 22:15:43 +01:00
{
0x52, 0x65, 0x49, 0x73, 0x45, 0x72, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2017-12-26 08:01:40 +00:00
public FileSystemType XmlFsType { get; private set; }
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-06-22 08:08:38 +01:00
public Encoding Encoding { get; private set; }
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-06-22 08:08:38 +01:00
public string Name => "Reiser4 Filesystem Plugin";
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-06-22 08:08:38 +01:00
public Guid Id => new Guid("301F2D00-E8D5-4F04-934E-81DFB21D15BA");
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-08-29 22:15:43 +01:00
public string Author => "Natalia Portillo";
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool Identify(IMediaImage imagePlugin, Partition partition)
{
2020-02-29 18:03:35 +00:00
if(imagePlugin.Info.SectorSize < 512)
return false;
2020-02-29 18:03:35 +00:00
uint sbAddr = REISER4_SUPER_OFFSET / imagePlugin.Info.SectorSize;
if(sbAddr == 0)
sbAddr = 1;
2020-07-20 21:11:32 +01:00
uint sbSize = (uint)(Marshal.SizeOf<Superblock>() / imagePlugin.Info.SectorSize);
2020-07-20 21:11:32 +01:00
if(Marshal.SizeOf<Superblock>() % imagePlugin.Info.SectorSize != 0)
2020-02-29 18:03:35 +00:00
sbSize++;
if(partition.Start + sbAddr + sbSize >= partition.End)
return false;
2017-07-23 19:58:11 +01:00
2017-07-19 16:37:11 +01:00
byte[] sector = imagePlugin.ReadSectors(partition.Start + sbAddr, sbSize);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(sector.Length < Marshal.SizeOf<Superblock>())
2020-02-29 18:03:35 +00:00
return false;
2020-07-20 21:11:32 +01:00
Superblock reiserSb = Marshal.ByteArrayToStructureLittleEndian<Superblock>(sector);
2020-07-20 21:11:32 +01:00
return _magic.SequenceEqual(reiserSb.magic);
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2017-12-26 08:01:40 +00:00
public void GetInformation(IMediaImage imagePlugin, Partition partition, out string information,
2020-02-29 18:03:35 +00:00
Encoding encoding)
{
2018-06-22 08:08:38 +01:00
Encoding = encoding ?? Encoding.GetEncoding("iso-8859-15");
information = "";
2020-02-29 18:03:35 +00:00
if(imagePlugin.Info.SectorSize < 512)
return;
uint sbAddr = REISER4_SUPER_OFFSET / imagePlugin.Info.SectorSize;
if(sbAddr == 0)
sbAddr = 1;
2020-07-20 21:11:32 +01:00
uint sbSize = (uint)(Marshal.SizeOf<Superblock>() / imagePlugin.Info.SectorSize);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(Marshal.SizeOf<Superblock>() % imagePlugin.Info.SectorSize != 0)
2020-02-29 18:03:35 +00:00
sbSize++;
2017-07-19 16:37:11 +01:00
byte[] sector = imagePlugin.ReadSectors(partition.Start + sbAddr, sbSize);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(sector.Length < Marshal.SizeOf<Superblock>())
2020-02-29 18:03:35 +00:00
return;
2020-07-20 21:11:32 +01:00
Superblock reiserSb = Marshal.ByteArrayToStructureLittleEndian<Superblock>(sector);
2020-07-20 21:11:32 +01:00
if(!_magic.SequenceEqual(reiserSb.magic))
2020-02-29 18:03:35 +00:00
return;
2020-02-29 18:03:35 +00:00
var sb = new StringBuilder();
sb.AppendLine("Reiser 4 filesystem");
sb.AppendFormat("{0} bytes per block", reiserSb.blocksize).AppendLine();
sb.AppendFormat("Volume disk format: {0}", reiserSb.diskformat).AppendLine();
sb.AppendFormat("Volume UUID: {0}", reiserSb.uuid).AppendLine();
2017-12-26 08:01:40 +00:00
sb.AppendFormat("Volume name: {0}", StringHandlers.CToString(reiserSb.label, Encoding)).AppendLine();
information = sb.ToString();
2017-12-26 08:01:40 +00:00
XmlFsType = new FileSystemType
{
2020-07-20 04:34:16 +01:00
Type = "Reiser 4 filesystem",
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
{
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
2019-04-23 01:38:33 +01:00
public readonly byte[] magic;
public readonly ushort diskformat;
public readonly ushort blocksize;
public readonly Guid uuid;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
2019-04-23 01:38:33 +01:00
public readonly byte[] label;
}
}
}