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.
5 // Package fsys is an abstraction for reading files that
6 // allows for virtual overlays on top of the files on disk.
27 // Trace emits a trace event for the operation and file path to the trace log,
28 // but only when $GODEBUG contains gofsystrace=1.
29 // The traces are appended to the file named by the $GODEBUG setting gofsystracelog, or else standard error.
30 // For debugging, if the $GODEBUG setting gofsystracestack is non-empty, then trace events for paths
31 // matching that glob pattern (using path.Match) will be followed by a full stack trace.
32 func Trace(op, path string) {
37 defer traceMu.Unlock()
38 fmt.Fprintf(traceFile, "%d gofsystrace %s %s\n", os.Getpid(), op, path)
39 if pattern := gofsystracestack.Value(); pattern != "" {
40 if match, _ := pathpkg.Match(pattern, path); match {
41 traceFile.Write(debug.Stack())
51 gofsystrace = godebug.New("gofsystrace")
52 gofsystracelog = godebug.New("gofsystracelog")
53 gofsystracestack = godebug.New("gofsystracestack")
57 if gofsystrace.Value() != "1" {
61 if f := gofsystracelog.Value(); f != "" {
62 // Note: No buffering on writes to this file, so no need to worry about closing it at exit.
64 traceFile, err = os.OpenFile(f, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
73 // OverlayFile is the path to a text file in the OverlayJSON format.
74 // It is the value of the -overlay flag.
75 var OverlayFile string
77 // OverlayJSON is the format overlay files are expected to be in.
78 // The Replace map maps from overlaid paths to replacement paths:
79 // the Go command will forward all reads trying to open
80 // each overlaid path to its replacement path, or consider the overlaid
81 // path not to exist if the replacement path is empty.
82 type OverlayJSON struct {
83 Replace map[string]string
87 actualFilePath string // empty if a directory
88 children map[string]*node // path element → file or directory
91 func (n *node) isDir() bool {
92 return n.actualFilePath == "" && n.children != nil
95 func (n *node) isDeleted() bool {
96 return n.actualFilePath == "" && n.children == nil
99 // TODO(matloob): encapsulate these in an io/fs-like interface
100 var overlay map[string]*node // path -> file or directory node
101 var cwd string // copy of base.Cwd() to avoid dependency
103 // canonicalize a path for looking it up in the overlay.
104 // Important: filepath.Join(cwd, path) doesn't always produce
105 // the correct absolute path if path is relative, because on
106 // Windows producing the correct absolute path requires making
107 // a syscall. So this should only be used when looking up paths
108 // in the overlay, or canonicalizing the paths in the overlay.
109 func canonicalize(path string) string {
113 if filepath.IsAbs(path) {
114 return filepath.Clean(path)
117 if v := filepath.VolumeName(cwd); v != "" && path[0] == filepath.Separator {
118 // On Windows filepath.Join(cwd, path) doesn't always work. In general
119 // filepath.Abs needs to make a syscall on Windows. Elsewhere in cmd/go
120 // use filepath.Join(cwd, path), but cmd/go specifically supports Windows
121 // paths that start with "\" which implies the path is relative to the
122 // volume of the working directory. See golang.org/issue/8130.
123 return filepath.Join(v, path)
126 // Make the path absolute.
127 return filepath.Join(cwd, path)
130 // Init initializes the overlay, if one is being used.
131 func Init(wd string) error {
133 // already initialized
139 if OverlayFile == "" {
143 Trace("ReadFile", OverlayFile)
144 b, err := os.ReadFile(OverlayFile)
146 return fmt.Errorf("reading overlay file: %v", err)
149 var overlayJSON OverlayJSON
150 if err := json.Unmarshal(b, &overlayJSON); err != nil {
151 return fmt.Errorf("parsing overlay JSON: %v", err)
154 return initFromJSON(overlayJSON)
157 func initFromJSON(overlayJSON OverlayJSON) error {
158 // Canonicalize the paths in the overlay map.
159 // Use reverseCanonicalized to check for collisions:
160 // no two 'from' paths should canonicalize to the same path.
161 overlay = make(map[string]*node)
162 reverseCanonicalized := make(map[string]string) // inverse of canonicalize operation, to check for duplicates
163 // Build a table of file and directory nodes from the replacement map.
165 // Remove any potential non-determinism from iterating over map by sorting it.
166 replaceFrom := make([]string, 0, len(overlayJSON.Replace))
167 for k := range overlayJSON.Replace {
168 replaceFrom = append(replaceFrom, k)
170 sort.Strings(replaceFrom)
172 for _, from := range replaceFrom {
173 to := overlayJSON.Replace[from]
174 // Canonicalize paths and check for a collision.
176 return fmt.Errorf("empty string key in overlay file Replace map")
178 cfrom := canonicalize(from)
180 // Don't canonicalize "", meaning to delete a file, because then it will turn into ".".
181 to = canonicalize(to)
183 if otherFrom, seen := reverseCanonicalized[cfrom]; seen {
185 "paths %q and %q both canonicalize to %q in overlay file Replace map", otherFrom, from, cfrom)
187 reverseCanonicalized[cfrom] = from
190 // Create node for overlaid file.
191 dir, base := filepath.Dir(from), filepath.Base(from)
192 if n, ok := overlay[from]; ok {
193 // All 'from' paths in the overlay are file paths. Since the from paths
194 // are in a map, they are unique, so if the node already exists we added
195 // it below when we create parent directory nodes. That is, that
196 // both a file and a path to one of its parent directories exist as keys
197 // in the Replace map.
199 // This only applies if the overlay directory has any files or directories
200 // in it: placeholder directories that only contain deleted files don't
201 // count. They are safe to be overwritten with actual files.
202 for _, f := range n.children {
204 return fmt.Errorf("invalid overlay: path %v is used as both file and directory", from)
208 overlay[from] = &node{actualFilePath: to}
210 // Add parent directory nodes to overlay structure.
211 childNode := overlay[from]
213 dirNode := overlay[dir]
214 if dirNode == nil || dirNode.isDeleted() {
215 dirNode = &node{children: make(map[string]*node)}
216 overlay[dir] = dirNode
218 if childNode.isDeleted() {
219 // Only create one parent for a deleted file:
220 // the directory only conditionally exists if
221 // there are any non-deleted children, so
222 // we don't create their parents.
224 dirNode.children[base] = childNode
228 if !dirNode.isDir() {
229 // This path already exists as a file, so it can't be a parent
230 // directory. See comment at error above.
231 return fmt.Errorf("invalid overlay: path %v is used as both file and directory", dir)
233 dirNode.children[base] = childNode
234 parent := filepath.Dir(dir)
236 break // reached the top; there is no parent
238 dir, base = parent, filepath.Base(dir)
246 // IsDir returns true if path is a directory on disk or in the
248 func IsDir(path string) (bool, error) {
250 path = canonicalize(path)
252 if _, ok := parentIsOverlayFile(path); ok {
256 if n, ok := overlay[path]; ok {
257 return n.isDir(), nil
260 fi, err := os.Stat(path)
265 return fi.IsDir(), nil
268 // parentIsOverlayFile returns whether name or any of
269 // its parents are files in the overlay, and the first parent found,
270 // including name itself, that's a file in the overlay.
271 func parentIsOverlayFile(name string) (string, bool) {
273 // Check if name can't possibly be a directory because
274 // it or one of its parents is overlaid with a file.
275 // TODO(matloob): Maybe save this to avoid doing it every time?
278 node := overlay[prefix]
279 if node != nil && !node.isDir() {
282 parent := filepath.Dir(prefix)
283 if parent == prefix {
293 // errNotDir is used to communicate from ReadDir to IsDirWithGoFiles
294 // that the argument is not a directory, so that IsDirWithGoFiles doesn't
296 var errNotDir = errors.New("not a directory")
298 func nonFileInOverlayError(overlayPath string) error {
299 return fmt.Errorf("replacement path %q is a directory, not a file", overlayPath)
302 // readDir reads a dir on disk, returning an error that is errNotDir if the dir is not a directory.
303 // Unfortunately, the error returned by os.ReadDir if dir is not a directory
304 // can vary depending on the OS (Linux, Mac, Windows return ENOTDIR; BSD returns EINVAL).
305 func readDir(dir string) ([]fs.FileInfo, error) {
306 entries, err := os.ReadDir(dir)
308 if os.IsNotExist(err) {
311 if dirfi, staterr := os.Stat(dir); staterr == nil && !dirfi.IsDir() {
312 return nil, &fs.PathError{Op: "ReadDir", Path: dir, Err: errNotDir}
317 fis := make([]fs.FileInfo, 0, len(entries))
318 for _, entry := range entries {
319 info, err := entry.Info()
323 fis = append(fis, info)
328 // ReadDir provides a slice of fs.FileInfo entries corresponding
329 // to the overlaid files in the directory.
330 func ReadDir(dir string) ([]fs.FileInfo, error) {
331 Trace("ReadDir", dir)
332 dir = canonicalize(dir)
333 if _, ok := parentIsOverlayFile(dir); ok {
334 return nil, &fs.PathError{Op: "ReadDir", Path: dir, Err: errNotDir}
337 dirNode := overlay[dir]
341 if dirNode.isDeleted() {
342 return nil, &fs.PathError{Op: "ReadDir", Path: dir, Err: fs.ErrNotExist}
344 diskfis, err := readDir(dir)
345 if err != nil && !os.IsNotExist(err) && !errors.Is(err, errNotDir) {
349 // Stat files in overlay to make composite list of fileinfos
350 files := make(map[string]fs.FileInfo)
351 for _, f := range diskfis {
354 for name, to := range dirNode.children {
357 files[name] = fakeDir(name)
361 // To keep the data model simple, if the overlay contains a symlink we
362 // always stat through it (using Stat, not Lstat). That way we don't need
363 // to worry about the interaction between Lstat and directories: if a
364 // symlink in the overlay points to a directory, we reject it like an
365 // ordinary directory.
366 fi, err := os.Stat(to.actualFilePath)
368 files[name] = missingFile(name)
370 } else if fi.IsDir() {
371 return nil, &fs.PathError{Op: "Stat", Path: filepath.Join(dir, name), Err: nonFileInOverlayError(to.actualFilePath)}
373 // Add a fileinfo for the overlaid file, so that it has
374 // the original file's name, but the overlaid file's metadata.
375 files[name] = fakeFile{name, fi}
378 sortedFiles := diskfis[:0]
379 for _, f := range files {
380 sortedFiles = append(sortedFiles, f)
382 sort.Slice(sortedFiles, func(i, j int) bool { return sortedFiles[i].Name() < sortedFiles[j].Name() })
383 return sortedFiles, nil
386 // OverlayPath returns the path to the overlaid contents of the
387 // file, the empty string if the overlay deletes the file, or path
388 // itself if the file is not in the overlay, the file is a directory
389 // in the overlay, or there is no overlay.
390 // It returns true if the path is overlaid with a regular file
391 // or deleted, and false otherwise.
392 func OverlayPath(path string) (string, bool) {
393 if p, ok := overlay[canonicalize(path)]; ok && !p.isDir() {
394 return p.actualFilePath, ok
400 // Open opens the file at or overlaid on the given path.
401 func Open(path string) (*os.File, error) {
403 return openFile(path, os.O_RDONLY, 0)
406 // OpenFile opens the file at or overlaid on the given path with the flag and perm.
407 func OpenFile(path string, flag int, perm os.FileMode) (*os.File, error) {
408 Trace("OpenFile", path)
409 return openFile(path, flag, perm)
412 func openFile(path string, flag int, perm os.FileMode) (*os.File, error) {
413 cpath := canonicalize(path)
414 if node, ok := overlay[cpath]; ok {
415 // Opening a file in the overlay.
417 return nil, &fs.PathError{Op: "OpenFile", Path: path, Err: errors.New("fsys.OpenFile doesn't support opening directories yet")}
419 // We can't open overlaid paths for write.
420 if perm != os.FileMode(os.O_RDONLY) {
421 return nil, &fs.PathError{Op: "OpenFile", Path: path, Err: errors.New("overlaid files can't be opened for write")}
423 return os.OpenFile(node.actualFilePath, flag, perm)
425 if parent, ok := parentIsOverlayFile(filepath.Dir(cpath)); ok {
426 // The file is deleted explicitly in the Replace map,
427 // or implicitly because one of its parent directories was
428 // replaced by a file.
429 return nil, &fs.PathError{
432 Err: fmt.Errorf("file %s does not exist: parent directory %s is replaced by a file in overlay", path, parent),
435 return os.OpenFile(cpath, flag, perm)
438 // IsDirWithGoFiles reports whether dir is a directory containing Go files
439 // either on disk or in the overlay.
440 func IsDirWithGoFiles(dir string) (bool, error) {
441 Trace("IsDirWithGoFiles", dir)
442 fis, err := ReadDir(dir)
443 if os.IsNotExist(err) || errors.Is(err, errNotDir) {
451 for _, fi := range fis {
456 // TODO(matloob): this enforces that the "from" in the map
457 // has a .go suffix, but the actual destination file
458 // doesn't need to have a .go suffix. Is this okay with the
460 if !strings.HasSuffix(fi.Name(), ".go") {
463 if fi.Mode().IsRegular() {
467 // fi is the result of an Lstat, so it doesn't follow symlinks.
468 // But it's okay if the file is a symlink pointing to a regular
469 // file, so use os.Stat to follow symlinks and check that.
470 actualFilePath, _ := OverlayPath(filepath.Join(dir, fi.Name()))
471 fi, err := os.Stat(actualFilePath)
472 if err == nil && fi.Mode().IsRegular() {
475 if err != nil && firstErr == nil {
480 // No go files found in directory.
481 return false, firstErr
484 // walk recursively descends path, calling walkFn. Copied, with some
485 // modifications from path/filepath.walk.
486 func walk(path string, info fs.FileInfo, walkFn filepath.WalkFunc) error {
487 if err := walkFn(path, info, nil); err != nil || !info.IsDir() {
491 fis, err := ReadDir(path)
493 return walkFn(path, info, err)
496 for _, fi := range fis {
497 filename := filepath.Join(path, fi.Name())
498 if err := walk(filename, fi, walkFn); err != nil {
499 if !fi.IsDir() || err != filepath.SkipDir {
507 // Walk walks the file tree rooted at root, calling walkFn for each file or
508 // directory in the tree, including root.
509 func Walk(root string, walkFn filepath.WalkFunc) error {
511 info, err := Lstat(root)
513 err = walkFn(root, nil, err)
515 err = walk(root, info, walkFn)
517 if err == filepath.SkipDir {
523 // Lstat implements a version of os.Lstat that operates on the overlay filesystem.
524 func Lstat(path string) (fs.FileInfo, error) {
526 return overlayStat(path, os.Lstat, "lstat")
529 // Stat implements a version of os.Stat that operates on the overlay filesystem.
530 func Stat(path string) (fs.FileInfo, error) {
532 return overlayStat(path, os.Stat, "stat")
535 // overlayStat implements lstat or Stat (depending on whether os.Lstat or os.Stat is passed in).
536 func overlayStat(path string, osStat func(string) (fs.FileInfo, error), opName string) (fs.FileInfo, error) {
537 cpath := canonicalize(path)
539 if _, ok := parentIsOverlayFile(filepath.Dir(cpath)); ok {
540 return nil, &fs.PathError{Op: opName, Path: cpath, Err: fs.ErrNotExist}
543 node, ok := overlay[cpath]
545 // The file or directory is not overlaid.
550 case node.isDeleted():
551 return nil, &fs.PathError{Op: opName, Path: cpath, Err: fs.ErrNotExist}
553 return fakeDir(filepath.Base(path)), nil
555 // To keep the data model simple, if the overlay contains a symlink we
556 // always stat through it (using Stat, not Lstat). That way we don't need to
557 // worry about the interaction between Lstat and directories: if a symlink
558 // in the overlay points to a directory, we reject it like an ordinary
560 fi, err := os.Stat(node.actualFilePath)
565 return nil, &fs.PathError{Op: opName, Path: cpath, Err: nonFileInOverlayError(node.actualFilePath)}
567 return fakeFile{name: filepath.Base(path), real: fi}, nil
571 // fakeFile provides an fs.FileInfo implementation for an overlaid file,
572 // so that the file has the name of the overlaid file, but takes all
573 // other characteristics of the replacement file.
574 type fakeFile struct {
579 func (f fakeFile) Name() string { return f.name }
580 func (f fakeFile) Size() int64 { return f.real.Size() }
581 func (f fakeFile) Mode() fs.FileMode { return f.real.Mode() }
582 func (f fakeFile) ModTime() time.Time { return f.real.ModTime() }
583 func (f fakeFile) IsDir() bool { return f.real.IsDir() }
584 func (f fakeFile) Sys() any { return f.real.Sys() }
586 // missingFile provides an fs.FileInfo for an overlaid file where the
587 // destination file in the overlay doesn't exist. It returns zero values
588 // for the fileInfo methods other than Name, set to the file's name, and Mode
589 // set to ModeIrregular.
590 type missingFile string
592 func (f missingFile) Name() string { return string(f) }
593 func (f missingFile) Size() int64 { return 0 }
594 func (f missingFile) Mode() fs.FileMode { return fs.ModeIrregular }
595 func (f missingFile) ModTime() time.Time { return time.Unix(0, 0) }
596 func (f missingFile) IsDir() bool { return false }
597 func (f missingFile) Sys() any { return nil }
599 // fakeDir provides an fs.FileInfo implementation for directories that are
600 // implicitly created by overlaid files. Each directory in the
601 // path of an overlaid file is considered to exist in the overlay filesystem.
604 func (f fakeDir) Name() string { return string(f) }
605 func (f fakeDir) Size() int64 { return 0 }
606 func (f fakeDir) Mode() fs.FileMode { return fs.ModeDir | 0500 }
607 func (f fakeDir) ModTime() time.Time { return time.Unix(0, 0) }
608 func (f fakeDir) IsDir() bool { return true }
609 func (f fakeDir) Sys() any { return nil }
611 // Glob is like filepath.Glob but uses the overlay file system.
612 func Glob(pattern string) (matches []string, err error) {
613 Trace("Glob", pattern)
614 // Check pattern is well-formed.
615 if _, err := filepath.Match(pattern, ""); err != nil {
618 if !hasMeta(pattern) {
619 if _, err = Lstat(pattern); err != nil {
622 return []string{pattern}, nil
625 dir, file := filepath.Split(pattern)
627 if runtime.GOOS == "windows" {
628 volumeLen, dir = cleanGlobPathWindows(dir)
630 dir = cleanGlobPath(dir)
633 if !hasMeta(dir[volumeLen:]) {
634 return glob(dir, file, nil)
637 // Prevent infinite recursion. See issue 15879.
639 return nil, filepath.ErrBadPattern
647 for _, d := range m {
648 matches, err = glob(d, file, matches)
656 // cleanGlobPath prepares path for glob matching.
657 func cleanGlobPath(path string) string {
661 case string(filepath.Separator):
662 // do nothing to the path
665 return path[0 : len(path)-1] // chop off trailing separator
669 func volumeNameLen(path string) int {
670 isSlash := func(c uint8) bool {
671 return c == '\\' || c == '/'
678 if path[1] == ':' && ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') {
681 // is it UNC? https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
682 if l := len(path); l >= 5 && isSlash(path[0]) && isSlash(path[1]) &&
683 !isSlash(path[2]) && path[2] != '.' {
684 // first, leading `\\` and next shouldn't be `\`. its server name.
685 for n := 3; n < l-1; n++ {
686 // second, next '\' shouldn't be repeated.
687 if isSlash(path[n]) {
689 // third, following something characters. its share name.
690 if !isSlash(path[n]) {
695 if isSlash(path[n]) {
708 // cleanGlobPathWindows is windows version of cleanGlobPath.
709 func cleanGlobPathWindows(path string) (prefixLen int, cleaned string) {
710 vollen := volumeNameLen(path)
714 case vollen+1 == len(path) && os.IsPathSeparator(path[len(path)-1]): // /, \, C:\ and C:/
715 // do nothing to the path
716 return vollen + 1, path
717 case vollen == len(path) && len(path) == 2: // C:
718 return vollen, path + "." // convert C: into C:.
720 if vollen >= len(path) {
721 vollen = len(path) - 1
723 return vollen, path[0 : len(path)-1] // chop off trailing separator
727 // glob searches for files matching pattern in the directory dir
728 // and appends them to matches. If the directory cannot be
729 // opened, it returns the existing matches. New matches are
730 // added in lexicographical order.
731 func glob(dir, pattern string, matches []string) (m []string, e error) {
735 return // ignore I/O error
738 return // ignore I/O error
741 list, err := ReadDir(dir)
743 return // ignore I/O error
747 for _, info := range list {
748 names = append(names, info.Name())
752 for _, n := range names {
753 matched, err := filepath.Match(pattern, n)
758 m = append(m, filepath.Join(dir, n))
764 // hasMeta reports whether path contains any of the magic characters
765 // recognized by filepath.Match.
766 func hasMeta(path string) bool {
768 if runtime.GOOS != "windows" {
771 return strings.ContainsAny(path, magicChars)