aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/modfetch/fetch.go
diff options
context:
space:
mode:
authorJay Conrod <jayconrod@google.com>2021-03-03 16:30:22 -0500
committerJay Conrod <jayconrod@google.com>2021-03-05 15:27:51 +0000
commit302a400316319501748c0f034464fa70e7815272 (patch)
tree70a07ba7df0923ff87a1eb3d2d37f9c65964b222 /src/cmd/go/internal/modfetch/fetch.go
parent2e794c2bb1302af764670dba894bbfe537bd63f0 (diff)
downloadgo-302a400316319501748c0f034464fa70e7815272.tar.gz
go-302a400316319501748c0f034464fa70e7815272.zip
cmd/go/internal/modfetch: detect and recover from missing ziphash file
Previously, if an extracted module directory existed in the module cache, but the corresponding ziphash file did not, if the sum was missing from go.sum, we would not verify the sum. This caused 'go get' not to write missing sums. 'go build' in readonly mode (now the default) checks for missing sums and doesn't attempt to fetch modules that can't be verified against go.sum. With this change, when requesting the module directory with modfetch.DownloadDir, if the ziphash file is missing, the go command will re-hash the zip without downloading or re-extracting it again. Note that the go command creates the ziphash file before the module directory, but another program could remove it separately, and it might not be present after a crash. Fixes #44749 Change-Id: I64551e048a3ba17d069de1ec123d5b8b2757543c Reviewed-on: https://go-review.googlesource.com/c/go/+/298352 Trust: Jay Conrod <jayconrod@google.com> Run-TryBot: Jay Conrod <jayconrod@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
Diffstat (limited to 'src/cmd/go/internal/modfetch/fetch.go')
-rw-r--r--src/cmd/go/internal/modfetch/fetch.go77
1 files changed, 48 insertions, 29 deletions
diff --git a/src/cmd/go/internal/modfetch/fetch.go b/src/cmd/go/internal/modfetch/fetch.go
index d5ad277dd0..7b4ce2154c 100644
--- a/src/cmd/go/internal/modfetch/fetch.go
+++ b/src/cmd/go/internal/modfetch/fetch.go
@@ -168,13 +168,16 @@ func DownloadZip(ctx context.Context, mod module.Version) (zipfile string, err e
if err != nil {
return cached{"", err}
}
+ ziphashfile := zipfile + "hash"
- // Skip locking if the zipfile already exists.
+ // Return without locking if the zip and ziphash files exist.
if _, err := os.Stat(zipfile); err == nil {
- return cached{zipfile, nil}
+ if _, err := os.Stat(ziphashfile); err == nil {
+ return cached{zipfile, nil}
+ }
}
- // The zip file does not exist. Acquire the lock and create it.
+ // The zip or ziphash file does not exist. Acquire the lock and create them.
if cfg.CmdName != "mod download" {
fmt.Fprintf(os.Stderr, "go: downloading %s %s\n", mod.Path, mod.Version)
}
@@ -184,14 +187,6 @@ func DownloadZip(ctx context.Context, mod module.Version) (zipfile string, err e
}
defer unlock()
- // Double-check that the zipfile was not created while we were waiting for
- // the lock.
- if _, err := os.Stat(zipfile); err == nil {
- return cached{zipfile, nil}
- }
- if err := os.MkdirAll(filepath.Dir(zipfile), 0777); err != nil {
- return cached{"", err}
- }
if err := downloadZip(ctx, mod, zipfile); err != nil {
return cached{"", err}
}
@@ -204,6 +199,25 @@ func downloadZip(ctx context.Context, mod module.Version, zipfile string) (err e
ctx, span := trace.StartSpan(ctx, "modfetch.downloadZip "+zipfile)
defer span.Done()
+ // Double-check that the zipfile was not created while we were waiting for
+ // the lock in DownloadZip.
+ ziphashfile := zipfile + "hash"
+ var zipExists, ziphashExists bool
+ if _, err := os.Stat(zipfile); err == nil {
+ zipExists = true
+ }
+ if _, err := os.Stat(ziphashfile); err == nil {
+ ziphashExists = true
+ }
+ if zipExists && ziphashExists {
+ return nil
+ }
+
+ // Create parent directories.
+ if err := os.MkdirAll(filepath.Dir(zipfile), 0777); err != nil {
+ return err
+ }
+
// Clean up any remaining tempfiles from previous runs.
// This is only safe to do because the lock file ensures that their
// writers are no longer active.
@@ -215,6 +229,12 @@ func downloadZip(ctx context.Context, mod module.Version, zipfile string) (err e
}
}
+ // If the zip file exists, the ziphash file must have been deleted
+ // or lost after a file system crash. Re-hash the zip without downloading.
+ if zipExists {
+ return hashZip(mod, zipfile, ziphashfile)
+ }
+
// From here to the os.Rename call below is functionally almost equivalent to
// renameio.WriteToFile, with one key difference: we want to validate the
// contents of the file (by hashing it) before we commit it. Because the file
@@ -287,15 +307,7 @@ func downloadZip(ctx context.Context, mod module.Version, zipfile string) (err e
}
// Hash the zip file and check the sum before renaming to the final location.
- hash, err := dirhash.HashZip(f.Name(), dirhash.DefaultHash)
- if err != nil {
- return err
- }
- if err := checkModSum(mod, hash); err != nil {
- return err
- }
-
- if err := renameio.WriteFile(zipfile+"hash", []byte(hash), 0666); err != nil {
+ if err := hashZip(mod, f.Name(), ziphashfile); err != nil {
return err
}
if err := os.Rename(f.Name(), zipfile); err != nil {
@@ -307,6 +319,22 @@ func downloadZip(ctx context.Context, mod module.Version, zipfile string) (err e
return nil
}
+// hashZip reads the zip file opened in f, then writes the hash to ziphashfile,
+// overwriting that file if it exists.
+//
+// If the hash does not match go.sum (or the sumdb if enabled), hashZip returns
+// an error and does not write ziphashfile.
+func hashZip(mod module.Version, zipfile, ziphashfile string) error {
+ hash, err := dirhash.HashZip(zipfile, dirhash.DefaultHash)
+ if err != nil {
+ return err
+ }
+ if err := checkModSum(mod, hash); err != nil {
+ return err
+ }
+ return renameio.WriteFile(ziphashfile, []byte(hash), 0666)
+}
+
// makeDirsReadOnly makes a best-effort attempt to remove write permissions for dir
// and its transitive contents.
func makeDirsReadOnly(dir string) {
@@ -450,11 +478,6 @@ func HaveSum(mod module.Version) bool {
// checkMod checks the given module's checksum.
func checkMod(mod module.Version) {
- if cfg.GOMODCACHE == "" {
- // Do not use current directory.
- return
- }
-
// Do the file I/O before acquiring the go.sum lock.
ziphash, err := CachePath(mod, "ziphash")
if err != nil {
@@ -462,10 +485,6 @@ func checkMod(mod module.Version) {
}
data, err := renameio.ReadFile(ziphash)
if err != nil {
- if errors.Is(err, fs.ErrNotExist) {
- // This can happen if someone does rm -rf GOPATH/src/cache/download. So it goes.
- return
- }
base.Fatalf("verifying %v", module.VersionError(mod, err))
}
h := strings.TrimSpace(string(data))