From 689ceb224bd974e0bdc15ba3867f1b3d972141f9 Mon Sep 17 00:00:00 2001 From: Hilmar Falkenberg Date: Tue, 23 Apr 2024 16:19:02 +0200 Subject: [PATCH] ExtractTgzToTempFs --- .../ocm/accessmethods/mvn/method_test.go | 2 +- .../handlers/generic/mvn/blobhandler.go | 8 +----- pkg/utils/tarutils/extract.go | 25 +++++++++++++++++++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/pkg/contexts/ocm/accessmethods/mvn/method_test.go b/pkg/contexts/ocm/accessmethods/mvn/method_test.go index 3a5bfc58cc..ceafc56da4 100644 --- a/pkg/contexts/ocm/accessmethods/mvn/method_test.go +++ b/pkg/contexts/ocm/accessmethods/mvn/method_test.go @@ -21,7 +21,7 @@ const ( FAILPATH = "/testdata" ) -var _ = Describe("Method", func() { +var _ = Describe("accessmethods.mvn.AccessSpec tests", func() { var env *Builder var cv ocm.ComponentVersionAccess diff --git a/pkg/contexts/ocm/blobhandler/handlers/generic/mvn/blobhandler.go b/pkg/contexts/ocm/blobhandler/handlers/generic/mvn/blobhandler.go index 4de8794d36..1fef7c1a6b 100644 --- a/pkg/contexts/ocm/blobhandler/handlers/generic/mvn/blobhandler.go +++ b/pkg/contexts/ocm/blobhandler/handlers/generic/mvn/blobhandler.go @@ -9,7 +9,6 @@ import ( "net/http" "strings" - "github.com/mandelsoft/vfs/pkg/osfs" "github.com/mandelsoft/vfs/pkg/vfs" "github.com/opencontainers/go-digest" @@ -80,16 +79,11 @@ func (b *artifactHandler) StoreBlob(blob cpi.BlobAccess, resourceType string, hi } defer blobReader.Close() - tempFs, err := osfs.NewTempFileSystem() + tempFs, err := tarutils.ExtractTgzToTempFs(blobReader) if err != nil { return nil, err } defer vfs.Cleanup(tempFs) - err = tarutils.ExtractTarToFs(tempFs, blobReader) - // err = tarutils.ExtractTarToFs(tempFs, compression.AutoDecompress(blobReader)) // ??? - if err != nil { - return nil, err - } files, err := tarutils.ListSortedFilesInDir(tempFs, "", false) if err != nil { return nil, err diff --git a/pkg/utils/tarutils/extract.go b/pkg/utils/tarutils/extract.go index b6769ff39d..a917196cb0 100644 --- a/pkg/utils/tarutils/extract.go +++ b/pkg/utils/tarutils/extract.go @@ -51,6 +51,31 @@ func ExtractTarToFs(fs vfs.FileSystem, in io.Reader) error { return err } +// UnzipTarToFs tries to decompress the input stream and then writes the tar stream to a filesystem. +func UnzipTarToFs(fs vfs.FileSystem, in io.Reader) error { + r, _, err := compression.AutoDecompress(in) + if err != nil { + return err + } + defer r.Close() + err = ExtractTarToFs(fs, r) + if err != nil { + return err + } + return err +} + +// ExtractTgzToTempFs extracts a tar.gz archive to a temporary filesystem. +// You should call vfs.Cleanup on the returned filesystem to clean up the temporary files. +func ExtractTgzToTempFs(in io.Reader) (vfs.FileSystem, error) { + fs, err := osfs.NewTempFileSystem() + if err != nil { + return nil, err + } + + return fs, UnzipTarToFs(fs, in) +} + func ExtractTarToFsWithInfo(fs vfs.FileSystem, in io.Reader) (fcnt int64, bcnt int64, err error) { tr := tar.NewReader(in) for {