From 853b7e82742735e74ea077dddc130fffbc0f53fb Mon Sep 17 00:00:00 2001 From: Lei Jitang Date: Wed, 8 Jul 2015 11:15:09 +0800 Subject: [PATCH] Fix copy from a "created" container. Fixes #14420 Signed-off-by: Lei Jitang --- fileutils/fileutils.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/fileutils/fileutils.go b/fileutils/fileutils.go index 8575150..f5ca966 100644 --- a/fileutils/fileutils.go +++ b/fileutils/fileutils.go @@ -167,3 +167,23 @@ func ReadSymlinkedDirectory(path string) (string, error) { } return realPath, nil } + +// CreateIfNotExists creates a file or a directory only if it does not already exist. +func CreateIfNotExists(path string, isDir bool) error { + if _, err := os.Stat(path); err != nil { + if os.IsNotExist(err) { + if isDir { + return os.MkdirAll(path, 0755) + } + if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { + return err + } + f, err := os.OpenFile(path, os.O_CREATE, 0755) + if err != nil { + return err + } + f.Close() + } + } + return nil +}