From 20f690f1762e369e9ad761e166f22ff57a17275a Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Fri, 8 Nov 2013 12:06:15 -0800 Subject: [PATCH] Make sure dirs are created before injecting file --- container.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/container.go b/container.go index f388a18e2f..6dee2335ff 100644 --- a/container.go +++ b/container.go @@ -396,7 +396,8 @@ func (container *Container) Inject(file io.Reader, pth string) error { } // Return error if path exists - if _, err := os.Stat(path.Join(container.RootfsPath(), pth)); err == nil { + destPath := path.Join(container.RootfsPath(), pth) + if _, err := os.Stat(destPath); err == nil { // Since err is nil, the path could be stat'd and it exists return fmt.Errorf("%s exists", pth) } else if !os.IsNotExist(err) { @@ -405,10 +406,18 @@ func (container *Container) Inject(file io.Reader, pth string) error { return err } - dest, err := os.Create(path.Join(container.RootfsPath(), pth)) + + // Make sure the directory exists + if err := os.MkdirAll(path.Join(container.RootfsPath(), path.Dir(pth)), 0755); err != nil { + return err + } + + dest, err := os.Create(destPath) if err != nil { return err } + defer dest.Close() + if _, err := io.Copy(dest, file); err != nil { return err }