Skip to content

Commit

Permalink
Fix boot priority option allowing negative values for rdb add and upd…
Browse files Browse the repository at this point in the history
…ate commands. Fix checking if image file exists for commands making changes
  • Loading branch information
henrikstengaard committed Dec 4, 2022
1 parent 0730f40 commit 9c8f48d
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 75 deletions.
4 changes: 2 additions & 2 deletions src/Hst.Imager.ConsoleApp/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ await Execute(new RdbFsUpdateCommand(GetLogger<RdbFsUpdateCommand>(), GetCommand
}

public static async Task RdbPartAdd(string path, string name, string dosType, string size, uint? reserved,
uint? preAlloc, uint? buffers, string maxTransfer, string mask, bool noMount, bool bootable, int priority,
int fileSystemBlockSize)
uint? preAlloc, uint? buffers, string maxTransfer, string mask, bool noMount, bool bootable, int? priority,
int? fileSystemBlockSize)
{
await Execute(new RdbPartAddCommand(GetLogger<RdbPartAddCommand>(), GetCommandHelper(),
await GetPhysicalDrives(), path, name, dosType, ParseSize(size), reserved, preAlloc, buffers,
Expand Down
21 changes: 10 additions & 11 deletions src/Hst.Imager.ConsoleApp/RdbCommandFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,11 @@ private static Command CreateRdbPartAdd()
description: "Set bootable.",
getDefaultValue: () => false);

var priorityOption = new Option<int>(
var bootPriorityOption = new Option<int?>(
new[] { "--boot-priority", "-bp" },
description: "Set boot priority.",
getDefaultValue: () => 0);
description: "Set boot priority.");

var blockSizeOption = new Option<int>(
var blockSizeOption = new Option<int?>(
new[] { "--block-size", "-bs" },
description: "Block size for the partition.",
getDefaultValue: () => 512);
Expand All @@ -300,11 +299,11 @@ private static Command CreateRdbPartAdd()
var mask = context.ParseResult.GetValueForOption(maskOption);
var noMount = context.ParseResult.GetValueForOption(noMountOption);
var bootable = context.ParseResult.GetValueForOption(bootableOption);
var priority = context.ParseResult.GetValueForOption(priorityOption);
var bootPriority = context.ParseResult.GetValueForOption(bootPriorityOption);
var blockSize = context.ParseResult.GetValueForOption(blockSizeOption);

await CommandHandler.RdbPartAdd(path, name, dosType, size, reserved, preAlloc, buffers, maxTransfer, mask,
noMount, bootable, priority, blockSize);
noMount, bootable, bootPriority, blockSize);
});

rdbPartAddCommand.AddArgument(pathArgument);
Expand All @@ -318,7 +317,7 @@ await CommandHandler.RdbPartAdd(path, name, dosType, size, reserved, preAlloc, b
rdbPartAddCommand.AddOption(maskOption);
rdbPartAddCommand.AddOption(noMountOption);
rdbPartAddCommand.AddOption(bootableOption);
rdbPartAddCommand.AddOption(priorityOption);
rdbPartAddCommand.AddOption(bootPriorityOption);
rdbPartAddCommand.AddOption(blockSizeOption);

return rdbPartAddCommand;
Expand Down Expand Up @@ -370,7 +369,7 @@ private static Command CreateRdbPartUpdate()
new[] { "--bootable", "-b" },
description: "Set bootable for partition.");

var priorityOption = new Option<int?>(
var bootPriorityOption = new Option<int?>(
new[] { "--boot-priority", "-bp" },
description: "Set boot priority (controls order of partitions to boot, lowest is booted first).");

Expand All @@ -392,13 +391,13 @@ private static Command CreateRdbPartUpdate()
var mask = context.ParseResult.GetValueForOption(maskOption);
var noMount = context.ParseResult.GetValueForOption(noMountOption);
var bootable = context.ParseResult.GetValueForOption(bootableOption);
var priority = context.ParseResult.GetValueForOption(priorityOption);
var bootPriority = context.ParseResult.GetValueForOption(bootPriorityOption);
var fileSystemBlockSize = context.ParseResult.GetValueForOption(fileSystemBlockSizeOption);

await CommandHandler.RdbPartUpdate(path, partitionNumber, name, dosType, reserved, preAlloc, buffers,
maxTransfer,
mask, noMount.HasValue ? noMount.Value == BoolType.True : null,
bootable.HasValue ? bootable.Value == BoolType.True : null, priority, fileSystemBlockSize);
bootable.HasValue ? bootable.Value == BoolType.True : null, bootPriority, fileSystemBlockSize);
});

command.AddArgument(pathArgument);
Expand All @@ -412,7 +411,7 @@ await CommandHandler.RdbPartUpdate(path, partitionNumber, name, dosType, reserve
command.AddOption(maskOption);
command.AddOption(noMountOption);
command.AddOption(bootableOption);
command.AddOption(priorityOption);
command.AddOption(bootPriorityOption);
command.AddOption(fileSystemBlockSizeOption);

return command;
Expand Down
33 changes: 7 additions & 26 deletions src/Hst.Imager.Core.Tests/FakeCommandHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Hst.Amiga.RigidDiskBlocks;
using Amiga.RigidDiskBlocks;
using Hst.Core;
using Hst.Imager.Core;
using Hst.Imager.Core.Commands;
using Hst.Imager.Core.Models;
using Core;
using Commands;
using Models;

public class FakeCommandHelper : CommandHelper
{
Expand Down Expand Up @@ -77,31 +77,12 @@ public override Result<Media> GetReadableMedia(IEnumerable<IPhysicalDrive> physi
}

public override Result<Media> GetWritableMedia(IEnumerable<IPhysicalDrive> physicalDrives, string path, long? size = null,
bool allowPhysicalDrive = true)
bool allowPhysicalDrive = true, bool create = false)
{
return path.EndsWith(".img", StringComparison.OrdinalIgnoreCase)
? new Result<Media>(WriteableMedias.FirstOrDefault(x => x.Path.Equals(path, StringComparison.OrdinalIgnoreCase)))
: base.GetWritableMedia(physicalDrives, path, size, allowPhysicalDrive);
: base.GetWritableMedia(physicalDrives, path, size, allowPhysicalDrive, create);
}

// public void CreateWritableMedia(string path, long size)
// {
// var mediaResult = GetWritableMedia(new List<IPhysicalDrive>(), path, size, false);
// using var media = mediaResult.Value;
// }
//
// public async Task AppendWriteableMediaData(string path, long size, byte[] data = null)
// {
// var destinationMediaResult = GetWritableMedia(new List<IPhysicalDrive>(), path,
// size, false);
// var destinationStream = destinationMediaResult.Value.Stream;
// if (data == null)
// {
// return;
// }
//
// await destinationStream.WriteAsync(data, 0, data.Length);
// }

public async Task AppendWriteableMediaDataVhd(string path, long size, byte[] data = null)
{
Expand All @@ -111,7 +92,7 @@ public async Task AppendWriteableMediaDataVhd(string path, long size, byte[] dat
}

var destinationMediaResult = GetWritableMedia(new List<IPhysicalDrive>(), path,
size, false);
size, false, true);
using var destinationMedia = destinationMediaResult.Value;
await using var destinationStream = destinationMedia.Stream;
if (data == null)
Expand Down
6 changes: 3 additions & 3 deletions src/Hst.Imager.Core.Tests/GivenConvertCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Hst.Imager.Core;
using Hst.Imager.Core.Commands;
using Hst.Imager.Core.Models;
using Core;
using Commands;
using Models;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;

Expand Down
2 changes: 1 addition & 1 deletion src/Hst.Imager.Core/Commands/BlankCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public override async Task<Result> Execute(CancellationToken token)

OnDebugMessage($"Opening '{path}' as writable");

var mediaResult = commandHelper.GetWritableMedia(Enumerable.Empty<IPhysicalDrive>(), path, mediaSize, false);
var mediaResult = commandHelper.GetWritableMedia(Enumerable.Empty<IPhysicalDrive>(), path, mediaSize, false, true);
if (mediaResult.IsFaulted)
{
return new Result(mediaResult.Error);
Expand Down
45 changes: 28 additions & 17 deletions src/Hst.Imager.Core/Commands/CommandHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public virtual Stream CreateWriteableStream(string path)
}

public virtual Result<Media> GetWritableMedia(IEnumerable<IPhysicalDrive> physicalDrives, string path,
long? size = null, bool allowPhysicalDrive = true)
long? size = null, bool allowPhysicalDrive = true, bool create = false)
{
var physicalDrive =
physicalDrives.FirstOrDefault(x => x.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
Expand Down Expand Up @@ -110,29 +110,40 @@ public virtual Result<Media> GetWritableMedia(IEnumerable<IPhysicalDrive> physic

var model = Path.GetFileName(path);

if (!IsVhd(path))
if (create)
{
return new Result<Media>(new Media(path, model, 0, Media.MediaType.Raw, false,
CreateWriteableStream(path)));
}
if (size == null || size.Value == 0)
{
throw new ArgumentNullException(nameof(size), "Size is required for creating image file");
}

if (File.Exists(path))
if (!IsVhd(path))
{
return new Result<Media>(new Media(path, model, 0, Media.MediaType.Raw, false,
CreateWriteableStream(path)));
}

var stream = CreateWriteableStream(path);
var newVhdDisk = Disk.InitializeDynamic(stream, Ownership.None, GetVhdSize(size.Value));
return new Result<Media>(new VhdMedia(path, model, newVhdDisk.Capacity, Media.MediaType.Vhd, false,
newVhdDisk, stream));
}

if (!File.Exists(path))
{
var vhdDisk = VirtualDisk.OpenDisk(path, FileAccess.ReadWrite);
vhdDisk.Content.Position = 0;
return new Result<Media>(new VhdMedia(path, model, vhdDisk.Capacity, Media.MediaType.Vhd, false,
vhdDisk));
return new Result<Media>(new PathNotFoundError($"Path '{path}' not found", nameof(path)));
}

if (size == null || size.Value == 0)
if (!IsVhd(path))
{
throw new ArgumentNullException(nameof(size), "Size required for vhd");
return new Result<Media>(new Media(path, model, 0, Media.MediaType.Raw, false,
CreateWriteableStream(path)));
}

var stream = CreateWriteableStream(path);
var newVhdDisk = Disk.InitializeDynamic(stream, Ownership.None, GetVhdSize(size.Value));
return new Result<Media>(new VhdMedia(path, model, newVhdDisk.Capacity, Media.MediaType.Vhd, false,
newVhdDisk, stream));
var vhdDisk = VirtualDisk.OpenDisk(path, FileAccess.ReadWrite);
vhdDisk.Content.Position = 0;
return new Result<Media>(new VhdMedia(path, model, vhdDisk.Capacity, Media.MediaType.Vhd, false,
vhdDisk));
}

public virtual long GetVhdSize(long size)
Expand Down
2 changes: 1 addition & 1 deletion src/Hst.Imager.Core/Commands/ConvertCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override async Task<Result> Execute(CancellationToken token)
OnDebugMessage($"Opening '{destinationPath}' as writable");

var destinationMediaResult =
commandHelper.GetWritableMedia(Enumerable.Empty<IPhysicalDrive>(), destinationPath, convertSize, false);
commandHelper.GetWritableMedia(Enumerable.Empty<IPhysicalDrive>(), destinationPath, convertSize, false, true);
if (destinationMediaResult.IsFaulted)
{
return new Result(destinationMediaResult.Error);
Expand Down
2 changes: 1 addition & 1 deletion src/Hst.Imager.Core/Commands/ICommandHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface ICommandHelper
{
Result<Media> GetReadableMedia(IEnumerable<IPhysicalDrive> physicalDrives, string path, bool allowPhysicalDrive = true);
Result<Media> GetWritableMedia(IEnumerable<IPhysicalDrive> physicalDrives, string path, long? size = null,
bool allowPhysicalDrive = true);
bool allowPhysicalDrive = true, bool create = false);
long GetVhdSize(long size);
bool IsVhd(string path);
Task<RigidDiskBlock> GetRigidDiskBlock(Stream stream);
Expand Down
21 changes: 15 additions & 6 deletions src/Hst.Imager.Core/Commands/RdbPartAddCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ public class RdbPartAddCommand : CommandBase
private readonly string dosType;
private readonly bool noMount;
private readonly bool bootable;
private readonly int priority;
private readonly int fileSystemBlockSize;
private readonly int? bootPriority;
private readonly int? fileSystemBlockSize;

public RdbPartAddCommand(ILogger<RdbPartAddCommand> logger, ICommandHelper commandHelper,
IEnumerable<IPhysicalDrive> physicalDrives, string path, string name, string dosType, Size size,
uint? reserved, uint? preAlloc, uint? buffers, uint? maxTransfer, uint? mask, bool noMount, bool bootable, int priority, int fileSystemBlockSize)
uint? reserved, uint? preAlloc, uint? buffers, uint? maxTransfer, uint? mask, bool noMount, bool bootable,
int? bootPriority, int? fileSystemBlockSize)
{
this.logger = logger;
this.commandHelper = commandHelper;
Expand All @@ -52,7 +53,7 @@ public RdbPartAddCommand(ILogger<RdbPartAddCommand> logger, ICommandHelper comma
this.dosType = dosType;
this.noMount = noMount;
this.bootable = bootable;
this.priority = priority;
this.bootPriority = bootPriority;
this.fileSystemBlockSize = fileSystemBlockSize;
}

Expand Down Expand Up @@ -114,7 +115,7 @@ public override async Task<Result> Execute(CancellationToken token)
OnInformationMessage($"- DOS type '{dosTypeBytes.FormatDosType()}'");

var partitionBlock =
PartitionBlock.Create(rigidDiskBlock, dosTypeBytes, name, partitionSize, fileSystemBlockSize,
PartitionBlock.Create(rigidDiskBlock, dosTypeBytes, name, partitionSize, fileSystemBlockSize ?? 512,
bootable);

if (partitionBlock.HighCyl - partitionBlock.LowCyl + 1 <= 0)
Expand Down Expand Up @@ -159,7 +160,12 @@ public override async Task<Result> Execute(CancellationToken token)
}

partitionBlock.Flags = flags;
partitionBlock.BootPriority = (uint)priority;

if (bootPriority.HasValue)
{
partitionBlock.BootPriority = bootPriority.Value;
}

partitionBlock.SizeBlock = rigidDiskBlock.BlockSize / SizeOf.ULong;

OnInformationMessage($"- Size '{partitionBlock.PartitionSize.FormatBytes()}' ({partitionBlock.PartitionSize} bytes)");
Expand All @@ -170,6 +176,9 @@ public override async Task<Result> Execute(CancellationToken token)
OnInformationMessage($"- Buffers '{partitionBlock.NumBuffer}'");
OnInformationMessage($"- Max Transfer '0x{partitionBlock.MaxTransfer.FormatHex().ToUpper()}' ({partitionBlock.MaxTransfer})");
OnInformationMessage($"- Mask '0x{partitionBlock.Mask.FormatHex().ToUpper()}' ({partitionBlock.Mask})");
OnInformationMessage($"- Bootable '{partitionBlock.Bootable.ToString()}'");
OnInformationMessage($"- Boot priority '{partitionBlock.BootPriority}'");
OnInformationMessage($"- No mount '{partitionBlock.NoMount}'");
OnInformationMessage($"- File System Block Size '{partitionBlock.Sectors * rigidDiskBlock.BlockSize}'");

rigidDiskBlock.PartitionBlocks = rigidDiskBlock.PartitionBlocks.Concat(new[] { partitionBlock });
Expand Down
6 changes: 3 additions & 3 deletions src/Hst.Imager.Core/Commands/RdbPartUpdateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class RdbPartUpdateCommand : CommandBase

public RdbPartUpdateCommand(ILogger<RdbPartUpdateCommand> logger, ICommandHelper commandHelper,
IEnumerable<IPhysicalDrive> physicalDrives, string path, int partitionNumber, string name, string dosType,
int? reserved,
int? preAlloc, int? buffers, uint? maxTransfer, uint? mask, bool? noMount, bool? bootable, int? bootPriority, int? fileSystemBlockSize)
int? reserved, int? preAlloc, int? buffers, uint? maxTransfer, uint? mask, bool? noMount, bool? bootable,
int? bootPriority, int? fileSystemBlockSize)
{
this.logger = logger;
this.commandHelper = commandHelper;
Expand Down Expand Up @@ -218,7 +218,7 @@ public override async Task<Result> Execute(CancellationToken token)
// update boot priority
if (bootPriority.HasValue)
{
partitionBlock.BootPriority = (uint)bootPriority;
partitionBlock.BootPriority = bootPriority.Value;
hasChanges = true;
OnInformationMessage($"Boot priority '{partitionBlock.BootPriority}'");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Hst.Imager.Core/Commands/ReadCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public override async Task<Result> Execute(CancellationToken token)

OnDebugMessage($"Opening '{destinationPath}' as writable");

var destinationMediaResult = commandHelper.GetWritableMedia(physicalDrivesList, destinationPath, readSize, false);
var destinationMediaResult = commandHelper.GetWritableMedia(physicalDrivesList, destinationPath, readSize, false, true);
if (destinationMediaResult.IsFaulted)
{
return new Result(destinationMediaResult.Error);
Expand Down
2 changes: 1 addition & 1 deletion src/Hst.Imager.Core/Commands/WriteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override async Task<Result> Execute(CancellationToken token)

OnDebugMessage($"Opening '{destinationPath}' as writable");

var destinationMediaResult = commandHelper.GetWritableMedia(physicalDrivesList, destinationPath, writeSize);
var destinationMediaResult = commandHelper.GetWritableMedia(physicalDrivesList, destinationPath, writeSize, true, true);
if (destinationMediaResult.IsFaulted)
{
return new Result(destinationMediaResult.Error);
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.61" />
<PackageReference Include="Hst.Amiga" Version="0.3.63" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="plist-cil" Version="2.2.0" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Hst.Imager.GuiApp/Models/PartitionBlockViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class PartitionBlockViewModel
public uint Mask { get; set; }
public string MaskHex { get; set; }

public uint BootPriority { get; set; }
public int BootPriority { get; set; }
public byte[] DosType { get; set; }
public string DosTypeFormatted { get; set; }
public string DosTypeHex { get; set; }
Expand Down

0 comments on commit 9c8f48d

Please sign in to comment.