Files
Aaru/Aaru.Filesystems/NILFS2/Info.cs

147 lines
5.8 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
2016-09-02 18:46:55 +01:00
// ----------------------------------------------------------------------------
//
2022-12-07 13:07:31 +00:00
// Filename : Info.cs
2016-09-02 18:46:55 +01:00
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : NILFS2 filesystem plugin.
2016-09-02 18:46: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/>.
//
// ----------------------------------------------------------------------------
2024-12-19 10:45:18 +00:00
// Copyright © 2011-2025 Natalia Portillo
2016-09-02 18:46:55 +01:00
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
// ReSharper disable UnusedMember.Local
2016-09-02 18:46:55 +01:00
using System.Text;
using Aaru.CommonTypes.AaruMetadata;
using Aaru.CommonTypes.Enums;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Interfaces;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
using Partition = Aaru.CommonTypes.Partition;
2016-09-02 18:46:55 +01:00
namespace Aaru.Filesystems;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
/// <summary>Implements detection of the New Implementation of a Log-structured File System v2</summary>
2022-12-07 13:07:31 +00:00
public sealed partial class NILFS2
2022-03-06 13:29:38 +00:00
{
#region IFilesystem Members
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool Identify(IMediaImage imagePlugin, Partition partition)
{
2024-05-01 04:05:22 +01:00
if(imagePlugin.Info.SectorSize < 512) return false;
2016-09-02 18:46:55 +01:00
2022-03-06 13:29:38 +00:00
uint sbAddr = NILFS2_SUPER_OFFSET / imagePlugin.Info.SectorSize;
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
if(sbAddr == 0) sbAddr = 1;
2017-07-23 19:58:11 +01:00
var sbSize = (uint)(Marshal.SizeOf<Superblock>() / imagePlugin.Info.SectorSize);
2024-05-01 04:05:22 +01:00
if(Marshal.SizeOf<Superblock>() % imagePlugin.Info.SectorSize != 0) sbSize++;
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
if(partition.Start + sbAddr + sbSize >= partition.End) return false;
2016-09-02 18:46:55 +01:00
ErrorNumber errno = imagePlugin.ReadSectors(partition.Start + sbAddr, false, sbSize, out byte[] sector, out _);
2016-09-02 18:46:55 +01:00
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return false;
2016-09-02 18:46:55 +01:00
2024-05-01 04:05:22 +01:00
if(sector.Length < Marshal.SizeOf<Superblock>()) return false;
2016-09-02 18:46:55 +01:00
2022-03-06 13:29:38 +00:00
Superblock nilfsSb = Marshal.ByteArrayToStructureLittleEndian<Superblock>(sector);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return nilfsSb.magic == NILFS2_MAGIC;
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public void GetInformation(IMediaImage imagePlugin, Partition partition, Encoding encoding, out string information,
out FileSystem metadata)
2022-03-06 13:29:38 +00:00
{
encoding ??= Encoding.UTF8;
information = "";
metadata = new FileSystem();
2016-09-02 18:46:55 +01:00
2024-05-01 04:05:22 +01:00
if(imagePlugin.Info.SectorSize < 512) return;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
uint sbAddr = NILFS2_SUPER_OFFSET / imagePlugin.Info.SectorSize;
2016-09-02 18:46:55 +01:00
2024-05-01 04:05:22 +01:00
if(sbAddr == 0) sbAddr = 1;
var sbSize = (uint)(Marshal.SizeOf<Superblock>() / imagePlugin.Info.SectorSize);
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
if(Marshal.SizeOf<Superblock>() % imagePlugin.Info.SectorSize != 0) sbSize++;
2016-09-02 18:46:55 +01:00
ErrorNumber errno = imagePlugin.ReadSectors(partition.Start + sbAddr, false, sbSize, out byte[] sector, out _);
2016-09-02 18:46:55 +01:00
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return;
2016-09-02 18:46:55 +01:00
2024-05-01 04:05:22 +01:00
if(sector.Length < Marshal.SizeOf<Superblock>()) return;
2016-09-02 18:46:55 +01:00
2022-03-06 13:29:38 +00:00
Superblock nilfsSb = Marshal.ByteArrayToStructureLittleEndian<Superblock>(sector);
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
if(nilfsSb.magic != NILFS2_MAGIC) return;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
var sb = new StringBuilder();
2020-02-29 18:03:35 +00:00
sb.AppendLine(Localization.NILFS2_filesystem);
sb.AppendFormat(Localization.Version_0_1, nilfsSb.rev_level, nilfsSb.minor_rev_level).AppendLine();
sb.AppendFormat(Localization._0_bytes_per_block, 1 << (int)(nilfsSb.log_block_size + 10)).AppendLine();
sb.AppendFormat(Localization._0_bytes_in_volume, nilfsSb.dev_size).AppendLine();
sb.AppendFormat(Localization._0_blocks_per_segment, nilfsSb.blocks_per_segment).AppendLine();
sb.AppendFormat(Localization._0_segments, nilfsSb.nsegments).AppendLine();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(nilfsSb.creator_os == 0)
sb.AppendLine(Localization.Filesystem_created_on_Linux);
2022-03-06 13:29:38 +00:00
else
sb.AppendFormat(Localization.Creator_OS_code_0, nilfsSb.creator_os).AppendLine();
sb.AppendFormat(Localization._0_bytes_per_inode, nilfsSb.inode_size).AppendLine();
sb.AppendFormat(Localization.Volume_UUID_0, nilfsSb.uuid).AppendLine();
2024-05-01 04:05:22 +01:00
sb.AppendFormat(Localization.Volume_name_0, StringHandlers.CToString(nilfsSb.volume_name, encoding))
.AppendLine();
2016-09-02 18:46:55 +01:00
2024-05-01 04:05:22 +01:00
sb.AppendFormat(Localization.Volume_created_on_0, DateHandlers.UnixUnsignedToDateTime(nilfsSb.ctime))
.AppendLine();
2016-09-02 18:46:55 +01:00
2024-05-01 04:05:22 +01:00
sb.AppendFormat(Localization.Volume_last_mounted_on_0, DateHandlers.UnixUnsignedToDateTime(nilfsSb.mtime))
.AppendLine();
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
sb.AppendFormat(Localization.Volume_last_written_on_0, DateHandlers.UnixUnsignedToDateTime(nilfsSb.wtime))
.AppendLine();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
information = sb.ToString();
2016-09-02 18:46:55 +01:00
metadata = new FileSystem
{
Type = FS_TYPE,
ClusterSize = (uint)(1 << (int)(nilfsSb.log_block_size + 10)),
VolumeName = StringHandlers.CToString(nilfsSb.volume_name, encoding),
VolumeSerial = nilfsSb.uuid.ToString(),
CreationDate = DateHandlers.UnixUnsignedToDateTime(nilfsSb.ctime),
ModificationDate = DateHandlers.UnixUnsignedToDateTime(nilfsSb.wtime)
2022-03-06 13:29:38 +00:00
};
2024-05-01 04:05:22 +01:00
if(nilfsSb.creator_os == 0) metadata.SystemIdentifier = "Linux";
2022-03-06 13:29:38 +00:00
metadata.Clusters = nilfsSb.dev_size / metadata.ClusterSize;
2022-03-06 13:29:38 +00:00
}
#endregion
2016-09-02 18:46:55 +01:00
}