From 52f432d83ce799d8ddcb6baeaa9e7094791b1df5 Mon Sep 17 00:00:00 2001 From: Avi Deitcher Date: Mon, 1 Apr 2024 20:24:27 +0300 Subject: [PATCH] add bzip2 support Signed-off-by: Avi Deitcher --- go.mod | 1 + go.sum | 6 ++++++ pkg/compression/bzip2.go | 21 +++++++++++++++++++++ pkg/compression/compressor.go | 4 +++- pkg/compression/gzip.go | 4 ++-- pkg/core/dump.go | 5 ++++- 6 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 pkg/compression/bzip2.go diff --git a/go.mod b/go.mod index 39c14229..00a138cb 100644 --- a/go.mod +++ b/go.mod @@ -48,6 +48,7 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/docker/distribution v2.8.2+incompatible // indirect github.com/docker/go-units v0.5.0 // indirect + github.com/dsnet/compress v0.0.1 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/geoffgarside/ber v1.1.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect diff --git a/go.sum b/go.sum index af0b9d8a..96a49a0f 100644 --- a/go.sum +++ b/go.sum @@ -80,6 +80,9 @@ github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKoh github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q= +github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo= +github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= @@ -134,8 +137,10 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= github.com/klauspost/compress v1.16.5 h1:IFV2oUNUzZaz+XyusxpLzpzS8Pt5rh0Z16For/djlyI= github.com/klauspost/compress v1.16.5/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= @@ -238,6 +243,7 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= +github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/pkg/compression/bzip2.go b/pkg/compression/bzip2.go new file mode 100644 index 00000000..529dbdca --- /dev/null +++ b/pkg/compression/bzip2.go @@ -0,0 +1,21 @@ +package compression + +import ( + "io" + + "github.com/dsnet/compress/bzip2" +) + +type Bzip2Compressor struct { +} + +func (b *Bzip2Compressor) Uncompress(in io.Reader) (io.Reader, error) { + return bzip2.NewReader(in, nil) +} + +func (b *Bzip2Compressor) Compress(out io.Writer) (io.WriteCloser, error) { + return bzip2.NewWriter(out, nil) +} +func (b *Bzip2Compressor) Extension() string { + return "tbz2" +} diff --git a/pkg/compression/compressor.go b/pkg/compression/compressor.go index 108df7a2..545b5e79 100644 --- a/pkg/compression/compressor.go +++ b/pkg/compression/compressor.go @@ -7,7 +7,7 @@ import ( type Compressor interface { Uncompress(in io.Reader) (io.Reader, error) - Compress(out io.Writer) io.WriteCloser + Compress(out io.Writer) (io.WriteCloser, error) Extension() string } @@ -15,6 +15,8 @@ func GetCompressor(name string) (Compressor, error) { switch name { case "gzip": return &GzipCompressor{}, nil + case "bzip2": + return &Bzip2Compressor{}, nil default: return nil, fmt.Errorf("unknown compression format: %s", name) } diff --git a/pkg/compression/gzip.go b/pkg/compression/gzip.go index 3134031c..eabad2ba 100644 --- a/pkg/compression/gzip.go +++ b/pkg/compression/gzip.go @@ -12,8 +12,8 @@ func (g *GzipCompressor) Uncompress(in io.Reader) (io.Reader, error) { return gzip.NewReader(in) } -func (g *GzipCompressor) Compress(out io.Writer) io.WriteCloser { - return gzip.NewWriter(out) +func (g *GzipCompressor) Compress(out io.Writer) (io.WriteCloser, error) { + return gzip.NewWriter(out), nil } func (g *GzipCompressor) Extension() string { return "tgz" diff --git a/pkg/core/dump.go b/pkg/core/dump.go index 19f25036..322d61ce 100644 --- a/pkg/core/dump.go +++ b/pkg/core/dump.go @@ -95,7 +95,10 @@ func Dump(opts DumpOptions) error { return fmt.Errorf("failed to open output file '%s': %v", outFile, err) } defer f.Close() - cw := compressor.Compress(f) + cw, err := compressor.Compress(f) + if err != nil { + return fmt.Errorf("failed to create compressor: %v", err) + } if err := archive.Tar(workdir, cw); err != nil { return fmt.Errorf("error creating the compressed archive: %v", err) }