net-predictable/loc.go

75 lines
1.3 KiB
Go
Raw Normal View History

package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
const (
PathProcPCI = iota
PathProcUSB
)
func (i *iface) ProcLoc() (err error) {
spath := strings.Join([]string{i.netpath, "device"}, "/")
var real string
if real, err = filepath.EvalSymlinks(spath); err != nil {
if errors.Is(err, os.ErrNotExist) {
err = fmt.Errorf("Interface %s isn't a hardware device", i.names[IfNameKern])
return
}
err = fmt.Errorf("Failed to resolve symlink to device: %w", err)
return
}
var devpath string
var ok bool
sysdev := strings.Join([]string{SYSFS, "devices/"}, "/")
if devpath, ok = strings.CutPrefix(real, sysdev); !ok {
err = fmt.Errorf("Symlink doesn't point to %s\n", sysdev)
return
}
var (
pci, usb, tmp string
procmode int
)
for _, v := range strings.Split(devpath, "/") {
if strings.HasPrefix(v, "pci") {
procmode = PathProcPCI
continue
} else if strings.HasPrefix(v, "usb") {
procmode = PathProcUSB
continue
}
switch procmode {
case PathProcPCI:
{
if tmp, err = i.ProcPCI(v); err != nil {
continue
}
pci = tmp
}
case PathProcUSB:
{
if tmp, err = i.ProcUSB(v); err != nil {
continue
}
usb = tmp
}
}
}
if pci == "" && usb == "" {
return
}
i.names[IfNameLoc] = strings.Join([]string{i.pfx, pci, usb}, "")
return
}