summaryrefslogtreecommitdiff
path: root/vendor/golang.org/x/tools/internal/modindex/index.go
blob: c41d1dd903575b5695a8b5f33c4b30a20a1e95e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2024 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 modindex

import (
	"bufio"
	"crypto/sha256"
	"encoding/csv"
	"fmt"
	"io"
	"log"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"testing"
	"time"
)

/*
The on-disk index ("payload") is a text file.
The first 3 lines are header information containing CurrentVersion,
the value of GOMODCACHE, and the validity date of the index.
(This is when the code started building the index.)
Following the header are sections of lines, one section for each
import path. These sections are sorted by package name.
The first line of each section, marked by a leading :, contains
the package name, the import path, the name of the directory relative
to GOMODCACHE, and its semantic version.
The rest of each section consists of one line per exported symbol.
The lines are sorted by the symbol's name and contain the name,
an indication of its lexical type (C, T, V, F), and if it is the
name of a function, information about the signature.

The fields in the section header lines are separated by commas, and
in the unlikely event this would be confusing, the csv package is used
to write (and read) them.

In the lines containing exported names, C=const, V=var, T=type, F=func.
If it is a func, the next field is the number of returned values,
followed by pairs consisting of formal parameter names and types.
All these fields are separated by spaces. Any spaces in a type
(e.g., chan struct{}) are replaced by $s on the disk. The $s are
turned back into spaces when read.

Here is an index header (the comments are not part of the index):
0                                      // version (of the index format)
/usr/local/google/home/pjw/go/pkg/mod  // GOMODCACHE
2024-09-11 18:55:09                    // validity date of the index

Here is an index section:
:yaml,gopkg.in/yaml.v1,gopkg.in/yaml.v1@v1.0.0-20140924161607-9f9df34309c0,v1.0.0-20140924161607-9f9df34309c0
Getter T
Marshal F 2 in interface{}
Setter T
Unmarshal F 1 in []byte out interface{}

The package name is yaml, the import path is gopkg.in/yaml.v1.
Getter and Setter are types, and Marshal and Unmarshal are functions.
The latter returns one value and has two arguments, 'in' and 'out'
whose types are []byte and interface{}.
*/

// CurrentVersion tells readers about the format of the index.
const CurrentVersion int = 0

// Index is returned by [Read].
type Index struct {
	Version    int
	GOMODCACHE string    // absolute path of Go module cache dir
	ValidAt    time.Time // moment at which the index was up to date
	Entries    []Entry
}

func (ix *Index) String() string {
	return fmt.Sprintf("Index(%s v%d has %d entries at %v)",
		ix.GOMODCACHE, ix.Version, len(ix.Entries), ix.ValidAt)
}

// An Entry contains information for an import path.
type Entry struct {
	Dir        string // package directory relative to GOMODCACHE; uses OS path separator
	ImportPath string
	PkgName    string
	Version    string
	Names      []string // exported names and information
}

// IndexDir is where the module index is stored.
// Each logical index entry consists of a pair of files:
//
//   - the "payload" (index-VERSION-XXX), whose name is
//     randomized, holds the actual index; and
//   - the "link" (index-name-VERSION-HASH),
//     whose name is predictable, contains the
//     name of the payload file.
//
// Since the link file is small (<512B),
// reads and writes to it may be assumed atomic.
var IndexDir string = func() string {
	var dir string
	if testing.Testing() {
		dir = os.TempDir()
	} else {
		var err error
		dir, err = os.UserCacheDir()
		// shouldn't happen, but TempDir is better than
		// creating ./go/imports
		if err != nil {
			dir = os.TempDir()
		}
	}
	dir = filepath.Join(dir, "goimports")
	if err := os.MkdirAll(dir, 0777); err != nil {
		log.Printf("failed to create modcache index dir: %v", err)
	}
	return dir
}()

// Read reads the latest version of the on-disk index
// for the specified Go module cache directory.
// If there is no index, it returns a nil Index and an fs.ErrNotExist error.
func Read(gomodcache string) (*Index, error) {
	gomodcache, err := filepath.Abs(gomodcache)
	if err != nil {
		return nil, err
	}

	// Read the "link" file for the specified gomodcache directory.
	// It names the payload file.
	content, err := os.ReadFile(filepath.Join(IndexDir, linkFileBasename(gomodcache)))
	if err != nil {
		return nil, err
	}
	payloadFile := filepath.Join(IndexDir, string(content))

	// Read the index out of the payload file.
	f, err := os.Open(payloadFile)
	if err != nil {
		return nil, err
	}
	defer f.Close()
	return readIndexFrom(gomodcache, bufio.NewReader(f))
}

func readIndexFrom(gomodcache string, r io.Reader) (*Index, error) {
	scan := bufio.NewScanner(r)

	// version
	if !scan.Scan() {
		return nil, fmt.Errorf("unexpected scan error: %v", scan.Err())
	}
	version, err := strconv.Atoi(scan.Text())
	if err != nil {
		return nil, err
	}
	if version != CurrentVersion {
		return nil, fmt.Errorf("got version %d, expected %d", version, CurrentVersion)
	}

	// gomodcache
	if !scan.Scan() {
		return nil, fmt.Errorf("scanner error reading module cache dir: %v", scan.Err())
	}
	// TODO(pjw): need to check that this is the expected cache dir
	// so the tag should be passed in to this function
	if dir := string(scan.Text()); dir != gomodcache {
		return nil, fmt.Errorf("index file GOMODCACHE mismatch: got %q, want %q", dir, gomodcache)
	}

	// changed
	if !scan.Scan() {
		return nil, fmt.Errorf("scanner error reading index creation time: %v", scan.Err())
	}
	changed, err := time.ParseInLocation(time.DateTime, scan.Text(), time.Local)
	if err != nil {
		return nil, err
	}

	// entries
	var (
		curEntry *Entry
		entries  []Entry
	)
	for scan.Scan() {
		v := scan.Text()
		if v[0] == ':' {
			if curEntry != nil {
				entries = append(entries, *curEntry)
			}
			// as directories may contain commas and quotes, they need to be read as csv.
			rdr := strings.NewReader(v[1:])
			cs := csv.NewReader(rdr)
			flds, err := cs.Read()
			if err != nil {
				return nil, err
			}
			if len(flds) != 4 {
				return nil, fmt.Errorf("header contains %d fields, not 4: %q", len(v), v)
			}
			curEntry = &Entry{
				PkgName:    flds[0],
				ImportPath: flds[1],
				Dir:        relative(gomodcache, flds[2]),
				Version:    flds[3],
			}
			continue
		}
		curEntry.Names = append(curEntry.Names, v)
	}
	if err := scan.Err(); err != nil {
		return nil, fmt.Errorf("scanner failed while reading modindex entry: %v", err)
	}
	if curEntry != nil {
		entries = append(entries, *curEntry)
	}

	return &Index{
		Version:    version,
		GOMODCACHE: gomodcache,
		ValidAt:    changed,
		Entries:    entries,
	}, nil
}

// write writes the index file and updates the index directory to refer to it.
func write(gomodcache string, ix *Index) error {
	// Write the index into a payload file with a fresh name.
	f, err := os.CreateTemp(IndexDir, fmt.Sprintf("index-%d-*", CurrentVersion))
	if err != nil {
		return err // e.g. disk full, or index dir deleted
	}
	if err := writeIndexToFile(ix, bufio.NewWriter(f)); err != nil {
		_ = f.Close() // ignore error
		return err
	}
	if err := f.Close(); err != nil {
		return err
	}

	// Write the name of the payload file into a link file.
	indexDirFile := filepath.Join(IndexDir, linkFileBasename(gomodcache))
	content := []byte(filepath.Base(f.Name()))
	return os.WriteFile(indexDirFile, content, 0666)
}

func writeIndexToFile(x *Index, w *bufio.Writer) error {
	fmt.Fprintf(w, "%d\n", x.Version)
	fmt.Fprintf(w, "%s\n", x.GOMODCACHE)
	tm := x.ValidAt.Truncate(time.Second) // round the time down
	fmt.Fprintf(w, "%s\n", tm.Format(time.DateTime))
	for _, e := range x.Entries {
		if e.ImportPath == "" {
			continue // shouldn't happen
		}
		// PJW: maybe always write these headers as csv?
		if strings.ContainsAny(string(e.Dir), ",\"") {
			cw := csv.NewWriter(w)
			cw.Write([]string{":" + e.PkgName, e.ImportPath, string(e.Dir), e.Version})
			cw.Flush()
		} else {
			fmt.Fprintf(w, ":%s,%s,%s,%s\n", e.PkgName, e.ImportPath, e.Dir, e.Version)
		}
		for _, x := range e.Names {
			fmt.Fprintf(w, "%s\n", x)
		}
	}
	return w.Flush()
}

// linkFileBasename returns the base name of the link file in the
// index directory that holds the name of the payload file for the
// specified (absolute) Go module cache dir.
func linkFileBasename(gomodcache string) string {
	// Note: coupled to logic in ./gomodindex/cmd.go. TODO: factor.
	h := sha256.Sum256([]byte(gomodcache)) // collision-resistant hash
	return fmt.Sprintf("index-name-%d-%032x", CurrentVersion, h)
}

func relative(base, file string) string {
	if rel, err := filepath.Rel(base, file); err == nil {
		return rel
	}
	return file
}