Skip to content

Commit

Permalink
export oci.DeviceFromPath()
Browse files Browse the repository at this point in the history
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 <github@gone.nl>
  • Loading branch information
thaJeztah committed Dec 2, 2021
1 parent 16c233b commit f3195b3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion oci/spec_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion oci/spec_opts_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
15 changes: 9 additions & 6 deletions oci/utils_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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{
Expand Down

0 comments on commit f3195b3

Please sign in to comment.