Debian Codegraph @

src/cmd/go/internal/fsys/fsys.go

raw
1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package fsys implements a virtual file system that the go command
6 // uses to read source file trees. The virtual file system redirects some
7 // OS file paths to other OS file paths, according to an overlay file.
8 // Editors can use this overlay support to invoke the go command on
9 // temporary files that have been edited but not yet saved into their
10 // final locations.
11 package fsys
12
13 import (
14 "cmd/go/internal/str"
15 "encoding/json"
16 "errors"
17 "fmt"
18 "internal/godebug"
19 "io"
20 "io/fs"
21 "iter"
22 "log"
23 "maps"
24 "os"
25 pathpkg "path"
26 "path/filepath"
27 "runtime/debug"
28 "slices"
29 "strings"
30 "sync"
31 "time"
32 )
33
34 // Trace emits a trace event for the operation and file path to the trace log,
35 // but only when $GODEBUG contains gofsystrace=1.
36 // The traces are appended to the file named by the $GODEBUG setting gofsystracelog, or else standard error.
37 // For debugging, if the $GODEBUG setting gofsystracestack is non-empty, then trace events for paths
38 // matching that glob pattern (using path.Match) will be followed by a full stack trace.
39 func Trace(op, path string) {
40 if !doTrace {
41 return
42 }
43 traceMu.Lock()
44 defer traceMu.Unlock()
45 fmt.Fprintf(traceFile, "%d gofsystrace %s %s\n", os.Getpid(), op, path)
46 if pattern := gofsystracestack.Value(); pattern != "" {
47 if match, _ := pathpkg.Match(pattern, path); match {
48 traceFile.Write(debug.Stack())
49 }
50 }
51 }
52
53 var (
54 doTrace bool
55 traceFile *os.File
56 traceMu sync.Mutex
57
58 gofsystrace = godebug.New("#gofsystrace")
59 gofsystracelog = godebug.New("#gofsystracelog")
60 gofsystracestack = godebug.New("#gofsystracestack")
61 )
62
63 func init() {
64 if gofsystrace.Value() != "1" {
65 return
66 }
67 doTrace = true
68 if f := gofsystracelog.Value(); f != "" {
69 // Note: No buffering on writes to this file, so no need to worry about closing it at exit.
70 var err error
71 traceFile, err = os.OpenFile(f, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
72 if err != nil {
73 log.Fatal(err)
74 }
75 } else {
76 traceFile = os.Stderr
77 }
78 }
79
80 // OverlayFile is the -overlay flag value.
81 // It names a file containing the JSON for an overlayJSON struct.
82 var OverlayFile string
83
84 // overlayJSON is the format for the -overlay file.
85 type overlayJSON struct {
86 // Replace maps file names observed by Go tools
87 // to the actual files that should be used when those are read.
88 // If the actual name is "", the file should appear to be deleted.
89 Replace map[string]string
90 }
91
92 // overlay is a list of replacements to be applied, sorted by cmp of the from field.
93 // cmp sorts the filepath.Separator less than any other byte so that x is always
94 // just before any children x/a, x/b, and so on, before x.go. (This would not
95 // be the case with byte-wise sorting, which would produce x, x.go, x/a.)
96 // The sorting lets us find the relevant overlay entry quickly even if it is for a
97 // parent of the path being searched.
98 var overlay []replace
99
100 // A replace represents a single replaced path.
101 type replace struct {
102 // from is the old path being replaced.
103 // It is an absolute path returned by abs.
104 from string
105
106 // to is the replacement for the old path.
107 // It is an absolute path returned by abs.
108 // If it is the empty string, the old path appears deleted.
109 // Otherwise the old path appears to be the file named by to.
110 // If to ends in a trailing slash, the overlay code below treats
111 // it as a directory replacement, akin to a bind mount.
112 // However, our processing of external overlay maps removes
113 // such paths by calling abs, except for / or C:\.
114 to string
115 }
116
117 var binds []replace
118
119 // Bind makes the virtual file system use dir as if it were mounted at mtpt,
120 // like Plan 9's “bind” or Linux's “mount --bind”, or like os.Symlink
121 // but without the symbolic link.
122 //
123 // For now, the behavior of using Bind on multiple overlapping
124 // mountpoints (for example Bind("x", "/a") and Bind("y", "/a/b"))
125 // is undefined.
126 func Bind(dir, mtpt string) {
127 if dir == "" || mtpt == "" {
128 panic("Bind of empty directory")
129 }
130 binds = append(binds, replace{abs(mtpt), abs(dir)})
131 }
132
133 // cwd returns the current directory, caching it on first use.
134 var cwd = sync.OnceValue(cwdOnce)
135
136 func cwdOnce() string {
137 wd, err := os.Getwd()
138 if err != nil {
139 // Note: cannot import base, so using log.Fatal.
140 log.Fatalf("cannot determine current directory: %v", err)
141 }
142 return wd
143 }
144
145 // abs returns the absolute form of path, for looking up in the overlay map.
146 // For the most part, this is filepath.Abs and filepath.Clean,
147 // except that Windows requires special handling, as always.
148 func abs(path string) string {
149 if path == "" {
150 return ""
151 }
152 if filepath.IsAbs(path) {
153 return filepath.Clean(path)
154 }
155
156 dir := cwd()
157 if vol := filepath.VolumeName(dir); vol != "" && (path[0] == '\\' || path[0] == '/') {
158 // path is volume-relative, like `\Temp`.
159 // Connect to volume name to make absolute path.
160 // See go.dev/issue/8130.
161 return filepath.Join(vol, path)
162 }
163
164 return filepath.Join(dir, path)
165 }
166
167 func searchcmp(r replace, t string) int {
168 return cmp(r.from, t)
169 }
170
171 // info is a summary of the known information about a path
172 // being looked up in the virtual file system.
173 type info struct {
174 abs string
175 deleted bool
176 replaced bool
177 dir bool // must be dir
178 file bool // must be file
179 actual string
180 }
181
182 // stat returns info about the path in the virtual file system.
183 func stat(path string) info {
184 apath := abs(path)
185 if path == "" {
186 return info{abs: apath, actual: path}
187 }
188
189 // Apply bind replacements before applying overlay.
190 replaced := false
191 for _, r := range binds {
192 if str.HasFilePathPrefix(apath, r.from) {
193 // apath is below r.from.
194 // Replace prefix with r.to and fall through to overlay.
195 apath = r.to + apath[len(r.from):]
196 path = apath
197 replaced = true
198 break
199 }
200 if str.HasFilePathPrefix(r.from, apath) {
201 // apath is above r.from.
202 // Synthesize a directory in case one does not exist.
203 return info{abs: apath, replaced: true, dir: true, actual: path}
204 }
205 }
206
207 // Binary search for apath to find the nearest relevant entry in the overlay.
208 i, ok := slices.BinarySearchFunc(overlay, apath, searchcmp)
209 if ok {
210 // Exact match; overlay[i].from == apath.
211 r := overlay[i]
212 if r.to == "" {
213 // Deleted.
214 return info{abs: apath, deleted: true}
215 }
216 if strings.HasSuffix(r.to, string(filepath.Separator)) {
217 // Replacement ends in slash, denoting directory.
218 // Note that this is impossible in current overlays since we call abs
219 // and it strips the trailing slashes. But we could support it in the future.
220 return info{abs: apath, replaced: true, dir: true, actual: path}
221 }
222 // Replaced file.
223 return info{abs: apath, replaced: true, file: true, actual: r.to}
224 }
225 if i < len(overlay) && str.HasFilePathPrefix(overlay[i].from, apath) {
226 // Replacement for child path; infer existence of parent directory.
227 return info{abs: apath, replaced: true, dir: true, actual: path}
228 }
229 if i > 0 && str.HasFilePathPrefix(apath, overlay[i-1].from) {
230 // Replacement for parent.
231 r := overlay[i-1]
232 if strings.HasSuffix(r.to, string(filepath.Separator)) {
233 // Parent replaced by directory; apply replacement in our path.
234 // Note that this is impossible in current overlays since we call abs
235 // and it strips the trailing slashes. But we could support it in the future.
236 p := r.to + apath[len(r.from)+1:]
237 return info{abs: apath, replaced: true, actual: p}
238 }
239 // Parent replaced by file; path is deleted.
240 return info{abs: apath, deleted: true}
241 }
242 return info{abs: apath, replaced: replaced, actual: path}
243 }
244
245 // children returns a sequence of (name, info)
246 // for all the children of the directory i
247 // implied by the overlay.
248 func (i *info) children() iter.Seq2[string, info] {
249 return func(yield func(string, info) bool) {
250 // Build list of directory children implied by the binds.
251 // Binds are not sorted, so just loop over them.
252 var dirs []string
253 for _, m := range binds {
254 if str.HasFilePathPrefix(m.from, i.abs) && m.from != i.abs {
255 name := m.from[len(i.abs)+1:]
256 if i := strings.IndexByte(name, filepath.Separator); i >= 0 {
257 name = name[:i]
258 }
259 dirs = append(dirs, name)
260 }
261 }
262 if len(dirs) > 1 {
263 slices.Sort(dirs)
264 str.Uniq(&dirs)
265 }
266
267 // Loop looking for next possible child in sorted overlay,
268 // which is previous child plus "\x00".
269 target := i.abs + string(filepath.Separator) + "\x00"
270 for {
271 // Search for next child: first entry in overlay >= target.
272 j, _ := slices.BinarySearchFunc(overlay, target, func(r replace, t string) int {
273 return cmp(r.from, t)
274 })
275
276 Loop:
277 // Skip subdirectories with deleted children (but not direct deleted children).
278 for j < len(overlay) && overlay[j].to == "" && str.HasFilePathPrefix(overlay[j].from, i.abs) && strings.Contains(overlay[j].from[len(i.abs)+1:], string(filepath.Separator)) {
279 j++
280 }
281 if j >= len(overlay) {
282 // Nothing found at all.
283 break
284 }
285 r := overlay[j]
286 if !str.HasFilePathPrefix(r.from, i.abs) {
287 // Next entry in overlay is beyond the directory we want; all done.
288 break
289 }
290
291 // Found the next child in the directory.
292 // Yield it and its info.
293 name := r.from[len(i.abs)+1:]
294 actual := r.to
295 dir := false
296 if j := strings.IndexByte(name, filepath.Separator); j >= 0 {
297 // Child is multiple levels down, so name must be a directory,
298 // and there is no actual replacement.
299 name = name[:j]
300 dir = true
301 actual = ""
302 }
303 deleted := !dir && r.to == ""
304 ci := info{
305 abs: filepath.Join(i.abs, name),
306 deleted: deleted,
307 replaced: !deleted,
308 dir: dir || strings.HasSuffix(r.to, string(filepath.Separator)),
309 actual: actual,
310 }
311 for ; len(dirs) > 0 && dirs[0] < name; dirs = dirs[1:] {
312 if !yield(dirs[0], info{abs: filepath.Join(i.abs, dirs[0]), replaced: true, dir: true}) {
313 return
314 }
315 }
316 if len(dirs) > 0 && dirs[0] == name {
317 dirs = dirs[1:]
318 }
319 if !yield(name, ci) {
320 return
321 }
322
323 // Next target is first name after the one we just returned.
324 target = ci.abs + "\x00"
325
326 // Optimization: Check whether the very next element
327 // is the next child. If so, skip the binary search.
328 if j+1 < len(overlay) && cmp(overlay[j+1].from, target) >= 0 {
329 j++
330 goto Loop
331 }
332 }
333
334 for _, dir := range dirs {
335 if !yield(dir, info{abs: filepath.Join(i.abs, dir), replaced: true, dir: true}) {
336 return
337 }
338 }
339 }
340 }
341
342 // Init initializes the overlay, if one is being used.
343 func Init() error {
344 if overlay != nil {
345 // already initialized
346 return nil
347 }
348
349 if OverlayFile == "" {
350 return nil
351 }
352
353 Trace("ReadFile", OverlayFile)
354 b, err := os.ReadFile(OverlayFile)
355 if err != nil {
356 return fmt.Errorf("reading overlay: %v", err)
357 }
358 return initFromJSON(b)
359 }
360
361 func initFromJSON(js []byte) error {
362 var ojs overlayJSON
363 if err := json.Unmarshal(js, &ojs); err != nil {
364 return fmt.Errorf("parsing overlay JSON: %v", err)
365 }
366
367 seen := make(map[string]string)
368 var list []replace
369 for _, from := range slices.Sorted(maps.Keys(ojs.Replace)) {
370 if from == "" {
371 return fmt.Errorf("empty string key in overlay map")
372 }
373 afrom := abs(from)
374 if old, ok := seen[afrom]; ok {
375 return fmt.Errorf("duplicate paths %s and %s in overlay map", old, from)
376 }
377 seen[afrom] = from
378 list = append(list, replace{from: afrom, to: abs(ojs.Replace[from])})
379 }
380
381 slices.SortFunc(list, func(x, y replace) int { return cmp(x.from, y.from) })
382
383 for i, r := range list {
384 if r.to == "" { // deleted
385 continue
386 }
387 // have file for r.from; look for child file implying r.from is a directory
388 prefix := r.from + string(filepath.Separator)
389 for _, next := range list[i+1:] {
390 if !strings.HasPrefix(next.from, prefix) {
391 break
392 }
393 if next.to != "" {
394 // found child file
395 return fmt.Errorf("inconsistent files %s and %s in overlay map", r.from, next.from)
396 }
397 }
398 }
399
400 overlay = list
401 return nil
402 }
403
404 // IsDir returns true if path is a directory on disk or in the
405 // overlay.
406 func IsDir(path string) (bool, error) {
407 Trace("IsDir", path)
408
409 switch info := stat(path); {
410 case info.dir:
411 return true, nil
412 case info.deleted, info.replaced:
413 return false, nil
414 }
415
416 info, err := os.Stat(path)
417 if err != nil {
418 return false, err
419 }
420 return info.IsDir(), nil
421 }
422
423 // errNotDir is used to communicate from ReadDir to IsGoDir
424 // that the argument is not a directory, so that IsGoDir doesn't
425 // return an error.
426 var errNotDir = errors.New("not a directory")
427
428 // osReadDir is like os.ReadDir corrects the error to be errNotDir
429 // if the problem is that name exists but is not a directory.
430 func osReadDir(name string) ([]fs.DirEntry, error) {
431 dirs, err := os.ReadDir(name)
432 if err != nil && !os.IsNotExist(err) {
433 if info, err := os.Stat(name); err == nil && !info.IsDir() {
434 return nil, &fs.PathError{Op: "ReadDir", Path: name, Err: errNotDir}
435 }
436 }
437 return dirs, err
438 }
439
440 // ReadDir reads the named directory in the virtual file system.
441 func ReadDir(name string) ([]fs.DirEntry, error) {
442 Trace("ReadDir", name)
443
444 info := stat(name)
445 if info.deleted {
446 return nil, &fs.PathError{Op: "read", Path: name, Err: fs.ErrNotExist}
447 }
448 if !info.replaced {
449 return osReadDir(name)
450 }
451 if info.file {
452 return nil, &fs.PathError{Op: "read", Path: name, Err: errNotDir}
453 }
454
455 // Start with normal disk listing.
456 dirs, err := osReadDir(info.actual)
457 if err != nil && !os.IsNotExist(err) && !errors.Is(err, errNotDir) {
458 return nil, err
459 }
460 dirErr := err
461
462 // Merge disk listing and overlay entries in map.
463 all := make(map[string]fs.DirEntry)
464 for _, d := range dirs {
465 all[d.Name()] = d
466 }
467 for cname, cinfo := range info.children() {
468 if cinfo.dir {
469 all[cname] = fs.FileInfoToDirEntry(fakeDir(cname))
470 continue
471 }
472 if cinfo.deleted {
473 delete(all, cname)
474 continue
475 }
476
477 // Overlay is not allowed to have targets that are directories.
478 // And we hide symlinks, although it's not clear it helps callers.
479 cinfo, err := os.Stat(cinfo.actual)
480 if err != nil {
481 all[cname] = fs.FileInfoToDirEntry(missingFile(cname))
482 continue
483 }
484 if cinfo.IsDir() {
485 return nil, &fs.PathError{Op: "read", Path: name, Err: fmt.Errorf("overlay maps child %s to directory", cname)}
486 }
487 all[cname] = fs.FileInfoToDirEntry(fakeFile{cname, cinfo})
488 }
489
490 // Rebuild list using same storage.
491 dirs = dirs[:0]
492 for _, d := range all {
493 dirs = append(dirs, d)
494 }
495 slices.SortFunc(dirs, func(x, y fs.DirEntry) int { return strings.Compare(x.Name(), y.Name()) })
496
497 if len(dirs) == 0 {
498 return nil, dirErr
499 }
500 return dirs, nil
501 }
502
503 // Actual returns the actual file system path for the named file.
504 // It returns the empty string if name has been deleted in the virtual file system.
505 func Actual(name string) string {
506 info := stat(name)
507 if info.deleted {
508 return ""
509 }
510 if info.dir || info.replaced {
511 return info.actual
512 }
513 return name
514 }
515
516 // Replaced reports whether the named file has been modified
517 // in the virtual file system compared to the OS file system.
518 func Replaced(name string) bool {
519 info := stat(name)
520 return info.deleted || info.replaced && !info.dir
521 }
522
523 // DirContainsReplacement reports whether the named directory is affected by a replacement,
524 // either because a parent directory has been replaced, it has been replaced, or a file or
525 // directory under it has been replaced.
526 // It is meant to be used to detect cases where GOMODCACHE has been replaced. That replacement
527 // is not supported (GOMODCACHE is meant to be immutable) and the caller will use the
528 // information to return an error.
529 func DirContainsReplacement(name string) (string, bool) {
530 apath := abs(name)
531
532 // Check the overlay using similar logic to what stat uses.
533 i, ok := slices.BinarySearchFunc(overlay, apath, searchcmp)
534 if ok {
535 // The named directory itself has been replaced.
536 return overlay[i].from, true
537 }
538 if i < len(overlay) && str.HasFilePathPrefix(overlay[i].from, apath) {
539 // A file or directory contained in the named directory has been replaced.
540 return overlay[i].from, true
541 }
542 if i > 0 && str.HasFilePathPrefix(apath, overlay[i-1].from) {
543 // A parent of the named directory has been replaced.
544 return overlay[i-1].from, true
545 }
546 return "", false
547 }
548
549 // Open opens the named file in the virtual file system.
550 // It must be an ordinary file, not a directory.
551 func Open(name string) (*os.File, error) {
552 Trace("Open", name)
553
554 bad := func(msg string) (*os.File, error) {
555 return nil, &fs.PathError{
556 Op: "Open",
557 Path: name,
558 Err: errors.New(msg),
559 }
560 }
561
562 info := stat(name)
563 if info.deleted {
564 return bad("deleted in overlay")
565 }
566 if info.dir {
567 return bad("cannot open directory in overlay")
568 }
569 if info.replaced {
570 name = info.actual
571 }
572
573 return os.Open(name)
574 }
575
576 // ReadFile reads the named file from the virtual file system
577 // and returns the contents.
578 func ReadFile(name string) ([]byte, error) {
579 f, err := Open(name)
580 if err != nil {
581 return nil, err
582 }
583 defer f.Close()
584
585 return io.ReadAll(f)
586 }
587
588 // IsGoDir reports whether the named directory in the virtual file system
589 // is a directory containing one or more Go source files.
590 func IsGoDir(name string) (bool, error) {
591 Trace("IsGoDir", name)
592 fis, err := ReadDir(name)
593 if os.IsNotExist(err) || errors.Is(err, errNotDir) {
594 return false, nil
595 }
596 if err != nil {
597 return false, err
598 }
599
600 var firstErr error
601 for _, d := range fis {
602 if d.IsDir() || !strings.HasSuffix(d.Name(), ".go") {
603 continue
604 }
605 if d.Type().IsRegular() {
606 return true, nil
607 }
608
609 // d is a non-directory, non-regular .go file.
610 // Stat to see if it is a symlink, which we allow.
611 if actual := Actual(filepath.Join(name, d.Name())); actual != "" {
612 fi, err := os.Stat(actual)
613 if err == nil && fi.Mode().IsRegular() {
614 return true, nil
615 }
616 if err != nil && firstErr == nil {
617 firstErr = err
618 }
619 }
620 }
621
622 // No go files found in directory.
623 return false, firstErr
624 }
625
626 // Lstat returns a FileInfo describing the named file in the virtual file system.
627 // It does not follow symbolic links
628 func Lstat(name string) (fs.FileInfo, error) {
629 Trace("Lstat", name)
630 return overlayStat("lstat", name, os.Lstat)
631 }
632
633 // Stat returns a FileInfo describing the named file in the virtual file system.
634 // It follows symbolic links.
635 func Stat(name string) (fs.FileInfo, error) {
636 Trace("Stat", name)
637 return overlayStat("stat", name, os.Stat)
638 }
639
640 // overlayStat implements lstat or Stat (depending on whether os.Lstat or os.Stat is passed in).
641 func overlayStat(op, path string, osStat func(string) (fs.FileInfo, error)) (fs.FileInfo, error) {
642 info := stat(path)
643 if info.deleted {
644 return nil, &fs.PathError{Op: op, Path: path, Err: fs.ErrNotExist}
645 }
646 if info.dir {
647 return fakeDir(filepath.Base(path)), nil
648 }
649 if info.replaced {
650 // To keep the data model simple, if the overlay contains a symlink we
651 // always stat through it (using Stat, not Lstat). That way we don't need to
652 // worry about the interaction between Lstat and directories: if a symlink
653 // in the overlay points to a directory, we reject it like an ordinary
654 // directory.
655 ainfo, err := os.Stat(info.actual)
656 if err != nil {
657 return nil, err
658 }
659 if ainfo.IsDir() {
660 return nil, &fs.PathError{Op: op, Path: path, Err: fmt.Errorf("overlay maps to directory")}
661 }
662 return fakeFile{name: filepath.Base(path), real: ainfo}, nil
663 }
664 return osStat(path)
665 }
666
667 // fakeFile provides an fs.FileInfo implementation for an overlaid file,
668 // so that the file has the name of the overlaid file, but takes all
669 // other characteristics of the replacement file.
670 type fakeFile struct {
671 name string
672 real fs.FileInfo
673 }
674
675 func (f fakeFile) Name() string { return f.name }
676 func (f fakeFile) Size() int64 { return f.real.Size() }
677 func (f fakeFile) Mode() fs.FileMode { return f.real.Mode() }
678 func (f fakeFile) ModTime() time.Time { return f.real.ModTime() }
679 func (f fakeFile) IsDir() bool { return f.real.IsDir() }
680 func (f fakeFile) Sys() any { return f.real.Sys() }
681
682 func (f fakeFile) String() string {
683 return fs.FormatFileInfo(f)
684 }
685
686 // missingFile provides an fs.FileInfo for an overlaid file where the
687 // destination file in the overlay doesn't exist. It returns zero values
688 // for the fileInfo methods other than Name, set to the file's name, and Mode
689 // set to ModeIrregular.
690 type missingFile string
691
692 func (f missingFile) Name() string { return string(f) }
693 func (f missingFile) Size() int64 { return 0 }
694 func (f missingFile) Mode() fs.FileMode { return fs.ModeIrregular }
695 func (f missingFile) ModTime() time.Time { return time.Unix(0, 0) }
696 func (f missingFile) IsDir() bool { return false }
697 func (f missingFile) Sys() any { return nil }
698
699 func (f missingFile) String() string {
700 return fs.FormatFileInfo(f)
701 }
702
703 // fakeDir provides an fs.FileInfo implementation for directories that are
704 // implicitly created by overlaid files. Each directory in the
705 // path of an overlaid file is considered to exist in the overlay filesystem.
706 type fakeDir string
707
708 func (f fakeDir) Name() string { return string(f) }
709 func (f fakeDir) Size() int64 { return 0 }
710 func (f fakeDir) Mode() fs.FileMode { return fs.ModeDir | 0500 }
711 func (f fakeDir) ModTime() time.Time { return time.Unix(0, 0) }
712 func (f fakeDir) IsDir() bool { return true }
713 func (f fakeDir) Sys() any { return nil }
714
715 func (f fakeDir) String() string {
716 return fs.FormatFileInfo(f)
717 }
718
719 func cmp(x, y string) int {
720 for i := 0; i < len(x) && i < len(y); i++ {
721 xi := int(x[i])
722 yi := int(y[i])
723 if xi == filepath.Separator {
724 xi = -1
725 }
726 if yi == filepath.Separator {
727 yi = -1
728 }
729 if xi != yi {
730 return xi - yi
731 }
732 }
733 return len(x) - len(y)
734 }