forked from TrueCloudLab/restic
Cleanups, move Hash() to id.go
This commit is contained in:
parent
b482df04ec
commit
da883d6196
2 changed files with 17 additions and 28 deletions
|
@ -1,29 +1,14 @@
|
||||||
package backend
|
package backend
|
||||||
|
|
||||||
import (
|
import "errors"
|
||||||
"crypto/sha256"
|
|
||||||
"errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
// ErrNoIDPrefixFound is returned by Find() when no ID for the given prefix
|
||||||
MinPrefixLength = 8
|
// could be found.
|
||||||
)
|
var ErrNoIDPrefixFound = errors.New("no ID found")
|
||||||
|
|
||||||
var (
|
// ErrMultipleIDMatches is returned by Find() when multiple IDs with the given
|
||||||
ErrNoIDPrefixFound = errors.New("no ID found")
|
// prefix are found.
|
||||||
ErrMultipleIDMatches = errors.New("multiple IDs with prefix found")
|
var ErrMultipleIDMatches = errors.New("multiple IDs with prefix found")
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
hashData = sha256.Sum256
|
|
||||||
)
|
|
||||||
|
|
||||||
const hashSize = sha256.Size
|
|
||||||
|
|
||||||
// Hash returns the ID for data.
|
|
||||||
func Hash(data []byte) ID {
|
|
||||||
return hashData(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find loads the list of all blobs of type t and searches for names which
|
// Find loads the list of all blobs of type t and searches for names which
|
||||||
// start with prefix. If none is found, nil and ErrNoIDPrefixFound is returned.
|
// start with prefix. If none is found, nil and ErrNoIDPrefixFound is returned.
|
||||||
|
@ -52,6 +37,8 @@ func Find(be Lister, t Type, prefix string) (string, error) {
|
||||||
return "", ErrNoIDPrefixFound
|
return "", ErrNoIDPrefixFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const minPrefixLength = 8
|
||||||
|
|
||||||
// PrefixLength returns the number of bytes required so that all prefixes of
|
// PrefixLength returns the number of bytes required so that all prefixes of
|
||||||
// all names of type t are unique.
|
// all names of type t are unique.
|
||||||
func PrefixLength(be Lister, t Type) (int, error) {
|
func PrefixLength(be Lister, t Type) (int, error) {
|
||||||
|
@ -66,7 +53,7 @@ func PrefixLength(be Lister, t Type) (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
|
||||||
outer:
|
outer:
|
||||||
for l := MinPrefixLength; l < IDSize; l++ {
|
for l := minPrefixLength; l < IDSize; l++ {
|
||||||
var last string
|
var last string
|
||||||
|
|
||||||
for _, name := range list {
|
for _, name := range list {
|
||||||
|
|
|
@ -2,13 +2,19 @@ package backend
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Hash returns the ID for data.
|
||||||
|
func Hash(data []byte) ID {
|
||||||
|
return sha256.Sum256(data)
|
||||||
|
}
|
||||||
|
|
||||||
// IDSize contains the size of an ID, in bytes.
|
// IDSize contains the size of an ID, in bytes.
|
||||||
const IDSize = hashSize
|
const IDSize = sha256.Size
|
||||||
|
|
||||||
// ID references content within a repository.
|
// ID references content within a repository.
|
||||||
type ID [IDSize]byte
|
type ID [IDSize]byte
|
||||||
|
@ -98,7 +104,3 @@ func (id *ID) UnmarshalJSON(b []byte) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func IDFromData(d []byte) ID {
|
|
||||||
return hashData(d)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue