-
Notifications
You must be signed in to change notification settings - Fork 115
/
concrete_udev_device.go
115 lines (98 loc) · 2.93 KB
/
concrete_udev_device.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
package udevdevice
import (
"os"
"time"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type ConcreteUdevDevice struct {
runner boshsys.CmdRunner
logger boshlog.Logger
logtag string
}
func NewConcreteUdevDevice(runner boshsys.CmdRunner, logger boshlog.Logger) ConcreteUdevDevice {
return ConcreteUdevDevice{
runner: runner,
logger: logger,
logtag: "ConcreteUdevDevice",
}
}
func (udev ConcreteUdevDevice) KickDevice(filePath string) {
maxTries := 5
for i := 0; i < maxTries; i++ {
udev.logger.Debug(udev.logtag, "Kicking device, attempt %d of %d", i, maxTries)
err := udev.readByte(filePath)
if err == nil {
break
}
time.Sleep(time.Second / 2)
}
if err := udev.readByte(filePath); err != nil {
udev.logger.Error(udev.logtag, "Failed to red byte from device: %s", err.Error())
}
return
}
func (udev ConcreteUdevDevice) Settle() (err error) {
udev.logger.Debug(udev.logtag, "Settling UdevDevice")
switch {
case udev.runner.CommandExists("udevadm"):
_, _, _, err = udev.runner.RunCommand("udevadm", "settle")
case udev.runner.CommandExists("udevsettle"):
_, _, _, err = udev.runner.RunCommand("udevsettle")
default:
err = bosherr.Error("can not find udevadm or udevsettle commands")
}
return
}
func (udev ConcreteUdevDevice) Trigger() (err error) {
udev.logger.Debug(udev.logtag, "Triggering UdevDevice")
switch {
case udev.runner.CommandExists("udevadm"):
_, _, _, err = udev.runner.RunCommand("udevadm", "trigger")
case udev.runner.CommandExists("udevtrigger"):
_, _, _, err = udev.runner.RunCommand("udevtrigger")
default:
err = bosherr.Error("can not find udevadm or udevtrigger commands")
}
return
}
func (udev ConcreteUdevDevice) EnsureDeviceReadable(filePath string) error {
maxTries := 5
for i := 0; i < maxTries; i++ {
udev.logger.Debug(udev.logtag, "Ensuring Device Readable, Attempt %d out of %d", i, maxTries)
err := udev.readByte(filePath)
if err != nil {
udev.logger.Debug(udev.logtag, "Ignorable error from readByte: %s", err.Error())
}
time.Sleep(time.Second / 2)
}
err := udev.readByte(filePath)
if err != nil {
return bosherr.WrapError(err, "Reading udev device")
}
return nil
}
func (udev ConcreteUdevDevice) readByte(filePath string) error {
udev.logger.Debug(udev.logtag, "readBytes from file: %s", filePath)
device, err := os.Open(filePath)
if err != nil {
return err
}
defer func() {
if err = device.Close(); err != nil {
udev.logger.Warn(udev.logtag, "Failed to close device: %s", err.Error())
}
}()
udev.logger.Debug(udev.logtag, "Successfully open file: %s", filePath)
bytes := make([]byte, 1, 1)
read, err := device.Read(bytes)
if err != nil {
return err
}
udev.logger.Debug(udev.logtag, "Successfully read %d bytes from file: %s", read, filePath)
if read != 1 {
return bosherr.Error("Device readable but zero length")
}
return nil
}