]>
Commit | Line | Data |
---|---|---|
15c0b25d AP |
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 | ||
5 | // Functions to access/create device major and minor numbers matching the | |
6 | // encoding used by the Linux kernel and glibc. | |
7 | // | |
8 | // The information below is extracted and adapted from bits/sysmacros.h in the | |
9 | // glibc sources: | |
10 | // | |
11 | // dev_t in glibc is 64-bit, with 32-bit major and minor numbers. glibc's | |
12 | // default encoding is MMMM Mmmm mmmM MMmm, where M is a hex digit of the major | |
13 | // number and m is a hex digit of the minor number. This is backward compatible | |
14 | // with legacy systems where dev_t is 16 bits wide, encoded as MMmm. It is also | |
15 | // backward compatible with the Linux kernel, which for some architectures uses | |
16 | // 32-bit dev_t, encoded as mmmM MMmm. | |
17 | ||
18 | package unix | |
19 | ||
20 | // Major returns the major component of a Linux device number. | |
21 | func Major(dev uint64) uint32 { | |
22 | major := uint32((dev & 0x00000000000fff00) >> 8) | |
23 | major |= uint32((dev & 0xfffff00000000000) >> 32) | |
24 | return major | |
25 | } | |
26 | ||
27 | // Minor returns the minor component of a Linux device number. | |
28 | func Minor(dev uint64) uint32 { | |
29 | minor := uint32((dev & 0x00000000000000ff) >> 0) | |
30 | minor |= uint32((dev & 0x00000ffffff00000) >> 12) | |
31 | return minor | |
32 | } | |
33 | ||
34 | // Mkdev returns a Linux device number generated from the given major and minor | |
35 | // components. | |
36 | func Mkdev(major, minor uint32) uint64 { | |
37 | dev := (uint64(major) & 0x00000fff) << 8 | |
38 | dev |= (uint64(major) & 0xfffff000) << 32 | |
39 | dev |= (uint64(minor) & 0x000000ff) << 0 | |
40 | dev |= (uint64(minor) & 0xffffff00) << 12 | |
41 | return dev | |
42 | } |