diff --git a/src/MaksIT.LTO.Backup/Application.cs b/src/MaksIT.LTO.Backup/Application.cs index 59d7609..8d8188f 100644 --- a/src/MaksIT.LTO.Backup/Application.cs +++ b/src/MaksIT.LTO.Backup/Application.cs @@ -153,8 +153,13 @@ public class Application { Console.WriteLine($"Writing {blocks} zero-filled blocks to tape."); Console.WriteLine($"Block Size: {blockSize}."); + var writeError = 0; + 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); } } @@ -191,6 +196,9 @@ public class Application { var currentTapeBlock = (descriptorJson.Length + blockSize - 1) / blockSize; + + int writeError = 0; + foreach (var file in descriptor.Files) { var filePath = Path.Combine(directoryPath, file.FilePath); 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 Array.Clear(buffer, bytesRead, buffer.Length - bytesRead); } - handler.WriteData(buffer); + + writeError = handler.WriteData(buffer); + if (writeError != 0) + return; + currentTapeBlock++; Thread.Sleep(_configuration.WriteDelay); // Small delay between blocks } @@ -223,7 +235,11 @@ public class Application { var length = Math.Min(blockSize, descriptorData.Length - startIndex); byte[] block = new byte[blockSize]; // Initialized with zeros by default Array.Copy(descriptorData, startIndex, block, 0, length); - handler.WriteData(block); + + writeError = handler.WriteData(block); + if (writeError != 0) + return; + currentTapeBlock++; Thread.Sleep(_configuration.WriteDelay); // Small delay between blocks } diff --git a/src/MaksIT.LTO.Core/DriverManager.cs b/src/MaksIT.LTO.Core/DriverManager.cs new file mode 100644 index 0000000..77395a5 --- /dev/null +++ b/src/MaksIT.LTO.Core/DriverManager.cs @@ -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}"); + } + } + } +} diff --git a/src/MaksIT.LTO.Core/TapeDeviceHandler.cs b/src/MaksIT.LTO.Core/TapeDeviceHandler.cs index ad0ef0a..dc4645b 100644 --- a/src/MaksIT.LTO.Core/TapeDeviceHandler.cs +++ b/src/MaksIT.LTO.Core/TapeDeviceHandler.cs @@ -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); try { Marshal.Copy(data, 0, unmanagedPointer, data.Length); @@ -94,11 +94,14 @@ public partial class TapeDeviceHandler : IDisposable { else { int error = Marshal.GetLastWin32Error(); Console.WriteLine($"Write Data: Failed with error code {error}"); + return error; } } finally { Marshal.FreeHGlobal(unmanagedPointer); } + + return 0; } public byte[] ReadData(uint length) {