Fix copy from a "created" container. Fixes #14420

Signed-off-by: Lei Jitang <leijitang@huawei.com>
This commit is contained in:
Lei Jitang
2015-07-08 11:15:09 +08:00
parent 2fe51ecee7
commit 289ee90b04
3 changed files with 46 additions and 0 deletions
+20
View File
@@ -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
}