diff --git a/Library/DiscUtils.Core/DiskImageBuilder.cs b/Library/DiscUtils.Core/DiskImageBuilder.cs index e6b31fb03..bfc3ea965 100644 --- a/Library/DiscUtils.Core/DiskImageBuilder.cs +++ b/Library/DiscUtils.Core/DiskImageBuilder.cs @@ -38,7 +38,7 @@ public abstract class DiskImageBuilder /// /// Gets or sets the geometry of this disk, as reported by the BIOS, will be implied from the content stream if not set. /// - public Geometry BiosGeometry { get; set; } + public Geometry? BiosGeometry { get; set; } /// /// Gets or sets the content for this disk, implying the size of the disk. @@ -53,7 +53,7 @@ public abstract class DiskImageBuilder /// /// Gets or sets the geometry of this disk, will be implied from the content stream if not set. /// - public Geometry Geometry { get; set; } + public Geometry? Geometry { get; set; } /// /// Gets a value indicating whether this file format preserves BIOS geometry information. diff --git a/Library/DiscUtils.Core/Geometry.cs b/Library/DiscUtils.Core/Geometry.cs index 79f2ef6f2..3ab7b7e39 100644 --- a/Library/DiscUtils.Core/Geometry.cs +++ b/Library/DiscUtils.Core/Geometry.cs @@ -30,7 +30,7 @@ namespace DiscUtils; /// Struct that represent disk geometries. /// /// Instances of this struct are immutable. -public sealed class Geometry : IEquatable +public readonly struct Geometry : IEquatable { /// /// Initializes a new instance of the Geometry class. The default 512 bytes per sector is assumed. @@ -199,19 +199,19 @@ public static Geometry LbaAssistedBiosGeometry(long capacity, int bytesPerSector /// The capacity of the disk. /// The new geometry. /// This method returns the LBA-Assisted geometry if the given geometry isn't BIOS-safe. - public static Geometry MakeBiosSafe(Geometry geometry, long capacity) + public static Geometry MakeBiosSafe(Geometry? geometry, long capacity) { if (geometry == null) { return LbaAssistedBiosGeometry(capacity, Sizes.Sector); } - if (geometry.IsBiosSafe) + if (geometry.Value.IsBiosSafe) { - return geometry; + return geometry.Value; } - return LbaAssistedBiosGeometry(capacity, geometry.BytesPerSector); + return LbaAssistedBiosGeometry(capacity, geometry.Value.BytesPerSector); } /// @@ -424,16 +424,10 @@ public Geometry TranslateToBios(long capacity, GeometryTranslation translation) /// The object to test against. /// true if the is equivalent, else false. public bool Equals(Geometry other) - { - return other is not null && - Cylinders == other.Cylinders && HeadsPerCylinder == other.HeadsPerCylinder && - SectorsPerTrack == other.SectorsPerTrack && BytesPerSector == other.BytesPerSector; - } + => Cylinders == other.Cylinders && HeadsPerCylinder == other.HeadsPerCylinder && + SectorsPerTrack == other.SectorsPerTrack && BytesPerSector == other.BytesPerSector; - public static bool Equals(Geometry a, Geometry b) - { - return ReferenceEquals(a, b) || (a is not null && a.Equals(b)); - } + public static bool Equals(Geometry a, Geometry b) => a.Equals(b); /// /// Determines if this object is equivalent to another. diff --git a/Library/DiscUtils.Core/LogicalVolumeInfo.cs b/Library/DiscUtils.Core/LogicalVolumeInfo.cs index d5cc28b29..b28657d9d 100644 --- a/Library/DiscUtils.Core/LogicalVolumeInfo.cs +++ b/Library/DiscUtils.Core/LogicalVolumeInfo.cs @@ -83,7 +83,7 @@ public override string Identity /// /// Gets the disk geometry of the underlying storage medium, if any (may be Geometry.Null). /// - public override Geometry PhysicalGeometry => _physicalVol == null ? Geometry.Null : _physicalVol.PhysicalGeometry; + public override Geometry? PhysicalGeometry => _physicalVol == null ? Geometry.Null : _physicalVol.PhysicalGeometry; /// /// Gets the offset of this volume in the underlying storage medium, if any (may be Zero). diff --git a/Library/DiscUtils.Core/Partitions/GuidPartitionTable.cs b/Library/DiscUtils.Core/Partitions/GuidPartitionTable.cs index 63e82c5ba..54483cffb 100644 --- a/Library/DiscUtils.Core/Partitions/GuidPartitionTable.cs +++ b/Library/DiscUtils.Core/Partitions/GuidPartitionTable.cs @@ -48,7 +48,8 @@ public sealed class GuidPartitionTable : PartitionTable /// The disk containing the partition table. public GuidPartitionTable(VirtualDisk disk) { - Init(disk.Content, disk.Geometry); + Init(disk.Content, disk.Geometry + ?? throw new InvalidOperationException("Unknown disk geometry")); } /// @@ -88,7 +89,8 @@ public GuidPartitionTable(Stream disk, Geometry diskGeometry) /// An object to access the newly created partition table. public static GuidPartitionTable Initialize(VirtualDisk disk) { - return Initialize(disk.Content, disk.Geometry); + return Initialize(disk.Content, disk.Geometry + ?? throw new InvalidOperationException("Unknown disk geometry")); } /// diff --git a/Library/DiscUtils.Core/PhysicalVolumeInfo.cs b/Library/DiscUtils.Core/PhysicalVolumeInfo.cs index a653b7e9f..f37f9dc12 100644 --- a/Library/DiscUtils.Core/PhysicalVolumeInfo.cs +++ b/Library/DiscUtils.Core/PhysicalVolumeInfo.cs @@ -148,7 +148,7 @@ public Guid PartitionIdentity /// /// Gets the disk geometry of the underlying storage medium, if any (may be null). /// - public override Geometry PhysicalGeometry => _disk.Geometry; + public override Geometry? PhysicalGeometry => _disk.Geometry; /// /// Gets the offset of this volume in the underlying storage medium, if any (may be Zero). diff --git a/Library/DiscUtils.Core/Raw/Disk.cs b/Library/DiscUtils.Core/Raw/Disk.cs index 2b8016c5d..402a0ad98 100644 --- a/Library/DiscUtils.Core/Raw/Disk.cs +++ b/Library/DiscUtils.Core/Raw/Disk.cs @@ -43,7 +43,7 @@ public sealed class Disk : VirtualDisk /// The stream to read. /// Indicates if the new instance should control the lifetime of the stream. public Disk(Stream stream, Ownership ownsStream) - : this(stream, ownsStream, default) {} + : this(stream, ownsStream, null) {} /// /// Initializes a new instance of the Disk class. @@ -51,7 +51,7 @@ public Disk(Stream stream, Ownership ownsStream) /// The stream to read. /// Indicates if the new instance should control the lifetime of the stream. /// The emulated geometry of the disk. - public Disk(Stream stream, Ownership ownsStream, Geometry geometry) + public Disk(Stream stream, Ownership ownsStream, Geometry? geometry) { _file = new DiskImageFile(stream, ownsStream, geometry); } @@ -80,7 +80,7 @@ public Disk(string path, FileAccess access) { var share = access == FileAccess.Read ? FileShare.Read : FileShare.None; var locator = new LocalFileLocator(string.Empty, useAsync: false); - _file = new DiskImageFile(locator.Open(path, FileMode.Open, access, share), Ownership.Dispose, default); + _file = new DiskImageFile(locator.Open(path, FileMode.Open, access, share), Ownership.Dispose, null); } /// @@ -93,7 +93,7 @@ public Disk(string path, FileAccess access, bool useAsync) { var share = access == FileAccess.Read ? FileShare.Read : FileShare.None; var locator = new LocalFileLocator(string.Empty, useAsync); - _file = new DiskImageFile(locator.Open(path, FileMode.Open, access, share), Ownership.Dispose, default); + _file = new DiskImageFile(locator.Open(path, FileMode.Open, access, share), Ownership.Dispose, null); } /// @@ -138,7 +138,7 @@ private Disk(DiskImageFile file) /// /// Gets the geometry of the disk. /// - public override Geometry Geometry => _file.Geometry; + public override Geometry? Geometry => _file.Geometry; /// /// Gets the layers that make up the disk. @@ -155,7 +155,7 @@ public override IEnumerable Layers /// An object that accesses the stream as a disk. public static Disk Initialize(Stream stream, Ownership ownsStream, long capacity) { - return Initialize(stream, ownsStream, capacity, default); + return Initialize(stream, ownsStream, capacity, null); } /// @@ -166,7 +166,7 @@ public static Disk Initialize(Stream stream, Ownership ownsStream, long capacity /// The desired capacity of the new disk. /// The desired geometry of the new disk, or null for default. /// An object that accesses the stream as a disk. - public static Disk Initialize(Stream stream, Ownership ownsStream, long capacity, Geometry geometry) + public static Disk Initialize(Stream stream, Ownership ownsStream, long capacity, Geometry? geometry) { return new Disk(DiskImageFile.Initialize(stream, ownsStream, capacity, geometry)); } diff --git a/Library/DiscUtils.Core/Raw/DiskImageFile.cs b/Library/DiscUtils.Core/Raw/DiskImageFile.cs index 4762722f0..1baec71db 100644 --- a/Library/DiscUtils.Core/Raw/DiskImageFile.cs +++ b/Library/DiscUtils.Core/Raw/DiskImageFile.cs @@ -48,7 +48,7 @@ public DiskImageFile(Stream stream) /// The stream to interpret. /// Indicates if the new instance should control the lifetime of the stream. /// The emulated geometry of the disk. - public DiskImageFile(Stream stream, Ownership ownsStream, Geometry geometry = default) + public DiskImageFile(Stream stream, Ownership ownsStream, Geometry? geometry = null) { Content = stream as SparseStream; _ownsContent = ownsStream; @@ -59,7 +59,7 @@ public DiskImageFile(Stream stream, Ownership ownsStream, Geometry geometry = de _ownsContent = Ownership.Dispose; } - Geometry = geometry != default ? geometry : DetectGeometry(Content); + Geometry = geometry ?? DetectGeometry(Content); } public override long Capacity => Content.Length; @@ -96,7 +96,7 @@ public DiskImageFile(Stream stream, Ownership ownsStream, Geometry geometry = de /// The desired capacity of the new disk. /// The geometry of the new disk. /// An object that accesses the stream as a raw disk image. - public static DiskImageFile Initialize(Stream stream, Ownership ownsStream, long capacity, Geometry geometry = default) + public static DiskImageFile Initialize(Stream stream, Ownership ownsStream, long capacity, Geometry? geometry = null) { stream.SetLength(MathUtilities.RoundUp(capacity, Sizes.Sector)); diff --git a/Library/DiscUtils.Core/VirtualDisk.cs b/Library/DiscUtils.Core/VirtualDisk.cs index d84cde379..586298c6f 100644 --- a/Library/DiscUtils.Core/VirtualDisk.cs +++ b/Library/DiscUtils.Core/VirtualDisk.cs @@ -73,7 +73,7 @@ public abstract class VirtualDisk : /// /// Gets the geometry of the disk. /// - public abstract Geometry Geometry { get; } + public abstract Geometry? Geometry { get; } /// /// Gets the geometry of the disk as it is anticipated a hypervisor BIOS will represent it. @@ -242,7 +242,7 @@ public static VirtualDiskTypeInfo GetDiskType(string type, string variant) /// The geometry of the new disk (or null). /// Untyped parameters controlling the creation process (TBD). /// The newly created disk. - public static VirtualDisk CreateDisk(DiscFileSystem fileSystem, string type, string variant, string path, long capacity, Geometry geometry, Dictionary parameters) + public static VirtualDisk CreateDisk(DiscFileSystem fileSystem, string type, string variant, string path, long capacity, Geometry? geometry, Dictionary parameters) { var factory = VirtualDiskManager.TypeMap[type]; @@ -274,7 +274,7 @@ public static VirtualDisk CreateDisk(DiscFileSystem fileSystem, string type, str /// The geometry of the new disk (or null). /// Untyped parameters controlling the creation process (TBD). /// The newly created disk. - public static VirtualDisk CreateDisk(string type, string variant, string path, long capacity, Geometry geometry, Dictionary parameters) + public static VirtualDisk CreateDisk(string type, string variant, string path, long capacity, Geometry? geometry, Dictionary parameters) { return CreateDisk(type, variant, path, capacity, geometry, null, null, parameters); } @@ -290,7 +290,7 @@ public static VirtualDisk CreateDisk(string type, string variant, string path, l /// Untyped parameters controlling the creation process (TBD). /// Underlying files will be opened optimized for async use. /// The newly created disk. - public static VirtualDisk CreateDisk(string type, string variant, string path, long capacity, Geometry geometry, Dictionary parameters, bool useAsync) + public static VirtualDisk CreateDisk(string type, string variant, string path, long capacity, Geometry? geometry, Dictionary parameters, bool useAsync) { return CreateDisk(type, variant, path, capacity, geometry, null, null, parameters, useAsync); } @@ -307,7 +307,7 @@ public static VirtualDisk CreateDisk(string type, string variant, string path, l /// The password to use when accessing the path (or null). /// Untyped parameters controlling the creation process (TBD). /// The newly created disk. - public static VirtualDisk CreateDisk(string type, string variant, string path, long capacity, Geometry geometry, string user, string password, Dictionary parameters) + public static VirtualDisk CreateDisk(string type, string variant, string path, long capacity, Geometry? geometry, string user, string password, Dictionary parameters) => CreateDisk(type, variant, path, capacity, geometry, user, password, parameters, useAsync: false); /// @@ -323,7 +323,7 @@ public static VirtualDisk CreateDisk(string type, string variant, string path, l /// Untyped parameters controlling the creation process (TBD). /// Underlying files will be opened optimized for async use. /// The newly created disk. - public static VirtualDisk CreateDisk(string type, string variant, string path, long capacity, Geometry geometry, string user, string password, Dictionary parameters, bool useAsync) + public static VirtualDisk CreateDisk(string type, string variant, string path, long capacity, Geometry? geometry, string user, string password, Dictionary parameters, bool useAsync) { var diskParams = new VirtualDiskParameters { diff --git a/Library/DiscUtils.Core/VirtualDiskParameters.cs b/Library/DiscUtils.Core/VirtualDiskParameters.cs index 684295cb7..2cd1fc1c8 100644 --- a/Library/DiscUtils.Core/VirtualDiskParameters.cs +++ b/Library/DiscUtils.Core/VirtualDiskParameters.cs @@ -60,5 +60,5 @@ public sealed class VirtualDiskParameters /// /// Gets or sets the physical (aka IDE) geometry of the disk. /// - public Geometry Geometry { get; set; } + public Geometry? Geometry { get; set; } } \ No newline at end of file diff --git a/Library/DiscUtils.Core/VolumeInfo.cs b/Library/DiscUtils.Core/VolumeInfo.cs index 8b9812672..5387ddbb6 100644 --- a/Library/DiscUtils.Core/VolumeInfo.cs +++ b/Library/DiscUtils.Core/VolumeInfo.cs @@ -55,7 +55,7 @@ internal VolumeInfo() {} /// /// Gets the disk geometry of the underlying storage medium, if any (may be null). /// - public abstract Geometry PhysicalGeometry { get; } + public abstract Geometry? PhysicalGeometry { get; } /// /// Gets the disk geometry of the underlying storage medium (as used in BIOS calls), may be null. diff --git a/Library/DiscUtils.Dmg/Disk.cs b/Library/DiscUtils.Dmg/Disk.cs index eb9c2b625..b9ba2670d 100644 --- a/Library/DiscUtils.Dmg/Disk.cs +++ b/Library/DiscUtils.Dmg/Disk.cs @@ -90,7 +90,7 @@ public override SparseStream Content /// /// Gets the geometry of the disk. /// - public override Geometry Geometry => _file.Geometry; + public override Geometry? Geometry => _file.Geometry; /// /// Gets the layers that make up the disk. diff --git a/Library/DiscUtils.Iscsi/Disk.cs b/Library/DiscUtils.Iscsi/Disk.cs index d6e72b566..2f1e891a0 100644 --- a/Library/DiscUtils.Iscsi/Disk.cs +++ b/Library/DiscUtils.Iscsi/Disk.cs @@ -108,13 +108,13 @@ public override SparseStream Content CanBeHardDisk = true, DeterministicGeometry = false, PreservesBiosGeometry = false, - CalcGeometry = Geometry.FromCapacity + CalcGeometry = DiscUtils.Geometry.FromCapacity }; /// /// The Geometry of the disk. /// - public override Geometry Geometry + public override Geometry? Geometry { get { diff --git a/Library/DiscUtils.OpticalDiscSharing/Disc.cs b/Library/DiscUtils.OpticalDiscSharing/Disc.cs index 30181a78e..a12bdd1c2 100644 --- a/Library/DiscUtils.OpticalDiscSharing/Disc.cs +++ b/Library/DiscUtils.OpticalDiscSharing/Disc.cs @@ -82,7 +82,7 @@ internal Disc(Uri uri, string userName, string password) /// /// Gets the geometry of the disk. /// - public override Geometry Geometry => _file.Geometry; + public override Geometry? Geometry => _file.Geometry; /// /// Gets the layers that make up the disc. diff --git a/Library/DiscUtils.OpticalDisk/Disc.cs b/Library/DiscUtils.OpticalDisk/Disc.cs index e93cb45f3..62027dfd9 100644 --- a/Library/DiscUtils.OpticalDisk/Disc.cs +++ b/Library/DiscUtils.OpticalDisk/Disc.cs @@ -138,7 +138,7 @@ public Disc(string path, FileAccess access, bool useAsync) /// /// Gets the geometry of the disk. /// - public override Geometry Geometry => _file.Geometry; + public override Geometry? Geometry => _file.Geometry; /// /// Gets the layers that make up the disc. diff --git a/Library/DiscUtils.OpticalDisk/FileSystemFactory.cs b/Library/DiscUtils.OpticalDisk/FileSystemFactory.cs index 04439452e..576b5cb5a 100644 --- a/Library/DiscUtils.OpticalDisk/FileSystemFactory.cs +++ b/Library/DiscUtils.OpticalDisk/FileSystemFactory.cs @@ -23,6 +23,7 @@ using System.Collections.Generic; using System.IO; using DiscUtils.Iso9660; +using DiscUtils.Streams; using DiscUtils.Udf; using DiscUtils.Vfs; @@ -48,7 +49,7 @@ private UdfReader OpenUdf(Stream stream, VolumeInfo volumeInfo, FileSystemParame { if (volumeInfo != null) { - return new UdfReader(stream, volumeInfo.PhysicalGeometry.BytesPerSector); + return new UdfReader(stream, volumeInfo.PhysicalGeometry?.BytesPerSector ?? Sizes.Sector); } return new UdfReader(stream); diff --git a/Library/DiscUtils.Vhd/DiskImageFile.cs b/Library/DiscUtils.Vhd/DiskImageFile.cs index 4fa6dab56..a41388bde 100644 --- a/Library/DiscUtils.Vhd/DiskImageFile.cs +++ b/Library/DiscUtils.Vhd/DiskImageFile.cs @@ -219,7 +219,7 @@ public override string FullPath /// An object that accesses the stream as a VHD file. public static DiskImageFile InitializeFixed(Stream stream, Ownership ownsStream, long capacity) { - return InitializeFixed(stream, ownsStream, capacity, default(Geometry)); + return InitializeFixed(stream, ownsStream, capacity, null); } /// @@ -231,7 +231,7 @@ public static DiskImageFile InitializeFixed(Stream stream, Ownership ownsStream, /// The desired geometry of the new disk, or null for default. /// An object that accesses the stream as a VHD file. public static DiskImageFile InitializeFixed(Stream stream, Ownership ownsStream, long capacity, - Geometry geometry) + Geometry? geometry) { InitializeFixedInternal(stream, capacity, geometry); return new DiskImageFile(stream, ownsStream); @@ -246,7 +246,7 @@ public static DiskImageFile InitializeFixed(Stream stream, Ownership ownsStream, /// An object that accesses the stream as a VHD file. public static DiskImageFile InitializeDynamic(Stream stream, Ownership ownsStream, long capacity) { - return InitializeDynamic(stream, ownsStream, capacity, default(Geometry), DynamicHeader.DefaultBlockSize); + return InitializeDynamic(stream, ownsStream, capacity, null, DynamicHeader.DefaultBlockSize); } /// @@ -258,7 +258,7 @@ public static DiskImageFile InitializeDynamic(Stream stream, Ownership ownsStrea /// The desired geometry of the new disk, or null for default. /// An object that accesses the stream as a VHD file. public static DiskImageFile InitializeDynamic(Stream stream, Ownership ownsStream, long capacity, - Geometry geometry) + Geometry? geometry) { return InitializeDynamic(stream, ownsStream, capacity, geometry, DynamicHeader.DefaultBlockSize); } @@ -273,7 +273,7 @@ public static DiskImageFile InitializeDynamic(Stream stream, Ownership ownsStrea /// An object that accesses the stream as a VHD file. public static DiskImageFile InitializeDynamic(Stream stream, Ownership ownsStream, long capacity, long blockSize) { - return InitializeDynamic(stream, ownsStream, capacity, default(Geometry), blockSize); + return InitializeDynamic(stream, ownsStream, capacity, null, blockSize); } /// @@ -286,7 +286,7 @@ public static DiskImageFile InitializeDynamic(Stream stream, Ownership ownsStrea /// The size of each block (unit of allocation). /// An object that accesses the stream as a VHD file. public static DiskImageFile InitializeDynamic(Stream stream, Ownership ownsStream, long capacity, - Geometry geometry, long blockSize) + Geometry? geometry, long blockSize) { InitializeDynamicInternal(stream, capacity, geometry, blockSize); @@ -348,7 +348,7 @@ public string[] GetParentLocations(string basePath) return GetParentLocations(new LocalFileLocator(basePath, useAsync: false)).ToArray(); } - internal static DiskImageFile InitializeFixed(FileLocator locator, string path, long capacity, Geometry geometry) + internal static DiskImageFile InitializeFixed(FileLocator locator, string path, long capacity, Geometry? geometry) { DiskImageFile result = null; var stream = locator.Open(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None); @@ -367,7 +367,7 @@ internal static DiskImageFile InitializeFixed(FileLocator locator, string path, } internal static DiskImageFile InitializeDynamic(FileLocator locator, string path, long capacity, - Geometry geometry, long blockSize) + Geometry? geometry, long blockSize) { DiskImageFile result = null; var stream = locator.Open(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None); @@ -455,14 +455,11 @@ protected override void Dispose(bool disposing) } } - private static void InitializeFixedInternal(Stream stream, long capacity, Geometry geometry) + private static void InitializeFixedInternal(Stream stream, long capacity, Geometry? geometry) { - if (geometry == null) - { - geometry = DiscUtils.Geometry.FromCapacity(capacity); - } + geometry ??= DiscUtils.Geometry.FromCapacity(capacity); - var footer = new Footer(geometry, capacity, FileType.Fixed); + var footer = new Footer(geometry.Value, capacity, FileType.Fixed); footer.UpdateChecksum(); Span sector = stackalloc byte[Sizes.Sector]; @@ -474,19 +471,16 @@ private static void InitializeFixedInternal(Stream stream, long capacity, Geomet stream.Position = 0; } - private static void InitializeDynamicInternal(Stream stream, long capacity, Geometry geometry, long blockSize) + private static void InitializeDynamicInternal(Stream stream, long capacity, Geometry? geometry, long blockSize) { if (blockSize is > uint.MaxValue or < 0) { throw new ArgumentOutOfRangeException(nameof(blockSize), "Must be in the range 0 to uint.MaxValue"); } - if (geometry == null) - { - geometry = DiscUtils.Geometry.FromCapacity(capacity); - } + geometry ??= DiscUtils.Geometry.FromCapacity(capacity); - var footer = new Footer(geometry, capacity, FileType.Dynamic) + var footer = new Footer(geometry.Value, capacity, FileType.Dynamic) { DataOffset = 512 // Offset of Dynamic Header }; diff --git a/Library/DiscUtils.Vhdx/Disk.cs b/Library/DiscUtils.Vhdx/Disk.cs index c0e106d22..a1f9eb346 100644 --- a/Library/DiscUtils.Vhdx/Disk.cs +++ b/Library/DiscUtils.Vhdx/Disk.cs @@ -309,7 +309,7 @@ public override SparseStream Content /// /// Gets the geometry of the disk. /// - public override Geometry Geometry => _files[0].DiakImageFile.Geometry; + public override Geometry? Geometry => _files[0].DiakImageFile.Geometry; /// /// Gets the layers that make up the disk. @@ -334,7 +334,7 @@ public override IEnumerable Layers /// An object that accesses the stream as a VHDX file. public static Disk InitializeFixed(Stream stream, Ownership ownsStream, long capacity) { - return InitializeFixed(stream, ownsStream, capacity, default(Geometry)); + return InitializeFixed(stream, ownsStream, capacity, null); } /// @@ -345,7 +345,7 @@ public static Disk InitializeFixed(Stream stream, Ownership ownsStream, long cap /// The desired capacity of the new disk. /// The desired geometry of the new disk, or null for default. /// An object that accesses the stream as a VHDX file. - public static Disk InitializeFixed(Stream stream, Ownership ownsStream, long capacity, Geometry geometry) + public static Disk InitializeFixed(Stream stream, Ownership ownsStream, long capacity, Geometry? geometry) { return new Disk(DiskImageFile.InitializeFixed(stream, ownsStream, capacity, geometry), Ownership.Dispose); } @@ -358,7 +358,7 @@ public static Disk InitializeFixed(Stream stream, Ownership ownsStream, long cap /// The desired capacity of the new disk. /// /// An object that accesses the stream as a VHDX file. - public static Disk InitializeDynamic(Stream stream, Ownership ownsStream, long capacity, Geometry geometry) + public static Disk InitializeDynamic(Stream stream, Ownership ownsStream, long capacity, Geometry? geometry) { return new Disk(DiskImageFile.InitializeDynamic(stream, ownsStream, capacity, geometry), Ownership.Dispose); } @@ -384,7 +384,7 @@ public static Disk InitializeDynamic(Stream stream, Ownership ownsStream, long c /// /// The size of each block (unit of allocation). /// An object that accesses the stream as a VHDX file. - public static Disk InitializeDynamic(Stream stream, Ownership ownsStream, long capacity, Geometry geometry, long blockSize) + public static Disk InitializeDynamic(Stream stream, Ownership ownsStream, long capacity, Geometry? geometry, long blockSize) { return new Disk(DiskImageFile.InitializeDynamic(stream, ownsStream, capacity, geometry, blockSize), Ownership.Dispose); } @@ -471,12 +471,12 @@ public override VirtualDisk CreateDifferencingDisk(string path, bool useAsync = return new Disk(file, Ownership.Dispose); } - internal static Disk InitializeFixed(FileLocator fileLocator, string path, long capacity, Geometry geometry) + internal static Disk InitializeFixed(FileLocator fileLocator, string path, long capacity, Geometry? geometry) { return new Disk(DiskImageFile.InitializeFixed(fileLocator, path, capacity, geometry), Ownership.Dispose); } - internal static Disk InitializeDynamic(FileLocator fileLocator, string path, long capacity, Geometry geometry, long blockSize) + internal static Disk InitializeDynamic(FileLocator fileLocator, string path, long capacity, Geometry? geometry, long blockSize) { return new Disk(DiskImageFile.InitializeDynamic(fileLocator, path, capacity, geometry, blockSize), Ownership.Dispose); } diff --git a/Library/DiscUtils.Vhdx/DiskImageFile.cs b/Library/DiscUtils.Vhdx/DiskImageFile.cs index f364aafe9..7c5799082 100644 --- a/Library/DiscUtils.Vhdx/DiskImageFile.cs +++ b/Library/DiscUtils.Vhdx/DiskImageFile.cs @@ -272,7 +272,7 @@ public Guid ParentUniqueId /// An object that accesses the stream as a VHDX file. public static DiskImageFile InitializeFixed(Stream stream, Ownership ownsStream, long capacity) { - return InitializeFixed(stream, ownsStream, capacity, default(Geometry)); + return InitializeFixed(stream, ownsStream, capacity, null); } /// @@ -284,7 +284,7 @@ public static DiskImageFile InitializeFixed(Stream stream, Ownership ownsStream, /// The desired geometry of the new disk, or null for default. /// An object that accesses the stream as a VHDX file. public static DiskImageFile InitializeFixed(Stream stream, Ownership ownsStream, long capacity, - Geometry geometry) + Geometry? geometry) { InitializeFixedInternal(stream, capacity, geometry); return new DiskImageFile(stream, ownsStream); @@ -311,7 +311,7 @@ public static DiskImageFile InitializeDynamic(Stream stream, Ownership ownsStrea /// The desired capacity of the new disk. /// /// An object that accesses the stream as a VHDX file. - public static DiskImageFile InitializeDynamic(Stream stream, Ownership ownsStream, long capacity, Geometry geometry) + public static DiskImageFile InitializeDynamic(Stream stream, Ownership ownsStream, long capacity, Geometry? geometry) { InitializeDynamicInternal(stream, capacity, geometry, FileParameters.DefaultDynamicBlockSize); return new DiskImageFile(stream, ownsStream); @@ -326,7 +326,7 @@ public static DiskImageFile InitializeDynamic(Stream stream, Ownership ownsStrea /// /// The size of each block (unit of allocation). /// An object that accesses the stream as a VHDX file. - public static DiskImageFile InitializeDynamic(Stream stream, Ownership ownsStream, long capacity, Geometry geometry, long blockSize) + public static DiskImageFile InitializeDynamic(Stream stream, Ownership ownsStream, long capacity, Geometry? geometry, long blockSize) { InitializeDynamicInternal(stream, capacity, geometry, blockSize); @@ -401,7 +401,7 @@ public IEnumerable GetParentLocations(string basePath) return GetParentLocations(new LocalFileLocator(basePath, useAsync: false)); } - internal static DiskImageFile InitializeFixed(FileLocator locator, string path, long capacity, Geometry geometry) + internal static DiskImageFile InitializeFixed(FileLocator locator, string path, long capacity, Geometry? geometry) { DiskImageFile result = null; var stream = locator.Open(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None); @@ -419,7 +419,7 @@ internal static DiskImageFile InitializeFixed(FileLocator locator, string path, return result; } - internal static DiskImageFile InitializeDynamic(FileLocator locator, string path, long capacity, Geometry geometry, long blockSize) + internal static DiskImageFile InitializeDynamic(FileLocator locator, string path, long capacity, Geometry? geometry, long blockSize) { DiskImageFile result = null; var stream = locator.Open(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None); @@ -498,12 +498,12 @@ protected override void Dispose(bool disposing) } } - private static void InitializeFixedInternal(Stream stream, long capacity, Geometry geometry) + private static void InitializeFixedInternal(Stream stream, long capacity, Geometry? geometry) { throw new NotImplementedException(); } - private static void InitializeDynamicInternal(Stream stream, long capacity, Geometry geometry, long blockSize) + private static void InitializeDynamicInternal(Stream stream, long capacity, Geometry? geometry, long blockSize) { if (blockSize < Sizes.OneMiB || blockSize > Sizes.OneMiB * 256 || !Utilities.IsPowerOfTwo(blockSize)) { @@ -513,7 +513,7 @@ private static void InitializeDynamicInternal(Stream stream, long capacity, Geom geometry ??= Geometry.FromCapacity(capacity); - var logicalSectorSize = geometry.BytesPerSector; + var logicalSectorSize = geometry.Value.BytesPerSector; var physicalSectorSize = 4096; var chunkRatio = 0x800000L * logicalSectorSize / blockSize; var dataBlocksCount = MathUtilities.Ceil(capacity, blockSize); @@ -617,39 +617,47 @@ private static void InitializeDifferencingInternal(Stream stream, DiskImageFile var fileEnd = Sizes.OneMiB; - var header1 = new VhdxHeader(); - header1.SequenceNumber = 0; - header1.FileWriteGuid = Guid.NewGuid(); - header1.DataWriteGuid = Guid.NewGuid(); - header1.LogGuid = Guid.Empty; - header1.LogVersion = 0; - header1.Version = 1; - header1.LogLength = (uint)Sizes.OneMiB; - header1.LogOffset = (ulong)fileEnd; + var header1 = new VhdxHeader + { + SequenceNumber = 0, + FileWriteGuid = Guid.NewGuid(), + DataWriteGuid = Guid.NewGuid(), + LogGuid = Guid.Empty, + LogVersion = 0, + Version = 1, + LogLength = (uint)Sizes.OneMiB, + LogOffset = (ulong)fileEnd + }; header1.CalcChecksum(); fileEnd += header1.LogLength; - var header2 = new VhdxHeader(header1); - header2.SequenceNumber = 1; + var header2 = new VhdxHeader(header1) + { + SequenceNumber = 1 + }; header2.CalcChecksum(); var regionTable = new RegionTable(); - var metadataRegion = new RegionEntry(); - metadataRegion.Guid = RegionEntry.MetadataRegionGuid; - metadataRegion.FileOffset = fileEnd; - metadataRegion.Length = (uint)Sizes.OneMiB; - metadataRegion.Flags = RegionFlags.Required; + var metadataRegion = new RegionEntry + { + Guid = RegionEntry.MetadataRegionGuid, + FileOffset = fileEnd, + Length = (uint)Sizes.OneMiB, + Flags = RegionFlags.Required + }; regionTable.Regions.Add(metadataRegion.Guid, metadataRegion); fileEnd += metadataRegion.Length; - var batRegion = new RegionEntry(); - batRegion.Guid = RegionEntry.BatGuid; - batRegion.FileOffset = 3 * Sizes.OneMiB; - batRegion.Length = (uint)MathUtilities.RoundUp(totalBatEntriesDynamic * 8, Sizes.OneMiB); - batRegion.Flags = RegionFlags.Required; + var batRegion = new RegionEntry + { + Guid = RegionEntry.BatGuid, + FileOffset = 3 * Sizes.OneMiB, + Length = (uint)MathUtilities.RoundUp(totalBatEntriesDynamic * 8, Sizes.OneMiB), + Flags = RegionFlags.Required + }; regionTable.Regions.Add(batRegion.Guid, batRegion); fileEnd += batRegion.Length; @@ -852,7 +860,7 @@ private LogSequence FindActiveLogSequence() currentTail = currentSequence.Head.Position + LogEntry.LogSectorSize; } - currentTail = currentTail % logStream.Length; + currentTail %= logStream.Length; } while (currentTail > oldTail); return candidateActiveSequence; diff --git a/Library/DiscUtils.Vmdk/DescriptorFile.cs b/Library/DiscUtils.Vmdk/DescriptorFile.cs index b02a2966f..9510b4176 100644 --- a/Library/DiscUtils.Vmdk/DescriptorFile.cs +++ b/Library/DiscUtils.Vmdk/DescriptorFile.cs @@ -81,7 +81,7 @@ public DiskAdapterType AdapterType set => SetDiskDatabase(DiskDbAdapterType, FormatAdapterType(value)); } - public Geometry BiosGeometry + public Geometry? BiosGeometry { get { @@ -97,13 +97,18 @@ public Geometry BiosGeometry int.Parse(sectorsStr, CultureInfo.InvariantCulture)); } - return default; + return null; } set { - SetDiskDatabase(DiskDbBiosCylinders, value.Cylinders.ToString(CultureInfo.InvariantCulture)); - SetDiskDatabase(DiskDbBiosHeads, value.HeadsPerCylinder.ToString(CultureInfo.InvariantCulture)); - SetDiskDatabase(DiskDbBiosSectors, value.SectorsPerTrack.ToString(CultureInfo.InvariantCulture)); + if (value is null) + { + throw new InvalidOperationException("A valid geometry is needed"); + } + + SetDiskDatabase(DiskDbBiosCylinders, value.Value.Cylinders.ToString(CultureInfo.InvariantCulture)); + SetDiskDatabase(DiskDbBiosHeads, value.Value.HeadsPerCylinder.ToString(CultureInfo.InvariantCulture)); + SetDiskDatabase(DiskDbBiosSectors, value.Value.SectorsPerTrack.ToString(CultureInfo.InvariantCulture)); } } @@ -120,7 +125,7 @@ public DiskCreateType CreateType set => SetHeader(HeaderCreateType, FormatCreateType(value), DescriptorFileEntryType.Plain); } - public Geometry DiskGeometry + public Geometry? DiskGeometry { get { @@ -136,14 +141,19 @@ public Geometry DiskGeometry int.Parse(sectorsStr, CultureInfo.InvariantCulture)); } - return default; + return null; } set { - SetDiskDatabase(DiskDbCylinders, value.Cylinders.ToString(CultureInfo.InvariantCulture)); - SetDiskDatabase(DiskDbHeads, value.HeadsPerCylinder.ToString(CultureInfo.InvariantCulture)); - SetDiskDatabase(DiskDbSectors, value.SectorsPerTrack.ToString(CultureInfo.InvariantCulture)); + if (value is null) + { + throw new InvalidOperationException("A valid geometry is needed"); + } + + SetDiskDatabase(DiskDbCylinders, value.Value.Cylinders.ToString(CultureInfo.InvariantCulture)); + SetDiskDatabase(DiskDbHeads, value.Value.HeadsPerCylinder.ToString(CultureInfo.InvariantCulture)); + SetDiskDatabase(DiskDbSectors, value.Value.SectorsPerTrack.ToString(CultureInfo.InvariantCulture)); } } diff --git a/Library/DiscUtils.Vmdk/Disk.cs b/Library/DiscUtils.Vmdk/Disk.cs index 87fe17ec4..c35bcc23a 100644 --- a/Library/DiscUtils.Vmdk/Disk.cs +++ b/Library/DiscUtils.Vmdk/Disk.cs @@ -139,15 +139,13 @@ public override Geometry BiosGeometry { get { - var result = _files[_files.Count - 1].VirtualDiskLayer is DiskImageFile file - ? file.BiosGeometry - : default; + if (_files[_files.Count - 1].VirtualDiskLayer is DiskImageFile file) + { + return file.BiosGeometry; + } - result = result != default - ? result - : Geometry.MakeBiosSafe(_files[_files.Count - 1].VirtualDiskLayer.Geometry, Capacity); - - return result; + return + DiscUtils.Geometry.MakeBiosSafe(_files[_files.Count - 1].VirtualDiskLayer.Geometry, Capacity); } } @@ -196,7 +194,7 @@ public override SparseStream Content /// /// Gets the Geometry of this disk. /// - public override Geometry Geometry => _files[_files.Count - 1].VirtualDiskLayer.Geometry; + public override Geometry? Geometry => _files[_files.Count - 1].VirtualDiskLayer.Geometry; /// /// Gets the layers that make up the disk. @@ -287,7 +285,7 @@ public static Disk Initialize(string path, DiskParameters parameters, bool useAs /// The newly created disk image. public static Disk Initialize(string path, long capacity, DiskCreateType type) { - return Initialize(path, capacity, default, type); + return Initialize(path, capacity, null, type); } /// @@ -300,7 +298,7 @@ public static Disk Initialize(string path, long capacity, DiskCreateType type) /// The newly created disk image. public static Disk Initialize(string path, long capacity, DiskCreateType type, bool useAsync) { - return Initialize(path, capacity, default, type, useAsync); + return Initialize(path, capacity, null, type, useAsync); } /// @@ -311,7 +309,7 @@ public static Disk Initialize(string path, long capacity, DiskCreateType type, b /// The desired geometry of the new disk, or null for default. /// The type of virtual disk to create. /// The newly created disk image. - public static Disk Initialize(string path, long capacity, Geometry geometry, DiskCreateType type) + public static Disk Initialize(string path, long capacity, Geometry? geometry, DiskCreateType type) { return new Disk(DiskImageFile.Initialize(path, capacity, geometry, type), Ownership.Dispose); } @@ -325,7 +323,7 @@ public static Disk Initialize(string path, long capacity, Geometry geometry, Dis /// The type of virtual disk to create. /// Underlying files will be opened optimized for async use. /// The newly created disk image. - public static Disk Initialize(string path, long capacity, Geometry geometry, DiskCreateType type, bool useAsync) + public static Disk Initialize(string path, long capacity, Geometry? geometry, DiskCreateType type, bool useAsync) { return new Disk(DiskImageFile.Initialize(path, capacity, geometry, type, useAsync), Ownership.Dispose); } @@ -353,7 +351,7 @@ public static Disk Initialize(DiscFileSystem fileSystem, string path, long capac /// The newly created disk image. public static Disk Initialize(string path, long capacity, DiskCreateType type, DiskAdapterType adapterType) { - return Initialize(path, capacity, default, type, adapterType); + return Initialize(path, capacity, null, type, adapterType); } /// @@ -367,7 +365,7 @@ public static Disk Initialize(string path, long capacity, DiskCreateType type, D /// The newly created disk image. public static Disk Initialize(string path, long capacity, DiskCreateType type, DiskAdapterType adapterType, bool useAsync) { - return Initialize(path, capacity, default, type, adapterType, useAsync); + return Initialize(path, capacity, null, type, adapterType, useAsync); } /// @@ -379,7 +377,7 @@ public static Disk Initialize(string path, long capacity, DiskCreateType type, D /// The type of virtual disk to create. /// The type of virtual disk adapter. /// The newly created disk image. - public static Disk Initialize(string path, long capacity, Geometry geometry, DiskCreateType type, + public static Disk Initialize(string path, long capacity, Geometry? geometry, DiskCreateType type, DiskAdapterType adapterType) { return new Disk(DiskImageFile.Initialize(path, capacity, geometry, type, adapterType), Ownership.Dispose); @@ -395,7 +393,7 @@ public static Disk Initialize(string path, long capacity, Geometry geometry, Dis /// The type of virtual disk adapter. /// Underlying files will be opened optimized for async use. /// The newly created disk image. - public static Disk Initialize(string path, long capacity, Geometry geometry, DiskCreateType type, + public static Disk Initialize(string path, long capacity, Geometry? geometry, DiskCreateType type, DiskAdapterType adapterType, bool useAsync) { return new Disk(DiskImageFile.Initialize(path, capacity, geometry, type, adapterType, useAsync), Ownership.Dispose); diff --git a/Library/DiscUtils.Vmdk/DiskBuilder.cs b/Library/DiscUtils.Vmdk/DiskBuilder.cs index 0234ba648..ac87be2ca 100644 --- a/Library/DiscUtils.Vmdk/DiskBuilder.cs +++ b/Library/DiscUtils.Vmdk/DiskBuilder.cs @@ -103,8 +103,8 @@ public override IEnumerable Build(string baseName) throw new NotImplementedException("Only MonolithicSparse, Vmfs and VmfsSparse disks implemented"); } - var geometry = Geometry != default ? Geometry : DiskImageFile.DefaultGeometry(Content.Length); - var biosGeometry = BiosGeometry != default ? BiosGeometry : Geometry.LbaAssistedBiosGeometry(Content.Length, Sizes.Sector); + var geometry = Geometry ?? DiskImageFile.DefaultGeometry(Content.Length); + var biosGeometry = BiosGeometry ?? DiscUtils.Geometry.LbaAssistedBiosGeometry(Content.Length, Sizes.Sector); var baseDescriptor = DiskImageFile.CreateSimpleDiskDescriptor(geometry, biosGeometry, DiskType, AdapterType); diff --git a/Library/DiscUtils.Vmdk/DiskImageFile.cs b/Library/DiscUtils.Vmdk/DiskImageFile.cs index 447c20d5d..3ecdfa6a2 100644 --- a/Library/DiscUtils.Vmdk/DiskImageFile.cs +++ b/Library/DiscUtils.Vmdk/DiskImageFile.cs @@ -177,7 +177,8 @@ internal DiskImageFile(FileLocator fileLocator, string file, FileAccess access) /// /// Gets the BIOS geometry of this disk. /// - internal Geometry BiosGeometry => _descriptor.BiosGeometry; + internal Geometry BiosGeometry => _descriptor.BiosGeometry + ?? throw new InvalidOperationException("Unknown geometry"); /// /// Gets the capacity of this disk (in bytes). @@ -247,7 +248,8 @@ public override IList Extents /// /// Gets the Geometry of this disk. /// - public override Geometry Geometry => _descriptor.DiskGeometry; + public override Geometry Geometry => _descriptor.DiskGeometry + ?? throw new InvalidOperationException("Unknown geometry"); /// /// Gets an indication as to whether the disk file is sparse. @@ -323,7 +325,7 @@ public static DiskImageFile Initialize(string path, long capacity, DiskCreateTyp /// The type of virtual disk to create. /// Underlying files will be opened optimized for async use. /// The newly created disk image. - public static DiskImageFile Initialize(string path, long capacity, Geometry geometry, DiskCreateType createType, bool useAsync = false) + public static DiskImageFile Initialize(string path, long capacity, Geometry? geometry, DiskCreateType createType, bool useAsync = false) { var diskParams = new DiskParameters { @@ -345,7 +347,7 @@ public static DiskImageFile Initialize(string path, long capacity, Geometry geom /// The type of disk adapter used with the disk. /// Underlying files will be opened optimized for async use. /// The newly created disk image. - public static DiskImageFile Initialize(string path, long capacity, Geometry geometry, DiskCreateType createType, + public static DiskImageFile Initialize(string path, long capacity, Geometry? geometry, DiskCreateType createType, DiskAdapterType adapterType, bool useAsync = false) { var diskParams = new DiskParameters @@ -524,17 +526,10 @@ internal static DiskImageFile Initialize(FileLocator fileLocator, string path, D throw new ArgumentException("Capacity must be greater than zero", nameof(parameters)); } - var geometry = parameters.Geometry != default ? parameters.Geometry : DefaultGeometry(parameters.Capacity); + var geometry = parameters.Geometry ?? DefaultGeometry(parameters.Capacity); - Geometry biosGeometry; - if (parameters.BiosGeometry != default) - { - biosGeometry = parameters.BiosGeometry; - } - else - { - biosGeometry = Geometry.MakeBiosSafe(geometry, parameters.Capacity); - } + var biosGeometry = parameters.BiosGeometry ?? + Geometry.MakeBiosSafe(geometry, parameters.Capacity); var adapterType = parameters.AdapterType == DiskAdapterType.None ? DiskAdapterType.LsiLogicScsi @@ -574,7 +569,7 @@ internal static Geometry DefaultGeometry(long diskSize) return new Geometry(cylinders, heads, sectors); } - internal static DescriptorFile CreateSimpleDiskDescriptor(Geometry geometry, Geometry biosGeometery, + internal static DescriptorFile CreateSimpleDiskDescriptor(Geometry? geometry, Geometry? biosGeometery, DiskCreateType createType, DiskAdapterType adapterType) { var baseDescriptor = new DescriptorFile diff --git a/Library/DiscUtils.Vmdk/DiskParameters.cs b/Library/DiscUtils.Vmdk/DiskParameters.cs index 04b2681aa..f1d9f7eb3 100644 --- a/Library/DiscUtils.Vmdk/DiskParameters.cs +++ b/Library/DiscUtils.Vmdk/DiskParameters.cs @@ -86,7 +86,7 @@ public DiskParameters(VirtualDiskParameters genericParameters) /// /// Gets or sets the BIOS Geometry of the virtual disk. /// - public Geometry BiosGeometry { get; set; } + public Geometry? BiosGeometry { get; set; } /// /// Gets or sets the capacity of the virtual disk. @@ -101,5 +101,5 @@ public DiskParameters(VirtualDiskParameters genericParameters) /// /// Gets or sets the Physical Geometry of the virtual disk. /// - public Geometry Geometry { get; set; } + public Geometry? Geometry { get; set; } } \ No newline at end of file diff --git a/Library/DiscUtils.Xva/Disk.cs b/Library/DiscUtils.Xva/Disk.cs index 2dd693fe2..076403662 100644 --- a/Library/DiscUtils.Xva/Disk.cs +++ b/Library/DiscUtils.Xva/Disk.cs @@ -92,7 +92,7 @@ public override SparseStream Content /// /// The geometry is not stored with the disk, so this is at best /// a guess of the actual geometry. - public override Geometry Geometry => Geometry.FromCapacity(_capacity); + public override Geometry? Geometry => DiscUtils.Geometry.FromCapacity(_capacity); /// /// Gets the (single) layer of an XVA disk. diff --git a/Tests/LibraryTests/Vdi/DiskTest.cs b/Tests/LibraryTests/Vdi/DiskTest.cs index f90ca8ebe..a0f60a2e5 100644 --- a/Tests/LibraryTests/Vdi/DiskTest.cs +++ b/Tests/LibraryTests/Vdi/DiskTest.cs @@ -38,8 +38,8 @@ public void InitializeFixed() using (var disk = Disk.InitializeFixed(ms, Ownership.None, 8 * 1024 * 1024)) { Assert.NotNull(disk); - Assert.True(disk.Geometry.Capacity is > (long)(7.5 * 1024 * 1024) and < (8 * 1024 * 1024)); - Assert.True(disk.Geometry.Capacity <= disk.Content.Length); + Assert.True(disk.Geometry.Value.Capacity is > (long)(7.5 * 1024 * 1024) and < (8 * 1024 * 1024)); + Assert.True(disk.Geometry.Value.Capacity <= disk.Content.Length); } // Check the stream is still valid @@ -65,16 +65,16 @@ public void InitializeDynamic() using (var disk = Disk.InitializeDynamic(ms, Ownership.None, 16 * 1024L * 1024 * 1024)) { Assert.NotNull(disk); - Assert.True(disk.Geometry.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and < (16 * 1024L * 1024 * 1024)); - Assert.True(disk.Geometry.Capacity <= disk.Content.Length); + Assert.True(disk.Geometry.Value.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and < (16 * 1024L * 1024 * 1024)); + Assert.True(disk.Geometry.Value.Capacity <= disk.Content.Length); } Assert.True(1 * 1024 * 1024 > ms.Length); using (var disk = new Disk(ms)) { - Assert.True(disk.Geometry.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and < (16 * 1024L * 1024 * 1024)); - Assert.True(disk.Geometry.Capacity <= disk.Content.Length); + Assert.True(disk.Geometry.Value.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and < (16 * 1024L * 1024 * 1024)); + Assert.True(disk.Geometry.Value.Capacity <= disk.Content.Length); } } @@ -85,7 +85,7 @@ public void ConstructorDynamic() var ms = new MemoryStream(); using (var disk = Disk.InitializeDynamic(ms, Ownership.None, 16 * 1024L * 1024 * 1024)) { - geometry = disk.Geometry; + geometry = disk.Geometry.Value; } using (var disk = new Disk(ms)) diff --git a/Tests/LibraryTests/Vhd/DiskTest.cs b/Tests/LibraryTests/Vhd/DiskTest.cs index bfa3a87ec..1ba629cd6 100644 --- a/Tests/LibraryTests/Vhd/DiskTest.cs +++ b/Tests/LibraryTests/Vhd/DiskTest.cs @@ -39,7 +39,7 @@ public void InitializeFixed() using (var disk = Disk.InitializeFixed(ms, Ownership.None, 8 * 1024 * 1024)) { Assert.NotNull(disk); - Assert.True(disk.Geometry.Capacity is > (long)(7.5 * 1024 * 1024) and <= (8 * 1024 * 1024)); + Assert.True(disk.Geometry.Value.Capacity is > (long)(7.5 * 1024 * 1024) and <= (8 * 1024 * 1024)); } // Check the stream is still valid @@ -65,14 +65,14 @@ public void InitializeDynamic() using (var disk = Disk.InitializeDynamic(ms, Ownership.None, 16 * 1024L * 1024 * 1024)) { Assert.NotNull(disk); - Assert.True(disk.Geometry.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and <= (16 * 1024L * 1024 * 1024)); + Assert.True(disk.Geometry.Value.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and <= (16 * 1024L * 1024 * 1024)); } Assert.True(1 * 1024 * 1024 > ms.Length); using (var disk = new Disk(ms, Ownership.Dispose)) { - Assert.True(disk.Geometry.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and <= (16 * 1024L * 1024 * 1024)); + Assert.True(disk.Geometry.Value.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and <= (16 * 1024L * 1024 * 1024)); } } @@ -85,8 +85,8 @@ public void InitializeDifferencing() using (var disk = Disk.InitializeDifferencing(diffStream, Ownership.None, baseFile, Ownership.Dispose, @"C:\TEMP\Base.vhd", @".\Base.vhd", DateTime.UtcNow)) { Assert.NotNull(disk); - Assert.True(disk.Geometry.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and <= (16 * 1024L * 1024 * 1024)); - Assert.True(disk.Geometry.Capacity == baseFile.Geometry.Capacity); + Assert.True(disk.Geometry.Value.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and <= (16 * 1024L * 1024 * 1024)); + Assert.True(disk.Geometry.Value.Capacity == baseFile.Geometry.Capacity); Assert.Equal(2, new List(disk.Layers).Count); } @@ -101,7 +101,7 @@ public void ConstructorDynamic() var ms = new MemoryStream(); using (var disk = Disk.InitializeDynamic(ms, Ownership.None, 16 * 1024L * 1024 * 1024)) { - geometry = disk.Geometry; + geometry = disk.Geometry.Value; } using (var disk = new Disk(ms, Ownership.None)) diff --git a/Tests/LibraryTests/Vhdx/DiskTest.cs b/Tests/LibraryTests/Vhdx/DiskTest.cs index 405020c64..5b7285c5d 100644 --- a/Tests/LibraryTests/Vhdx/DiskTest.cs +++ b/Tests/LibraryTests/Vhdx/DiskTest.cs @@ -39,7 +39,7 @@ public void InitializeFixed() using (var disk = Disk.InitializeDynamic(ms, Ownership.None, 8 * 1024 * 1024)) { Assert.NotNull(disk); - Assert.True(disk.Geometry.Capacity is > (long)(7.5 * 1024 * 1024) and <= (8 * 1024 * 1024)); + Assert.True(disk.Geometry.Value.Capacity is > (long)(7.5 * 1024 * 1024) and <= (8 * 1024 * 1024)); } // Check the stream is still valid @@ -65,14 +65,14 @@ public void InitializeDynamic() using (var disk = Disk.InitializeDynamic(ms, Ownership.None, 16 * 1024L * 1024 * 1024)) { Assert.NotNull(disk); - Assert.True(disk.Geometry.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and <= (16 * 1024L * 1024 * 1024)); + Assert.True(disk.Geometry.Value.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and <= (16 * 1024L * 1024 * 1024)); } Assert.True(8 * 1024 * 1024 > ms.Length); using (var disk = new Disk(ms, Ownership.Dispose)) { - Assert.True(disk.Geometry.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and <= (16 * 1024L * 1024 * 1024)); + Assert.True(disk.Geometry.Value.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and <= (16 * 1024L * 1024 * 1024)); } } @@ -85,8 +85,8 @@ public void InitializeDifferencing() using (var disk = Disk.InitializeDifferencing(diffStream, Ownership.None, baseFile, Ownership.Dispose, @"C:\TEMP\Base.vhd", @".\Base.vhd", DateTime.UtcNow)) { Assert.NotNull(disk); - Assert.True(disk.Geometry.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and <= (16 * 1024L * 1024 * 1024)); - Assert.True(disk.Geometry.Capacity == baseFile.Geometry.Capacity); + Assert.True(disk.Geometry.Value.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and <= (16 * 1024L * 1024 * 1024)); + Assert.True(disk.Geometry.Value.Capacity == baseFile.Geometry.Capacity); Assert.Equal(2, new List(disk.Layers).Count); } @@ -101,7 +101,7 @@ public void ConstructorDynamic() var ms = new MemoryStream(); using (var disk = Disk.InitializeDynamic(ms, Ownership.None, 16 * 1024L * 1024 * 1024)) { - geometry = disk.Geometry; + geometry = disk.Geometry.Value; } using (var disk = new Disk(ms, Ownership.None)) diff --git a/Tests/LibraryTests/Vmdk/DiskTest.cs b/Tests/LibraryTests/Vmdk/DiskTest.cs index 7d1c83dae..4e2e63a6a 100644 --- a/Tests/LibraryTests/Vmdk/DiskTest.cs +++ b/Tests/LibraryTests/Vmdk/DiskTest.cs @@ -41,8 +41,8 @@ public void InitializeFixed() { using var disk = Disk.Initialize(new InMemoryFileSystem(), "a.vmdk", 8 * 1024 * 1024, DiskCreateType.MonolithicFlat); Assert.NotNull(disk); - Assert.True(disk.Geometry.Capacity is > (long)(7.9 * 1024 * 1024) and < (long)(8.1 * 1024 * 1024)); - Assert.True(disk.Geometry.Capacity == disk.Content.Length); + Assert.True(disk.Geometry.Value.Capacity is > (long)(7.9 * 1024 * 1024) and < (long)(8.1 * 1024 * 1024)); + Assert.True(disk.Geometry.Value.Capacity == disk.Content.Length); var links = new List(disk.Links); var paths = new List(links[0].ExtentPaths); @@ -55,8 +55,8 @@ public void InitializeFixedIDE() { using var disk = Disk.Initialize(new InMemoryFileSystem(), "a.vmdk", 8 * 1024 * 1024, DiskCreateType.MonolithicFlat, DiskAdapterType.Ide); Assert.NotNull(disk); - Assert.True(disk.Geometry.Capacity is > (long)(7.9 * 1024 * 1024) and < (long)(8.1 * 1024 * 1024)); - Assert.True(disk.Geometry.Capacity == disk.Content.Length); + Assert.True(disk.Geometry.Value.Capacity is > (long)(7.9 * 1024 * 1024) and < (long)(8.1 * 1024 * 1024)); + Assert.True(disk.Geometry.Value.Capacity == disk.Content.Length); var links = new List(disk.Links); var paths = new List(links[0].ExtentPaths); @@ -71,7 +71,7 @@ public void InitializeDynamic() using (var disk = Disk.Initialize(fs, "a.vmdk", 16 * 1024L * 1024 * 1024, DiskCreateType.MonolithicSparse)) { Assert.NotNull(disk); - Assert.True(disk.Geometry.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and <= (16 * 1024L * 1024 * 1024)); + Assert.True(disk.Geometry.Value.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and <= (16 * 1024L * 1024 * 1024)); Assert.True(disk.Content.Length == 16 * 1024L * 1024 * 1024); } @@ -80,7 +80,7 @@ public void InitializeDynamic() using (var disk = new Disk(fs, "a.vmdk", FileAccess.Read)) { - Assert.True(disk.Geometry.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and <= (16 * 1024L * 1024 * 1024)); + Assert.True(disk.Geometry.Value.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and <= (16 * 1024L * 1024 * 1024)); Assert.True(disk.Content.Length == 16 * 1024L * 1024 * 1024); var links = new List(disk.Links); @@ -101,7 +101,7 @@ public void InitializeDifferencing() using (var disk = Disk.InitializeDifferencing(fs, $"{sep}diff{sep}diff.vmdk", DiskCreateType.MonolithicSparse, $"{sep}base{sep}base.vmdk")) { Assert.NotNull(disk); - Assert.True(disk.Geometry.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and < (16 * 1024L * 1024 * 1024)); + Assert.True(disk.Geometry.Value.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and < (16 * 1024L * 1024 * 1024)); Assert.True(disk.Content.Length == 16 * 1024L * 1024 * 1024); Assert.Equal(2, new List(disk.Layers).Count); @@ -128,7 +128,7 @@ public void InitializeDifferencingRelPath() using (var disk = Disk.InitializeDifferencing(fs, $"{sep}dir{sep}diff.vmdk", DiskCreateType.MonolithicSparse, $"subdir{sep}base.vmdk")) { Assert.NotNull(disk); - Assert.True(disk.Geometry.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and < (16 * 1024L * 1024 * 1024)); + Assert.True(disk.Geometry.Value.Capacity is > (long)(15.8 * 1024L * 1024 * 1024) and < (16 * 1024L * 1024 * 1024)); Assert.True(disk.Content.Length == 16 * 1024L * 1024 * 1024); Assert.Equal(2, new List(disk.Layers).Count); } diff --git a/Utilities/DiscUtils.PowerShell/NewVirtualDiskCommand.cs b/Utilities/DiscUtils.PowerShell/NewVirtualDiskCommand.cs index 50d3d9d63..c2c0f47ed 100644 --- a/Utilities/DiscUtils.PowerShell/NewVirtualDiskCommand.cs +++ b/Utilities/DiscUtils.PowerShell/NewVirtualDiskCommand.cs @@ -90,7 +90,7 @@ private void CreateNewDisk() if (parentObj.BaseObject is DirectoryInfo dirInfo) { var path = Path.Combine(dirInfo.FullName, child); - using (var realDisk = VirtualDisk.CreateDisk(type, variant, path, size, default, null, useAsync: false)) + using (var realDisk = VirtualDisk.CreateDisk(type, variant, path, size, null, null, useAsync: false)) { } @@ -99,7 +99,7 @@ private void CreateNewDisk() else if (parentObj.BaseObject is DiscDirectoryInfo ddi) { var path = Path.Combine(ddi.FullName, child); - using (var realDisk = VirtualDisk.CreateDisk(ddi.FileSystem, type, variant, path, size, default, null)) + using (var realDisk = VirtualDisk.CreateDisk(ddi.FileSystem, type, variant, path, size, null, null)) { } diff --git a/Utilities/DiscUtils.PowerShell/VirtualDiskProvider/OnDemandVirtualDisk.cs b/Utilities/DiscUtils.PowerShell/VirtualDiskProvider/OnDemandVirtualDisk.cs index ddc57b281..164388256 100644 --- a/Utilities/DiscUtils.PowerShell/VirtualDiskProvider/OnDemandVirtualDisk.cs +++ b/Utilities/DiscUtils.PowerShell/VirtualDiskProvider/OnDemandVirtualDisk.cs @@ -69,7 +69,7 @@ public bool IsValid } } - public override Geometry Geometry + public override Geometry? Geometry { get { diff --git a/Utilities/DiskClone/Disk.cs b/Utilities/DiskClone/Disk.cs index ec00338f7..9303466a3 100644 --- a/Utilities/DiskClone/Disk.cs +++ b/Utilities/DiskClone/Disk.cs @@ -74,7 +74,7 @@ public override SparseStream Content } } - public override Geometry Geometry => Geometry.FromCapacity(Capacity); + public override Geometry? Geometry => DiscUtils.Geometry.FromCapacity(Capacity); public override Geometry BiosGeometry { @@ -98,7 +98,7 @@ public override Geometry BiosGeometry CanBeHardDisk = true, DeterministicGeometry = false, PreservesBiosGeometry = false, - CalcGeometry = Geometry.FromCapacity, + CalcGeometry = DiscUtils.Geometry.FromCapacity, }; public override VirtualDisk CreateDifferencingDisk(DiscFileSystem fileSystem, string path) diff --git a/Utilities/DiskClone/Program.cs b/Utilities/DiskClone/Program.cs index a6c1131b3..2bb0fa74c 100644 --- a/Utilities/DiskClone/Program.cs +++ b/Utilities/DiskClone/Program.cs @@ -104,7 +104,8 @@ protected override void DoRun() { contentBuilder = new BiosPartitionedDiskBuilder(disk); biosGeometry = disk.BiosGeometry; - ideGeometry = disk.Geometry; + ideGeometry = disk.Geometry + ?? throw new NotSupportedException("Unknown disk geometry"); capacity = disk.Capacity; } @@ -126,7 +127,7 @@ protected override void DoRun() if (translation != GeometryTranslation.None) { - contentBuilder.UpdateBiosGeometry(builder.BiosGeometry); + contentBuilder.UpdateBiosGeometry(builder.BiosGeometry.Value); } IVssBackupComponents backupCmpnts; @@ -197,7 +198,7 @@ protected override void DoRun() if (translation != GeometryTranslation.None) { - ntfs.UpdateBiosGeometry(builder.BiosGeometry); + ntfs.UpdateBiosGeometry(builder.BiosGeometry.Value); } } diff --git a/Utilities/VirtualDiskConvert/Program.cs b/Utilities/VirtualDiskConvert/Program.cs index 4581f0dac..ffc627ad5 100644 --- a/Utilities/VirtualDiskConvert/Program.cs +++ b/Utilities/VirtualDiskConvert/Program.cs @@ -83,7 +83,7 @@ protected override void DoRun() if (_translation.IsPresent && _translation.EnumValue != GeometryTranslation.None) { - diskParams.BiosGeometry = diskParams.Geometry.TranslateToBios(diskParams.Capacity, _translation.EnumValue); + diskParams.BiosGeometry = diskParams.Geometry.Value.TranslateToBios(diskParams.Capacity, _translation.EnumValue); } else if (!inDisk.DiskTypeInfo.PreservesBiosGeometry) {