You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dev_linux.go 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Functions to access/create device major and minor numbers matching the
  5. // encoding used by the Linux kernel and glibc.
  6. //
  7. // The information below is extracted and adapted from bits/sysmacros.h in the
  8. // glibc sources:
  9. //
  10. // dev_t in glibc is 64-bit, with 32-bit major and minor numbers. glibc's
  11. // default encoding is MMMM Mmmm mmmM MMmm, where M is a hex digit of the major
  12. // number and m is a hex digit of the minor number. This is backward compatible
  13. // with legacy systems where dev_t is 16 bits wide, encoded as MMmm. It is also
  14. // backward compatible with the Linux kernel, which for some architectures uses
  15. // 32-bit dev_t, encoded as mmmM MMmm.
  16. package unix
  17. // Major returns the major component of a Linux device number.
  18. func Major(dev uint64) uint32 {
  19. major := uint32((dev & 0x00000000000fff00) >> 8)
  20. major |= uint32((dev & 0xfffff00000000000) >> 32)
  21. return major
  22. }
  23. // Minor returns the minor component of a Linux device number.
  24. func Minor(dev uint64) uint32 {
  25. minor := uint32((dev & 0x00000000000000ff) >> 0)
  26. minor |= uint32((dev & 0x00000ffffff00000) >> 12)
  27. return minor
  28. }
  29. // Mkdev returns a Linux device number generated from the given major and minor
  30. // components.
  31. func Mkdev(major, minor uint32) uint64 {
  32. dev := (uint64(major) & 0x00000fff) << 8
  33. dev |= (uint64(major) & 0xfffff000) << 32
  34. dev |= (uint64(minor) & 0x000000ff) << 0
  35. dev |= (uint64(minor) & 0xffffff00) << 12
  36. return dev
  37. }