forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/tools/go/buildutil/overlay.go

171 lines
2.9 KiB
Go
Raw Normal View History

2024-05-14 13:07:09 +00:00
// Copyright 2016 The Go Authors. All rights reserved.
2024-05-14 13:07:09 +00:00
// Use of this source code is governed by a BSD-style
2024-05-14 13:07:09 +00:00
// license that can be found in the LICENSE file.
package buildutil
import (
"bufio"
"bytes"
"fmt"
"go/build"
"io"
"path/filepath"
"strconv"
"strings"
)
// OverlayContext overlays a build.Context with additional files from
2024-05-14 13:07:09 +00:00
// a map. Files in the map take precedence over other files.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// In addition to plain string comparison, two file names are
2024-05-14 13:07:09 +00:00
// considered equal if their base names match and their directory
2024-05-14 13:07:09 +00:00
// components point at the same directory on the file system. That is,
2024-05-14 13:07:09 +00:00
// symbolic links are followed for directories, but not files.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// A common use case for OverlayContext is to allow editors to pass in
2024-05-14 13:07:09 +00:00
// a set of unsaved, modified files.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Currently, only the Context.OpenFile function will respect the
2024-05-14 13:07:09 +00:00
// overlay. This may change in the future.
2024-05-14 13:07:09 +00:00
func OverlayContext(orig *build.Context, overlay map[string][]byte) *build.Context {
2024-05-14 13:07:09 +00:00
// TODO(dominikh): Implement IsDir, HasSubdir and ReadDir
rc := func(data []byte) (io.ReadCloser, error) {
2024-05-14 13:07:09 +00:00
return io.NopCloser(bytes.NewBuffer(data)), nil
2024-05-14 13:07:09 +00:00
}
copy := *orig // make a copy
2024-05-14 13:07:09 +00:00
ctxt := &copy
2024-05-14 13:07:09 +00:00
ctxt.OpenFile = func(path string) (io.ReadCloser, error) {
2024-05-14 13:07:09 +00:00
// Fast path: names match exactly.
2024-05-14 13:07:09 +00:00
if content, ok := overlay[path]; ok {
2024-05-14 13:07:09 +00:00
return rc(content)
2024-05-14 13:07:09 +00:00
}
// Slow path: check for same file under a different
2024-05-14 13:07:09 +00:00
// alias, perhaps due to a symbolic link.
2024-05-14 13:07:09 +00:00
for filename, content := range overlay {
2024-05-14 13:07:09 +00:00
if sameFile(path, filename) {
2024-05-14 13:07:09 +00:00
return rc(content)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
return OpenFile(orig, path)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return ctxt
2024-05-14 13:07:09 +00:00
}
// ParseOverlayArchive parses an archive containing Go files and their
2024-05-14 13:07:09 +00:00
// contents. The result is intended to be used with OverlayContext.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// # Archive format
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// The archive consists of a series of files. Each file consists of a
2024-05-14 13:07:09 +00:00
// name, a decimal file size and the file contents, separated by
2024-05-14 13:07:09 +00:00
// newlines. No newline follows after the file contents.
2024-05-14 13:07:09 +00:00
func ParseOverlayArchive(archive io.Reader) (map[string][]byte, error) {
2024-05-14 13:07:09 +00:00
overlay := make(map[string][]byte)
2024-05-14 13:07:09 +00:00
r := bufio.NewReader(archive)
2024-05-14 13:07:09 +00:00
for {
2024-05-14 13:07:09 +00:00
// Read file name.
2024-05-14 13:07:09 +00:00
filename, err := r.ReadString('\n')
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
if err == io.EOF {
2024-05-14 13:07:09 +00:00
break // OK
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return nil, fmt.Errorf("reading archive file name: %v", err)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
filename = filepath.Clean(strings.TrimSpace(filename))
// Read file size.
2024-05-14 13:07:09 +00:00
sz, err := r.ReadString('\n')
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, fmt.Errorf("reading size of archive file %s: %v", filename, err)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
sz = strings.TrimSpace(sz)
2024-05-14 13:07:09 +00:00
size, err := strconv.ParseUint(sz, 10, 32)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, fmt.Errorf("parsing size of archive file %s: %v", filename, err)
2024-05-14 13:07:09 +00:00
}
// Read file content.
2024-05-14 13:07:09 +00:00
content := make([]byte, size)
2024-05-14 13:07:09 +00:00
if _, err := io.ReadFull(r, content); err != nil {
2024-05-14 13:07:09 +00:00
return nil, fmt.Errorf("reading archive file %s: %v", filename, err)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
overlay[filename] = content
2024-05-14 13:07:09 +00:00
}
return overlay, nil
2024-05-14 13:07:09 +00:00
}