@@ -2,6 +2,7 @@ package provider
22
33import (
44 "fmt"
5+ "io"
56 "io/ioutil"
67 "log"
78 "os"
@@ -19,32 +20,27 @@ func resourceShortName(name, providerName string) string {
1920 return strings .TrimPrefix (name , psn + "_" )
2021}
2122
22- // func copyFile(dst, src string, perm os.FileMode) error {
23- // in, err := os.Open(src)
24- // if err != nil {
25- // return err
26- // }
27- // defer in.Close()
28- // tmp, err := TempFile(filepath.Dir(dst), "")
29- // if err != nil {
30- // return err
31- // }
32- // _, err = io.Copy(tmp, in)
33- // if err != nil {
34- // tmp.Close()
35- // os.Remove(tmp.Name())
36- // return err
37- // }
38- // if err = tmp.Close(); err != nil {
39- // os.Remove(tmp.Name())
40- // return err
41- // }
42- // if err = os.Chmod(tmp.Name(), perm); err != nil {
43- // os.Remove(tmp.Name())
44- // return err
45- // }
46- // return os.Rename(tmp.Name(), dst)
47- // }
23+ func copyFile (srcPath , dstPath string , mode os.FileMode ) error {
24+ srcFile , err := os .Open (srcPath )
25+ if err != nil {
26+ return err
27+ }
28+ defer srcFile .Close ()
29+
30+ // If the destination file already exists, we shouldn't blow it away
31+ dstFile , err := os .OpenFile (dstPath , os .O_WRONLY | os .O_CREATE | os .O_EXCL , mode )
32+ if err != nil {
33+ return err
34+ }
35+ defer dstFile .Close ()
36+
37+ _ , err = io .Copy (dstFile , srcFile )
38+ if err != nil {
39+ return err
40+ }
41+
42+ return nil
43+ }
4844
4945func removeAllExt (file string ) string {
5046 for {
@@ -81,9 +77,34 @@ func runCmd(cmd *exec.Cmd) ([]byte, error) {
8177 return output , nil
8278}
8379
84- func cp (src , dst string ) error {
85- cpCmd := exec .Command ("cp" , "-rf" , src , dst )
86- _ , err := runCmd (cpCmd )
80+ func cp (srcDir , dstDir string ) error {
81+ err := filepath .Walk (srcDir , func (srcPath string , f os.FileInfo , err error ) error {
82+ if err != nil {
83+ return err
84+ }
85+
86+ relPath , err := filepath .Rel (srcDir , srcPath )
87+ if err != nil {
88+ return err
89+ }
90+
91+ dstPath := filepath .Join (dstDir , relPath )
92+
93+ switch mode := f .Mode (); {
94+ case mode .IsDir ():
95+ if err := os .Mkdir (dstPath , f .Mode ()); err != nil && ! os .IsExist (err ) {
96+ return err
97+ }
98+ case mode .IsRegular ():
99+ if err := copyFile (srcPath , dstPath , mode ); err != nil {
100+ return err
101+ }
102+ default :
103+ return fmt .Errorf ("unknown file type (%d / %s) for %s" , f .Mode (), f .Mode ().String (), srcPath )
104+ }
105+
106+ return nil
107+ })
87108 return err
88109}
89110
0 commit comments