@@ -2,11 +2,9 @@ package devices
2
2
3
3
import (
4
4
"errors"
5
- "fmt"
6
5
"io/ioutil"
7
6
"os"
8
7
"path/filepath"
9
- "syscall" //only for Stat_t
10
8
11
9
"github.com/opencontainers/runc/libcontainer/configs"
12
10
@@ -19,45 +17,41 @@ var (
19
17
20
18
// Testing dependencies
21
19
var (
22
- osLstat = os .Lstat
20
+ unixLstat = unix .Lstat
23
21
ioutilReadDir = ioutil .ReadDir
24
22
)
25
23
26
24
// 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.
27
25
func DeviceFromPath (path , permissions string ) (* configs.Device , error ) {
28
- fileInfo , err := osLstat (path )
26
+ var stat unix.Stat_t
27
+ err := unixLstat (path , & stat )
29
28
if err != nil {
30
29
return nil , err
31
30
}
32
31
var (
33
- devType rune
34
- mode = fileInfo .Mode ()
35
- fileModePermissionBits = os .FileMode .Perm (mode )
32
+ devType rune
33
+ mode = stat .Mode
36
34
)
37
35
switch {
38
- case mode & os .ModeDevice == 0 :
39
- return nil , ErrNotADevice
40
- case mode & os .ModeCharDevice != 0 :
41
- fileModePermissionBits |= unix .S_IFCHR
36
+ case mode & unix .S_IFBLK != 0 :
37
+ devType = 'b'
38
+ case mode & unix .S_IFCHR != 0 :
42
39
devType = 'c'
43
40
default :
44
- fileModePermissionBits |= unix .S_IFBLK
45
- devType = 'b'
46
- }
47
- stat_t , ok := fileInfo .Sys ().(* syscall.Stat_t )
48
- if ! ok {
49
- return nil , fmt .Errorf ("cannot determine the device number for device %s" , path )
41
+ return nil , ErrNotADevice
50
42
}
51
- devNumber := int (stat_t .Rdev )
43
+ devNumber := int (stat .Rdev )
44
+ uid := stat .Uid
45
+ gid := stat .Gid
52
46
return & configs.Device {
53
47
Type : devType ,
54
48
Path : path ,
55
49
Major : Major (devNumber ),
56
50
Minor : Minor (devNumber ),
57
51
Permissions : permissions ,
58
- FileMode : fileModePermissionBits ,
59
- Uid : stat_t . Uid ,
60
- Gid : stat_t . Gid ,
52
+ FileMode : os . FileMode ( mode ) ,
53
+ Uid : uid ,
54
+ Gid : gid ,
61
55
}, nil
62
56
}
63
57
0 commit comments