Add writing support to T98 Hard Disk Image.

This commit is contained in:
2017-12-30 16:55:40 +00:00
parent 845f42f20d
commit 55291344a6

View File

@@ -33,46 +33,48 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using DiscImageChef.CommonTypes;
using DiscImageChef.Console;
using DiscImageChef.Filters;
namespace DiscImageChef.DiscImages
{
public class T98 : IMediaImage
public class T98 : IWritableImage
{
ImageInfo imageInfo;
IFilter t98ImageFilter;
ImageInfo imageInfo;
IFilter t98ImageFilter;
FileStream writingStream;
public T98()
{
imageInfo = new ImageInfo
{
ReadableSectorTags = new List<SectorTagType>(),
ReadableMediaTags = new List<MediaTagType>(),
HasPartitions = false,
HasSessions = false,
Version = null,
Application = null,
ApplicationVersion = null,
Creator = null,
Comments = null,
MediaManufacturer = null,
MediaModel = null,
MediaSerialNumber = null,
MediaBarcode = null,
MediaPartNumber = null,
MediaSequence = 0,
LastMediaSequence = 0,
DriveManufacturer = null,
DriveModel = null,
DriveSerialNumber = null,
ReadableSectorTags = new List<SectorTagType>(),
ReadableMediaTags = new List<MediaTagType>(),
HasPartitions = false,
HasSessions = false,
Version = null,
Application = null,
ApplicationVersion = null,
Creator = null,
Comments = null,
MediaManufacturer = null,
MediaModel = null,
MediaSerialNumber = null,
MediaBarcode = null,
MediaPartNumber = null,
MediaSequence = 0,
LastMediaSequence = 0,
DriveManufacturer = null,
DriveModel = null,
DriveSerialNumber = null,
DriveFirmwareRevision = null
};
}
public string Name => "T98 Hard Disk Image";
public Guid Id => new Guid("0410003E-6E7B-40E6-9328-BA5651ADF6B7");
public string Name => "T98 Hard Disk Image";
public Guid Id => new Guid("0410003E-6E7B-40E6-9328-BA5651ADF6B7");
public ImageInfo Info => imageInfo;
public string Format => "T98 disk image";
@@ -96,7 +98,9 @@ namespace DiscImageChef.DiscImages
byte[] hdrB = new byte[256];
stream.Read(hdrB, 0, hdrB.Length);
for(int i = 4; i < 256; i++) if(hdrB[i] != 0) return false;
for(int i = 4; i < 256; i++)
if(hdrB[i] != 0)
return false;
int cylinders = BitConverter.ToInt32(hdrB, 0);
@@ -117,22 +121,24 @@ namespace DiscImageChef.DiscImages
byte[] hdrB = new byte[256];
stream.Read(hdrB, 0, hdrB.Length);
for(int i = 4; i < 256; i++) if(hdrB[i] != 0) return false;
for(int i = 4; i < 256; i++)
if(hdrB[i] != 0)
return false;
int cylinders = BitConverter.ToInt32(hdrB, 0);
imageInfo.MediaType = MediaType.GENERIC_HDD;
imageInfo.ImageSize = (ulong)(stream.Length - 256);
imageInfo.CreationTime = imageFilter.GetCreationTime();
imageInfo.ImageSize = (ulong)(stream.Length - 256);
imageInfo.CreationTime = imageFilter.GetCreationTime();
imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
imageInfo.Sectors = (ulong)(stream.Length / 256 - 1);
imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
imageInfo.SectorSize = 256;
imageInfo.Cylinders = (uint)cylinders;
imageInfo.Heads = 8;
imageInfo.SectorsPerTrack = 33;
imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
imageInfo.Sectors = (ulong)(stream.Length / 256 - 1);
imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
imageInfo.SectorSize = 256;
imageInfo.Cylinders = (uint)cylinders;
imageInfo.Heads = 8;
imageInfo.SectorsPerTrack = 33;
t98ImageFilter = imageFilter;
@@ -239,7 +245,7 @@ namespace DiscImageChef.DiscImages
}
public bool? VerifySectors(ulong sectorAddress, uint length, out List<ulong> failingLbas,
out List<ulong> unknownLbas)
out List<ulong> unknownLbas)
{
failingLbas = new List<ulong>();
unknownLbas = new List<ulong>();
@@ -249,7 +255,7 @@ namespace DiscImageChef.DiscImages
}
public bool? VerifySectors(ulong sectorAddress, uint length, uint track, out List<ulong> failingLbas,
out List<ulong> unknownLbas)
out List<ulong> unknownLbas)
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
@@ -258,5 +264,146 @@ namespace DiscImageChef.DiscImages
{
return null;
}
public IEnumerable<MediaTagType> SupportedMediaTags => new MediaTagType[] { };
public IEnumerable<SectorTagType> SupportedSectorTags => new SectorTagType[] { };
public IEnumerable<MediaType> SupportedMediaTypes =>
new[] {MediaType.GENERIC_HDD, MediaType.Unknown};
public IEnumerable<(string name, Type type, string description)> SupportedOptions =>
new (string name, Type type, string description)[] { };
public IEnumerable<string> KnownExtensions => new[] {".t98"};
public bool IsWriting { get; private set; }
public string ErrorMessage { get; private set; }
public bool Create(string path, MediaType mediaType, Dictionary<string, string> options, ulong sectors,
uint sectorSize)
{
if(sectorSize != 256)
{
ErrorMessage = "Unsupported sector size";
return false;
}
if(!SupportedMediaTypes.Contains(mediaType))
{
ErrorMessage = $"Unsupport media format {mediaType}";
return false;
}
imageInfo = new ImageInfo {MediaType = mediaType, SectorSize = sectorSize, Sectors = sectors};
try { writingStream = new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None); }
catch(IOException e)
{
ErrorMessage = $"Could not create new image file, exception {e.Message}";
return false;
}
IsWriting = true;
ErrorMessage = null;
return true;
}
public bool WriteMediaTag(byte[] data, MediaTagType tag)
{
ErrorMessage = "Writing media tags is not supported.";
return false;
}
public bool WriteSector(byte[] data, ulong sectorAddress)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
return false;
}
if(data.Length != imageInfo.SectorSize)
{
ErrorMessage = "Incorrect data size";
return false;
}
if(sectorAddress >= imageInfo.Sectors)
{
ErrorMessage = "Tried to write past image size";
return false;
}
writingStream.Seek((long)(256 + sectorAddress * 256), SeekOrigin.Begin);
writingStream.Write(data, 0, data.Length);
ErrorMessage = "";
return true;
}
public bool WriteSectors(byte[] data, ulong sectorAddress, uint length)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
return false;
}
if(data.Length % imageInfo.SectorSize != 0)
{
ErrorMessage = "Incorrect data size";
return false;
}
if(sectorAddress + length > imageInfo.Sectors)
{
ErrorMessage = "Tried to write past image size";
return false;
}
writingStream.Seek((long)(256 + sectorAddress * 256), SeekOrigin.Begin);
writingStream.Write(data, 0, data.Length);
ErrorMessage = "";
return true;
}
public bool WriteSectorLong(byte[] data, ulong sectorAddress)
{
ErrorMessage = "Writing sectors with tags is not supported.";
return false;
}
public bool WriteSectorsLong(byte[] data, ulong sectorAddress, uint length)
{
ErrorMessage = "Writing sectors with tags is not supported.";
return false;
}
public bool SetTracks(List<Track> tracks)
{
ErrorMessage = "Unsupported feature";
return false;
}
public bool Close()
{
if(!IsWriting)
{
ErrorMessage = "Image is not opened for writing";
return false;
}
int cylinders = (int)(imageInfo.Sectors / 33 / 8);
writingStream.Seek(0, SeekOrigin.Begin);
writingStream.Write(BitConverter.GetBytes(cylinders), 0, 4);
writingStream.Flush();
writingStream.Close();
IsWriting = false;
return true;
}
public bool SetMetadata(ImageInfo metadata)
{
return true;
}
}
}