internal/pkg: remove tar built-in decompressor
All checks were successful
Test / Create distribution (push) Successful in 1m30s
Test / Sandbox (push) Successful in 5m13s
Test / Hakurei (push) Successful in 7m56s
Test / ShareFS (push) Successful in 7m57s
Test / Hakurei (race detector) (push) Successful in 10m12s
Test / Sandbox (race detector) (push) Successful in 3m3s
Test / Flake checks (push) Successful in 1m47s

This is replaced by decompressArtifact and is no longer necessary.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-06-04 18:37:07 +09:00
parent 76c1fb84c8
commit db69dcf0be
9 changed files with 78 additions and 164 deletions

View File

@@ -2,32 +2,18 @@ package pkg
import (
"archive/tar"
"compress/bzip2"
"compress/gzip"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
)
const (
// TarUncompressed denotes an uncompressed tarball.
TarUncompressed = iota
// TarGzip denotes a tarball compressed via [gzip].
TarGzip
// TarBzip2 denotes a tarball compressed via [bzip2].
TarBzip2
)
// A tarArtifact is an [Artifact] unpacking a tarball backed by a [FileArtifact].
type tarArtifact struct {
// Caller-supplied backing tarball.
f Artifact
// Compression on top of the tarball.
compression uint32
}
// tarArtifactNamed embeds tarArtifact for a [fmt.Stringer] tarball.
@@ -42,10 +28,10 @@ var _ fmt.Stringer = new(tarArtifactNamed)
// String returns the name of the underlying [Artifact] suffixed with unpack.
func (a *tarArtifactNamed) String() string { return a.name + "-unpack" }
// NewTar returns a new [Artifact] backed by the supplied [Artifact] and
// compression method. The source [Artifact] must be a [FileArtifact].
func NewTar(a Artifact, compression uint32) Artifact {
ta := tarArtifact{a, compression}
// NewTar returns a new [Artifact] unpacking the tar stream produced by the
// backing [Artifact]. The source [Artifact] must be a [FileArtifact].
func NewTar(a Artifact) Artifact {
ta := tarArtifact{a}
if s, ok := a.(fmt.Stringer); ok {
if name := s.String(); name != "" {
return &tarArtifactNamed{ta, name}
@@ -54,25 +40,15 @@ func NewTar(a Artifact, compression uint32) Artifact {
return &ta
}
// NewHTTPGetTar is abbreviation for NewHTTPGet passed to NewTar.
func NewHTTPGetTar(
hc *http.Client,
url string,
checksum Checksum,
compression uint32,
) Artifact {
return NewTar(NewHTTPGet(hc, url, checksum), compression)
}
// Kind returns the hardcoded [Kind] constant.
func (a *tarArtifact) Kind() Kind { return KindTar }
// Params writes compression encoded in little endian.
func (a *tarArtifact) Params(ctx *IContext) { ctx.WriteUint32(a.compression) }
// Params is a noop.
func (a *tarArtifact) Params(*IContext) {}
func init() {
register(KindTar, func(r *IRReader) Artifact {
a := NewTar(r.Next(), r.ReadUint32())
a := NewTar(r.Next())
if _, ok := r.Finalise(); ok {
panic(ErrUnexpectedChecksum)
}
@@ -98,42 +74,17 @@ func (e DisallowedTypeflagError) Error() string {
// Cure cures the [Artifact], producing a directory located at work.
func (a *tarArtifact) Cure(t *TContext) (err error) {
var tr io.ReadCloser
if tr, err = t.Open(a.f); err != nil {
var r io.ReadCloser
if r, err = t.Open(a.f); err != nil {
return
}
defer func(f io.ReadCloser) {
if err == nil {
err = tr.Close()
}
closeErr := f.Close()
defer func() {
closeErr := r.Close()
if err == nil {
err = closeErr
}
}(tr)
br := t.cache.getReader(tr)
defer t.cache.putReader(br)
tr = io.NopCloser(br)
switch a.compression {
case TarUncompressed:
break
case TarGzip:
if tr, err = gzip.NewReader(tr); err != nil {
return
}
break
case TarBzip2:
tr = io.NopCloser(bzip2.NewReader(tr))
break
default:
return os.ErrInvalid
}
}()
type dirTargetPerm struct {
path string
@@ -156,8 +107,8 @@ func (a *tarArtifact) Cure(t *TContext) (err error) {
}()
var header *tar.Header
r := tar.NewReader(tr)
for header, err = r.Next(); err == nil; header, err = r.Next() {
tr := tar.NewReader(r)
for header, err = tr.Next(); err == nil; header, err = tr.Next() {
typeflag := header.Typeflag
if typeflag == 0 {
if len(header.Name) > 0 && header.Name[len(header.Name)-1] == '/' {
@@ -183,7 +134,7 @@ func (a *tarArtifact) Cure(t *TContext) (err error) {
); err != nil {
return
}
if _, err = io.Copy(f, r); err != nil {
if _, err = io.Copy(f, tr); err != nil {
_ = f.Close()
return
} else if err = f.Close(); err != nil {