mirror of
https://github.com/golang/go
synced 2024-11-14 06:20:23 -07:00
ef06a5f44a
go:embed disallows using symlinked files by design. crypto/elliptic is the first std package to use it as of CL 380475, and unfortunately that broke the TestRepeatBootstrap long test. The reason it uses symlinks is for speed; it wants to copy GOROOT/src, but regular files aren't going to be modified in any way, so a symlink, if supported, means not needing to copy the contents. Replace the symlink attempt with hard links, which will mean regular files remain as such, fixing go:embed. It's worth noting that on many systems hard links won't work, as the temporary filesystem tends to be separate, but it doesn't hurt to try. In my system, where /tmp is tmpfs, the test now copies more bytes. With the added Logf, I can see overlayDir goes from ~30ms to ~100ms. This makes sense, as GOROOT/src currently weighs around 100MiB. To alleviate that slow-down, stop copying testdata directories, as they currently weigh around 20MiB and aren't needed for the test. This gets overlayDir on my system down to an acceptable ~70ms. I briefly considered teaching overlayDir what files can be symlinks, but that seemed fairly complex long-term, as any file could be embedded. While here, start using testing.T.TempDir and fs.WalkDir. For #50995. Change-Id: I17947e6bdee96237e1ca0606ad0b95e7c5987bc1 Reviewed-on: https://go-review.googlesource.com/c/go/+/383995 Trust: Daniel Martí <mvdan@mvdan.cc> Trust: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
86 lines
2.1 KiB
Go
86 lines
2.1 KiB
Go
// Copyright 2019 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package reboot_test
|
|
|
|
import (
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// overlayDir makes a minimal-overhead copy of srcRoot in which new files may be added.
|
|
//
|
|
// TODO: Once we no longer need to support the misc module in GOPATH mode,
|
|
// factor this function out into a package to reduce duplication.
|
|
func overlayDir(dstRoot, srcRoot string) error {
|
|
dstRoot = filepath.Clean(dstRoot)
|
|
if err := os.MkdirAll(dstRoot, 0777); err != nil {
|
|
return err
|
|
}
|
|
|
|
srcRoot, err := filepath.Abs(srcRoot)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return filepath.WalkDir(srcRoot, func(srcPath string, entry fs.DirEntry, err error) error {
|
|
if err != nil || srcPath == srcRoot {
|
|
return err
|
|
}
|
|
if filepath.Base(srcPath) == "testdata" {
|
|
// We're just building, so no need to copy those.
|
|
return fs.SkipDir
|
|
}
|
|
|
|
suffix := strings.TrimPrefix(srcPath, srcRoot)
|
|
for len(suffix) > 0 && suffix[0] == filepath.Separator {
|
|
suffix = suffix[1:]
|
|
}
|
|
dstPath := filepath.Join(dstRoot, suffix)
|
|
|
|
info, err := entry.Info()
|
|
perm := info.Mode() & os.ModePerm
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
info, err = os.Stat(srcPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
perm = info.Mode() & os.ModePerm
|
|
}
|
|
|
|
// Always make copies of directories.
|
|
// If we add a file in the overlay, we don't want to add it in the original.
|
|
if info.IsDir() {
|
|
return os.MkdirAll(dstPath, perm|0200)
|
|
}
|
|
|
|
// If we can use a hard link, do that instead of copying bytes.
|
|
// Go builds don't like symlinks in some cases, such as go:embed.
|
|
if err := os.Link(srcPath, dstPath); err == nil {
|
|
return nil
|
|
}
|
|
|
|
// Otherwise, copy the bytes.
|
|
src, err := os.Open(srcPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer src.Close()
|
|
|
|
dst, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = io.Copy(dst, src)
|
|
if closeErr := dst.Close(); err == nil {
|
|
err = closeErr
|
|
}
|
|
return err
|
|
})
|
|
}
|