Files

26 lines
895 B
Go
Raw Permalink Normal View History

// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
// SPDX-License-Identifier: MIT
package nfsfs
import "syscall"
// mknod creates a device node. It needs CAP_MKNOD on Linux, and a failure
// of permission is reported as such rather than as an io error. The kind
// is picked before the single call: a second mknod over an existing node
// fails EEXIST and leaves the wrong kind behind.
func mknod(path string, spec CreateSpec, perm uint32) error {
dev := int(makedev(spec.Major, spec.Minor))
kind := uint32(syscall.S_IFCHR)
if spec.Kind == KindBlk {
kind = syscall.S_IFBLK
}
return syscall.Mknod(path, perm|kind, dev)
}
// makedev assembles a device number the way the kernel expects it.
func makedev(major, minor uint32) uint64 {
return uint64(minor&0xff) | uint64(major&0xfff)<<8 |
uint64(minor&0xfff00)<<12 | uint64(major&0xfffff000)<<32
}