-
Notifications
You must be signed in to change notification settings - Fork 7
/
scanner.go
213 lines (169 loc) · 4.13 KB
/
scanner.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
Package libudev implements a native udev library.
Recursively passes `/sys/devices/...` directory and reads `uevent` files (placed in `Env`).
Based on the information received from `uevent` files, it tries to enrich the data based
on the data received from the files that are on the same level as the `uevent` file (they are placed in `Attrs`),
and also tries to find and read the files `/run/udev/data/...` (placed in `Env` or `Tags`).
After building a list of devices, the library builds a device tree.
*/
package libudev
import (
"bufio"
"bytes"
"fmt"
"github.com/citilinkru/libudev/types"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// Scanner structure of the device scanner.
type Scanner struct {
devicesPath string
udevDataPath string
}
// NewScanner creates a new instance of device scanner.
func NewScanner() *Scanner {
return &Scanner{
devicesPath: "/sys/devices",
udevDataPath: "/run/udev/data",
}
}
// ScanDevices scans directories for `uevent` files and creates a device tree.
func (s *Scanner) ScanDevices() (err error, devices []*types.Device) {
devices = []*types.Device{}
devicesMap := map[string]*types.Device{}
err = filepath.Walk(s.devicesPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() || info.Name() != "uevent" {
return nil
}
device := &types.Device{
Devpath: filepath.Dir(path),
Env: map[string]string{},
Attrs: map[string]string{},
Parent: nil,
}
s.readAttrs(filepath.Dir(path), device)
err = s.readUeventFile(path, device)
if err != nil {
return nil
}
devicesMap[device.Devpath] = device
return nil
})
// make tree
for _, v := range devicesMap {
parts := strings.Split(v.Devpath, "/")
devpath := v.Devpath
for i := len(parts) - 1; i >= 0; i-- {
devpath = strings.TrimSuffix(devpath, "/"+parts[i])
if devpath == s.devicesPath {
break
}
if device, ok := devicesMap[devpath]; ok {
v.Parent = device
device.Children = append(device.Children, v)
break
}
}
devices = append(devices, v)
}
return err, devices
}
func (s *Scanner) readAttrs(path string, device *types.Device) error {
files, err := ioutil.ReadDir(path)
if err != nil {
return err
}
for _, f := range files {
if f.IsDir() || f.Name() == "uevent" || f.Name() == "descriptors" {
continue
}
data, err := ioutil.ReadFile(filepath.Join(path, f.Name()))
if err != nil {
continue
}
device.Attrs[f.Name()] = strings.Trim(string(data), "\n\r\t ")
}
return nil
}
func (s *Scanner) readUeventFile(path string, device *types.Device) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return err
}
buf := bufio.NewScanner(bytes.NewBuffer(data))
var line string
for buf.Scan() {
line = buf.Text()
field := strings.SplitN(line, "=", 2)
if len(field) != 2 {
continue
}
device.Env[field[0]] = field[1]
}
devPath := filepath.Join(filepath.Dir(path), "dev")
devString, err := s.readDevFile(devPath)
if err != nil {
return err
}
err = s.readUdevInfo(devString, device)
if err != nil {
return err
}
return nil
}
func (s *Scanner) readDevFile(path string) (data string, err error) {
f, err := os.Open(path)
if err != nil {
return
}
defer f.Close()
d, err := ioutil.ReadAll(f)
return strings.Trim(string(d), "\n\r\t "), err
}
func (s *Scanner) readUdevInfo(devString string, d *types.Device) error {
path := fmt.Sprintf("%s/c%s", s.udevDataPath, devString)
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return err
}
buf := bufio.NewScanner(bytes.NewBuffer(data))
var line string
for buf.Scan() {
line = buf.Text()
groups := strings.SplitN(line, ":", 2)
if len(groups) != 2 {
continue
}
if groups[0] == "I" {
d.UsecInitialized = groups[1]
continue
}
if groups[0] == "G" {
d.Tags = append(d.Tags, groups[1])
continue
}
if groups[0] == "E" {
fields := strings.SplitN(groups[1], "=", 2)
if len(fields) != 2 {
continue
}
d.Env[fields[0]] = fields[1]
}
}
return nil
}