]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modfetch: export new func Unzip
authorRuss Cox <rsc@golang.org>
Sun, 17 Nov 2024 21:54:11 +0000 (16:54 -0500)
committerGopher Robot <gobot@golang.org>
Tue, 19 Nov 2024 21:52:30 +0000 (21:52 +0000)
Add new func Unzip, which is Download (= download+unzip)
without the download. This will be used for unpacking the
FIPS module zips, which are part of the Go distribution,
not downloaded.

Change-Id: Ia04f8c376db8cb1cf27acb5567dd256afb10f410
Reviewed-on: https://go-review.googlesource.com/c/go/+/629200
Auto-Submit: Russ Cox <rsc@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

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

index 65bbcae5fb6a7b11cbc9b833c0cd52fe57b188ea..c16e83aea3175e9ee36d1589c661fa0388025eaf 100644 (file)
@@ -72,6 +72,30 @@ func Download(ctx context.Context, mod module.Version) (dir string, err error) {
        })
 }
 
+// Unzip is like Download but is given the explicit zip file to use,
+// rather than downloading it. This is used for the GOFIPS140 zip files,
+// which ship in the Go distribution itself.
+func Unzip(ctx context.Context, mod module.Version, zipfile string) (dir string, err error) {
+       if err := checkCacheDir(ctx); err != nil {
+               base.Fatal(err)
+       }
+
+       return downloadCache.Do(mod, func() (string, error) {
+               ctx, span := trace.StartSpan(ctx, "modfetch.Unzip "+mod.String())
+               defer span.Done()
+
+               dir, err = DownloadDir(ctx, mod)
+               if err == nil {
+                       // The directory has already been completely extracted (no .partial file exists).
+                       return dir, nil
+               } else if dir == "" || !errors.Is(err, fs.ErrNotExist) {
+                       return "", err
+               }
+
+               return unzip(ctx, mod, zipfile)
+       })
+}
+
 func download(ctx context.Context, mod module.Version) (dir string, err error) {
        ctx, span := trace.StartSpan(ctx, "modfetch.download "+mod.String())
        defer span.Done()
@@ -92,17 +116,21 @@ func download(ctx context.Context, mod module.Version) (dir string, err error) {
                return "", err
        }
 
+       return unzip(ctx, mod, zipfile)
+}
+
+func unzip(ctx context.Context, mod module.Version, zipfile string) (dir string, err error) {
        unlock, err := lockVersion(ctx, mod)
        if err != nil {
                return "", err
        }
        defer unlock()
 
-       ctx, span = trace.StartSpan(ctx, "unzip "+zipfile)
+       ctx, span := trace.StartSpan(ctx, "unzip "+zipfile)
        defer span.Done()
 
        // Check whether the directory was populated while we were waiting on the lock.
-       _, dirErr := DownloadDir(ctx, mod)
+       dir, dirErr := DownloadDir(ctx, mod)
        if dirErr == nil {
                return dir, nil
        }