2017-07-24 23:35:33 +01:00
|
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
|
// Aaru Data Preservation Suite
|
2017-07-24 23:35:33 +01:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : Partitions.cs
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
2017-07-24 23:35:33 +01:00
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Component : Core algorithms.
|
2017-07-24 23:35:33 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Logic to find partitions
|
2017-07-24 23:35:33 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
|
// it under the terms of the GNU General Public License as
|
|
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This program 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 General Public License for more details.
|
|
|
|
|
|
//
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2022-12-03 16:07:10 +00:00
|
|
|
|
// Copyright © 2011-2023 Natalia Portillo
|
2017-07-24 23:35:33 +01:00
|
|
|
|
// ****************************************************************************/
|
2017-12-19 03:50:57 +00:00
|
|
|
|
|
2022-12-17 19:16:42 +00:00
|
|
|
|
using System;
|
2017-07-24 23:35:33 +01:00
|
|
|
|
using System.Collections.Generic;
|
2017-12-19 19:33:46 +00:00
|
|
|
|
using System.Linq;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Aaru.CommonTypes;
|
|
|
|
|
|
using Aaru.CommonTypes.Interfaces;
|
|
|
|
|
|
using Aaru.CommonTypes.Structs;
|
|
|
|
|
|
using Aaru.Console;
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
|
namespace Aaru.Core;
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <summary>Implements methods for handling partitions</summary>
|
|
|
|
|
|
public static class Partitions
|
2017-07-24 23:35:33 +01:00
|
|
|
|
{
|
2023-10-03 17:17:16 +01:00
|
|
|
|
const string MODULE_NAME = "Partitions";
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <summary>Gets a list of all partitions present in the specified image</summary>
|
|
|
|
|
|
/// <param name="image">Image</param>
|
|
|
|
|
|
/// <returns>List of found partitions</returns>
|
|
|
|
|
|
public static List<Partition> GetAll(IMediaImage image)
|
2017-07-24 23:35:33 +01:00
|
|
|
|
{
|
2022-12-17 20:50:17 +00:00
|
|
|
|
PluginBase plugins = PluginBase.Singleton;
|
2022-11-15 15:58:43 +00:00
|
|
|
|
List<Partition> foundPartitions = new();
|
|
|
|
|
|
List<Partition> childPartitions = new();
|
|
|
|
|
|
List<ulong> checkedLocations = new();
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
var tapeImage = image as ITapeImage;
|
|
|
|
|
|
var partitionableImage = image as IPartitionableMediaImage;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
// Create partitions from image files
|
|
|
|
|
|
if(tapeImage?.Files != null)
|
2023-10-03 22:57:50 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
foreach(TapeFile tapeFile in tapeImage.Files)
|
|
|
|
|
|
{
|
2022-12-17 19:16:42 +00:00
|
|
|
|
foreach(Type pluginType in plugins.Partitions.Values)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(Activator.CreateInstance(pluginType) is not IPartition part)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
if(!part.GetInformation(image, out List<Partition> partitions, tapeFile.FirstBlock))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
foundPartitions.AddRange(partitions);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2023-10-03 17:17:16 +01:00
|
|
|
|
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.Core.Found_0_at_1, part.Name,
|
2022-12-17 19:16:42 +00:00
|
|
|
|
tapeFile.FirstBlock);
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
checkedLocations.Add(tapeFile.FirstBlock);
|
|
|
|
|
|
}
|
2023-10-03 22:57:50 +01:00
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
// Getting all partitions from device (e.g. tracks)
|
|
|
|
|
|
if(partitionableImage?.Partitions != null)
|
2023-10-03 22:57:50 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
foreach(Partition imagePartition in partitionableImage.Partitions)
|
2017-07-24 23:35:33 +01:00
|
|
|
|
{
|
2022-12-17 19:16:42 +00:00
|
|
|
|
foreach(Type pluginType in plugins.Partitions.Values)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(Activator.CreateInstance(pluginType) is not IPartition part)
|
|
|
|
|
|
continue;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2022-12-17 19:16:42 +00:00
|
|
|
|
if(!part.GetInformation(image, out List<Partition> partitions, imagePartition.Start))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
foundPartitions.AddRange(partitions);
|
|
|
|
|
|
|
2023-10-03 17:17:16 +01:00
|
|
|
|
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.Core.Found_0_at_1, part.Name,
|
2022-12-17 19:16:42 +00:00
|
|
|
|
imagePartition.Start);
|
|
|
|
|
|
}
|
2017-07-26 23:45:20 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
checkedLocations.Add(imagePartition.Start);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
}
|
2023-10-03 22:57:50 +01:00
|
|
|
|
}
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
// Getting all partitions at start of device
|
|
|
|
|
|
if(!checkedLocations.Contains(0))
|
|
|
|
|
|
{
|
2022-12-17 19:16:42 +00:00
|
|
|
|
foreach(Type pluginType in plugins.Partitions.Values)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(Activator.CreateInstance(pluginType) is not IPartition part)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
if(!part.GetInformation(image, out List<Partition> partitions, 0))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
foundPartitions.AddRange(partitions);
|
2023-10-03 17:17:16 +01:00
|
|
|
|
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.Core.Found_0_at_zero, part.Name);
|
2022-12-17 19:16:42 +00:00
|
|
|
|
}
|
2017-07-30 15:45:28 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
checkedLocations.Add(0);
|
|
|
|
|
|
}
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
while(foundPartitions.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(checkedLocations.Contains(foundPartitions[0].Start))
|
|
|
|
|
|
{
|
|
|
|
|
|
childPartitions.Add(foundPartitions[0]);
|
|
|
|
|
|
foundPartitions.RemoveAt(0);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2017-12-21 06:06:19 +00:00
|
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
|
List<Partition> children = new();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-12-17 19:16:42 +00:00
|
|
|
|
foreach(Type pluginType in plugins.Partitions.Values)
|
2022-03-06 13:29:38 +00:00
|
|
|
|
{
|
2022-12-17 19:16:42 +00:00
|
|
|
|
if(Activator.CreateInstance(pluginType) is not IPartition part)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
2023-10-03 17:17:16 +01:00
|
|
|
|
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.Core.Trying_0_at_1, part.Name,
|
2022-03-06 13:29:38 +00:00
|
|
|
|
foundPartitions[0].Start);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
2022-12-17 19:16:42 +00:00
|
|
|
|
if(!part.GetInformation(image, out List<Partition> partitions, foundPartitions[0].Start))
|
2022-03-06 13:29:38 +00:00
|
|
|
|
continue;
|
2017-07-26 23:45:20 +01:00
|
|
|
|
|
2023-10-03 17:17:16 +01:00
|
|
|
|
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.Core.Found_0_at_1, part.Name,
|
2022-03-06 13:29:38 +00:00
|
|
|
|
foundPartitions[0].Start);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
children.AddRange(partitions);
|
|
|
|
|
|
}
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
checkedLocations.Add(foundPartitions[0].Start);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
2023-10-03 17:17:16 +01:00
|
|
|
|
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.Core.Got_0_children, children.Count);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(children.Count > 0)
|
2019-05-02 19:15:04 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
foundPartitions.RemoveAt(0);
|
2020-03-09 20:39:25 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
foreach(Partition child in children)
|
2023-10-03 22:57:50 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(checkedLocations.Contains(child.Start))
|
|
|
|
|
|
childPartitions.Add(child);
|
|
|
|
|
|
else
|
|
|
|
|
|
foundPartitions.Add(child);
|
2023-10-03 22:57:50 +01:00
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
2017-08-07 16:06:51 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
childPartitions.Add(foundPartitions[0]);
|
|
|
|
|
|
foundPartitions.RemoveAt(0);
|
2017-08-07 16:06:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-03 22:57:50 +01:00
|
|
|
|
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.Core.Got_0_parents, foundPartitions.Count);
|
2023-10-03 17:17:16 +01:00
|
|
|
|
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.Core.Got_0_partitions, childPartitions.Count);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
// Be sure that device partitions are not excluded if not mapped by any scheme...
|
2022-11-14 01:20:28 +00:00
|
|
|
|
if(tapeImage is not null)
|
2022-03-06 13:29:38 +00:00
|
|
|
|
{
|
2023-10-03 22:57:50 +01:00
|
|
|
|
var startLocations = childPartitions.Select(detectedPartition => detectedPartition.Start).ToList();
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
if(tapeImage.Files != null)
|
2023-10-03 22:57:50 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
childPartitions.AddRange(tapeImage.Files.Where(f => !startLocations.Contains(f.FirstBlock)).
|
|
|
|
|
|
Select(tapeFile => new Partition
|
|
|
|
|
|
{
|
|
|
|
|
|
Start = tapeFile.FirstBlock,
|
|
|
|
|
|
Length = tapeFile.LastBlock - tapeFile.FirstBlock + 1,
|
|
|
|
|
|
Sequence = tapeFile.File
|
|
|
|
|
|
}));
|
2023-10-03 22:57:50 +01:00
|
|
|
|
}
|
2017-07-24 23:35:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-14 01:20:28 +00:00
|
|
|
|
if(partitionableImage is not null)
|
2017-07-24 23:35:33 +01:00
|
|
|
|
{
|
2023-10-03 22:57:50 +01:00
|
|
|
|
var startLocations = childPartitions.Select(detectedPartition => detectedPartition.Start).ToList();
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(partitionableImage.Partitions != null)
|
2023-10-03 22:57:50 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
childPartitions.AddRange(partitionableImage.Partitions.Where(imagePartition =>
|
2022-03-07 07:36:44 +00:00
|
|
|
|
!startLocations.
|
|
|
|
|
|
Contains(imagePartition.Start)));
|
2023-10-03 22:57:50 +01:00
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
2023-10-04 17:34:40 +01:00
|
|
|
|
Partition[] childArray = childPartitions.OrderBy(part => part.Start).
|
|
|
|
|
|
ThenBy(part => part.Length).
|
|
|
|
|
|
ThenBy(part => part.Scheme).
|
|
|
|
|
|
ToArray();
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
for(long i = 0; i < childArray.LongLength; i++)
|
|
|
|
|
|
childArray[i].Sequence = (ulong)i;
|
|
|
|
|
|
|
|
|
|
|
|
return childArray.ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Adds all partition schemes from the specified list of partitions to statistics</summary>
|
|
|
|
|
|
/// <param name="partitions">List of partitions</param>
|
|
|
|
|
|
public static void AddSchemesToStats(List<Partition> partitions)
|
|
|
|
|
|
{
|
2023-10-04 17:34:40 +01:00
|
|
|
|
if(partitions == null || partitions.Count == 0)
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
|
List<string> schemes = new();
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
foreach(Partition part in partitions.Where(part => !schemes.Contains(part.Scheme)))
|
|
|
|
|
|
schemes.Add(part.Scheme);
|
|
|
|
|
|
|
|
|
|
|
|
foreach(string scheme in schemes)
|
|
|
|
|
|
Statistics.AddPartition(scheme);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|