Skip to content

Commit e7358c6

Browse files
committed
cmd/go: remove fips140 dependency on global Fetcher_
This commit makes Unzip a method on the *Fetcher type, and updates fips140 initialization to use a new Fetcher instance instead of the global Fetcher_ variable. Change-Id: Iea8d9ee4dd6e6a2be43520c144aaec6e75c9cd63 Reviewed-on: https://go-review.googlesource.com/c/go/+/722581 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Matloob <matloob@google.com>
1 parent 89f6dba commit e7358c6

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

src/cmd/go/internal/fips140/fips140.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,19 @@
8585
package fips140
8686

8787
import (
88-
"cmd/go/internal/base"
89-
"cmd/go/internal/cfg"
90-
"cmd/go/internal/fsys"
91-
"cmd/go/internal/modfetch"
92-
"cmd/go/internal/str"
9388
"context"
9489
"os"
9590
"path"
9691
"path/filepath"
9792
"slices"
9893
"strings"
9994

95+
"cmd/go/internal/base"
96+
"cmd/go/internal/cfg"
97+
"cmd/go/internal/fsys"
98+
"cmd/go/internal/modfetch"
99+
"cmd/go/internal/str"
100+
100101
"golang.org/x/mod/module"
101102
"golang.org/x/mod/semver"
102103
)
@@ -224,12 +225,11 @@ func initDir() {
224225

225226
mod := module.Version{Path: "golang.org/fips140", Version: v}
226227
file := filepath.Join(cfg.GOROOT, "lib/fips140", v+".zip")
227-
zdir, err := modfetch.Unzip(context.Background(), mod, file)
228+
zdir, err := modfetch.NewFetcher().Unzip(context.Background(), mod, file)
228229
if err != nil {
229230
base.Fatalf("go: unpacking GOFIPS140=%v: %v", v, err)
230231
}
231232
dir = filepath.Join(zdir, "fips140")
232-
return
233233
}
234234

235235
// ResolveImport resolves the import path imp.

src/cmd/go/internal/modfetch/fetch.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ func Download(ctx context.Context, mod module.Version) (dir string, err error) {
7373
// Unzip is like Download but is given the explicit zip file to use,
7474
// rather than downloading it. This is used for the GOFIPS140 zip files,
7575
// which ship in the Go distribution itself.
76-
func Unzip(ctx context.Context, mod module.Version, zipfile string) (dir string, err error) {
76+
func (f *Fetcher) Unzip(ctx context.Context, mod module.Version, zipfile string) (dir string, err error) {
7777
if err := checkCacheDir(ctx); err != nil {
7878
base.Fatal(err)
7979
}
8080

81-
return Fetcher_.downloadCache.Do(mod, func() (string, error) {
81+
return f.downloadCache.Do(mod, func() (string, error) {
8282
ctx, span := trace.StartSpan(ctx, "modfetch.Unzip "+mod.String())
8383
defer span.Done()
8484

@@ -171,10 +171,10 @@ func unzip(ctx context.Context, mod module.Version, zipfile string) (dir string,
171171
// Go 1.14.2 and higher respect .partial files. Older versions may use
172172
// partially extracted directories. 'go mod verify' can detect this,
173173
// and 'go clean -modcache' can fix it.
174-
if err := os.MkdirAll(parentDir, 0777); err != nil {
174+
if err := os.MkdirAll(parentDir, 0o777); err != nil {
175175
return "", err
176176
}
177-
if err := os.WriteFile(partialPath, nil, 0666); err != nil {
177+
if err := os.WriteFile(partialPath, nil, 0o666); err != nil {
178178
return "", err
179179
}
180180
if err := modzip.Unzip(dir, mod, zipfile); err != nil {
@@ -264,7 +264,7 @@ func downloadZip(ctx context.Context, mod module.Version, zipfile string) (err e
264264
}
265265

266266
// Create parent directories.
267-
if err := os.MkdirAll(filepath.Dir(zipfile), 0777); err != nil {
267+
if err := os.MkdirAll(filepath.Dir(zipfile), 0o777); err != nil {
268268
return err
269269
}
270270

@@ -289,7 +289,7 @@ func downloadZip(ctx context.Context, mod module.Version, zipfile string) (err e
289289
// contents of the file (by hashing it) before we commit it. Because the file
290290
// is zip-compressed, we need an actual file — or at least an io.ReaderAt — to
291291
// validate it: we can't just tee the stream as we write it.
292-
f, err := tempFile(ctx, filepath.Dir(zipfile), filepath.Base(zipfile), 0666)
292+
f, err := tempFile(ctx, filepath.Dir(zipfile), filepath.Base(zipfile), 0o666)
293293
if err != nil {
294294
return err
295295
}
@@ -404,7 +404,7 @@ func makeDirsReadOnly(dir string) {
404404
filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
405405
if err == nil && d.IsDir() {
406406
info, err := d.Info()
407-
if err == nil && info.Mode()&0222 != 0 {
407+
if err == nil && info.Mode()&0o222 != 0 {
408408
dirs = append(dirs, pathMode{path, info.Mode()})
409409
}
410410
}
@@ -413,7 +413,7 @@ func makeDirsReadOnly(dir string) {
413413

414414
// Run over list backward to chmod children before parents.
415415
for i := len(dirs) - 1; i >= 0; i-- {
416-
os.Chmod(dirs[i].path, dirs[i].mode&^0222)
416+
os.Chmod(dirs[i].path, dirs[i].mode&^0o222)
417417
}
418418
}
419419

@@ -426,7 +426,7 @@ func RemoveAll(dir string) error {
426426
return nil // ignore errors walking in file system
427427
}
428428
if info.IsDir() {
429-
os.Chmod(path, 0777)
429+
os.Chmod(path, 0o777)
430430
}
431431
return nil
432432
})
@@ -970,7 +970,6 @@ Outer:
970970
tidyGoSum := tidyGoSum(data, keep)
971971
return tidyGoSum, nil
972972
})
973-
974973
if err != nil {
975974
return fmt.Errorf("updating go.sum: %w", err)
976975
}

0 commit comments

Comments
 (0)