Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Don't try to copy files over themselves #7273

Merged
merged 1 commit into from
Jun 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions config/module/copy_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"strings"
"syscall"
)

// copyDir copies the src directory contents into dst. Both directories
Expand All @@ -19,6 +20,7 @@ func copyDir(dst, src string) error {
if err != nil {
return err
}

if path == src {
return nil
}
Expand All @@ -36,6 +38,19 @@ func copyDir(dst, src string) error {
// destination with the path without the src on it.
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
// the walk.
if info.IsDir() {
Expand Down Expand Up @@ -74,3 +89,36 @@ func copyDir(dst, src string) error {

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
}
2 changes: 2 additions & 0 deletions config/module/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func GetCopy(dst, src string) error {
if err != nil {
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 {
return err
}
Expand Down