Make ArchiveReader a struct

This commit is contained in:
Alexander Neumann 2017-03-02 15:45:35 +01:00
parent 9b776dc7ab
commit f53d33ba34
3 changed files with 47 additions and 9 deletions

View file

@ -272,7 +272,13 @@ func readBackupFromStdin(opts BackupOptions, gopts GlobalOptions, args []string)
return err return err
} }
_, id, err := archiver.ArchiveReader(repo, newArchiveStdinProgress(gopts), os.Stdin, opts.StdinFilename, opts.Tags, opts.Hostname) r := &archiver.Reader{
Repository: repo,
Tags: opts.Tags,
Hostname: opts.Hostname,
}
_, id, err := r.Archive(opts.StdinFilename, os.Stdin, newArchiveStdinProgress(gopts))
if err != nil { if err != nil {
return err return err
} }

View file

@ -11,15 +11,22 @@ import (
"github.com/restic/chunker" "github.com/restic/chunker"
) )
// ArchiveReader reads from the reader and archives the data. Returned is the // Reader allows saving a stream of data to the repository.
// resulting snapshot and its ID. type Reader struct {
func ArchiveReader(repo restic.Repository, p *restic.Progress, rd io.Reader, name string, tags []string, hostname string) (*restic.Snapshot, restic.ID, error) { restic.Repository
Tags []string
Hostname string
}
// Archive reads data from the reader and saves it to the repo.
func (r *Reader) Archive(name string, rd io.Reader, p *restic.Progress) (*restic.Snapshot, restic.ID, error) {
if name == "" { if name == "" {
return nil, restic.ID{}, errors.New("no filename given") return nil, restic.ID{}, errors.New("no filename given")
} }
debug.Log("start archiving %s", name) debug.Log("start archiving %s", name)
sn, err := restic.NewSnapshot([]string{name}, tags, hostname) sn, err := restic.NewSnapshot([]string{name}, r.Tags, r.Hostname)
if err != nil { if err != nil {
return nil, restic.ID{}, err return nil, restic.ID{}, err
} }
@ -27,6 +34,7 @@ func ArchiveReader(repo restic.Repository, p *restic.Progress, rd io.Reader, nam
p.Start() p.Start()
defer p.Done() defer p.Done()
repo := r.Repository
chnker := chunker.New(rd, repo.Config().ChunkerPolynomial) chnker := chunker.New(rd, repo.Config().ChunkerPolynomial)
ids := restic.IDs{} ids := restic.IDs{}

View file

@ -79,7 +79,13 @@ func TestArchiveReader(t *testing.T) {
f := fakeFile(t, seed, size) f := fakeFile(t, seed, size)
sn, id, err := ArchiveReader(repo, nil, f, "fakefile", []string{"test"}, "localhost") r := &Reader{
Repository: repo,
Hostname: "localhost",
Tags: []string{"test"},
}
sn, id, err := r.Archive("fakefile", f, nil)
if err != nil { if err != nil {
t.Fatalf("ArchiveReader() returned error %v", err) t.Fatalf("ArchiveReader() returned error %v", err)
} }
@ -99,7 +105,13 @@ func TestArchiveReaderNull(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo, cleanup := repository.TestRepository(t)
defer cleanup() defer cleanup()
sn, id, err := ArchiveReader(repo, nil, bytes.NewReader(nil), "fakefile", nil, "localhost") r := &Reader{
Repository: repo,
Hostname: "localhost",
Tags: []string{"test"},
}
sn, id, err := r.Archive("fakefile", bytes.NewReader(nil), nil)
if err != nil { if err != nil {
t.Fatalf("ArchiveReader() returned error %v", err) t.Fatalf("ArchiveReader() returned error %v", err)
} }
@ -134,7 +146,13 @@ func TestArchiveReaderError(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo, cleanup := repository.TestRepository(t)
defer cleanup() defer cleanup()
sn, id, err := ArchiveReader(repo, nil, errReader("error returned by reading stdin"), "fakefile", nil, "localhost") r := &Reader{
Repository: repo,
Hostname: "localhost",
Tags: []string{"test"},
}
sn, id, err := r.Archive("fakefile", errReader("error returned by reading stdin"), nil)
if err == nil { if err == nil {
t.Errorf("expected error not returned") t.Errorf("expected error not returned")
} }
@ -167,11 +185,17 @@ func BenchmarkArchiveReader(t *testing.B) {
t.Fatal(err) t.Fatal(err)
} }
r := &Reader{
Repository: repo,
Hostname: "localhost",
Tags: []string{"test"},
}
t.SetBytes(size) t.SetBytes(size)
t.ResetTimer() t.ResetTimer()
for i := 0; i < t.N; i++ { for i := 0; i < t.N; i++ {
_, _, err := ArchiveReader(repo, nil, bytes.NewReader(buf), "fakefile", []string{"test"}, "localhost") _, _, err := r.Archive("fakefile", bytes.NewReader(buf), nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }