forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvacation.go
336 lines (285 loc) · 9.67 KB
/
vacation.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Package vacation provides a set of functions that enable input stream
// decompression logic from several popular decompression formats. This allows
// from decompression from either a file or any other byte stream, which is
// useful for decompressing files that are being downloaded.
package vacation
import (
"archive/tar"
"archive/zip"
"bufio"
"bytes"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/gabriel-vasile/mimetype"
"github.com/ulikunitz/xz"
)
// An Archive decompresses tar, gzip and xz compressed tar, and zip files from
// an input stream.
type Archive struct {
reader io.Reader
components int
}
// A TarArchive decompresses tar files from an input stream.
type TarArchive struct {
reader io.Reader
components int
}
// A TarGzipArchive decompresses gziped tar files from an input stream.
type TarGzipArchive struct {
reader io.Reader
components int
}
// A TarXZArchive decompresses xz tar files from an input stream.
type TarXZArchive struct {
reader io.Reader
components int
}
// NewArchive returns a new Archive that reads from inputReader.
func NewArchive(inputReader io.Reader) Archive {
return Archive{reader: inputReader}
}
// NewTarArchive returns a new TarArchive that reads from inputReader.
func NewTarArchive(inputReader io.Reader) TarArchive {
return TarArchive{reader: inputReader}
}
// NewTarGzipArchive returns a new TarGzipArchive that reads from inputReader.
func NewTarGzipArchive(inputReader io.Reader) TarGzipArchive {
return TarGzipArchive{reader: inputReader}
}
// NewTarXZArchive returns a new TarXZArchive that reads from inputReader.
func NewTarXZArchive(inputReader io.Reader) TarXZArchive {
return TarXZArchive{reader: inputReader}
}
// Decompress reads from TarArchive and writes files into the
// destination specified.
func (ta TarArchive) Decompress(destination string) error {
// This map keeps track of what directories have been made already so that we
// only attempt to make them once for a cleaner interaction. This map is
// only necessary in cases where there are no directory headers in the
// tarball, which can be seen in the test around there being no directory
// metadata.
directories := map[string]interface{}{}
tarReader := tar.NewReader(ta.reader)
for {
hdr, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("failed to read tar response: %s", err)
}
fileNames := strings.Split(hdr.Name, string(filepath.Separator))
// Checks to see if file should be written when stripping components
if len(fileNames) <= ta.components {
continue
}
// Constructs the path that conforms to the stripped components.
path := filepath.Join(append([]string{destination}, fileNames[ta.components:]...)...)
// This switch case handles all cases for creating the directory structure
// this logic is needed to handle tarballs with no directory headers.
switch hdr.Typeflag {
case tar.TypeDir:
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create archived directory: %s", err)
}
directories[path] = nil
default:
dir := filepath.Dir(path)
_, ok := directories[dir]
if !ok {
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create archived directory from file path: %s", err)
}
directories[dir] = nil
}
}
// This switch case handles the creation of files during the untaring process.
switch hdr.Typeflag {
case tar.TypeReg:
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, hdr.FileInfo().Mode())
if err != nil {
return fmt.Errorf("failed to create archived file: %s", err)
}
_, err = io.Copy(file, tarReader)
if err != nil {
return err
}
err = file.Close()
if err != nil {
return err
}
case tar.TypeSymlink:
err = os.Symlink(hdr.Linkname, path)
if err != nil {
return fmt.Errorf("failed to extract symlink: %s", err)
}
}
}
return nil
}
// Decompress reads from Archive, determines the archive type of the input
// stream, and writes files into the destination specified.
//
// Archive decompression will also handle files that are types "text/plain;
// charset=utf-8" and write the contents of the input stream to a file name
// "artifact" in the destination directory.
func (a Archive) Decompress(destination string) error {
// Convert reader into a buffered read so that the header can be peeked to
// determine the type.
bufferedReader := bufio.NewReader(a.reader)
// The number 3072 is lifted from the mimetype library and the definition of
// the constant at the time of writing this functionality is listed below.
// https://github.com/gabriel-vasile/mimetype/blob/c64c025a7c2d8d45ba57d3cebb50a1dbedb3ed7e/internal/matchers/matchers.go#L6
header, err := bufferedReader.Peek(3072)
if err != nil && err != io.EOF {
return err
}
mime := mimetype.Detect(header)
// This switch case is reponsible for determining what the decompression
// startegy should be.
switch mime.String() {
case "application/x-tar":
return NewTarArchive(bufferedReader).StripComponents(a.components).Decompress(destination)
case "application/gzip":
return NewTarGzipArchive(bufferedReader).StripComponents(a.components).Decompress(destination)
case "application/x-xz":
return NewTarXZArchive(bufferedReader).StripComponents(a.components).Decompress(destination)
case "application/zip":
return NewZipArchive(bufferedReader).Decompress(destination)
case "text/plain; charset=utf-8":
// This function will write the contents of the reader to file called
// "artifact" in the destination directory
return writeTextFile(bufferedReader, destination)
default:
return fmt.Errorf("unsupported archive type: %s", mime.String())
}
}
// Decompress reads from TarGzipArchive and writes files into the destination
// specified.
func (gz TarGzipArchive) Decompress(destination string) error {
gzr, err := gzip.NewReader(gz.reader)
if err != nil {
return fmt.Errorf("failed to create gzip reader: %w", err)
}
return NewTarArchive(gzr).StripComponents(gz.components).Decompress(destination)
}
// Decompress reads from TarXZArchive and writes files into the destination
// specified.
func (txz TarXZArchive) Decompress(destination string) error {
xzr, err := xz.NewReader(txz.reader)
if err != nil {
return fmt.Errorf("failed to create xz reader: %w", err)
}
return NewTarArchive(xzr).StripComponents(txz.components).Decompress(destination)
}
func writeTextFile(reader io.Reader, destination string) error {
file, err := os.Create(filepath.Join(destination, "artifact"))
if err != nil {
panic(err)
}
_, err = io.Copy(file, reader)
if err != nil {
return err
}
return nil
}
// StripComponents behaves like the --strip-components flag on tar command
// removing the first n levels from the final decompression destination.
// Setting this is a no-op for archive types that do not use --strip-components
// (such as zip).
func (a Archive) StripComponents(components int) Archive {
a.components = components
return a
}
// StripComponents behaves like the --strip-components flag on tar command
// removing the first n levels from the final decompression destination.
func (ta TarArchive) StripComponents(components int) TarArchive {
ta.components = components
return ta
}
// StripComponents behaves like the --strip-components flag on tar command
// removing the first n levels from the final decompression destination.
func (gz TarGzipArchive) StripComponents(components int) TarGzipArchive {
gz.components = components
return gz
}
// StripComponents behaves like the --strip-components flag on tar command
// removing the first n levels from the final decompression destination.
func (txz TarXZArchive) StripComponents(components int) TarXZArchive {
txz.components = components
return txz
}
// A ZipArchive decompresses zip files from an input stream.
type ZipArchive struct {
reader io.Reader
}
// NewZipArchive returns a new ZipArchive that reads from inputReader.
func NewZipArchive(inputReader io.Reader) ZipArchive {
return ZipArchive{reader: inputReader}
}
// Decompress reads from ZipArchive and writes files into the destination
// specified.
func (z ZipArchive) Decompress(destination string) error {
// Have to convert an io.Reader into a bytes.Reader which implements the
// ReadAt function making it compatible with the io.ReaderAt inteface which
// required for zip.NewReader
buff := bytes.NewBuffer(nil)
size, err := io.Copy(buff, z.reader)
if err != nil {
return err
}
readerAt := bytes.NewReader(buff.Bytes())
zr, err := zip.NewReader(readerAt, size)
if err != nil {
return fmt.Errorf("failed to create zip reader: %w", err)
}
for _, f := range zr.File {
path := filepath.Join(destination, f.Name)
switch {
case f.FileInfo().IsDir():
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to unzip directory: %w", err)
}
case f.FileInfo().Mode()&os.ModeSymlink != 0:
fd, err := f.Open()
if err != nil {
return err
}
content, err := io.ReadAll(fd)
if err != nil {
return err
}
err = os.Symlink(string(content), path)
if err != nil {
return fmt.Errorf("failed to unzip symlink: %w", err)
}
default:
err = os.MkdirAll(filepath.Dir(path), os.ModePerm)
if err != nil {
return fmt.Errorf("failed to unzip directory that was part of file path: %w", err)
}
dst, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return fmt.Errorf("failed to unzip file: %w", err)
}
defer dst.Close()
src, err := f.Open()
if err != nil {
return err
}
defer src.Close()
_, err = io.Copy(dst, src)
if err != nil {
return err
}
}
}
return nil
}