From f3195b3b51adfc8c2389e3c914cf3c21d2dc67a5 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 1 Dec 2021 17:05:42 +0100 Subject: [PATCH] export oci.DeviceFromPath() This will help to reduce the amount of runc/libcontainer code that's used in Moby / Docker Engine (in favor of using the containerd implementation). Signed-off-by: Sebastiaan van Stijn --- oci/spec_opts.go | 2 +- oci/spec_opts_windows.go | 2 +- oci/utils_unix.go | 15 +++++++++------ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/oci/spec_opts.go b/oci/spec_opts.go index cbdf41584..1f57737be 100644 --- a/oci/spec_opts.go +++ b/oci/spec_opts.go @@ -1213,7 +1213,7 @@ func WithLinuxDevice(path, permissions string) SpecOpts { setLinux(s) setResources(s) - dev, err := deviceFromPath(path) + dev, err := DeviceFromPath(path) if err != nil { return err } diff --git a/oci/spec_opts_windows.go b/oci/spec_opts_windows.go index a289162ba..3adef1d88 100644 --- a/oci/spec_opts_windows.go +++ b/oci/spec_opts_windows.go @@ -72,6 +72,6 @@ func WithHostDevices(_ context.Context, _ Client, _ *containers.Container, s *Sp return nil } -func deviceFromPath(path string) (*specs.LinuxDevice, error) { +func DeviceFromPath(path string) (*specs.LinuxDevice, error) { return nil, errors.New("device from path not supported on Windows") } diff --git a/oci/utils_unix.go b/oci/utils_unix.go index 874cc941a..7d42901e3 100644 --- a/oci/utils_unix.go +++ b/oci/utils_unix.go @@ -28,7 +28,8 @@ import ( "golang.org/x/sys/unix" ) -var errNotADevice = errors.New("not a device node") +// ErrNotADevice denotes that a file is not a valid linux device. +var ErrNotADevice = errors.New("not a device node") // HostDevices returns all devices that can be found under /dev directory. func HostDevices() ([]specs.LinuxDevice, error) { @@ -42,7 +43,7 @@ func getDevices(path, containerPath string) ([]specs.LinuxDevice, error) { } if !stat.IsDir() { - dev, err := deviceFromPath(path) + dev, err := DeviceFromPath(path) if err != nil { return nil, err } @@ -81,9 +82,9 @@ func getDevices(path, containerPath string) ([]specs.LinuxDevice, error) { case f.Name() == "console": continue } - device, err := deviceFromPath(filepath.Join(path, f.Name())) + device, err := DeviceFromPath(filepath.Join(path, f.Name())) if err != nil { - if err == errNotADevice { + if err == ErrNotADevice { continue } if os.IsNotExist(err) { @@ -110,7 +111,9 @@ const ( fifoDevice = "p" ) -func deviceFromPath(path string) (*specs.LinuxDevice, error) { +// DeviceFromPath takes the path to a device to look up the information about a +// linux device and returns that information as a LinuxDevice struct. +func DeviceFromPath(path string) (*specs.LinuxDevice, error) { var stat unix.Stat_t if err := unix.Lstat(path, &stat); err != nil { return nil, err @@ -135,7 +138,7 @@ func deviceFromPath(path string) (*specs.LinuxDevice, error) { case unix.S_IFIFO: devType = fifoDevice default: - return nil, errNotADevice + return nil, ErrNotADevice } fm := os.FileMode(mode &^ unix.S_IFMT) return &specs.LinuxDevice{