From 83b10dbb12d1c0da639b152d2356d3176c3ca1da Mon Sep 17 00:00:00 2001 From: DRON-666 <64691982+DRON-666@users.noreply.github.com> Date: Sat, 19 Dec 2020 02:42:46 +0300 Subject: [PATCH] Deduplicate dumper closing logic --- internal/dump/common.go | 5 ++++- internal/dump/tar.go | 9 +++------ internal/dump/zip.go | 9 +++------ 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/internal/dump/common.go b/internal/dump/common.go index 5b3d82068..fd74a4a07 100644 --- a/internal/dump/common.go +++ b/internal/dump/common.go @@ -12,6 +12,7 @@ import ( // dumper implements saving node data. type dumper interface { + io.Closer dumpNode(ctx context.Context, node *restic.Node, repo restic.Repository) error } @@ -24,11 +25,13 @@ func writeDump(ctx context.Context, repo restic.Repository, tree *restic.Tree, r rootNode.Path = rootPath err := dumpTree(ctx, repo, rootNode, rootPath, dmp) if err != nil { + dmp.Close() + return err } } - return nil + return dmp.Close() } func dumpTree(ctx context.Context, repo restic.Repository, rootNode *restic.Node, rootPath string, dmp dumper) error { diff --git a/internal/dump/tar.go b/internal/dump/tar.go index c1cd9343f..f9716d152 100644 --- a/internal/dump/tar.go +++ b/internal/dump/tar.go @@ -23,13 +23,10 @@ var _ dumper = tarDumper{} func WriteTar(ctx context.Context, repo restic.Repository, tree *restic.Tree, rootPath string, dst io.Writer) error { dmp := tarDumper{w: tar.NewWriter(dst)} - err := writeDump(ctx, repo, tree, rootPath, dmp, dst) - if err != nil { - dmp.w.Close() - - return err - } + return writeDump(ctx, repo, tree, rootPath, dmp, dst) +} +func (dmp tarDumper) Close() error { return dmp.w.Close() } diff --git a/internal/dump/zip.go b/internal/dump/zip.go index f98afa4a0..f1e779d8e 100644 --- a/internal/dump/zip.go +++ b/internal/dump/zip.go @@ -21,13 +21,10 @@ var _ dumper = zipDumper{} func WriteZip(ctx context.Context, repo restic.Repository, tree *restic.Tree, rootPath string, dst io.Writer) error { dmp := zipDumper{w: zip.NewWriter(dst)} - err := writeDump(ctx, repo, tree, rootPath, dmp, dst) - if err != nil { - dmp.w.Close() - - return err - } + return writeDump(ctx, repo, tree, rootPath, dmp, dst) +} +func (dmp zipDumper) Close() error { return dmp.w.Close() }