-
Notifications
You must be signed in to change notification settings - Fork 19
/
utils.go
61 lines (53 loc) · 1.28 KB
/
utils.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
package lxcri
import (
"bytes"
"fmt"
"os"
"path/filepath"
"runtime"
"golang.org/x/sys/unix"
)
func canExecute(cmds ...string) error {
for _, c := range cmds {
if err := unix.Access(c, unix.X_OK); err != nil {
return fmt.Errorf("can not execute %q: %w", c, err)
}
}
return nil
}
func fsMagic(fsName string) int64 {
switch fsName {
case "proc", "procfs":
return unix.PROC_SUPER_MAGIC
case "cgroup2", "cgroup2fs":
return unix.CGROUP2_SUPER_MAGIC
default:
return -1
}
}
// TODO check whether dir is the filsystem root (use /proc/mounts)
func isFilesystem(dir string, fsName string) error {
fsType := fsMagic(fsName)
if fsType == -1 {
return fmt.Errorf("undefined filesystem %q", fsName)
}
var stat unix.Statfs_t
err := unix.Statfs(dir, &stat)
if err != nil {
return fmt.Errorf("fstat failed for %q: %w", dir, err)
}
if stat.Type != fsType {
return fmt.Errorf("%q is not on filesystem %s", dir, fsName)
}
return nil
}
func nullTerminatedString(data []byte) string {
i := bytes.Index(data, []byte{0})
return string(data[:i])
}
func errorf(sfmt string, args ...interface{}) error {
bin := filepath.Base(os.Args[0])
_, file, line, _ := runtime.Caller(1)
prefix := fmt.Sprintf("[%s:%s:%d] ", bin, filepath.Base(file), line)
return fmt.Errorf(prefix+sfmt, args...)
}