Skip to content

Commit

Permalink
Update hst amiga package to fix updating rigid disk block pointers. F…
Browse files Browse the repository at this point in the history
…ix use of percent for size of disk and partition. Moved size to sector size to relevant commands.
  • Loading branch information
henrikstengaard committed Dec 6, 2022
1 parent 9c8f48d commit 2215133
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/Hst.Imager.ConsoleApp/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Hst.Imager.Core/Commands/MbrPartAddCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public override async Task<Result> 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");
Expand Down
3 changes: 2 additions & 1 deletion src/Hst.Imager.Core/Commands/OptimizeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Hst.Core;
using Core;
using Extensions;
using Hst.Core.Extensions;
using Microsoft.Extensions.Logging;
using Models;

Expand Down Expand Up @@ -63,7 +64,7 @@ public override async Task<Result> Execute(CancellationToken token)
}
else if (size.Value != 0)
{
optimizedSize = currentSize.ResolveSize(size);
optimizedSize = currentSize.ResolveSize(size).ToSectorSize();
}

// return error, if optimized size is zero
Expand Down
3 changes: 2 additions & 1 deletion src/Hst.Imager.Core/Commands/RdbInitCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Amiga.RigidDiskBlocks;
using Extensions;
using Hst.Core;
using Hst.Core.Extensions;
using Microsoft.Extensions.Logging;
using Size = Models.Size;

Expand Down Expand Up @@ -81,7 +82,7 @@ public override async Task<Result> 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)");

Expand Down
5 changes: 4 additions & 1 deletion src/Hst.Imager.Core/Commands/RdbPartAddCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public override async Task<Result> 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}'");
Expand Down Expand Up @@ -186,6 +186,9 @@ public override async Task<Result> Execute(CancellationToken token)
OnDebugMessage("Writing Rigid Disk Block");
await RigidDiskBlockWriter.WriteBlock(rigidDiskBlock, stream);

stream.Close();
media.Dispose();

return new Result();
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/Hst.Imager.Core/Extensions/PercentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
}
}
}
3 changes: 1 addition & 2 deletions src/Hst.Imager.Core/Extensions/SizeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Hst.Imager.Core.Extensions
{
using System;
using Hst.Core.Extensions;
using Models;

public static class SizeExtensions
Expand All @@ -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}'")
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Hst.Imager.Core/Hst.Imager.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<PackageReference Include="DiscUtils.Containers" Version="0.16.15" />
<PackageReference Include="DiscUtils.FileSystems" Version="0.16.15" />
<PackageReference Include="DiscUtils.Vhd" Version="0.16.15" />
<PackageReference Include="Hst.Amiga" Version="0.3.63" />
<PackageReference Include="Hst.Amiga" Version="0.3.65" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="plist-cil" Version="2.2.0" />
</ItemGroup>
Expand Down

0 comments on commit 2215133

Please sign in to comment.