libcontainer: one more switch from syscall to x/sys/unix
Refactor DeviceFromPath in order to get rid of package syscall and directly use the functions from x/sys/unix. This also allows to get rid of the conversion from the OS-independent file mode values (from the os package) to Linux specific values and instead let's us use the raw file mode value directly. Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
This commit is contained in:
parent
c5ec254876
commit
b0d014d0e1
|
@ -2,11 +2,9 @@ package devices
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"syscall" //only for Stat_t
|
|
||||||
|
|
||||||
"github.com/opencontainers/runc/libcontainer/configs"
|
"github.com/opencontainers/runc/libcontainer/configs"
|
||||||
|
|
||||||
|
@ -19,45 +17,41 @@ var (
|
||||||
|
|
||||||
// Testing dependencies
|
// Testing dependencies
|
||||||
var (
|
var (
|
||||||
osLstat = os.Lstat
|
unixLstat = unix.Lstat
|
||||||
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) {
|
||||||
fileInfo, err := osLstat(path)
|
var stat unix.Stat_t
|
||||||
|
err := unixLstat(path, &stat)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var (
|
var (
|
||||||
devType rune
|
devType rune
|
||||||
mode = fileInfo.Mode()
|
mode = stat.Mode
|
||||||
fileModePermissionBits = os.FileMode.Perm(mode)
|
|
||||||
)
|
)
|
||||||
switch {
|
switch {
|
||||||
case mode&os.ModeDevice == 0:
|
case mode&unix.S_IFBLK != 0:
|
||||||
return nil, ErrNotADevice
|
devType = 'b'
|
||||||
case mode&os.ModeCharDevice != 0:
|
case mode&unix.S_IFCHR != 0:
|
||||||
fileModePermissionBits |= unix.S_IFCHR
|
|
||||||
devType = 'c'
|
devType = 'c'
|
||||||
default:
|
default:
|
||||||
fileModePermissionBits |= unix.S_IFBLK
|
return nil, ErrNotADevice
|
||||||
devType = 'b'
|
|
||||||
}
|
}
|
||||||
stat_t, ok := fileInfo.Sys().(*syscall.Stat_t)
|
devNumber := int(stat.Rdev)
|
||||||
if !ok {
|
uid := stat.Uid
|
||||||
return nil, fmt.Errorf("cannot determine the device number for device %s", path)
|
gid := stat.Gid
|
||||||
}
|
|
||||||
devNumber := int(stat_t.Rdev)
|
|
||||||
return &configs.Device{
|
return &configs.Device{
|
||||||
Type: devType,
|
Type: devType,
|
||||||
Path: path,
|
Path: path,
|
||||||
Major: Major(devNumber),
|
Major: Major(devNumber),
|
||||||
Minor: Minor(devNumber),
|
Minor: Minor(devNumber),
|
||||||
Permissions: permissions,
|
Permissions: permissions,
|
||||||
FileMode: fileModePermissionBits,
|
FileMode: os.FileMode(mode),
|
||||||
Uid: stat_t.Uid,
|
Uid: uid,
|
||||||
Gid: stat_t.Gid,
|
Gid: gid,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,14 +6,16 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDeviceFromPathLstatFailure(t *testing.T) {
|
func TestDeviceFromPathLstatFailure(t *testing.T) {
|
||||||
testError := errors.New("test error")
|
testError := errors.New("test error")
|
||||||
|
|
||||||
// Override os.Lstat to inject error.
|
// Override unix.Lstat to inject error.
|
||||||
osLstat = func(path string) (os.FileInfo, error) {
|
unixLstat = func(path string, stat *unix.Stat_t) error {
|
||||||
return nil, testError
|
return testError
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := DeviceFromPath("", "")
|
_, err := DeviceFromPath("", "")
|
||||||
|
|
Loading…
Reference in New Issue