-
Notifications
You must be signed in to change notification settings - Fork 21
/
io_helper_linux.go
74 lines (67 loc) · 2.21 KB
/
io_helper_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//go:build linux
// +build linux
package cgv2
import (
"fmt"
"io/fs"
"path/filepath"
"syscall"
)
// fetchDeviceName will attempt to find a device name associated with a major/minor pair
// the bool indicates if a device was found.
func fetchDeviceName(major uint64, minor uint64) (bool, string, error) {
// iterate over /dev/ and pull major and minor values
found := false
var devName string
var curMajor uint64
var curMinor uint64
var devID uint64
walkFunc := func(path string, d fs.DirEntry, err error) error {
if d.IsDir() && path != "/dev/" {
return fs.SkipDir
}
if d.Type() != fs.ModeDevice {
return nil
}
fInfo, dirErr := d.Info()
if dirErr != nil {
return nil //nolint: nilerr // errors are hidden on purpose
}
infoT, ok := fInfo.Sys().(*syscall.Stat_t)
if !ok {
return nil
}
devID = uint64(infoT.Rdev)
// do some bitmapping to extract the major and minor device values
// The odd duplicated logic here is to deal with 32 and 64 bit values.
// see bits/sysmacros.h
curMajor = ((devID & 0xfffff00000000000) >> 32) | ((devID & 0x00000000000fff00) >> 8)
curMinor = ((devID & 0x00000000000000ff) >> 0) | ((devID & 0x00000ffffff00000) >> 12)
if curMajor == major && curMinor == minor {
found = true
devName = d.Name()
}
return nil
}
err := filepath.WalkDir("/dev/", walkFunc)
if err != nil {
return false, "", fmt.Errorf("error walking /dev/: %w", err)
}
return found, devName, nil
}