(refactor): dependency injection, console and file logging

This commit is contained in:
Maksym Sadovnychyy 2024-11-02 21:29:34 +01:00
parent 0654476cf0
commit 62fbda88ba
15 changed files with 356 additions and 234 deletions

View File

@ -1,42 +1,102 @@
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Diagnostics.CodeAnalysis; using System.Net;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MaksIT.LTO.Core; using MaksIT.LTO.Core;
using MaksIT.LTO.Backup.Entities; using MaksIT.LTO.Backup.Entities;
using System.Net; using MaksIT.LTO.Core.MassStorage;
using MaksIT.LTO.Core.Networking;
namespace MaksIT.LTO.Backup; namespace MaksIT.LTO.Backup;
public class Application { public class Application {
private const string _descriptoFileName = "descriptor.json"; private const string _descriptoFileName = "descriptor.json";
private const string _configurationFileName = "configuration.json";
private readonly string appPath = AppDomain.CurrentDomain.BaseDirectory; private readonly string appPath = AppDomain.CurrentDomain.BaseDirectory;
private readonly string _tapePath; private readonly string _tapePath;
private readonly string _descriptorFilePath; private readonly string _descriptorFilePath;
private Configuration _configuration; private readonly ILogger<Application> _logger;
private readonly ILogger<TapeDeviceHandler> _tapeDeviceLogger;
private readonly ILogger<NetworkConnection> _networkConnectionLogger;
private readonly Configuration _configuration;
public Application(
ILogger<Application> logger,
ILoggerFactory loggerFactory,
IOptions<Configuration> configuration
) {
_logger = logger;
_tapeDeviceLogger = loggerFactory.CreateLogger<TapeDeviceHandler>();
_networkConnectionLogger = loggerFactory.CreateLogger<NetworkConnection>();
public Application() {
_descriptorFilePath = Path.Combine(appPath, _descriptoFileName); _descriptorFilePath = Path.Combine(appPath, _descriptoFileName);
LoadConfiguration();
_configuration = configuration.Value;
_tapePath = _configuration.TapePath; _tapePath = _configuration.TapePath;
} }
[MemberNotNull(nameof(_configuration))] public void Run() {
public void LoadConfiguration() { Console.OutputEncoding = Encoding.UTF8;
var configFilePath = Path.Combine(appPath, _configurationFileName);
var configuration = JsonSerializer.Deserialize<Configuration>(File.ReadAllText(configFilePath));
if (configuration == null)
throw new InvalidOperationException("Failed to deserialize configuration.");
_configuration = configuration; while (true) {
Console.WriteLine("MaksIT.LTO.Backup v0.0.1");
Console.WriteLine("© Maksym Sadovnychyy (MAKS-IT) 2024");
Console.WriteLine("\nSelect an action:");
Console.WriteLine("1. Load tape");
Console.WriteLine("2. Backup");
Console.WriteLine("3. Restore");
Console.WriteLine("4. Eject tape");
Console.WriteLine("5. Get device status");
Console.WriteLine("6. Tape Erase (Short)");
Console.WriteLine("7. Exit");
Console.Write("Enter your choice: ");
var choice = Console.ReadLine();
try {
switch (choice) {
case "1":
LoadTape();
break;
case "2":
Backup();
break;
case "3":
Restore();
break;
case "4":
EjectTape();
break;
case "5":
GetDeviceStatus();
break;
case "6":
TapeErase();
break;
case "7":
Console.WriteLine("Exiting...");
return;
default:
Console.WriteLine("Invalid choice. Please try again.");
break;
}
}
catch (Exception ex) {
_logger.LogError(ex, $"An error occurred: {ex.Message}");
}
}
} }
public void LoadTape() { public void LoadTape() {
using var handler = new TapeDeviceHandler(_tapePath); using var handler = new TapeDeviceHandler(_tapeDeviceLogger, _tapePath);
LoadTape(handler); LoadTape(handler);
} }
@ -44,11 +104,11 @@ public class Application {
handler.Prepare(TapeDeviceHandler.TAPE_LOAD); handler.Prepare(TapeDeviceHandler.TAPE_LOAD);
Thread.Sleep(2000); Thread.Sleep(2000);
Console.WriteLine("Tape loaded."); _logger.LogInformation("Tape loaded.");
} }
public void EjectTape() { public void EjectTape() {
using var handler = new TapeDeviceHandler(_tapePath); using var handler = new TapeDeviceHandler(_tapeDeviceLogger, _tapePath);
EjectTape(handler); EjectTape(handler);
} }
@ -56,11 +116,11 @@ public class Application {
handler.Prepare(TapeDeviceHandler.TAPE_UNLOAD); handler.Prepare(TapeDeviceHandler.TAPE_UNLOAD);
Thread.Sleep(2000); Thread.Sleep(2000);
Console.WriteLine("Tape ejected."); _logger.LogInformation("Tape ejected.");
} }
public void TapeErase() { public void TapeErase() {
using var handler = new TapeDeviceHandler(_tapePath); using var handler = new TapeDeviceHandler(_tapeDeviceLogger, _tapePath);
LoadTape(handler); LoadTape(handler);
handler.SetMediaParams(LTOBlockSizes.LTO5); handler.SetMediaParams(LTOBlockSizes.LTO5);
@ -80,16 +140,14 @@ public class Application {
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND); handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
Thread.Sleep(2000); Thread.Sleep(2000);
Console.WriteLine("Tape erased."); _logger.LogInformation("Tape erased.");
} }
public void GetDeviceStatus() { public void GetDeviceStatus() {
using var handler = new TapeDeviceHandler(_tapePath); using var handler = new TapeDeviceHandler(_tapeDeviceLogger, _tapePath);
handler.GetStatus(); handler.GetStatus();
} }
public void PathAccessWrapper(WorkingFolder workingFolder, Action<string> myAction) { public void PathAccessWrapper(WorkingFolder workingFolder, Action<string> myAction) {
if (workingFolder.LocalPath != null) { if (workingFolder.LocalPath != null) {
@ -117,7 +175,7 @@ public class Application {
throw new InvalidOperationException("Network credentials are required for remote paths."); throw new InvalidOperationException("Network credentials are required for remote paths.");
} }
using (new NetworkConnection(smbPath, networkCredential)) { using (new NetworkConnection(_networkConnectionLogger, smbPath, networkCredential)) {
myAction(smbPath); myAction(smbPath);
} }
} }
@ -175,8 +233,8 @@ public class Application {
} }
private void ZeroFillBlocks(TapeDeviceHandler handler, int blocks, uint blockSize) { private void ZeroFillBlocks(TapeDeviceHandler handler, int blocks, uint blockSize) {
Console.WriteLine($"Writing {blocks} zero-filled blocks to tape."); _logger.LogInformation($"Writing {blocks} zero-filled blocks to tape.");
Console.WriteLine($"Block Size: {blockSize}."); _logger.LogInformation($"Block Size: {blockSize}.");
var writeError = 0; var writeError = 0;
@ -191,10 +249,10 @@ public class Application {
public void WriteFilesToTape(WorkingFolder workingFolder, string descriptorFilePath, uint blockSize) { public void WriteFilesToTape(WorkingFolder workingFolder, string descriptorFilePath, uint blockSize) {
PathAccessWrapper(workingFolder, (directoryPath) => { PathAccessWrapper(workingFolder, (directoryPath) => {
Console.WriteLine($"Writing files to tape from: {directoryPath}."); _logger.LogInformation($"Writing files to tape from: {directoryPath}.");
Console.WriteLine($"Block Size: {blockSize}."); _logger.LogInformation($"Block Size: {blockSize}.");
using var handler = new TapeDeviceHandler(_tapePath); using var handler = new TapeDeviceHandler(_tapeDeviceLogger, _tapePath);
LoadTape(handler); LoadTape(handler);
@ -240,7 +298,7 @@ public class Application {
writeError = handler.WriteData(buffer); writeError = handler.WriteData(buffer);
if (writeError != 0) { if (writeError != 0) {
Console.WriteLine($"Failed to write file: {filePath}"); _logger.LogInformation($"Failed to write file: {filePath}");
return; return;
} }
@ -284,10 +342,10 @@ public class Application {
} }
public BackupDescriptor? FindDescriptor(uint blockSize) { public BackupDescriptor? FindDescriptor(uint blockSize) {
Console.WriteLine("Searching for descriptor on tape..."); _logger.LogInformation("Searching for descriptor on tape...");
Console.WriteLine($"Block Size: {blockSize}."); _logger.LogInformation($"Block Size: {blockSize}.");
using var handler = new TapeDeviceHandler(_tapePath); using var handler = new TapeDeviceHandler(_tapeDeviceLogger,_tapePath);
LoadTape(handler); LoadTape(handler);
@ -331,12 +389,12 @@ public class Application {
try { try {
var descriptor = JsonSerializer.Deserialize<BackupDescriptor>(json); var descriptor = JsonSerializer.Deserialize<BackupDescriptor>(json);
if (descriptor != null) { if (descriptor != null) {
Console.WriteLine("Descriptor read successfully."); _logger.LogInformation("Descriptor read successfully.");
return descriptor; return descriptor;
} }
} }
catch (JsonException ex) { catch (JsonException ex) {
Console.WriteLine($"Failed to parse descriptor JSON: {ex.Message}"); _logger.LogInformation($"Failed to parse descriptor JSON: {ex.Message}");
} }
@ -352,10 +410,10 @@ public class Application {
public void RestoreDirectory(BackupDescriptor descriptor, WorkingFolder workingFolder) { public void RestoreDirectory(BackupDescriptor descriptor, WorkingFolder workingFolder) {
PathAccessWrapper(workingFolder, (restoreDirectoryPath) => { PathAccessWrapper(workingFolder, (restoreDirectoryPath) => {
Console.WriteLine("Restoring files to directory: " + restoreDirectoryPath); _logger.LogInformation("Restoring files to directory: " + restoreDirectoryPath);
Console.WriteLine("Block Size: " + descriptor.BlockSize); _logger.LogInformation("Block Size: " + descriptor.BlockSize);
using var handler = new TapeDeviceHandler(_tapePath); using var handler = new TapeDeviceHandler(_tapeDeviceLogger,_tapePath);
LoadTape(handler); LoadTape(handler);
@ -397,10 +455,10 @@ public class Application {
var fileHashString = BitConverter.ToString(fileHash).Replace("-", "").ToLower(); var fileHashString = BitConverter.ToString(fileHash).Replace("-", "").ToLower();
if (fileHashString != file.FileHash) { if (fileHashString != file.FileHash) {
Console.WriteLine($"Checksum mismatch for file: {filePath}"); _logger.LogInformation($"Checksum mismatch for file: {filePath}");
} }
else { else {
Console.WriteLine($"Restored file: {filePath}"); _logger.LogInformation($"Restored file: {filePath}");
} }
} }
} }
@ -414,7 +472,7 @@ public class Application {
public int CheckMediaSize(string ltoGen) { public int CheckMediaSize(string ltoGen) {
var descriptor = JsonSerializer.Deserialize<BackupDescriptor>(File.ReadAllText(_descriptorFilePath)); var descriptor = JsonSerializer.Deserialize<BackupDescriptor>(File.ReadAllText(_descriptorFilePath));
if (descriptor == null) { if (descriptor == null) {
Console.WriteLine("Failed to read descriptor."); _logger.LogInformation("Failed to read descriptor.");
return 1; return 1;
} }
@ -430,11 +488,11 @@ public class Application {
var maxBlocks = LTOBlockSizes.GetMaxBlocks(ltoGen); var maxBlocks = LTOBlockSizes.GetMaxBlocks(ltoGen);
if (totalBlocks > maxBlocks) { if (totalBlocks > maxBlocks) {
Console.WriteLine("Backup will not fit on tape. Please use a larger tape."); _logger.LogInformation("Backup will not fit on tape. Please use a larger tape.");
return 1; return 1;
} }
else { else {
Console.WriteLine("Backup will fit on tape."); _logger.LogInformation("Backup will fit on tape.");
} }
return 0; return 0;
@ -442,21 +500,21 @@ public class Application {
public void Backup() { public void Backup() {
while (true) { while (true) {
Console.WriteLine("\nSelect a backup to perform:"); _logger.LogInformation("\nSelect a backup to perform:");
for (int i = 0; i < _configuration.Backups.Count; i++) { for (int i = 0; i < _configuration.Backups.Count; i++) {
var backupInt = _configuration.Backups[i]; var backupInt = _configuration.Backups[i];
Console.WriteLine($"{i + 1}. Backup Name: {backupInt.Name}, Bar code {(string.IsNullOrEmpty(backupInt.Barcode) ? "None" : backupInt.Barcode)}, Source: {backupInt.Source}, Destination: {backupInt.Destination}"); _logger.LogInformation($"{i + 1}. Backup Name: {backupInt.Name}, Bar code {(string.IsNullOrEmpty(backupInt.Barcode) ? "None" : backupInt.Barcode)}, Source: {backupInt.Source}, Destination: {backupInt.Destination}");
} }
Console.Write("Enter your choice (or '0' to go back): "); Console.Write("Enter your choice (or '0' to go back): ");
var choice = Console.ReadLine(); var choice = Console.ReadLine();
if (choice == "0") { if (choice == "0") {
return; // Go back to the main menu return;
} }
if (!int.TryParse(choice, out int index) || index < 1 || index > _configuration.Backups.Count) { if (!int.TryParse(choice, out int index) || index < 1 || index > _configuration.Backups.Count) {
Console.WriteLine("Invalid choice. Please try again."); _logger.LogInformation("Invalid choice. Please try again.");
continue; continue;
} }
@ -476,28 +534,28 @@ public class Application {
WriteFilesToTape(backup.Source, _descriptorFilePath, blockSize); WriteFilesToTape(backup.Source, _descriptorFilePath, blockSize);
File.Delete(_descriptorFilePath); File.Delete(_descriptorFilePath);
Console.WriteLine("Backup completed."); _logger.LogInformation("Backup completed.");
return; // Go back to the main menu after completing the backup return;
} }
} }
public void Restore() { public void Restore() {
while (true) { while (true) {
Console.WriteLine("\nSelect a backup to restore:"); _logger.LogInformation("\nSelect a backup to restore:");
for (int i = 0; i < _configuration.Backups.Count; i++) { for (int i = 0; i < _configuration.Backups.Count; i++) {
var backupInt = _configuration.Backups[i]; var backupInt = _configuration.Backups[i];
Console.WriteLine($"{i + 1}. Backup Name: {backupInt.Name}, Bar code {(string.IsNullOrEmpty(backupInt.Barcode) ? "None" : backupInt.Barcode)}, Source: {backupInt.Source}, Destination: {backupInt.Destination}"); _logger.LogInformation($"{i + 1}. Backup Name: {backupInt.Name}, Bar code {(string.IsNullOrEmpty(backupInt.Barcode) ? "None" : backupInt.Barcode)}, Source: {backupInt.Source}, Destination: {backupInt.Destination}");
} }
Console.Write("Enter your choice (or '0' to go back): "); Console.Write("Enter your choice (or '0' to go back): ");
var choice = Console.ReadLine(); var choice = Console.ReadLine();
if (choice == "0") { if (choice == "0") {
return; // Go back to the main menu return;
} }
if (!int.TryParse(choice, out int index) || index < 1 || index > _configuration.Backups.Count) { if (!int.TryParse(choice, out int index) || index < 1 || index > _configuration.Backups.Count) {
Console.WriteLine("Invalid choice. Please try again."); _logger.LogInformation("Invalid choice. Please try again.");
continue; continue;
} }
@ -505,21 +563,22 @@ public class Application {
uint blockSize = LTOBlockSizes.GetBlockSize(backup.LTOGen); uint blockSize = LTOBlockSizes.GetBlockSize(backup.LTOGen);
// Step 1: Find Descriptor on Tape
var descriptor = FindDescriptor(blockSize); var descriptor = FindDescriptor(blockSize);
if (descriptor != null) { if (descriptor != null) {
var json = JsonSerializer.Serialize(descriptor, new JsonSerializerOptions { WriteIndented = true }); var json = JsonSerializer.Serialize(descriptor, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine(json); _logger.LogInformation(json);
} }
if (descriptor == null) { if (descriptor == null) {
Console.WriteLine("Descriptor not found on tape."); _logger.LogInformation("Descriptor not found on tape.");
return; return;
} }
// Step 3: Test restore from tape // Step 2: Restore Files to Directory
RestoreDirectory(descriptor, backup.Destination); RestoreDirectory(descriptor, backup.Destination);
Console.WriteLine("Restore completed."); _logger.LogInformation("Restore completed.");
return; // Go back to the main menu after completing the restore return;
} }
} }
} }

View File

@ -7,6 +7,14 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.1" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\MaksIT.LTO.Core\MaksIT.LTO.Core.csproj" /> <ProjectReference Include="..\MaksIT.LTO.Core\MaksIT.LTO.Core.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -1,65 +1,35 @@
namespace MaksIT.LTO.Backup; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MaksIT.LTO.Core.Logging;
namespace MaksIT.LTO.Backup;
class Program { class Program {
public static void Main() { public static void Main() {
var app = new Application(); // Set up configuration with reload support
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("configuration.json", optional: false, reloadOnChange: true) // Enable reload on change
.Build();
Console.OutputEncoding = System.Text.Encoding.UTF8; var serviceProvider = new ServiceCollection()
.Configure<Configuration>(configuration.GetSection("Configuration")) // Bind AppConfig directly
.AddSingleton(configuration) // Make IConfiguration available if needed
.AddLogging(builder =>
{
builder.AddConfiguration(configuration.GetSection("Logging"));
builder.AddConsole();
builder.AddFile(Path.Combine(Directory.GetCurrentDirectory(), "log.txt"));
})
.AddTransient<Application>()
.BuildServiceProvider();
while (true) {
// Console.Clear();
Console.WriteLine("MaksIT.LTO.Backup v0.0.1");
Console.WriteLine("© Maksym Sadovnychyy (MAKS-IT) 2024");
Console.WriteLine("\nSelect an action:"); // Get the App service and run it
Console.WriteLine("1. Load tape"); var app = serviceProvider.GetRequiredService<Application>();
Console.WriteLine("2. Backup"); app.Run();
Console.WriteLine("3. Restore");
Console.WriteLine("4. Eject tape");
Console.WriteLine("5. Get device status");
Console.WriteLine("6. Tape Erase (Short)");
Console.WriteLine("7. Reload configurations");
Console.WriteLine("8. Exit");
Console.Write("Enter your choice: ");
var choice = Console.ReadLine();
try {
switch (choice) {
case "1":
app.LoadTape();
break;
case "2":
app.Backup();
break;
case "3":
app.Restore();
break;
case "4":
app.EjectTape();
break;
case "5":
app.GetDeviceStatus();
break;
case "6":
app.TapeErase();
break;
case "7":
app.LoadConfiguration();
break;
case "8":
Console.WriteLine("Exiting...");
return;
default:
Console.WriteLine("Invalid choice. Please try again.");
break;
}
}
catch (Exception ex) {
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
} }
} }

View File

@ -1,4 +1,13 @@
{ {
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Configuration": {
"TapePath": "\\\\.\\Tape0", "TapePath": "\\\\.\\Tape0",
"WriteDelay": 100, "WriteDelay": 100,
"Backups": [ "Backups": [
@ -38,4 +47,5 @@
} }
} }
] ]
}
} }

View File

@ -1,9 +1,5 @@
using System; using System.Diagnostics;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.LTO.Core; namespace MaksIT.LTO.Core;
public class DriverManager { public class DriverManager {

View File

@ -1,60 +0,0 @@
namespace MaksIT.LTO.Core;
public static class LTOBlockSizes {
public const uint LTO1 = 65536; // 64 KB
public const uint LTO2 = 65536; // 64 KB
public const uint LTO3 = 131072; // 128 KB
public const uint LTO4 = 131072; // 128 KB
public const uint LTO5 = 262144; // 256 KB
public const uint LTO6 = 262144; // 256 KB
public const uint LTO7 = 524288; // 512 KB
public const uint LTO8 = 524288; // 512 KB
public const uint LTO9 = 1048576; // 1 MB
// Dictionary to store the total capacity for each LTO generation (in bytes)
private static readonly Dictionary<string, ulong> TapeCapacities = new Dictionary<string, ulong>
{
{ "LTO1", 100UL * 1024 * 1024 * 1024 }, // 100 GB
{ "LTO2", 200UL * 1024 * 1024 * 1024 }, // 200 GB
{ "LTO3", 400UL * 1024 * 1024 * 1024 }, // 400 GB
{ "LTO4", 800UL * 1024 * 1024 * 1024 }, // 800 GB
{ "LTO5", 1500UL * 1024 * 1024 * 1024 }, // 1.5 TB
{ "LTO6", 2500UL * 1024 * 1024 * 1024 }, // 2.5 TB
{ "LTO7", 6000UL * 1024 * 1024 * 1024 }, // 6 TB
{ "LTO8", 12000UL * 1024 * 1024 * 1024 },// 12 TB
{ "LTO9", 18000UL * 1024 * 1024 * 1024 } // 18 TB
};
// Method to get the block size for a given LTO generation
// Method to get the block size for a given LTO generation
public static uint GetBlockSize(string ltoGen) {
return ltoGen switch {
"LTO1" => LTO1,
"LTO2" => LTO2,
"LTO3" => LTO3,
"LTO4" => LTO4,
"LTO5" => LTO5,
"LTO6" => LTO6,
"LTO7" => LTO7,
"LTO8" => LTO8,
"LTO9" => LTO9,
_ => throw new ArgumentException("Invalid LTO generation")
};
}
// Method to get the total capacity for a given LTO generation
public static ulong GetTapeCapacity(string ltoGen) {
if (TapeCapacities.TryGetValue(ltoGen, out var capacity)) {
return capacity;
}
throw new ArgumentException("Invalid LTO generation");
}
// Method to calculate the maximum number of blocks that can be written on the tape
public static ulong GetMaxBlocks(string ltoGen) {
var blockSize = GetBlockSize(ltoGen);
var tapeCapacity = GetTapeCapacity(ltoGen);
return tapeCapacity / blockSize;
}
}

View File

@ -0,0 +1,37 @@
using Microsoft.Extensions.Logging;
namespace MaksIT.LTO.Core.Logging;
public class FileLogger : ILogger {
private readonly string _filePath;
private readonly object _lock = new object();
public FileLogger(string filePath) {
_filePath = filePath;
}
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
public bool IsEnabled(LogLevel logLevel) {
return logLevel != LogLevel.None;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) {
if (!IsEnabled(logLevel))
return;
var message = formatter(state, exception);
if (string.IsNullOrEmpty(message))
return;
var logRecord = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} [{logLevel}] {message}";
if (exception != null) {
logRecord += Environment.NewLine + exception;
}
lock (_lock) {
File.AppendAllText(_filePath, logRecord + Environment.NewLine);
}
}
}

View File

@ -0,0 +1,19 @@
using Microsoft.Extensions.Logging;
namespace MaksIT.LTO.Core.Logging;
[ProviderAlias("FileLogger")]
public class FileLoggerProvider : ILoggerProvider {
private readonly string _filePath;
public FileLoggerProvider(string filePath) {
_filePath = filePath ?? throw new ArgumentNullException(nameof(filePath));
}
public ILogger CreateLogger(string categoryName) {
return new FileLogger(_filePath);
}
public void Dispose() { }
}

View File

@ -0,0 +1,12 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace MaksIT.LTO.Core.Logging;
public static class LoggingBuilderExtensions {
public static ILoggingBuilder AddFile(this ILoggingBuilder builder, string filePath) {
builder.Services.AddSingleton<ILoggerProvider>(new FileLoggerProvider(filePath));
return builder;
}
}

View File

@ -7,4 +7,9 @@
<DefineConstants>NTDDI_VERSION_05010000;NTDDI_WINXP_05010000</DefineConstants> <DefineConstants>NTDDI_VERSION_05010000;NTDDI_WINXP_05010000</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.2" />
</ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,65 @@
namespace MaksIT.LTO.Core.MassStorage;
public static class LTOBlockSizes
{
public const uint LTO1 = 65536; // 64 KB
public const uint LTO2 = 65536; // 64 KB
public const uint LTO3 = 131072; // 128 KB
public const uint LTO4 = 131072; // 128 KB
public const uint LTO5 = 262144; // 256 KB
public const uint LTO6 = 262144; // 256 KB
public const uint LTO7 = 524288; // 512 KB
public const uint LTO8 = 524288; // 512 KB
public const uint LTO9 = 1048576; // 1 MB
// Dictionary to store the total capacity for each LTO generation (in bytes)
private static readonly Dictionary<string, ulong> TapeCapacities = new Dictionary<string, ulong>
{
{ "LTO1", 100UL * 1024 * 1024 * 1024 }, // 100 GB
{ "LTO2", 200UL * 1024 * 1024 * 1024 }, // 200 GB
{ "LTO3", 400UL * 1024 * 1024 * 1024 }, // 400 GB
{ "LTO4", 800UL * 1024 * 1024 * 1024 }, // 800 GB
{ "LTO5", 1500UL * 1024 * 1024 * 1024 }, // 1.5 TB
{ "LTO6", 2500UL * 1024 * 1024 * 1024 }, // 2.5 TB
{ "LTO7", 6000UL * 1024 * 1024 * 1024 }, // 6 TB
{ "LTO8", 12000UL * 1024 * 1024 * 1024 },// 12 TB
{ "LTO9", 18000UL * 1024 * 1024 * 1024 } // 18 TB
};
// Method to get the block size for a given LTO generation
// Method to get the block size for a given LTO generation
public static uint GetBlockSize(string ltoGen)
{
return ltoGen switch
{
"LTO1" => LTO1,
"LTO2" => LTO2,
"LTO3" => LTO3,
"LTO4" => LTO4,
"LTO5" => LTO5,
"LTO6" => LTO6,
"LTO7" => LTO7,
"LTO8" => LTO8,
"LTO9" => LTO9,
_ => throw new ArgumentException("Invalid LTO generation")
};
}
// Method to get the total capacity for a given LTO generation
public static ulong GetTapeCapacity(string ltoGen)
{
if (TapeCapacities.TryGetValue(ltoGen, out var capacity))
{
return capacity;
}
throw new ArgumentException("Invalid LTO generation");
}
// Method to calculate the maximum number of blocks that can be written on the tape
public static ulong GetMaxBlocks(string ltoGen)
{
var blockSize = GetBlockSize(ltoGen);
var tapeCapacity = GetTapeCapacity(ltoGen);
return tapeCapacity / blockSize;
}
}

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Microsoft.Extensions.Logging;
using Microsoft.Win32.SafeHandles; using Microsoft.Win32.SafeHandles;
// https://github.com/tpn/winsdk-10 // https://github.com/tpn/winsdk-10
@ -8,11 +9,11 @@ using Microsoft.Win32.SafeHandles;
namespace MaksIT.LTO.Core; namespace MaksIT.LTO.Core;
public partial class TapeDeviceHandler : IDisposable { public partial class TapeDeviceHandler : IDisposable {
private readonly ILogger<TapeDeviceHandler> _logger;
private string _tapeDevicePath; private string _tapeDevicePath;
private SafeFileHandle _tapeHandle; private SafeFileHandle _tapeHandle;
private const uint GENERIC_READ = 0x80000000; private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000; private const uint GENERIC_WRITE = 0x40000000;
private const uint OPEN_EXISTING = 3; private const uint OPEN_EXISTING = 3;
@ -67,7 +68,11 @@ public partial class TapeDeviceHandler : IDisposable {
out uint lpNumberOfBytesRead, out uint lpNumberOfBytesRead,
IntPtr lpOverlapped); IntPtr lpOverlapped);
public TapeDeviceHandler(string tapeDevicePath) { public TapeDeviceHandler(
ILogger<TapeDeviceHandler> logger,
string tapeDevicePath
) {
_logger = logger;
_tapeDevicePath = tapeDevicePath; _tapeDevicePath = tapeDevicePath;
OpenTapeDevice(GENERIC_READ | GENERIC_WRITE); OpenTapeDevice(GENERIC_READ | GENERIC_WRITE);
} }

View File

@ -1,27 +1,23 @@
using System.Net; using System.Net;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace MaksIT.LTO.Core; using Microsoft.Extensions.Logging;
//public void RestoreFilesFromSmbShare(string smbPath, string username, string password, string domain, string restoreDirectory) {
// var credentials = new NetworkCredential(username, password, domain);
// using (new NetworkConnection(smbPath, credentials)) {
// var files = Directory.GetFiles(smbPath, "*.*", SearchOption.AllDirectories);
// foreach (var file in files) {
// var relativePath = Path.GetRelativePath(smbPath, file);
// var destinationPath = Path.Combine(restoreDirectory, relativePath);
// Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
// File.Copy(file, destinationPath, overwrite: true);
// Console.WriteLine($"Restored file: {file} to {destinationPath}");
// }
// }
//}
namespace MaksIT.LTO.Core.Networking;
public class NetworkConnection : IDisposable { public class NetworkConnection : IDisposable {
private readonly ILogger<NetworkConnection> _logger;
private readonly string _networkName; private readonly string _networkName;
public NetworkConnection(string networkName, NetworkCredential credentials) { public NetworkConnection(
ILogger<NetworkConnection> logger,
string networkName,
NetworkCredential credentials) {
_logger = logger;
_networkName = networkName; _networkName = networkName;
var netResource = new NetResource { var netResource = new NetResource {