-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
49 lines (41 loc) · 903 Bytes
/
fs.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
package acp
import (
"fmt"
"path/filepath"
"strings"
mapset "github.com/deckarep/golang-set/v2"
"github.com/moby/sys/mountinfo"
)
func getMountpointCache() (func(string) string, error) {
mounts, err := mountinfo.GetMounts(nil)
if err != nil {
return nil, fmt.Errorf("get mounts fail, %w", err)
}
mountPoints := mapset.NewThreadUnsafeSet[string]()
for _, mount := range mounts {
if mount == nil {
continue
}
if mount.Mountpoint == "" {
continue
}
mp := mount.Mountpoint
if !strings.HasSuffix(mp, "/") {
mp = mp + "/"
}
mountPoints.Add(mp)
}
mps := mountPoints.ToSlice()
return Cache(func(path string) string {
path, err := filepath.Abs(path)
if err != nil {
panic(fmt.Errorf("get abs from file path failed, path= '%s', %w", path, err))
}
for _, mp := range mps {
if strings.HasPrefix(path, mp) {
return mp
}
}
return ""
}), nil
}