Skip to content

Commit 9bcb3e8

Browse files
authored
fix: cannot decompress tar file correctly (#453)
Signed-off-by: rick <LinuxSuRen@users.noreply.github.com> Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
1 parent ca9209c commit 9bcb3e8

File tree

6 files changed

+80
-53
lines changed

6 files changed

+80
-53
lines changed

pkg/compress/gzip.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"os"
10+
"path"
1011
)
1112

1213
// GZip implements a compress which based is based on gzip
@@ -43,7 +44,11 @@ func (c *GZip) ExtractFiles(sourceFile, targetName string) (err error) {
4344
return
4445
}
4546

46-
tarReader := tar.NewReader(gzf)
47+
err = zipProcess(tar.NewReader(gzf), targetName, sourceFile, c.additionBinaries)
48+
return
49+
}
50+
51+
func zipProcess(tarReader *tar.Reader, targetName, sourceFile string, additionBinaries []string) (err error) {
4752
var header *tar.Header
4853
var found bool
4954
for {
@@ -53,20 +58,26 @@ func (c *GZip) ExtractFiles(sourceFile, targetName string) (err error) {
5358
} else if err != nil {
5459
break
5560
}
56-
name := header.Name
61+
name := path.Base(header.Name)
5762

5863
switch header.Typeflag {
5964
case tar.TypeReg:
60-
if err = extraFile(name, targetName, sourceFile, header, tarReader); err == nil {
61-
found = true
65+
if name == targetName {
66+
if err = extraFile(name, targetName, sourceFile, header, tarReader); err == nil {
67+
found = true
68+
} else {
69+
break
70+
}
6271
} else {
63-
break
64-
}
72+
for i := range additionBinaries {
73+
addition := additionBinaries[i]
74+
if name != addition {
75+
continue
76+
}
6577

66-
for i := range c.additionBinaries {
67-
addition := c.additionBinaries[i]
68-
if err = extraFile(addition, addition, sourceFile, header, tarReader); err != nil {
69-
return
78+
if err = extraFile(addition, addition, sourceFile, header, tarReader); err != nil {
79+
return
80+
}
7081
}
7182
}
7283
}

pkg/compress/testdata/simple.tar.gz

231 Bytes
Binary file not shown.

pkg/compress/types.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package compress
22

33
import (
44
"archive/tar"
5-
"fmt"
65
"io"
76
"os"
87
"path/filepath"
@@ -19,7 +18,7 @@ func extraFile(name, targetName, tarFile string, header *tar.Header, tarReader *
1918
return
2019
}
2120
var targetFile *os.File
22-
if targetFile, err = os.OpenFile(fmt.Sprintf("%s/%s", filepath.Dir(tarFile), targetName),
21+
if targetFile, err = os.OpenFile(filepath.Join(filepath.Dir(tarFile), targetName),
2322
os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode)); err != nil {
2423
return
2524
}

pkg/compress/types_test.go

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,36 @@ func TestIsSupport(t *testing.T) {
8585
extension: path.Ext("test.tar.gz"),
8686
},
8787
want: true,
88+
}, {
89+
name: "supported extension: .xz",
90+
args: args{
91+
extension: path.Ext("test.xz"),
92+
},
93+
want: true,
94+
}, {
95+
name: "supported extension: .zip",
96+
args: args{
97+
extension: path.Ext("test.zip"),
98+
},
99+
want: true,
100+
}, {
101+
name: "supported extension: .gz",
102+
args: args{
103+
extension: path.Ext("test.gz"),
104+
},
105+
want: true,
106+
}, {
107+
name: "supported extension: .tgz",
108+
args: args{
109+
extension: path.Ext("test.tgz"),
110+
},
111+
want: true,
112+
}, {
113+
name: "supported extension: .bz2",
114+
args: args{
115+
extension: path.Ext("test.bz2"),
116+
},
117+
want: true,
88118
}, {
89119
name: "not supported extension: .ab",
90120
args: args{
@@ -148,9 +178,6 @@ func Test_extraFile(t *testing.T) {
148178
file, err = os.CreateTemp(os.TempDir(), tt.args.tarFile)
149179
assert.Nil(t, err)
150180
assert.NotNil(t, file)
151-
tt.args.tarFile = file.Name()
152-
tt.args.targetName = path.Base(file.Name())
153-
tt.args.name = tt.args.targetName
154181
}
155182

156183
if err != nil || file == nil {
@@ -160,7 +187,31 @@ func Test_extraFile(t *testing.T) {
160187
defer func() {
161188
_ = os.RemoveAll(tt.args.tarFile)
162189
}()
163-
tt.wantErr(t, extraFile(tt.args.name, tt.args.targetName, tt.args.tarFile, tt.args.header, tt.args.tarReader), fmt.Sprintf("extraFile(%v, %v, %v, %v, %v)", tt.args.name, tt.args.targetName, tt.args.tarFile, tt.args.header, tt.args.tarReader))
190+
err = extraFile(tt.args.name, tt.args.targetName, tt.args.tarFile, tt.args.header, tt.args.tarReader)
191+
tt.wantErr(t, err, fmt.Sprintf("extraFile(%v, %v, %v, %v, %v)", tt.args.name, tt.args.targetName, tt.args.tarFile, tt.args.header, tt.args.tarReader))
164192
})
165193
}
166194
}
195+
196+
func TestExtractFiles(t *testing.T) {
197+
compressor := GetCompressor(".tar.gz", []string{"bb", "cc"})
198+
assert.NotNil(t, compressor)
199+
200+
err := compressor.ExtractFiles("testdata/simple.tar.gz", "aa")
201+
assert.NoError(t, err)
202+
203+
assertFileContentEqual(t, "testdata/aa", "aa\n")
204+
assertFileContentEqual(t, "testdata/bb", "bb\n")
205+
assertFileContentEqual(t, "testdata/cc", "cc\n")
206+
}
207+
208+
func assertFileContentEqual(t *testing.T, filePath string, expectedContent string) {
209+
defer func() {
210+
_ = os.RemoveAll(filePath)
211+
}()
212+
if data, err := os.ReadFile(filePath); err == nil {
213+
assert.Equal(t, expectedContent, string(data))
214+
} else {
215+
t.Fatalf("not found %q: %v", filePath, err)
216+
}
217+
}

pkg/compress/xz.go

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package compress
33
import (
44
"archive/tar"
55
"errors"
6-
"fmt"
76
"github.com/xi2/xz"
8-
"io"
97
"os"
108
)
119

@@ -42,38 +40,6 @@ func (x *Xz) ExtractFiles(sourceFile, targetName string) (err error) {
4240
return
4341
}
4442

45-
var header *tar.Header
46-
var found bool
47-
// Create a tar Reader
48-
tarReader := tar.NewReader(r)
49-
for {
50-
if header, err = tarReader.Next(); err == io.EOF {
51-
err = nil
52-
break
53-
}
54-
if err != nil {
55-
return
56-
}
57-
name := header.Name
58-
59-
switch header.Typeflag {
60-
case tar.TypeReg:
61-
if err = extraFile(name, targetName, sourceFile, header, tarReader); err == nil {
62-
found = true
63-
} else {
64-
break
65-
}
66-
67-
for i := range x.additionBinaries {
68-
addition := x.additionBinaries[i]
69-
if err = extraFile(addition, addition, sourceFile, header, tarReader); err != nil {
70-
return
71-
}
72-
}
73-
}
74-
}
75-
if err == nil && !found {
76-
err = fmt.Errorf("cannot find item '%s' from '%s'", targetName, sourceFile)
77-
}
43+
err = zipProcess(tar.NewReader(r), targetName, sourceFile, x.additionBinaries)
7844
return
7945
}

pkg/installer/fetch_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package installer
22

33
import (
44
"context"
5-
"fmt"
65
"os/user"
6+
"path/filepath"
77
"testing"
88

99
"github.com/stretchr/testify/assert"
@@ -17,7 +17,7 @@ func TestGetConfigDir(t *testing.T) {
1717
fetcher = &DefaultFetcher{}
1818
dir, err := fetcher.GetConfigDir()
1919
assert.Nil(t, err)
20-
assert.Equal(t, fmt.Sprintf("%s/.config/hd-home", u.HomeDir), dir)
20+
assert.Equal(t, filepath.Join(u.HomeDir, ".config", "hd-home"), dir)
2121
fetcher.SetContext(context.TODO())
2222

2323
// test the fake fetcher

0 commit comments

Comments
 (0)