Files
Aaru/Aaru.Core/Devices/Dumping/Metadata.cs

166 lines
6.8 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
2020-03-11 21:56:55 +00:00
// Filename : Metadata.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Core algorithms.
//
// --[ Description ] ----------------------------------------------------------
//
2020-03-11 21:56:55 +00:00
// Generates metadata from dumps.
//
// --[ 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
// ****************************************************************************/
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.AaruMetadata;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
2023-09-26 02:40:11 +01:00
using Humanizer;
using Humanizer.Bytes;
2023-09-26 03:39:10 +01:00
using Humanizer.Localisation;
namespace Aaru.Core.Devices.Dumping;
2022-03-06 13:29:38 +00:00
partial class Dump
{
2022-03-06 13:29:38 +00:00
/// <summary>Creates optical metadata sidecar</summary>
/// <param name="blockSize">Size of the read sector in bytes</param>
/// <param name="blocks">Total number of positive sectors</param>
/// <param name="mediaType">Disc type</param>
/// <param name="layers">Disc layers</param>
/// <param name="mediaTags">Media tags</param>
/// <param name="sessions">Disc sessions</param>
/// <param name="totalChkDuration">Total time spent doing checksums</param>
/// <param name="discOffset">Disc write offset</param>
void WriteOpticalSidecar(uint blockSize, ulong blocks, MediaType mediaType, Layers layers,
2022-03-06 13:29:38 +00:00
Dictionary<MediaTagType, byte[]> mediaTags, int sessions, out double totalChkDuration,
int? discOffset)
{
_dumpLog.WriteLine(Localization.Core.Creating_sidecar);
var filters = new FiltersList();
IFilter filter = filters.GetFilter(_outputPath);
2022-03-06 13:29:38 +00:00
totalChkDuration = 0;
2022-03-17 00:46:26 +00:00
if(ImageFormat.Detect(filter) is not IMediaImage inputPlugin)
{
StoppingErrorMessage?.Invoke(Localization.Core.Could_not_detect_image_format);
2022-03-17 00:46:26 +00:00
return;
}
2022-03-06 13:29:38 +00:00
ErrorNumber opened = inputPlugin.Open(filter);
if(opened != ErrorNumber.NoError)
{
StoppingErrorMessage?.Invoke(string.Format(Localization.Core.Error_0_opening_created_image, opened));
2022-03-06 13:29:38 +00:00
return;
}
2022-03-06 13:29:38 +00:00
_sidecarStopwatch.Restart();
2022-03-06 13:29:38 +00:00
// ReSharper disable once UseObjectOrCollectionInitializer
_sidecarClass = new Sidecar(inputPlugin, _outputPath, filter.Id, _encoding);
_sidecarClass.InitProgressEvent += InitProgress;
_sidecarClass.UpdateProgressEvent += UpdateProgress;
_sidecarClass.EndProgressEvent += EndProgress;
_sidecarClass.InitProgressEvent2 += InitProgress2;
_sidecarClass.UpdateProgressEvent2 += UpdateProgress2;
_sidecarClass.EndProgressEvent2 += EndProgress2;
_sidecarClass.UpdateStatusEvent += UpdateStatus;
Metadata sidecar = _sidecarClass.Create();
_sidecarStopwatch.Stop();
2022-03-06 13:29:38 +00:00
if(_aborted)
return;
totalChkDuration = _sidecarStopwatch.Elapsed.TotalMilliseconds;
_dumpLog.WriteLine(Localization.Core.Sidecar_created_in_0,
_sidecarStopwatch.Elapsed.Humanize(minUnit: TimeUnit.Second));
2022-03-06 13:29:38 +00:00
2023-09-26 02:40:11 +01:00
_dumpLog.WriteLine(Localization.Core.Average_checksum_speed_0,
ByteSize.FromBytes(blockSize * (blocks + 1)).Per(totalChkDuration.Milliseconds()).
Humanize());
2022-03-06 13:29:38 +00:00
if(_preSidecar != null)
{
_preSidecar.OpticalDiscs = sidecar.OpticalDiscs;
sidecar = _preSidecar;
2022-03-06 13:29:38 +00:00
}
List<(ulong start, string type)> filesystems = new();
if(sidecar.OpticalDiscs[0].Track != null)
2023-10-03 22:57:50 +01:00
{
filesystems.AddRange(from xmlTrack in sidecar.OpticalDiscs[0].Track
2023-10-03 22:57:50 +01:00
where xmlTrack.FileSystemInformation != null
from partition in xmlTrack.FileSystemInformation
where partition.FileSystems != null
2022-03-07 07:36:44 +00:00
from fileSystem in partition.FileSystems
2022-03-06 13:29:38 +00:00
select (partition.StartSector, fileSystem.Type));
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
if(filesystems.Count > 0)
2023-10-03 22:57:50 +01:00
{
2022-03-06 13:29:38 +00:00
foreach(var filesystem in filesystems.Select(o => new
{
o.start,
o.type
}).Distinct())
_dumpLog.WriteLine(Localization.Core.Found_filesystem_0_at_sector_1, filesystem.type, filesystem.start);
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
2022-12-17 20:59:12 +00:00
sidecar.OpticalDiscs[0].Dimensions = Dimensions.FromMediaType(mediaType);
2022-03-06 13:29:38 +00:00
(string type, string subType) discType = CommonTypes.Metadata.MediaType.MediaTypeToString(mediaType);
sidecar.OpticalDiscs[0].DiscType = discType.type;
sidecar.OpticalDiscs[0].DiscSubType = discType.subType;
sidecar.OpticalDiscs[0].DumpHardware = _resume.Tries;
sidecar.OpticalDiscs[0].Sessions = (uint)sessions;
sidecar.OpticalDiscs[0].Layers = layers;
2022-03-06 13:29:38 +00:00
if(discOffset.HasValue)
sidecar.OpticalDiscs[0].Offset = (int)(discOffset / 4);
2022-03-06 13:29:38 +00:00
if(mediaTags != null)
2023-10-03 22:57:50 +01:00
{
2022-03-07 07:36:44 +00:00
foreach(KeyValuePair<MediaTagType, byte[]> tag in mediaTags.Where(tag => _outputPlugin.SupportedMediaTags.
Contains(tag.Key)))
2022-03-06 13:29:38 +00:00
AddMediaTagToSidecar(_outputPath, tag.Key, tag.Value, ref sidecar);
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke(Localization.Core.Writing_metadata_sidecar);
2022-03-06 13:29:38 +00:00
var jsonFs = new FileStream(_outputPrefix + ".metadata.json", FileMode.Create);
JsonSerializer.Serialize(jsonFs, new MetadataJson
{
AaruMetadata = sidecar
}, typeof(MetadataJson), MetadataJsonContext.Default);
2022-03-06 13:29:38 +00:00
jsonFs.Close();
}
}