Add SCSI INQUIRY responses management view with duplicate handling

This commit is contained in:
2025-09-12 15:14:08 +01:00
parent 6975c6c67a
commit dc51390bb3
5 changed files with 314 additions and 9 deletions

View File

@@ -1,14 +1,15 @@
using Aaru.CommonTypes.Structs.Devices.SCSI;
using Aaru.Helpers;
namespace Aaru.Server.Database.Models;
public class IdHashModel : BaseModel<int>
{
public IdHashModel(int id, string hash)
{
Id = id;
Hash = hash;
}
public string Hash { get; set; }
public string Description { get; set; }
public int[] Duplicates { get; set; }
public string Hash { get; set; }
public string Description => string.Join(' ', VendorIdentification, ProductIdentification, ProductRevisionLevel);
public int[] Duplicates { get; set; }
public string VendorIdentification => StringHandlers.CToString(Inquiry?.VendorIdentification);
public string ProductIdentification => StringHandlers.CToString(Inquiry?.ProductIdentification);
public string ProductRevisionLevel => StringHandlers.CToString(Inquiry?.ProductRevisionLevel);
public Inquiry? Inquiry { get; set; }
}

View File

@@ -50,6 +50,9 @@
<NavLink class="nav-link" href="/admin/scsi/cdrom">
SCSI CD-ROM Capabilities
</NavLink>
<NavLink class="nav-link" href="/admin/scsi">
SCSI INQUIRY responses
</NavLink>
<NavLink class="nav-link" href="/admin/scsi/pages">
SCSI MODE SENSE pages
</NavLink>

View File

@@ -0,0 +1,128 @@
@page "/admin/scsi"
@using Aaru.Helpers
@attribute [Authorize]
@layout AdminLayout
@inject Microsoft.EntityFrameworkCore.IDbContextFactory<DbContext> DbContextFactory
<PageTitle>SCSI INQUIRY responses</PageTitle>
@if(!_initialized)
{
<div class="stats-section">
<h1 style="color: red; align-content: center; padding: 2rem">Loading...</h1>
</div>
return;
}
<section class="stats-section">
<h4>SCSI INQUIRY responses</h4>
<table class="table table-dark table-striped table-bordered mt-4 mb-4">
<thead class="thead-dark">
<tr>
<th class="fw-bold bg-secondary text-light">
@DisplayNameHelper.GetDisplayName(typeof(Scsi), nameof(Scsi.Id))
</th>
<th class="fw-bold bg-secondary text-light">
Vendor identification
</th>
<th class="fw-bold bg-secondary text-light">
Product identification
</th>
<th class="fw-bold bg-secondary text-light">
Product revision level
</th>
<th class="fw-bold bg-secondary text-light">
@DisplayNameHelper.GetDisplayName(typeof(Scsi), nameof(Scsi.SupportsModeSense6))
</th>
<th class="fw-bold bg-secondary text-light">
@DisplayNameHelper.GetDisplayName(typeof(Scsi), nameof(Scsi.SupportsModeSense10))
</th>
<th class="fw-bold bg-secondary text-light">
@DisplayNameHelper.GetDisplayName(typeof(Scsi), nameof(Scsi.SupportsModeSubpages))
</th>
<th class="fw-bold bg-secondary text-light">
Actions
</th>
</tr>
</thead>
<tbody>
@foreach(Scsi item in _items)
{
<tr>
<td>
@item.Id
</td>
<td>
@StringHandlers.CToString(item.Inquiry?.VendorIdentification)
</td>
<td>
@StringHandlers.CToString(item.Inquiry?.ProductIdentification)
</td>
<td>
@StringHandlers.CToString(item.Inquiry?.ProductRevisionLevel)
</td>
<td>
@item.SupportsModeSense6
</td>
<td>
@item.SupportsModeSense10
</td>
<td>
@item.SupportsModeSubpages
</td>
<td>
<a href="/admin/scsi/@item.Id" class="btn btn-primary btn-sm">
<i class="bi bi-eye"></i> Details
</a>
<button class="btn btn-danger btn-sm" @onclick="async () => await ShowDeleteModal(item.Id)">
<i class="bi bi-trash"></i> Delete
</button>
</td>
</tr>
}
</tbody>
</table>
@if(_duplicates.Count > 0)
{
<div>
The following SCSI INQUIRY responses have duplicates.
<table class="table table-dark table-striped table-bordered mt-4 mb-4">
<tbody>
@foreach(IdHashModel item in _duplicates)
{
<tr>
<td>
@item.Description
</td>
</tr>
}
</tbody>
</table>
</div>
<Button class="btn btn-danger" @onclick="ConsolidateDuplicatesAsync">Consolidate Duplicates</Button>
}
</section>
<BlazorBootstrap.Modal @ref="_consolidateModal" Title="Consolidate duplicates" Size="ModalSize.Small">
<BodyTemplate>
<div class="text-danger">Are you sure you want to delete the duplicates?</div>
</BodyTemplate>
<FooterTemplate>
<button class="btn btn-secondary" @onclick="HideConsolidateModalAsync">Cancel</button>
<button class="btn btn-danger" @onclick="ConfirmConsolidateAsync">Delete</button>
</FooterTemplate>
</BlazorBootstrap.Modal>
<BlazorBootstrap.Modal @ref="_deleteModal" Title="Delete SCSI INQUIRY response" Size="ModalSize.Small">
<BodyTemplate>
<div class="text-danger">Are you sure you want to delete this SCSI INQUIRY response?</div>
</BodyTemplate>
<FooterTemplate>
<button class="btn btn-secondary" @onclick="HideDeleteModal">Cancel</button>
<button class="btn btn-danger" @onclick="ConfirmDelete">Delete</button>
</FooterTemplate>
</BlazorBootstrap.Modal>

View File

@@ -0,0 +1,138 @@
using Aaru.Helpers;
using Aaru.Server.Core;
using Aaru.Server.Database.Models;
using BlazorBootstrap;
using Microsoft.EntityFrameworkCore;
using DbContext = Aaru.Server.Database.DbContext;
namespace Aaru.Server.Components.Admin.Pages.Scsi;
public partial class List
{
private Modal? _consolidateModal;
private int _deleteId;
private Modal? _deleteModal;
List<IdHashModel?> _duplicates;
bool _initialized;
List<CommonTypes.Metadata.Scsi> _items;
/// <inheritdoc />
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
StateHasChanged();
await RefreshItemsAsync();
_initialized = true;
StateHasChanged();
}
async Task RefreshItemsAsync()
{
await using DbContext ctx = await DbContextFactory.CreateDbContextAsync();
_items = ctx.Scsi.AsEnumerable()
.OrderBy(static m => StringHandlers.CToString(m.Inquiry?.VendorIdentification))
.ThenBy(static m => StringHandlers.CToString(m.Inquiry?.ProductIdentification))
.ThenBy(static m => StringHandlers.CToString(m.Inquiry?.ProductRevisionLevel))
.ToList();
List<IdHashModel> hashes = await ctx.Scsi.Where(static m => m.InquiryData != null)
.Select(static m => new IdHashModel
{
Id = m.Id,
Hash = Hash.Sha512(m.InquiryData),
Inquiry = m.Inquiry
})
.ToListAsync();
_duplicates = hashes.GroupBy(static x => x.Hash)
.Where(static g => g.Count() > 1)
.Select(x => hashes.FirstOrDefault(y => y.Hash == x.Key))
.OrderBy(static d => d?.Description)
.ToList();
}
Task ConsolidateDuplicatesAsync() => _consolidateModal?.ShowAsync();
Task HideConsolidateModalAsync() => _consolidateModal?.HideAsync();
async Task ConfirmConsolidateAsync()
{
await using DbContext ctx = await DbContextFactory.CreateDbContextAsync();
foreach(IdHashModel duplicate in _duplicates)
{
CommonTypes.Metadata.Scsi? master = ctx.Scsi.FirstOrDefault(m => duplicate != null && m.Id == duplicate.Id);
if(master is null) continue;
if(duplicate?.Duplicates == null) continue;
foreach(int duplicateId in duplicate.Duplicates)
{
CommonTypes.Metadata.Scsi? slave = ctx.Scsi.Include(static scsi => scsi.ReadCapabilities)
.FirstOrDefault(m => m.Id == duplicateId);
if(slave is null) continue;
foreach(Device scsiDevice in ctx.Devices.Where(d => d.SCSI.Id == duplicateId)) scsiDevice.SCSI = master;
foreach(UploadedReport scsiReport in ctx.Reports.Where(d => d.SCSI.Id == duplicateId))
scsiReport.SCSI = master;
foreach(CommonTypes.Metadata.TestedMedia testedMedia in
ctx.TestedMedia.Where(d => d.ScsiId == duplicateId))
{
testedMedia.ScsiId = duplicate.Id;
ctx.Update(testedMedia);
}
if(master.ReadCapabilities is null && slave.ReadCapabilities != null)
master.ReadCapabilities = slave.ReadCapabilities;
ctx.Scsi.Remove(slave);
}
}
await ctx.SaveChangesAsync();
await RefreshItemsAsync();
StateHasChanged();
}
private async Task ShowDeleteModal(int id)
{
_deleteId = id;
if(_deleteModal != null) await _deleteModal.ShowAsync();
}
private async Task HideDeleteModal()
{
if(_deleteModal != null) await _deleteModal.HideAsync();
}
private async Task ConfirmDelete()
{
await DeleteAsync(_deleteId);
await HideDeleteModal();
await RefreshItemsAsync();
}
private async Task DeleteAsync(int id)
{
await using DbContext ctx = await DbContextFactory.CreateDbContextAsync();
CommonTypes.Metadata.Ssc? ssc = await ctx.Ssc.FindAsync(id);
if(ssc is not null)
{
ctx.Ssc.Remove(ssc);
await ctx.SaveChangesAsync();
}
}
}

35
Aaru.Server/Core/Hash.cs Normal file
View File

@@ -0,0 +1,35 @@
using System.Security.Cryptography;
namespace Aaru.Server.Core;
public static class Hash
{
public static string Sha512(byte[] data)
{
byte[] hash;
using(var sha = new SHA512Managed())
{
sha.Initialize();
hash = sha.ComputeHash(data);
}
char[] chars = new char[hash.Length * 2];
int j = 0;
foreach(byte b in hash)
{
int nibble1 = (b & 0xF0) >> 4;
int nibble2 = b & 0x0F;
nibble1 += nibble1 > 9 ? 0x57 : 0x30;
nibble2 += nibble2 > 9 ? 0x57 : 0x30;
chars[j++] = (char)nibble1;
chars[j++] = (char)nibble2;
}
return new string(chars);
}
}