-
Notifications
You must be signed in to change notification settings - Fork 0
/
solidblock.go
100 lines (86 loc) · 2.21 KB
/
solidblock.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
package solidblock
import (
"errors"
"hash"
"hash/crc32"
"io"
"io/ioutil"
)
var (
// ErrChecksumMismatch is returned when a file's crc check fails.
ErrChecksumMismatch = errors.New("checksum mismatch")
)
// Solidblock provides sequential access to files that have been concatenated
// into a single compressed data block.
type Solidblock struct {
sizes []uint64
crcs []uint32
base io.Reader
file io.Reader
crc hash.Hash32
target int
index int
}
// New returns a new solidblock reader.
func New(r io.Reader, sizes []uint64, crcs []uint32) *Solidblock {
if len(sizes) != len(crcs) {
panic("crcs slice needs to be the same length as sizes slice")
}
return &Solidblock{
sizes: sizes,
crcs: crcs,
target: -1,
base: r,
}
}
// Next advances to the next file entry in solid block.
//
// Calling Next without reading the current file is supported. Only when Read
// is called will decompression occur for current file. Any skipped files will
// still need to be decompressed, but their contents is discarded.
//
// io.EOF is returned at the end of the input.
func (fr *Solidblock) Next() error {
if fr.target < len(fr.sizes)-1 {
fr.target++
return nil
}
return io.EOF
}
// Read reads from the current file in solid block.
// It returns (0, io.EOF) when it reaches the end of that file,
// until Next is called to advance to the next file.
func (fr *Solidblock) Read(p []byte) (int, error) {
if fr.file != nil && fr.index != fr.target {
// drain current fileReader
_, err := io.Copy(ioutil.Discard, fr.file)
if err != nil {
return 0, err
}
}
if fr.file == nil || fr.index != fr.target {
// discard until we're at the position we want to be at
for i := fr.index + 1; i < fr.target; i++ {
_, err := io.CopyN(ioutil.Discard, fr.base, int64(fr.sizes[i]))
if err != nil {
return 0, err
}
}
fr.crc = crc32.NewIEEE()
fr.file = io.TeeReader(io.LimitReader(fr.base, int64(fr.sizes[fr.target])), fr.crc)
fr.index = fr.target
}
n, err := fr.file.Read(p)
if err == io.EOF {
if fr.crc.Sum32() != fr.crcs[fr.index] {
return n, ErrChecksumMismatch
}
}
return n, err
}
func (fr *Solidblock) Size() int64 {
if fr.target < 0 {
return 0
}
return int64(fr.sizes[fr.target])
}