net-predictable/loc.go

70 lines
1.2 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 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 pci, err = i.ProcPCI(v); err != nil {
continue
}
}
case PathProcUSB:
{
if usb, err = i.ProcUSB(v); err != nil {
continue
}
}
}
}
i.names[IfNameLoc] = strings.Join([]string{i.pfx, pci, usb}, "")
return
}