Skip to content

Commit 9b7913e

Browse files
authored
Merge pull request #6 from scunningham/progress
Work around handler behavior in lz4.
2 parents 889f059 + a717e64 commit 9b7913e

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

cmd/plz4/internal/ops/bakeoff.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,6 @@ func _prepLz4(rd io.ReadSeeker, srcSz int64, pw progress.Writer) (bakeFuncT, err
179179
i = 0
180180
)
181181

182-
bsz, err := parseBlockSize(CLI.Bakeoff.BS)
183-
if err != nil {
184-
return nil, err
185-
}
186-
bss := int64(bsz.Size())
187-
188182
tr := &progress.Tracker{
189183
Message: "Processing lz4",
190184
Total: srcSz * 10,
@@ -193,10 +187,21 @@ func _prepLz4(rd io.ReadSeeker, srcSz int64, pw progress.Writer) (bakeFuncT, err
193187

194188
pw.AppendTracker(tr)
195189

196-
// lz4 callback on write is odd, returns compressed block size
197-
// Work around by assuming incremental block size and fix up for overflow
198-
cbHandler := func(_ int) {
199-
tr.Increment(bss)
190+
// lz4 callback on write is buggy. If you call through the Write interface,
191+
// the handler gets called back once with the size of the compressed buffer.
192+
// If you call through the ReadFrom interface, the handler gets called twice,
193+
// once with the size of the compressed buffer and once with the size of the src.
194+
// Work around by ignoring the first callback and accumulating the second.
195+
196+
cnt := 0
197+
cbHandler := func(sz int) {
198+
cnt += 1
199+
if cnt%2 == 0 {
200+
// Ignore every other callback; we only track the src size, effectively
201+
// taking advantage of buggy implementation. This is likely fragile assuming the
202+
// bug will be fixed at some point.
203+
tr.Increment(int64(sz))
204+
}
200205
}
201206

202207
opts = append(opts, lz4.OnBlockDoneOption(cbHandler))
@@ -537,7 +542,6 @@ func _parseBakeLz4Opts(srcSz int64) ([]lz4.Option, error) {
537542

538543
func lz4Level(l int) (lz4.CompressionLevel, error) {
539544

540-
// Last one wins; so append is ok.
541545
var lz4Level lz4.CompressionLevel
542546
switch l {
543547
case 0:

0 commit comments

Comments
 (0)