2015-10-15 22:48:58 +02:00
|
|
|
package module
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// copyDir copies the src directory contents into dst. Both directories
|
|
|
|
// should already exist.
|
|
|
|
func copyDir(dst, src string) error {
|
|
|
|
src, err := filepath.EvalSymlinks(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
walkFn := func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-22 16:28:44 +02:00
|
|
|
|
2015-10-15 22:48:58 +02:00
|
|
|
if path == src {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(filepath.Base(path), ".") {
|
|
|
|
// Skip any dot files
|
|
|
|
if info.IsDir() {
|
|
|
|
return filepath.SkipDir
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The "path" has the src prefixed to it. We need to join our
|
|
|
|
// destination with the path without the src on it.
|
|
|
|
dstPath := filepath.Join(dst, path[len(src):])
|
|
|
|
|
2016-06-22 16:28:44 +02:00
|
|
|
// we don't want to try and copy the same file over itself.
|
2016-06-23 14:02:32 +02:00
|
|
|
if eq, err := sameFile(path, dstPath); eq {
|
2016-06-22 16:28:44 +02:00
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-15 22:48:58 +02:00
|
|
|
// If we have a directory, make that subdirectory, then continue
|
|
|
|
// the walk.
|
|
|
|
if info.IsDir() {
|
|
|
|
if path == filepath.Join(src, dst) {
|
|
|
|
// dst is in src; don't walk it.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(dstPath, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a file, copy the contents.
|
|
|
|
srcF, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcF.Close()
|
|
|
|
|
|
|
|
dstF, err := os.Create(dstPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dstF.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(dstF, srcF); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chmod it
|
|
|
|
return os.Chmod(dstPath, info.Mode())
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Walk(src, walkFn)
|
|
|
|
}
|
2016-06-22 16:28:44 +02:00
|
|
|
|
2016-06-23 14:02:32 +02:00
|
|
|
// sameFile tried to determine if to paths are the same file.
|
|
|
|
// If the paths don't match, we lookup the inode on supported systems.
|
|
|
|
func sameFile(a, b string) (bool, error) {
|
|
|
|
if a == b {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
aIno, err := inode(a)
|
2016-06-22 16:28:44 +02:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2016-06-23 14:02:32 +02:00
|
|
|
bIno, err := inode(b)
|
2016-06-22 16:28:44 +02:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if aIno > 0 && aIno == bIno {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|