|
@@ -4,6 +4,7 @@ import (
|
|
"fmt"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"io/ioutil"
|
|
"os"
|
|
"os"
|
|
|
|
+ "path/filepath"
|
|
"testing"
|
|
"testing"
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/assert"
|
|
@@ -22,10 +23,36 @@ func Copy(sourceFile string, destinationFile string) error {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-func CopyDir(src string, dest string) error {
|
|
|
|
|
|
+// checkPathNotContained returns an error if 'subpath' is inside 'path'
|
|
|
|
+func checkPathNotContained(path string, subpath string) error {
|
|
|
|
+ absPath, err := filepath.Abs(path)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
|
|
- if dest[:len(src)] == src {
|
|
|
|
- return fmt.Errorf("Cannot copy a folder into the folder itself!")
|
|
|
|
|
|
+ absSubPath, err := filepath.Abs(subpath)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ current := absSubPath
|
|
|
|
+ for {
|
|
|
|
+ if current == absPath {
|
|
|
|
+ return fmt.Errorf("cannot copy a folder onto itself")
|
|
|
|
+ }
|
|
|
|
+ up := filepath.Dir(current)
|
|
|
|
+ if current == up {
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ current = up
|
|
|
|
+ }
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func CopyDir(src string, dest string) error {
|
|
|
|
+ err := checkPathNotContained(src, dest)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
}
|
|
}
|
|
|
|
|
|
f, err := os.Open(src)
|
|
f, err := os.Open(src)
|