internal/pkg: tar optional file
All checks were successful
Test / Create distribution (push) Successful in 47s
Test / Sandbox (push) Successful in 2m49s
Test / ShareFS (push) Successful in 4m46s
Test / Sandbox (race detector) (push) Successful in 5m17s
Test / Hpkg (push) Successful in 5m19s
Test / Hakurei (push) Successful in 5m31s
Test / Hakurei (race detector) (push) Successful in 7m27s
Test / Flake checks (push) Successful in 1m39s

This allows tar to take a single-file directory Artifact as input.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-01-07 22:15:55 +09:00
parent e8dda70c41
commit c86ff02d8d
5 changed files with 187 additions and 55 deletions

View File

@@ -28,15 +28,17 @@ const (
// A tarArtifact is an [Artifact] unpacking a tarball backed by a [File].
type tarArtifact struct {
// Caller-supplied backing tarball.
f File
f Artifact
// Compression on top of the tarball.
compression uint64
}
// NewTar returns a new [Artifact] backed by the supplied [File] and
// compression method.
func NewTar(f File, compression uint64) Artifact {
return &tarArtifact{f: f, compression: compression}
// NewTar returns a new [Artifact] backed by the supplied [Artifact] and
// compression method. If f implements [File], its data might be used directly,
// eliminating the roundtrip to vfs. If f is a directory, it must contain a
// single regular file.
func NewTar(a Artifact, compression uint64) Artifact {
return &tarArtifact{a, compression}
}
// NewHTTPGetTar is abbreviation for NewHTTPGet passed to NewTar.
@@ -76,26 +78,46 @@ func (a *tarArtifact) Cure(c *CureContext) (err error) {
temp := c.GetTempDir()
var tr io.ReadCloser
{
if tr, err = c.OpenFile(a.f); err != nil {
if file, ok := a.f.(File); ok {
if tr, err = c.OpenFile(file); err != nil {
return
}
} else {
var pathname *check.Absolute
if pathname, _, err = c.Cure(a.f); err != nil {
return
}
var entries []os.DirEntry
if entries, err = os.ReadDir(pathname.String()); err != nil {
return
}
if len(entries) != 1 || !entries[0].Type().IsRegular() {
return errors.New(
"input directory does not contain a single regular file",
)
} else {
pathname = pathname.Append(entries[0].Name())
}
if tr, err = os.Open(pathname.String()); err != nil {
return
}
defer func(f io.ReadCloser) {
closeErr := f.Close()
if err == nil {
err = closeErr
}
}(tr)
tr = io.NopCloser(tr)
}
defer func() {
defer func(f io.ReadCloser) {
closeErr := tr.Close()
if err == nil {
err = closeErr
}
}()
closeErr = f.Close()
if err == nil {
err = closeErr
}
}(tr)
tr = io.NopCloser(tr)
switch a.compression {
case TarUncompressed: