Implement CopyTape writing.

This commit is contained in:
2019-05-06 22:42:13 +01:00
parent c963a56af3
commit 73d45970c5
6 changed files with 256 additions and 3 deletions

View File

@@ -787,6 +787,7 @@
<e p="Properties.cs" t="Include" />
<e p="Read.cs" t="Include" />
<e p="Unsupported.cs" t="Include" />
<e p="Write.cs" t="Include" />
</e>
<e p="D88" t="Include">
<e p="Constants.cs" t="Include" />

View File

@@ -6,7 +6,7 @@ using DiscImageChef.CommonTypes.Structs;
namespace DiscImageChef.DiscImages.CopyTape
{
public partial class CopyTape : ITapeImage
public partial class CopyTape : IWritableTapeImage
{
long[] blockPositionCache;
ImageInfo imageInfo;

View File

@@ -32,6 +32,8 @@
using System;
using System.Collections.Generic;
using DiscImageChef.CommonTypes;
using DiscImageChef.CommonTypes.Enums;
using DiscImageChef.CommonTypes.Structs;
using Schemas;
@@ -47,7 +49,47 @@ namespace DiscImageChef.DiscImages.CopyTape
public List<DumpHardwareType> DumpHardware => null;
public CICMMetadataType CicmMetadata => null;
public List<TapeFile> Files { get; private set; }
public List<TapePartition> TapePartitions { get; set; }
public bool IsTape => true;
public List<TapePartition> TapePartitions { get; private set; }
public bool IsTape { get; set; }
public IEnumerable<MediaTagType> SupportedMediaTags => new MediaTagType[] { };
public IEnumerable<SectorTagType> SupportedSectorTags => new SectorTagType[] { };
public IEnumerable<MediaType> SupportedMediaTypes =>
new[]
{
MediaType.UnknownTape, MediaType.ADR2120, MediaType.ADR260, MediaType.ADR30, MediaType.ADR50,
MediaType.AIT1, MediaType.AIT1Turbo, MediaType.AIT2, MediaType.AIT2Turbo, MediaType.AIT3,
MediaType.AIT3Ex, MediaType.AIT3Turbo, MediaType.AIT4, MediaType.AIT5, MediaType.AITETurbo,
MediaType.SAIT1, MediaType.SAIT2, MediaType.Ditto, MediaType.DittoMax, MediaType.DigitalAudioTape,
MediaType.DAT160, MediaType.DAT320, MediaType.DAT72, MediaType.DDS1, MediaType.DDS2, MediaType.DDS3,
MediaType.DDS4, MediaType.CompactTapeI, MediaType.CompactTapeII, MediaType.DECtapeII,
MediaType.DLTtapeIII, MediaType.DLTtapeIIIxt, MediaType.DLTtapeIV, MediaType.DLTtapeS4,
MediaType.SDLT1, MediaType.SDLT2, MediaType.VStapeI, MediaType.Exatape15m, MediaType.Exatape22m,
MediaType.Exatape22mAME, MediaType.Exatape28m, MediaType.Exatape40m, MediaType.Exatape45m,
MediaType.Exatape54m, MediaType.Exatape75m, MediaType.Exatape76m, MediaType.Exatape80m,
MediaType.Exatape106m, MediaType.Exatape160mXL, MediaType.Exatape112m, MediaType.Exatape125m,
MediaType.Exatape150m, MediaType.Exatape170m, MediaType.Exatape225m, MediaType.IBM3470,
MediaType.IBM3480, MediaType.IBM3490, MediaType.IBM3490E, MediaType.IBM3592, MediaType.LTO,
MediaType.LTO2, MediaType.LTO3, MediaType.LTO3WORM, MediaType.LTO4, MediaType.LTO4WORM,
MediaType.LTO5, MediaType.LTO5WORM, MediaType.LTO6, MediaType.LTO6WORM, MediaType.LTO7,
MediaType.LTO7WORM, MediaType.MLR1, MediaType.MLR1SL, MediaType.MLR3, MediaType.SLR1,
MediaType.SLR2, MediaType.SLR3, MediaType.SLR32, MediaType.SLR32SL, MediaType.SLR4, MediaType.SLR5,
MediaType.SLR5SL, MediaType.SLR6, MediaType.SLRtape7, MediaType.SLRtape7SL, MediaType.SLRtape24,
MediaType.SLRtape24SL, MediaType.SLRtape40, MediaType.SLRtape50, MediaType.SLRtape60,
MediaType.SLRtape75, MediaType.SLRtape100, MediaType.SLRtape140, MediaType.QIC11, MediaType.QIC120,
MediaType.QIC1350, MediaType.QIC150, MediaType.QIC24, MediaType.QIC3010, MediaType.QIC3020,
MediaType.QIC3080, MediaType.QIC3095, MediaType.QIC320, MediaType.QIC40, MediaType.QIC525,
MediaType.QIC80, MediaType.STK4480, MediaType.STK4490, MediaType.STK9490, MediaType.T9840A,
MediaType.T9840B, MediaType.T9840C, MediaType.T9840D, MediaType.T9940A, MediaType.T9940B,
MediaType.T10000A, MediaType.T10000B, MediaType.T10000C, MediaType.T10000D, MediaType.Travan,
MediaType.Travan1Ex, MediaType.Travan3, MediaType.Travan3Ex, MediaType.Travan4, MediaType.Travan5,
MediaType.Travan7, MediaType.VXA1, MediaType.VXA2, MediaType.VXA3
};
public IEnumerable<(string name, Type type, string description, object @default)> SupportedOptions =>
new (string name, Type type, string description, object @default)[] { };
public IEnumerable<string> KnownExtensions => new[] {".cptp"};
public bool IsWriting { get; private set; }
public string ErrorMessage { get; private set; }
}
}

View File

@@ -143,6 +143,7 @@ namespace DiscImageChef.DiscImages.CopyTape
imageInfo.CreationTime = imageFilter.GetCreationTime();
imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
IsTape = true;
return true;
}

View File

@@ -0,0 +1,208 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2019 Natalia Portillo
// ****************************************************************************/
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using DiscImageChef.CommonTypes;
using DiscImageChef.CommonTypes.Enums;
using DiscImageChef.CommonTypes.Structs;
using Schemas;
namespace DiscImageChef.DiscImages.CopyTape
{
public partial class CopyTape
{
FileStream dataStream;
ulong lastWrittenBlock;
Dictionary<ulong, ulong> writtenBlockPositions;
public bool Create(string path, MediaType mediaType, Dictionary<string, string> options, ulong sectors,
uint sectorSize)
{
if(!SupportedMediaTypes.Contains(mediaType))
{
ErrorMessage = $"Unsupport media format {mediaType}";
return false;
}
imageInfo = new ImageInfo {MediaType = mediaType, SectorSize = sectorSize, Sectors = sectors};
try { dataStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); }
catch(IOException e)
{
ErrorMessage = $"Could not create new image file, exception {e.Message}";
return false;
}
imageInfo.MediaType = mediaType;
IsWriting = true;
IsTape = false;
ErrorMessage = null;
lastWrittenBlock = 0;
writtenBlockPositions = new Dictionary<ulong, ulong>();
return true;
}
public bool WriteMediaTag(byte[] data, MediaTagType tag)
{
ErrorMessage = "Unsupported feature";
return false;
}
public bool WriteSector(byte[] data, ulong sectorAddress)
{
if(!writtenBlockPositions.TryGetValue(sectorAddress, out ulong position))
{
if(dataStream.Length != 0 && lastWrittenBlock >= sectorAddress)
{
ErrorMessage = "Cannot write unwritten blocks";
return false;
}
if(lastWrittenBlock + 1 != sectorAddress && sectorAddress != 0 && lastWrittenBlock != 0)
{
ErrorMessage = "Cannot skip blocks";
return false;
}
}
else dataStream.Position = (long)position;
byte[] header = Encoding.ASCII.GetBytes($"CPTP:BLK {data.Length:06D}\n");
writtenBlockPositions[sectorAddress] = (ulong)dataStream.Position;
dataStream.Write(header, 0, header.Length);
dataStream.Write(data, 0, data.Length);
dataStream.WriteByte(0x0A);
if(sectorAddress > lastWrittenBlock) lastWrittenBlock = sectorAddress;
dataStream.Seek(0, SeekOrigin.End);
return true;
}
public bool WriteSectors(byte[] data, ulong sectorAddress, uint length)
{
for(uint i = 0; i < length; i++)
{
bool ret = WriteSector(data, sectorAddress + i);
if(!ret) return false;
}
return true;
}
public bool WriteSectorLong(byte[] data, ulong sectorAddress)
{
ErrorMessage = "Unsupported feature";
return false;
}
public bool WriteSectorsLong(byte[] data, ulong sectorAddress, uint length)
{
ErrorMessage = "Unsupported feature";
return false;
}
public bool Close()
{
if(!IsWriting)
{
ErrorMessage = "Image is not opened for writing";
return false;
}
byte[] footer = Encoding.ASCII.GetBytes("CPTP:EOT\n");
dataStream.Write(footer, 0, footer.Length);
dataStream.Flush();
dataStream.Close();
IsWriting = false;
return true;
}
public bool SetMetadata(ImageInfo metadata) => true;
public bool SetGeometry(uint cylinders, uint heads, uint sectorsPerTrack)
{
ErrorMessage = "Unsupported feature";
return false;
}
public bool WriteSectorTag(byte[] data, ulong sectorAddress, SectorTagType tag)
{
ErrorMessage = "Unsupported feature";
return false;
}
public bool WriteSectorsTag(byte[] data, ulong sectorAddress, uint length, SectorTagType tag)
{
ErrorMessage = "Unsupported feature";
return false;
}
public bool SetDumpHardware(List<DumpHardwareType> dumpHardware) => false;
public bool SetCicmMetadata(CICMMetadataType metadata) => false;
public bool AddFile(TapeFile file)
{
if(file.Partition != 0)
{
ErrorMessage = "Unsupported feature";
return false;
}
byte[] marker = Encoding.ASCII.GetBytes("CPTP:MRK\n");
dataStream.Write(marker, 0, marker.Length);
return true;
}
public bool AddPartition(TapePartition partition)
{
if(partition.Number == 0) return true;
ErrorMessage = "Unsupported feature";
return false;
}
public bool SetTape()
{
IsTape = true;
return true;
}
}
}

View File

@@ -177,6 +177,7 @@
<Compile Include="CopyTape\Properties.cs" />
<Compile Include="CopyTape\Read.cs" />
<Compile Include="CopyTape\Unsupported.cs" />
<Compile Include="CopyTape\Write.cs" />
<Compile Include="CPCDSK\Constants.cs" />
<Compile Include="CPCDSK\Helpers.cs" />
<Compile Include="CPCDSK\Identify.cs" />