From 04563efa9c0dc1f6a36094dfd884ae432cf46b29 Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Mon, 18 Oct 2021 16:00:08 +0200 Subject: add intermediate export package no to make repo dependant on dna --- dna/drive.go | 36 +++++++++++++------------------- export/exporter.go | 24 ++++++++++++++++++++++ main.go | 5 ++++- repo/export.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ repo/export_dir.go | 60 ------------------------------------------------------ 5 files changed, 100 insertions(+), 83 deletions(-) create mode 100644 export/exporter.go create mode 100644 repo/export.go delete mode 100644 repo/export_dir.go diff --git a/dna/drive.go b/dna/drive.go index 013c4b4..0f62d5e 100644 --- a/dna/drive.go +++ b/dna/drive.go @@ -25,6 +25,7 @@ import ( "os" "path/filepath" + "github.com/n-peugnet/dna-backup/export" "github.com/n-peugnet/dna-backup/logger" "github.com/n-peugnet/dna-backup/utils" ) @@ -56,23 +57,6 @@ type Header struct { Files uint64 } -type dnaVersion struct { - Input dnaInput - Output dnaOutput -} - -type dnaInput struct { - Chunks io.WriteCloser - Recipe io.WriteCloser - Files io.WriteCloser -} - -type dnaOutput struct { - Chunks io.ReadCloser - Recipe io.ReadCloser - Files io.ReadCloser -} - func New( destination string, poolCount int, @@ -105,20 +89,28 @@ func New( } } -func (d *DnaDrive) VersionInput() (dnaInput, <-chan bool) { +func (d *DnaDrive) ExportVersion() (export.Input, <-chan bool) { rChunks, wChunks := io.Pipe() rRecipe, wRecipe := io.Pipe() rFiles, wFiles := io.Pipe() - version := dnaVersion{ - Input: dnaInput{wChunks, wRecipe, wFiles}, - Output: dnaOutput{rChunks, rRecipe, rFiles}, + version := export.Version{ + Input: export.Input{ + Chunks: wChunks, + Recipe: wRecipe, + Files: wFiles, + }, + Output: export.Output{ + Chunks: rChunks, + Recipe: rRecipe, + Files: rFiles, + }, } end := make(chan bool) go d.writeVersion(version.Output, end) return version.Input, end } -func (d *DnaDrive) writeVersion(output dnaOutput, end chan<- bool) { +func (d *DnaDrive) writeVersion(output export.Output, end chan<- bool) { var err error var recipe, files, version bytes.Buffer n := d.write(output.Chunks, d.pools[1:], Forward) diff --git a/export/exporter.go b/export/exporter.go new file mode 100644 index 0000000..208fbd7 --- /dev/null +++ b/export/exporter.go @@ -0,0 +1,24 @@ +package export + +import "io" + +type Version struct { + Input + Output +} + +type Input struct { + Chunks io.WriteCloser + Recipe io.WriteCloser + Files io.WriteCloser +} + +type Output struct { + Chunks io.ReadCloser + Recipe io.ReadCloser + Files io.ReadCloser +} + +type Exporter interface { + ExportVersion() (input Input, end <-chan bool) +} diff --git a/main.go b/main.go index ae180b9..ce1c4b3 100644 --- a/main.go +++ b/main.go @@ -22,8 +22,10 @@ import ( "fmt" "os" + "github.com/n-peugnet/dna-backup/dna" "github.com/n-peugnet/dna-backup/logger" "github.com/n-peugnet/dna-backup/repo" + "github.com/n-peugnet/dna-backup/utils" ) type command struct { @@ -137,7 +139,8 @@ func exportMain(args []string) error { r := repo.NewRepo(source, chunkSize) switch format { case "dir": - r.ExportDir(dest, trackSize) + exporter := dna.New(dest, 96, trackSize, 10000, utils.ZlibWriter, utils.ZlibReader) + r.Export(exporter) case "csv": fmt.Println("csv") default: diff --git a/repo/export.go b/repo/export.go new file mode 100644 index 0000000..dd12077 --- /dev/null +++ b/repo/export.go @@ -0,0 +1,58 @@ +/* Copyright (C) 2021 Nicolas Peugnet + + This file is part of dna-backup. + + dna-backup is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + dna-backup is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with dna-backup. If not, see . */ + +package repo + +import ( + "io" + + "github.com/n-peugnet/dna-backup/export" + "github.com/n-peugnet/dna-backup/logger" +) + +func (r *Repo) Export(exporter export.Exporter) { + r.Init() + chunks := r.loadChunks(r.versions) + for i := range r.versions { + var err error + input, end := exporter.ExportVersion() + if len(chunks[i]) > 0 { + for _, c := range chunks[i] { + _, err := io.Copy(input.Chunks, c.Reader()) + if err != nil { + logger.Error(err) + } + } + input.Chunks.Close() + } + readDelta(r.versions[i], recipeName, r.chunkReadWrapper, func(rc io.ReadCloser) { + _, err = io.Copy(input.Recipe, rc) + if err != nil { + logger.Error("load recipe ", err) + } + input.Recipe.Close() + }) + readDelta(r.versions[i], filesName, r.chunkReadWrapper, func(rc io.ReadCloser) { + _, err = io.Copy(input.Files, rc) + if err != nil { + logger.Error("load files ", err) + } + input.Files.Close() + }) + <-end + } +} diff --git a/repo/export_dir.go b/repo/export_dir.go deleted file mode 100644 index bffa7f0..0000000 --- a/repo/export_dir.go +++ /dev/null @@ -1,60 +0,0 @@ -/* Copyright (C) 2021 Nicolas Peugnet - - This file is part of dna-backup. - - dna-backup is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - dna-backup is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with dna-backup. If not, see . */ - -package repo - -import ( - "io" - - "github.com/n-peugnet/dna-backup/dna" - "github.com/n-peugnet/dna-backup/logger" - "github.com/n-peugnet/dna-backup/utils" -) - -func (r *Repo) ExportDir(dest string, trackSize int) { - r.Init() - exporter := dna.New(dest, 96, trackSize, 10000, utils.ZlibWriter, utils.ZlibReader) - chunks := r.loadChunks(r.versions) - for i := range r.versions { - var err error - input, end := exporter.VersionInput() - if len(chunks[i]) > 0 { - for _, c := range chunks[i] { - _, err := io.Copy(input.Chunks, c.Reader()) - if err != nil { - logger.Error(err) - } - } - input.Chunks.Close() - } - readDelta(r.versions[i], recipeName, r.chunkReadWrapper, func(rc io.ReadCloser) { - _, err = io.Copy(input.Recipe, rc) - if err != nil { - logger.Error("load recipe ", err) - } - input.Recipe.Close() - }) - readDelta(r.versions[i], filesName, r.chunkReadWrapper, func(rc io.ReadCloser) { - _, err = io.Copy(input.Files, rc) - if err != nil { - logger.Error("load files ", err) - } - input.Files.Close() - }) - <-end - } -} -- cgit v1.2.3