-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Partitions.cs
203 lines (168 loc) · 8.43 KB
/
Partitions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Partitions.cs
// Author(s) : Natalia Portillo <[email protected]>
//
// 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2023 Natalia Portillo
// ****************************************************************************/
using System.Collections.Generic;
using System.Linq;
using Aaru.CommonTypes;
using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Structs;
using Aaru.Console;
namespace Aaru.Core;
/// <summary>Implements methods for handling partitions</summary>
public static class Partitions
{
/// <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)
{
PluginBase plugins = GetPluginBase.Instance;
List<Partition> foundPartitions = new();
List<Partition> childPartitions = new();
List<ulong> checkedLocations = new();
var tapeImage = image as ITapeImage;
var partitionableImage = image as IPartitionableMediaImage;
// 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);
AaruConsole.DebugWriteLine("Partitions", Localization.Core.Found_0_at_1, partitionPlugin.Name,
tapeFile.FirstBlock);
}
checkedLocations.Add(tapeFile.FirstBlock);
}
// 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))
{
foundPartitions.AddRange(partitions);
AaruConsole.DebugWriteLine("Partitions", Localization.Core.Found_0_at_1, partitionPlugin.Name,
imagePartition.Start);
}
checkedLocations.Add(imagePartition.Start);
}
// 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))
{
foundPartitions.AddRange(partitions);
AaruConsole.DebugWriteLine("Partitions", Localization.Core.Found_0_at_zero, partitionPlugin.Name);
}
checkedLocations.Add(0);
}
while(foundPartitions.Count > 0)
{
if(checkedLocations.Contains(foundPartitions[0].Start))
{
childPartitions.Add(foundPartitions[0]);
foundPartitions.RemoveAt(0);
continue;
}
List<Partition> children = new();
foreach(IPartition partitionPlugin in plugins.PartPluginsList.Values)
{
AaruConsole.DebugWriteLine("Partitions", Localization.Core.Trying_0_at_1, partitionPlugin.Name,
foundPartitions[0].Start);
if(!partitionPlugin.GetInformation(image, out List<Partition> partitions, foundPartitions[0].Start))
continue;
AaruConsole.DebugWriteLine("Partitions", Localization.Core.Found_0_at_1, partitionPlugin.Name,
foundPartitions[0].Start);
children.AddRange(partitions);
}
checkedLocations.Add(foundPartitions[0].Start);
AaruConsole.DebugWriteLine("Partitions", Localization.Core.Got_0_children, children.Count);
if(children.Count > 0)
{
foundPartitions.RemoveAt(0);
foreach(Partition child in children)
if(checkedLocations.Contains(child.Start))
childPartitions.Add(child);
else
foundPartitions.Add(child);
}
else
{
childPartitions.Add(foundPartitions[0]);
foundPartitions.RemoveAt(0);
}
AaruConsole.DebugWriteLine("Partitions", Localization.Core.Got_0_parents, foundPartitions.Count);
AaruConsole.DebugWriteLine("Partitions", Localization.Core.Got_0_partitions, childPartitions.Count);
}
// Be sure that device partitions are not excluded if not mapped by any scheme...
if(tapeImage is not null)
{
List<ulong> startLocations = childPartitions.Select(detectedPartition => detectedPartition.Start).ToList();
if(tapeImage.Files != null)
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
}));
}
if(partitionableImage is not null)
{
List<ulong> startLocations = childPartitions.Select(detectedPartition => detectedPartition.Start).ToList();
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();
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)
{
if(partitions == null ||
partitions.Count == 0)
return;
List<string> schemes = new();
foreach(Partition part in partitions.Where(part => !schemes.Contains(part.Scheme)))
schemes.Add(part.Scheme);
foreach(string scheme in schemes)
Statistics.AddPartition(scheme);
}
}