Files
Aaru/Aaru.Filesystems/XFS.cs

300 lines
12 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
// ----------------------------------------------------------------------------
//
// Filename : XFS.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : XFS filesystem plugin.
2016-09-02 20:05:55 +01:00
//
// --[ Description ] ----------------------------------------------------------
//
// Identifies the XFS 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/>.
//
// ----------------------------------------------------------------------------
2025-08-14 02:49:52 +01:00
// Copyright © 2011-2025 Natalia Portillo
2016-09-02 20:05:55 +01:00
// ****************************************************************************/
using System;
using System.Runtime.InteropServices;
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;
2020-02-27 00:33:26 +00:00
using Marshal = Aaru.Helpers.Marshal;
2016-09-02 20:05:55 +01:00
2020-02-27 00:33:26 +00:00
namespace Aaru.Filesystems
2016-09-02 20:05:55 +01:00
{
/// <inheritdoc />
2021-08-17 21:23:10 +01:00
/// <summary>Implements detection of SGI's XFS</summary>
2020-07-22 13:20:25 +01:00
public sealed class XFS : IFilesystem
2016-09-02 20:05:55 +01:00
{
const uint XFS_MAGIC = 0x58465342;
2016-09-02 20:05:55 +01:00
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 />
2021-08-17 21:23:10 +01:00
public Encoding Encoding { get; private set; }
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-08-17 21:23:10 +01:00
public string Name => "XFS Filesystem Plugin";
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-08-17 21:23:10 +01:00
public Guid Id => new Guid("1D8CD8B8-27E6-410F-9973-D16409225FBA");
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-08-17 21:23:10 +01:00
public string Author => "Natalia Portillo";
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool Identify(IMediaImage imagePlugin, Partition partition)
2016-09-02 20:05:55 +01:00
{
2020-02-29 18:03:35 +00:00
if(imagePlugin.Info.SectorSize < 512)
return false;
2016-09-02 20:05:55 +01:00
// Misaligned
if(imagePlugin.Info.XmlMediaType == XmlMediaType.OpticalDisc)
{
2020-07-20 21:11:32 +01:00
uint sbSize = (uint)((Marshal.SizeOf<Superblock>() + 0x400) / imagePlugin.Info.SectorSize);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if((Marshal.SizeOf<Superblock>() + 0x400) % imagePlugin.Info.SectorSize != 0)
2020-02-29 18:03:35 +00:00
sbSize++;
2016-09-02 20:05:55 +01:00
2017-08-09 04:46:14 +01:00
byte[] sector = imagePlugin.ReadSectors(partition.Start, 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
byte[] sbpiece = new byte[Marshal.SizeOf<Superblock>()];
2020-02-29 18:03:35 +00:00
foreach(int location in new[]
{
0, 0x200, 0x400
})
{
2020-07-20 21:11:32 +01:00
Array.Copy(sector, location, sbpiece, 0, Marshal.SizeOf<Superblock>());
2020-07-20 21:11:32 +01:00
Superblock xfsSb = Marshal.ByteArrayToStructureBigEndian<Superblock>(sbpiece);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("XFS plugin", "magic at 0x{0:X3} = 0x{1:X8} (expected 0x{2:X8})",
2020-02-29 18:03:35 +00:00
location, xfsSb.magicnum, XFS_MAGIC);
2020-02-29 18:03:35 +00:00
if(xfsSb.magicnum == XFS_MAGIC)
return true;
}
}
else
2020-02-29 18:03:35 +00:00
foreach(int i in new[]
{
0, 1, 2
})
{
ulong location = (ulong)i;
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++;
byte[] sector = imagePlugin.ReadSectors(partition.Start + location, 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 xfsSb = Marshal.ByteArrayToStructureBigEndian<Superblock>(sector);
2016-09-02 20:05:55 +01:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("XFS plugin", "magic at {0} = 0x{1:X8} (expected 0x{2:X8})", location,
2020-02-29 18:03:35 +00:00
xfsSb.magicnum, XFS_MAGIC);
2016-09-02 20:05:55 +01:00
2020-02-29 18:03:35 +00:00
if(xfsSb.magicnum == XFS_MAGIC)
return true;
}
return false;
2016-09-02 20:05:55 +01:00
}
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)
2016-09-02 20:05:55 +01:00
{
2018-06-22 08:08:38 +01:00
Encoding = encoding ?? Encoding.GetEncoding("iso-8859-15");
2016-09-02 20:05:55 +01:00
information = "";
2020-02-29 18:03:35 +00:00
if(imagePlugin.Info.SectorSize < 512)
return;
2020-07-20 21:11:32 +01:00
var xfsSb = new Superblock();
2016-09-02 20:05:55 +01:00
// Misaligned
if(imagePlugin.Info.XmlMediaType == XmlMediaType.OpticalDisc)
{
2020-07-20 21:11:32 +01:00
uint sbSize = (uint)((Marshal.SizeOf<Superblock>() + 0x400) / imagePlugin.Info.SectorSize);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if((Marshal.SizeOf<Superblock>() + 0x400) % imagePlugin.Info.SectorSize != 0)
2020-02-29 18:03:35 +00:00
sbSize++;
2016-09-02 20:05:55 +01:00
2017-08-09 04:46:14 +01:00
byte[] sector = imagePlugin.ReadSectors(partition.Start, 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
byte[] sbpiece = new byte[Marshal.SizeOf<Superblock>()];
2020-02-29 18:03:35 +00:00
foreach(int location in new[]
{
0, 0x200, 0x400
})
{
2020-07-20 21:11:32 +01:00
Array.Copy(sector, location, sbpiece, 0, Marshal.SizeOf<Superblock>());
2020-07-20 21:11:32 +01:00
xfsSb = Marshal.ByteArrayToStructureBigEndian<Superblock>(sbpiece);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("XFS plugin", "magic at 0x{0:X3} = 0x{1:X8} (expected 0x{2:X8})",
2020-02-29 18:03:35 +00:00
location, xfsSb.magicnum, XFS_MAGIC);
2020-02-29 18:03:35 +00:00
if(xfsSb.magicnum == XFS_MAGIC)
break;
}
}
else
2020-02-29 18:03:35 +00:00
foreach(int i in new[]
{
0, 1, 2
})
{
2018-06-20 22:22:21 +01:00
ulong location = (ulong)i;
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++;
byte[] sector = imagePlugin.ReadSectors(partition.Start + location, 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
xfsSb = Marshal.ByteArrayToStructureBigEndian<Superblock>(sector);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("XFS plugin", "magic at {0} = 0x{1:X8} (expected 0x{2:X8})", location,
2020-02-29 18:03:35 +00:00
xfsSb.magicnum, XFS_MAGIC);
2016-09-02 20:05:55 +01:00
2020-02-29 18:03:35 +00:00
if(xfsSb.magicnum == XFS_MAGIC)
break;
}
2016-09-02 20:05:55 +01:00
2020-02-29 18:03:35 +00:00
if(xfsSb.magicnum != XFS_MAGIC)
return;
2016-09-02 20:05:55 +01:00
2020-02-29 18:03:35 +00:00
var sb = new StringBuilder();
2016-09-02 20:05:55 +01:00
sb.AppendLine("XFS filesystem");
sb.AppendFormat("Filesystem version {0}", xfsSb.version & 0xF).AppendLine();
sb.AppendFormat("{0} bytes per sector", xfsSb.sectsize).AppendLine();
sb.AppendFormat("{0} bytes per block", xfsSb.blocksize).AppendLine();
sb.AppendFormat("{0} bytes per inode", xfsSb.inodesize).AppendLine();
sb.AppendFormat("{0} data blocks in volume, {1} free", xfsSb.dblocks, xfsSb.fdblocks).AppendLine();
sb.AppendFormat("{0} blocks per allocation group", xfsSb.agblocks).AppendLine();
sb.AppendFormat("{0} allocation groups in volume", xfsSb.agcount).AppendLine();
sb.AppendFormat("{0} inodes in volume, {1} free", xfsSb.icount, xfsSb.ifree).AppendLine();
2020-02-29 18:03:35 +00:00
if(xfsSb.inprogress > 0)
sb.AppendLine("fsck in progress");
2017-12-26 08:01:40 +00:00
sb.AppendFormat("Volume name: {0}", StringHandlers.CToString(xfsSb.fname, Encoding)).AppendLine();
2016-09-02 20:05:55 +01:00
sb.AppendFormat("Volume UUID: {0}", xfsSb.uuid).AppendLine();
information = sb.ToString();
2017-12-26 08:01:40 +00:00
XmlFsType = new FileSystemType
{
2020-07-20 04:34:16 +01:00
Type = "XFS filesystem",
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
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct Superblock
{
2019-04-23 01:38:33 +01:00
public readonly uint magicnum;
public readonly uint blocksize;
public readonly ulong dblocks;
public readonly ulong rblocks;
public readonly ulong rextents;
public readonly Guid uuid;
public readonly ulong logstat;
public readonly ulong rootino;
public readonly ulong rbmino;
public readonly ulong rsumino;
public readonly uint rextsize;
public readonly uint agblocks;
public readonly uint agcount;
public readonly uint rbmblocks;
public readonly uint logblocks;
public readonly ushort version;
public readonly ushort sectsize;
public readonly ushort inodesize;
public readonly ushort inopblock;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
2019-04-23 01:38:33 +01:00
public readonly byte[] fname;
public readonly byte blocklog;
public readonly byte sectlog;
public readonly byte inodelog;
public readonly byte inopblog;
public readonly byte agblklog;
public readonly byte rextslog;
public readonly byte inprogress;
public readonly byte imax_pct;
public readonly ulong icount;
public readonly ulong ifree;
public readonly ulong fdblocks;
public readonly ulong frextents;
public readonly ulong uquotino;
public readonly ulong gquotino;
public readonly ushort qflags;
public readonly byte flags;
public readonly byte shared_vn;
public readonly ulong inoalignmt;
public readonly ulong unit;
public readonly ulong width;
public readonly byte dirblklog;
public readonly byte logsectlog;
public readonly ushort logsectsize;
public readonly uint logsunit;
public readonly uint features2;
public readonly uint bad_features2;
public readonly uint features_compat;
public readonly uint features_ro_compat;
public readonly uint features_incompat;
public readonly uint features_log_incompat;
2020-02-29 18:03:35 +00:00
// This field is little-endian while rest of superblock is big-endian
2019-04-23 01:38:33 +01:00
public readonly uint crc;
public readonly uint spino_align;
public readonly ulong pquotino;
public readonly ulong lsn;
public readonly Guid meta_uuid;
}
2016-09-02 20:05:55 +01:00
}
}