dump: compress zip archives

This commit is contained in:
Phillipp Röll
2024-09-15 21:04:54 +02:00
parent bad6c54a33
commit 1a7fafc7eb
8 changed files with 22 additions and 45 deletions
+9 -11
View File
@@ -15,20 +15,18 @@ import (
// A Dumper writes trees and files from a repository to a Writer
// in an archive format.
type Dumper struct {
cache *bloblru.Cache
format string
repo restic.Loader
w io.Writer
compress bool
cache *bloblru.Cache
format string
repo restic.Loader
w io.Writer
}
func New(format string, compress bool, repo restic.Loader, w io.Writer) *Dumper {
func New(format string, repo restic.Loader, w io.Writer) *Dumper {
return &Dumper{
cache: bloblru.New(64 << 20),
format: format,
repo: repo,
w: w,
compress: compress,
cache: bloblru.New(64 << 20),
format: format,
repo: repo,
w: w,
}
}
+2 -2
View File
@@ -23,7 +23,7 @@ func prepareTempdirRepoSrc(t testing.TB, src archiver.TestDir) (string, restic.R
type CheckDump func(t *testing.T, testDir string, testDump *bytes.Buffer) error
func WriteTest(t *testing.T, format string, compress bool, cd CheckDump) {
func WriteTest(t *testing.T, format string, cd CheckDump) {
tests := []struct {
name string
args archiver.TestDir
@@ -85,7 +85,7 @@ func WriteTest(t *testing.T, format string, compress bool, cd CheckDump) {
rtest.OK(t, err)
dst := &bytes.Buffer{}
d := New(format, compress, repo, dst)
d := New(format, repo, dst)
if err := d.DumpTree(ctx, tree, tt.target); err != nil {
t.Fatalf("Dumper.Run error = %v", err)
}
+1 -12
View File
@@ -2,7 +2,6 @@ package dump
import (
"archive/tar"
"compress/gzip"
"context"
"fmt"
"os"
@@ -14,22 +13,12 @@ import (
)
func (d *Dumper) dumpTar(ctx context.Context, ch <-chan *restic.Node) (err error) {
outer := d.w
if d.compress {
outer = gzip.NewWriter(outer)
}
w := tar.NewWriter(outer)
w := tar.NewWriter(d.w)
defer func() {
if err == nil {
err = w.Close()
err = errors.Wrap(err, "Close")
if gz, ok := outer.(*gzip.Writer); ok {
err = gz.Close()
err = errors.Wrap(err, "Close")
}
}
}()
+1 -2
View File
@@ -18,8 +18,7 @@ import (
)
func TestWriteTar(t *testing.T) {
WriteTest(t, "tar", false, checkTar)
WriteTest(t, "tar", true, checkTar)
WriteTest(t, "tar", checkTar)
}
func checkTar(t *testing.T, testDir string, srcTar *bytes.Buffer) error {
+1 -4
View File
@@ -39,10 +39,7 @@ func (d *Dumper) dumpNodeZip(ctx context.Context, node *restic.Node, zw *zip.Wri
Modified: node.ModTime,
}
header.SetMode(node.Mode)
if d.compress {
header.Method = zip.Deflate
}
header.Method = zip.Deflate
if node.Type == restic.NodeTypeDir {
header.Name += "/"
+1 -2
View File
@@ -12,8 +12,7 @@ import (
)
func TestWriteZip(t *testing.T) {
WriteTest(t, "zip", true, checkZip)
WriteTest(t, "zip", false, checkZip)
WriteTest(t, "zip", checkZip)
}
func readZipFile(f *zip.File) ([]byte, error) {