Deduplicate creation of optical disc sidecar.

This commit is contained in:
2019-12-31 21:27:16 +00:00
parent a8d8ff97d7
commit aa6b6ac8f7
7 changed files with 192 additions and 323 deletions

View File

@@ -241,6 +241,7 @@
</e>
<e p="Dump.cs" t="Include" />
<e p="MMC.cs" t="Include" />
<e p="Metadata.cs" t="Include" />
<e p="NVMe.cs" t="Include" />
<e p="PlayStationPortable.cs" t="Include" />
<e p="ResumeSupport.cs" t="Include" />

View File

@@ -35,23 +35,20 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using DiscImageChef.CommonTypes;
using DiscImageChef.CommonTypes.Enums;
using DiscImageChef.CommonTypes.Extents;
using DiscImageChef.CommonTypes.Interfaces;
using DiscImageChef.CommonTypes.Metadata;
using DiscImageChef.CommonTypes.Structs;
using DiscImageChef.Console;
using DiscImageChef.Core.Logging;
using DiscImageChef.Core.Media.Detection;
using DiscImageChef.Database.Models;
using DiscImageChef.Decoders.CD;
using DiscImageChef.Decoders.SCSI;
using DiscImageChef.Decoders.SCSI.MMC;
using DiscImageChef.Devices;
using Schemas;
using CdOffset = DiscImageChef.Database.Models.CdOffset;
using MediaType = DiscImageChef.CommonTypes.MediaType;
using PlatformID = DiscImageChef.CommonTypes.Interop.PlatformID;
using Session = DiscImageChef.Decoders.CD.Session;
using TrackType = DiscImageChef.CommonTypes.Enums.TrackType;
@@ -2111,79 +2108,7 @@ namespace DiscImageChef.Core.Devices.Dumping
double totalChkDuration = 0;
if(!_nometadata)
{
_dumpLog.WriteLine("Creating sidecar.");
var filters = new FiltersList();
IFilter filter = filters.GetFilter(_outputPath);
IMediaImage inputPlugin = ImageFormat.Detect(filter);
if(!inputPlugin.Open(filter))
{
StoppingErrorMessage?.Invoke("Could not open created image.");
return;
}
DateTime chkStart = DateTime.UtcNow;
// 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;
CICMMetadataType sidecar = _sidecarClass.Create();
end = DateTime.UtcNow;
totalChkDuration = (end - chkStart).TotalMilliseconds;
_dumpLog.WriteLine("Sidecar created in {0} seconds.", (end - chkStart).TotalSeconds);
_dumpLog.WriteLine("Average checksum speed {0:F3} KiB/sec.",
((double)blockSize * (double)(blocks + 1)) / 1024 / (totalChkDuration / 1000));
if(_preSidecar != null)
{
_preSidecar.OpticalDisc = sidecar.OpticalDisc;
sidecar = _preSidecar;
}
List<(ulong start, string type)> filesystems = new List<(ulong start, string type)>();
if(sidecar.OpticalDisc[0].Track != null)
filesystems.AddRange(from xmlTrack in sidecar.OpticalDisc[0].Track
where xmlTrack.FileSystemInformation != null
from partition in xmlTrack.FileSystemInformation
where partition.FileSystems != null from fileSystem in partition.FileSystems
select (partition.StartSector, fileSystem.Type));
if(filesystems.Count > 0)
foreach(var filesystem in filesystems.Select(o => new
{
o.start, o.type
}).Distinct())
_dumpLog.WriteLine("Found filesystem {0} at sector {1}", filesystem.type, filesystem.start);
sidecar.OpticalDisc[0].Dimensions = Dimensions.DimensionsFromMediaType(dskType);
(string type, string subType) discType = CommonTypes.Metadata.MediaType.MediaTypeToString(dskType);
sidecar.OpticalDisc[0].DiscType = discType.type;
sidecar.OpticalDisc[0].DiscSubType = discType.subType;
sidecar.OpticalDisc[0].DumpHardwareArray = _resume.Tries.ToArray();
foreach(KeyValuePair<MediaTagType, byte[]> tag in mediaTags)
if(_outputPlugin.SupportedMediaTags.Contains(tag.Key))
AddMediaTagToSidecar(_outputPath, tag, ref sidecar);
UpdateStatus?.Invoke("Writing metadata sidecar");
var xmlFs = new FileStream(_outputPrefix + ".cicm.xml", FileMode.Create);
var xmlSer = new XmlSerializer(typeof(CICMMetadataType));
xmlSer.Serialize(xmlFs, sidecar);
xmlFs.Close();
}
WriteOpticalSidecar(blockSize, blocks, dskType, null, mediaTags, sessions, out totalChkDuration);
end = DateTime.UtcNow;
UpdateStatus?.Invoke("");

View File

@@ -0,0 +1,138 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : SBC.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Core algorithms.
//
// --[ Description ] ----------------------------------------------------------
//
// Dumps SCSI Block devices.
//
// --[ 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-2019 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using DiscImageChef.CommonTypes;
using DiscImageChef.CommonTypes.Enums;
using DiscImageChef.CommonTypes.Interfaces;
using DiscImageChef.CommonTypes.Metadata;
using Schemas;
using MediaType = DiscImageChef.CommonTypes.MediaType;
namespace DiscImageChef.Core.Devices.Dumping
{
partial class Dump
{
/// <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>
void WriteOpticalSidecar(uint blockSize, ulong blocks, MediaType mediaType, LayersType layers,
Dictionary<MediaTagType, byte[]> mediaTags, int sessions, out double totalChkDuration)
{
_dumpLog.WriteLine("Creating sidecar.");
var filters = new FiltersList();
IFilter filter = filters.GetFilter(_outputPath);
IMediaImage inputPlugin = ImageFormat.Detect(filter);
totalChkDuration = 0;
if(!inputPlugin.Open(filter))
{
StoppingErrorMessage?.Invoke("Could not open created image.");
return;
}
DateTime chkStart = DateTime.UtcNow;
// 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;
CICMMetadataType sidecar = _sidecarClass.Create();
DateTime end = DateTime.UtcNow;
totalChkDuration = (end - chkStart).TotalMilliseconds;
_dumpLog.WriteLine("Sidecar created in {0} seconds.", (end - chkStart).TotalSeconds);
_dumpLog.WriteLine("Average checksum speed {0:F3} KiB/sec.",
((double)blockSize * (double)(blocks + 1)) / 1024 / (totalChkDuration / 1000));
if(_preSidecar != null)
{
_preSidecar.OpticalDisc = sidecar.OpticalDisc;
sidecar = _preSidecar;
}
List<(ulong start, string type)> filesystems = new List<(ulong start, string type)>();
if(sidecar.OpticalDisc[0].Track != null)
filesystems.AddRange(from xmlTrack in sidecar.OpticalDisc[0].Track
where xmlTrack.FileSystemInformation != null
from partition in xmlTrack.FileSystemInformation
where partition.FileSystems != null from fileSystem in partition.FileSystems
select (partition.StartSector, fileSystem.Type));
if(filesystems.Count > 0)
foreach(var filesystem in filesystems.Select(o => new
{
o.start, o.type
}).Distinct())
_dumpLog.WriteLine("Found filesystem {0} at sector {1}", filesystem.type, filesystem.start);
sidecar.OpticalDisc[0].Dimensions = Dimensions.DimensionsFromMediaType(mediaType);
(string type, string subType) discType = CommonTypes.Metadata.MediaType.MediaTypeToString(mediaType);
sidecar.OpticalDisc[0].DiscType = discType.type;
sidecar.OpticalDisc[0].DiscSubType = discType.subType;
sidecar.OpticalDisc[0].DumpHardwareArray = _resume.Tries.ToArray();
sidecar.OpticalDisc[0].Sessions = (uint)sessions;
sidecar.OpticalDisc[0].Layers = layers;
if(mediaTags != null)
foreach(KeyValuePair<MediaTagType, byte[]> tag in mediaTags.Where(tag => _outputPlugin.
SupportedMediaTags.
Contains(tag.Key)))
AddMediaTagToSidecar(_outputPath, tag, ref sidecar);
UpdateStatus?.Invoke("Writing metadata sidecar");
var xmlFs = new FileStream(_outputPrefix + ".cicm.xml", FileMode.Create);
var xmlSer = new XmlSerializer(typeof(CICMMetadataType));
xmlSer.Serialize(xmlFs, sidecar);
xmlFs.Close();
}
}
}

View File

@@ -653,81 +653,7 @@ namespace DiscImageChef.Core.Devices.Dumping
if(!_nometadata)
{
UpdateStatus?.Invoke("Creating sidecar.");
_dumpLog.WriteLine("Creating sidecar.");
var filters = new FiltersList();
IFilter filter = filters.GetFilter(_outputPath);
IMediaImage inputPlugin = ImageFormat.Detect(filter);
if(!inputPlugin.Open(filter))
{
StoppingErrorMessage?.Invoke("Could not open created image.");
return;
}
DateTime chkStart = DateTime.UtcNow;
_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;
CICMMetadataType sidecar = _sidecarClass.Create();
end = DateTime.UtcNow;
totalChkDuration = (end - chkStart).TotalMilliseconds;
UpdateStatus?.Invoke($"Sidecar created in {(end - chkStart).TotalSeconds} seconds.");
UpdateStatus?.
Invoke($"Average checksum speed {((double)BLOCK_SIZE * (double)(blocks + 1)) / 1024 / (totalChkDuration / 1000):F3} KiB/sec.");
_dumpLog.WriteLine("Sidecar created in {0} seconds.", (end - chkStart).TotalSeconds);
_dumpLog.WriteLine("Average checksum speed {0:F3} KiB/sec.",
((double)BLOCK_SIZE * (double)(blocks + 1)) / 1024 / (totalChkDuration / 1000));
if(_preSidecar != null)
{
_preSidecar.OpticalDisc = sidecar.OpticalDisc;
sidecar = _preSidecar;
}
List<(ulong start, string type)> filesystems = new List<(ulong start, string type)>();
if(sidecar.OpticalDisc[0].Track != null)
filesystems.AddRange(from xmlTrack in sidecar.OpticalDisc[0].Track
where xmlTrack.FileSystemInformation != null
from partition in xmlTrack.FileSystemInformation
where partition.FileSystems != null from fileSystem in partition.FileSystems
select (partition.StartSector, fileSystem.Type));
if(filesystems.Count > 0)
foreach(var filesystem in filesystems.Select(o => new
{
o.start, o.type
}).Distinct())
{
UpdateStatus?.Invoke($"Found filesystem {filesystem.type} at sector {filesystem.start}");
_dumpLog.WriteLine("Found filesystem {0} at sector {1}", filesystem.type, filesystem.start);
}
// TODO: Implement layers
sidecar.OpticalDisc[0].Dimensions = Dimensions.DimensionsFromMediaType(DSK_TYPE);
(string type, string subType) discType = CommonTypes.Metadata.MediaType.MediaTypeToString(DSK_TYPE);
sidecar.OpticalDisc[0].DiscType = discType.type;
sidecar.OpticalDisc[0].DiscSubType = discType.subType;
sidecar.OpticalDisc[0].DumpHardwareArray = _resume.Tries.ToArray();
UpdateStatus?.Invoke("Writing metadata sidecar");
var xmlFs = new FileStream(_outputPrefix + ".cicm.xml", FileMode.Create);
var xmlSer = new XmlSerializer(typeof(CICMMetadataType));
xmlSer.Serialize(xmlFs, sidecar);
xmlFs.Close();
WriteOpticalSidecar(BLOCK_SIZE, blocks, DSK_TYPE, null, null, 1, out totalChkDuration);
}
UpdateStatus?.Invoke("");

View File

@@ -987,6 +987,11 @@ namespace DiscImageChef.Core.Devices.Dumping
double totalChkDuration = 0;
if(!_nometadata)
{
// TODO: Layers
if(opticalDisc)
WriteOpticalSidecar(blockSize, blocks, dskType, null, mediaTags, 1, out totalChkDuration);
else
{
UpdateStatus?.Invoke("Creating sidecar.");
_dumpLog.WriteLine("Creating sidecar.");
@@ -1024,47 +1029,6 @@ namespace DiscImageChef.Core.Devices.Dumping
_dumpLog.WriteLine("Average checksum speed {0:F3} KiB/sec.",
((double)blockSize * (double)(blocks + 1)) / 1024 / (totalChkDuration / 1000));
if(opticalDisc)
{
if(_preSidecar != null)
{
_preSidecar.OpticalDisc = sidecar.OpticalDisc;
sidecar = _preSidecar;
}
List<(ulong start, string type)> filesystems = new List<(ulong start, string type)>();
if(sidecar.OpticalDisc[0].Track != null)
filesystems.AddRange(from xmlTrack in sidecar.OpticalDisc[0].Track
where xmlTrack.FileSystemInformation != null
from partition in xmlTrack.FileSystemInformation
where partition.FileSystems != null
from fileSystem in partition.FileSystems
select (partition.StartSector, fileSystem.Type));
if(filesystems.Count > 0)
foreach(var filesystem in filesystems.Select(o => new
{
o.start, o.type
}).Distinct())
{
UpdateStatus?.Invoke($"Found filesystem {filesystem.type} at sector {filesystem.start}");
_dumpLog.WriteLine("Found filesystem {0} at sector {1}", filesystem.type, filesystem.start);
}
// TODO: Implement layers
sidecar.OpticalDisc[0].Dimensions = Dimensions.DimensionsFromMediaType(dskType);
(string type, string subType) xmlType = CommonTypes.Metadata.MediaType.MediaTypeToString(dskType);
sidecar.OpticalDisc[0].DiscType = xmlType.type;
sidecar.OpticalDisc[0].DiscSubType = xmlType.subType;
sidecar.OpticalDisc[0].DumpHardwareArray = _resume.Tries.ToArray();
foreach(KeyValuePair<MediaTagType, byte[]> tag in mediaTags)
if(_outputPlugin.SupportedMediaTags.Contains(tag.Key))
AddMediaTagToSidecar(_outputPath, tag, ref sidecar);
}
else
{
if(_preSidecar != null)
{
_preSidecar.BlockMedia = sidecar.BlockMedia;
@@ -1247,7 +1211,6 @@ namespace DiscImageChef.Core.Devices.Dumping
if(_dev.IsRemovable)
sidecar.BlockMedia[0].DumpHardwareArray = _resume.Tries.ToArray();
}
UpdateStatus?.Invoke("Writing metadata sidecar");
@@ -1257,6 +1220,7 @@ namespace DiscImageChef.Core.Devices.Dumping
xmlSer.Serialize(xmlFs, sidecar);
xmlFs.Close();
}
}
UpdateStatus?.Invoke("");

View File

@@ -32,15 +32,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using DiscImageChef.CommonTypes;
using DiscImageChef.CommonTypes.Enums;
using DiscImageChef.CommonTypes.Extents;
using DiscImageChef.CommonTypes.Interfaces;
using DiscImageChef.CommonTypes.Interop;
using DiscImageChef.CommonTypes.Metadata;
using DiscImageChef.CommonTypes.Structs;
using DiscImageChef.Console;
using DiscImageChef.Core.Logging;
@@ -49,7 +46,6 @@ using DiscImageChef.Decoders.SCSI;
using DiscImageChef.Decoders.Xbox;
using DiscImageChef.Devices;
using Schemas;
using MediaType = DiscImageChef.CommonTypes.MediaType;
using PlatformID = DiscImageChef.CommonTypes.Interop.PlatformID;
using TrackType = DiscImageChef.CommonTypes.Enums.TrackType;
@@ -1173,99 +1169,17 @@ namespace DiscImageChef.Core.Devices.Dumping
if(!_nometadata)
{
UpdateStatus?.Invoke("Creating sidecar.");
_dumpLog.WriteLine("Creating sidecar.");
var filters = new FiltersList();
IFilter filter = filters.GetFilter(_outputPath);
IMediaImage inputPlugin = ImageFormat.Detect(filter);
if(!inputPlugin.Open(filter))
{
StoppingErrorMessage?.Invoke("Could not open created image.");
return;
}
DateTime chkStart = DateTime.UtcNow;
_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;
CICMMetadataType sidecar = _sidecarClass.Create();
end = DateTime.UtcNow;
if(_preSidecar != null)
{
_preSidecar.OpticalDisc = sidecar.OpticalDisc;
sidecar = _preSidecar;
}
totalChkDuration = (end - chkStart).TotalMilliseconds;
UpdateStatus?.Invoke($"Sidecar created in {(end - chkStart).TotalSeconds} seconds.");
UpdateStatus?.
Invoke($"Average checksum speed {((double)BLOCK_SIZE * (double)(blocks + 1)) / 1024 / (totalChkDuration / 1000):F3} KiB/sec.");
_dumpLog.WriteLine("Sidecar created in {0} seconds.", (end - chkStart).TotalSeconds);
_dumpLog.WriteLine("Average checksum speed {0:F3} KiB/sec.",
((double)BLOCK_SIZE * (double)(blocks + 1)) / 1024 / (totalChkDuration / 1000));
foreach(KeyValuePair<MediaTagType, byte[]> tag in mediaTags)
AddMediaTagToSidecar(_outputPath, tag, ref sidecar);
List<(ulong start, string type)> filesystems = new List<(ulong start, string type)>();
if(sidecar.OpticalDisc[0].Track != null)
filesystems.AddRange(from xmlTrack in sidecar.OpticalDisc[0].Track
where xmlTrack.FileSystemInformation != null
from partition in xmlTrack.FileSystemInformation
where partition.FileSystems != null from fileSystem in partition.FileSystems
select (partition.StartSector, fileSystem.Type));
if(filesystems.Count > 0)
foreach(var filesystem in filesystems.Select(o => new
{
o.start, o.type
}).Distinct())
{
UpdateStatus?.Invoke($"Found filesystem {filesystem.type} at sector {filesystem.start}");
_dumpLog.WriteLine("Found filesystem {0} at sector {1}", filesystem.type, filesystem.start);
}
sidecar.OpticalDisc[0].Layers = new LayersType
var layers = new LayersType
{
type = LayersTypeType.OTP, typeSpecified = true, Sectors = new SectorsType[1]
};
sidecar.OpticalDisc[0].Layers.Sectors[0] = new SectorsType
layers.Sectors[0] = new SectorsType
{
Value = layerBreak
};
sidecar.OpticalDisc[0].Sessions = 1;
sidecar.OpticalDisc[0].Dimensions = Dimensions.DimensionsFromMediaType(dskType);
(string type, string subType) xmlType = CommonTypes.Metadata.MediaType.MediaTypeToString(dskType);
sidecar.OpticalDisc[0].DiscType = xmlType.type;
sidecar.OpticalDisc[0].DiscSubType = xmlType.subType;
foreach(KeyValuePair<MediaTagType, byte[]> tag in mediaTags)
if(_outputPlugin.SupportedMediaTags.Contains(tag.Key))
AddMediaTagToSidecar(_outputPath, tag, ref sidecar);
UpdateStatus?.Invoke("Writing metadata sidecar");
var xmlFs = new FileStream(_outputPrefix + ".cicm.xml", FileMode.Create);
var xmlSer = new XmlSerializer(typeof(CICMMetadataType));
xmlSer.Serialize(xmlFs, sidecar);
xmlFs.Close();
WriteOpticalSidecar(BLOCK_SIZE, blocks, dskType, layers, mediaTags, 1, out totalChkDuration);
}
UpdateStatus?.Invoke("");

View File

@@ -52,6 +52,7 @@
<Compile Include="Devices\Dumping\CompactDisc\Subchannel.cs" />
<Compile Include="Devices\Dumping\CompactDisc\Tracks.cs" />
<Compile Include="Devices\Dumping\Dump.cs" />
<Compile Include="Devices\Dumping\Metadata.cs" />
<Compile Include="Devices\Dumping\PlayStationPortable.cs" />
<Compile Include="Devices\Info\DeviceInfo.cs" />
<Compile Include="Devices\Info\Plextor.cs" />