Store and retrieve USB IDs from databases.

This commit is contained in:
2018-12-25 00:07:22 +00:00
parent 5a0714e795
commit b1287f8e2c
12 changed files with 414 additions and 21358 deletions

View File

@@ -1868,6 +1868,16 @@
<e p="packages.config" t="Include" /> <e p="packages.config" t="Include" />
<e p="usb.ids" t="Include" /> <e p="usb.ids" t="Include" />
</e> </e>
<e p="DiscImageChef.Server.Task" t="IncludeRecursive">
<e p="App.config" t="Include" />
<e p="DiscImageChef.Server.Task.csproj" t="IncludeRecursive" />
<e p="Program.cs" t="Include" />
<e p="Properties" t="Include">
<e p="AssemblyInfo.cs" t="Include" />
</e>
<e p="bin" t="ExcludeRecursive" />
<e p="obj" t="ExcludeRecursive" />
</e>
<e p="DiscImageChef.Settings" t="IncludeRecursive"> <e p="DiscImageChef.Settings" t="IncludeRecursive">
<e p="DiscImageChef.Settings.csproj" t="IncludeRecursive" /> <e p="DiscImageChef.Settings.csproj" t="IncludeRecursive" />
<e p="Settings.cs" t="Include" /> <e p="Settings.cs" t="Include" />

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
https://go.microsoft.com/fwlink/?LinkId=301879
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DicServerContext" connectionString="server=localhost;port=3306;database=discimagechef;uid=dic;password=dicpass"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
<providers>
<provider invariantName="MySql.Data.MySqlClient"
type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.EntityFramework"/>
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL"
type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=8.0.13.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
</configuration>

View File

@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{79D6A54E-47B6-4D0A-ABEB-4FA7B5ADF4B2}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DiscImageChef.Server.Task</RootNamespace>
<AssemblyName>DiscImageChef.Server.Task</AssemblyName>
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<HintPath>..\..\..\.nuget\packages\entityframework\6.2.0\lib\net45\EntityFramework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DiscImageChef.Server\DiscImageChef.Server.csproj">
<Project>{911addf5-e5fa-445d-88cd-c7f8ffdbe645}</Project>
<Name>DiscImageChef.Server</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="EntityFramework" Version="6.2.0" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -0,0 +1,180 @@
using System;
using System.Data.Entity.Migrations;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using DiscImageChef.Server.Migrations;
using DiscImageChef.Server.Models;
namespace DiscImageChef.Server.Task
{
class Program
{
public static void Main(string[] args)
{
DateTime start, end;
Console.WriteLine("{0}: Migrating database to latest version...", DateTime.UtcNow);
start = DateTime.UtcNow;
Configuration migratorConfig = new Configuration();
DbMigrator dbMigrator = new DbMigrator(migratorConfig);
dbMigrator.Update();
end = DateTime.UtcNow;
Console.WriteLine("{0}: Took {1:F2} seconds", DateTime.UtcNow, (end - start).TotalSeconds);
start = DateTime.UtcNow;
Console.WriteLine("{0}: Connecting to database...", DateTime.UtcNow);
DicServerContext ctx = new DicServerContext();
end = DateTime.UtcNow;
Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
try
{
Console.WriteLine("{0}: Retrieving USB IDs from Linux USB...", DateTime.UtcNow);
start = DateTime.UtcNow;
WebClient client = new WebClient();
StringReader sr = new StringReader(client.DownloadString("http://www.linux-usb.org/usb.ids"));
end = DateTime.UtcNow;
Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
UsbVendor vendor = null;
int newVendors = 0;
int newProducts = 0;
int modifiedVendors = 0;
int modifiedProducts = 0;
int counter = 0;
start = DateTime.UtcNow;
Console.WriteLine("{0}: Adding and updating database entries...", DateTime.UtcNow);
do
{
if(counter == 1000)
{
DateTime start2 = DateTime.UtcNow;
Console.WriteLine("{0}: Saving changes", start2);
ctx.SaveChanges();
end = DateTime.UtcNow;
Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start2).TotalSeconds);
counter = 0;
}
string line = sr.ReadLine();
if(line is null) break;
if(line.Length == 0 || line[0] == '#') continue;
ushort number;
string name;
if(line[0] == '\t')
{
try { number = Convert.ToUInt16(line.Substring(1, 4), 16); }
catch(FormatException) { continue; }
if(number == 0) continue;
name = line.Substring(7);
UsbProduct product =
ctx.UsbProducts.FirstOrDefault(p => p.ProductId == number && p.Vendor != null &&
p.Vendor.VendorId == vendor.VendorId);
if(product is null)
{
product = new UsbProduct(vendor, number, name);
ctx.UsbProducts.Add(product);
Console.WriteLine("{0}: Will add product {1} with ID {2:X4} and vendor {3} ({4:X4})",
DateTime.UtcNow, product.Product, product.ProductId,
product.Vendor?.Vendor ?? "null", product.Vendor?.VendorId ?? 0);
newProducts++;
counter++;
}
else if(name != product.Product)
{
Console
.WriteLine("{0}: Will modify product with ID {1:X4} and vendor {2} ({3:X4}) from \"{4}\" to \"{5}\"",
DateTime.UtcNow, product.ProductId, product.Vendor?.Vendor ?? "null",
product.Vendor?.VendorId ?? 0,
product.Product, name);
product.Product = name;
product.ModifiedWhen = DateTime.UtcNow;
modifiedProducts++;
counter++;
}
continue;
}
try { number = Convert.ToUInt16(line.Substring(0, 4), 16); }
catch(FormatException) { continue; }
if(number == 0) continue;
name = line.Substring(6);
vendor = ctx.UsbVendors.FirstOrDefault(v => v.VendorId == number);
if(vendor is null)
{
vendor = new UsbVendor(number, name);
ctx.UsbVendors.Add(vendor);
Console.WriteLine("{0}: Will add vendor {1} with ID {2:X4}", DateTime.UtcNow, vendor.Vendor,
vendor.VendorId);
newVendors++;
counter++;
}
else if(name != vendor.Vendor)
{
Console.WriteLine("{0}: Will modify vendor with ID {1:X4} from \"{2}\" to \"{3}\"",
DateTime.UtcNow, vendor.VendorId, vendor.Vendor, name);
vendor.Vendor = name;
vendor.ModifiedWhen = DateTime.UtcNow;
modifiedVendors++;
counter++;
}
}
while(true);
end = DateTime.UtcNow;
Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
Console.WriteLine("{0}: Saving database changes...", DateTime.UtcNow);
start = DateTime.UtcNow;
ctx.SaveChanges();
end = DateTime.UtcNow;
Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
Console.WriteLine("{0}: {1} vendors added.", DateTime.UtcNow, newVendors);
Console.WriteLine("{0}: {1} products added.", DateTime.UtcNow, newProducts);
Console.WriteLine("{0}: {1} vendors modified.", DateTime.UtcNow, modifiedVendors);
Console.WriteLine("{0}: {1} products modified.", DateTime.UtcNow, modifiedProducts);
Console.WriteLine("{0}: Looking up a vendor", DateTime.UtcNow);
start = DateTime.UtcNow;
vendor = ctx.UsbVendors.FirstOrDefault(v => v.VendorId == 0x8086);
if(vendor is null) Console.WriteLine("{0}: Error, could not find vendor.", DateTime.UtcNow);
else
Console.WriteLine("{0}: Found {1}.", DateTime.UtcNow,
vendor.Vendor);
end = DateTime.UtcNow;
Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
Console.WriteLine("{0}: Looking up a product", DateTime.UtcNow);
start = DateTime.UtcNow;
UsbProduct prd =
ctx.UsbProducts.FirstOrDefault(p => p.ProductId == 0x0001 && p.Vendor.VendorId == 0x8086);
if(prd is null) Console.WriteLine("{0}: Error, could not find product.", DateTime.UtcNow);
else Console.WriteLine("{0}: Found {1}.", DateTime.UtcNow, prd.Product);
end = DateTime.UtcNow;
Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
}
catch(Exception ex)
{
#if DEBUG
if(Debugger.IsAttached) throw;
#endif
Console.WriteLine("{0}: Exception {1} filling USB IDs...", DateTime.UtcNow, ex);
}
}
}
}

View File

@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("DiscImageChef.Server.Task")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("DiscImageChef.Server.Task")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("79D6A54E-47B6-4D0A-ABEB-4FA7B5ADF4B2")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -291,7 +291,6 @@
<None Include="int10h.org\.htaccess" /> <None Include="int10h.org\.htaccess" />
<None Include="int10h.org\vga_squarepx.woff2" /> <None Include="int10h.org\vga_squarepx.woff2" />
<None Include="Reports\.htaccess" /> <None Include="Reports\.htaccess" />
<None Include="usb.ids" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <None Include="packages.config" />

View File

@@ -3,7 +3,7 @@ using DiscImageChef.Server.Models;
namespace DiscImageChef.Server.Migrations namespace DiscImageChef.Server.Migrations
{ {
sealed class Configuration : DbMigrationsConfiguration<DicServerContext> public sealed class Configuration : DbMigrationsConfiguration<DicServerContext>
{ {
public Configuration() public Configuration()
{ {

View File

@@ -32,16 +32,13 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Web; using System.Web;
using System.Web.Hosting;
using System.Web.UI; using System.Web.UI;
using DiscImageChef.CommonTypes.Metadata;
using DiscImageChef.Decoders.PCMCIA; using DiscImageChef.Decoders.PCMCIA;
using DiscImageChef.Decoders.SCSI; using DiscImageChef.Decoders.SCSI;
using DiscImageChef.Server.App_Start;
using DiscImageChef.Server.Models; using DiscImageChef.Server.Models;
using TestedMedia = DiscImageChef.CommonTypes.Metadata.TestedMedia;
using Tuple = DiscImageChef.Decoders.PCMCIA.Tuple; using Tuple = DiscImageChef.Decoders.PCMCIA.Tuple;
namespace DiscImageChef.Server namespace DiscImageChef.Server
@@ -76,8 +73,25 @@ namespace DiscImageChef.Server
if(report.USB != null) if(report.USB != null)
{ {
GetUsbDescriptions(report.USB.VendorID, report.USB.ProductID, out string usbVendorDescription, string usbVendorDescription = null;
out string usbProductDescription); string usbProductDescription = null;
UsbProduct dbProduct =
ctx.UsbProducts.FirstOrDefault(p => p.ProductId == report.USB.ProductID &&
p.Vendor != null &&
p.Vendor.VendorId == report.USB.VendorID);
if(dbProduct is null)
{
UsbVendor dbVendor = ctx.UsbVendors.FirstOrDefault(v => v.VendorId == report.USB.VendorID);
if(!(dbVendor is null)) usbVendorDescription = dbVendor.Vendor;
}
else
{
usbProductDescription = dbProduct.Product;
usbVendorDescription = dbProduct.Vendor.Vendor;
}
lblUsbManufacturer.Text = HttpUtility.HtmlEncode(report.USB.Manufacturer); lblUsbManufacturer.Text = HttpUtility.HtmlEncode(report.USB.Manufacturer);
lblUsbProduct.Text = HttpUtility.HtmlEncode(report.USB.Product); lblUsbProduct.Text = HttpUtility.HtmlEncode(report.USB.Product);
@@ -497,54 +511,5 @@ namespace DiscImageChef.Server
#endif #endif
} }
} }
static void GetUsbDescriptions(ushort vendor, ushort product, out string vendorDescription,
out string productDescription)
{
vendorDescription = null;
productDescription = null;
if(!File.Exists(Path.Combine(HostingEnvironment.MapPath("~") ?? throw new InvalidOperationException(),
"usb.ids"))) return;
StreamReader tocStream =
new StreamReader(Path.Combine(HostingEnvironment.MapPath("~") ?? throw new InvalidOperationException(),
"usb.ids"));
bool inManufacturer = false;
while(tocStream.Peek() >= 0)
{
string line = tocStream.ReadLine();
if(line == null) break;
if(line.Length == 0 || line[0] == '#') continue;
ushort number;
if(inManufacturer)
{
// Finished with the manufacturer
if(line[0] != '\t') return;
number = Convert.ToUInt16(line.Substring(1, 4), 16);
if(number != product) continue;
productDescription = line.Substring(7);
return;
}
// Skip products
if(line[0] == '\t') continue;
try { number = Convert.ToUInt16(line.Substring(0, 4), 16); }
catch(FormatException) { continue; }
if(number != vendor) continue;
vendorDescription = line.Substring(6);
inManufacturer = true;
}
}
} }
} }

View File

@@ -0,0 +1,30 @@
<?xml version="1.0"?>
<!-- For more information on using Web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=301874 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an attribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
-->
<system.web>
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your Web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
</configuration>

View File

@@ -0,0 +1,31 @@
<?xml version="1.0"?>
<!-- For more information on using Web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=301874 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an attribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
-->
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your Web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
</configuration>

File diff suppressed because it is too large Load Diff

View File

@@ -43,6 +43,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscImageChef.EntityFramewo
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiscImageChef.Server", "DiscImageChef.Server\DiscImageChef.Server.csproj", "{911ADDF5-E5FA-445D-88CD-C7F8FFDBE645}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiscImageChef.Server", "DiscImageChef.Server\DiscImageChef.Server.csproj", "{911ADDF5-E5FA-445D-88CD-C7F8FFDBE645}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiscImageChef.Server.Task", "DiscImageChef.Server.Task\DiscImageChef.Server.Task.csproj", "{79D6A54E-47B6-4D0A-ABEB-4FA7B5ADF4B2}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -211,6 +213,14 @@ Global
{911ADDF5-E5FA-445D-88CD-C7F8FFDBE645}.Release|Any CPU.Build.0 = Release|Any CPU {911ADDF5-E5FA-445D-88CD-C7F8FFDBE645}.Release|Any CPU.Build.0 = Release|Any CPU
{911ADDF5-E5FA-445D-88CD-C7F8FFDBE645}.Release|x86.ActiveCfg = Release|Any CPU {911ADDF5-E5FA-445D-88CD-C7F8FFDBE645}.Release|x86.ActiveCfg = Release|Any CPU
{911ADDF5-E5FA-445D-88CD-C7F8FFDBE645}.Release|x86.Build.0 = Release|Any CPU {911ADDF5-E5FA-445D-88CD-C7F8FFDBE645}.Release|x86.Build.0 = Release|Any CPU
{79D6A54E-47B6-4D0A-ABEB-4FA7B5ADF4B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{79D6A54E-47B6-4D0A-ABEB-4FA7B5ADF4B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{79D6A54E-47B6-4D0A-ABEB-4FA7B5ADF4B2}.Debug|x86.ActiveCfg = Debug|Any CPU
{79D6A54E-47B6-4D0A-ABEB-4FA7B5ADF4B2}.Debug|x86.Build.0 = Debug|Any CPU
{79D6A54E-47B6-4D0A-ABEB-4FA7B5ADF4B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79D6A54E-47B6-4D0A-ABEB-4FA7B5ADF4B2}.Release|Any CPU.Build.0 = Release|Any CPU
{79D6A54E-47B6-4D0A-ABEB-4FA7B5ADF4B2}.Release|x86.ActiveCfg = Release|Any CPU
{79D6A54E-47B6-4D0A-ABEB-4FA7B5ADF4B2}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE