-
Notifications
You must be signed in to change notification settings - Fork 2
/
type.go
74 lines (63 loc) · 1.71 KB
/
type.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
package mdevfs
import (
"io/fs"
"os"
"path"
"strconv"
"github.com/gentlemanautomaton/machina"
)
// TypeList holds a set of mediated device types.
type TypeList []Type
// FindName returns the first supported type with the given name, if present
// in the list.
func (list TypeList) FindName(name machina.MediatedDeviceType) (typ Type, ok bool) {
for i := range list {
if list[i].Name() == string(name) {
return list[i], true
}
}
return Type{}, false
}
// Type describes a supported type offered by a mediated device
// on the local system.
type Type struct {
path string
typ string
name string
description string
}
// Path returns the sysfs path for the supported type on the local system.
func (t Type) Path() string {
return t.path
}
// ID returns the supported type identifier.
func (t Type) ID() string {
return t.typ
}
// Name returns the name of the supported type, which is optional.
func (t Type) Name() string {
return t.name
}
// Description returns the description of the supported type, which is
// optional.
func (t Type) Description() string {
return t.description
}
// AvailableInstances returns the number of instances currently available for
// the supported type.
//
// The value is queried at the of the function call, and is not cached.
func (t Type) AvailableInstances() (int, error) {
typefs := os.DirFS(t.path)
avail, err := fs.ReadFile(typefs, "available_instances")
if err != nil {
return 0, err
}
return strconv.Atoi(string(avail))
}
// Create requests the creation of a mediated device of type t with the given
// device ID.
func (t Type) Create(id machina.DeviceID) error {
create := path.Join(t.path, "create")
return writeToSystemFile(create, id.String())
}