Skip to content

Commit

Permalink
Add mask option to rdb part add and update commands
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikstengaard committed Dec 2, 2022
1 parent 12a2b93 commit b2a277e
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 67 deletions.
41 changes: 35 additions & 6 deletions src/Hst.Imager.ConsoleApp/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,34 @@ private static async Task<IEnumerable<IPhysicalDrive>> GetPhysicalDrives()
return (await physicalDriveManager.GetPhysicalDrives()).ToList();
}

private static readonly Regex IntRegex =
new("^(\\d+)$", RegexOptions.Compiled | RegexOptions.IgnoreCase);

private static readonly Regex HexRegex =
new("^0x([\\da-f])+$", RegexOptions.Compiled | RegexOptions.IgnoreCase);

private static uint? ParseHexOrIntegerValue(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return null;
}

if (HexRegex.IsMatch(value))
{
return System.Convert.ToUInt32(value, 16);
}

if (IntRegex.IsMatch(value) && uint.TryParse(value, out var uintValue))
{
return uintValue;
}

throw new ArgumentException("Invalid hex or integer value", nameof(value));
}

private static readonly Regex SizeUnitRegex =
new("(\\d+)([\\.]{1}\\d+)?(kb|mb|gb|%)?$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
new("^(\\d+)([\\.]{1}\\d+)?(kb|mb|gb|%)?$", RegexOptions.Compiled | RegexOptions.IgnoreCase);

private static Size ParseSize(string size)
{
Expand Down Expand Up @@ -291,22 +317,25 @@ await Execute(new RdbFsUpdateCommand(GetLogger<RdbFsUpdateCommand>(), GetCommand
await GetPhysicalDrives(), path, fileSystemNumber, dosType, fileSystemName, fileSystemPath));
}

public static async Task RdbPartAdd(string path, string name, string dosType, string size, int reserved,
int preAlloc, int buffers, int maxTransfer, bool noMount, bool bootable, int priority, int fileSystemBlockSize)
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)
{
await Execute(new RdbPartAddCommand(GetLogger<RdbPartAddCommand>(), GetCommandHelper(),
await GetPhysicalDrives(), path, name, dosType, ParseSize(size), reserved, preAlloc, buffers,
maxTransfer, noMount, bootable, priority, fileSystemBlockSize));
ParseHexOrIntegerValue(maxTransfer), ParseHexOrIntegerValue(mask), noMount, bootable, priority,
fileSystemBlockSize));
}

public static async Task RdbPartUpdate(string path, int partitionNumber, string name, string dosType,
int? reserved,
int? preAlloc, int? buffers, uint? maxTransfer, uint? mask, bool? noMount, bool? bootable,
int? preAlloc, int? buffers, string maxTransfer, string mask, bool? noMount, bool? bootable,
int? bootPriority, int? fileSystemBlockSize)
{
await Execute(new RdbPartUpdateCommand(GetLogger<RdbPartUpdateCommand>(), GetCommandHelper(),
await GetPhysicalDrives(), path, partitionNumber, name, dosType, reserved, preAlloc, buffers,
maxTransfer, mask, noMount, bootable, bootPriority, fileSystemBlockSize));
ParseHexOrIntegerValue(maxTransfer), ParseHexOrIntegerValue(mask), noMount, bootable, bootPriority,
fileSystemBlockSize));
}

public static async Task RdbPartDel(string path, int partitionNumber)
Expand Down
10 changes: 6 additions & 4 deletions src/Hst.Imager.ConsoleApp/Presenters/RdbInfoPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
{
using System.Linq;
using System.Text;
using Amiga.Extensions;
using Core.Commands;
using Core.Extensions;
using Hst.Core.Extensions;

public static class RigidDiskBlockPresenter
{
Expand Down Expand Up @@ -82,7 +84,7 @@ public static string Present(RdbInfo rdbInfo)
Columns = new[]
{
(++fileSystemNumber).ToString(),
$"0x{x.DosType.FormatHex().ToUpper()}, {x.DosTypeFormatted}",
$"0x{x.DosType.FormatHex().ToUpper()} ({x.DosType.FormatDosType()})",
x.VersionFormatted,
x.FileSystemName ?? string.Empty,
((long)x.FileSystemSize).FormatBytes()
Expand Down Expand Up @@ -128,9 +130,9 @@ public static string Present(RdbInfo rdbInfo)
x.DriveName, x.PartitionSize.FormatBytes(), x.LowCyl.ToString(), x.HighCyl.ToString(),
x.Reserved.ToString(), x.PreAlloc.ToString(), x.FileSystemBlockSize.ToString(),
x.NumBuffer.ToString(),
$"0x{x.DosType.FormatHex().ToUpper()}, {x.DosTypeFormatted}",
$"0x{x.MaxTransfer.FormatHex().ToUpper()}, {x.MaxTransfer}",
$"0x{x.Mask.FormatHex().ToUpper()}, {x.Mask}",
$"0x{x.DosType.FormatHex().ToUpper()} ({x.DosTypeFormatted})",
$"0x{x.MaxTransfer.FormatHex().ToUpper()} ({x.MaxTransfer})",
$"0x{x.Mask.FormatHex().ToUpper()} ({x.Mask})",
x.Bootable.ToString(), x.NoMount.ToString(),
x.BootPriority.ToString()
}
Expand Down
33 changes: 18 additions & 15 deletions src/Hst.Imager.ConsoleApp/RdbCommandFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,25 +246,25 @@ private static Command CreateRdbPartAdd()
name: "Size",
description: "Size of the partition.");

var reservedOption = new Option<int>(
var reservedOption = new Option<uint?>(
new[] { "--reserved", "-r" },
description: "Set reserved blocks at start of partition.",
getDefaultValue: () => 2);
description: "Set reserved blocks at start of partition.");

var preAllocOption = new Option<int>(
var preAllocOption = new Option<uint?>(
new[] { "--pre-alloc", "-pa" },
description: "Set reserved blocks at end of partition",
getDefaultValue: () => 5);
description: "Set reserved blocks at end of partition");

var buffersOption = new Option<int>(
var buffersOption = new Option<uint?>(
new[] { "--buffers", "-bu" },
description: "Set buffers",
getDefaultValue: () => 30);
description: "Set buffers");

var maxTransferOption = new Option<int>(
var maxTransferOption = new Option<string>(
new[] { "--max-transfer", "-mt" },
description: "Set buffers",
getDefaultValue: () => 130560);
description: "Set buffers");

var maskOption = new Option<string>(
new[] { "--mask", "-ma" },
description: "Mask");

var noMountOption = new Option<bool>(
new[] { "--no-mount", "-nm" },
Expand Down Expand Up @@ -297,12 +297,13 @@ private static Command CreateRdbPartAdd()
var preAlloc = context.ParseResult.GetValueForOption(preAllocOption);
var buffers = context.ParseResult.GetValueForOption(buffersOption);
var maxTransfer = context.ParseResult.GetValueForOption(maxTransferOption);
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 blockSize = context.ParseResult.GetValueForOption(blockSizeOption);

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

Expand All @@ -314,6 +315,7 @@ await CommandHandler.RdbPartAdd(path, name, dosType, size, reserved, preAlloc, b
rdbPartAddCommand.AddOption(preAllocOption);
rdbPartAddCommand.AddOption(buffersOption);
rdbPartAddCommand.AddOption(maxTransferOption);
rdbPartAddCommand.AddOption(maskOption);
rdbPartAddCommand.AddOption(noMountOption);
rdbPartAddCommand.AddOption(bootableOption);
rdbPartAddCommand.AddOption(priorityOption);
Expand Down Expand Up @@ -352,11 +354,11 @@ private static Command CreateRdbPartUpdate()
new[] { "--buffers", "-bu" },
description: "Buffers");

var maxTransferOption = new Option<uint?>(
var maxTransferOption = new Option<string>(
new[] { "--max-transfer", "-mt" },
description: "Max transfer");

var maskOption = new Option<uint?>(
var maskOption = new Option<string>(
new[] { "--mask", "-ma" },
description: "Mask");

Expand Down Expand Up @@ -407,6 +409,7 @@ await CommandHandler.RdbPartUpdate(path, partitionNumber, name, dosType, reserve
command.AddOption(preAllocOption);
command.AddOption(buffersOption);
command.AddOption(maxTransferOption);
command.AddOption(maskOption);
command.AddOption(noMountOption);
command.AddOption(bootableOption);
command.AddOption(priorityOption);
Expand Down
2 changes: 1 addition & 1 deletion src/Hst.Imager.Core/Commands/RdbFsAddCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public override async Task<Result> Execute(CancellationToken token)
long size = fileSystemHeaderBlock.FileSystemSize;

OnInformationMessage("Adding file system:");
OnInformationMessage($"- DOS type '{fileSystemHeaderBlock.DosType.FormatDosType()}'");
OnInformationMessage($"- DOS type '0x{fileSystemHeaderBlock.DosType.FormatHex()}' ({fileSystemHeaderBlock.DosType.FormatDosType()})");
OnInformationMessage($"- Version '{fileSystemHeaderBlock.VersionFormatted}'");
OnInformationMessage($"- Size '{size.FormatBytes()}' ({size} bytes)");
OnInformationMessage($"- File system name '{fileSystemHeaderBlock.FileSystemName}'");
Expand Down
3 changes: 2 additions & 1 deletion src/Hst.Imager.Core/Commands/RdbFsExportCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Amiga.Extensions;
using Extensions;
using Hst.Core;
using Hst.Core.Extensions;
using Microsoft.Extensions.Logging;

public class RdbFsExportCommand : CommandBase
Expand Down Expand Up @@ -67,7 +68,7 @@ public override async Task<Result> Execute(CancellationToken token)
var fileSystemBytes = fileSystemHeaderBlock.LoadSegBlocks.SelectMany(x => x.Data).ToArray();
long fileSystemSize = fileSystemBytes.Length;

OnDebugMessage($"DOS type '{fileSystemHeaderBlock.DosType.FormatDosType()}' {fileSystemSize.FormatBytes()} ({fileSystemSize} bytes)");
OnDebugMessage($"DOS type '0x{fileSystemHeaderBlock.DosType.FormatHex()}' ({fileSystemHeaderBlock.DosType.FormatDosType()})");
OnDebugMessage($"Version '{fileSystemHeaderBlock.VersionFormatted}'");
OnDebugMessage($"Size '{fileSystemSize.FormatBytes()}' ({fileSystemSize} bytes)");
OnDebugMessage($"File system name '{fileSystemHeaderBlock.FileSystemName}'");
Expand Down
2 changes: 1 addition & 1 deletion src/Hst.Imager.Core/Commands/RdbFsImportCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public override async Task<Result> Execute(CancellationToken token)
{
long size = fileSystemHeaderBlock.FileSystemSize;
OnInformationMessage("Imported file system:");
OnInformationMessage($"- DOS type '{fileSystemHeaderBlock.DosType.FormatDosType()}'");
OnInformationMessage($"- DOS type '0x{fileSystemHeaderBlock.DosType.FormatHex()}' ({fileSystemHeaderBlock.DosType.FormatDosType()})");
OnInformationMessage($"- Version '{fileSystemHeaderBlock.VersionFormatted}'");
OnInformationMessage($"- Size '{size.FormatBytes()}' ({size} bytes)");
OnInformationMessage($"- File system name '{fileSystemHeaderBlock.FileSystemName}'");
Expand Down
2 changes: 1 addition & 1 deletion src/Hst.Imager.Core/Commands/RdbFsUpdateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public override async Task<Result> Execute(CancellationToken token)

fileSystemHeaderBlock.DosType = dosTypeBytes;

OnInformationMessage($"DOS type '{fileSystemHeaderBlock.DosType.FormatDosType()}'");
OnInformationMessage($"DOS type '0x{fileSystemHeaderBlock.DosType.FormatHex()}' ({fileSystemHeaderBlock.DosType.FormatDosType()})");
}

if (!string.IsNullOrEmpty(fileSystemName))
Expand Down
45 changes: 35 additions & 10 deletions src/Hst.Imager.Core/Commands/RdbPartAddCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Amiga;
using Amiga.RigidDiskBlocks;
using Hst.Core;
using Hst.Core.Extensions;
using Microsoft.Extensions.Logging;
using Size = Models.Size;
using PartitionBlock = Amiga.RigidDiskBlocks.PartitionBlock;
Expand All @@ -22,10 +23,11 @@ public class RdbPartAddCommand : CommandBase
private readonly string path;
private readonly string name;
private readonly Size size;
private readonly int reserved;
private readonly int preAlloc;
private readonly int buffers;
private readonly int maxTransfer;
private readonly uint? reserved;
private readonly uint? preAlloc;
private readonly uint? buffers;
private readonly uint? maxTransfer;
private readonly uint? mask;
private readonly string dosType;
private readonly bool noMount;
private readonly bool bootable;
Expand All @@ -34,7 +36,7 @@ public class RdbPartAddCommand : CommandBase

public RdbPartAddCommand(ILogger<RdbPartAddCommand> logger, ICommandHelper commandHelper,
IEnumerable<IPhysicalDrive> physicalDrives, string path, string name, string dosType, Size size,
int reserved, int preAlloc, int buffers, int maxTransfer, bool noMount, bool bootable, int priority, int fileSystemBlockSize)
uint? reserved, uint? preAlloc, uint? buffers, uint? maxTransfer, uint? mask, bool noMount, bool bootable, int priority, int fileSystemBlockSize)
{
this.logger = logger;
this.commandHelper = commandHelper;
Expand All @@ -46,6 +48,7 @@ public RdbPartAddCommand(ILogger<RdbPartAddCommand> logger, ICommandHelper comma
this.preAlloc = preAlloc;
this.buffers = buffers;
this.maxTransfer = maxTransfer;
this.mask = mask;
this.dosType = dosType;
this.noMount = noMount;
this.bootable = bootable;
Expand Down Expand Up @@ -130,10 +133,31 @@ public override async Task<Result> Execute(CancellationToken token)
flags += (int)PartitionBlock.PartitionFlagsEnum.NoMount;
}

partitionBlock.Reserved = (uint)reserved;
partitionBlock.PreAlloc = (uint)preAlloc;
partitionBlock.MaxTransfer = (uint)maxTransfer;
partitionBlock.NumBuffer = (uint)buffers;
if (reserved.HasValue)
{
partitionBlock.Reserved = reserved.Value;
}

if (preAlloc.HasValue)
{
partitionBlock.PreAlloc = preAlloc.Value;
}

if (maxTransfer.HasValue)
{
partitionBlock.MaxTransfer = maxTransfer.Value;
}

if (mask.HasValue)
{
partitionBlock.Mask = mask.Value;
}

if (buffers.HasValue)
{
partitionBlock.NumBuffer = buffers.Value;
}

partitionBlock.Flags = flags;
partitionBlock.BootPriority = (uint)priority;
partitionBlock.SizeBlock = rigidDiskBlock.BlockSize / SizeOf.ULong;
Expand All @@ -144,7 +168,8 @@ public override async Task<Result> Execute(CancellationToken token)
OnInformationMessage($"- Reserved '{partitionBlock.Reserved}'");
OnInformationMessage($"- PreAlloc '{partitionBlock.PreAlloc}'");
OnInformationMessage($"- Buffers '{partitionBlock.NumBuffer}'");
OnInformationMessage($"- Max Transfer '{partitionBlock.MaxTransfer}'");
OnInformationMessage($"- Max Transfer '0x{partitionBlock.MaxTransfer.FormatHex().ToUpper()}' ({partitionBlock.MaxTransfer})");
OnInformationMessage($"- Mask '0x{partitionBlock.Mask.FormatHex().ToUpper()}' ({partitionBlock.Mask})");
OnInformationMessage($"- File System Block Size '{partitionBlock.Sectors * rigidDiskBlock.BlockSize}'");

rigidDiskBlock.PartitionBlocks = rigidDiskBlock.PartitionBlocks.Concat(new[] { partitionBlock });
Expand Down
4 changes: 3 additions & 1 deletion src/Hst.Imager.Core/Commands/RdbPartFormatCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Amiga.Extensions;
using Amiga.FileSystems.FastFileSystem;
using Amiga.FileSystems.Pfs3;
using Hst.Core;
using Hst.Core.Extensions;
using Microsoft.Extensions.Logging;

public class RdbPartFormatCommand : CommandBase
Expand Down Expand Up @@ -65,7 +67,7 @@ public override async Task<Result> Execute(CancellationToken token)
var partitionBlock = partitionBlocks[partitionNumber - 1];

OnInformationMessage($"- Name '{partitionBlock.DriveName}'");
OnInformationMessage($"- DOS type '{partitionBlock.DosTypeFormatted}'");
OnInformationMessage($"- DOS type '0x{partitionBlock.DosType.FormatHex()}' ({partitionBlock.DosType.FormatDosType()})");
OnInformationMessage($"- Volume name '{name}'");

switch (partitionBlock.DosTypeFormatted)
Expand Down
Loading

0 comments on commit b2a277e

Please sign in to comment.