(feature): improved write error handling

This commit is contained in:
Maksym Sadovnychyy 2024-11-01 04:36:43 -07:00
parent 4d7e11544b
commit b518360409
3 changed files with 60 additions and 4 deletions

View File

@ -153,8 +153,13 @@ public class Application {
Console.WriteLine($"Writing {blocks} zero-filled blocks to tape."); Console.WriteLine($"Writing {blocks} zero-filled blocks to tape.");
Console.WriteLine($"Block Size: {blockSize}."); Console.WriteLine($"Block Size: {blockSize}.");
var writeError = 0;
for (int i = 0; i < blocks; i++) { for (int i = 0; i < blocks; i++) {
handler.WriteData(new byte[blockSize]); writeError = handler.WriteData(new byte[blockSize]);
if (writeError != 0)
return;
Thread.Sleep(_configuration.WriteDelay); Thread.Sleep(_configuration.WriteDelay);
} }
} }
@ -191,6 +196,9 @@ public class Application {
var currentTapeBlock = (descriptorJson.Length + blockSize - 1) / blockSize; var currentTapeBlock = (descriptorJson.Length + blockSize - 1) / blockSize;
int writeError = 0;
foreach (var file in descriptor.Files) { foreach (var file in descriptor.Files) {
var filePath = Path.Combine(directoryPath, file.FilePath); var filePath = Path.Combine(directoryPath, file.FilePath);
using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
@ -204,7 +212,11 @@ public class Application {
// Zero-fill the remaining part of the buffer if the last block is smaller than blockSize // Zero-fill the remaining part of the buffer if the last block is smaller than blockSize
Array.Clear(buffer, bytesRead, buffer.Length - bytesRead); Array.Clear(buffer, bytesRead, buffer.Length - bytesRead);
} }
handler.WriteData(buffer);
writeError = handler.WriteData(buffer);
if (writeError != 0)
return;
currentTapeBlock++; currentTapeBlock++;
Thread.Sleep(_configuration.WriteDelay); // Small delay between blocks Thread.Sleep(_configuration.WriteDelay); // Small delay between blocks
} }
@ -223,7 +235,11 @@ public class Application {
var length = Math.Min(blockSize, descriptorData.Length - startIndex); var length = Math.Min(blockSize, descriptorData.Length - startIndex);
byte[] block = new byte[blockSize]; // Initialized with zeros by default byte[] block = new byte[blockSize]; // Initialized with zeros by default
Array.Copy(descriptorData, startIndex, block, 0, length); Array.Copy(descriptorData, startIndex, block, 0, length);
handler.WriteData(block);
writeError = handler.WriteData(block);
if (writeError != 0)
return;
currentTapeBlock++; currentTapeBlock++;
Thread.Sleep(_configuration.WriteDelay); // Small delay between blocks Thread.Sleep(_configuration.WriteDelay); // Small delay between blocks
} }

View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.LTO.Core;
public class DriverManager {
public static void RestartDriver(string deviceName) {
string script = $@"
$device = Get-PnpDevice -FriendlyName '{deviceName}'
Disable-PnpDevice -InstanceId $device.InstanceId -Confirm:$false
Start-Sleep -Seconds 5
Enable-PnpDevice -InstanceId $device.InstanceId -Confirm:$false
";
ProcessStartInfo psi = new ProcessStartInfo {
FileName = "powershell.exe",
Arguments = $"-NoProfile -ExecutionPolicy Bypass -Command \"{script}\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using (Process process = Process.Start(psi)) {
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
Console.WriteLine(output);
if (!string.IsNullOrEmpty(error)) {
Console.WriteLine($"Error: {error}");
}
}
}
}

View File

@ -82,7 +82,7 @@ public partial class TapeDeviceHandler : IDisposable {
} }
public void WriteData(byte[] data) { public int WriteData(byte[] data) {
IntPtr unmanagedPointer = Marshal.AllocHGlobal(data.Length); IntPtr unmanagedPointer = Marshal.AllocHGlobal(data.Length);
try { try {
Marshal.Copy(data, 0, unmanagedPointer, data.Length); Marshal.Copy(data, 0, unmanagedPointer, data.Length);
@ -94,11 +94,14 @@ public partial class TapeDeviceHandler : IDisposable {
else { else {
int error = Marshal.GetLastWin32Error(); int error = Marshal.GetLastWin32Error();
Console.WriteLine($"Write Data: Failed with error code {error}"); Console.WriteLine($"Write Data: Failed with error code {error}");
return error;
} }
} }
finally { finally {
Marshal.FreeHGlobal(unmanagedPointer); Marshal.FreeHGlobal(unmanagedPointer);
} }
return 0;
} }
public byte[] ReadData(uint length) { public byte[] ReadData(uint length) {