Repository navigation
Expand file tree
/
Copy pathstream_zip.go
More file actions
152 lines (141 loc) · 3.25 KB
/
Copy pathstream_zip.go
File metadata and controls
152 lines (141 loc) · 3.25 KB
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
package archives
import (
"archive/zip"
"bytes"
"context"
"fmt"
"io"
"strings"
)
type zipStream struct {
ctx context.Context
reader *zip.Reader
index int
file *zip.File
current io.ReadCloser
skipUnread bool
}
//nolint:ireturn // archive formats share a sequential source
func newZipStream(ctx context.Context, raw []byte, options StreamOptions, conda bool) (streamSource, error) {
if err := checkZipEntryCountLimitContext(ctx, bytes.NewReader(raw), int64(len(raw)), options.MaxEntries); err != nil {
return nil, err
}
reader, err := zip.NewReader(contextReaderAt{ctx: ctx, reader: bytes.NewReader(raw)}, int64(len(raw)))
if err != nil {
return nil, fmt.Errorf("opening zip: %w", err)
}
if len(reader.File) > options.MaxEntries {
return nil, ErrEntryLimit
}
reader.RegisterDecompressor(zip.Deflate, newFlateReader)
z := &zipStream{ctx: ctx, reader: reader}
if conda {
return &condaStream{outer: &Stream{ctx: ctx, source: z, options: containerOptions(options)}}, nil
}
return z, nil
}
func (z *zipStream) Next() (*StreamEntry, error) {
if z.file != nil && (!z.skipUnread || z.current != nil) {
if _, err := io.Copy(io.Discard, z); err != nil {
return nil, err
}
if err := z.current.Close(); err != nil {
return nil, err
}
z.current = nil
z.file = nil
}
if z.index == len(z.reader.File) {
return nil, io.EOF
}
z.file = z.reader.File[z.index]
z.index++
return &StreamEntry{FileInfo: fileInfoFromZip(z.file)}, nil
}
func (z *zipStream) Read(p []byte) (int, error) {
if err := z.ctx.Err(); err != nil {
return 0, err
}
if z.file == nil {
return 0, io.EOF
}
if z.current == nil {
var err error
z.current, err = z.file.Open()
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
if err != nil {
return 0, err
}
}
return z.current.Read(p)
}
func (z *zipStream) Close() error {
z.reader = nil
z.file = nil
if z.current != nil {
return z.current.Close()
}
return nil
}
func (z *zipStream) recover() {
if z.current != nil {
// A decompressor can repeat the already-reported member error on Close.
_ = z.current.Close()
}
z.current = nil
z.file = nil
}
type condaStream struct {
outer *Stream
inner *tarStream
found bool
}
func (c *condaStream) Next() (*StreamEntry, error) {
for {
if c.inner != nil {
entry, err := c.inner.Next()
if err != io.EOF {
return entry, err
}
if err := c.inner.Close(); err != nil {
return nil, err
}
c.inner = nil
}
entry, err := c.outer.Next()
if err == io.EOF && !c.found {
return nil, fmt.Errorf("no pkg-*.tar.zst or info-*.tar.zst member in conda package")
}
if err != nil {
return nil, err
}
if !strings.HasSuffix(entry.Path, ".tar.zst") ||
(!strings.HasPrefix(entry.Path, "pkg-") && !strings.HasPrefix(entry.Path, "info-")) {
continue
}
c.found = true
c.inner, err = newTarStream(c.outer.ctx, c.outer, compressionZstd)
if err != nil {
return nil, fmt.Errorf("opening %s: %w", entry.Path, err)
}
}
}
func (c *condaStream) Read(p []byte) (int, error) {
if c.inner == nil {
return 0, io.EOF
}
return c.inner.Read(p)
}
func (c *condaStream) Close() error {
var err error
if c.inner != nil {
err = c.inner.Close()
}
outerErr := c.outer.Close()
if err != nil {
return err
}
return outerErr
}