Skip to content

Commit

Permalink
Geometry as readonly struct
Browse files Browse the repository at this point in the history
  • Loading branch information
LTRData committed Apr 20, 2024
1 parent 0010397 commit 7b32ad7
Show file tree
Hide file tree
Showing 33 changed files with 188 additions and 185 deletions.
4 changes: 2 additions & 2 deletions Library/DiscUtils.Core/DiskImageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public abstract class DiskImageBuilder
/// <summary>
/// Gets or sets the geometry of this disk, as reported by the BIOS, will be implied from the content stream if not set.
/// </summary>
public Geometry BiosGeometry { get; set; }
public Geometry? BiosGeometry { get; set; }

/// <summary>
/// Gets or sets the content for this disk, implying the size of the disk.
Expand All @@ -53,7 +53,7 @@ public abstract class DiskImageBuilder
/// <summary>
/// Gets or sets the geometry of this disk, will be implied from the content stream if not set.
/// </summary>
public Geometry Geometry { get; set; }
public Geometry? Geometry { get; set; }

/// <summary>
/// Gets a value indicating whether this file format preserves BIOS geometry information.
Expand Down
22 changes: 8 additions & 14 deletions Library/DiscUtils.Core/Geometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace DiscUtils;
/// Struct that represent disk geometries.
/// </summary>
/// <remarks>Instances of this struct are immutable.</remarks>
public sealed class Geometry : IEquatable<Geometry>
public readonly struct Geometry : IEquatable<Geometry>
{
/// <summary>
/// Initializes a new instance of the Geometry class. The default 512 bytes per sector is assumed.
Expand Down Expand Up @@ -199,19 +199,19 @@ public static Geometry LbaAssistedBiosGeometry(long capacity, int bytesPerSector
/// <param name="capacity">The capacity of the disk.</param>
/// <returns>The new geometry.</returns>
/// <remarks>This method returns the LBA-Assisted geometry if the given geometry isn't BIOS-safe.</remarks>
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);
}

/// <summary>
Expand Down Expand Up @@ -424,16 +424,10 @@ public Geometry TranslateToBios(long capacity, GeometryTranslation translation)
/// <param name="other">The object to test against.</param>
/// <returns><c>true</c> if the <paramref name="other"/> is equivalent, else <c>false</c>.</returns>
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);

/// <summary>
/// Determines if this object is equivalent to another.
Expand Down
2 changes: 1 addition & 1 deletion Library/DiscUtils.Core/LogicalVolumeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public override string Identity
/// <summary>
/// Gets the disk geometry of the underlying storage medium, if any (may be Geometry.Null).
/// </summary>
public override Geometry PhysicalGeometry => _physicalVol == null ? Geometry.Null : _physicalVol.PhysicalGeometry;
public override Geometry? PhysicalGeometry => _physicalVol == null ? Geometry.Null : _physicalVol.PhysicalGeometry;

/// <summary>
/// Gets the offset of this volume in the underlying storage medium, if any (may be Zero).
Expand Down
6 changes: 4 additions & 2 deletions Library/DiscUtils.Core/Partitions/GuidPartitionTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public sealed class GuidPartitionTable : PartitionTable
/// <param name="disk">The disk containing the partition table.</param>
public GuidPartitionTable(VirtualDisk disk)
{
Init(disk.Content, disk.Geometry);
Init(disk.Content, disk.Geometry
?? throw new InvalidOperationException("Unknown disk geometry"));
}

/// <summary>
Expand Down Expand Up @@ -88,7 +89,8 @@ public GuidPartitionTable(Stream disk, Geometry diskGeometry)
/// <returns>An object to access the newly created partition table.</returns>
public static GuidPartitionTable Initialize(VirtualDisk disk)
{
return Initialize(disk.Content, disk.Geometry);
return Initialize(disk.Content, disk.Geometry
?? throw new InvalidOperationException("Unknown disk geometry"));
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Library/DiscUtils.Core/PhysicalVolumeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public Guid PartitionIdentity
/// <summary>
/// Gets the disk geometry of the underlying storage medium, if any (may be null).
/// </summary>
public override Geometry PhysicalGeometry => _disk.Geometry;
public override Geometry? PhysicalGeometry => _disk.Geometry;

/// <summary>
/// Gets the offset of this volume in the underlying storage medium, if any (may be Zero).
Expand Down
14 changes: 7 additions & 7 deletions Library/DiscUtils.Core/Raw/Disk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ public sealed class Disk : VirtualDisk
/// <param name="stream">The stream to read.</param>
/// <param name="ownsStream">Indicates if the new instance should control the lifetime of the stream.</param>
public Disk(Stream stream, Ownership ownsStream)
: this(stream, ownsStream, default) {}
: this(stream, ownsStream, null) {}

/// <summary>
/// Initializes a new instance of the Disk class.
/// </summary>
/// <param name="stream">The stream to read.</param>
/// <param name="ownsStream">Indicates if the new instance should control the lifetime of the stream.</param>
/// <param name="geometry">The emulated geometry of the disk.</param>
public Disk(Stream stream, Ownership ownsStream, Geometry geometry)
public Disk(Stream stream, Ownership ownsStream, Geometry? geometry)
{
_file = new DiskImageFile(stream, ownsStream, geometry);
}
Expand Down Expand Up @@ -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);
}

/// <summary>
Expand All @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -138,7 +138,7 @@ private Disk(DiskImageFile file)
/// <summary>
/// Gets the geometry of the disk.
/// </summary>
public override Geometry Geometry => _file.Geometry;
public override Geometry? Geometry => _file.Geometry;

/// <summary>
/// Gets the layers that make up the disk.
Expand All @@ -155,7 +155,7 @@ public override IEnumerable<VirtualDiskLayer> Layers
/// <returns>An object that accesses the stream as a disk.</returns>
public static Disk Initialize(Stream stream, Ownership ownsStream, long capacity)
{
return Initialize(stream, ownsStream, capacity, default);
return Initialize(stream, ownsStream, capacity, null);
}

/// <summary>
Expand All @@ -166,7 +166,7 @@ public static Disk Initialize(Stream stream, Ownership ownsStream, long capacity
/// <param name="capacity">The desired capacity of the new disk.</param>
/// <param name="geometry">The desired geometry of the new disk, or <c>null</c> for default.</param>
/// <returns>An object that accesses the stream as a disk.</returns>
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));
}
Expand Down
6 changes: 3 additions & 3 deletions Library/DiscUtils.Core/Raw/DiskImageFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public DiskImageFile(Stream stream)
/// <param name="stream">The stream to interpret.</param>
/// <param name="ownsStream">Indicates if the new instance should control the lifetime of the stream.</param>
/// <param name="geometry">The emulated geometry of the disk.</param>
public DiskImageFile(Stream stream, Ownership ownsStream, Geometry geometry = default)
public DiskImageFile(Stream stream, Ownership ownsStream, Geometry? geometry = null)
{
Content = stream as SparseStream;
_ownsContent = ownsStream;
Expand All @@ -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;
Expand Down Expand Up @@ -96,7 +96,7 @@ public DiskImageFile(Stream stream, Ownership ownsStream, Geometry geometry = de
/// <param name="capacity">The desired capacity of the new disk.</param>
/// <param name="geometry">The geometry of the new disk.</param>
/// <returns>An object that accesses the stream as a raw disk image.</returns>
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));

Expand Down
12 changes: 6 additions & 6 deletions Library/DiscUtils.Core/VirtualDisk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public abstract class VirtualDisk :
/// <summary>
/// Gets the geometry of the disk.
/// </summary>
public abstract Geometry Geometry { get; }
public abstract Geometry? Geometry { get; }

/// <summary>
/// Gets the geometry of the disk as it is anticipated a hypervisor BIOS will represent it.
Expand Down Expand Up @@ -242,7 +242,7 @@ public static VirtualDiskTypeInfo GetDiskType(string type, string variant)
/// <param name="geometry">The geometry of the new disk (or null).</param>
/// <param name="parameters">Untyped parameters controlling the creation process (TBD).</param>
/// <returns>The newly created disk.</returns>
public static VirtualDisk CreateDisk(DiscFileSystem fileSystem, string type, string variant, string path, long capacity, Geometry geometry, Dictionary<string, string> parameters)
public static VirtualDisk CreateDisk(DiscFileSystem fileSystem, string type, string variant, string path, long capacity, Geometry? geometry, Dictionary<string, string> parameters)
{
var factory = VirtualDiskManager.TypeMap[type];

Expand Down Expand Up @@ -274,7 +274,7 @@ public static VirtualDisk CreateDisk(DiscFileSystem fileSystem, string type, str
/// <param name="geometry">The geometry of the new disk (or null).</param>
/// <param name="parameters">Untyped parameters controlling the creation process (TBD).</param>
/// <returns>The newly created disk.</returns>
public static VirtualDisk CreateDisk(string type, string variant, string path, long capacity, Geometry geometry, Dictionary<string, string> parameters)
public static VirtualDisk CreateDisk(string type, string variant, string path, long capacity, Geometry? geometry, Dictionary<string, string> parameters)
{
return CreateDisk(type, variant, path, capacity, geometry, null, null, parameters);
}
Expand All @@ -290,7 +290,7 @@ public static VirtualDisk CreateDisk(string type, string variant, string path, l
/// <param name="parameters">Untyped parameters controlling the creation process (TBD).</param>
/// <param name="useAsync">Underlying files will be opened optimized for async use.</param>
/// <returns>The newly created disk.</returns>
public static VirtualDisk CreateDisk(string type, string variant, string path, long capacity, Geometry geometry, Dictionary<string, string> parameters, bool useAsync)
public static VirtualDisk CreateDisk(string type, string variant, string path, long capacity, Geometry? geometry, Dictionary<string, string> parameters, bool useAsync)
{
return CreateDisk(type, variant, path, capacity, geometry, null, null, parameters, useAsync);
}
Expand All @@ -307,7 +307,7 @@ public static VirtualDisk CreateDisk(string type, string variant, string path, l
/// <param name="password">The password to use when accessing the <c>path</c> (or null).</param>
/// <param name="parameters">Untyped parameters controlling the creation process (TBD).</param>
/// <returns>The newly created disk.</returns>
public static VirtualDisk CreateDisk(string type, string variant, string path, long capacity, Geometry geometry, string user, string password, Dictionary<string, string> parameters)
public static VirtualDisk CreateDisk(string type, string variant, string path, long capacity, Geometry? geometry, string user, string password, Dictionary<string, string> parameters)
=> CreateDisk(type, variant, path, capacity, geometry, user, password, parameters, useAsync: false);

/// <summary>
Expand All @@ -323,7 +323,7 @@ public static VirtualDisk CreateDisk(string type, string variant, string path, l
/// <param name="parameters">Untyped parameters controlling the creation process (TBD).</param>
/// <param name="useAsync">Underlying files will be opened optimized for async use.</param>
/// <returns>The newly created disk.</returns>
public static VirtualDisk CreateDisk(string type, string variant, string path, long capacity, Geometry geometry, string user, string password, Dictionary<string, string> parameters, bool useAsync)
public static VirtualDisk CreateDisk(string type, string variant, string path, long capacity, Geometry? geometry, string user, string password, Dictionary<string, string> parameters, bool useAsync)
{
var diskParams = new VirtualDiskParameters
{
Expand Down
2 changes: 1 addition & 1 deletion Library/DiscUtils.Core/VirtualDiskParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ public sealed class VirtualDiskParameters
/// <summary>
/// Gets or sets the physical (aka IDE) geometry of the disk.
/// </summary>
public Geometry Geometry { get; set; }
public Geometry? Geometry { get; set; }
}
2 changes: 1 addition & 1 deletion Library/DiscUtils.Core/VolumeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ internal VolumeInfo() {}
/// <summary>
/// Gets the disk geometry of the underlying storage medium, if any (may be null).
/// </summary>
public abstract Geometry PhysicalGeometry { get; }
public abstract Geometry? PhysicalGeometry { get; }

/// <summary>
/// Gets the disk geometry of the underlying storage medium (as used in BIOS calls), may be null.
Expand Down
2 changes: 1 addition & 1 deletion Library/DiscUtils.Dmg/Disk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public override SparseStream Content
/// <summary>
/// Gets the geometry of the disk.
/// </summary>
public override Geometry Geometry => _file.Geometry;
public override Geometry? Geometry => _file.Geometry;

/// <summary>
/// Gets the layers that make up the disk.
Expand Down
4 changes: 2 additions & 2 deletions Library/DiscUtils.Iscsi/Disk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ public override SparseStream Content
CanBeHardDisk = true,
DeterministicGeometry = false,
PreservesBiosGeometry = false,
CalcGeometry = Geometry.FromCapacity
CalcGeometry = DiscUtils.Geometry.FromCapacity
};

/// <summary>
/// The Geometry of the disk.
/// </summary>
public override Geometry Geometry
public override Geometry? Geometry
{
get
{
Expand Down
2 changes: 1 addition & 1 deletion Library/DiscUtils.OpticalDiscSharing/Disc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal Disc(Uri uri, string userName, string password)
/// <summary>
/// Gets the geometry of the disk.
/// </summary>
public override Geometry Geometry => _file.Geometry;
public override Geometry? Geometry => _file.Geometry;

/// <summary>
/// Gets the layers that make up the disc.
Expand Down
2 changes: 1 addition & 1 deletion Library/DiscUtils.OpticalDisk/Disc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public Disc(string path, FileAccess access, bool useAsync)
/// <summary>
/// Gets the geometry of the disk.
/// </summary>
public override Geometry Geometry => _file.Geometry;
public override Geometry? Geometry => _file.Geometry;

/// <summary>
/// Gets the layers that make up the disc.
Expand Down
3 changes: 2 additions & 1 deletion Library/DiscUtils.OpticalDisk/FileSystemFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Collections.Generic;
using System.IO;
using DiscUtils.Iso9660;
using DiscUtils.Streams;
using DiscUtils.Udf;
using DiscUtils.Vfs;

Expand All @@ -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);
Expand Down
Loading

0 comments on commit 7b32ad7

Please sign in to comment.