Skip to content
This repository was archived by the owner on May 9, 2025. It is now read-only.

Commit 0dcec81

Browse files
committed
fix: tar without compression
1 parent d9322c0 commit 0dcec81

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/Masterminds/semver/v3 v3.2.0
77
github.com/fluxcd/pkg/apis/meta v0.19.0
88
github.com/fluxcd/pkg/runtime v0.27.0
9-
github.com/fluxcd/pkg/tar v0.2.0
109
github.com/go-git/go-git/v5 v5.6.0
1110
github.com/go-logr/logr v1.2.3
1211
github.com/open-component-model/ocm-controller v0.4.0
@@ -51,7 +50,6 @@ require (
5150
github.com/containers/libtrust v0.0.0-20200511145503-9c3a6c22cd9a // indirect
5251
github.com/containers/ocicrypt v1.1.5 // indirect
5352
github.com/containers/storage v1.42.0 // indirect
54-
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
5553
github.com/davecgh/go-spew v1.1.1 // indirect
5654
github.com/distribution/distribution/v3 v3.0.0-20230214150026-36d8c594d7aa // indirect
5755
github.com/docker/cli v23.0.1+incompatible // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,6 @@ github.com/fluxcd/pkg/apis/meta v0.19.0 h1:CX75e/eaRWZDTzNdMSWomY1InlssLKcS8GQDS
475475
github.com/fluxcd/pkg/apis/meta v0.19.0/go.mod h1:7b6prDPsViyAzoY7eRfSPS0/MbXpGGsOMvRq2QrTKa4=
476476
github.com/fluxcd/pkg/runtime v0.27.0 h1:zVA95Z0KvNjvZxEZhvIbJyJIwtaiv1aVttHZ4YB/FzY=
477477
github.com/fluxcd/pkg/runtime v0.27.0/go.mod h1:fC1l4Wv1hnsqPKB46eDZBXF8RMZm5FXeU4bnJkwGkqk=
478-
github.com/fluxcd/pkg/tar v0.2.0 h1:HEUHgONQYsJGeZZ4x6h5nQU9Aox1I4T3bOp1faWTqf8=
479-
github.com/fluxcd/pkg/tar v0.2.0/go.mod h1:w0/TOC7kwBJhnSJn7TCABkc/I7ib1f2Yz6vOsbLBnhw=
480478
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
481479
github.com/form3tech-oss/jwt-go v3.2.3+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
482480
github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k=

pkg/providers/gogit/git.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
package gogit
66

77
import (
8-
"compress/gzip"
98
"context"
10-
"errors"
119
"fmt"
1210
"os"
1311
"path/filepath"
1412
"time"
1513

16-
"github.com/fluxcd/pkg/tar"
1714
"github.com/go-git/go-git/v5"
1815
"github.com/go-git/go-git/v5/plumbing"
1916
"github.com/go-git/go-git/v5/plumbing/object"
@@ -99,8 +96,8 @@ func (g *Git) Push(ctx context.Context, opts *pkg.PushOptions) (string, error) {
9996

10097
// we only care about the error if it is NOT a header error. Otherwise, we assume the content
10198
// wasn't compressed.
102-
if err = tar.Untar(blob, dir, tar.WithMaxUntarSize(-1)); err != nil && !errors.Is(err, gzip.ErrHeader) {
103-
return "", fmt.Errorf("failed to untar first layer: %w", err)
99+
if err = Untar(blob, dir); err != nil {
100+
return "", fmt.Errorf("failed to untar content: %w", err)
104101
}
105102

106103
// Add all extracted files.

pkg/providers/gogit/tar.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package gogit
2+
3+
import (
4+
"archive/tar"
5+
"errors"
6+
"fmt"
7+
"io"
8+
"os"
9+
"path/filepath"
10+
)
11+
12+
// Untar writes a tar stream to a filesystem.
13+
func Untar(in io.Reader, dir string) error {
14+
tr := tar.NewReader(in)
15+
for {
16+
header, err := tr.Next()
17+
if err != nil {
18+
if errors.Is(err, io.EOF) {
19+
return nil
20+
}
21+
return err
22+
}
23+
24+
abs := filepath.Join(dir, header.Name)
25+
26+
switch header.Typeflag {
27+
case tar.TypeDir:
28+
if err := os.MkdirAll(abs, os.FileMode(header.Mode)); err != nil {
29+
return fmt.Errorf("unable to create directory %s: %w", header.Name, err)
30+
}
31+
case tar.TypeReg:
32+
file, err := os.OpenFile(abs, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(header.Mode))
33+
if err != nil {
34+
return fmt.Errorf("unable to open file %s: %w", header.Name, err)
35+
}
36+
//nolint:gosec // We don't know what size limit we could set, the tar
37+
// archive can be an image layer and that can even reach the gigabyte range.
38+
// For now, we acknowledge the risk.
39+
//
40+
// We checked other softwares and tried to figure out how they manage this,
41+
// but it's handled the same way.
42+
if _, err := io.Copy(file, tr); err != nil {
43+
return fmt.Errorf("unable to copy tar file to filesystem: %w", err)
44+
}
45+
if err := file.Close(); err != nil {
46+
return fmt.Errorf("unable to close file %s: %w", header.Name, err)
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)