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
|
package ingest
import (
"bytes"
"errors"
"fmt"
"hash"
"hash/crc32"
"io"
"os"
)
const streamScannerBufferSize = 64 << 10
// streamScanner incrementally reads/consumes one pack stream while mirroring
// consumed bytes into one destination pack file.
type streamScanner struct {
src io.Reader
dstFile *os.File
// Input buffer window: buf[off:n] is unread.
buf []byte
off int
n int
// Absolute consumed stream bytes.
consumed uint64
// Running pack hash over consumed bytes while hashEnabled is true.
hash hash.Hash
hashSize int
hashEnabled bool
// Entry CRC state while one entry is being consumed.
entryCRC uint32
inEntryCRC bool
packTrailer []byte
}
// newStreamScanner constructs one scanner with fixed input buffering.
func newStreamScanner(src io.Reader, dstFile *os.File, hash hash.Hash, hashSize int) *streamScanner {
return &streamScanner{
src: src,
dstFile: dstFile,
buf: make([]byte, streamScannerBufferSize),
hash: hash,
hashSize: hashSize,
hashEnabled: true,
}
}
// Read implements io.Reader.
func (scanner *streamScanner) Read(dst []byte) (int, error) {
if len(dst) == 0 {
return 0, nil
}
if scanner.n-scanner.off == 0 {
err := scanner.fill(1)
if err != nil {
if errors.Is(err, io.EOF) {
return 0, io.EOF
}
return 0, err
}
}
unread := scanner.n - scanner.off
if unread == 0 {
return 0, io.EOF
}
n := len(dst)
if n > unread {
n = unread
}
copy(dst, scanner.buf[scanner.off:scanner.off+n])
err := scanner.use(n)
if err != nil {
return 0, err
}
return n, nil
}
// ReadByte implements io.ByteReader without allocation.
func (scanner *streamScanner) ReadByte() (byte, error) {
if scanner.n-scanner.off == 0 {
err := scanner.fill(1)
if err != nil {
return 0, err
}
}
b := scanner.buf[scanner.off]
err := scanner.use(1)
if err != nil {
return 0, err
}
return b, nil
}
// fill ensures at least min unread bytes are available in receiver's buffer.
func (scanner *streamScanner) fill(min int) error {
if min <= 0 {
return nil
}
if min > len(scanner.buf) {
return fmt.Errorf("format/pack/ingest: fill(%d) exceeds scanner buffer", min)
}
for scanner.n-scanner.off < min {
err := scanner.flushConsumedPrefix()
if err != nil {
return err
}
readN, err := scanner.src.Read(scanner.buf[scanner.n:])
if readN > 0 {
scanner.n += readN
}
if err != nil {
if errors.Is(err, io.EOF) && scanner.n-scanner.off >= min {
return nil
}
return err
}
if readN == 0 {
return io.ErrNoProgress
}
}
return nil
}
// use consumes n unread bytes and updates accounting/checksum state.
func (scanner *streamScanner) use(n int) error {
if n < 0 || n > scanner.n-scanner.off {
return fmt.Errorf("format/pack/ingest: invalid consume length %d", n)
}
if n == 0 {
return nil
}
chunk := scanner.buf[scanner.off : scanner.off+n]
if scanner.hashEnabled {
_, err := scanner.hash.Write(chunk)
if err != nil {
return err
}
}
if scanner.inEntryCRC {
scanner.entryCRC = crc32.Update(scanner.entryCRC, crc32.IEEETable, chunk)
}
scanner.off += n
scanner.consumed += uint64(n)
return nil
}
// readFull reads exactly len(dst) bytes through receiver.
func (scanner *streamScanner) readFull(dst []byte) error {
_, err := io.ReadFull(scanner, dst)
if err != nil {
return err
}
return nil
}
// flush writes all consumed-but-unflushed bytes to destination pack file.
func (scanner *streamScanner) flush() error {
return scanner.flushConsumedPrefix()
}
// finishAndFlushTrailer reads trailer hash bytes, verifies trailer checksum,
// and ensures no trailing garbage remains in stream.
func (scanner *streamScanner) finishAndFlushTrailer() error {
if scanner.hashSize <= 0 {
return fmt.Errorf("format/pack/ingest: invalid hash size")
}
trailer := make([]byte, scanner.hashSize)
scanner.hashEnabled = false
err := scanner.readFull(trailer)
if err != nil {
return &ErrPackTrailerMismatch{}
}
scanner.packTrailer = append(scanner.packTrailer[:0], trailer...)
var probe [1]byte
n, err := scanner.Read(probe[:])
if n > 0 || err == nil {
return fmt.Errorf("format/pack/ingest: pack has trailing garbage")
}
if !errors.Is(err, io.EOF) {
return err
}
computed := scanner.hash.Sum(nil)
if !bytes.Equal(computed, trailer) {
return &ErrPackTrailerMismatch{}
}
return nil
}
// beginEntryCRC starts inline CRC accumulation for one packed entry.
func (scanner *streamScanner) beginEntryCRC() {
scanner.entryCRC = 0
scanner.inEntryCRC = true
}
// endEntryCRC finishes inline CRC accumulation for one packed entry.
func (scanner *streamScanner) endEntryCRC() (uint32, error) {
if !scanner.inEntryCRC {
return 0, fmt.Errorf("format/pack/ingest: entry CRC not started")
}
crc := scanner.entryCRC
scanner.entryCRC = 0
scanner.inEntryCRC = false
return crc, nil
}
// flushConsumedPrefix writes scanner.buf[:scanner.off] and compacts unread
// bytes to the start of buffer.
func (scanner *streamScanner) flushConsumedPrefix() error {
if scanner.off == 0 {
return nil
}
written := 0
for written < scanner.off {
n, err := scanner.dstFile.Write(scanner.buf[written:scanner.off])
if err != nil {
return &ErrDestinationWrite{Op: fmt.Sprintf("write pack: %v", err)}
}
if n == 0 {
return &ErrDestinationWrite{Op: "write pack: short write"}
}
written += n
}
unread := scanner.n - scanner.off
copy(scanner.buf[:unread], scanner.buf[scanner.off:scanner.n])
scanner.off = 0
scanner.n = unread
return nil
}
|