| 1 | // Copyright 2016 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 buildutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/ |
| 6 | |
| 7 | import ( |
| 8 | "bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/" |
| 9 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 10 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 11 | "go/buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/" |
| 12 | "ioscip-go gomod github.com/golang/go/src go1.25.0 io/" |
| 13 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 14 | "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/" |
| 15 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 16 | ) |
| 17 | |
| 18 | // OverlayContext overlays a build.Context with additional files from |
| 19 | // a map. Files in the map take precedence over other files. |
| 20 | // |
| 21 | // In addition to plain string comparison, two file names are |
| 22 | // considered equal if their base names match and their directory |
| 23 | // components point at the same directory on the file system. That is, |
| 24 | // symbolic links are followed for directories, but not files. |
| 25 | // |
| 26 | // A common use case for OverlayContext is to allow editors to pass in |
| 27 | // a set of unsaved, modified files. |
| 28 | // |
| 29 | // Currently, only the Context.OpenFile function will respect the |
| 30 | // overlay. This may change in the future. |
| 31 | func OverlayContextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/OverlayContext().(orig *buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Contextscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#, overlay map[string][]byte) *buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Contextscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context# { |
| 32 | // TODO(dominikh): Implement IsDir, HasSubdir and ReadDir |
| 33 | |
| 34 | rc := func(data []byte) (ioscip-go gomod github.com/golang/go/src go1.25.0 io/.ReadCloserscip-go gomod github.com/golang/go/src go1.25.0 io/ReadCloser#, error) { |
| 35 | return ioscip-go gomod github.com/golang/go/src go1.25.0 io/.NopCloserscip-go gomod github.com/golang/go/src go1.25.0 io/NopCloser().(bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.NewBufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/NewBuffer().(data)), nil |
| 36 | } |
| 37 | |
| 38 | copy := *orig // make a copy |
| 39 | ctxt := © |
| 40 | ctxt.OpenFilescip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#OpenFile. = func(path string) (ioscip-go gomod github.com/golang/go/src go1.25.0 io/.ReadCloserscip-go gomod github.com/golang/go/src go1.25.0 io/ReadCloser#, error) { |
| 41 | // Fast path: names match exactly. |
| 42 | if content, ok := overlay[path]; ok { |
| 43 | return rc(content) |
| 44 | } |
| 45 | |
| 46 | // Slow path: check for same file under a different |
| 47 | // alias, perhaps due to a symbolic link. |
| 48 | for filename, content := range overlay { |
| 49 | if sameFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/sameFile().(path, filename) { |
| 50 | return rc(content) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return OpenFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/OpenFile().(orig, path) |
| 55 | } |
| 56 | return ctxt |
| 57 | } |
| 58 | |
| 59 | // ParseOverlayArchive parses an archive containing Go files and their |
| 60 | // contents. The result is intended to be used with OverlayContext. |
| 61 | // |
| 62 | // # Archive format |
| 63 | // |
| 64 | // The archive consists of a series of files. Each file consists of a |
| 65 | // name, a decimal file size and the file contents, separated by |
| 66 | // newlines. No newline follows after the file contents. |
| 67 | func ParseOverlayArchivescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/ParseOverlayArchive().(archive ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Readerscip-go gomod github.com/golang/go/src go1.25.0 io/Reader#) (map[string][]byte, error) { |
| 68 | overlay := make(map[string][]byte) |
| 69 | r := bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/.NewReaderscip-go gomod github.com/golang/go/src go1.25.0 bufio/NewReader().(archive) |
| 70 | for { |
| 71 | // Read file name. |
| 72 | filename, err := r.ReadStringscip-go gomod github.com/golang/go/src go1.25.0 bufio/Reader#ReadString().('\n') |
| 73 | if err != nil { |
| 74 | if err == ioscip-go gomod github.com/golang/go/src go1.25.0 io/.EOFscip-go gomod github.com/golang/go/src go1.25.0 io/EOF. { |
| 75 | break // OK |
| 76 | } |
| 77 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("reading archive file name: %v", err) |
| 78 | } |
| 79 | filename = filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Cleanscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Clean().(stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(filename)) |
| 80 | |
| 81 | // Read file size. |
| 82 | sz, err := r.ReadStringscip-go gomod github.com/golang/go/src go1.25.0 bufio/Reader#ReadString().('\n') |
| 83 | if err != nil { |
| 84 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("reading size of archive file %s: %v", filename, err) |
| 85 | } |
| 86 | sz = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(sz) |
| 87 | size, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseUintscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseUint().(sz, 10, 32) |
| 88 | if err != nil { |
| 89 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("parsing size of archive file %s: %v", filename, err) |
| 90 | } |
| 91 | |
| 92 | // Read file content. |
| 93 | content := make([]byte, size) |
| 94 | if _, err := ioscip-go gomod github.com/golang/go/src go1.25.0 io/.ReadFullscip-go gomod github.com/golang/go/src go1.25.0 io/ReadFull().(r, content); err != nil { |
| 95 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("reading archive file %s: %v", filename, err) |
| 96 | } |
| 97 | overlay[filename] = content |
| 98 | } |
| 99 | |
| 100 | return overlay, nil |
| 101 | } |