From 0cf04c4adf2e1b033cb48aa9f0f4d1fc44e9f50f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Sat, 9 Jul 2016 05:41:47 +0200 Subject: [PATCH] Do not duplicate OCI subdirectory paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of supplying literals to ensureParentDirectoryExists, first compute the path of the file within the directory using the ociReference methods, and then use that to compute the parent directory path. Should not change behavior, except that filepath.Dir() implicitly calls filepath.Clean(), apparently incompatible with UNIX path semantics. But then we always call filepath.Join() with the same d.ref.dir input, which makes the same implicit call, and Go's handling of symlinks and .. seems close-to-unfixably broken in general, and trying to fix that only here is rather pointless: https://github.com/golang/go/issues/4382 Signed-off-by: Miloslav Trmač --- oci/oci_dest.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/oci/oci_dest.go b/oci/oci_dest.go index dd0644fc..6509aba2 100644 --- a/oci/oci_dest.go +++ b/oci/oci_dest.go @@ -76,9 +76,6 @@ func createManifest(m []byte) ([]byte, string, error) { } func (d *ociImageDestination) PutManifest(m []byte) error { - if err := d.ensureParentDirectoryExists("refs"); err != nil { - return err - } // TODO(mitr, runcom): this breaks signatures entirely since at this point we're creating a new manifest // and signatures don't apply anymore. Will fix. ociMan, mt, err := createManifest(m) @@ -106,14 +103,19 @@ func (d *ociImageDestination) PutManifest(m []byte) error { if err := ioutil.WriteFile(d.ref.ociLayoutPath(), []byte(`{"imageLayoutVersion": "1.0.0"}`), 0644); err != nil { return err } - return ioutil.WriteFile(d.ref.descriptorPath(d.ref.tag), data, 0644) + descriptorPath := d.ref.descriptorPath(d.ref.tag) + if err := ensureParentDirectoryExists(descriptorPath); err != nil { + return err + } + return ioutil.WriteFile(descriptorPath, data, 0644) } func (d *ociImageDestination) PutBlob(digest string, stream io.Reader) error { - if err := d.ensureParentDirectoryExists("blobs"); err != nil { + blobPath := d.ref.blobPath(digest) + if err := ensureParentDirectoryExists(blobPath); err != nil { return err } - blob, err := os.Create(d.ref.blobPath(digest)) + blob, err := os.Create(blobPath) if err != nil { return err } @@ -127,10 +129,11 @@ func (d *ociImageDestination) PutBlob(digest string, stream io.Reader) error { return nil } -func (d *ociImageDestination) ensureParentDirectoryExists(parent string) error { - path := filepath.Join(d.ref.dir, parent) - if _, err := os.Stat(path); err != nil && os.IsNotExist(err) { - if err := os.MkdirAll(path, 0755); err != nil { +// ensureParentDirectoryExists ensures the parent of the supplied path exists. +func ensureParentDirectoryExists(path string) error { + parent := filepath.Dir(path) + if _, err := os.Stat(parent); err != nil && os.IsNotExist(err) { + if err := os.MkdirAll(parent, 0755); err != nil { return err } }