Files
Aaru/Aaru.Core/Partitions.cs

205 lines
8.8 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Partitions.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Core algorithms.
//
// --[ Description ] ----------------------------------------------------------
//
// Logic to find partitions
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2020-01-03 17:51:30 +00:00
// Copyright © 2011-2020 Natalia Portillo
// ****************************************************************************/
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;
2020-02-27 00:33:26 +00:00
namespace Aaru.Core
{
2020-02-29 18:03:35 +00:00
/// <summary>Implements methods for handling partitions</summary>
public static class Partitions
{
2020-02-29 18:03:35 +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)
{
2018-07-20 22:53:46 +01:00
PluginBase plugins = GetPluginBase.Instance;
2018-06-22 08:08:38 +01:00
List<Partition> foundPartitions = new List<Partition>();
List<Partition> childPartitions = new List<Partition>();
List<ulong> checkedLocations = new List<ulong>();
2020-02-29 18:03:35 +00:00
var tapeImage = image as ITapeImage;
var partitionableImage = image as IPartitionableMediaImage;
2019-05-02 19:15:04 +01:00
// Create partitions from image files
if(tapeImage?.Files != null)
foreach(TapeFile tapeFile in tapeImage.Files)
{
foreach(IPartition partitionPlugin in plugins.PartPluginsList.Values)
if(partitionPlugin.GetInformation(image, out List<Partition> partitions, tapeFile.FirstBlock))
{
foundPartitions.AddRange(partitions);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partitions", "Found {0} @ {1}", partitionPlugin.Name,
2020-02-29 18:03:35 +00:00
tapeFile.FirstBlock);
2019-05-02 19:15:04 +01:00
}
checkedLocations.Add(tapeFile.FirstBlock);
}
2020-02-29 18:03:35 +00:00
// Getting all partitions from device (e.g. tracks)
if(partitionableImage?.Partitions != null)
foreach(Partition imagePartition in partitionableImage.Partitions)
{
foreach(IPartition partitionPlugin in plugins.PartPluginsList.Values)
if(partitionPlugin.GetInformation(image, out List<Partition> partitions, imagePartition.Start))
2017-07-30 15:45:28 +01:00
{
foundPartitions.AddRange(partitions);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partitions", "Found {0} @ {1}", partitionPlugin.Name,
2020-02-29 18:03:35 +00:00
imagePartition.Start);
2017-07-30 15:45:28 +01:00
}
2017-12-19 20:33:03 +00:00
2017-07-26 23:45:20 +01:00
checkedLocations.Add(imagePartition.Start);
}
2020-02-29 18:03:35 +00:00
// Getting all partitions at start of device
if(!checkedLocations.Contains(0))
{
foreach(IPartition partitionPlugin in plugins.PartPluginsList.Values)
if(partitionPlugin.GetInformation(image, out List<Partition> partitions, 0))
2017-07-30 15:45:28 +01:00
{
foundPartitions.AddRange(partitions);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partitions", "Found {0} @ 0", partitionPlugin.Name);
2017-07-30 15:45:28 +01:00
}
2017-07-26 23:45:20 +01:00
checkedLocations.Add(0);
}
while(foundPartitions.Count > 0)
{
if(checkedLocations.Contains(foundPartitions[0].Start))
2017-07-30 15:45:28 +01:00
{
childPartitions.Add(foundPartitions[0]);
foundPartitions.RemoveAt(0);
2020-02-29 18:03:35 +00:00
2017-07-30 15:45:28 +01:00
continue;
}
2020-07-22 13:20:25 +01:00
List<Partition> children = new List<Partition>();
foreach(IPartition partitionPlugin in plugins.PartPluginsList.Values)
{
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partitions", "Trying {0} @ {1}", partitionPlugin.Name,
2020-02-29 18:03:35 +00:00
foundPartitions[0].Start);
2018-06-22 08:08:38 +01:00
if(!partitionPlugin.GetInformation(image, out List<Partition> partitions, foundPartitions[0].Start))
continue;
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partitions", "Found {0} @ {1}", partitionPlugin.Name,
2020-02-29 18:03:35 +00:00
foundPartitions[0].Start);
2020-07-22 13:20:25 +01:00
children.AddRange(partitions);
}
checkedLocations.Add(foundPartitions[0].Start);
2017-07-26 23:45:20 +01:00
2020-07-22 13:20:25 +01:00
AaruConsole.DebugWriteLine("Partitions", "Got {0} children", children.Count);
2020-07-22 13:20:25 +01:00
if(children.Count > 0)
{
foundPartitions.RemoveAt(0);
2020-07-22 13:20:25 +01:00
foreach(Partition child in children)
2020-02-29 18:03:35 +00:00
if(checkedLocations.Contains(child.Start))
childPartitions.Add(child);
else
foundPartitions.Add(child);
}
else
{
childPartitions.Add(foundPartitions[0]);
foundPartitions.RemoveAt(0);
}
2020-02-29 18:03:35 +00:00
AaruConsole.DebugWriteLine("Partitions", "Got {0} parents", foundPartitions.Count);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partitions", "Got {0} partitions", childPartitions.Count);
}
// Be sure that device partitions are not excluded if not mapped by any scheme...
2019-05-02 19:15:04 +01:00
if(!(tapeImage is null))
{
List<ulong> startLocations =
childPartitions.Select(detectedPartition => detectedPartition.Start).ToList();
if(tapeImage.Files != null)
2020-02-29 18:03:35 +00:00
childPartitions.AddRange(tapeImage.Files.Where(f => !startLocations.Contains(f.FirstBlock)).
Select(tapeFile => new Partition
2019-05-02 19:15:04 +01:00
{
2020-02-29 18:03:35 +00:00
Start = tapeFile.FirstBlock,
Length = (tapeFile.LastBlock - tapeFile.FirstBlock) + 1,
2019-05-02 19:15:04 +01:00
Sequence = tapeFile.File
}));
}
if(!(partitionableImage is null))
{
List<ulong> startLocations =
childPartitions.Select(detectedPartition => detectedPartition.Start).ToList();
2019-02-03 15:42:27 +00:00
if(partitionableImage.Partitions != null)
childPartitions.AddRange(partitionableImage.Partitions.Where(imagePartition =>
!startLocations.Contains(imagePartition.Start)));
}
Partition[] childArray = childPartitions.OrderBy(part => part.Start).ThenBy(part => part.Length).
ThenBy(part => part.Scheme).ToArray();
2020-02-29 18:03:35 +00:00
for(long i = 0; i < childArray.LongLength; i++)
childArray[i].Sequence = (ulong)i;
return childArray.ToList();
}
2020-02-29 18:03:35 +00:00
/// <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)
{
2020-02-29 18:03:35 +00:00
if(partitions == null ||
partitions.Count == 0)
return;
2017-12-19 20:33:03 +00:00
List<string> schemes = new List<string>();
foreach(Partition part in partitions.Where(part => !schemes.Contains(part.Scheme)))
schemes.Add(part.Scheme);
2020-02-29 18:03:35 +00:00
foreach(string scheme in schemes)
Statistics.AddPartition(scheme);
}
}
2017-12-19 20:33:03 +00:00
}