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

Commit 0febeb5

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

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

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

0 commit comments

Comments
 (0)