-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace github.com/pierrec/lz4 library with lz4 cli wrapper
github.com/pierrec/lz4 has numerous problems with its legacy writer (e.g. pierrec/lz4#156) that prevents it using for initramfs compression. Replace it with a cli tool ('lz4') wrapper. Fixes #117
- Loading branch information
Showing
8 changed files
with
128 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
) | ||
|
||
// lz4 compressor/decompressor library, wrapper over command-line 'lz4' tool | ||
|
||
func newLz4Reader(r io.Reader) (io.ReadCloser, error) { | ||
return newPipeCommandReader(r, "lz4", "-d", "-c", "-") | ||
} | ||
|
||
func newLz4Writer(w io.Writer, legacy bool) (io.WriteCloser, error) { | ||
args := []string{"-z", "-c"} | ||
if legacy { | ||
args = append(args, "-l") | ||
} | ||
args = append(args, "-") | ||
|
||
return newPipeCommandWriter(w, "lz4", args...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLz4Writer(t *testing.T) { | ||
w := bytes.NewBuffer(nil) | ||
c, err := newLz4Writer(w, true) | ||
require.NoError(t, err) | ||
_, err = c.Write([]byte("hello")) | ||
require.NoError(t, err) | ||
c.Close() | ||
|
||
require.Equal(t, []byte("\x02!L\x18\x06\x00\x00\x00Phello"), w.Bytes()) | ||
} | ||
|
||
func TestLz4Reader(t *testing.T) { | ||
r := bytes.NewBuffer([]byte("\x02!L\x18\x06\x00\x00\x00Phello")) | ||
c, err := newLz4Reader(r) | ||
require.NoError(t, err) | ||
plain, err := io.ReadAll(c) | ||
require.NoError(t, err) | ||
c.Close() | ||
|
||
require.Equal(t, "hello", string(plain)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package main | ||
|
||
// utility class that helps to wrap a command line tool into a pipe-like structure | ||
|
||
import ( | ||
"io" | ||
"os/exec" | ||
) | ||
|
||
type pipeCommandReader struct { | ||
cmd *exec.Cmd | ||
pipe io.ReadCloser | ||
} | ||
|
||
func (r pipeCommandReader) Read(p []byte) (n int, err error) { | ||
return r.pipe.Read(p) | ||
} | ||
|
||
func (r pipeCommandReader) Close() error { | ||
_ = r.pipe.Close() | ||
return r.cmd.Wait() | ||
} | ||
|
||
// newPipeCommandReader creates a new pipe command | ||
// r becomes STDIN for the command | ||
// the function returns a reader that contains information from the command STDOUT | ||
func newPipeCommandReader(r io.Reader, app string, args ...string) (io.ReadCloser, error) { | ||
cmd := exec.Command(app, args...) | ||
cmd.Stdin = r | ||
pipe, err := cmd.StdoutPipe() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := cmd.Start(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return pipeCommandReader{cmd, pipe}, nil | ||
} | ||
|
||
type pipeCommandWriter struct { | ||
cmd *exec.Cmd | ||
pipe io.WriteCloser | ||
} | ||
|
||
func (w pipeCommandWriter) Write(p []byte) (n int, err error) { | ||
return w.pipe.Write(p) | ||
} | ||
|
||
func (w pipeCommandWriter) Close() error { | ||
_ = w.pipe.Close() | ||
return w.cmd.Wait() | ||
} | ||
|
||
// newPipeCommandWriter creates a new pipe command | ||
// w becomes STDOUT for the command | ||
// the function returns a writer that becomes STDIN for the command | ||
func newPipeCommandWriter(w io.Writer, app string, args ...string) (io.WriteCloser, error) { | ||
cmd := exec.Command(app, args...) | ||
cmd.Stdout = w | ||
pipe, err := cmd.StdinPipe() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := cmd.Start(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return pipeCommandWriter{cmd, pipe}, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters