github.com/demonoid81/containerd@v1.3.4/oci/spec_opts_linux.go (about) 1 // +build linux 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 // The type is 32bit on mips. 36 devNumber = uint64(stat.Rdev) // nolint: unconvert 37 major = unix.Major(devNumber) 38 minor = unix.Minor(devNumber) 39 ) 40 if major == 0 { 41 return nil, ErrNotADevice 42 } 43 44 var ( 45 devType string 46 mode = stat.Mode 47 ) 48 switch { 49 case mode&unix.S_IFBLK == unix.S_IFBLK: 50 devType = "b" 51 case mode&unix.S_IFCHR == unix.S_IFCHR: 52 devType = "c" 53 } 54 fm := os.FileMode(mode) 55 return &specs.LinuxDevice{ 56 Type: devType, 57 Path: path, 58 Major: int64(major), 59 Minor: int64(minor), 60 FileMode: &fm, 61 UID: &stat.Uid, 62 GID: &stat.Gid, 63 }, nil 64 }