Merge pull request #7273 from hashicorp/jbardin/GH-7238

core: Don't try to copy files over themselves
This commit is contained in:
James Bardin 2016-06-22 11:38:01 -04:00 committed by GitHub
commit 146d40ac40
2 changed files with 50 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"syscall"
) )
// copyDir copies the src directory contents into dst. Both directories // copyDir copies the src directory contents into dst. Both directories
@ -19,6 +20,7 @@ func copyDir(dst, src string) error {
if err != nil { if err != nil {
return err return err
} }
if path == src { if path == src {
return nil return nil
} }
@ -36,6 +38,19 @@ func copyDir(dst, src string) error {
// destination with the path without the src on it. // destination with the path without the src on it.
dstPath := filepath.Join(dst, path[len(src):]) dstPath := filepath.Join(dst, path[len(src):])
// we don't want to try and copy the same file over itself.
if path == dstPath {
return nil
}
// We still might have the same file through a link, so check the
// inode if we can
if eq, err := sameInode(path, dstPath); eq {
return nil
} else if err != nil {
return err
}
// If we have a directory, make that subdirectory, then continue // If we have a directory, make that subdirectory, then continue
// the walk. // the walk.
if info.IsDir() { if info.IsDir() {
@ -74,3 +89,36 @@ func copyDir(dst, src string) error {
return filepath.Walk(src, walkFn) return filepath.Walk(src, walkFn)
} }
// sameInode looks up the inode for paths a and b and returns if they are
// equal. On windows this will always return false.
func sameInode(a, b string) (bool, error) {
var aIno, bIno uint64
aStat, err := os.Stat(a)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
if st, ok := aStat.Sys().(*syscall.Stat_t); ok {
aIno = st.Ino
}
bStat, err := os.Stat(b)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
if st, ok := bStat.Sys().(*syscall.Stat_t); ok {
bIno = st.Ino
}
if aIno > 0 && aIno == bIno {
return true, nil
}
return false, nil
}

View File

@ -37,6 +37,8 @@ func GetCopy(dst, src string) error {
if err != nil { if err != nil {
return err return err
} }
// FIXME: This isn't completely safe. Creating and removing our temp path
// exposes where to race to inject files.
if err := os.RemoveAll(tmpDir); err != nil { if err := os.RemoveAll(tmpDir); err != nil {
return err return err
} }