From 2215133a22c8537f12a379754ad40e033bb029e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20N=C3=B8rfjand=20Stengaard?= Date: Tue, 6 Dec 2022 13:50:37 +0100 Subject: [PATCH] Update hst amiga package to fix updating rigid disk block pointers. Fix use of percent for size of disk and partition. Moved size to sector size to relevant commands. --- src/Hst.Imager.ConsoleApp/CommandHandler.cs | 1 + src/Hst.Imager.Core/Commands/MbrPartAddCommand.cs | 2 +- src/Hst.Imager.Core/Commands/OptimizeCommand.cs | 3 ++- src/Hst.Imager.Core/Commands/RdbInitCommand.cs | 3 ++- src/Hst.Imager.Core/Commands/RdbPartAddCommand.cs | 5 ++++- src/Hst.Imager.Core/Extensions/PercentExtensions.cs | 12 +++++++++++- src/Hst.Imager.Core/Extensions/SizeExtensions.cs | 3 +-- src/Hst.Imager.Core/Hst.Imager.Core.csproj | 2 +- 8 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/Hst.Imager.ConsoleApp/CommandHandler.cs b/src/Hst.Imager.ConsoleApp/CommandHandler.cs index b5c131e..b8e9d31 100644 --- a/src/Hst.Imager.ConsoleApp/CommandHandler.cs +++ b/src/Hst.Imager.ConsoleApp/CommandHandler.cs @@ -106,6 +106,7 @@ private static Size ParseSize(string size) "kb" => new Size(sizeValue * 1024, Unit.Bytes), "mb" => new Size(sizeValue * (long)Math.Pow(1024, 2), Unit.Bytes), "gb" => new Size(sizeValue * (long)Math.Pow(1024, 3), Unit.Bytes), + "tb" => new Size(sizeValue * (long)Math.Pow(1024, 4), Unit.Bytes), _ => throw new ArgumentException("Invalid size", nameof(size)) }; } diff --git a/src/Hst.Imager.Core/Commands/MbrPartAddCommand.cs b/src/Hst.Imager.Core/Commands/MbrPartAddCommand.cs index ee33219..c2058d3 100644 --- a/src/Hst.Imager.Core/Commands/MbrPartAddCommand.cs +++ b/src/Hst.Imager.Core/Commands/MbrPartAddCommand.cs @@ -93,7 +93,7 @@ public override async Task Execute(CancellationToken token) } // calculate partition size and sectors - var partitionSize = availableSize.ResolveSize(size); + var partitionSize = availableSize.ResolveSize(size).ToSectorSize(); var partitionSectors = partitionSize / 512; OnDebugMessage("Reading Master Boot Record"); diff --git a/src/Hst.Imager.Core/Commands/OptimizeCommand.cs b/src/Hst.Imager.Core/Commands/OptimizeCommand.cs index 3ce09eb..4def50f 100644 --- a/src/Hst.Imager.Core/Commands/OptimizeCommand.cs +++ b/src/Hst.Imager.Core/Commands/OptimizeCommand.cs @@ -6,6 +6,7 @@ using Hst.Core; using Core; using Extensions; + using Hst.Core.Extensions; using Microsoft.Extensions.Logging; using Models; @@ -63,7 +64,7 @@ public override async Task Execute(CancellationToken token) } else if (size.Value != 0) { - optimizedSize = currentSize.ResolveSize(size); + optimizedSize = currentSize.ResolveSize(size).ToSectorSize(); } // return error, if optimized size is zero diff --git a/src/Hst.Imager.Core/Commands/RdbInitCommand.cs b/src/Hst.Imager.Core/Commands/RdbInitCommand.cs index ee79984..239581d 100644 --- a/src/Hst.Imager.Core/Commands/RdbInitCommand.cs +++ b/src/Hst.Imager.Core/Commands/RdbInitCommand.cs @@ -8,6 +8,7 @@ using Amiga.RigidDiskBlocks; using Extensions; using Hst.Core; + using Hst.Core.Extensions; using Microsoft.Extensions.Logging; using Size = Models.Size; @@ -81,7 +82,7 @@ public override async Task Execute(CancellationToken token) var defaultName = media.IsPhysicalDrive ? media.Model : Path.GetFileNameWithoutExtension(media.Model); var diskSize = stream.Length; - var rigidDiskBlockSize = diskSize.ResolveSize(size); + var rigidDiskBlockSize = diskSize.ResolveSize(size).ToSectorSize(); OnDebugMessage($"Disk size '{diskSize.FormatBytes()}' ({diskSize} bytes)"); diff --git a/src/Hst.Imager.Core/Commands/RdbPartAddCommand.cs b/src/Hst.Imager.Core/Commands/RdbPartAddCommand.cs index b5eb083..da5c641 100644 --- a/src/Hst.Imager.Core/Commands/RdbPartAddCommand.cs +++ b/src/Hst.Imager.Core/Commands/RdbPartAddCommand.cs @@ -108,7 +108,7 @@ public override async Task Execute(CancellationToken token) return new Result(new Error($"Partition name '{name}' already exists")); } - var partitionSize = rigidDiskBlock.DiskSize.ResolveSize(size); + var partitionSize = rigidDiskBlock.DiskSize.ResolveSize(size).ToSectorSize(); OnInformationMessage($"- Partition number '{partitionBlocks.Count + 1}'"); OnInformationMessage($"- Name '{name}'"); @@ -186,6 +186,9 @@ public override async Task Execute(CancellationToken token) OnDebugMessage("Writing Rigid Disk Block"); await RigidDiskBlockWriter.WriteBlock(rigidDiskBlock, stream); + stream.Close(); + media.Dispose(); + return new Result(); } } diff --git a/src/Hst.Imager.Core/Extensions/PercentExtensions.cs b/src/Hst.Imager.Core/Extensions/PercentExtensions.cs index 03aaef5..0abce17 100644 --- a/src/Hst.Imager.Core/Extensions/PercentExtensions.cs +++ b/src/Hst.Imager.Core/Extensions/PercentExtensions.cs @@ -4,7 +4,7 @@ public static class PercentExtensions { - public static long Percent(this long value, int percent) + public static long Percent(this long value, double percent) { if (percent < 0 || percent > 100) { @@ -13,5 +13,15 @@ public static long Percent(this long value, int percent) return Convert.ToInt64((double)value / 100 * percent); } + + public static long Percent(this double value, double percent) + { + if (percent < 0 || percent > 100) + { + throw new ArgumentOutOfRangeException(nameof(percent)); + } + + return Convert.ToInt64(value / 100 * percent); + } } } \ No newline at end of file diff --git a/src/Hst.Imager.Core/Extensions/SizeExtensions.cs b/src/Hst.Imager.Core/Extensions/SizeExtensions.cs index ae6d091..7e10393 100644 --- a/src/Hst.Imager.Core/Extensions/SizeExtensions.cs +++ b/src/Hst.Imager.Core/Extensions/SizeExtensions.cs @@ -1,7 +1,6 @@ namespace Hst.Imager.Core.Extensions { using System; - using Hst.Core.Extensions; using Models; public static class SizeExtensions @@ -11,7 +10,7 @@ public static long ResolveSize(this long value, Size size) return size.Unit switch { Unit.Bytes => Convert.ToInt64(size.Value == 0 ? value : size.Value), - Unit.Percent => value.Percent((int)size.Value).ToSectorSize(), + Unit.Percent => value.Percent(size.Value), _ => throw new ArgumentOutOfRangeException($"Invalid size unit '{size.Unit}'") }; } diff --git a/src/Hst.Imager.Core/Hst.Imager.Core.csproj b/src/Hst.Imager.Core/Hst.Imager.Core.csproj index 81a66e8..b7bee34 100644 --- a/src/Hst.Imager.Core/Hst.Imager.Core.csproj +++ b/src/Hst.Imager.Core/Hst.Imager.Core.csproj @@ -5,7 +5,7 @@ - +