Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverts bc1239ba, no longer needed to conform to legacy #216

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions internal/lz4block/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ const (
Block256Kb
Block1Mb
Block4Mb
Block8Mb = 2 * Block4Mb
)

// In legacy mode all blocks are compressed regardless
// of the compressed size: use the bound size.
var Block8Mb = uint32(CompressBlockBound(8 << 20))

var (
BlockPool64K = sync.Pool{New: func() interface{} { return make([]byte, Block64Kb) }}
BlockPool256K = sync.Pool{New: func() interface{} { return make([]byte, Block256Kb) }}
Expand Down
4 changes: 1 addition & 3 deletions internal/lz4stream/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,7 @@ func (b *FrameDataBlock) Close(f *Frame) {
func (b *FrameDataBlock) Compress(f *Frame, src []byte, level lz4block.CompressionLevel) *FrameDataBlock {
data := b.data
if f.isLegacy() {
// In legacy mode, the buffer is sized according to CompressBlockBound,
// but only 8Mb is buffered for compression.
src = src[:8<<20]
data = data[:cap(data)]
} else {
data = data[:len(src)] // trigger the incompressible flag in CompressBlock
}
Expand Down
57 changes: 57 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"io/ioutil"
"os"
"os/exec"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -285,13 +286,69 @@ func TestWriterLegacy(t *testing.T) {
if _, err := io.Copy(out2, zr); err != nil {
t.Fatal(err)
}

if len(src) != out2.Len() {
t.Fatalf("uncompressed output not correct size. %d != %d", len(src), out2.Len())
}

if !bytes.Equal(out2.Bytes(), src) {
t.Fatal("uncompressed compressed output different from source")
}
})
}
}

func TestWriterLegacyCommand(t *testing.T) {
_, err := exec.LookPath("lz4")
if err != nil {
t.Skip("no lz4 binary to test against")
}

goldenFiles := []string{
"testdata/vmlinux_LZ4_19377",
"testdata/bzImage_lz4_isolated",
}

for _, fname := range goldenFiles {
t.Run(fname, func(t *testing.T) {
fname := fname
t.Parallel()

src, err := ioutil.ReadFile(fname)
if err != nil {
t.Fatal(err)
}

out := new(bytes.Buffer)
zw := lz4.NewWriter(out)
if err := zw.Apply(lz4.LegacyOption(true), lz4.CompressionLevelOption(lz4.Fast)); err != nil {
t.Fatal(err)
}
if _, err := io.Copy(zw, bytes.NewReader(src)); err != nil {
t.Fatal(err)
}
if err := zw.Close(); err != nil {
t.Fatal(err)
}

// write to filesystem for further checking
tmp, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmp.Name())
if _, err := tmp.Write(out.Bytes()); err != nil {
t.Fatal(err)
}

cmd := exec.Command("lz4", "--test", tmp.Name())
if _, err := cmd.Output(); err != nil {
t.Fatal(err)
}
})
}
}

func TestWriterConcurrency(t *testing.T) {
const someGiantFile = "testdata/vmlinux_LZ4_19377"

Expand Down
Loading