2019-05-06 22:42:13 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2019-05-06 22:42:13 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : Write.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : Disk image plugins.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Writes CopyTape tape images.
|
|
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// This library is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
// published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This library 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
|
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2024-05-01 04:17:32 +01:00
|
|
|
// Copyright © 2011-2024 Natalia Portillo
|
2019-05-06 22:42:13 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.CommonTypes;
|
2022-12-15 22:21:07 +00:00
|
|
|
using Aaru.CommonTypes.AaruMetadata;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.CommonTypes.Enums;
|
|
|
|
|
using Aaru.CommonTypes.Structs;
|
2023-10-08 04:10:19 +01:00
|
|
|
using Aaru.Console;
|
2022-12-15 22:21:07 +00:00
|
|
|
using TapeFile = Aaru.CommonTypes.Structs.TapeFile;
|
|
|
|
|
using TapePartition = Aaru.CommonTypes.Structs.TapePartition;
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2023-10-06 01:16:28 +01:00
|
|
|
namespace Aaru.Images;
|
2022-11-15 15:58:43 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
public sealed partial class CopyTape
|
2019-05-06 22:42:13 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
FileStream _dataStream;
|
|
|
|
|
ulong _lastWrittenBlock;
|
|
|
|
|
Dictionary<ulong, ulong> _writtenBlockPositions;
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2023-10-03 23:34:59 +01:00
|
|
|
#region IWritableTapeImage Members
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool Create(string path, MediaType mediaType, Dictionary<string, string> options, ulong sectors,
|
2023-10-03 23:34:59 +01:00
|
|
|
uint sectorSize)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
|
|
|
|
if(!SupportedMediaTypes.Contains(mediaType))
|
2019-05-06 22:42:13 +01:00
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
ErrorMessage = string.Format(Localization.Unsupported_media_format_0, mediaType);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
_imageInfo = new ImageInfo
|
|
|
|
|
{
|
|
|
|
|
MediaType = mediaType,
|
|
|
|
|
SectorSize = sectorSize,
|
|
|
|
|
Sectors = sectors
|
|
|
|
|
};
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_dataStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
|
2019-05-06 22:42:13 +01:00
|
|
|
}
|
2023-10-08 04:10:19 +01:00
|
|
|
catch(IOException ex)
|
2019-05-06 22:42:13 +01:00
|
|
|
{
|
2023-10-08 04:10:19 +01:00
|
|
|
ErrorMessage = string.Format(Localization.Could_not_create_new_image_file_exception_0, ex.Message);
|
|
|
|
|
AaruConsole.WriteException(ex);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2019-05-06 22:42:13 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
_imageInfo.MediaType = mediaType;
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
IsWriting = true;
|
|
|
|
|
IsTape = false;
|
|
|
|
|
ErrorMessage = null;
|
|
|
|
|
_lastWrittenBlock = 0;
|
|
|
|
|
_writtenBlockPositions = new Dictionary<ulong, ulong>();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool WriteMediaTag(byte[] data, MediaTagType tag)
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
ErrorMessage = Localization.Unsupported_feature;
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool WriteSector(byte[] data, ulong sectorAddress)
|
|
|
|
|
{
|
|
|
|
|
if(!_writtenBlockPositions.TryGetValue(sectorAddress, out ulong position))
|
2019-05-06 22:42:13 +01:00
|
|
|
{
|
2023-10-04 17:34:40 +01:00
|
|
|
if(_dataStream.Length != 0 && _lastWrittenBlock >= sectorAddress)
|
2019-05-06 22:42:13 +01:00
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
ErrorMessage = Localization.Cannot_write_unwritten_blocks;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return false;
|
2019-05-06 22:42:13 +01:00
|
|
|
}
|
|
|
|
|
|
2023-10-04 17:34:40 +01:00
|
|
|
if(_lastWrittenBlock + 1 != sectorAddress && sectorAddress != 0 && _lastWrittenBlock != 0)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
ErrorMessage = Localization.Cannot_skip_blocks;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-05-06 22:42:13 +01:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
else
|
|
|
|
|
_dataStream.Position = (long)position;
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
byte[] header = Encoding.ASCII.GetBytes($"CPTP:BLK {data.Length:D6}\n");
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
_writtenBlockPositions[sectorAddress] = (ulong)_dataStream.Position;
|
|
|
|
|
_dataStream.Write(header, 0, header.Length);
|
2023-10-03 23:34:59 +01:00
|
|
|
_dataStream.Write(data, 0, data.Length);
|
2022-03-06 13:29:38 +00:00
|
|
|
_dataStream.WriteByte(0x0A);
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(sectorAddress > _lastWrittenBlock) _lastWrittenBlock = sectorAddress;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
_dataStream.Seek(0, SeekOrigin.End);
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool WriteSectors(byte[] data, ulong sectorAddress, uint length)
|
|
|
|
|
{
|
|
|
|
|
for(uint i = 0; i < length; i++)
|
2019-05-06 22:42:13 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
bool ret = WriteSector(data, sectorAddress + i);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!ret) return false;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool WriteSectorLong(byte[] data, ulong sectorAddress)
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
ErrorMessage = Localization.Unsupported_feature;
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool WriteSectorsLong(byte[] data, ulong sectorAddress, uint length)
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
ErrorMessage = Localization.Unsupported_feature;
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool Close()
|
|
|
|
|
{
|
|
|
|
|
if(!IsWriting)
|
2019-05-06 22:42:13 +01:00
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
ErrorMessage = Localization.Image_is_not_opened_for_writing;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2019-05-06 22:42:13 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-29 18:27:27 +01:00
|
|
|
byte[] footer = "CPTP:EOT\n"u8.ToArray();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
_dataStream.Write(footer, 0, footer.Length);
|
|
|
|
|
_dataStream.Flush();
|
|
|
|
|
_dataStream.Close();
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
IsWriting = false;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
2022-12-15 22:21:07 +00:00
|
|
|
public bool SetImageInfo(ImageInfo imageInfo) => true;
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool SetGeometry(uint cylinders, uint heads, uint sectorsPerTrack)
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
ErrorMessage = Localization.Unsupported_feature;
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool WriteSectorTag(byte[] data, ulong sectorAddress, SectorTagType tag)
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
ErrorMessage = Localization.Unsupported_feature;
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool WriteSectorsTag(byte[] data, ulong sectorAddress, uint length, SectorTagType tag)
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
ErrorMessage = Localization.Unsupported_feature;
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
2022-12-15 22:21:07 +00:00
|
|
|
public bool SetDumpHardware(List<DumpHardware> dumpHardware) => false;
|
2019-05-06 22:42:13 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
2022-12-15 22:21:07 +00:00
|
|
|
public bool SetMetadata(Metadata metadata) => false;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool AddFile(TapeFile file)
|
|
|
|
|
{
|
|
|
|
|
if(file.Partition != 0)
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
ErrorMessage = Localization.Unsupported_feature;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2019-05-06 22:42:13 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-29 18:27:27 +01:00
|
|
|
byte[] marker = "CPTP:MRK\n"u8.ToArray();
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
_dataStream.Write(marker, 0, marker.Length);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool AddPartition(TapePartition partition)
|
|
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(partition.Number == 0) return true;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-11-29 02:10:37 +00:00
|
|
|
ErrorMessage = Localization.Unsupported_feature;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool SetTape()
|
|
|
|
|
{
|
|
|
|
|
IsTape = true;
|
|
|
|
|
|
|
|
|
|
return true;
|
2019-05-06 22:42:13 +01:00
|
|
|
}
|
2023-10-03 23:34:59 +01:00
|
|
|
|
|
|
|
|
#endregion
|
2019-05-06 22:42:13 +01:00
|
|
|
}
|