From a146b48cfe7569aa65de001fac9156603dcf9394 Mon Sep 17 00:00:00 2001 From: Ganawa Juanah Date: Fri, 17 Jan 2025 14:40:00 -0600 Subject: [PATCH] move parent check outside of readMACFromFile --- pkg/sysfsnet/interfaces_linux.go | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/pkg/sysfsnet/interfaces_linux.go b/pkg/sysfsnet/interfaces_linux.go index 10bb37cb..e3805772 100644 --- a/pkg/sysfsnet/interfaces_linux.go +++ b/pkg/sysfsnet/interfaces_linux.go @@ -24,20 +24,6 @@ func Interfaces() ([]Interface, error) { readMACFromFile := func(s string) (string, error) { - // Check if parent exists and is a directory - // parent := filepath.Dir(s) - // info, err := os.Stat(parent) - // if err != nil { - // if os.IsNotExist(err) { - // return "", nil - // } - // return "", err - // } - - // if !info.IsDir() { - // return "", nil - // } - f, err := os.Open(s) if os.IsNotExist(err) { @@ -56,11 +42,20 @@ func Interfaces() ([]Interface, error) { for _, dentry := range dents { - if !dentry.IsDir() { + entryPath := filepath.Join(sysClassNet, dentry.Name()) + + // os.Stat to follow symlinks and return the info of the target + info, err := os.Stat(entryPath) + if err != nil { + continue + } + + // os.FileInfo.IsDir to check if the entry is a directory + if !info.IsDir() { continue } - hwText, err := readMACFromFile(filepath.Join(sysClassNet, dentry.Name(), "address")) + hwText, err := readMACFromFile(filepath.Join(entryPath, "address")) if os.IsNotExist(err) { continue }