Files
Aaru/Aaru.Core/Sidecar/BlockTape.cs

222 lines
8.5 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : BlockTape.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Core algorithms.
//
// --[ Description ] ----------------------------------------------------------
//
// Contains logic to create sidecar from a block tape media dump.
//
// --[ 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-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
namespace Aaru.Core;
using System.Collections.Generic;
using System.IO;
2017-12-19 19:33:46 +00:00
using Schemas;
2022-03-06 13:29:38 +00:00
/// <summary>Sidecar operations</summary>
public sealed partial class Sidecar
{
2022-03-06 13:29:38 +00:00
/// <summary>Creates a metadata sidecar for a block tape (e.g. scsi streaming)</summary>
/// <param name="files">List of files</param>
/// <param name="folderName">Dump path</param>
/// <param name="blockSize">Expected block size in bytes</param>
public CICMMetadataType BlockTape(string folderName, List<string> files, uint blockSize)
{
2022-03-06 13:29:38 +00:00
_sidecar = new CICMMetadataType
{
2022-03-06 13:29:38 +00:00
BlockMedia = new[]
{
2022-03-06 13:29:38 +00:00
new BlockMediaType
2017-12-19 20:33:03 +00:00
{
2022-03-06 13:29:38 +00:00
Image = new ImageType
2017-12-19 20:33:03 +00:00
{
2022-03-06 13:29:38 +00:00
format = "Directory",
offsetSpecified = false,
Value = folderName
},
Sequence = new SequenceType
{
MediaTitle = folderName,
MediaSequence = 1,
TotalMedia = 1
},
PhysicalBlockSize = blockSize,
LogicalBlockSize = blockSize,
TapeInformation = new[]
{
new TapePartitionType
2017-12-19 20:33:03 +00:00
{
2022-03-06 13:29:38 +00:00
Image = new ImageType
2017-12-19 20:33:03 +00:00
{
2022-03-06 13:29:38 +00:00
format = "Directory",
offsetSpecified = false,
Value = folderName
2017-12-19 20:33:03 +00:00
}
}
}
}
2022-03-06 13:29:38 +00:00
}
};
2022-03-06 13:29:38 +00:00
if(_aborted)
return _sidecar;
2019-04-20 19:21:00 +01:00
2022-03-07 07:36:44 +00:00
ulong currentBlock = 0;
ulong totalSize = 0;
var tapeWorker = new Checksum();
var tapeFiles = new List<TapeFileType>();
2022-03-06 13:29:38 +00:00
UpdateStatus("Hashing files...");
2020-02-29 18:03:35 +00:00
2022-03-07 07:36:44 +00:00
for(var i = 0; i < files.Count; i++)
2022-03-06 13:29:38 +00:00
{
if(_aborted)
return _sidecar;
2019-04-20 19:21:00 +01:00
2022-03-06 13:29:38 +00:00
_fs = new FileStream(files[i], FileMode.Open, FileAccess.Read);
var fileWorker = new Checksum();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
var tapeFile = new TapeFileType
{
Image = new ImageType
{
2022-03-06 13:29:38 +00:00
format = "Raw disk image (sector by sector copy)",
offset = 0,
Value = Path.GetFileName(files[i])
},
Size = (ulong)_fs.Length,
BlockSize = blockSize,
StartBlock = currentBlock,
Sequence = (ulong)i
};
2022-03-06 13:29:38 +00:00
const uint sectorsToRead = 512;
ulong sectors = (ulong)_fs.Length / blockSize;
ulong doneSectors = 0;
2022-03-06 13:29:38 +00:00
InitProgress2();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
while(doneSectors < sectors)
{
if(_aborted)
{
2022-03-06 13:29:38 +00:00
EndProgress2();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return _sidecar;
}
2019-04-20 19:21:00 +01:00
2022-03-06 13:29:38 +00:00
byte[] sector;
2022-03-06 13:29:38 +00:00
if(sectors - doneSectors >= sectorsToRead)
{
sector = new byte[sectorsToRead * blockSize];
_fs.Read(sector, 0, sector.Length);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
UpdateProgress2($"Hashing block {doneSectors} of {sectors} on file {i + 1} of {files.Count}",
(long)doneSectors, (long)sectors);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
doneSectors += sectorsToRead;
}
else
{
sector = new byte[(uint)(sectors - doneSectors) * blockSize];
_fs.Read(sector, 0, sector.Length);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
UpdateProgress2($"Hashing block {doneSectors} of {sectors} on file {i + 1} of {files.Count}",
(long)doneSectors, (long)sectors);
2022-03-06 13:29:38 +00:00
doneSectors += sectors - doneSectors;
}
2022-03-06 13:29:38 +00:00
fileWorker.Update(sector);
tapeWorker.Update(sector);
}
2022-03-06 13:29:38 +00:00
tapeFile.EndBlock = tapeFile.StartBlock + sectors - 1;
currentBlock += sectors;
totalSize += (ulong)_fs.Length;
tapeFile.Checksums = fileWorker.End().ToArray();
tapeFiles.Add(tapeFile);
EndProgress2();
}
UpdateStatus("Setting metadata...");
_sidecar.BlockMedia[0].Checksums = tapeWorker.End().ToArray();
_sidecar.BlockMedia[0].ContentChecksums = _sidecar.BlockMedia[0].Checksums;
_sidecar.BlockMedia[0].Size = totalSize;
_sidecar.BlockMedia[0].LogicalBlocks = currentBlock;
_sidecar.BlockMedia[0].TapeInformation[0].EndBlock = currentBlock - 1;
_sidecar.BlockMedia[0].TapeInformation[0].Size = totalSize;
_sidecar.BlockMedia[0].TapeInformation[0].Checksums = _sidecar.BlockMedia[0].Checksums;
_sidecar.BlockMedia[0].TapeInformation[0].File = tapeFiles.ToArray();
// This is purely for convenience, as typically these kind of data represents QIC tapes
if(blockSize == 512)
{
_sidecar.BlockMedia[0].DiskType = "Quarter-inch cartridge";
if(totalSize <= 20 * 1048576)
_sidecar.BlockMedia[0].DiskSubType = "QIC-11";
else if(totalSize <= 40 * 1048576)
_sidecar.BlockMedia[0].DiskSubType = "QIC-40";
else if(totalSize <= 60 * 1048576)
_sidecar.BlockMedia[0].DiskSubType = "QIC-24";
else if(totalSize <= 80 * 1048576)
_sidecar.BlockMedia[0].DiskSubType = "QIC-80";
else if(totalSize <= 120 * 1048576)
_sidecar.BlockMedia[0].DiskSubType = "QIC-120";
else if(totalSize <= 150 * 1048576)
_sidecar.BlockMedia[0].DiskSubType = "QIC-150";
else if(totalSize <= 320 * 1048576)
_sidecar.BlockMedia[0].DiskSubType = "QIC-320";
else if(totalSize <= 340 * 1048576)
_sidecar.BlockMedia[0].DiskSubType = "QIC-3010";
else if(totalSize <= 525 * 1048576)
_sidecar.BlockMedia[0].DiskSubType = "QIC-525";
else if(totalSize <= 670 * 1048576)
_sidecar.BlockMedia[0].DiskSubType = "QIC-3020";
else if(totalSize <= 1200 * 1048576)
_sidecar.BlockMedia[0].DiskSubType = "QIC-3080";
else if(totalSize <= 1350 * 1048576)
_sidecar.BlockMedia[0].DiskSubType = "QIC-1350";
else if(totalSize <= (long)4000 * 1048576)
_sidecar.BlockMedia[0].DiskSubType = "QIC-3095";
else
{
2020-07-20 21:11:32 +01:00
_sidecar.BlockMedia[0].DiskType = "Unknown tape";
_sidecar.BlockMedia[0].DiskSubType = "Unknown tape";
}
}
2022-03-06 13:29:38 +00:00
else
{
_sidecar.BlockMedia[0].DiskType = "Unknown tape";
_sidecar.BlockMedia[0].DiskSubType = "Unknown tape";
}
return _sidecar;
}
2017-12-19 20:33:03 +00:00
}