-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathfs.go
386 lines (330 loc) · 8.13 KB
/
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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package fs
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"sync"
"syscall"
)
type EntryKind uint8
const (
DirEntry EntryKind = 1
FileEntry EntryKind = 2
)
type Entry struct {
symlink string
dir string
base string
mutex sync.Mutex
kind EntryKind
needStat bool
}
func (e *Entry) Kind() EntryKind {
e.mutex.Lock()
defer e.mutex.Unlock()
if e.needStat {
e.stat()
}
return e.kind
}
func (e *Entry) Symlink() string {
e.mutex.Lock()
defer e.mutex.Unlock()
if e.needStat {
e.stat()
}
return e.symlink
}
func (e *Entry) stat() {
e.needStat = false
entryPath := filepath.Join(e.dir, e.base)
// Use "lstat" since we want information about symbolic links
BeforeFileOpen()
defer AfterFileClose()
stat, err := os.Lstat(entryPath)
if err != nil {
return
}
mode := stat.Mode()
// Follow symlinks now so the cache contains the translation
if (mode & os.ModeSymlink) != 0 {
link, err := os.Readlink(entryPath)
if err != nil {
return // Skip over this entry
}
if !filepath.IsAbs(link) {
link = filepath.Join(e.dir, link)
}
e.symlink = filepath.Clean(link)
// Re-run "lstat" on the symlink target
stat2, err2 := os.Lstat(e.symlink)
if err2 != nil {
return // Skip over this entry
}
mode = stat2.Mode()
if (mode & os.ModeSymlink) != 0 {
return // Symlink chains are not supported
}
}
// We consider the entry either a directory or a file
if (mode & os.ModeDir) != 0 {
e.kind = DirEntry
} else {
e.kind = FileEntry
}
}
type FS interface {
// The returned map is immutable and is cached across invocations. Do not
// mutate it.
ReadDirectory(path string) (map[string]*Entry, error)
ReadFile(path string) (string, error)
// This is part of the interface because the mock interface used for tests
// should not depend on file system behavior (i.e. different slashes for
// Windows) while the real interface should.
Abs(path string) (string, bool)
Dir(path string) string
Base(path string) string
Ext(path string) string
Join(parts ...string) string
Cwd() string
Rel(base string, target string) (string, bool)
}
////////////////////////////////////////////////////////////////////////////////
type mockFS struct {
dirs map[string]map[string]*Entry
files map[string]string
}
func MockFS(input map[string]string) FS {
dirs := make(map[string]map[string]*Entry)
files := make(map[string]string)
for k, v := range input {
files[k] = v
original := k
// Build the directory map
for {
kDir := path.Dir(k)
dir, ok := dirs[kDir]
if !ok {
dir = make(map[string]*Entry)
dirs[kDir] = dir
}
if kDir == k {
break
}
if k == original {
dir[path.Base(k)] = &Entry{kind: FileEntry}
} else {
dir[path.Base(k)] = &Entry{kind: DirEntry}
}
k = kDir
}
}
return &mockFS{dirs, files}
}
func (fs *mockFS) ReadDirectory(path string) (map[string]*Entry, error) {
dir := fs.dirs[path]
if dir == nil {
return nil, syscall.ENOENT
}
return dir, nil
}
func (fs *mockFS) ReadFile(path string) (string, error) {
contents, ok := fs.files[path]
if !ok {
return "", syscall.ENOENT
}
return contents, nil
}
func (*mockFS) Abs(p string) (string, bool) {
return path.Clean(path.Join("/", p)), true
}
func (*mockFS) Dir(p string) string {
return path.Dir(p)
}
func (*mockFS) Base(p string) string {
return path.Base(p)
}
func (*mockFS) Ext(p string) string {
return path.Ext(p)
}
func (*mockFS) Join(parts ...string) string {
return path.Clean(path.Join(parts...))
}
func (*mockFS) Cwd() string {
return ""
}
func splitOnSlash(path string) (string, string) {
if slash := strings.IndexByte(path, '/'); slash != -1 {
return path[:slash], path[slash+1:]
}
return path, ""
}
func (*mockFS) Rel(base string, target string) (string, bool) {
// Base cases
if base == "" || base == "." {
return target, true
}
if base == target {
return ".", true
}
// Find the common parent directory
for {
bHead, bTail := splitOnSlash(base)
tHead, tTail := splitOnSlash(target)
if bHead != tHead {
break
}
base = bTail
target = tTail
}
// Stop now if base is a subpath of target
if base == "" {
return target, true
}
// Traverse up to the common parent
commonParent := strings.Repeat("../", strings.Count(base, "/")+1)
// Stop now if target is a subpath of base
if target == "" {
return commonParent[:len(commonParent)-1], true
}
// Otherwise, down to the parent
return commonParent + target, true
}
////////////////////////////////////////////////////////////////////////////////
type realFS struct {
// Stores the file entries for directories we've listed before
entries map[string]entriesOrErr
// For the current working directory
cwd string
}
type entriesOrErr struct {
entries map[string]*Entry
err error
}
// Limit the number of files open simultaneously to avoid ulimit issues
var fileOpenLimit = make(chan bool, 32)
func BeforeFileOpen() {
// This will block if the number of open files is already at the limit
fileOpenLimit <- false
}
func AfterFileClose() {
<-fileOpenLimit
}
func realpath(path string) string {
dir := filepath.Dir(path)
if dir == path {
return path
}
dir = realpath(dir)
path = filepath.Join(dir, filepath.Base(path))
BeforeFileOpen()
defer AfterFileClose()
if link, err := os.Readlink(path); err == nil {
if filepath.IsAbs(link) {
return link
}
return filepath.Join(dir, link)
}
return path
}
func RealFS() FS {
cwd, err := os.Getwd()
if err != nil {
cwd = ""
} else {
// Resolve symlinks in the current working directory. Symlinks are resolved
// when input file paths are converted to absolute paths because we need to
// recognize an input file as unique even if it has multiple symlinks
// pointing to it. The build will generate relative paths from the current
// working directory to the absolute input file paths for error messages,
// so the current working directory should be processed the same way. Not
// doing this causes test failures with esbuild when run from inside a
// symlinked directory.
cwd = realpath(cwd)
}
return &realFS{
entries: make(map[string]entriesOrErr),
cwd: cwd,
}
}
func (fs *realFS) ReadDirectory(dir string) (map[string]*Entry, error) {
// First, check the cache
cached, ok := fs.entries[dir]
// Cache hit: stop now
if ok {
return cached.entries, cached.err
}
// Cache miss: read the directory entries
names, err := readdir(dir)
entries := make(map[string]*Entry)
if err == nil {
for _, name := range names {
// Call "stat" lazily for performance. The "@material-ui/icons" package
// contains a directory with over 11,000 entries in it and running "stat"
// for each entry was a big performance issue for that package.
entries[name] = &Entry{
dir: dir,
base: name,
needStat: true,
}
}
}
// Update the cache unconditionally. Even if the read failed, we don't want to
// retry again later. The directory is inaccessible so trying again is wasted.
if err != nil {
if pathErr, ok := err.(*os.PathError); ok {
err = pathErr.Unwrap()
}
entries = nil
}
fs.entries[dir] = entriesOrErr{entries: entries, err: err}
return entries, err
}
func (fs *realFS) ReadFile(path string) (string, error) {
BeforeFileOpen()
defer AfterFileClose()
buffer, err := ioutil.ReadFile(path)
if err != nil {
if pathErr, ok := err.(*os.PathError); ok {
err = pathErr.Unwrap()
}
}
return string(buffer), err
}
func (*realFS) Abs(p string) (string, bool) {
abs, err := filepath.Abs(p)
return abs, err == nil
}
func (*realFS) Dir(p string) string {
return filepath.Dir(p)
}
func (*realFS) Base(p string) string {
return filepath.Base(p)
}
func (*realFS) Ext(p string) string {
return filepath.Ext(p)
}
func (*realFS) Join(parts ...string) string {
return filepath.Clean(filepath.Join(parts...))
}
func (fs *realFS) Cwd() string {
return fs.cwd
}
func (*realFS) Rel(base string, target string) (string, bool) {
if rel, err := filepath.Rel(base, target); err == nil {
return rel, true
}
return "", false
}
func readdir(dirname string) ([]string, error) {
BeforeFileOpen()
defer AfterFileClose()
f, err := os.Open(dirname)
if err != nil {
return nil, err
}
defer f.Close()
return f.Readdirnames(-1)
}