// /*************************************************************************** // Aaru Data Preservation Suite // ---------------------------------------------------------------------------- // // Filename : Reiser.cs // Author(s) : Natalia Portillo // // Component : Reiser filesystem plugin // // --[ Description ] ---------------------------------------------------------- // // Identifies the Reiser 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 . // // ---------------------------------------------------------------------------- // Copyright © 2011-2023 Natalia Portillo // ****************************************************************************/ using System; using System.Linq; using System.Runtime.InteropServices; using System.Text; using Aaru.CommonTypes; using Aaru.CommonTypes.Enums; using Aaru.CommonTypes.Interfaces; using Aaru.Helpers; using Schemas; using Marshal = Aaru.Helpers.Marshal; namespace Aaru.Filesystems; /// /// Implements detection of the Reiser v3 filesystem public sealed class Reiser : IFilesystem { const uint REISER_SUPER_OFFSET = 0x10000; const string FS_TYPE = "reiserfs"; readonly byte[] _magic35 = { 0x52, 0x65, 0x49, 0x73, 0x45, 0x72, 0x46, 0x73, 0x00, 0x00 }; readonly byte[] _magic36 = { 0x52, 0x65, 0x49, 0x73, 0x45, 0x72, 0x32, 0x46, 0x73, 0x00 }; readonly byte[] _magicJr = { 0x52, 0x65, 0x49, 0x73, 0x45, 0x72, 0x33, 0x46, 0x73, 0x00 }; /// public FileSystemType XmlFsType { get; private set; } /// public Encoding Encoding { get; private set; } /// public string Name => Localization.Reiser_Name; /// public Guid Id => new("1D8CD8B8-27E6-410F-9973-D16409225FBA"); /// public string Author => Authors.NataliaPortillo; /// public bool Identify(IMediaImage imagePlugin, Partition partition) { if(imagePlugin.Info.SectorSize < 512) return false; uint sbAddr = REISER_SUPER_OFFSET / imagePlugin.Info.SectorSize; if(sbAddr == 0) sbAddr = 1; uint sbSize = (uint)(Marshal.SizeOf() / imagePlugin.Info.SectorSize); if(Marshal.SizeOf() % imagePlugin.Info.SectorSize != 0) sbSize++; if(partition.Start + sbAddr + sbSize >= partition.End) return false; ErrorNumber errno = imagePlugin.ReadSectors(partition.Start + sbAddr, sbSize, out byte[] sector); if(errno != ErrorNumber.NoError) return false; if(sector.Length < Marshal.SizeOf()) return false; Superblock reiserSb = Marshal.ByteArrayToStructureLittleEndian(sector); return _magic35.SequenceEqual(reiserSb.magic) || _magic36.SequenceEqual(reiserSb.magic) || _magicJr.SequenceEqual(reiserSb.magic); } /// public void GetInformation(IMediaImage imagePlugin, Partition partition, out string information, Encoding encoding) { Encoding = encoding ?? Encoding.GetEncoding("iso-8859-15"); information = ""; if(imagePlugin.Info.SectorSize < 512) return; uint sbAddr = REISER_SUPER_OFFSET / imagePlugin.Info.SectorSize; if(sbAddr == 0) sbAddr = 1; uint sbSize = (uint)(Marshal.SizeOf() / imagePlugin.Info.SectorSize); if(Marshal.SizeOf() % imagePlugin.Info.SectorSize != 0) sbSize++; ErrorNumber errno = imagePlugin.ReadSectors(partition.Start + sbAddr, sbSize, out byte[] sector); if(errno != ErrorNumber.NoError) return; if(sector.Length < Marshal.SizeOf()) return; Superblock reiserSb = Marshal.ByteArrayToStructureLittleEndian(sector); if(!_magic35.SequenceEqual(reiserSb.magic) && !_magic36.SequenceEqual(reiserSb.magic) && !_magicJr.SequenceEqual(reiserSb.magic)) return; var sb = new StringBuilder(); if(_magic35.SequenceEqual(reiserSb.magic)) sb.AppendLine(Localization.Reiser_3_5_filesystem); else if(_magic36.SequenceEqual(reiserSb.magic)) sb.AppendLine(Localization.Reiser_3_6_filesystem); else if(_magicJr.SequenceEqual(reiserSb.magic)) sb.AppendLine(Localization.Reiser_Jr_filesystem); sb.AppendFormat(Localization.Volume_has_0_blocks_with_1_blocks_free, reiserSb.block_count, reiserSb.free_blocks).AppendLine(); sb.AppendFormat(Localization._0_bytes_per_block, reiserSb.blocksize).AppendLine(); sb.AppendFormat(Localization.Root_directory_resides_on_block_0, reiserSb.root_block).AppendLine(); if(reiserSb.umount_state == 2) sb.AppendLine(Localization.Volume_has_not_been_cleanly_umounted); sb.AppendFormat(Localization.Volume_last_checked_on_0, DateHandlers.UnixUnsignedToDateTime(reiserSb.last_check)).AppendLine(); if(reiserSb.version >= 2) { sb.AppendFormat(Localization.Volume_UUID_0, reiserSb.uuid).AppendLine(); sb.AppendFormat(Localization.Volume_name_0, Encoding.GetString(reiserSb.label)).AppendLine(); } information = sb.ToString(); XmlFsType = new FileSystemType { Type = FS_TYPE, ClusterSize = reiserSb.blocksize, Clusters = reiserSb.block_count, FreeClusters = reiserSb.free_blocks, FreeClustersSpecified = true, Dirty = reiserSb.umount_state == 2 }; if(reiserSb.version < 2) return; XmlFsType.VolumeName = StringHandlers.CToString(reiserSb.label, Encoding); XmlFsType.VolumeSerial = reiserSb.uuid.ToString(); } [StructLayout(LayoutKind.Sequential, Pack = 1)] readonly struct JournalParameters { public readonly uint journal_1stblock; public readonly uint journal_dev; public readonly uint journal_size; public readonly uint journal_trans_max; public readonly uint journal_magic; public readonly uint journal_max_batch; public readonly uint journal_max_commit_age; public readonly uint journal_max_trans_age; } [StructLayout(LayoutKind.Sequential, Pack = 1)] struct Superblock { public readonly uint block_count; public readonly uint free_blocks; public readonly uint root_block; public readonly JournalParameters journal; public readonly ushort blocksize; public readonly ushort oid_maxsize; public readonly ushort oid_cursize; public readonly ushort umount_state; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)] public readonly byte[] magic; public readonly ushort fs_state; public readonly uint hash_function_code; public readonly ushort tree_height; public readonly ushort bmap_nr; public readonly ushort version; public readonly ushort reserved_for_journal; public readonly uint inode_generation; public readonly uint flags; public readonly Guid uuid; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public readonly byte[] label; public readonly ushort mnt_count; public readonly ushort max_mnt_count; public readonly uint last_check; public readonly uint check_interval; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 76)] public readonly byte[] unused; } }