From aaaf3f072601923594754e46c1f0152847d9c2d9 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Wed, 8 May 2013 19:08:11 -0700 Subject: [PATCH] Store the actual archive when commit --- graph.go | 6 +++--- image.go | 45 +++++++++++++++++++++++++++++++++++++++++---- registry.go | 2 +- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/graph.go b/graph.go index 84b8c9a1e9..24b8975dbd 100644 --- a/graph.go +++ b/graph.go @@ -112,7 +112,7 @@ func (graph *Graph) Create(layerData Archive, container *Container, comment, aut img.Container = container.Id img.ContainerConfig = *container.Config } - if err := graph.Register(layerData, img); err != nil { + if err := graph.Register(layerData, true, img); err != nil { return nil, err } go img.Checksum() @@ -121,7 +121,7 @@ func (graph *Graph) Create(layerData Archive, container *Container, comment, aut // Register imports a pre-existing image into the graph. // FIXME: pass img as first argument -func (graph *Graph) Register(layerData Archive, img *Image) error { +func (graph *Graph) Register(layerData Archive, store bool, img *Image) error { if err := ValidateId(img.Id); err != nil { return err } @@ -134,7 +134,7 @@ func (graph *Graph) Register(layerData Archive, img *Image) error { if err != nil { return fmt.Errorf("Mktemp failed: %s", err) } - if err := StoreImage(img, layerData, tmp); err != nil { + if err := StoreImage(img, layerData, tmp, store); err != nil { return err } // Commit diff --git a/image.go b/image.go index d208ed6ce8..55be632880 100644 --- a/image.go +++ b/image.go @@ -56,7 +56,7 @@ func LoadImage(root string) (*Image, error) { return img, nil } -func StoreImage(img *Image, layerData Archive, root string) error { +func StoreImage(img *Image, layerData Archive, root string, store bool) error { // Check that root doesn't already exist if _, err := os.Stat(root); err == nil { return fmt.Errorf("Image %s already exists", img.Id) @@ -68,6 +68,28 @@ func StoreImage(img *Image, layerData Archive, root string) error { if err := os.MkdirAll(layer, 0700); err != nil { return err } + + if store { + layerArchive := layerArchivePath(root) + file, err := os.OpenFile(layerArchive, os.O_WRONLY|os.O_CREATE, 0600) + if err != nil { + return err + } + // FIXME: Retrieve the image layer size from here? + if _, err := io.Copy(file, layerData); err != nil { + return err + } + // FIXME: Don't close/open, read/write instead of Copy + file.Close() + + file, err = os.Open(layerArchive) + if err != nil { + return err + } + defer file.Close() + layerData = file + } + if err := Untar(layerData, layer); err != nil { return err } @@ -86,6 +108,10 @@ func layerPath(root string) string { return path.Join(root, "layer") } +func layerArchivePath(root string) string { + return path.Join(root, "layer.tar.xz") +} + func jsonPath(root string) string { return path.Join(root, "json") } @@ -290,9 +316,20 @@ func (img *Image) Checksum() (string, error) { return "", err } - layerData, err := Tar(layer, Xz) - if err != nil { - return "", err + var layerData io.Reader + + if file, err := os.Open(layerArchivePath(root)); err != nil { + if os.IsNotExist(err) { + layerData, err = Tar(layer, Xz) + if err != nil { + return "", err + } + } else { + return "", err + } + } else { + defer file.Close() + layerData = file } h := sha256.New() diff --git a/registry.go b/registry.go index bf6556f6d5..f674b61ebb 100644 --- a/registry.go +++ b/registry.go @@ -259,7 +259,7 @@ func (graph *Graph) PullImage(stdout io.Writer, imgId, registry string, token [] // FIXME: Keep goging in case of error? return err } - if err = graph.Register(layer, img); err != nil { + if err = graph.Register(layer, false, img); err != nil { return err } }