-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdecompress.go
174 lines (147 loc) · 4.75 KB
/
decompress.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package selfupdate
import (
"archive/tar"
"archive/zip"
"bytes"
"compress/bzip2"
"compress/gzip"
"errors"
"fmt"
"io"
"path/filepath"
"regexp"
"strings"
"github.com/ulikunitz/xz"
)
var (
fileTypes = []struct {
ext string
decompress func(src io.Reader, cmd, os, arch string) (io.Reader, error)
}{
{".zip", unzip},
{".tar.gz", untar},
{".tgz", untar},
{".gzip", gunzip},
{".gz", gunzip},
{".tar.xz", untarxz},
{".xz", unxz},
{".bz2", unbz2},
}
// pattern copied from bottom of the page: https://semver.org/
semverPattern = `(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?`
)
// DecompressCommand decompresses the given source. Archive and compression format is
// automatically detected from 'url' parameter, which represents the URL of asset,
// or simply a filename (with an extension).
// This returns a reader for the decompressed command given by 'cmd'. '.zip',
// '.tar.gz', '.tar.xz', '.tgz', '.gz', '.bz2' and '.xz' are supported.
//
// These wrapped errors can be returned:
// - ErrCannotDecompressFile
// - ErrExecutableNotFoundInArchive
func DecompressCommand(src io.Reader, url, cmd, os, arch string) (io.Reader, error) {
for _, fileType := range fileTypes {
if strings.HasSuffix(url, fileType.ext) {
return fileType.decompress(src, cmd, os, arch)
}
}
log.Print("File is not compressed")
return src, nil
}
func unzip(src io.Reader, cmd, os, arch string) (io.Reader, error) {
log.Print("Decompressing zip file")
// Zip format requires its file size for Decompressing.
// So we need to read the HTTP response into a buffer at first.
buf, err := io.ReadAll(src)
if err != nil {
return nil, fmt.Errorf("%w zip file: %v", ErrCannotDecompressFile, err)
}
r := bytes.NewReader(buf)
z, err := zip.NewReader(r, r.Size())
if err != nil {
return nil, fmt.Errorf("%w zip file: %s", ErrCannotDecompressFile, err)
}
for _, file := range z.File {
_, name := filepath.Split(file.Name)
if !file.FileInfo().IsDir() && matchExecutableName(cmd, os, arch, name) {
log.Printf("Executable file %q was found in zip archive", file.Name)
return file.Open()
}
}
return nil, fmt.Errorf("%w in zip file: %q", ErrExecutableNotFoundInArchive, cmd)
}
func untar(src io.Reader, cmd, os, arch string) (io.Reader, error) {
log.Print("Decompressing tar.gz file")
gz, err := gzip.NewReader(src)
if err != nil {
return nil, fmt.Errorf("%w tar.gz file: %s", ErrCannotDecompressFile, err)
}
return unarchiveTar(gz, cmd, os, arch)
}
func gunzip(src io.Reader, cmd, os, arch string) (io.Reader, error) {
log.Print("Decompressing gzip file")
r, err := gzip.NewReader(src)
if err != nil {
return nil, fmt.Errorf("%w gzip file: %s", ErrCannotDecompressFile, err)
}
name := r.Header.Name
if !matchExecutableName(cmd, os, arch, name) {
return nil, fmt.Errorf("%w: expected %q but found %q", ErrExecutableNotFoundInArchive, cmd, name)
}
log.Printf("Executable file %q was found in gzip file", name)
return r, nil
}
func untarxz(src io.Reader, cmd, os, arch string) (io.Reader, error) {
log.Print("Decompressing tar.xz file")
xzip, err := xz.NewReader(src)
if err != nil {
return nil, fmt.Errorf("%w tar.xz file: %s", ErrCannotDecompressFile, err)
}
return unarchiveTar(xzip, cmd, os, arch)
}
func unxz(src io.Reader, cmd, os, arch string) (io.Reader, error) {
log.Print("Decompressing xzip file")
xzip, err := xz.NewReader(src)
if err != nil {
return nil, fmt.Errorf("%w xzip file: %s", ErrCannotDecompressFile, err)
}
log.Printf("Decompressed file from xzip is assumed to be an executable: %s", cmd)
return xzip, nil
}
func unbz2(src io.Reader, cmd, os, arch string) (io.Reader, error) {
log.Print("Decompressing bzip2 file")
bz2 := bzip2.NewReader(src)
log.Printf("Decompressed file from bzip2 is assumed to be an executable: %s", cmd)
return bz2, nil
}
func matchExecutableName(cmd, os, arch, target string) bool {
cmd = strings.TrimSuffix(cmd, ".exe")
pattern := regexp.MustCompile(
fmt.Sprintf(
`^%s([_-]%s)?([_-]%s[_-]%s)?(\.exe)?$`,
regexp.QuoteMeta(cmd),
semverPattern,
regexp.QuoteMeta(os),
regexp.QuoteMeta(arch),
),
)
return pattern.MatchString(target)
}
func unarchiveTar(src io.Reader, cmd, os, arch string) (io.Reader, error) {
t := tar.NewReader(src)
for {
h, err := t.Next()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, fmt.Errorf("%w tar file: %s", ErrCannotDecompressFile, err)
}
_, name := filepath.Split(h.Name)
if matchExecutableName(cmd, os, arch, name) {
log.Printf("Executable file %q was found in tar archive", h.Name)
return t, nil
}
}
return nil, fmt.Errorf("%w in tar: %q", ErrExecutableNotFoundInArchive, cmd)
}