2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-23 11:28:54 +00:00

Remove IDSize, TestRandomID -> NewRandomID

This commit is contained in:
Alexander Neumann 2016-09-01 21:37:59 +02:00
parent 0045f2fb61
commit debf1fce54
7 changed files with 34 additions and 51 deletions

View File

@ -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 // select prefixes of length l, test if the last one is the same as the current one
id := ID{}
outer: outer:
for l := minPrefixLength; l < IDSize; l++ { for l := minPrefixLength; l < len(id); l++ {
var last string var last string
for _, name := range list { for _, name := range list {
@ -66,5 +67,5 @@ outer:
return l, nil return l, nil
} }
return IDSize, nil return len(id), nil
} }

View File

@ -1,10 +1,6 @@
package restic package restic
import ( import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"io"
"testing" "testing"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -21,9 +17,6 @@ type Config struct {
ChunkerPolynomial chunker.Pol `json:"chunker_polynomial"` 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 // RepoVersion is the version that is written to the config when a repository
// is newly created with Init(). // is newly created with Init().
const RepoVersion = 1 const RepoVersion = 1
@ -51,13 +44,7 @@ func CreateConfig() (Config, error) {
return Config{}, errors.Wrap(err, "chunker.RandomPolynomial") return Config{}, errors.Wrap(err, "chunker.RandomPolynomial")
} }
newID := make([]byte, repositoryIDSize) cfg.ID = NewRandomID().String()
_, err = io.ReadFull(rand.Reader, newID)
if err != nil {
return Config{}, errors.Wrap(err, "io.ReadFull")
}
cfg.ID = hex.EncodeToString(newID)
cfg.Version = RepoVersion cfg.Version = RepoVersion
debug.Log("Repo.CreateConfig", "New config: %#v", cfg) 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) { func TestCreateConfig(t testing.TB, pol chunker.Pol) (cfg Config) {
cfg.ChunkerPolynomial = pol cfg.ChunkerPolynomial = pol
newID := make([]byte, repositoryIDSize) cfg.ID = NewRandomID().String()
_, err := io.ReadFull(rand.Reader, newID)
if err != nil {
t.Fatalf("unable to create random ID: %v", err)
}
cfg.ID = hex.EncodeToString(newID)
cfg.Version = RepoVersion cfg.Version = RepoVersion
return cfg return cfg

View File

@ -2,9 +2,11 @@ package restic
import ( import (
"bytes" "bytes"
"crypto/rand"
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"io"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -14,11 +16,11 @@ func Hash(data []byte) ID {
return sha256.Sum256(data) return sha256.Sum256(data)
} }
// IDSize contains the size of an ID, in bytes. // idSize contains the size of an ID, in bytes.
const IDSize = sha256.Size const idSize = sha256.Size
// ID references content within a repository. // ID references content within a repository.
type ID [IDSize]byte type ID [idSize]byte
// ParseID converts the given string to an ID. // ParseID converts the given string to an ID.
func ParseID(s string) (ID, error) { func ParseID(s string) (ID, error) {
@ -28,7 +30,7 @@ func ParseID(s string) (ID, error) {
return ID{}, errors.Wrap(err, "hex.DecodeString") return ID{}, errors.Wrap(err, "hex.DecodeString")
} }
if len(b) != IDSize { if len(b) != idSize {
return ID{}, errors.New("invalid length for hash") return ID{}, errors.New("invalid length for hash")
} }
@ -42,6 +44,17 @@ func (id ID) String() string {
return hex.EncodeToString(id[:]) 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 const shortStr = 4
// Str returns the shortened string version of id. // Str returns the shortened string version of id.

View File

@ -50,13 +50,13 @@ func (p *Packer) Add(t restic.BlobType, id restic.ID, data []byte) (int, error)
return n, errors.Wrap(err, "Write") 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 // headerEntry is used with encoding/binary to read and write header entries
type headerEntry struct { type headerEntry struct {
Type uint8 Type uint8
Length uint32 Length uint32
ID [restic.IDSize]byte ID restic.ID
} }
// Finalize writes the header for all added blobs and finalizes the pack. // Finalize writes the header for all added blobs and finalizes the pack.

View File

@ -55,7 +55,7 @@ func verifyBlobs(t testing.TB, bufs []Buf, k *crypto.Key, rd io.ReaderAt, packSi
// header length // header length
written += binary.Size(uint32(0)) written += binary.Size(uint32(0))
// header // 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 // header crypto
written += crypto.Extension written += crypto.Extension

View File

@ -22,11 +22,11 @@ func TestIndexSerialize(t *testing.T) {
// create 50 packs with 20 blobs each // create 50 packs with 20 blobs each
for i := 0; i < 50; i++ { for i := 0; i < 50; i++ {
packID := restic.TestRandomID() packID := restic.NewRandomID()
pos := uint(0) pos := uint(0)
for j := 0; j < 20; j++ { for j := 0; j < 20; j++ {
id := restic.TestRandomID() id := restic.NewRandomID()
length := uint(i*100 + j) length := uint(i*100 + j)
idx.Store(restic.PackedBlob{ idx.Store(restic.PackedBlob{
Blob: restic.Blob{ Blob: restic.Blob{
@ -94,11 +94,11 @@ func TestIndexSerialize(t *testing.T) {
// add more blobs to idx // add more blobs to idx
newtests := []testEntry{} newtests := []testEntry{}
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
packID := restic.TestRandomID() packID := restic.NewRandomID()
pos := uint(0) pos := uint(0)
for j := 0; j < 10; j++ { for j := 0; j < 10; j++ {
id := restic.TestRandomID() id := restic.NewRandomID()
length := uint(i*100 + j) length := uint(i*100 + j)
idx.Store(restic.PackedBlob{ idx.Store(restic.PackedBlob{
Blob: restic.Blob{ Blob: restic.Blob{
@ -130,7 +130,7 @@ func TestIndexSerialize(t *testing.T) {
Assert(t, idx.Final(), Assert(t, idx.Final(),
"index not final after encoding") "index not final after encoding")
id := restic.TestRandomID() id := restic.NewRandomID()
OK(t, idx.SetID(id)) OK(t, idx.SetID(id))
id2, err := idx.ID() id2, err := idx.ID()
Assert(t, id2.Equal(id), Assert(t, id2.Equal(id),
@ -167,11 +167,11 @@ func TestIndexSize(t *testing.T) {
packs := 200 packs := 200
blobs := 100 blobs := 100
for i := 0; i < packs; i++ { for i := 0; i < packs; i++ {
packID := restic.TestRandomID() packID := restic.NewRandomID()
pos := uint(0) pos := uint(0)
for j := 0; j < blobs; j++ { for j := 0; j < blobs; j++ {
id := restic.TestRandomID() id := restic.NewRandomID()
length := uint(i*100 + j) length := uint(i*100 + j)
idx.Store(restic.PackedBlob{ idx.Store(restic.PackedBlob{
Blob: restic.Blob{ Blob: restic.Blob{
@ -353,11 +353,11 @@ func TestIndexPacks(t *testing.T) {
packs := restic.NewIDSet() packs := restic.NewIDSet()
for i := 0; i < 20; i++ { for i := 0; i < 20; i++ {
packID := restic.TestRandomID() packID := restic.NewRandomID()
idx.Store(restic.PackedBlob{ idx.Store(restic.PackedBlob{
Blob: restic.Blob{ Blob: restic.Blob{
Type: restic.DataBlob, Type: restic.DataBlob,
ID: restic.TestRandomID(), ID: restic.NewRandomID(),
Offset: 0, Offset: 0,
Length: 23, Length: 23,
}, },

View File

@ -1,7 +1,6 @@
package restic package restic
import ( import (
crand "crypto/rand"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
@ -201,14 +200,3 @@ func TestParseID(s string) ID {
return 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
}