Files
Aaru/Aaru.Filesystems/NILFS2.cs

210 lines
8.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
// ----------------------------------------------------------------------------
//
// Filename : NILFS2.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : NILFS2 filesystem plugin.
2016-09-02 18:46:55 +01:00
//
// --[ Description ] ----------------------------------------------------------
//
// Identifies the NILFS2 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/>.
//
// ----------------------------------------------------------------------------
2020-01-03 17:51:30 +00:00
// Copyright © 2011-2020 Natalia Portillo
2016-09-02 18:46: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.Interfaces;
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 18:46:55 +01:00
2020-07-20 07:47:12 +01:00
// ReSharper disable UnusedMember.Local
2020-02-27 00:33:26 +00:00
namespace Aaru.Filesystems
2016-09-02 18:46:55 +01:00
{
public class NILFS2 : IFilesystem
2016-09-02 18:46:55 +01:00
{
2018-06-22 08:08:38 +01:00
const ushort NILFS2_MAGIC = 0x3434;
const uint NILFS2_SUPER_OFFSET = 1024;
2016-09-02 18:46:55 +01:00
2017-12-26 08:01:40 +00:00
public FileSystemType XmlFsType { get; private set; }
2018-06-22 08:08:38 +01:00
public Encoding Encoding { get; private set; }
public string Name => "NILFS2 Plugin";
public Guid Id => new Guid("35224226-C5CC-48B5-8FFD-3781E91E86B6");
2018-08-29 22:15:43 +01:00
public string Author => "Natalia Portillo";
public bool Identify(IMediaImage imagePlugin, Partition partition)
2016-09-02 18:46:55 +01:00
{
2020-02-29 18:03:35 +00:00
if(imagePlugin.Info.SectorSize < 512)
return false;
2016-09-02 18:46:55 +01:00
2020-02-29 18:03:35 +00:00
uint sbAddr = NILFS2_SUPER_OFFSET / imagePlugin.Info.SectorSize;
if(sbAddr == 0)
sbAddr = 1;
2016-09-02 18:46:55 +01:00
uint sbSize = (uint)(Marshal.SizeOf<NILFS2_Superblock>() / imagePlugin.Info.SectorSize);
2016-09-02 18:46:55 +01:00
2020-02-29 18:03:35 +00:00
if(Marshal.SizeOf<NILFS2_Superblock>() % imagePlugin.Info.SectorSize != 0)
sbSize++;
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);
2020-02-29 18:03:35 +00:00
if(sector.Length < Marshal.SizeOf<NILFS2_Superblock>())
return false;
2016-09-02 18:46:55 +01:00
NILFS2_Superblock nilfsSb = Marshal.ByteArrayToStructureLittleEndian<NILFS2_Superblock>(sector);
2016-09-02 18:46:55 +01:00
return nilfsSb.magic == NILFS2_MAGIC;
2016-09-02 18:46:55 +01:00
}
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 18:46:55 +01:00
{
2018-06-22 08:08:38 +01:00
Encoding = encoding ?? Encoding.UTF8;
2016-09-02 18:46:55 +01:00
information = "";
2020-02-29 18:03:35 +00:00
if(imagePlugin.Info.SectorSize < 512)
return;
uint sbAddr = NILFS2_SUPER_OFFSET / imagePlugin.Info.SectorSize;
if(sbAddr == 0)
sbAddr = 1;
2016-09-02 18:46:55 +01:00
uint sbSize = (uint)(Marshal.SizeOf<NILFS2_Superblock>() / imagePlugin.Info.SectorSize);
2020-02-29 18:03:35 +00:00
if(Marshal.SizeOf<NILFS2_Superblock>() % imagePlugin.Info.SectorSize != 0)
sbSize++;
2016-09-02 18:46:55 +01:00
2017-07-19 16:37:11 +01:00
byte[] sector = imagePlugin.ReadSectors(partition.Start + sbAddr, sbSize);
2020-02-29 18:03:35 +00:00
if(sector.Length < Marshal.SizeOf<NILFS2_Superblock>())
return;
2016-09-02 18:46:55 +01:00
NILFS2_Superblock nilfsSb = Marshal.ByteArrayToStructureLittleEndian<NILFS2_Superblock>(sector);
2016-09-02 18:46:55 +01:00
2020-02-29 18:03:35 +00:00
if(nilfsSb.magic != NILFS2_MAGIC)
return;
2016-09-02 18:46:55 +01:00
2020-02-29 18:03:35 +00:00
var sb = new StringBuilder();
2016-09-02 18:46:55 +01:00
sb.AppendLine("NILFS2 filesystem");
sb.AppendFormat("Version {0}.{1}", nilfsSb.rev_level, nilfsSb.minor_rev_level).AppendLine();
sb.AppendFormat("{0} bytes per block", 1 << (int)(nilfsSb.log_block_size + 10)).AppendLine();
sb.AppendFormat("{0} bytes in volume", nilfsSb.dev_size).AppendLine();
sb.AppendFormat("{0} blocks per segment", nilfsSb.blocks_per_segment).AppendLine();
sb.AppendFormat("{0} segments", nilfsSb.nsegments).AppendLine();
2020-02-29 18:03:35 +00:00
if(nilfsSb.creator_os == 0)
sb.AppendLine("Filesystem created on Linux");
else
sb.AppendFormat("Creator OS code: {0}", nilfsSb.creator_os).AppendLine();
2016-09-02 18:46:55 +01:00
sb.AppendFormat("{0} bytes per inode", nilfsSb.inode_size).AppendLine();
sb.AppendFormat("Volume UUID: {0}", nilfsSb.uuid).AppendLine();
2017-12-26 08:01:40 +00:00
sb.AppendFormat("Volume name: {0}", StringHandlers.CToString(nilfsSb.volume_name, Encoding)).AppendLine();
2017-12-23 03:59:48 +00:00
sb.AppendFormat("Volume created on {0}", DateHandlers.UnixUnsignedToDateTime(nilfsSb.ctime)).AppendLine();
2020-02-29 18:03:35 +00:00
sb.AppendFormat("Volume last mounted on {0}", DateHandlers.UnixUnsignedToDateTime(nilfsSb.mtime)).
AppendLine();
sb.AppendFormat("Volume last written on {0}", DateHandlers.UnixUnsignedToDateTime(nilfsSb.wtime)).
AppendLine();
2016-09-02 18:46:55 +01:00
information = sb.ToString();
2017-12-26 08:01:40 +00:00
XmlFsType = new FileSystemType
{
2020-07-20 04:34:16 +01:00
Type = "NILFS2 filesystem",
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),
CreationDateSpecified = true,
ModificationDate = DateHandlers.UnixUnsignedToDateTime(nilfsSb.wtime),
ModificationDateSpecified = true
};
2020-02-29 18:03:35 +00:00
if(nilfsSb.creator_os == 0)
XmlFsType.SystemIdentifier = "Linux";
2019-04-23 01:38:33 +01:00
XmlFsType.Clusters = nilfsSb.dev_size / XmlFsType.ClusterSize;
2016-09-02 18:46:55 +01:00
}
enum NILFS2_State : ushort
{
2020-02-29 18:03:35 +00:00
Valid = 0x0001, Error = 0x0002, Resize = 0x0004
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct NILFS2_Superblock
{
2019-04-23 01:38:33 +01:00
public readonly uint rev_level;
public readonly ushort minor_rev_level;
public readonly ushort magic;
public readonly ushort bytes;
public readonly ushort flags;
public readonly uint crc_seed;
public readonly uint sum;
public readonly uint log_block_size;
public readonly ulong nsegments;
public readonly ulong dev_size;
public readonly ulong first_data_block;
public readonly uint blocks_per_segment;
public readonly uint r_segments_percentage;
public readonly ulong last_cno;
public readonly ulong last_pseg;
public readonly ulong last_seq;
public readonly ulong free_blocks_count;
public readonly ulong ctime;
public readonly ulong mtime;
public readonly ulong wtime;
public readonly ushort mnt_count;
public readonly ushort max_mnt_count;
public readonly NILFS2_State state;
public readonly ushort errors;
public readonly ulong lastcheck;
public readonly uint checkinterval;
public readonly uint creator_os;
public readonly ushort def_resuid;
public readonly ushort def_resgid;
public readonly uint first_ino;
public readonly ushort inode_size;
public readonly ushort dat_entry_size;
public readonly ushort checkpoint_size;
public readonly ushort segment_usage_size;
public readonly Guid uuid;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 80)]
2019-04-23 01:38:33 +01:00
public readonly byte[] volume_name;
public readonly uint c_interval;
public readonly uint c_block_max;
public readonly ulong feature_compat;
public readonly ulong feature_compat_ro;
public readonly ulong feature_incompat;
}
2016-09-02 18:46:55 +01:00
}
}