Files
Aaru.Server/DiscImageChef.Filesystems/Reiser4.cs

135 lines
5.7 KiB
C#
Raw Normal View History

// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2018 Natalia Portillo
// ****************************************************************************/
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using DiscImageChef.CommonTypes;
2017-12-21 14:30:38 +00:00
using DiscImageChef.DiscImages;
using Schemas;
namespace DiscImageChef.Filesystems
{
public class Reiser4 : IFilesystem
{
const uint REISER4_SUPER_OFFSET = 0x10000;
2018-06-20 22:22:21 +01:00
readonly byte[] reiser4_magic =
2017-12-19 20:33:03 +00:00
{0x52, 0x65, 0x49, 0x73, 0x45, 0x72, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2017-12-26 08:01:40 +00:00
public FileSystemType XmlFsType { get; private set; }
public Encoding Encoding { get; private set; }
public string Name => "Reiser4 Filesystem Plugin";
public Guid Id => new Guid("301F2D00-E8D5-4F04-934E-81DFB21D15BA");
public bool Identify(IMediaImage imagePlugin, Partition partition)
{
if(imagePlugin.Info.SectorSize < 512) return false;
uint sbAddr = REISER4_SUPER_OFFSET / imagePlugin.Info.SectorSize;
2017-12-19 20:33:03 +00:00
if(sbAddr == 0) sbAddr = 1;
Reiser4_Superblock reiserSb = new Reiser4_Superblock();
uint sbSize = (uint)(Marshal.SizeOf(reiserSb) / imagePlugin.Info.SectorSize);
if(Marshal.SizeOf(reiserSb) % imagePlugin.Info.SectorSize != 0) sbSize++;
2017-12-19 20:33:03 +00:00
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);
2017-12-19 20:33:03 +00:00
if(sector.Length < Marshal.SizeOf(reiserSb)) return false;
IntPtr sbPtr = Marshal.AllocHGlobal(Marshal.SizeOf(reiserSb));
Marshal.Copy(sector, 0, sbPtr, Marshal.SizeOf(reiserSb));
reiserSb = (Reiser4_Superblock)Marshal.PtrToStructure(sbPtr, typeof(Reiser4_Superblock));
Marshal.FreeHGlobal(sbPtr);
2018-06-20 22:22:21 +01:00
return reiser4_magic.SequenceEqual(reiserSb.magic);
}
2017-12-26 08:01:40 +00:00
public void GetInformation(IMediaImage imagePlugin, Partition partition, out string information,
Encoding encoding)
{
2017-12-26 08:01:40 +00:00
Encoding = encoding ?? Encoding.GetEncoding("iso-8859-15");
information = "";
if(imagePlugin.Info.SectorSize < 512) return;
uint sbAddr = REISER4_SUPER_OFFSET / imagePlugin.Info.SectorSize;
2017-12-19 20:33:03 +00:00
if(sbAddr == 0) sbAddr = 1;
Reiser4_Superblock reiserSb = new Reiser4_Superblock();
uint sbSize = (uint)(Marshal.SizeOf(reiserSb) / imagePlugin.Info.SectorSize);
if(Marshal.SizeOf(reiserSb) % imagePlugin.Info.SectorSize != 0) sbSize++;
2017-07-19 16:37:11 +01:00
byte[] sector = imagePlugin.ReadSectors(partition.Start + sbAddr, sbSize);
2017-12-19 20:33:03 +00:00
if(sector.Length < Marshal.SizeOf(reiserSb)) return;
IntPtr sbPtr = Marshal.AllocHGlobal(Marshal.SizeOf(reiserSb));
Marshal.Copy(sector, 0, sbPtr, Marshal.SizeOf(reiserSb));
reiserSb = (Reiser4_Superblock)Marshal.PtrToStructure(sbPtr, typeof(Reiser4_Superblock));
Marshal.FreeHGlobal(sbPtr);
2018-06-20 22:22:21 +01:00
if(!reiser4_magic.SequenceEqual(reiserSb.magic)) return;
StringBuilder 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
{
Type = "Reiser 4 filesystem",
ClusterSize = reiserSb.blocksize,
Clusters = (long)((partition.End - partition.Start) * imagePlugin.Info.SectorSize / reiserSb.blocksize),
2017-12-26 08:01:40 +00:00
VolumeName = StringHandlers.CToString(reiserSb.label, Encoding),
VolumeSerial = reiserSb.uuid.ToString()
};
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct Reiser4_Superblock
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public byte[] magic;
public ushort diskformat;
public ushort blocksize;
public Guid uuid;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public byte[] label;
}
}
}