...

Source file src/github.com/containerd/continuity/devices/devices_unix.go

Documentation: github.com/containerd/continuity/devices

     1  //go:build !windows
     2  // +build !windows
     3  
     4  /*
     5     Copyright The containerd Authors.
     6  
     7     Licensed under the Apache License, Version 2.0 (the "License");
     8     you may not use this file except in compliance with the License.
     9     You may obtain a copy of the License at
    10  
    11         http://www.apache.org/licenses/LICENSE-2.0
    12  
    13     Unless required by applicable law or agreed to in writing, software
    14     distributed under the License is distributed on an "AS IS" BASIS,
    15     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16     See the License for the specific language governing permissions and
    17     limitations under the License.
    18  */
    19  
    20  package devices
    21  
    22  import (
    23  	"fmt"
    24  	"os"
    25  	"syscall"
    26  
    27  	"golang.org/x/sys/unix"
    28  )
    29  
    30  func DeviceInfo(fi os.FileInfo) (uint64, uint64, error) {
    31  	sys, ok := fi.Sys().(*syscall.Stat_t)
    32  	if !ok {
    33  		return 0, 0, fmt.Errorf("cannot extract device from os.FileInfo")
    34  	}
    35  
    36  	//nolint:unconvert
    37  	dev := uint64(sys.Rdev)
    38  	return uint64(unix.Major(dev)), uint64(unix.Minor(dev)), nil
    39  }
    40  
    41  // mknod provides a shortcut for syscall.Mknod
    42  func Mknod(p string, mode os.FileMode, maj, min int) error {
    43  	var (
    44  		m   = syscallMode(mode.Perm())
    45  		dev uint64
    46  	)
    47  
    48  	if mode&os.ModeDevice != 0 {
    49  		dev = unix.Mkdev(uint32(maj), uint32(min))
    50  
    51  		if mode&os.ModeCharDevice != 0 {
    52  			m |= unix.S_IFCHR
    53  		} else {
    54  			m |= unix.S_IFBLK
    55  		}
    56  	} else if mode&os.ModeNamedPipe != 0 {
    57  		m |= unix.S_IFIFO
    58  	}
    59  
    60  	return mknod(p, m, dev)
    61  }
    62  
    63  // syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
    64  func syscallMode(i os.FileMode) (o uint32) {
    65  	o |= uint32(i.Perm())
    66  	if i&os.ModeSetuid != 0 {
    67  		o |= unix.S_ISUID
    68  	}
    69  	if i&os.ModeSetgid != 0 {
    70  		o |= unix.S_ISGID
    71  	}
    72  	if i&os.ModeSticky != 0 {
    73  		o |= unix.S_ISVTX
    74  	}
    75  	return
    76  }
    77  

View as plain text