From c0fe9d72d1b80bfd68e1f09ab04fa83367dd7666 Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Wed, 19 Nov 2014 14:59:05 -0800 Subject: [PATCH] Various adjustments to digest package for govet/golint --- digest/digest.go | 9 +++++---- digest/doc.go | 2 +- digest/verifiers.go | 16 ++++++++++------ digest/verifiers_test.go | 10 +++++----- storage/layerupload.go | 6 +++--- storage/paths.go | 4 ++-- 6 files changed, 26 insertions(+), 21 deletions(-) diff --git a/digest/digest.go b/digest/digest.go index f2ce021ae..cbd0ab6bc 100644 --- a/digest/digest.go +++ b/digest/digest.go @@ -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 diff --git a/digest/doc.go b/digest/doc.go index 2ce7698c2..278c50e01 100644 --- a/digest/doc.go +++ b/digest/doc.go @@ -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 diff --git a/digest/verifiers.go b/digest/verifiers.go index e738026ac..26b2b2b25 100644 --- a/digest/verifiers.go +++ b/digest/verifiers.go @@ -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, } diff --git a/digest/verifiers_test.go b/digest/verifiers_test.go index 77b02ed07..fb176cc1a 100644 --- a/digest/verifiers_test.go +++ b/digest/verifiers_test.go @@ -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) diff --git a/storage/layerupload.go b/storage/layerupload.go index 4ad021626..c07927f17 100644 --- a/storage/layerupload.go +++ b/storage/layerupload.go @@ -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 } diff --git a/storage/paths.go b/storage/paths.go index aedba3207..18aef17ec 100644 --- a/storage/paths.go +++ b/storage/paths.go @@ -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"