Various adjustments to digest package for govet/golint

This commit is contained in:
Stephen J Day 2014-11-19 14:59:05 -08:00
parent 1a508d67d9
commit c0fe9d72d1
6 changed files with 26 additions and 21 deletions

View file

@ -75,8 +75,8 @@ func ParseDigest(s string) (Digest, error) {
return Digest(s), nil return Digest(s), nil
} }
// DigestReader returns the most valid digest for the underlying content. // FromReader returns the most valid digest for the underlying content.
func DigestReader(rd io.Reader) (Digest, error) { func FromReader(rd io.Reader) (Digest, error) {
// TODO(stevvooe): This is pretty inefficient to always be calculating a // TODO(stevvooe): This is pretty inefficient to always be calculating a
// sha256 hash to provide fallback, but it provides some nice semantics in // 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 return d, nil
} }
func DigestBytes(p []byte) (Digest, error) { // FromBytes digests the input and returns a Digest.
return DigestReader(bytes.NewReader(p)) func FromBytes(p []byte) (Digest, error) {
return FromReader(bytes.NewReader(p))
} }
// Algorithm returns the algorithm portion of the digest. This will panic if // Algorithm returns the algorithm portion of the digest. This will panic if

View file

@ -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 // digests and their operations within the registry. The Digest type is
// designed to serve as a flexible identifier in a content-addressable system. // designed to serve as a flexible identifier in a content-addressable system.
// More importantly, it provides tools and wrappers to work with tarsums and // More importantly, it provides tools and wrappers to work with tarsums and

View file

@ -11,6 +11,10 @@ import (
"github.com/docker/docker/pkg/tarsum" "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 { type Verifier interface {
io.Writer io.Writer
@ -23,7 +27,9 @@ type Verifier interface {
// Reset() // 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() alg := d.Algorithm()
switch alg { switch alg {
case "md5", "sha1", "sha256": case "md5", "sha1", "sha256":
@ -62,13 +68,11 @@ func DigestVerifier(d Digest) Verifier {
pw: pw, pw: pw,
} }
} }
panic("unsupported digest: " + d)
} }
// LengthVerifier returns a verifier that returns true when the number of read // NewLengthVerifier returns a verifier that returns true when the number of
// bytes equals the expected parameter. // read bytes equals the expected parameter.
func LengthVerifier(expected int64) Verifier { func NewLengthVerifier(expected int64) Verifier {
return &lengthVerifier{ return &lengthVerifier{
expected: expected, expected: expected,
} }

View file

@ -13,12 +13,12 @@ import (
func TestDigestVerifier(t *testing.T) { func TestDigestVerifier(t *testing.T) {
p := make([]byte, 1<<20) p := make([]byte, 1<<20)
rand.Read(p) rand.Read(p)
digest, err := DigestBytes(p) digest, err := FromBytes(p)
if err != nil { if err != nil {
t.Fatalf("unexpected error digesting bytes: %#v", err) t.Fatalf("unexpected error digesting bytes: %#v", err)
} }
verifier := DigestVerifier(digest) verifier := NewDigestVerifier(digest)
io.Copy(verifier, bytes.NewReader(p)) io.Copy(verifier, bytes.NewReader(p))
if !verifier.Verified() { if !verifier.Verified() {
@ -30,7 +30,7 @@ func TestDigestVerifier(t *testing.T) {
t.Fatalf("error creating tarfile: %v", err) t.Fatalf("error creating tarfile: %v", err)
} }
digest, err = DigestReader(tf) digest, err = FromReader(tf)
if err != nil { if err != nil {
t.Fatalf("error digesting tarsum: %v", err) 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 // This is the most relevant example for the registry application. It's
// effectively a read through pipeline, where the final sink is the digest // effectively a read through pipeline, where the final sink is the digest
// verifier. // verifier.
verifier = DigestVerifier(digest) verifier = NewDigestVerifier(digest)
lengthVerifier := LengthVerifier(expectedSize) lengthVerifier := NewLengthVerifier(expectedSize)
rd := io.TeeReader(tf, lengthVerifier) rd := io.TeeReader(tf, lengthVerifier)
io.Copy(verifier, rd) io.Copy(verifier, rd)

View file

@ -241,8 +241,8 @@ func (luc *layerUploadController) validateLayer(fp layerFile, size int64, dgst d
return "", ErrLayerTarSumVersionUnsupported return "", ErrLayerTarSumVersionUnsupported
} }
digestVerifier := digest.DigestVerifier(dgst) digestVerifier := digest.NewDigestVerifier(dgst)
lengthVerifier := digest.LengthVerifier(size) lengthVerifier := digest.NewLengthVerifier(size)
// First, seek to the end of the file, checking the size is as expected. // First, seek to the end of the file, checking the size is as expected.
end, err := fp.Seek(0, os.SEEK_END) 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. // sink. Instead, its read driven. This migth be okay.
// Calculate an updated digest with the latest version. // Calculate an updated digest with the latest version.
dgst, err = digest.DigestReader(tr) dgst, err = digest.FromReader(tr)
if err != nil { if err != nil {
return "", err return "", err
} }

View file

@ -1,12 +1,12 @@
package storage package storage
import ( import (
"strings"
"github.com/docker/docker-registry/digest"
"fmt" "fmt"
"path" "path"
"strings"
"github.com/docker/docker-registry/common" "github.com/docker/docker-registry/common"
"github.com/docker/docker-registry/digest"
) )
const storagePathVersion = "v2" const storagePathVersion = "v2"