Skip to content

Commit

Permalink
ExtractTgzToTempFs
Browse files Browse the repository at this point in the history
  • Loading branch information
hilmarf committed Apr 23, 2024
1 parent 57ec467 commit 689ceb2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/contexts/ocm/accessmethods/mvn/method_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions pkg/utils/tarutils/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 689ceb2

Please sign in to comment.