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

250 lines
8.8 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/>.
//
// ----------------------------------------------------------------------------
2024-12-19 10:45:18 +00:00
// Copyright © 2011-2025 Natalia Portillo
// ****************************************************************************/
using System.Collections.Generic;
using System.IO;
using Aaru.CommonTypes.AaruMetadata;
using Aaru.Helpers;
namespace Aaru.Core;
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 Metadata BlockTape(string folderName, List<string> files, uint blockSize)
{
_sidecar = new Metadata
{
2024-05-01 04:39:38 +01:00
BlockMedias =
[
new BlockMedia
2017-12-19 20:33:03 +00:00
{
Image = new Image
2017-12-19 20:33:03 +00:00
{
Format = "Directory",
Value = folderName
2022-03-06 13:29:38 +00:00
},
Sequence = new Sequence
2022-03-06 13:29:38 +00:00
{
Title = folderName,
2022-03-06 13:29:38 +00:00
MediaSequence = 1,
TotalMedia = 1
},
PhysicalBlockSize = blockSize,
LogicalBlockSize = blockSize,
2024-05-01 04:39:38 +01:00
TapeInformation =
[
new TapePartition
2017-12-19 20:33:03 +00:00
{
Image = new Image
2017-12-19 20:33:03 +00:00
{
Format = "Directory",
Value = folderName
2017-12-19 20:33:03 +00:00
}
}
2024-05-01 04:39:38 +01:00
]
}
2024-05-01 04:39:38 +01:00
]
2022-03-06 13:29:38 +00:00
};
2024-05-01 04:05:22 +01:00
if(_aborted) return _sidecar;
2019-04-20 19:21:00 +01:00
ulong currentBlock = 0;
ulong totalSize = 0;
var tapeWorker = new Checksum();
2024-05-01 04:39:38 +01:00
List<TapeFile> tapeFiles = [];
UpdateStatus(Localization.Core.Hashing_files);
2020-02-29 18:03:35 +00:00
2023-10-03 22:57:50 +01:00
for(var i = 0; i < files.Count; i++)
2022-03-06 13:29:38 +00:00
{
2024-05-01 04:05:22 +01: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
var tapeFile = new TapeFile
2022-03-06 13:29:38 +00:00
{
Image = new Image
{
Format = "Raw disk image (sector by sector copy)",
Offset = 0,
2022-03-06 13:29:38 +00:00
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.EnsureRead(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}",
2024-05-01 04:05:22 +01:00
(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.EnsureRead(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}",
2024-05-01 04:05:22 +01:00
(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();
2022-03-06 13:29:38 +00:00
tapeFiles.Add(tapeFile);
EndProgress2();
}
UpdateStatus("Setting metadata...");
_sidecar.BlockMedias[0].Checksums = tapeWorker.End();
_sidecar.BlockMedias[0].ContentChecksums = _sidecar.BlockMedias[0].Checksums;
_sidecar.BlockMedias[0].Size = totalSize;
_sidecar.BlockMedias[0].LogicalBlocks = currentBlock;
_sidecar.BlockMedias[0].TapeInformation[0].EndBlock = currentBlock - 1;
_sidecar.BlockMedias[0].TapeInformation[0].Size = totalSize;
_sidecar.BlockMedias[0].TapeInformation[0].Checksums = _sidecar.BlockMedias[0].Checksums;
_sidecar.BlockMedias[0].TapeInformation[0].Files = tapeFiles;
2022-03-06 13:29:38 +00:00
// This is purely for convenience, as typically these kind of data represents QIC tapes
if(blockSize == 512)
{
_sidecar.BlockMedias[0].MediaType = "Quarter-inch cartridge";
2022-03-06 13:29:38 +00:00
2022-11-13 19:38:03 +00:00
switch(totalSize)
{
2022-11-13 19:38:03 +00:00
case <= 20 * 1048576:
_sidecar.BlockMedias[0].MediaSubType = "QIC-11";
2022-11-13 19:38:03 +00:00
break;
case <= 40 * 1048576:
_sidecar.BlockMedias[0].MediaSubType = "QIC-40";
2022-11-13 19:38:03 +00:00
break;
case <= 60 * 1048576:
_sidecar.BlockMedias[0].MediaSubType = "QIC-24";
2022-11-13 19:38:03 +00:00
break;
case <= 80 * 1048576:
_sidecar.BlockMedias[0].MediaSubType = "QIC-80";
2022-11-13 19:38:03 +00:00
break;
case <= 120 * 1048576:
_sidecar.BlockMedias[0].MediaSubType = "QIC-120";
2022-11-13 19:38:03 +00:00
break;
case <= 150 * 1048576:
_sidecar.BlockMedias[0].MediaSubType = "QIC-150";
2022-11-13 19:38:03 +00:00
break;
case <= 320 * 1048576:
_sidecar.BlockMedias[0].MediaSubType = "QIC-320";
2022-11-13 19:38:03 +00:00
break;
case <= 340 * 1048576:
_sidecar.BlockMedias[0].MediaSubType = "QIC-3010";
2022-11-13 19:38:03 +00:00
break;
case <= 525 * 1048576:
_sidecar.BlockMedias[0].MediaSubType = "QIC-525";
2022-11-13 19:38:03 +00:00
break;
case <= 670 * 1048576:
_sidecar.BlockMedias[0].MediaSubType = "QIC-3020";
2022-11-13 19:38:03 +00:00
break;
case <= 1200 * 1048576:
_sidecar.BlockMedias[0].MediaSubType = "QIC-3080";
2022-11-13 19:38:03 +00:00
break;
case <= 1350 * 1048576:
_sidecar.BlockMedias[0].MediaSubType = "QIC-1350";
2022-11-13 19:38:03 +00:00
break;
case <= (long)4000 * 1048576:
_sidecar.BlockMedias[0].MediaSubType = "QIC-3095";
2022-11-13 19:38:03 +00:00
break;
default:
_sidecar.BlockMedias[0].MediaType = "Unknown tape";
_sidecar.BlockMedias[0].MediaSubType = "Unknown tape";
2022-11-13 19:38:03 +00:00
break;
}
}
2022-03-06 13:29:38 +00:00
else
{
_sidecar.BlockMedias[0].MediaType = "Unknown tape";
_sidecar.BlockMedias[0].MediaSubType = "Unknown tape";
2022-03-06 13:29:38 +00:00
}
return _sidecar;
}
2017-12-19 20:33:03 +00:00
}