feat(parser): linux-generic content-driven parser and RAR archive support
Field bundles from ad-hoc service scripts (first sample: Giga G894-SD3 .rar with dmidecode/lscpu/lspci/nvme/lsblk output, no fixed layout). ADL-078: - linux-generic: recognizes utility output by content, merges records by stable key, low detect confidence (20-30) below all vendor parsers - pciids.LookupByDescription: resolve a textual lspci description back to vendor/device IDs and manufacturer/model - archive: accept .rar via the in-repo internal/rar5 reader (RAR 5 / RAR 7, single volume, solid; encrypted, multi-volume, SFX and RAR 4.x rejected), same per-file and total limits as tar/zip - UI: .rar in the upload accept list Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ff5b99fa5c
commit
b4b15e58a2
25 files changed
+4901
-5
No files matched your search
+79
@@ -5,6 +5,7 @@ import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -165,3 +166,81 @@ func parsePCIIDs(r interface{ Read([]byte) (int, error) }, outVendors map[int]st
|
||||
outVendors[currentVendor] = name
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
reverseOnce sync.Once
|
||||
vendorIDsByName map[string][]int
|
||||
deviceIDsByName map[int]map[string]int
|
||||
ambiguousDeviceID = -1
|
||||
)
|
||||
|
||||
func loadReverseIndex() {
|
||||
loadPCIIDs()
|
||||
reverseOnce.Do(func() {
|
||||
vendorIDsByName = make(map[string][]int, len(vendors))
|
||||
for id, name := range vendors {
|
||||
vendorIDsByName[name] = append(vendorIDsByName[name], id)
|
||||
}
|
||||
for _, ids := range vendorIDsByName {
|
||||
sort.Ints(ids)
|
||||
}
|
||||
|
||||
deviceIDsByName = make(map[int]map[string]int)
|
||||
for key, name := range devices {
|
||||
vendorID, err1 := strconv.ParseInt(key[:4], 16, 32)
|
||||
deviceID, err2 := strconv.ParseInt(key[5:], 16, 32)
|
||||
if err1 != nil || err2 != nil {
|
||||
continue
|
||||
}
|
||||
byName := deviceIDsByName[int(vendorID)]
|
||||
if byName == nil {
|
||||
byName = make(map[string]int)
|
||||
deviceIDsByName[int(vendorID)] = byName
|
||||
}
|
||||
if prev, seen := byName[name]; seen && prev != int(deviceID) {
|
||||
byName[name] = ambiguousDeviceID
|
||||
continue
|
||||
}
|
||||
byName[name] = int(deviceID)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// LookupByDescription resolves a textual `lspci` device description
|
||||
// ("<vendor name> <device name>", printed without numeric IDs) back to PCI
|
||||
// IDs. The vendor is the longest pci.ids vendor name that prefixes the
|
||||
// description; deviceName is the remainder. deviceID is resolved from an
|
||||
// `lspci` "Device 1a2b" placeholder or from a device name that is unique
|
||||
// within the vendor, and is 0 otherwise. ok is false when no vendor matches.
|
||||
func LookupByDescription(description string) (vendorID, deviceID int, vendorName, deviceName string, ok bool) {
|
||||
loadReverseIndex()
|
||||
description = strings.TrimSpace(description)
|
||||
|
||||
for name := range vendorIDsByName {
|
||||
if len(name) <= len(vendorName) || !strings.HasPrefix(description, name) {
|
||||
continue
|
||||
}
|
||||
if len(description) > len(name) && description[len(name)] != ' ' {
|
||||
continue
|
||||
}
|
||||
vendorName = name
|
||||
}
|
||||
if vendorName == "" {
|
||||
return 0, 0, "", description, false
|
||||
}
|
||||
deviceName = strings.TrimSpace(description[len(vendorName):])
|
||||
candidates := vendorIDsByName[vendorName]
|
||||
vendorID = candidates[0]
|
||||
|
||||
if hex, found := strings.CutPrefix(deviceName, "Device "); found && len(hex) == 4 {
|
||||
if id, err := strconv.ParseInt(hex, 16, 32); err == nil {
|
||||
return vendorID, int(id), vendorName, deviceName, true
|
||||
}
|
||||
}
|
||||
for _, candidate := range candidates {
|
||||
if id, found := deviceIDsByName[candidate][deviceName]; found && id != ambiguousDeviceID {
|
||||
return candidate, id, vendorName, deviceName, true
|
||||
}
|
||||
}
|
||||
return vendorID, 0, vendorName, deviceName, true
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package pciids
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLookupByDescription(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
idsPath := filepath.Join(dir, "pci.ids")
|
||||
content := "" +
|
||||
"fff0 Example Devices\n" +
|
||||
"\t0001 Widget 100 [W100]\n" +
|
||||
"\t0002 Twin Port Adapter\n" +
|
||||
"\t0003 Twin Port Adapter\n" +
|
||||
"fff1 Example Devices, Inc. [EXD]\n" +
|
||||
"\t0010 Gadget\n"
|
||||
if err := os.WriteFile(idsPath, []byte(content), 0o644); err != nil {
|
||||
t.Fatalf("write pci.ids: %v", err)
|
||||
}
|
||||
|
||||
t.Setenv("LOGPILE_PCI_IDS_PATH", idsPath)
|
||||
reset := func() {
|
||||
loadOnce = sync.Once{}
|
||||
reverseOnce = sync.Once{}
|
||||
vendors = nil
|
||||
devices = nil
|
||||
}
|
||||
reset()
|
||||
t.Cleanup(reset)
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
description string
|
||||
wantOK bool
|
||||
wantVendor int
|
||||
wantDevice int
|
||||
wantName string
|
||||
wantModel string
|
||||
}{
|
||||
{
|
||||
name: "unique device name",
|
||||
description: "Example Devices Widget 100 [W100]",
|
||||
wantOK: true, wantVendor: 0xfff0, wantDevice: 0x0001,
|
||||
wantName: "Example Devices", wantModel: "Widget 100 [W100]",
|
||||
},
|
||||
{
|
||||
name: "longest vendor name wins",
|
||||
description: "Example Devices, Inc. [EXD] Gadget",
|
||||
wantOK: true, wantVendor: 0xfff1, wantDevice: 0x0010,
|
||||
wantName: "Example Devices, Inc. [EXD]", wantModel: "Gadget",
|
||||
},
|
||||
{
|
||||
name: "lspci placeholder carries the device id",
|
||||
description: "Example Devices Device 0abc",
|
||||
wantOK: true, wantVendor: 0xfff0, wantDevice: 0x0abc,
|
||||
wantName: "Example Devices", wantModel: "Device 0abc",
|
||||
},
|
||||
{
|
||||
name: "ambiguous device name keeps the id unknown",
|
||||
description: "Example Devices Twin Port Adapter",
|
||||
wantOK: true, wantVendor: 0xfff0, wantDevice: 0,
|
||||
wantName: "Example Devices", wantModel: "Twin Port Adapter",
|
||||
},
|
||||
{
|
||||
name: "vendor name must end at a word boundary",
|
||||
description: "Example DevicesX Widget",
|
||||
wantOK: false, wantModel: "Example DevicesX Widget",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
vendorID, deviceID, vendorName, deviceName, ok := LookupByDescription(tc.description)
|
||||
if ok != tc.wantOK || vendorID != tc.wantVendor || deviceID != tc.wantDevice ||
|
||||
vendorName != tc.wantName || deviceName != tc.wantModel {
|
||||
t.Fatalf("LookupByDescription(%q) = %04x:%04x %q %q ok=%v", tc.description, vendorID, deviceID, vendorName, deviceName, ok)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user