forked from TrueCloudLab/distribution
Various adjustments to digest package for govet/golint
This commit is contained in:
parent
1a508d67d9
commit
c0fe9d72d1
6 changed files with 26 additions and 21 deletions
|
@ -75,8 +75,8 @@ func ParseDigest(s string) (Digest, error) {
|
|||
return Digest(s), nil
|
||||
}
|
||||
|
||||
// DigestReader returns the most valid digest for the underlying content.
|
||||
func DigestReader(rd io.Reader) (Digest, error) {
|
||||
// FromReader returns the most valid digest for the underlying content.
|
||||
func FromReader(rd io.Reader) (Digest, error) {
|
||||
|
||||
// TODO(stevvooe): This is pretty inefficient to always be calculating a
|
||||
// sha256 hash to provide fallback, but it provides some nice semantics in
|
||||
|
@ -114,8 +114,9 @@ func DigestReader(rd io.Reader) (Digest, error) {
|
|||
return d, nil
|
||||
}
|
||||
|
||||
func DigestBytes(p []byte) (Digest, error) {
|
||||
return DigestReader(bytes.NewReader(p))
|
||||
// FromBytes digests the input and returns a Digest.
|
||||
func FromBytes(p []byte) (Digest, error) {
|
||||
return FromReader(bytes.NewReader(p))
|
||||
}
|
||||
|
||||
// Algorithm returns the algorithm portion of the digest. This will panic if
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// This package provides a generalized type to opaquely represent message
|
||||
// Package digest provides a generalized type to opaquely represent message
|
||||
// digests and their operations within the registry. The Digest type is
|
||||
// designed to serve as a flexible identifier in a content-addressable system.
|
||||
// More importantly, it provides tools and wrappers to work with tarsums and
|
||||
|
|
|
@ -11,6 +11,10 @@ import (
|
|||
"github.com/docker/docker/pkg/tarsum"
|
||||
)
|
||||
|
||||
// Verifier presents a general verification interface to be used with message
|
||||
// digests and other byte stream verifications. Users instantiate a Verifier
|
||||
// from one of the various methods, write the data under test to it then check
|
||||
// the result with the Verified method.
|
||||
type Verifier interface {
|
||||
io.Writer
|
||||
|
||||
|
@ -23,7 +27,9 @@ type Verifier interface {
|
|||
// Reset()
|
||||
}
|
||||
|
||||
func DigestVerifier(d Digest) Verifier {
|
||||
// NewDigestVerifier returns a verifier that compares the written bytes
|
||||
// against a passed in digest.
|
||||
func NewDigestVerifier(d Digest) Verifier {
|
||||
alg := d.Algorithm()
|
||||
switch alg {
|
||||
case "md5", "sha1", "sha256":
|
||||
|
@ -62,13 +68,11 @@ func DigestVerifier(d Digest) Verifier {
|
|||
pw: pw,
|
||||
}
|
||||
}
|
||||
|
||||
panic("unsupported digest: " + d)
|
||||
}
|
||||
|
||||
// LengthVerifier returns a verifier that returns true when the number of read
|
||||
// bytes equals the expected parameter.
|
||||
func LengthVerifier(expected int64) Verifier {
|
||||
// NewLengthVerifier returns a verifier that returns true when the number of
|
||||
// read bytes equals the expected parameter.
|
||||
func NewLengthVerifier(expected int64) Verifier {
|
||||
return &lengthVerifier{
|
||||
expected: expected,
|
||||
}
|
||||
|
|
|
@ -13,12 +13,12 @@ import (
|
|||
func TestDigestVerifier(t *testing.T) {
|
||||
p := make([]byte, 1<<20)
|
||||
rand.Read(p)
|
||||
digest, err := DigestBytes(p)
|
||||
digest, err := FromBytes(p)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error digesting bytes: %#v", err)
|
||||
}
|
||||
|
||||
verifier := DigestVerifier(digest)
|
||||
verifier := NewDigestVerifier(digest)
|
||||
io.Copy(verifier, bytes.NewReader(p))
|
||||
|
||||
if !verifier.Verified() {
|
||||
|
@ -30,7 +30,7 @@ func TestDigestVerifier(t *testing.T) {
|
|||
t.Fatalf("error creating tarfile: %v", err)
|
||||
}
|
||||
|
||||
digest, err = DigestReader(tf)
|
||||
digest, err = FromReader(tf)
|
||||
if err != nil {
|
||||
t.Fatalf("error digesting tarsum: %v", err)
|
||||
}
|
||||
|
@ -45,8 +45,8 @@ func TestDigestVerifier(t *testing.T) {
|
|||
// This is the most relevant example for the registry application. It's
|
||||
// effectively a read through pipeline, where the final sink is the digest
|
||||
// verifier.
|
||||
verifier = DigestVerifier(digest)
|
||||
lengthVerifier := LengthVerifier(expectedSize)
|
||||
verifier = NewDigestVerifier(digest)
|
||||
lengthVerifier := NewLengthVerifier(expectedSize)
|
||||
rd := io.TeeReader(tf, lengthVerifier)
|
||||
io.Copy(verifier, rd)
|
||||
|
||||
|
|
|
@ -241,8 +241,8 @@ func (luc *layerUploadController) validateLayer(fp layerFile, size int64, dgst d
|
|||
return "", ErrLayerTarSumVersionUnsupported
|
||||
}
|
||||
|
||||
digestVerifier := digest.DigestVerifier(dgst)
|
||||
lengthVerifier := digest.LengthVerifier(size)
|
||||
digestVerifier := digest.NewDigestVerifier(dgst)
|
||||
lengthVerifier := digest.NewLengthVerifier(size)
|
||||
|
||||
// First, seek to the end of the file, checking the size is as expected.
|
||||
end, err := fp.Seek(0, os.SEEK_END)
|
||||
|
@ -267,7 +267,7 @@ func (luc *layerUploadController) validateLayer(fp layerFile, size int64, dgst d
|
|||
// sink. Instead, its read driven. This migth be okay.
|
||||
|
||||
// Calculate an updated digest with the latest version.
|
||||
dgst, err = digest.DigestReader(tr)
|
||||
dgst, err = digest.FromReader(tr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"github.com/docker/docker-registry/digest"
|
||||
"fmt"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker-registry/common"
|
||||
"github.com/docker/docker-registry/digest"
|
||||
)
|
||||
|
||||
const storagePathVersion = "v2"
|
||||
|
|
Loading…
Reference in a new issue