-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkeyfs.go
79 lines (74 loc) · 1.85 KB
/
keyfs.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
package ginit
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// KeyFS implements a simple structure for
// interacting with "key" based filesystems
// like procfs and sysfs.
type KeyFS struct {
Base string
}
// Find traverses the path below the Base path
// and optionally follows one level of symbolic
// link. For example, to find the mac addresses
// of all network devices you could do:
// Find("/class/net", "address", true)
// And it will return an array of resolved devices
// at /sys/devices/pci...
func (k KeyFS) Find(dir, name string, follow bool) ([]string, error) {
var keys []string
walkFn := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Skip directories
if info.IsDir() {
return nil
}
if (info.Mode()&os.ModeSymlink) == os.ModeSymlink && follow {
err := os.Chdir(filepath.Dir(path))
if err != nil {
return err
}
path, err := os.Readlink(path)
if err != nil {
return err
}
path, err = filepath.Abs(path)
if err != nil {
return err
}
found, err := k.Find(strings.Replace(path, k.Base, "", -1), name, false)
for _, key := range found {
keys = append(keys, key)
}
}
if _, n := filepath.Split(path); n == name {
keys = append(keys, strings.Replace(path, k.Base, "", -1))
}
return nil
}
err := filepath.Walk(filepath.Join(k.Base, dir), walkFn)
return keys, err
}
// Read reads from the given key
func (k KeyFS) Read(key string) ([]byte, error) {
return ioutil.ReadFile(filepath.Join(k.Base, key))
}
// Write writes to the given key
func (k KeyFS) Write(key string, value []byte) error {
stat, err := os.Stat(filepath.Join(k.Base, key))
if err != nil {
return err
}
fd, err := os.OpenFile(filepath.Join(k.Base, key), os.O_WRONLY, stat.Mode())
if err != nil {
return err
}
defer fd.Close()
_, err = fd.Write(value)
return err
}