Files
Aaru/Aaru.Filesystems/XFS/Info.cs

224 lines
8.5 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
2016-09-02 20:05:55 +01:00
// ----------------------------------------------------------------------------
//
2022-12-07 13:07:31 +00:00
// Filename : Info.cs
2016-09-02 20:05:55 +01:00
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : XFS filesystem plugin.
2016-09-02 20:05:55 +01:00
//
// --[ 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
2016-09-02 20:05:55 +01:00
// ****************************************************************************/
using System;
using System.Text;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums;
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;
2016-09-02 20:05:55 +01:00
namespace Aaru.Filesystems;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
/// <summary>Implements detection of SGI's XFS</summary>
2022-12-07 13:07:31 +00:00
public sealed partial class XFS
2016-09-02 20:05:55 +01:00
{
/// <inheritdoc />
2022-03-06 13:29:38 +00:00
public bool Identify(IMediaImage imagePlugin, Partition partition)
2016-09-02 20:05:55 +01:00
{
2022-03-06 13:29:38 +00:00
if(imagePlugin.Info.SectorSize < 512)
return false;
// Misaligned
if(imagePlugin.Info.XmlMediaType == XmlMediaType.OpticalDisc)
2016-09-02 20:05:55 +01:00
{
uint sbSize = (uint)((Marshal.SizeOf<Superblock>() + 0x400) / imagePlugin.Info.SectorSize);
2022-03-06 13:29:38 +00:00
if((Marshal.SizeOf<Superblock>() + 0x400) % imagePlugin.Info.SectorSize != 0)
sbSize++;
ErrorNumber errno = imagePlugin.ReadSectors(partition.Start, sbSize, out byte[] sector);
if(errno != ErrorNumber.NoError)
2020-02-29 18:03:35 +00:00
return false;
2016-09-02 20:05:55 +01:00
2022-03-06 13:29:38 +00:00
if(sector.Length < Marshal.SizeOf<Superblock>())
return false;
byte[] sbpiece = new byte[Marshal.SizeOf<Superblock>()];
2022-03-06 13:29:38 +00:00
foreach(int location in new[]
{
0, 0x200, 0x400
})
{
2022-03-06 13:29:38 +00:00
Array.Copy(sector, location, sbpiece, 0, Marshal.SizeOf<Superblock>());
Superblock xfsSb = Marshal.ByteArrayToStructureBigEndian<Superblock>(sbpiece);
AaruConsole.DebugWriteLine("XFS plugin", Localization.magic_at_0_X3_equals_1_expected_2, location,
2022-03-07 07:36:44 +00:00
xfsSb.magicnum, XFS_MAGIC);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(xfsSb.magicnum == XFS_MAGIC)
return true;
}
}
else
foreach(int i in new[]
{
0, 1, 2
})
{
ulong location = (ulong)i;
2022-03-06 13:29:38 +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)
2020-02-29 18:03:35 +00:00
sbSize++;
2016-09-02 20:05:55 +01:00
2022-03-06 13:29:38 +00:00
ErrorNumber errno = imagePlugin.ReadSectors(partition.Start + location, sbSize, out byte[] sector);
if(errno != ErrorNumber.NoError)
2022-03-06 13:29:38 +00:00
continue;
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;
2022-03-06 13:29:38 +00:00
Superblock xfsSb = Marshal.ByteArrayToStructureBigEndian<Superblock>(sector);
AaruConsole.DebugWriteLine("XFS plugin", Localization.magic_at_0_equals_1_expected_2, location,
2022-03-06 13:29:38 +00:00
xfsSb.magicnum, XFS_MAGIC);
2022-03-06 13:29:38 +00:00
if(xfsSb.magicnum == XFS_MAGIC)
return true;
}
2022-03-06 13:29:38 +00:00
return false;
}
2020-02-29 18:03:35 +00:00
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 = "";
2022-03-06 13:29:38 +00:00
if(imagePlugin.Info.SectorSize < 512)
return;
2022-03-06 13:29:38 +00:00
var xfsSb = new Superblock();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
// Misaligned
if(imagePlugin.Info.XmlMediaType == XmlMediaType.OpticalDisc)
{
uint sbSize = (uint)((Marshal.SizeOf<Superblock>() + 0x400) / imagePlugin.Info.SectorSize);
2022-03-06 13:29:38 +00:00
if((Marshal.SizeOf<Superblock>() + 0x400) % imagePlugin.Info.SectorSize != 0)
sbSize++;
2016-09-02 20:05:55 +01:00
2022-03-06 13:29:38 +00:00
ErrorNumber errno = imagePlugin.ReadSectors(partition.Start, sbSize, out byte[] sector);
2016-09-02 20:05:55 +01:00
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError ||
sector.Length < Marshal.SizeOf<Superblock>())
return;
byte[] sbpiece = new byte[Marshal.SizeOf<Superblock>()];
2016-09-02 20:05:55 +01:00
2022-03-06 13:29:38 +00:00
foreach(int location in new[]
{
0, 0x200, 0x400
})
{
Array.Copy(sector, location, sbpiece, 0, Marshal.SizeOf<Superblock>());
2016-09-02 20:05:55 +01:00
2022-03-06 13:29:38 +00:00
xfsSb = Marshal.ByteArrayToStructureBigEndian<Superblock>(sbpiece);
2020-02-29 18:03:35 +00:00
AaruConsole.DebugWriteLine("XFS plugin", Localization.magic_at_0_X3_equals_1_expected_2, location,
2022-03-07 07:36:44 +00:00
xfsSb.magicnum, XFS_MAGIC);
2016-09-02 20:05:55 +01:00
2022-03-06 13:29:38 +00:00
if(xfsSb.magicnum == XFS_MAGIC)
break;
}
}
else
foreach(int i in new[]
{
0, 1, 2
})
{
ulong location = (ulong)i;
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)
2020-02-29 18:03:35 +00:00
sbSize++;
2016-09-02 20:05:55 +01:00
2022-03-06 13:29:38 +00:00
ErrorNumber errno = imagePlugin.ReadSectors(partition.Start + location, sbSize, out byte[] sector);
2020-02-29 18:03:35 +00:00
if(errno != ErrorNumber.NoError ||
sector.Length < Marshal.SizeOf<Superblock>())
2020-02-29 18:03:35 +00:00
return;
2022-03-06 13:29:38 +00:00
xfsSb = Marshal.ByteArrayToStructureBigEndian<Superblock>(sector);
AaruConsole.DebugWriteLine("XFS plugin", Localization.magic_at_0_equals_1_expected_2, location,
2022-03-06 13:29:38 +00:00
xfsSb.magicnum, XFS_MAGIC);
2022-03-06 13:29:38 +00:00
if(xfsSb.magicnum == XFS_MAGIC)
break;
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(xfsSb.magicnum != XFS_MAGIC)
return;
2022-03-06 13:29:38 +00:00
var sb = new StringBuilder();
2020-02-29 18:03:35 +00:00
sb.AppendLine(Localization.XFS_filesystem);
sb.AppendFormat(Localization.Filesystem_version_0, xfsSb.version & 0xF).AppendLine();
sb.AppendFormat(Localization._0_bytes_per_sector, xfsSb.sectsize).AppendLine();
sb.AppendFormat(Localization._0_bytes_per_block, xfsSb.blocksize).AppendLine();
sb.AppendFormat(Localization._0_bytes_per_inode, xfsSb.inodesize).AppendLine();
sb.AppendFormat(Localization._0_data_blocks_in_volume_1_free, xfsSb.dblocks, xfsSb.fdblocks).AppendLine();
sb.AppendFormat(Localization._0_blocks_per_allocation_group, xfsSb.agblocks).AppendLine();
sb.AppendFormat(Localization._0_allocation_groups_in_volume, xfsSb.agcount).AppendLine();
sb.AppendFormat(Localization._0_inodes_in_volume_1_free, xfsSb.icount, xfsSb.ifree).AppendLine();
2022-03-06 13:29:38 +00:00
if(xfsSb.inprogress > 0)
sb.AppendLine(Localization.fsck_in_progress);
sb.AppendFormat(Localization.Volume_name_0, StringHandlers.CToString(xfsSb.fname, Encoding)).AppendLine();
sb.AppendFormat(Localization.Volume_UUID_0, xfsSb.uuid).AppendLine();
2016-09-02 20:05:55 +01:00
2022-03-06 13:29:38 +00:00
information = sb.ToString();
2016-09-02 20:05:55 +01:00
2022-03-06 13:29:38 +00:00
XmlFsType = new FileSystemType
{
Type = FS_TYPE,
2022-03-06 13:29:38 +00:00
ClusterSize = xfsSb.blocksize,
Clusters = xfsSb.dblocks,
FreeClusters = xfsSb.fdblocks,
FreeClustersSpecified = true,
Files = xfsSb.icount - xfsSb.ifree,
FilesSpecified = true,
Dirty = xfsSb.inprogress > 0,
VolumeName = StringHandlers.CToString(xfsSb.fname, Encoding),
VolumeSerial = xfsSb.uuid.ToString()
};
}
2016-09-02 20:05:55 +01:00
}