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/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-01-03 17:51:30 +00:00
|
|
|
|
// Copyright © 2011-2020 Natalia Portillo
|
2017-07-24 23:35:33 +01:00
|
|
|
|
// ****************************************************************************/
|
2017-12-19 03:50:57 +00:00
|
|
|
|
|
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
|
|
|
|
|
2020-02-27 00:33:26 +00:00
|
|
|
|
namespace Aaru.Core
|
2017-07-24 23:35:33 +01:00
|
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Implements methods for handling partitions</summary>
|
2017-07-24 23:35:33 +01:00
|
|
|
|
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>
|
2017-12-23 01:46:08 +00:00
|
|
|
|
/// <param name="image">Image</param>
|
|
|
|
|
|
/// <returns>List of found partitions</returns>
|
2017-12-26 06:05:12 +00:00
|
|
|
|
public static List<Partition> GetAll(IMediaImage image)
|
2017-07-24 23:35:33 +01:00
|
|
|
|
{
|
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>();
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
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
|
|
|
|
|
2017-07-24 23:35:33 +01:00
|
|
|
|
// Getting all partitions from device (e.g. tracks)
|
2020-03-09 20:39:25 +00:00
|
|
|
|
if(partitionableImage?.Partitions != null)
|
2019-01-20 20:11:10 +00:00
|
|
|
|
foreach(Partition imagePartition in partitionableImage.Partitions)
|
2017-07-24 23:35:33 +01:00
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
|
foreach(IPartition partitionPlugin in plugins.PartPluginsList.Values)
|
2017-12-21 23:00:30 +00:00
|
|
|
|
if(partitionPlugin.GetInformation(image, out List<Partition> partitions, imagePartition.Start))
|
2017-07-30 15:45:28 +01:00
|
|
|
|
{
|
2017-12-21 23:00:30 +00: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);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2017-07-24 23:35:33 +01:00
|
|
|
|
// Getting all partitions at start of device
|
2020-03-09 20:39:25 +00:00
|
|
|
|
if(!checkedLocations.Contains(0))
|
2017-07-24 23:35:33 +01:00
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
|
foreach(IPartition partitionPlugin in plugins.PartPluginsList.Values)
|
2017-12-21 23:00:30 +00:00
|
|
|
|
if(partitionPlugin.GetInformation(image, out List<Partition> partitions, 0))
|
2017-07-30 15:45:28 +01:00
|
|
|
|
{
|
2017-12-21 23:00:30 +00: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);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-21 23:00:30 +00:00
|
|
|
|
while(foundPartitions.Count > 0)
|
2017-07-24 23:35:33 +01:00
|
|
|
|
{
|
2017-12-21 23:00:30 +00:00
|
|
|
|
if(checkedLocations.Contains(foundPartitions[0].Start))
|
2017-07-30 15:45:28 +01:00
|
|
|
|
{
|
2017-12-21 23:00:30 +00: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>();
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
foreach(IPartition partitionPlugin in plugins.PartPluginsList.Values)
|
2017-07-24 23:35:33 +01:00
|
|
|
|
{
|
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;
|
2017-12-21 06:06:19 +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
|
|
|
|
foundPartitions[0].Start);
|
|
|
|
|
|
|
2020-07-22 13:20:25 +01:00
|
|
|
|
children.AddRange(partitions);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-21 23:00:30 +00:00
|
|
|
|
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);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
2020-07-22 13:20:25 +01:00
|
|
|
|
if(children.Count > 0)
|
2017-07-24 23:35:33 +01:00
|
|
|
|
{
|
2017-12-21 23:00:30 +00:00
|
|
|
|
foundPartitions.RemoveAt(0);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
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);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2017-12-21 23:00:30 +00:00
|
|
|
|
childPartitions.Add(foundPartitions[0]);
|
|
|
|
|
|
foundPartitions.RemoveAt(0);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
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);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-07 16:06:51 +01:00
|
|
|
|
// 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
|
|
|
|
|
|
}));
|
|
|
|
|
|
}
|
2020-03-09 20:39:25 +00:00
|
|
|
|
|
|
|
|
|
|
if(!(partitionableImage is null))
|
2017-08-07 16:06:51 +01:00
|
|
|
|
{
|
2017-12-23 17:41:23 +00:00
|
|
|
|
List<ulong> startLocations =
|
|
|
|
|
|
childPartitions.Select(detectedPartition => detectedPartition.Start).ToList();
|
2017-08-07 16:06:51 +01:00
|
|
|
|
|
2019-02-03 15:42:27 +00:00
|
|
|
|
if(partitionableImage.Partitions != null)
|
|
|
|
|
|
childPartitions.AddRange(partitionableImage.Partitions.Where(imagePartition =>
|
2020-02-29 18:03:35 +00:00
|
|
|
|
!startLocations.
|
|
|
|
|
|
Contains(imagePartition.
|
|
|
|
|
|
Start)));
|
2017-08-07 16:06:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-29 18:03:35 +00: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
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
for(long i = 0; i < childArray.LongLength; i++)
|
|
|
|
|
|
childArray[i].Sequence = (ulong)i;
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
|
|
|
|
|
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>
|
2017-12-23 01:46:08 +00:00
|
|
|
|
/// <param name="partitions">List of partitions</param>
|
2017-07-24 23:35:33 +01:00
|
|
|
|
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
|
|
|
|
|
2017-07-24 23:35:33 +01:00
|
|
|
|
List<string> schemes = new List<string>();
|
|
|
|
|
|
|
2017-12-23 17:41:23 +00:00
|
|
|
|
foreach(Partition part in partitions.Where(part => !schemes.Contains(part.Scheme)))
|
|
|
|
|
|
schemes.Add(part.Scheme);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
foreach(string scheme in schemes)
|
|
|
|
|
|
Statistics.AddPartition(scheme);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|