Skip to content

Commit

Permalink
Remove unnecessary archive code; fix .tar.xz support (chainguard-dev#214
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Evan Gibler authored May 10, 2024
1 parent 94e0a41 commit 684ff03
Showing 1 changed file with 9 additions and 42 deletions.
51 changes: 9 additions & 42 deletions pkg/action/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ func extractTar(ctx context.Context, d string, f string) error {
}
defer gzStream.Close()
tr = tar.NewReader(gzStream)
} else if strings.Contains(filename, ".tar.xz") {
}
if strings.Contains(filename, ".tar.xz") {
xzStream, err := xz.NewReader(tf)
if err != nil {
return fmt.Errorf("failed to create xz reader: %w", err)
Expand Down Expand Up @@ -85,19 +86,6 @@ func extractTar(ctx context.Context, d string, f string) error {
return fmt.Errorf("failed to copy file: %w", err)
}

// If the file is an archive, recursively extract it
ext := getExt(target)
if _, ok := archiveMap[ext]; ok {
// determine the file type to choose the right extraction method
extract := extractionMethod(ext)
if extract == nil {
return fmt.Errorf("unsupported archive type: %s", ext)
}
if err := extract(ctx, d, target); err != nil {
return fmt.Errorf("failed to extract nested archive: %w", err)
}
}

if err := f.Close(); err != nil {
return fmt.Errorf("failed to close file: %w", err)
}
Expand Down Expand Up @@ -141,19 +129,6 @@ func extractGzip(ctx context.Context, d string, f string) error {
return fmt.Errorf("failed to copy file: %w", err)
}

// If the file is an archive, recursively extract it
ext := getExt(target)
if _, ok := archiveMap[ext]; ok {
// determine the file type to choose the right extraction method
extract := extractionMethod(ext)
if extract == nil {
return fmt.Errorf("unsupported archive type: %s", ext)
}
if err := extract(ctx, d, target); err != nil {
return fmt.Errorf("failed to extract nested archive: %w", err)
}
}

return nil
}

Expand Down Expand Up @@ -213,18 +188,6 @@ func extractZip(ctx context.Context, d string, f string) error {
return fmt.Errorf("failed to copy file: %w", err)
}

// If the file is an archive, recursively extract it
ext := getExt(name)
if _, ok := archiveMap[ext]; ok {
// determine the file type to choose the right extraction method
extract := extractionMethod(ext)
if extract == nil {
return fmt.Errorf("unsupported archive type: %s", ext)
}
if err := extract(ctx, d, name); err != nil {
return fmt.Errorf("failed to extract nested archive: %w", err)
}
}
open.Close()
create.Close()
}
Expand Down Expand Up @@ -300,8 +263,12 @@ func extractArchiveToTempDir(ctx context.Context, path string) (string, error) {
}

for file := range extractedFiles {
if err := extractNestedArchive(ctx, tmpDir, file, extractedFiles); err != nil {
return "", fmt.Errorf("extract nested archive: %w", err)
ext := getExt(file)
if _, ok := archiveMap[ext]; ok {
fmt.Printf("found a nested archive %s", file)
if err := extractNestedArchive(ctx, tmpDir, file, extractedFiles); err != nil {
return "", fmt.Errorf("extract nested archive: %w", err)
}
}
}

Expand All @@ -314,7 +281,7 @@ func extractionMethod(ext string) func(context.Context, string, string) error {
return extractZip
case ".gz":
return extractGzip
case ".apk", ".gem", ".tar", ".tar.gz", ".tgz":
case ".apk", ".gem", ".tar", ".tar.gz", ".tgz", ".tar.xz":
return extractTar
default:
return nil
Expand Down

0 comments on commit 684ff03

Please sign in to comment.