From debf1fce5426186b2e98ac81bed3b639262ba125 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Thu, 1 Sep 2016 21:37:59 +0200 Subject: [PATCH] Remove IDSize, TestRandomID -> NewRandomID --- src/restic/backend_find.go | 5 +++-- src/restic/config.go | 23 ++--------------------- src/restic/id.go | 21 +++++++++++++++++---- src/restic/pack/pack.go | 4 ++-- src/restic/pack/pack_test.go | 2 +- src/restic/repository/index_test.go | 18 +++++++++--------- src/restic/testing.go | 12 ------------ 7 files changed, 34 insertions(+), 51 deletions(-) diff --git a/src/restic/backend_find.go b/src/restic/backend_find.go index 6ab6427fb..d788e6797 100644 --- a/src/restic/backend_find.go +++ b/src/restic/backend_find.go @@ -52,8 +52,9 @@ func PrefixLength(be Lister, t FileType) (int, error) { } // select prefixes of length l, test if the last one is the same as the current one + id := ID{} outer: - for l := minPrefixLength; l < IDSize; l++ { + for l := minPrefixLength; l < len(id); l++ { var last string for _, name := range list { @@ -66,5 +67,5 @@ outer: return l, nil } - return IDSize, nil + return len(id), nil } diff --git a/src/restic/config.go b/src/restic/config.go index d2e9bd39d..5d1699295 100644 --- a/src/restic/config.go +++ b/src/restic/config.go @@ -1,10 +1,6 @@ package restic import ( - "crypto/rand" - "crypto/sha256" - "encoding/hex" - "io" "testing" "github.com/pkg/errors" @@ -21,9 +17,6 @@ type Config struct { ChunkerPolynomial chunker.Pol `json:"chunker_polynomial"` } -// repositoryIDSize is the length of the ID chosen at random for a new repository. -const repositoryIDSize = sha256.Size - // RepoVersion is the version that is written to the config when a repository // is newly created with Init(). const RepoVersion = 1 @@ -51,13 +44,7 @@ func CreateConfig() (Config, error) { return Config{}, errors.Wrap(err, "chunker.RandomPolynomial") } - newID := make([]byte, repositoryIDSize) - _, err = io.ReadFull(rand.Reader, newID) - if err != nil { - return Config{}, errors.Wrap(err, "io.ReadFull") - } - - cfg.ID = hex.EncodeToString(newID) + cfg.ID = NewRandomID().String() cfg.Version = RepoVersion debug.Log("Repo.CreateConfig", "New config: %#v", cfg) @@ -68,13 +55,7 @@ func CreateConfig() (Config, error) { func TestCreateConfig(t testing.TB, pol chunker.Pol) (cfg Config) { cfg.ChunkerPolynomial = pol - newID := make([]byte, repositoryIDSize) - _, err := io.ReadFull(rand.Reader, newID) - if err != nil { - t.Fatalf("unable to create random ID: %v", err) - } - - cfg.ID = hex.EncodeToString(newID) + cfg.ID = NewRandomID().String() cfg.Version = RepoVersion return cfg diff --git a/src/restic/id.go b/src/restic/id.go index 2e9308888..5a1f4ab6d 100644 --- a/src/restic/id.go +++ b/src/restic/id.go @@ -2,9 +2,11 @@ package restic import ( "bytes" + "crypto/rand" "crypto/sha256" "encoding/hex" "encoding/json" + "io" "github.com/pkg/errors" ) @@ -14,11 +16,11 @@ func Hash(data []byte) ID { return sha256.Sum256(data) } -// IDSize contains the size of an ID, in bytes. -const IDSize = sha256.Size +// idSize contains the size of an ID, in bytes. +const idSize = sha256.Size // ID references content within a repository. -type ID [IDSize]byte +type ID [idSize]byte // ParseID converts the given string to an ID. func ParseID(s string) (ID, error) { @@ -28,7 +30,7 @@ func ParseID(s string) (ID, error) { return ID{}, errors.Wrap(err, "hex.DecodeString") } - if len(b) != IDSize { + if len(b) != idSize { return ID{}, errors.New("invalid length for hash") } @@ -42,6 +44,17 @@ func (id ID) String() string { return hex.EncodeToString(id[:]) } +// NewRandomID retuns a randomly generated ID. When reading from rand fails, +// the function panics. +func NewRandomID() ID { + id := ID{} + _, err := io.ReadFull(rand.Reader, id[:]) + if err != nil { + panic(err) + } + return id +} + const shortStr = 4 // Str returns the shortened string version of id. diff --git a/src/restic/pack/pack.go b/src/restic/pack/pack.go index e100578c0..a1e62d543 100644 --- a/src/restic/pack/pack.go +++ b/src/restic/pack/pack.go @@ -50,13 +50,13 @@ func (p *Packer) Add(t restic.BlobType, id restic.ID, data []byte) (int, error) return n, errors.Wrap(err, "Write") } -var entrySize = uint(binary.Size(restic.BlobType(0)) + binary.Size(uint32(0)) + restic.IDSize) +var entrySize = uint(binary.Size(restic.BlobType(0)) + binary.Size(uint32(0)) + len(restic.ID{})) // headerEntry is used with encoding/binary to read and write header entries type headerEntry struct { Type uint8 Length uint32 - ID [restic.IDSize]byte + ID restic.ID } // Finalize writes the header for all added blobs and finalizes the pack. diff --git a/src/restic/pack/pack_test.go b/src/restic/pack/pack_test.go index 8a21785df..f90d1a426 100644 --- a/src/restic/pack/pack_test.go +++ b/src/restic/pack/pack_test.go @@ -55,7 +55,7 @@ func verifyBlobs(t testing.TB, bufs []Buf, k *crypto.Key, rd io.ReaderAt, packSi // header length written += binary.Size(uint32(0)) // header - written += len(bufs) * (binary.Size(restic.BlobType(0)) + binary.Size(uint32(0)) + restic.IDSize) + written += len(bufs) * (binary.Size(restic.BlobType(0)) + binary.Size(uint32(0)) + len(restic.ID{})) // header crypto written += crypto.Extension diff --git a/src/restic/repository/index_test.go b/src/restic/repository/index_test.go index 65e8cdbd7..243104547 100644 --- a/src/restic/repository/index_test.go +++ b/src/restic/repository/index_test.go @@ -22,11 +22,11 @@ func TestIndexSerialize(t *testing.T) { // create 50 packs with 20 blobs each for i := 0; i < 50; i++ { - packID := restic.TestRandomID() + packID := restic.NewRandomID() pos := uint(0) for j := 0; j < 20; j++ { - id := restic.TestRandomID() + id := restic.NewRandomID() length := uint(i*100 + j) idx.Store(restic.PackedBlob{ Blob: restic.Blob{ @@ -94,11 +94,11 @@ func TestIndexSerialize(t *testing.T) { // add more blobs to idx newtests := []testEntry{} for i := 0; i < 10; i++ { - packID := restic.TestRandomID() + packID := restic.NewRandomID() pos := uint(0) for j := 0; j < 10; j++ { - id := restic.TestRandomID() + id := restic.NewRandomID() length := uint(i*100 + j) idx.Store(restic.PackedBlob{ Blob: restic.Blob{ @@ -130,7 +130,7 @@ func TestIndexSerialize(t *testing.T) { Assert(t, idx.Final(), "index not final after encoding") - id := restic.TestRandomID() + id := restic.NewRandomID() OK(t, idx.SetID(id)) id2, err := idx.ID() Assert(t, id2.Equal(id), @@ -167,11 +167,11 @@ func TestIndexSize(t *testing.T) { packs := 200 blobs := 100 for i := 0; i < packs; i++ { - packID := restic.TestRandomID() + packID := restic.NewRandomID() pos := uint(0) for j := 0; j < blobs; j++ { - id := restic.TestRandomID() + id := restic.NewRandomID() length := uint(i*100 + j) idx.Store(restic.PackedBlob{ Blob: restic.Blob{ @@ -353,11 +353,11 @@ func TestIndexPacks(t *testing.T) { packs := restic.NewIDSet() for i := 0; i < 20; i++ { - packID := restic.TestRandomID() + packID := restic.NewRandomID() idx.Store(restic.PackedBlob{ Blob: restic.Blob{ Type: restic.DataBlob, - ID: restic.TestRandomID(), + ID: restic.NewRandomID(), Offset: 0, Length: 23, }, diff --git a/src/restic/testing.go b/src/restic/testing.go index a28f98d7d..1be705bb4 100644 --- a/src/restic/testing.go +++ b/src/restic/testing.go @@ -1,7 +1,6 @@ package restic import ( - crand "crypto/rand" "encoding/json" "fmt" "io" @@ -201,14 +200,3 @@ func TestParseID(s string) ID { return id } - -// TestRandomID retuns a randomly generated ID. When reading from rand fails, -// the function panics. -func TestRandomID() ID { - id := ID{} - _, err := io.ReadFull(crand.Reader, id[:]) - if err != nil { - panic(err) - } - return id -}