Blame vendor/golang.org/x/sys/unix/dev_linux.go

Packit 63bb0d
// Copyright 2017 The Go Authors. All rights reserved.
Packit 63bb0d
// Use of this source code is governed by a BSD-style
Packit 63bb0d
// license that can be found in the LICENSE file.
Packit 63bb0d
Packit 63bb0d
// Functions to access/create device major and minor numbers matching the
Packit 63bb0d
// encoding used by the Linux kernel and glibc.
Packit 63bb0d
//
Packit 63bb0d
// The information below is extracted and adapted from bits/sysmacros.h in the
Packit 63bb0d
// glibc sources:
Packit 63bb0d
//
Packit 63bb0d
// dev_t in glibc is 64-bit, with 32-bit major and minor numbers. glibc's
Packit 63bb0d
// default encoding is MMMM Mmmm mmmM MMmm, where M is a hex digit of the major
Packit 63bb0d
// number and m is a hex digit of the minor number. This is backward compatible
Packit 63bb0d
// with legacy systems where dev_t is 16 bits wide, encoded as MMmm. It is also
Packit 63bb0d
// backward compatible with the Linux kernel, which for some architectures uses
Packit 63bb0d
// 32-bit dev_t, encoded as mmmM MMmm.
Packit 63bb0d
Packit 63bb0d
package unix
Packit 63bb0d
Packit 63bb0d
// Major returns the major component of a Linux device number.
Packit 63bb0d
func Major(dev uint64) uint32 {
Packit 63bb0d
	major := uint32((dev & 0x00000000000fff00) >> 8)
Packit 63bb0d
	major |= uint32((dev & 0xfffff00000000000) >> 32)
Packit 63bb0d
	return major
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// Minor returns the minor component of a Linux device number.
Packit 63bb0d
func Minor(dev uint64) uint32 {
Packit 63bb0d
	minor := uint32((dev & 0x00000000000000ff) >> 0)
Packit 63bb0d
	minor |= uint32((dev & 0x00000ffffff00000) >> 12)
Packit 63bb0d
	return minor
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// Mkdev returns a Linux device number generated from the given major and minor
Packit 63bb0d
// components.
Packit 63bb0d
func Mkdev(major, minor uint32) uint64 {
Packit 63bb0d
	dev := (uint64(major) & 0x00000fff) << 8
Packit 63bb0d
	dev |= (uint64(major) & 0xfffff000) << 32
Packit 63bb0d
	dev |= (uint64(minor) & 0x000000ff) << 0
Packit 63bb0d
	dev |= (uint64(minor) & 0xffffff00) << 12
Packit 63bb0d
	return dev
Packit 63bb0d
}