1 // Copyright 2019 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.
15 // overlayDir makes a minimal-overhead copy of srcRoot in which new files may be added.
17 // TODO: Once we no longer need to support the misc module in GOPATH mode,
18 // factor this function out into a package to reduce duplication.
19 func overlayDir(dstRoot, srcRoot string) error {
20 dstRoot = filepath.Clean(dstRoot)
21 if err := os.MkdirAll(dstRoot, 0777); err != nil {
25 srcRoot, err := filepath.Abs(srcRoot)
30 return filepath.WalkDir(srcRoot, func(srcPath string, entry fs.DirEntry, err error) error {
31 if err != nil || srcPath == srcRoot {
34 if filepath.Base(srcPath) == "testdata" {
35 // We're just building, so no need to copy those.
39 suffix := strings.TrimPrefix(srcPath, srcRoot)
40 for len(suffix) > 0 && suffix[0] == filepath.Separator {
43 dstPath := filepath.Join(dstRoot, suffix)
45 info, err := entry.Info()
46 perm := info.Mode() & os.ModePerm
47 if info.Mode()&os.ModeSymlink != 0 {
48 info, err = os.Stat(srcPath)
52 perm = info.Mode() & os.ModePerm
55 // Always make copies of directories.
56 // If we add a file in the overlay, we don't want to add it in the original.
58 return os.MkdirAll(dstPath, perm|0200)
61 // If we can use a hard link, do that instead of copying bytes.
62 // Go builds don't like symlinks in some cases, such as go:embed.
63 if err := os.Link(srcPath, dstPath); err == nil {
67 // Otherwise, copy the bytes.
68 src, err := os.Open(srcPath)
74 dst, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
79 _, err = io.Copy(dst, src)
80 if closeErr := dst.Close(); err == nil {