2017-07-24 23:35:33 +01:00
|
|
|
|
// /***************************************************************************
|
|
|
|
|
|
// The Disc Image Chef
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// 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/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Copyright © 2011-2018 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;
|
2017-07-24 23:35:33 +01:00
|
|
|
|
using DiscImageChef.CommonTypes;
|
2017-12-19 19:33:46 +00:00
|
|
|
|
using DiscImageChef.Console;
|
2017-12-20 17:15:26 +00:00
|
|
|
|
using DiscImageChef.DiscImages;
|
|
|
|
|
|
using DiscImageChef.Partitions;
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
|
|
|
|
|
namespace DiscImageChef.Core
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class Partitions
|
|
|
|
|
|
{
|
|
|
|
|
|
public static List<Partition> GetAll(ImagePlugin image)
|
|
|
|
|
|
{
|
|
|
|
|
|
PluginBase plugins = new PluginBase();
|
|
|
|
|
|
plugins.RegisterAllPlugins();
|
|
|
|
|
|
List<Partition> partitions = new List<Partition>();
|
|
|
|
|
|
List<Partition> childPartitions = new List<Partition>();
|
2017-07-26 23:45:20 +01:00
|
|
|
|
List<ulong> checkedLocations = new List<ulong>();
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
|
|
|
|
|
// Getting all partitions from device (e.g. tracks)
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(image.ImageInfo.ImageHasPartitions)
|
2017-07-24 23:35:33 +01:00
|
|
|
|
{
|
|
|
|
|
|
foreach(Partition imagePartition in image.GetPartitions())
|
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
|
foreach(PartitionPlugin _partplugin in plugins.PartPluginsList.Values)
|
2017-07-24 23:35:33 +01:00
|
|
|
|
{
|
|
|
|
|
|
if(_partplugin.GetInformation(image, out List<Partition> _partitions, imagePartition.Start))
|
2017-07-30 15:45:28 +01:00
|
|
|
|
{
|
2017-07-24 23:35:33 +01:00
|
|
|
|
partitions.AddRange(_partitions);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
DicConsole.DebugWriteLine("Partitions", "Found {0} @ {1}", _partplugin.Name,
|
|
|
|
|
|
imagePartition.Start);
|
2017-07-30 15:45:28 +01:00
|
|
|
|
}
|
2017-07-24 23:35:33 +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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// Getting all partitions at start of device
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
|
foreach(PartitionPlugin _partplugin in plugins.PartPluginsList.Values)
|
2017-07-24 23:35:33 +01:00
|
|
|
|
{
|
|
|
|
|
|
if(_partplugin.GetInformation(image, out List<Partition> _partitions, 0))
|
2017-07-30 15:45:28 +01:00
|
|
|
|
{
|
2017-07-24 23:35:33 +01:00
|
|
|
|
partitions.AddRange(_partitions);
|
2017-07-30 15:45:28 +01:00
|
|
|
|
DicConsole.DebugWriteLine("Partitions", "Found {0} @ 0", _partplugin.Name);
|
|
|
|
|
|
}
|
2017-07-24 23:35:33 +01:00
|
|
|
|
}
|
2017-07-26 23:45:20 +01:00
|
|
|
|
|
|
|
|
|
|
checkedLocations.Add(0);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while(partitions.Count > 0)
|
|
|
|
|
|
{
|
2017-07-30 15:45:28 +01:00
|
|
|
|
if(checkedLocations.Contains(partitions[0].Start))
|
|
|
|
|
|
{
|
|
|
|
|
|
childPartitions.Add(partitions[0]);
|
|
|
|
|
|
partitions.RemoveAt(0);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-24 23:35:33 +01:00
|
|
|
|
List<Partition> childs = new List<Partition>();
|
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
foreach(PartitionPlugin _partplugin in plugins.PartPluginsList.Values)
|
2017-07-24 23:35:33 +01:00
|
|
|
|
{
|
|
|
|
|
|
DicConsole.DebugWriteLine("Partitions", "Trying {0} @ {1}", _partplugin.Name, partitions[0].Start);
|
|
|
|
|
|
if(_partplugin.GetInformation(image, out List<Partition> _partitions, partitions[0].Start))
|
2017-07-26 23:45:20 +01:00
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
|
DicConsole.DebugWriteLine("Partitions", "Found {0} @ {1}", _partplugin.Name,
|
|
|
|
|
|
partitions[0].Start);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
childs.AddRange(_partitions);
|
2017-07-26 23:45:20 +01:00
|
|
|
|
}
|
2017-07-24 23:35:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-26 23:45:20 +01:00
|
|
|
|
checkedLocations.Add(partitions[0].Start);
|
|
|
|
|
|
|
2017-07-24 23:35:33 +01:00
|
|
|
|
DicConsole.DebugWriteLine("Partitions", "Got {0} childs", childs.Count);
|
|
|
|
|
|
|
|
|
|
|
|
if(childs.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
partitions.RemoveAt(0);
|
|
|
|
|
|
|
|
|
|
|
|
foreach(Partition child in childs)
|
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(checkedLocations.Contains(child.Start)) childPartitions.Add(child);
|
|
|
|
|
|
else partitions.Add(child);
|
2017-07-24 23:35:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
childPartitions.Add(partitions[0]);
|
|
|
|
|
|
partitions.RemoveAt(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DicConsole.DebugWriteLine("Partitions", "Got {0} parents", partitions.Count);
|
|
|
|
|
|
DicConsole.DebugWriteLine("Partitions", "Got {0} partitions", childPartitions.Count);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-07 16:06:51 +01:00
|
|
|
|
// Be sure that device partitions are not excluded if not mapped by any scheme...
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(image.ImageInfo.ImageHasPartitions)
|
2017-08-07 16:06:51 +01:00
|
|
|
|
{
|
|
|
|
|
|
List<ulong> startLocations = new List<ulong>();
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
foreach(Partition detectedPartition in childPartitions) startLocations.Add(detectedPartition.Start);
|
2017-08-07 16:06:51 +01:00
|
|
|
|
|
|
|
|
|
|
foreach(Partition imagePartition in image.GetPartitions())
|
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(!startLocations.Contains(imagePartition.Start)) childPartitions.Add(imagePartition);
|
2017-08-07 16:06:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-19 20:33:03 +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
|
|
|
|
|
2017-12-19 20:33:03 +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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void AddSchemesToStats(List<Partition> partitions)
|
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(partitions == null || partitions.Count == 0) return;
|
|
|
|
|
|
|
2017-07-24 23:35:33 +01:00
|
|
|
|
List<string> schemes = new List<string>();
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
foreach(Partition part in partitions) { if(!schemes.Contains(part.Scheme)) schemes.Add(part.Scheme); }
|
2017-07-24 23:35:33 +01:00
|
|
|
|
|
2017-12-19 20:33:03 +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
|
|
|
|
}
|