public async Task <IEnumerable <IPhysicalDrive> > GetPhysicalDrives() { if (!OperatingSystem.IsLinux()) { throw new NotSupportedException("Linux physical drive manager is not running on Linux environment"); } var lsBlkJson = await GetLsBlkJson(); var lsBlk = LsBlkReader.ParseLsBlk(lsBlkJson); if (lsBlk.BlockDevices == null) { return(Enumerable.Empty <IPhysicalDrive>()); } var diskBlockDevices = lsBlk.BlockDevices.Where(x => !string.IsNullOrWhiteSpace(x.Type) && x.Type.Equals("disk", StringComparison.OrdinalIgnoreCase) && x.Removable).ToList(); return(diskBlockDevices.Select(x => new GenericPhysicalDrive(x.Path, x.Type, string.Concat(x.Vendor, " ", x.Model), x.Size ?? 0))); }
public async Task <IEnumerable <IPhysicalDrive> > GetPhysicalDrives() { if (!OperatingSystem.IsMacOs()) { throw new NotSupportedException("MacOS physical drive manager is not running on macOS environment"); } var listOutput = await GetDiskUtilExternalDisks(); var disks = DiskUtilReader.ParseList(new MemoryStream(Encoding.UTF8.GetBytes(listOutput))).ToList(); var physicalDrives = new List <IPhysicalDrive>(); foreach (var disk in disks) { var partitionDevices = disk.Partitions.Select(x => x.DeviceIdentifier).ToList(); var infoOutput = await GetDiskUtilInfoDisk(disk.DeviceIdentifier); var info = DiskUtilReader.ParseInfo(new MemoryStream(Encoding.UTF8.GetBytes(infoOutput))); if (info.BusProtocol.Equals("Disk Image", StringComparison.OrdinalIgnoreCase)) { continue; } physicalDrives.Add(new MacOsPhysicalDrive(info.DeviceNode, info.MediaType, info.IoRegistryEntryName, info.Size, partitionDevices)); } return(physicalDrives); }
public IPhysicalDriveManager Create() { if (OperatingSystem.IsWindows()) { return(new WindowsPhysicalDriveManager(this.loggerFactory.CreateLogger <WindowsPhysicalDriveManager>())); } if (OperatingSystem.IsMacOs()) { return(new MacOsPhysicalDriveManager(this.loggerFactory.CreateLogger <MacOsPhysicalDriveManager>())); } if (OperatingSystem.IsLinux()) { return(new LinuxPhysicalDriveManager(this.loggerFactory.CreateLogger <LinuxPhysicalDriveManager>())); } throw new NotSupportedException("Unsupported operating system"); }
static async Task Run(Arguments arguments) { Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .WriteTo.Console() .CreateLogger(); var serviceProvider = new ServiceCollection() .AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true)) .BuildServiceProvider(); var loggerFactory = serviceProvider.GetService <ILoggerFactory>(); var isAdministrator = OperatingSystem.IsAdministrator(); if (!isAdministrator) { Console.WriteLine("Requires administrator rights!"); } var commandHelper = new CommandHelper(); var physicalDrives = (await GetPhysicalDrives(arguments)).ToList(); var cancellationTokenSource = new CancellationTokenSource(); switch (arguments.Command) { case Arguments.CommandEnum.List: var listCommand = new ListCommand(loggerFactory.CreateLogger <ListCommand>(), commandHelper, physicalDrives); listCommand.ListRead += (_, args) => { // // await Task.Run(() => // { // Console.WriteLine(JsonSerializer.Serialize(physicalDrivesList, JsonSerializerOptions)); // }); InfoPresenter.PresentInfo(args.MediaInfos); }; var listResult = await listCommand.Execute(cancellationTokenSource.Token); Console.WriteLine(listResult.IsSuccess ? "Done" : $"ERROR: Read failed, {listResult.Error}"); break; case Arguments.CommandEnum.Info: var infoCommand = new InfoCommand(loggerFactory.CreateLogger <InfoCommand>(), commandHelper, physicalDrives, arguments.SourcePath); infoCommand.DiskInfoRead += (_, args) => { InfoPresenter.PresentInfo(args.MediaInfo); }; var infoResult = await infoCommand.Execute(cancellationTokenSource.Token); Console.WriteLine(infoResult.IsSuccess ? "Done" : $"ERROR: Read failed, {infoResult.Error}"); break; case Arguments.CommandEnum.Read: Console.WriteLine("Reading physical drive to image file"); GenericPresenter.PresentPaths(arguments); var readCommand = new ReadCommand(loggerFactory.CreateLogger <ReadCommand>(), commandHelper, physicalDrives, arguments.SourcePath, arguments.DestinationPath, arguments.Size); readCommand.DataProcessed += (_, args) => { GenericPresenter.Present(args); }; var readResult = await readCommand.Execute(cancellationTokenSource.Token); Console.WriteLine(readResult.IsSuccess ? "Done" : $"ERROR: Read failed, {readResult.Error}"); break; case Arguments.CommandEnum.Convert: Console.WriteLine("Converting source image to destination image file"); GenericPresenter.PresentPaths(arguments); var convertCommand = new ConvertCommand(loggerFactory.CreateLogger <ConvertCommand>(), commandHelper, arguments.SourcePath, arguments.DestinationPath, arguments.Size); convertCommand.DataProcessed += (_, args) => { GenericPresenter.Present(args); }; var convertResult = await convertCommand.Execute(cancellationTokenSource.Token); Console.WriteLine( convertResult.IsSuccess ? "Done" : $"ERROR: Convert failed, {convertResult.Error}"); break; case Arguments.CommandEnum.Write: Console.WriteLine("Writing source image file to physical drive"); GenericPresenter.PresentPaths(arguments); var writeCommand = new WriteCommand(loggerFactory.CreateLogger <WriteCommand>(), commandHelper, physicalDrives, arguments.SourcePath, arguments.DestinationPath, arguments.Size); writeCommand.DataProcessed += (_, args) => { GenericPresenter.Present(args); }; var writeResult = await writeCommand.Execute(cancellationTokenSource.Token); Console.WriteLine(writeResult.IsSuccess ? "Done" : $"ERROR: Write failed, {writeResult.Error}"); break; case Arguments.CommandEnum.Verify: Console.WriteLine("Verifying source image to destination"); GenericPresenter.PresentPaths(arguments); var verifyCommand = new VerifyCommand(loggerFactory.CreateLogger <VerifyCommand>(), commandHelper, physicalDrives, arguments.SourcePath, arguments.DestinationPath, arguments.Size); verifyCommand.DataProcessed += (_, args) => { GenericPresenter.Present(args); }; var verifyResult = await verifyCommand.Execute(cancellationTokenSource.Token); Console.WriteLine(verifyResult.IsSuccess ? "Done" : $"ERROR: Verify failed, {verifyResult.Error}"); break; case Arguments.CommandEnum.Blank: Console.WriteLine("Creating blank image"); Console.WriteLine($"Path: {arguments.SourcePath}"); var blankCommand = new BlankCommand(loggerFactory.CreateLogger <BlankCommand>(), commandHelper, arguments.SourcePath, arguments.Size); var blankResult = await blankCommand.Execute(cancellationTokenSource.Token); Console.WriteLine(blankResult.IsSuccess ? "Done" : $"ERROR: Blank failed, {blankResult.Error}"); break; case Arguments.CommandEnum.Optimize: Console.WriteLine("Optimizing image file"); Console.WriteLine($"Path: {arguments.SourcePath}"); var optimizeCommand = new OptimizeCommand(loggerFactory.CreateLogger <OptimizeCommand>(), commandHelper, arguments.SourcePath); var optimizeResult = await optimizeCommand.Execute(cancellationTokenSource.Token); Console.WriteLine(optimizeResult.IsSuccess ? "Done" : $"ERROR: Optimize failed, {optimizeResult.Error}"); break; } }