From 74f853a2aeaf86ff675ecab5387bbd710a0138b7 Mon Sep 17 00:00:00 2001 From: Mark Allen Date: Tue, 5 Nov 2013 23:44:52 -0600 Subject: [PATCH] See if a path exists before injecting Reader there Closes #1965 --- container.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/container.go b/container.go index 92e05283d1..50bf2ec674 100644 --- a/container.go +++ b/container.go @@ -390,11 +390,22 @@ func (settings *NetworkSettings) PortMappingAPI() []APIPort { // Inject the io.Reader at the given path. Note: do not close the reader func (container *Container) Inject(file io.Reader, pth string) error { + // Return error if path exists + if _, err := os.Stat(path.Join(container.rwPath(), pth)); 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) { + // Expect err might be that the file doesn't exist, so + // if it's some other error, return that. + + return err + } + // Make sure the directory exists if err := os.MkdirAll(path.Join(container.rwPath(), path.Dir(pth)), 0755); err != nil { return err } - // FIXME: Handle permissions/already existing dest + dest, err := os.Create(path.Join(container.rwPath(), pth)) if err != nil { return err