This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 604
Expand file tree
/
Copy pathmountcli.go
More file actions
208 lines (167 loc) · 4.74 KB
/
mountcli.go
File metadata and controls
208 lines (167 loc) · 4.74 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// mountcli is a package for interacting with the local "mount" command.
//
// See mount_<os>.go for examples.
package mountcli
import (
"errors"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
var (
// ErrNotInMount happens when path is not inside/at root level of mount.
ErrNotInMount = errors.New("Path not run in mount.")
// ErrNoMountName happens when no mount with given name.
ErrNoMountName = errors.New("No mount found with given name.")
// ErrNoMountName happens when no mount there's mount on given path.
ErrNoMountPath = errors.New("No mount found with given path.")
// folderSeparator is the os specific separator for dividing folders.
folderSeparator = string(filepath.Separator)
)
type Mountcli struct {
// binName is the name of the command that returns results of mounts on the
// filesystem. It should take -t option that filters specific types of mounts.
binName string
// matcher matches name and path from results returned in mount command.
matcher *regexp.Regexp
// filterTag is used to filter just specific mounts in mount command.
filterTag string
// binRunner is func to run the given binary and return the output as a
// string. This is used for mocking ability.
binRunner func(string, string) (string, error)
}
// NewMountcli creates a new Mountcli instance.
func NewMountcli() *Mountcli {
return &Mountcli{
binName: "mount",
binRunner: binRunner,
matcher: FuseMatcher,
filterTag: FuseTag,
}
}
// GetAllMountedPaths returns all osxfuse mounted paths.
func (m *Mountcli) GetAllMountedPaths() ([]string, error) {
mounts, err := m.parse()
if err != nil {
return nil, err
}
paths := []string{}
for _, m := range mounts {
paths = append(paths, m.path)
}
return paths, nil
}
// FindMountedPathByName returns the mounted paths for the given name.
func (m *Mountcli) FindMountedPathByName(name string) (string, error) {
mounts, err := m.parse()
if err != nil {
return "", err
}
for _, m := range mounts {
if m.name == name {
return m.path, nil
}
}
return "", ErrNoMountName
}
// FindMountedPathByName returns the mounted name for the given path.
// It also works with nested mounted paths.
func (m *Mountcli) FindMountNameByPath(path string) (string, error) {
path = filepath.Clean(path)
if relativePath, err := m.FindRelativeMountPath(path); err == nil {
path = strings.TrimSuffix(path, relativePath)
path = filepath.Clean(path)
}
mounts, err := m.parse()
if err != nil {
return "", err
}
for _, m := range mounts {
if m.path == path {
return m.name, nil
}
}
return "", ErrNoMountPath
}
// FindRelativeMountPath returns the path that's relative to mount path based on
// specified local path. If the specified path and mount are equal, it returns
// an empty string, else it returns the remaining paths.
//
// Ex: if mount path is "/a/b" and local path is "/a/b/c/d", it returns "c/d".
//
// It returns ErrNotInMount if specified local path is not inside or equal to
// mount.
func (m *Mountcli) FindRelativeMountPath(path string) (string, error) {
mounts, err := m.parse()
if err != nil {
return "", err
}
splitLocal := strings.Split(path, folderSeparator)
for _, m := range mounts {
splitMount := strings.Split(m.path, folderSeparator)
// if local path is smaller in length than mount, it can't be in it
if len(splitLocal) < len(splitMount) {
continue
}
// if local path and mount are same size or greater, compare the entries
for i, localFolder := range splitLocal[:len(splitMount)] {
if localFolder != splitMount[i] {
break
}
}
// whatever entries remaining in local path is the relative path
return filepath.Join(splitLocal[len(splitMount):]...), nil
}
return "", ErrNotInMount
}
// IsPathInMountedPath returns if given path is equal to mount or inside a
// mount.
func (m *Mountcli) IsPathInMountedPath(path string) (bool, error) {
mounts, err := m.parse()
if err != nil {
return false, err
}
for _, m := range mounts {
if strings.HasPrefix(path, m.path) {
return true, nil
}
}
return false, nil
}
func (m *Mountcli) parse() ([]*resp, error) {
out, err := m.binRunner(m.binName, m.filterTag)
if err != nil {
return nil, err
}
resps := []*resp{}
lines := strings.Split(out, "\n")
for _, line := range lines {
if line == "" {
continue
}
resp := &resp{}
matches := m.matcher.FindStringSubmatch(line)
if len(matches) >= 2 {
resp.name = matches[1]
}
if len(matches) >= 3 {
resp.path = matches[2]
}
resps = append(resps, resp)
}
return resps, nil
}
///// helpers
type resp struct {
name string
path string
}
func binRunner(bin, defaultFuseTag string) (string, error) {
cmd := exec.Command(bin, "-t", defaultFuseTag)
out, err := cmd.CombinedOutput()
if err != nil {
return "", err
}
return string(out), nil
}