From fd41449410ab59cfba965a9e4b09b1773f8dc3f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Mon, 2 May 2016 15:12:06 +0200 Subject: [PATCH] Use dirImageDestination for writing to local files in docker.go This will hopefully allow better reuse of the "copy images" code from docker.go in the future. No behavior change, the dirImageDestination code was based on the code this commit is replacing. --- docker.go | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/docker.go b/docker.go index 6c43ac36..25d9efa2 100644 --- a/docker.go +++ b/docker.go @@ -10,7 +10,6 @@ import ( "io/ioutil" "net/http" "os" - "path" "path/filepath" "regexp" "strings" @@ -415,11 +414,12 @@ func (i *dockerImage) Layers(layers ...string) error { if err != nil { return err } + dest := NewDirImageDestination(tmpDir) data, err := json.Marshal(m) if err != nil { return err } - if err := ioutil.WriteFile(path.Join(tmpDir, "manifest.json"), data, 0644); err != nil { + if err := dest.PutManifest(data); err != nil { return err } if len(layers) == 0 { @@ -429,31 +429,20 @@ func (i *dockerImage) Layers(layers ...string) error { if !strings.HasPrefix(l, "sha256:") { l = "sha256:" + l } - if err := i.getLayer(l, tmpDir); err != nil { + if err := i.getLayer(dest, l); err != nil { return err } } return nil } -func (i *dockerImage) getLayer(digest, tmpDir string) error { +func (i *dockerImage) getLayer(dest types.ImageDestination, digest string) error { stream, err := i.src.GetLayer(digest) if err != nil { return err } defer stream.Close() - layerPath := path.Join(tmpDir, strings.Replace(digest, "sha256:", "", -1)+".tar") - layerFile, err := os.Create(layerPath) - if err != nil { - return err - } - if _, err := io.Copy(layerFile, stream); err != nil { - return err - } - if err := layerFile.Sync(); err != nil { - return err - } - return nil + return dest.PutLayer(digest, stream) } // newDockerImageSource is the same as NewDockerImageSource, only it returns the more specific *dockerImageSource type.