Make get devices function public

Signed-off-by: sashayakovtseva <sasha@sylabs.io>
This commit is contained in:
sashayakovtseva 2019-08-15 17:16:38 +03:00
parent 2e94378464
commit afc24792dc
1 changed files with 10 additions and 6 deletions

View File

@ -7,11 +7,11 @@ import (
"path/filepath" "path/filepath"
"github.com/opencontainers/runc/libcontainer/configs" "github.com/opencontainers/runc/libcontainer/configs"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
var ( var (
// ErrNotADevice denotes that a file is not a valid linux device.
ErrNotADevice = errors.New("not a device node") ErrNotADevice = errors.New("not a device node")
) )
@ -21,7 +21,8 @@ var (
ioutilReadDir = ioutil.ReadDir ioutilReadDir = ioutil.ReadDir
) )
// Given the path to a device and its cgroup_permissions(which cannot be easily queried) look up the information about a linux device and return that information as a Device struct. // Given the path to a device and its cgroup_permissions(which cannot be easily queried) look up the
// information about a linux device and return that information as a Device struct.
func DeviceFromPath(path, permissions string) (*configs.Device, error) { func DeviceFromPath(path, permissions string) (*configs.Device, error) {
var stat unix.Stat_t var stat unix.Stat_t
err := unixLstat(path, &stat) err := unixLstat(path, &stat)
@ -60,16 +61,19 @@ func DeviceFromPath(path, permissions string) (*configs.Device, error) {
}, nil }, nil
} }
// HostDevices returns all devices that can be found under /dev directory.
func HostDevices() ([]*configs.Device, error) { func HostDevices() ([]*configs.Device, error) {
return getDevices("/dev") return GetDevices("/dev")
} }
func getDevices(path string) ([]*configs.Device, error) { // GetDevices recursively traverses a directory specified by path
// and returns all devices found there.
func GetDevices(path string) ([]*configs.Device, error) {
files, err := ioutilReadDir(path) files, err := ioutilReadDir(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
out := []*configs.Device{} var out []*configs.Device
for _, f := range files { for _, f := range files {
switch { switch {
case f.IsDir(): case f.IsDir():
@ -79,7 +83,7 @@ func getDevices(path string) ([]*configs.Device, error) {
case "pts", "shm", "fd", "mqueue", ".lxc", ".lxd-mounts", ".udev": case "pts", "shm", "fd", "mqueue", ".lxc", ".lxd-mounts", ".udev":
continue continue
default: default:
sub, err := getDevices(filepath.Join(path, f.Name())) sub, err := GetDevices(filepath.Join(path, f.Name()))
if err != nil { if err != nil {
return nil, err return nil, err
} }