From 30293c98d079c5732f12f40f99bfa687752cec68 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 8 Dec 2025 00:11:09 +0000 Subject: [PATCH] Bump github.com/jaypipes/ghw from 0.19.1 to 0.21.0 Bumps [github.com/jaypipes/ghw](https://github.com/jaypipes/ghw) from 0.19.1 to 0.21.0. - [Release notes](https://github.com/jaypipes/ghw/releases) - [Commits](https://github.com/jaypipes/ghw/compare/v0.19.1...v0.21.0) --- updated-dependencies: - dependency-name: github.com/jaypipes/ghw dependency-version: 0.21.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 4 +- vendor/github.com/jaypipes/ghw/alias.go | 5 + vendor/github.com/jaypipes/ghw/host.go | 11 +- .../jaypipes/ghw/pkg/block/block.go | 2 +- .../jaypipes/ghw/pkg/block/block_windows.go | 5 +- .../jaypipes/ghw/pkg/linuxpath/path_linux.go | 4 + vendor/github.com/jaypipes/ghw/pkg/pci/pci.go | 41 ++++--- .../jaypipes/ghw/pkg/pci/pci_linux.go | 31 +++++ .../jaypipes/ghw/pkg/snapshot/clonetree.go | 1 + .../ghw/pkg/snapshot/clonetree_pci_linux.go | 1 + .../ghw/pkg/snapshot/clonetree_stub.go | 4 + .../ghw/pkg/snapshot/clonetree_usb_linux.go | 45 +++++++ vendor/github.com/jaypipes/ghw/pkg/usb/usb.go | 115 ++++++++++++++++++ .../jaypipes/ghw/pkg/usb/usb_linux.go | 115 ++++++++++++++++++ .../jaypipes/ghw/pkg/usb/usb_stub.go | 19 +++ vendor/modules.txt | 3 +- 17 files changed, 384 insertions(+), 24 deletions(-) create mode 100644 vendor/github.com/jaypipes/ghw/pkg/snapshot/clonetree_usb_linux.go create mode 100644 vendor/github.com/jaypipes/ghw/pkg/usb/usb.go create mode 100644 vendor/github.com/jaypipes/ghw/pkg/usb/usb_linux.go create mode 100644 vendor/github.com/jaypipes/ghw/pkg/usb/usb_stub.go diff --git a/go.mod b/go.mod index 25686f11d8b..3a9c0e52ddb 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/distribution/distribution v2.8.1+incompatible github.com/google/go-containerregistry v0.20.3 github.com/hashicorp/go-multierror v1.1.1 - github.com/jaypipes/ghw v0.19.1 + github.com/jaypipes/ghw v0.21.0 github.com/joho/godotenv v1.5.1 github.com/mitchellh/mapstructure v1.5.0 github.com/onsi/ginkgo/v2 v2.27.2 diff --git a/go.sum b/go.sum index 78e65b31811..953a4603bd8 100644 --- a/go.sum +++ b/go.sum @@ -153,8 +153,8 @@ github.com/itchyny/gojq v0.12.16 h1:yLfgLxhIr/6sJNVmYfQjTIv0jGctu6/DgDoivmxTr7g= github.com/itchyny/gojq v0.12.16/go.mod h1:6abHbdC2uB9ogMS38XsErnfqJ94UlngIJGlRAIj4jTM= github.com/itchyny/timefmt-go v0.1.6 h1:ia3s54iciXDdzWzwaVKXZPbiXzxxnv1SPGFfM/myJ5Q= github.com/itchyny/timefmt-go v0.1.6/go.mod h1:RRDZYC5s9ErkjQvTvvU7keJjxUYzIISJGxm9/mAERQg= -github.com/jaypipes/ghw v0.19.1 h1:Lhybk6aadgEJqIxeS0h07UOL/EgMGIdxbAy6V8J7RgY= -github.com/jaypipes/ghw v0.19.1/go.mod h1:GPrvwbtPoxYUenr74+nAnWbardIZq600vJDD5HnPsPE= +github.com/jaypipes/ghw v0.21.0 h1:ClG2xWtYY0c1ud9jZYwVGdSgfCI7AbmZmZyw3S5HHz8= +github.com/jaypipes/ghw v0.21.0/go.mod h1:GPrvwbtPoxYUenr74+nAnWbardIZq600vJDD5HnPsPE= github.com/jaypipes/pcidb v1.1.1 h1:QmPhpsbmmnCwZmHeYAATxEaoRuiMAJusKYkUncMC0ro= github.com/jaypipes/pcidb v1.1.1/go.mod h1:x27LT2krrUgjf875KxQXKB0Ha/YXLdZRVmw6hH0G7g8= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= diff --git a/vendor/github.com/jaypipes/ghw/alias.go b/vendor/github.com/jaypipes/ghw/alias.go index 9c403d9694d..2909bead8f2 100644 --- a/vendor/github.com/jaypipes/ghw/alias.go +++ b/vendor/github.com/jaypipes/ghw/alias.go @@ -21,6 +21,7 @@ import ( pciaddress "github.com/jaypipes/ghw/pkg/pci/address" "github.com/jaypipes/ghw/pkg/product" "github.com/jaypipes/ghw/pkg/topology" + "github.com/jaypipes/ghw/pkg/usb" ) type WithOption = option.Option @@ -68,6 +69,10 @@ var ( Memory = memory.New ) +var ( + USB = usb.New +) + type BlockInfo = block.Info type Disk = block.Disk type Partition = block.Partition diff --git a/vendor/github.com/jaypipes/ghw/host.go b/vendor/github.com/jaypipes/ghw/host.go index 89b1ad2776d..034798d6f48 100644 --- a/vendor/github.com/jaypipes/ghw/host.go +++ b/vendor/github.com/jaypipes/ghw/host.go @@ -10,6 +10,7 @@ import ( "fmt" "github.com/jaypipes/ghw/pkg/context" + "github.com/jaypipes/ghw/pkg/usb" "github.com/jaypipes/ghw/pkg/accelerator" "github.com/jaypipes/ghw/pkg/baseboard" @@ -42,6 +43,7 @@ type HostInfo struct { Baseboard *baseboard.Info `json:"baseboard"` Product *product.Info `json:"product"` PCI *pci.Info `json:"pci"` + USB *usb.Info `json:"usb"` } // Host returns a pointer to a HostInfo struct that contains fields with @@ -97,6 +99,11 @@ func Host(opts ...*WithOption) (*HostInfo, error) { if err != nil { return nil, err } + usbInfo, err := usb.New(opts...) + if err != nil { + return nil, err + } + return &HostInfo{ ctx: ctx, CPU: cpuInfo, @@ -111,6 +118,7 @@ func Host(opts ...*WithOption) (*HostInfo, error) { Baseboard: baseboardInfo, Product: productInfo, PCI: pciInfo, + USB: usbInfo, }, nil } @@ -118,7 +126,7 @@ func Host(opts ...*WithOption) (*HostInfo, error) { // structs' String-ified output func (info *HostInfo) String() string { return fmt.Sprintf( - "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", + "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", info.Block.String(), info.CPU.String(), info.GPU.String(), @@ -131,6 +139,7 @@ func (info *HostInfo) String() string { info.Baseboard.String(), info.Product.String(), info.PCI.String(), + info.USB.String(), ) } diff --git a/vendor/github.com/jaypipes/ghw/pkg/block/block.go b/vendor/github.com/jaypipes/ghw/pkg/block/block.go index 088060185fb..016b5c7de49 100644 --- a/vendor/github.com/jaypipes/ghw/pkg/block/block.go +++ b/vendor/github.com/jaypipes/ghw/pkg/block/block.go @@ -251,7 +251,7 @@ type Disk struct { type Partition struct { // Disk is a pointer to the `Disk` struct that houses this partition. Disk *Disk `json:"-"` - // Name is the system name given to the partition, e.g. "sda1". + // Name is the system given or user given name to the partition, e.g. "sda1". Name string `json:"name"` // Label is the human-readable label given to the partition. On Linux, this // is derived from the `ID_PART_ENTRY_NAME` udev entry. diff --git a/vendor/github.com/jaypipes/ghw/pkg/block/block_windows.go b/vendor/github.com/jaypipes/ghw/pkg/block/block_windows.go index 09f44a3a18e..cbb052a853e 100644 --- a/vendor/github.com/jaypipes/ghw/pkg/block/block_windows.go +++ b/vendor/github.com/jaypipes/ghw/pkg/block/block_windows.go @@ -85,7 +85,7 @@ type win32LogicalDiskToPartition struct { Dependent *string } -const wqlLogicalDisk = "SELECT Caption, CreationClassName, Description, DeviceID, FileSystem, FreeSpace, Name, Size, SystemName, VolumeSerialNumber FROM Win32_LogicalDisk" +const wqlLogicalDisk = "SELECT Caption, CreationClassName, Description, DeviceID, FileSystem, FreeSpace, Name, Size, SystemName, VolumeName, VolumeSerialNumber FROM Win32_LogicalDisk" type win32LogicalDisk struct { Caption *string @@ -97,6 +97,7 @@ type win32LogicalDisk struct { Name *string Size *uint64 SystemName *string + VolumeName *string VolumeSerialNumber *string } @@ -175,7 +176,7 @@ func (i *Info) load() error { // Appending Partition p := &Partition{ Disk: disk, - Name: strings.TrimSpace(*logicaldisk.Caption), + Name: strings.TrimSpace(*logicaldisk.VolumeName), Label: strings.TrimSpace(*logicaldisk.Caption), SizeBytes: *logicaldisk.Size, MountPoint: *logicaldisk.DeviceID, diff --git a/vendor/github.com/jaypipes/ghw/pkg/linuxpath/path_linux.go b/vendor/github.com/jaypipes/ghw/pkg/linuxpath/path_linux.go index bbe81b64efa..17c0c147d65 100644 --- a/vendor/github.com/jaypipes/ghw/pkg/linuxpath/path_linux.go +++ b/vendor/github.com/jaypipes/ghw/pkg/linuxpath/path_linux.go @@ -56,6 +56,7 @@ func PathRootsFromContext(ctx *context.Context) PathRoots { } type Paths struct { + SysRoot string VarLog string ProcMeminfo string ProcCpuinfo string @@ -66,6 +67,7 @@ type Paths struct { SysDevicesSystemMemory string SysDevicesSystemCPU string SysBusPciDevices string + SysBusUsbDevices string SysClassDRM string SysClassDMI string SysClassNet string @@ -77,6 +79,7 @@ type Paths struct { func New(ctx *context.Context) *Paths { roots := PathRootsFromContext(ctx) return &Paths{ + SysRoot: filepath.Join(ctx.Chroot, roots.Sys), VarLog: filepath.Join(ctx.Chroot, roots.Var, "log"), ProcMeminfo: filepath.Join(ctx.Chroot, roots.Proc, "meminfo"), ProcCpuinfo: filepath.Join(ctx.Chroot, roots.Proc, "cpuinfo"), @@ -87,6 +90,7 @@ func New(ctx *context.Context) *Paths { SysDevicesSystemMemory: filepath.Join(ctx.Chroot, roots.Sys, "devices", "system", "memory"), SysDevicesSystemCPU: filepath.Join(ctx.Chroot, roots.Sys, "devices", "system", "cpu"), SysBusPciDevices: filepath.Join(ctx.Chroot, roots.Sys, "bus", "pci", "devices"), + SysBusUsbDevices: filepath.Join(ctx.Chroot, roots.Sys, "bus", "usb", "devices"), SysClassDRM: filepath.Join(ctx.Chroot, roots.Sys, "class", "drm"), SysClassDMI: filepath.Join(ctx.Chroot, roots.Sys, "class", "dmi"), SysClassNet: filepath.Join(ctx.Chroot, roots.Sys, "class", "net"), diff --git a/vendor/github.com/jaypipes/ghw/pkg/pci/pci.go b/vendor/github.com/jaypipes/ghw/pkg/pci/pci.go index 2453597d733..c27928ca0db 100644 --- a/vendor/github.com/jaypipes/ghw/pkg/pci/pci.go +++ b/vendor/github.com/jaypipes/ghw/pkg/pci/pci.go @@ -21,11 +21,13 @@ import ( type Device struct { // The PCI address of the device - Address string `json:"address"` - Vendor *pcidb.Vendor `json:"vendor"` - Product *pcidb.Product `json:"product"` - Revision string `json:"revision"` - Subsystem *pcidb.Product `json:"subsystem"` + Address string `json:"address"` + // The PCI address of the parent device + ParentAddress string `json:"parent_address"` + Vendor *pcidb.Vendor `json:"vendor"` + Product *pcidb.Product `json:"product"` + Revision string `json:"revision"` + Subsystem *pcidb.Product `json:"subsystem"` // optional subvendor/sub-device information Class *pcidb.Class `json:"class"` // optional sub-class for the device @@ -36,6 +38,9 @@ type Device struct { // architecture is not NUMA. Node *topology.Node `json:"node,omitempty"` Driver string `json:"driver"` + // for IOMMU Groups see also: + // https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/7/html/virtualization_deployment_and_administration_guide/sect-iommu-deep-dive + IOMMUGroup string `json:"iommu_group"` } type devIdent struct { @@ -44,15 +49,17 @@ type devIdent struct { } type devMarshallable struct { - Driver string `json:"driver"` - Address string `json:"address"` - Vendor devIdent `json:"vendor"` - Product devIdent `json:"product"` - Revision string `json:"revision"` - Subsystem devIdent `json:"subsystem"` - Class devIdent `json:"class"` - Subclass devIdent `json:"subclass"` - Interface devIdent `json:"programming_interface"` + Driver string `json:"driver"` + Address string `json:"address"` + ParentAddress string `json:"parent_address"` + Vendor devIdent `json:"vendor"` + Product devIdent `json:"product"` + Revision string `json:"revision"` + Subsystem devIdent `json:"subsystem"` + Class devIdent `json:"class"` + Subclass devIdent `json:"subclass"` + Interface devIdent `json:"programming_interface"` + IOMMUGroup string `json:"iommu_group"` } // NOTE(jaypipes) Device has a custom JSON marshaller because we don't want @@ -61,8 +68,9 @@ type devMarshallable struct { // human-readable name of the vendor, product, class, etc. func (d *Device) MarshalJSON() ([]byte, error) { dm := devMarshallable{ - Driver: d.Driver, - Address: d.Address, + Driver: d.Driver, + Address: d.Address, + ParentAddress: d.ParentAddress, Vendor: devIdent{ ID: d.Vendor.ID, Name: d.Vendor.Name, @@ -88,6 +96,7 @@ func (d *Device) MarshalJSON() ([]byte, error) { ID: d.ProgrammingInterface.ID, Name: d.ProgrammingInterface.Name, }, + IOMMUGroup: d.IOMMUGroup, } return json.Marshal(dm) } diff --git a/vendor/github.com/jaypipes/ghw/pkg/pci/pci_linux.go b/vendor/github.com/jaypipes/ghw/pkg/pci/pci_linux.go index 0bdf659d072..23068a8d8b5 100644 --- a/vendor/github.com/jaypipes/ghw/pkg/pci/pci_linux.go +++ b/vendor/github.com/jaypipes/ghw/pkg/pci/pci_linux.go @@ -95,6 +95,35 @@ func getDeviceNUMANode(ctx *context.Context, pciAddr *pciaddr.Address) *topology } } +func getDeviceIommuGroup(ctx *context.Context, pciAddr *pciaddr.Address) string { + paths := linuxpath.New(ctx) + iommuGroupPath := filepath.Join(paths.SysBusPciDevices, pciAddr.String(), "iommu_group") + + dest, err := os.Readlink(iommuGroupPath) + if err != nil { + return "" + } + return filepath.Base(dest) +} + +func getDeviceParentAddress(ctx *context.Context, pciAddr *pciaddr.Address) string { + paths := linuxpath.New(ctx) + devPath := filepath.Join(paths.SysBusPciDevices, pciAddr.String()) + + dest, err := os.Readlink(devPath) + if err != nil { + return "" + } + + parentAddr := filepath.Base(filepath.Dir(dest)) + + if pciaddr.FromString(parentAddr) == nil { + return "" + } + + return parentAddr +} + func getDeviceDriver(ctx *context.Context, pciAddr *pciaddr.Address) string { paths := linuxpath.New(ctx) driverPath := filepath.Join(paths.SysBusPciDevices, pciAddr.String(), "driver") @@ -331,6 +360,8 @@ func (info *Info) GetDevice(address string) *Device { device.Node = getDeviceNUMANode(info.ctx, pciAddr) } device.Driver = getDeviceDriver(info.ctx, pciAddr) + device.ParentAddress = getDeviceParentAddress(info.ctx, pciAddr) + device.IOMMUGroup = getDeviceIommuGroup(info.ctx, pciAddr) return device } diff --git a/vendor/github.com/jaypipes/ghw/pkg/snapshot/clonetree.go b/vendor/github.com/jaypipes/ghw/pkg/snapshot/clonetree.go index 020e7e673f7..b509aff5471 100644 --- a/vendor/github.com/jaypipes/ghw/pkg/snapshot/clonetree.go +++ b/vendor/github.com/jaypipes/ghw/pkg/snapshot/clonetree.go @@ -40,6 +40,7 @@ func CloneTreeInto(scratchDir string) error { func ExpectedCloneContent() []string { fileSpecs := ExpectedCloneStaticContent() fileSpecs = append(fileSpecs, ExpectedCloneNetContent()...) + fileSpecs = append(fileSpecs, ExpectedCloneUSBContent()...) fileSpecs = append(fileSpecs, ExpectedClonePCIContent()...) fileSpecs = append(fileSpecs, ExpectedCloneGPUContent()...) return fileSpecs diff --git a/vendor/github.com/jaypipes/ghw/pkg/snapshot/clonetree_pci_linux.go b/vendor/github.com/jaypipes/ghw/pkg/snapshot/clonetree_pci_linux.go index e7aa7d265de..0c2c68b3959 100644 --- a/vendor/github.com/jaypipes/ghw/pkg/snapshot/clonetree_pci_linux.go +++ b/vendor/github.com/jaypipes/ghw/pkg/snapshot/clonetree_pci_linux.go @@ -62,6 +62,7 @@ func scanPCIDeviceRoot(root string) (fileSpecs []string, pciRoots []string) { "class", "device", "driver", + "iommu_group", "irq", "local_cpulist", "modalias", diff --git a/vendor/github.com/jaypipes/ghw/pkg/snapshot/clonetree_stub.go b/vendor/github.com/jaypipes/ghw/pkg/snapshot/clonetree_stub.go index af85a55b5d8..39b8cd9a73b 100644 --- a/vendor/github.com/jaypipes/ghw/pkg/snapshot/clonetree_stub.go +++ b/vendor/github.com/jaypipes/ghw/pkg/snapshot/clonetree_stub.go @@ -28,3 +28,7 @@ func ExpectedCloneNetContent() []string { func ExpectedClonePCIContent() []string { return []string{} } + +func ExpectedCloneUSBContent() []string { + return []string{} +} diff --git a/vendor/github.com/jaypipes/ghw/pkg/snapshot/clonetree_usb_linux.go b/vendor/github.com/jaypipes/ghw/pkg/snapshot/clonetree_usb_linux.go new file mode 100644 index 00000000000..323c88bc7df --- /dev/null +++ b/vendor/github.com/jaypipes/ghw/pkg/snapshot/clonetree_usb_linux.go @@ -0,0 +1,45 @@ +// +// Use and distribution licensed under the Apache license version 2. +// +// See the COPYING file in the root project directory for full text. +// + +package snapshot + +import ( + "os" + "path/filepath" +) + +// ExpectedCloneUSBContent returns a slice of strings pertaning to the USB interfaces +func ExpectedCloneUSBContent() []string { + const sysBusUSB = "/sys/bus/usb/devices/" + + paths := []string{sysBusUSB} + usbDevicesDirs, err := os.ReadDir(sysBusUSB) + if err != nil { + return []string{} + } + + for _, dir := range usbDevicesDirs { + susBusUSBLink := filepath.Join(sysBusUSB, dir.Name()) + paths = append(paths, susBusUSBLink) + + fullDir, err := os.Readlink(susBusUSBLink) + if err != nil { + continue + } + if !filepath.IsAbs(fullDir) { + fullDir, err = filepath.Abs(filepath.Join(sysBusUSB, fullDir)) + if err != nil { + continue + } + } + for _, fileName := range []string{"uevent", "interface", "product"} { + paths = append(paths, filepath.Join(fullDir, fileName)) + } + + } + + return paths +} diff --git a/vendor/github.com/jaypipes/ghw/pkg/usb/usb.go b/vendor/github.com/jaypipes/ghw/pkg/usb/usb.go new file mode 100644 index 00000000000..4418f0d359a --- /dev/null +++ b/vendor/github.com/jaypipes/ghw/pkg/usb/usb.go @@ -0,0 +1,115 @@ +// Use and distribution licensed under the Apache license version 2. +// +// See the COPYING file in the root project directory for full text. +// + +package usb + +import ( + "fmt" + "strings" + + "github.com/jaypipes/ghw/pkg/context" + "github.com/jaypipes/ghw/pkg/marshal" + "github.com/jaypipes/ghw/pkg/option" +) + +type Device struct { + Driver string `json:"driver"` + Type string `json:"type"` + VendorID string `json:"vendor_id"` + ProductID string `json:"product_id"` + Product string `json:"product"` + RevisionID string `json:"revision_id"` + Interface string `json:"interface"` +} + +func (d Device) String() string { + kvs := []struct { + name string + value string + }{ + {"driver", d.Driver}, + {"type", d.Type}, + {"vendorID", d.VendorID}, + {"productID", d.ProductID}, + {"product", d.Product}, + {"revisionID", d.RevisionID}, + {"interface", d.Interface}, + } + + var str strings.Builder + + i := 0 + for _, s := range kvs { + k := s.name + v := s.value + + if v == "" { + continue + } + needsQuotationMarks := strings.ContainsAny(v, " \t") + + if i > 0 { + str.WriteString(" ") + } + i++ + str.WriteString(k) + str.WriteString("=") + if needsQuotationMarks { + str.WriteString("\"") + } + str.WriteString(v) + if needsQuotationMarks { + str.WriteString("\"") + } + + } + + return str.String() +} + +// Info describes all network interface controllers (NICs) in the host system. +type Info struct { + ctx *context.Context + Devices []*Device `json:"devices"` +} + +// String returns a short string with information about the networking on the +// host system. +func (i *Info) String() string { + return fmt.Sprintf( + "USB (%d USBs)", + len(i.Devices), + ) +} + +// New returns a pointer to an Info struct that contains information about the +// network interface controllers (NICs) on the host system +func New(opts ...*option.Option) (*Info, error) { + ctx := context.New(opts...) + info := &Info{ctx: ctx} + if err := ctx.Do(info.load); err != nil { + return nil, err + } + + return info, nil +} + +// simple private struct used to encapsulate usb information in a +// top-level "usb" YAML/JSON map/object key +type usbPrinter struct { + Info *Info `json:"usb"` +} + +// YAMLString returns a string with the net information formatted as YAML +// under a top-level "net:" key +func (i *Info) YAMLString() string { + return marshal.SafeYAML(i.ctx, usbPrinter{i}) +} + +// JSONString returns a string with the net information formatted as JSON +// under a top-level "net:" key +func (i *Info) JSONString(indent bool) string { + return marshal.SafeJSON(i.ctx, usbPrinter{i}, indent) +} diff --git a/vendor/github.com/jaypipes/ghw/pkg/usb/usb_linux.go b/vendor/github.com/jaypipes/ghw/pkg/usb/usb_linux.go new file mode 100644 index 00000000000..14dd6ae47fb --- /dev/null +++ b/vendor/github.com/jaypipes/ghw/pkg/usb/usb_linux.go @@ -0,0 +1,115 @@ +// Use and distribution licensed under the Apache license version 2. +// +// See the COPYING file in the root project directory for full text. +// + +package usb + +import ( + "bufio" + "bytes" + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/jaypipes/ghw/pkg/context" + "github.com/jaypipes/ghw/pkg/linuxpath" +) + +func (i *Info) load() error { + var errs []error + + i.Devices, errs = usbs(i.ctx) + + if len(errs) == 0 { + return nil + } + return fmt.Errorf("error(s) happened during reading usb info: %+v", errs) +} + +func fillUSBFromUevent(dir string, dev *Device) (err error) { + ueventFp, err := os.Open(filepath.Join(dir, "uevent")) + if err != nil { + return + } + defer func() { + err = ueventFp.Close() + }() + + sc := bufio.NewScanner(ueventFp) + for sc.Scan() { + line := sc.Text() + + splits := strings.SplitN(line, "=", 2) + if len(splits) != 2 { + continue + } + + key := strings.ToUpper(splits[0]) + val := splits[1] + + switch key { + case "DRIVER": + dev.Driver = val + case "TYPE": + dev.Type = val + case "PRODUCT": + splits := strings.SplitN(val, "/", 3) + if len(splits) != 3 { + continue + } + dev.VendorID = splits[0] + dev.ProductID = splits[1] + dev.RevisionID = splits[2] + } + } + return nil +} + +func slurp(path string) string { + bs, err := os.ReadFile(path) + if err != nil { + return "" + } + + return string(bytes.TrimSpace(bs)) +} + +func usbs(ctx *context.Context) ([]*Device, []error) { + devs := make([]*Device, 0) + errs := []error{} + + paths := linuxpath.New(ctx) + usbDevicesDirs, err := os.ReadDir(paths.SysBusUsbDevices) + if err != nil { + return devs, []error{err} + } + + for _, dir := range usbDevicesDirs { + fullDir, err := os.Readlink(filepath.Join(paths.SysBusUsbDevices, dir.Name())) + if err != nil { + continue + } + if !filepath.IsAbs(fullDir) { + fullDir, err = filepath.Abs(filepath.Join(paths.SysBusUsbDevices, fullDir)) + if err != nil { + continue + } + } + + dev := Device{} + + err = fillUSBFromUevent(fullDir, &dev) + if err != nil { + errs = append(errs, err) + } + + dev.Interface = slurp(filepath.Join(fullDir, "interface")) + dev.Product = slurp(filepath.Join(fullDir, "product")) + + devs = append(devs, &dev) + } + + return devs, errs +} diff --git a/vendor/github.com/jaypipes/ghw/pkg/usb/usb_stub.go b/vendor/github.com/jaypipes/ghw/pkg/usb/usb_stub.go new file mode 100644 index 00000000000..3c1ccdee9da --- /dev/null +++ b/vendor/github.com/jaypipes/ghw/pkg/usb/usb_stub.go @@ -0,0 +1,19 @@ +//go:build !linux +// +build !linux + +// Use and distribution licensed under the Apache license version 2. +// +// See the COPYING file in the root project directory for full text. +// + +package usb + +import ( + "runtime" + + "errors" +) + +func (i *Info) load() error { + return errors.New("usb load not implemented on " + runtime.GOOS) +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 1fa1ea55487..a6430aac3ac 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -252,7 +252,7 @@ github.com/itchyny/gojq # github.com/itchyny/timefmt-go v0.1.6 ## explicit; go 1.20 github.com/itchyny/timefmt-go -# github.com/jaypipes/ghw v0.19.1 +# github.com/jaypipes/ghw v0.21.0 ## explicit; go 1.21 github.com/jaypipes/ghw github.com/jaypipes/ghw/pkg/accelerator @@ -275,6 +275,7 @@ github.com/jaypipes/ghw/pkg/product github.com/jaypipes/ghw/pkg/snapshot github.com/jaypipes/ghw/pkg/topology github.com/jaypipes/ghw/pkg/unitutil +github.com/jaypipes/ghw/pkg/usb github.com/jaypipes/ghw/pkg/util # github.com/jaypipes/pcidb v1.1.1 ## explicit; go 1.21