Skip to content

Commit 6e17a24

Browse files
committed
Reverts bc1239b, no longer needed to conform to legacy
This fixes the tests and verifies that the fix from bc1239b, which broke the tests, is no longer needed to conform to legacy compression.
1 parent 9542ba5 commit 6e17a24

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

internal/lz4block/blocks.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ const (
88
Block256Kb
99
Block1Mb
1010
Block4Mb
11+
Block8Mb = 2 * Block4Mb
1112
)
1213

13-
// In legacy mode all blocks are compressed regardless
14-
// of the compressed size: use the bound size.
15-
var Block8Mb = uint32(CompressBlockBound(8 << 20))
16-
1714
var (
1815
BlockPool64K = sync.Pool{New: func() interface{} { return make([]byte, Block64Kb) }}
1916
BlockPool256K = sync.Pool{New: func() interface{} { return make([]byte, Block256Kb) }}

internal/lz4stream/block.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,7 @@ func (b *FrameDataBlock) Close(f *Frame) {
224224
func (b *FrameDataBlock) Compress(f *Frame, src []byte, level lz4block.CompressionLevel) *FrameDataBlock {
225225
data := b.data
226226
if f.isLegacy() {
227-
// In legacy mode, the buffer is sized according to CompressBlockBound,
228-
// but only 8Mb is buffered for compression.
229-
src = src[:8<<20]
227+
data = data[:cap(data)]
230228
} else {
231229
data = data[:len(src)] // trigger the incompressible flag in CompressBlock
232230
}

writer_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"io/ioutil"
99
"os"
10+
"os/exec"
1011
"reflect"
1112
"strings"
1213
"testing"
@@ -285,13 +286,69 @@ func TestWriterLegacy(t *testing.T) {
285286
if _, err := io.Copy(out2, zr); err != nil {
286287
t.Fatal(err)
287288
}
289+
290+
if len(src) != out2.Len() {
291+
t.Fatalf("uncompressed output not correct size. %d != %d", len(src), out2.Len())
292+
}
293+
288294
if !bytes.Equal(out2.Bytes(), src) {
289295
t.Fatal("uncompressed compressed output different from source")
290296
}
291297
})
292298
}
293299
}
294300

301+
func TestWriterLegacyCommand(t *testing.T) {
302+
_, err := exec.LookPath("lz4")
303+
if err != nil {
304+
t.Skip("no lz4 binary to test against")
305+
}
306+
307+
goldenFiles := []string{
308+
"testdata/vmlinux_LZ4_19377",
309+
"testdata/bzImage_lz4_isolated",
310+
}
311+
312+
for _, fname := range goldenFiles {
313+
t.Run(fname, func(t *testing.T) {
314+
fname := fname
315+
t.Parallel()
316+
317+
src, err := ioutil.ReadFile(fname)
318+
if err != nil {
319+
t.Fatal(err)
320+
}
321+
322+
out := new(bytes.Buffer)
323+
zw := lz4.NewWriter(out)
324+
if err := zw.Apply(lz4.LegacyOption(true), lz4.CompressionLevelOption(lz4.Fast)); err != nil {
325+
t.Fatal(err)
326+
}
327+
if _, err := io.Copy(zw, bytes.NewReader(src)); err != nil {
328+
t.Fatal(err)
329+
}
330+
if err := zw.Close(); err != nil {
331+
t.Fatal(err)
332+
}
333+
334+
// write to filesystem for further checking
335+
tmp, err := ioutil.TempFile("", "")
336+
if err != nil {
337+
t.Fatal(err)
338+
}
339+
defer os.Remove(tmp.Name())
340+
if _, err := tmp.Write(out.Bytes()); err != nil {
341+
t.Fatal(err)
342+
}
343+
344+
cmd := exec.Command("lz4", "--test", tmp.Name())
345+
if _, err := cmd.Output(); err != nil {
346+
t.Fatal(err)
347+
}
348+
})
349+
}
350+
}
351+
295352
func TestWriterConcurrency(t *testing.T) {
296353
const someGiantFile = "testdata/vmlinux_LZ4_19377"
297354

0 commit comments

Comments
 (0)