Debian Codegraph @

src/cmd/go/internal/modfetch/cache.go

raw
1 // Copyright 2018 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 modfetch
6
7 import (
8 "bytes"
9 "context"
10 "encoding/json"
11 "errors"
12 "fmt"
13 "io"
14 "io/fs"
15 "math/rand"
16 "os"
17 "path/filepath"
18 "strconv"
19 "strings"
20 "sync"
21
22 "cmd/go/internal/base"
23 "cmd/go/internal/cfg"
24 "cmd/go/internal/gover"
25 "cmd/go/internal/lockedfile"
26 "cmd/go/internal/modfetch/codehost"
27 "cmd/internal/par"
28 "cmd/internal/robustio"
29 "cmd/internal/telemetry/counter"
30
31 "golang.org/x/mod/module"
32 "golang.org/x/mod/semver"
33 )
34
35 func cacheDir(ctx context.Context, path string) (string, error) {
36 if err := checkCacheDir(ctx); err != nil {
37 return "", err
38 }
39 enc, err := module.EscapePath(path)
40 if err != nil {
41 return "", err
42 }
43 return filepath.Join(cfg.GOMODCACHE, "cache/download", enc, "/@v"), nil
44 }
45
46 func CachePath(ctx context.Context, m module.Version, suffix string) (string, error) {
47 if gover.IsToolchain(m.Path) {
48 return "", ErrToolchain
49 }
50 dir, err := cacheDir(ctx, m.Path)
51 if err != nil {
52 return "", err
53 }
54 if !gover.ModIsValid(m.Path, m.Version) {
55 return "", fmt.Errorf("non-semver module version %q", m.Version)
56 }
57 if module.CanonicalVersion(m.Version) != m.Version {
58 return "", fmt.Errorf("non-canonical module version %q", m.Version)
59 }
60 encVer, err := module.EscapeVersion(m.Version)
61 if err != nil {
62 return "", err
63 }
64 return filepath.Join(dir, encVer+"."+suffix), nil
65 }
66
67 // DownloadDir returns the directory to which m should have been downloaded.
68 // An error will be returned if the module path or version cannot be escaped.
69 // An error satisfying errors.Is(err, fs.ErrNotExist) will be returned
70 // along with the directory if the directory does not exist or if the directory
71 // is not completely populated.
72 func DownloadDir(ctx context.Context, m module.Version) (string, error) {
73 if gover.IsToolchain(m.Path) {
74 return "", ErrToolchain
75 }
76 if err := checkCacheDir(ctx); err != nil {
77 return "", err
78 }
79 enc, err := module.EscapePath(m.Path)
80 if err != nil {
81 return "", err
82 }
83 if !gover.ModIsValid(m.Path, m.Version) {
84 return "", fmt.Errorf("non-semver module version %q", m.Version)
85 }
86 if module.CanonicalVersion(m.Version) != m.Version {
87 return "", fmt.Errorf("non-canonical module version %q", m.Version)
88 }
89 encVer, err := module.EscapeVersion(m.Version)
90 if err != nil {
91 return "", err
92 }
93
94 // Check whether the directory itself exists.
95 dir := filepath.Join(cfg.GOMODCACHE, enc+"@"+encVer)
96 if fi, err := os.Stat(dir); os.IsNotExist(err) {
97 return dir, err
98 } else if err != nil {
99 return dir, &DownloadDirPartialError{dir, err}
100 } else if !fi.IsDir() {
101 return dir, &DownloadDirPartialError{dir, errors.New("not a directory")}
102 }
103
104 // Check if a .partial file exists. This is created at the beginning of
105 // a download and removed after the zip is extracted.
106 partialPath, err := CachePath(ctx, m, "partial")
107 if err != nil {
108 return dir, err
109 }
110 if _, err := os.Stat(partialPath); err == nil {
111 return dir, &DownloadDirPartialError{dir, errors.New("not completely extracted")}
112 } else if !os.IsNotExist(err) {
113 return dir, err
114 }
115
116 // Special case: ziphash is not required for the golang.org/fips140 module,
117 // because it is unpacked from a file in GOROOT, not downloaded.
118 // We've already checked that it's not a partial unpacking, so we're happy.
119 if m.Path == "golang.org/fips140" {
120 return dir, nil
121 }
122
123 // Check if a .ziphash file exists. It should be created before the
124 // zip is extracted, but if it was deleted (by another program?), we need
125 // to re-calculate it. Note that checkMod will repopulate the ziphash
126 // file if it doesn't exist, but if the module is excluded by checks
127 // through GONOSUMDB or GOPRIVATE, that check and repopulation won't happen.
128 ziphashPath, err := CachePath(ctx, m, "ziphash")
129 if err != nil {
130 return dir, err
131 }
132 if _, err := os.Stat(ziphashPath); os.IsNotExist(err) {
133 return dir, &DownloadDirPartialError{dir, errors.New("ziphash file is missing")}
134 } else if err != nil {
135 return dir, err
136 }
137 return dir, nil
138 }
139
140 // DownloadDirPartialError is returned by DownloadDir if a module directory
141 // exists but was not completely populated.
142 //
143 // DownloadDirPartialError is equivalent to fs.ErrNotExist.
144 type DownloadDirPartialError struct {
145 Dir string
146 Err error
147 }
148
149 func (e *DownloadDirPartialError) Error() string { return fmt.Sprintf("%s: %v", e.Dir, e.Err) }
150 func (e *DownloadDirPartialError) Is(err error) bool { return err == fs.ErrNotExist }
151
152 // lockVersion locks a file within the module cache that guards the downloading
153 // and extraction of the zipfile for the given module version.
154 func lockVersion(ctx context.Context, mod module.Version) (unlock func(), err error) {
155 path, err := CachePath(ctx, mod, "lock")
156 if err != nil {
157 return nil, err
158 }
159 if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
160 return nil, err
161 }
162 return lockedfile.MutexAt(path).Lock()
163 }
164
165 // SideLock locks a file within the module cache that previously guarded
166 // edits to files outside the cache, such as go.sum and go.mod files in the
167 // user's working directory.
168 // If err is nil, the caller MUST eventually call the unlock function.
169 func SideLock(ctx context.Context) (unlock func(), err error) {
170 if err := checkCacheDir(ctx); err != nil {
171 return nil, err
172 }
173
174 path := filepath.Join(cfg.GOMODCACHE, "cache", "lock")
175 if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
176 return nil, fmt.Errorf("failed to create cache directory: %w", err)
177 }
178
179 return lockedfile.MutexAt(path).Lock()
180 }
181
182 // A cachingRepo is a cache around an underlying Repo,
183 // avoiding redundant calls to ModulePath, Versions, Stat, Latest, and GoMod (but not CheckReuse or Zip).
184 // It is also safe for simultaneous use by multiple goroutines
185 // (so that it can be returned from Lookup multiple times).
186 // It serializes calls to the underlying Repo.
187 type cachingRepo struct {
188 path string
189 versionsCache par.ErrCache[string, *Versions]
190 statCache par.ErrCache[string, *RevInfo]
191 latestCache par.ErrCache[struct{}, *RevInfo]
192 gomodCache par.ErrCache[string, []byte]
193
194 once sync.Once
195 initRepo func(context.Context) (Repo, error)
196 r Repo
197 }
198
199 func newCachingRepo(ctx context.Context, path string, initRepo func(context.Context) (Repo, error)) *cachingRepo {
200 return &cachingRepo{
201 path: path,
202 initRepo: initRepo,
203 }
204 }
205
206 func (r *cachingRepo) repo(ctx context.Context) Repo {
207 r.once.Do(func() {
208 var err error
209 r.r, err = r.initRepo(ctx)
210 if err != nil {
211 r.r = errRepo{r.path, err}
212 }
213 })
214 return r.r
215 }
216
217 func (r *cachingRepo) CheckReuse(ctx context.Context, old *codehost.Origin) error {
218 return r.repo(ctx).CheckReuse(ctx, old)
219 }
220
221 func (r *cachingRepo) ModulePath() string {
222 return r.path
223 }
224
225 func (r *cachingRepo) Versions(ctx context.Context, prefix string) (*Versions, error) {
226 v, err := r.versionsCache.Do(prefix, func() (*Versions, error) {
227 return r.repo(ctx).Versions(ctx, prefix)
228 })
229
230 if err != nil {
231 return nil, err
232 }
233 return &Versions{
234 Origin: v.Origin,
235 List: append([]string(nil), v.List...),
236 }, nil
237 }
238
239 type cachedInfo struct {
240 info *RevInfo
241 err error
242 }
243
244 func (r *cachingRepo) Stat(ctx context.Context, rev string) (*RevInfo, error) {
245 if gover.IsToolchain(r.path) {
246 // Skip disk cache; the underlying golang.org/toolchain repo is cached instead.
247 return r.repo(ctx).Stat(ctx, rev)
248 }
249 info, err := r.statCache.Do(rev, func() (*RevInfo, error) {
250 file, info, err := readDiskStat(ctx, r.path, rev)
251 if err == nil {
252 return info, err
253 }
254
255 info, err = r.repo(ctx).Stat(ctx, rev)
256 if err == nil {
257 // If we resolved, say, 1234abcde to v0.0.0-20180604122334-1234abcdef78,
258 // then save the information under the proper version, for future use.
259 if info.Version != rev {
260 file, _ = CachePath(ctx, module.Version{Path: r.path, Version: info.Version}, "info")
261 r.statCache.Do(info.Version, func() (*RevInfo, error) {
262 return info, nil
263 })
264 }
265
266 if err := writeDiskStat(ctx, file, info); err != nil {
267 fmt.Fprintf(os.Stderr, "go: writing stat cache: %v\n", err)
268 }
269 }
270 return info, err
271 })
272 if info != nil {
273 copy := *info
274 info = &copy
275 }
276 return info, err
277 }
278
279 func (r *cachingRepo) Latest(ctx context.Context) (*RevInfo, error) {
280 if gover.IsToolchain(r.path) {
281 // Skip disk cache; the underlying golang.org/toolchain repo is cached instead.
282 return r.repo(ctx).Latest(ctx)
283 }
284 info, err := r.latestCache.Do(struct{}{}, func() (*RevInfo, error) {
285 info, err := r.repo(ctx).Latest(ctx)
286
287 // Save info for likely future Stat call.
288 if err == nil {
289 r.statCache.Do(info.Version, func() (*RevInfo, error) {
290 return info, nil
291 })
292 if file, _, err := readDiskStat(ctx, r.path, info.Version); err != nil {
293 writeDiskStat(ctx, file, info)
294 }
295 }
296
297 return info, err
298 })
299 if info != nil {
300 copy := *info
301 info = &copy
302 }
303 return info, err
304 }
305
306 func (r *cachingRepo) GoMod(ctx context.Context, version string) ([]byte, error) {
307 if gover.IsToolchain(r.path) {
308 // Skip disk cache; the underlying golang.org/toolchain repo is cached instead.
309 return r.repo(ctx).GoMod(ctx, version)
310 }
311 text, err := r.gomodCache.Do(version, func() ([]byte, error) {
312 file, text, err := readDiskGoMod(ctx, r.path, version)
313 if err == nil {
314 // Note: readDiskGoMod already called checkGoMod.
315 return text, nil
316 }
317
318 text, err = r.repo(ctx).GoMod(ctx, version)
319 if err == nil {
320 if err := checkGoMod(r.path, version, text); err != nil {
321 return text, err
322 }
323 if err := writeDiskGoMod(ctx, file, text); err != nil {
324 fmt.Fprintf(os.Stderr, "go: writing go.mod cache: %v\n", err)
325 }
326 }
327 return text, err
328 })
329 if err != nil {
330 return nil, err
331 }
332 return append([]byte(nil), text...), nil
333 }
334
335 func (r *cachingRepo) Zip(ctx context.Context, dst io.Writer, version string) error {
336 if gover.IsToolchain(r.path) {
337 return ErrToolchain
338 }
339 return r.repo(ctx).Zip(ctx, dst, version)
340 }
341
342 // InfoFile is like Lookup(ctx, path).Stat(version) but also returns the name of the file
343 // containing the cached information.
344 func InfoFile(ctx context.Context, path, version string) (*RevInfo, string, error) {
345 if !gover.ModIsValid(path, version) {
346 return nil, "", fmt.Errorf("invalid version %q", version)
347 }
348
349 if file, info, err := readDiskStat(ctx, path, version); err == nil {
350 return info, file, nil
351 }
352
353 var info *RevInfo
354 var err2info map[error]*RevInfo
355 err := TryProxies(func(proxy string) error {
356 i, err := Lookup(ctx, proxy, path).Stat(ctx, version)
357 if err == nil {
358 info = i
359 } else {
360 if err2info == nil {
361 err2info = make(map[error]*RevInfo)
362 }
363 err2info[err] = info
364 }
365 return err
366 })
367 if err != nil {
368 return err2info[err], "", err
369 }
370
371 // Stat should have populated the disk cache for us.
372 file, err := CachePath(ctx, module.Version{Path: path, Version: version}, "info")
373 if err != nil {
374 return nil, "", err
375 }
376 return info, file, nil
377 }
378
379 // GoMod is like Lookup(ctx, path).GoMod(rev) but avoids the
380 // repository path resolution in Lookup if the result is
381 // already cached on local disk.
382 func GoMod(ctx context.Context, path, rev string) ([]byte, error) {
383 // Convert commit hash to pseudo-version
384 // to increase cache hit rate.
385 if !gover.ModIsValid(path, rev) {
386 if _, info, err := readDiskStat(ctx, path, rev); err == nil {
387 rev = info.Version
388 } else {
389 if errors.Is(err, statCacheErr) {
390 return nil, err
391 }
392 err := TryProxies(func(proxy string) error {
393 info, err := Lookup(ctx, proxy, path).Stat(ctx, rev)
394 if err == nil {
395 rev = info.Version
396 }
397 return err
398 })
399 if err != nil {
400 return nil, err
401 }
402 }
403 }
404
405 _, data, err := readDiskGoMod(ctx, path, rev)
406 if err == nil {
407 return data, nil
408 }
409
410 err = TryProxies(func(proxy string) (err error) {
411 data, err = Lookup(ctx, proxy, path).GoMod(ctx, rev)
412 return err
413 })
414 return data, err
415 }
416
417 // GoModFile is like GoMod but returns the name of the file containing
418 // the cached information.
419 func GoModFile(ctx context.Context, path, version string) (string, error) {
420 if !gover.ModIsValid(path, version) {
421 return "", fmt.Errorf("invalid version %q", version)
422 }
423 if _, err := GoMod(ctx, path, version); err != nil {
424 return "", err
425 }
426 // GoMod should have populated the disk cache for us.
427 file, err := CachePath(ctx, module.Version{Path: path, Version: version}, "mod")
428 if err != nil {
429 return "", err
430 }
431 return file, nil
432 }
433
434 // GoModSum returns the go.sum entry for the module version's go.mod file.
435 // (That is, it returns the entry listed in go.sum as "path version/go.mod".)
436 func GoModSum(ctx context.Context, path, version string) (string, error) {
437 if !gover.ModIsValid(path, version) {
438 return "", fmt.Errorf("invalid version %q", version)
439 }
440 data, err := GoMod(ctx, path, version)
441 if err != nil {
442 return "", err
443 }
444 sum, err := goModSum(data)
445 if err != nil {
446 return "", err
447 }
448 return sum, nil
449 }
450
451 var errNotCached = fmt.Errorf("not in cache")
452
453 // readDiskStat reads a cached stat result from disk,
454 // returning the name of the cache file and the result.
455 // If the read fails, the caller can use
456 // writeDiskStat(file, info) to write a new cache entry.
457 func readDiskStat(ctx context.Context, path, rev string) (file string, info *RevInfo, err error) {
458 if gover.IsToolchain(path) {
459 return "", nil, errNotCached
460 }
461 file, data, err := readDiskCache(ctx, path, rev, "info")
462 if err != nil {
463 // If the cache already contains a pseudo-version with the given hash, we
464 // would previously return that pseudo-version without checking upstream.
465 // However, that produced an unfortunate side-effect: if the author added a
466 // tag to the repository, 'go get' would not pick up the effect of that new
467 // tag on the existing commits, and 'go' commands that referred to those
468 // commits would use the previous name instead of the new one.
469 //
470 // That's especially problematic if the original pseudo-version starts with
471 // v0.0.0-, as was the case for all pseudo-versions during vgo development,
472 // since a v0.0.0- pseudo-version has lower precedence than pretty much any
473 // tagged version.
474 //
475 // In practice, we're only looking up by hash during initial conversion of a
476 // legacy config and during an explicit 'go get', and a little extra latency
477 // for those operations seems worth the benefit of picking up more accurate
478 // versions.
479 //
480 // Fall back to this resolution scheme only if the GOPROXY setting prohibits
481 // us from resolving upstream tags.
482 if cfg.GOPROXY == "off" {
483 if file, info, err := readDiskStatByHash(ctx, path, rev); err == nil {
484 return file, info, nil
485 }
486 }
487 return file, nil, err
488 }
489 info = new(RevInfo)
490 if err := json.Unmarshal(data, info); err != nil {
491 return file, nil, errNotCached
492 }
493 // The disk might have stale .info files that have Name and Short fields set.
494 // We want to canonicalize to .info files with those fields omitted.
495 // Remarshal and update the cache file if needed.
496 data2, err := json.Marshal(info)
497 if err == nil && !bytes.Equal(data2, data) {
498 writeDiskCache(ctx, file, data)
499 }
500 return file, info, nil
501 }
502
503 // readDiskStatByHash is a fallback for readDiskStat for the case
504 // where rev is a commit hash instead of a proper semantic version.
505 // In that case, we look for a cached pseudo-version that matches
506 // the commit hash. If we find one, we use it.
507 // This matters most for converting legacy package management
508 // configs, when we are often looking up commits by full hash.
509 // Without this check we'd be doing network I/O to the remote repo
510 // just to find out about a commit we already know about
511 // (and have cached under its pseudo-version).
512 func readDiskStatByHash(ctx context.Context, path, rev string) (file string, info *RevInfo, err error) {
513 if gover.IsToolchain(path) {
514 return "", nil, errNotCached
515 }
516 if cfg.GOMODCACHE == "" {
517 // Do not download to current directory.
518 return "", nil, errNotCached
519 }
520
521 if !codehost.AllHex(rev) || len(rev) < 12 {
522 return "", nil, errNotCached
523 }
524 rev = rev[:12]
525 cdir, err := cacheDir(ctx, path)
526 if err != nil {
527 return "", nil, errNotCached
528 }
529 dir, err := os.Open(cdir)
530 if err != nil {
531 return "", nil, errNotCached
532 }
533 names, err := dir.Readdirnames(-1)
534 dir.Close()
535 if err != nil {
536 return "", nil, errNotCached
537 }
538
539 // A given commit hash may map to more than one pseudo-version,
540 // depending on which tags are present on the repository.
541 // Take the highest such version.
542 var maxVersion string
543 suffix := "-" + rev + ".info"
544 err = errNotCached
545 for _, name := range names {
546 if strings.HasSuffix(name, suffix) {
547 v := strings.TrimSuffix(name, ".info")
548 if module.IsPseudoVersion(v) && semver.Compare(v, maxVersion) > 0 {
549 maxVersion = v
550 file, info, err = readDiskStat(ctx, path, strings.TrimSuffix(name, ".info"))
551 }
552 }
553 }
554 return file, info, err
555 }
556
557 // oldVgoPrefix is the prefix in the old auto-generated cached go.mod files.
558 // We stopped trying to auto-generate the go.mod files. Now we use a trivial
559 // go.mod with only a module line, and we've dropped the version prefix
560 // entirely. If we see a version prefix, that means we're looking at an old copy
561 // and should ignore it.
562 var oldVgoPrefix = []byte("//vgo 0.0.")
563
564 // readDiskGoMod reads a cached go.mod file from disk,
565 // returning the name of the cache file and the result.
566 // If the read fails, the caller can use
567 // writeDiskGoMod(file, data) to write a new cache entry.
568 func readDiskGoMod(ctx context.Context, path, rev string) (file string, data []byte, err error) {
569 if gover.IsToolchain(path) {
570 return "", nil, errNotCached
571 }
572 file, data, err = readDiskCache(ctx, path, rev, "mod")
573
574 // If the file has an old auto-conversion prefix, pretend it's not there.
575 if bytes.HasPrefix(data, oldVgoPrefix) {
576 err = errNotCached
577 data = nil
578 }
579
580 if err == nil {
581 if err := checkGoMod(path, rev, data); err != nil {
582 return "", nil, err
583 }
584 }
585
586 return file, data, err
587 }
588
589 // readDiskCache is the generic "read from a cache file" implementation.
590 // It takes the revision and an identifying suffix for the kind of data being cached.
591 // It returns the name of the cache file and the content of the file.
592 // If the read fails, the caller can use
593 // writeDiskCache(file, data) to write a new cache entry.
594 func readDiskCache(ctx context.Context, path, rev, suffix string) (file string, data []byte, err error) {
595 if gover.IsToolchain(path) {
596 return "", nil, errNotCached
597 }
598 file, err = CachePath(ctx, module.Version{Path: path, Version: rev}, suffix)
599 if err != nil {
600 return "", nil, errNotCached
601 }
602 data, err = robustio.ReadFile(file)
603 if err != nil {
604 return file, nil, errNotCached
605 }
606 return file, data, nil
607 }
608
609 // writeDiskStat writes a stat result cache entry.
610 // The file name must have been returned by a previous call to readDiskStat.
611 func writeDiskStat(ctx context.Context, file string, info *RevInfo) error {
612 if file == "" {
613 return nil
614 }
615
616 if info.Origin != nil {
617 // Clean the origin information, which might have too many
618 // validation criteria, for example if we are saving the result of
619 // m@master as m@pseudo-version.
620 clean := *info
621 info = &clean
622 o := *info.Origin
623 info.Origin = &o
624
625 // Tags never matter if you are starting with a semver version,
626 // as we would be when finding this cache entry.
627 o.TagSum = ""
628 o.TagPrefix = ""
629 // Ref doesn't matter if you have a pseudoversion.
630 if module.IsPseudoVersion(info.Version) {
631 o.Ref = ""
632 }
633 }
634
635 js, err := json.Marshal(info)
636 if err != nil {
637 return err
638 }
639 return writeDiskCache(ctx, file, js)
640 }
641
642 // writeDiskGoMod writes a go.mod cache entry.
643 // The file name must have been returned by a previous call to readDiskGoMod.
644 func writeDiskGoMod(ctx context.Context, file string, text []byte) error {
645 return writeDiskCache(ctx, file, text)
646 }
647
648 // writeDiskCache is the generic "write to a cache file" implementation.
649 // The file must have been returned by a previous call to readDiskCache.
650 func writeDiskCache(ctx context.Context, file string, data []byte) error {
651 if file == "" {
652 return nil
653 }
654 // Make sure directory for file exists.
655 if err := os.MkdirAll(filepath.Dir(file), 0777); err != nil {
656 return err
657 }
658
659 // Write the file to a temporary location, and then rename it to its final
660 // path to reduce the likelihood of a corrupt file existing at that final path.
661 f, err := tempFile(ctx, filepath.Dir(file), filepath.Base(file), 0666)
662 if err != nil {
663 return err
664 }
665 defer func() {
666 // Only call os.Remove on f.Name() if we failed to rename it: otherwise,
667 // some other process may have created a new file with the same name after
668 // the rename completed.
669 if err != nil {
670 f.Close()
671 os.Remove(f.Name())
672 }
673 }()
674
675 if _, err := f.Write(data); err != nil {
676 return err
677 }
678 if err := f.Close(); err != nil {
679 return err
680 }
681 if err := robustio.Rename(f.Name(), file); err != nil {
682 return err
683 }
684
685 if strings.HasSuffix(file, ".mod") {
686 rewriteVersionList(ctx, filepath.Dir(file))
687 }
688 return nil
689 }
690
691 // tempFile creates a new temporary file with given permission bits.
692 func tempFile(ctx context.Context, dir, prefix string, perm fs.FileMode) (f *os.File, err error) {
693 for i := 0; i < 10000; i++ {
694 name := filepath.Join(dir, prefix+strconv.Itoa(rand.Intn(1000000000))+".tmp")
695 f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, perm)
696 if os.IsExist(err) {
697 if ctx.Err() != nil {
698 return nil, ctx.Err()
699 }
700 continue
701 }
702 break
703 }
704 return
705 }
706
707 // rewriteVersionList rewrites the version list in dir
708 // after a new *.mod file has been written.
709 func rewriteVersionList(ctx context.Context, dir string) (err error) {
710 if filepath.Base(dir) != "@v" {
711 base.Fatalf("go: internal error: misuse of rewriteVersionList")
712 }
713
714 listFile := filepath.Join(dir, "list")
715
716 // Lock listfile when writing to it to try to avoid corruption to the file.
717 // Under rare circumstances, for instance, if the system loses power in the
718 // middle of a write it is possible for corrupt data to be written. This is
719 // not a problem for the go command itself, but may be an issue if the
720 // cache is being served by a GOPROXY HTTP server. This will be corrected
721 // the next time a new version of the module is fetched and the file is rewritten.
722 // TODO(matloob): golang.org/issue/43313 covers adding a go mod verify
723 // command that removes module versions that fail checksums. It should also
724 // remove list files that are detected to be corrupt.
725 f, err := lockedfile.Edit(listFile)
726 if err != nil {
727 return err
728 }
729 defer func() {
730 if cerr := f.Close(); cerr != nil && err == nil {
731 err = cerr
732 }
733 }()
734 infos, err := os.ReadDir(dir)
735 if err != nil {
736 return err
737 }
738 var list []string
739 for _, info := range infos {
740 // We look for *.mod files on the theory that if we can't supply
741 // the .mod file then there's no point in listing that version,
742 // since it's unusable. (We can have *.info without *.mod.)
743 // We don't require *.zip files on the theory that for code only
744 // involved in module graph construction, many *.zip files
745 // will never be requested.
746 name := info.Name()
747 if v, found := strings.CutSuffix(name, ".mod"); found {
748 if v != "" && module.CanonicalVersion(v) == v {
749 list = append(list, v)
750 }
751 }
752 }
753 semver.Sort(list)
754
755 var buf bytes.Buffer
756 for _, v := range list {
757 buf.WriteString(v)
758 buf.WriteString("\n")
759 }
760 if fi, err := f.Stat(); err == nil && int(fi.Size()) == buf.Len() {
761 old := make([]byte, buf.Len()+1)
762 if n, err := f.ReadAt(old, 0); err == io.EOF && n == buf.Len() && bytes.Equal(buf.Bytes(), old) {
763 return nil // No edit needed.
764 }
765 }
766 // Remove existing contents, so that when we truncate to the actual size it will zero-fill,
767 // and we will be able to detect (some) incomplete writes as files containing trailing NUL bytes.
768 if err := f.Truncate(0); err != nil {
769 return err
770 }
771 // Reserve the final size and zero-fill.
772 if err := f.Truncate(int64(buf.Len())); err != nil {
773 return err
774 }
775 // Write the actual contents. If this fails partway through,
776 // the remainder of the file should remain as zeroes.
777 if _, err := f.Write(buf.Bytes()); err != nil {
778 f.Truncate(0)
779 return err
780 }
781
782 return nil
783 }
784
785 var (
786 statCacheOnce sync.Once
787 statCacheErr error
788
789 counterErrorsGOMODCACHEEntryRelative = counter.New("go/errors:gomodcache-entry-relative")
790 )
791
792 // checkCacheDir checks if the directory specified by GOMODCACHE exists. An
793 // error is returned if it does not.
794 func checkCacheDir(ctx context.Context) error {
795 if cfg.GOMODCACHE == "" {
796 // modload.Init exits if GOPATH[0] is empty, and cfg.GOMODCACHE
797 // is set to GOPATH[0]/pkg/mod if GOMODCACHE is empty, so this should never happen.
798 return fmt.Errorf("module cache not found: neither GOMODCACHE nor GOPATH is set")
799 }
800 if !filepath.IsAbs(cfg.GOMODCACHE) {
801 counterErrorsGOMODCACHEEntryRelative.Inc()
802 return fmt.Errorf("GOMODCACHE entry is relative; must be absolute path: %q.\n", cfg.GOMODCACHE)
803 }
804
805 // os.Stat is slow on Windows, so we only call it once to prevent unnecessary
806 // I/O every time this function is called.
807 statCacheOnce.Do(func() {
808 fi, err := os.Stat(cfg.GOMODCACHE)
809 if err != nil {
810 if !os.IsNotExist(err) {
811 statCacheErr = fmt.Errorf("could not create module cache: %w", err)
812 return
813 }
814 if err := os.MkdirAll(cfg.GOMODCACHE, 0777); err != nil {
815 statCacheErr = fmt.Errorf("could not create module cache: %w", err)
816 return
817 }
818 return
819 }
820 if !fi.IsDir() {
821 statCacheErr = fmt.Errorf("could not create module cache: %q is not a directory", cfg.GOMODCACHE)
822 return
823 }
824 })
825 return statCacheErr
826 }