forked from itchio/savior
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bzip2source.go
181 lines (150 loc) · 4.42 KB
/
bzip2source.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
package bzip2source
import (
"encoding/gob"
"fmt"
"github.com/go-errors/errors"
"github.com/itchio/kompress/bzip2"
"github.com/itchio/savior"
)
type bzip2Source struct {
// input
source savior.Source
// internal
sr bzip2.SaverReader
offset int64
counter int64
bytebuf []byte
ssc savior.SourceSaveConsumer
sourceCheckpoint *savior.SourceCheckpoint
}
type Bzip2SourceCheckpoint struct {
Offset int64
SourceCheckpoint *savior.SourceCheckpoint
Bzip2Checkpoint *bzip2.Checkpoint
}
var _ savior.Source = (*bzip2Source)(nil)
func New(source savior.Source) *bzip2Source {
return &bzip2Source{
source: source,
bytebuf: []byte{0x00},
}
}
func (bs *bzip2Source) SetSourceSaveConsumer(ssc savior.SourceSaveConsumer) {
savior.Debugf("bzip2: set source save consumer!")
bs.ssc = ssc
bs.source.SetSourceSaveConsumer(&savior.CallbackSourceSaveConsumer{
OnSave: func(checkpoint *savior.SourceCheckpoint) error {
savior.Debugf("bzip2: on save!")
bs.sourceCheckpoint = checkpoint
bs.sr.WantSave()
return nil
},
})
}
func (bs *bzip2Source) WantSave() {
savior.Debugf("bzip2: want save!")
bs.source.WantSave()
}
func (bs *bzip2Source) Resume(checkpoint *savior.SourceCheckpoint) (int64, error) {
savior.Debugf(`bzip2: asked to resume`)
if checkpoint != nil {
if ourCheckpoint, ok := checkpoint.Data.(*Bzip2SourceCheckpoint); ok {
sourceOffset, err := bs.source.Resume(ourCheckpoint.SourceCheckpoint)
if err != nil {
return 0, errors.Wrap(err, 0)
}
bc := ourCheckpoint.Bzip2Checkpoint
if sourceOffset < bc.Roffset {
delta := bc.Roffset - sourceOffset
savior.Debugf(`bzip2source: discarding %d bytes to align source with decompressor`, delta)
err = savior.DiscardByRead(bs.source, delta)
if err != nil {
return 0, errors.Wrap(err, 0)
}
sourceOffset += delta
}
if sourceOffset == bc.Roffset {
bs.sr, err = bc.Resume(bs.source)
if err != nil {
savior.Debugf(`bzip2source: could not use bzip2 checkpoint at R=%d`, bc.Roffset)
// well, let's start over
_, err = bs.source.Resume(nil)
if err != nil {
return 0, errors.Wrap(err, 0)
}
} else {
bs.offset = ourCheckpoint.Offset
return bs.offset, nil
}
} else {
savior.Debugf(`bzip2source: expected source to resume at %d but got %d`, bc.Roffset, sourceOffset)
}
}
}
// start from beginning
sourceOffset, err := bs.source.Resume(nil)
if err != nil {
return 0, errors.Wrap(err, 0)
}
if sourceOffset != 0 {
msg := fmt.Sprintf("bzip2source: expected source to resume at start but got %d", sourceOffset)
return 0, errors.New(msg)
}
bs.sr = bzip2.NewSaverReader(bs.source)
bs.offset = 0
return 0, nil
}
func (bs *bzip2Source) Read(buf []byte) (int, error) {
if bs.sr == nil {
return 0, errors.Wrap(savior.ErrUninitializedSource, 0)
}
n, err := bs.sr.Read(buf)
bs.offset += int64(n)
if err == bzip2.ReadyToSaveError {
err = nil
if bs.sourceCheckpoint == nil {
savior.Debugf("bzip2source: can't save, sourceCheckpoint is nil!")
} else if bs.ssc == nil {
savior.Debugf("bzip2source: can't save, ssc is nil!")
} else {
bzip2Checkpoint, saveErr := bs.sr.Save()
if saveErr != nil {
return n, saveErr
}
savior.Debugf("bzip2source: saving, bzip2 rOffset = %d, sourceCheckpoint.Offset = %d", bzip2Checkpoint.Roffset, bs.sourceCheckpoint.Offset)
checkpoint := &savior.SourceCheckpoint{
Offset: bs.offset,
Data: &Bzip2SourceCheckpoint{
Offset: bs.offset,
Bzip2Checkpoint: bzip2Checkpoint,
SourceCheckpoint: bs.sourceCheckpoint,
},
}
err = bs.ssc.Save(checkpoint)
savior.Debugf("bzip2source: saved checkpoint at byte %d", bs.offset)
}
}
return n, err
}
func (bs *bzip2Source) ReadByte() (byte, error) {
if bs.sr == nil {
return 0, errors.Wrap(savior.ErrUninitializedSource, 0)
}
n, err := bs.Read(bs.bytebuf)
if n == 0 {
/* this happens when Read needs to save, but it swallows the error */
/* we're not meant to surface them, but there's no way to handle a */
/* short read from ReadByte, so we just read again */
n, err = bs.Read(bs.bytebuf)
}
return bs.bytebuf[0], err
}
func (bs *bzip2Source) Progress() float64 {
// We can't tell how large the uncompressed stream is until we finish
// decompressing it. The underlying's source progress is a good enough
// approximation.
return bs.source.Progress()
}
func init() {
gob.Register(&Bzip2SourceCheckpoint{})
}