diff --git a/graph/graph.go b/graph/graph.go index 994ce30289..087a6f093c 100644 --- a/graph/graph.go +++ b/graph/graph.go @@ -1,6 +1,8 @@ package graph import ( + "compress/gzip" + "crypto/sha256" "fmt" "io" "io/ioutil" @@ -13,6 +15,7 @@ import ( "time" "github.com/Sirupsen/logrus" + "github.com/docker/distribution/digest" "github.com/docker/docker/autogen/dockerversion" "github.com/docker/docker/daemon/graphdriver" "github.com/docker/docker/image" @@ -242,18 +245,27 @@ func (graph *Graph) newTempFile() (*os.File, error) { return ioutil.TempFile(tmp, "") } -func bufferToFile(f *os.File, src io.Reader) (int64, error) { - n, err := io.Copy(f, src) +func bufferToFile(f *os.File, src io.Reader) (int64, digest.Digest, error) { + var ( + h = sha256.New() + w = gzip.NewWriter(io.MultiWriter(f, h)) + ) + _, err := io.Copy(w, src) + w.Close() if err != nil { - return n, err + return 0, "", err } if err = f.Sync(); err != nil { - return n, err + return 0, "", err + } + n, err := f.Seek(0, os.SEEK_CUR) + if err != nil { + return 0, "", err } if _, err := f.Seek(0, 0); err != nil { - return n, err + return 0, "", err } - return n, nil + return n, digest.NewDigest("sha256", h), nil } // setupInitLayer populates a directory with mountpoints suitable diff --git a/graph/push.go b/graph/push.go index 927c13c9d5..767f118c50 100644 --- a/graph/push.go +++ b/graph/push.go @@ -1,7 +1,6 @@ package graph import ( - "crypto/sha256" "encoding/json" "errors" "fmt" @@ -13,7 +12,6 @@ import ( "sync" "github.com/Sirupsen/logrus" - "github.com/docker/distribution/digest" "github.com/docker/docker/engine" "github.com/docker/docker/image" "github.com/docker/docker/pkg/progressreader" @@ -465,12 +463,7 @@ func (s *TagStore) pushV2Image(r *registry.Session, img *image.Image, endpoint * os.Remove(tf.Name()) }() - h := sha256.New() - size, err := bufferToFile(tf, io.TeeReader(arch, h)) - if err != nil { - return "", err - } - dgst := digest.NewDigest("sha256", h) + size, dgst, err := bufferToFile(tf, arch) // Send the layer logrus.Debugf("rendered layer for %s of [%d] size", img.ID, size)