|
| 1 | +// +build !linux,!windows |
| 2 | + |
| 3 | +/* |
| 4 | + Copyright The containerd Authors. |
| 5 | +
|
| 6 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + you may not use this file except in compliance with the License. |
| 8 | + You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package oci |
| 20 | + |
| 21 | +import ( |
| 22 | + "os" |
| 23 | + |
| 24 | + specs "github.com/opencontainers/runtime-spec/specs-go" |
| 25 | + "golang.org/x/sys/unix" |
| 26 | +) |
| 27 | + |
| 28 | +func deviceFromPath(path, permissions string) (*specs.LinuxDevice, error) { |
| 29 | + var stat unix.Stat_t |
| 30 | + if err := unix.Lstat(path, &stat); err != nil { |
| 31 | + return nil, err |
| 32 | + } |
| 33 | + |
| 34 | + var ( |
| 35 | + devNumber = uint64(stat.Rdev) |
| 36 | + major = unix.Major(devNumber) |
| 37 | + minor = unix.Minor(devNumber) |
| 38 | + ) |
| 39 | + if major == 0 { |
| 40 | + return nil, ErrNotADevice |
| 41 | + } |
| 42 | + |
| 43 | + var ( |
| 44 | + devType string |
| 45 | + mode = stat.Mode |
| 46 | + ) |
| 47 | + switch { |
| 48 | + case mode&unix.S_IFBLK == unix.S_IFBLK: |
| 49 | + devType = "b" |
| 50 | + case mode&unix.S_IFCHR == unix.S_IFCHR: |
| 51 | + devType = "c" |
| 52 | + } |
| 53 | + fm := os.FileMode(mode) |
| 54 | + return &specs.LinuxDevice{ |
| 55 | + Type: devType, |
| 56 | + Path: path, |
| 57 | + Major: int64(major), |
| 58 | + Minor: int64(minor), |
| 59 | + FileMode: &fm, |
| 60 | + UID: &stat.Uid, |
| 61 | + GID: &stat.Gid, |
| 62 | + }, nil |
| 63 | +} |
0 commit comments