github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/syscall.go (about) 1 // Copyright 2015 The rkt Authors 2 // Copyright (c) 2016 Intel Corporation 3 // 4 // SPDX-License-Identifier: Apache-2.0 5 // 6 7 package virtcontainers 8 9 import ( 10 "fmt" 11 "os" 12 "path/filepath" 13 ) 14 15 // ensureDestinationExists will recursively create a given mountpoint. If directories 16 // are created, their permissions are initialized to mountPerm 17 func ensureDestinationExists(source, destination string) error { 18 fileInfo, err := os.Stat(source) 19 if err != nil { 20 return fmt.Errorf("could not stat source location %v: %v", source, err) 21 } 22 23 targetPathParent, _ := filepath.Split(destination) 24 if err := os.MkdirAll(targetPathParent, mountPerm); err != nil { 25 return fmt.Errorf("could not create parent directory %v: %v", targetPathParent, err) 26 } 27 28 if fileInfo.IsDir() { 29 if err := os.Mkdir(destination, mountPerm); !os.IsExist(err) { 30 return err 31 } 32 } else { 33 file, err := os.OpenFile(destination, os.O_CREATE, mountPerm) 34 if err != nil { 35 return err 36 } 37 38 file.Close() 39 } 40 return nil 41 }