-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
tarconverter.go
57 lines (50 loc) · 1.37 KB
/
tarconverter.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
package tarconverter
import (
"archive/tar"
"io"
)
type HeaderConverter func(*tar.Header)
// NewReader returns a reader that applies headerConverter.
// srcContent is drained until hitting EOF.
// Forked from https://github.com/moby/moby/blob/v24.0.6/pkg/archive/copy.go#L308-L373 .
func NewReader(srcContent io.Reader, headerConverter HeaderConverter) io.ReadCloser {
rebased, w := io.Pipe()
go func() {
srcTar := tar.NewReader(srcContent)
rebasedTar := tar.NewWriter(w)
for {
hdr, err := srcTar.Next()
if err == io.EOF {
// Signals end of archive.
rebasedTar.Close()
// drain the reader into io.Discard, until hitting EOF
// https://github.com/moby/buildkit/pull/4807#discussion_r1544621787
_, err = io.Copy(io.Discard, srcContent)
if err != nil {
w.CloseWithError(err)
} else {
w.Close()
}
return
}
if err != nil {
w.CloseWithError(err)
return
}
if headerConverter != nil {
headerConverter(hdr)
}
if err = rebasedTar.WriteHeader(hdr); err != nil {
w.CloseWithError(err)
return
}
// Ignoring GoSec G110. See https://github.com/moby/moby/blob/v24.0.6/pkg/archive/copy.go#L355-L363
//nolint:gosec // G110: Potential DoS vulnerability via decompression bomb (gosec)
if _, err = io.Copy(rebasedTar, srcTar); err != nil {
w.CloseWithError(err)
return
}
}
}()
return rebased
}