-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change moves linux specific functions to _linux.go files. This enables basic builds and tests on other platforms. Signed-off-by: Evan Lezar <[email protected]>
- Loading branch information
Showing
6 changed files
with
85 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cgroups | ||
|
||
import ( | ||
"encoding/binary" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func getID(path string) uint64 { | ||
h, _, err := unix.NameToHandleAt(unix.AT_FDCWD, path, 0) | ||
if err != nil { | ||
return 0 | ||
} | ||
|
||
return binary.LittleEndian.Uint64(h.Bytes()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package cgroups | ||
|
||
func getID(path string) uint64 { | ||
panic("not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package topology | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"syscall" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// FindSysFsDevice for given argument returns physical device where it is linked to. | ||
// For device nodes it will return path for device itself. For regular files or directories | ||
// this function returns physical device where this inode resides (storage device). | ||
// If result device is a virtual one (e.g. tmpfs), error will be returned. | ||
// For non-existing path, no error returned and path is empty. | ||
func FindSysFsDevice(dev string) (string, error) { | ||
fi, err := os.Stat(dev) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return "", nil | ||
} | ||
return "", fmt.Errorf("unable to get stat for %s: %w", dev, err) | ||
} | ||
|
||
devType := "block" | ||
rdev := fi.Sys().(*syscall.Stat_t).Dev | ||
if mode := fi.Mode(); mode&os.ModeDevice != 0 { | ||
rdev = fi.Sys().(*syscall.Stat_t).Rdev | ||
if mode&os.ModeCharDevice != 0 { | ||
devType = "char" | ||
} | ||
} | ||
|
||
major := int64(unix.Major(rdev)) | ||
minor := int64(unix.Minor(rdev)) | ||
if major == 0 { | ||
return "", fmt.Errorf("%s is a virtual device node: %w", dev, err) | ||
} | ||
|
||
realDevPath, err := findSysFsDevice(devType, major, minor) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to find sysfs device for %s: %w", dev, err) | ||
} | ||
|
||
return realDevPath, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package topology | ||
|
||
import "errors" | ||
|
||
// FindSysFsDevice for given argument returns physical device where it is linked to. | ||
// For device nodes it will return path for device itself. For regular files or directories | ||
// this function returns physical device where this inode resides (storage device). | ||
// If result device is a virtual one (e.g. tmpfs), error will be returned. | ||
// For non-existing path, no error returned and path is empty. | ||
func FindSysFsDevice(dev string) (string, error) { | ||
return "", errors.New("not implemented") | ||
} |