-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
56 lines (49 loc) · 1.15 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
package atomfs
import (
"fmt"
"os"
"strings"
)
var TestOverrideRuntimeDirKey = "ATOMFS_TEST_RUN_DIR"
func EnsureDir(dir string) error {
err := os.MkdirAll(dir, 0755)
if err != nil {
return fmt.Errorf("Failed creating directory %s: %w", dir, err)
}
return nil
}
func PathExists(d string) bool {
_, err := os.Stat(d)
if err != nil && os.IsNotExist(err) {
return false
}
return true
}
// remove dir separators to make one dir name. It is OK that this can't be
// cleanly backed out, we don't need it to
func ReplacePathSeparators(p string) string {
if p[0] == '/' {
p = p[1:]
}
return strings.ReplaceAll(p, "/", "-")
}
func GetMountNSName() (string, error) {
val, err := os.Readlink("/proc/self/ns/mnt")
if err != nil {
return "", err
}
// link target looks like 'mnt:[NUMBER]', we just want NUMBER
return val[5 : len(val)-1], nil
}
// Allow overriding runtime dir for tests so we can assert empty dirs, etc.
func RuntimeDir(metadir string) string {
testOverrideDir := os.Getenv(TestOverrideRuntimeDirKey)
if testOverrideDir == "" {
if metadir == "" {
return "/run/atomfs"
} else {
return metadir
}
}
return testOverrideDir
}