/
dir.go
253 lines (212 loc) · 6.49 KB
/
dir.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package gopathfs
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/hanwen/go-fuse/fuse"
)
// OpenDir overwrites the parent's OpenDir method.
func (gpf *GoPathFs) OpenDir(name string, context *fuse.Context) ([]fuse.DirEntry, fuse.Status) {
if name == "" {
return gpf.openTopDir()
}
if name == gpf.cfg.GoPkgPrefix {
return gpf.openFirstPartyDir()
}
if strings.HasPrefix(name, gpf.cfg.GoPkgPrefix+pathSeparator) {
return gpf.openFirstPartyChildDir(name)
}
entries := []fuse.DirEntry{}
var status fuse.Status
// Search in fall-through directories.
for _, dir := range gpf.cfg.FallThrough {
if dir == name || strings.HasPrefix(name, dir) {
fname := filepath.Join(gpf.dirs.Workspace, name)
entries, status = gpf.openUnderlyingDir(fname, nil /* excludes */, entries)
if status == fuse.OK {
return entries, fuse.OK
}
fmt.Printf("failed to open entry %s\n", fname)
return nil, fuse.ENOENT
}
}
// Search in vendor directories.
for _, vendor := range gpf.cfg.Vendors {
entries, status = gpf.openVendorChildDir(vendor, name, entries)
if status == fuse.OK {
return entries, fuse.OK
}
}
return nil, fuse.ENOENT
}
// Mkdir overwrites the parent's Mkdir method.
func (gpf *GoPathFs) Mkdir(name string, mode uint32, context *fuse.Context) fuse.Status {
prefix := gpf.cfg.GoPkgPrefix + pathSeparator
if strings.HasPrefix(name, prefix) {
return gpf.mkFirstPartyChildDir(name[len(prefix):], mode, context)
}
return gpf.mkThirdPartyChildDir(name, mode, context)
}
// Rmdir overwrites the parent's Rmdir method.
func (gpf *GoPathFs) Rmdir(name string, context *fuse.Context) fuse.Status {
prefix := gpf.cfg.GoPkgPrefix + pathSeparator
if strings.HasPrefix(name, prefix) {
return gpf.rmFirstPartyChildDir(name[len(prefix):], context)
}
return gpf.rmThirdPartyChildDir(name, context)
}
func (gpf *GoPathFs) openTopDir() ([]fuse.DirEntry, fuse.Status) {
entries := []fuse.DirEntry{
{
Name: gpf.cfg.GoPkgPrefix,
Mode: fuse.S_IFDIR,
},
}
// Vendor directories.
for _, vendor := range gpf.cfg.Vendors {
entries, _ = gpf.openUnderlyingDir(filepath.Join(gpf.dirs.Workspace, vendor), gpf.cfg.FallThroughSet /* excludes */, entries)
}
// Fall-through directories.
for _, dir := range gpf.cfg.FallThrough {
dir = filepath.Join(gpf.dirs.Workspace, dir)
fi, err := os.Stat(dir)
if err != nil {
fmt.Printf("Failed to access %s, %v", dir, err)
continue
}
entry := fuse.DirEntry{
Name: fi.Name(),
Mode: fuse.S_IFREG,
}
if fi.IsDir() {
entry.Mode = fuse.S_IFDIR
}
entries = append(entries, entry)
}
return entries, fuse.OK
}
func (gpf *GoPathFs) openFirstPartyDir() ([]fuse.DirEntry, fuse.Status) {
h, err := os.Open(gpf.dirs.Workspace)
if err != nil {
return nil, fuse.ENOENT
}
defer h.Close()
fis, err := h.Readdir(-1)
if err != nil {
return nil, fuse.ENOENT
}
entries := []fuse.DirEntry{}
for _, fi := range fis {
if gpf.isIgnored(fi.Name()) {
continue
}
if gpf.isVendorDir(fi.Name()) {
continue
}
if fi.IsDir() {
entry := fuse.DirEntry{
Name: fi.Name(),
Mode: fuse.S_IFREG,
}
entry.Mode = fuse.S_IFDIR
entries = append(entries, entry)
}
}
return entries, fuse.OK
}
func (gpf *GoPathFs) openFirstPartyChildDir(name string) ([]fuse.DirEntry, fuse.Status) {
name = name[len(gpf.cfg.GoPkgPrefix+pathSeparator):]
entries := []fuse.DirEntry{}
// Search in GOROOT (for debugger).
if name == "GOROOT" || strings.HasPrefix(name, "GOROOT"+pathSeparator) {
fname := filepath.Join(gpf.dirs.GoSDKDir, name[len("GOROOT"):])
entries, status := gpf.openUnderlyingDir(fname, nil /* excludes */, entries)
if status == fuse.OK {
return entries, fuse.OK
}
fmt.Printf("failed to open entry %s\n", fname)
return nil, fuse.ENOENT
}
entries, _ = gpf.openUnderlyingDir(filepath.Join(gpf.dirs.Workspace, name), gpf.cfg.FallThroughSet /* excludes */, entries)
// Also search in bazel-genfiles.
entries, _ = gpf.openUnderlyingDir(filepath.Join(gpf.dirs.Workspace, "bazel-genfiles", name), gpf.cfg.FallThroughSet /* excludes */, entries)
return entries, fuse.OK
}
func (gpf *GoPathFs) openVendorChildDir(vendor, name string, entries []fuse.DirEntry) ([]fuse.DirEntry, fuse.Status) {
entries, _ = gpf.openUnderlyingDir(filepath.Join(gpf.dirs.Workspace, vendor, name), gpf.cfg.FallThroughSet /* excludes */, entries)
// Also search in bazel-genfiles.
entries, _ = gpf.openUnderlyingDir(filepath.Join(gpf.dirs.Workspace, "bazel-genfiles", vendor, name), gpf.cfg.FallThroughSet /* excludes */, entries)
return entries, fuse.OK
}
func (gpf *GoPathFs) openUnderlyingDir(dir string, excludes map[string]struct{}, entries []fuse.DirEntry) ([]fuse.DirEntry, fuse.Status) {
h, err := os.Open(dir)
if err != nil {
return entries, fuse.ENOENT
}
defer h.Close()
fis, err := h.Readdir(-1)
if err != nil {
return entries, fuse.ENOENT
}
outterLoop:
for _, fi := range fis {
if fi.IsDir() {
for _, e := range entries {
if fi.Name() == e.Name {
// The generated folder has the same name as the original
// one.
continue outterLoop
}
if _, ok := excludes[fi.Name()]; ok {
// The folder should be excluded, e.g., when it has the same
// name as a fall-through folder.
continue outterLoop
}
}
}
entry := fuse.DirEntry{
Name: fi.Name(),
Mode: fuse.S_IFREG,
}
if fi.IsDir() {
entry.Mode = fuse.S_IFDIR
}
entries = append(entries, entry)
}
return entries, fuse.OK
}
func (gpf *GoPathFs) mkFirstPartyChildDir(name string, mode uint32, context *fuse.Context) fuse.Status {
name = filepath.Join(gpf.dirs.Workspace, name)
if err := os.MkdirAll(name, os.FileMode(mode)); err != nil {
return fuse.ENOENT
}
return fuse.OK
}
func (gpf *GoPathFs) mkThirdPartyChildDir(name string, mode uint32, context *fuse.Context) fuse.Status {
if len(gpf.cfg.Vendors) == 0 {
return fuse.ENOENT
}
name = filepath.Join(gpf.dirs.Workspace, gpf.cfg.Vendors[0], name)
if err := os.MkdirAll(name, os.FileMode(mode)); err != nil {
return fuse.ENOENT
}
return fuse.OK
}
func (gpf *GoPathFs) rmFirstPartyChildDir(name string, context *fuse.Context) fuse.Status {
name = filepath.Join(gpf.dirs.Workspace, name)
if err := os.RemoveAll(name); err != nil {
return fuse.ENOENT
}
return fuse.OK
}
func (gpf *GoPathFs) rmThirdPartyChildDir(name string, context *fuse.Context) fuse.Status {
if len(gpf.cfg.Vendors) == 0 {
return fuse.ENOENT
}
name = filepath.Join(gpf.dirs.Workspace, gpf.cfg.Vendors[0], name)
if err := os.RemoveAll(name); err != nil {
return fuse.ENOENT
}
return fuse.OK
}