diff --git a/base/base.go b/base/base.go index 3841e68..f1731ce 100644 --- a/base/base.go +++ b/base/base.go @@ -53,21 +53,7 @@ type ReadSeekCloser interface { // SameFile checks whether the object of `sum` address, and `path` file path are the same file. // This checks by inode and device. func (b Base) SameFile(sum, path string) bool { - var ( - bInode, dInode uint64 - err error - ) - if bInode, err = file.GetInode(b.blobPath(sum)); err != nil { - return false - } - if dInode, err = file.GetInode(path); err != nil { - return false - } - if bInode == dInode { - return true - } - return false - + return file.SameFile(b.blobPath(sum), path) } // GetBlob store the content from src, for the sum and hashType diff --git a/file/dev.go b/file/dev.go index 49ee224..23c760a 100644 --- a/file/dev.go +++ b/file/dev.go @@ -83,6 +83,27 @@ func GetDev(path string) (uint64, error) { return stat.Dev, nil } +// SameFile checks whether the two paths are same device and inode +func SameFile(fpath1, fpath2 string) bool { + bStat, err := GetStat(fpath1) + if err != nil { + return false + } + dStat, err := GetStat(fpath2) + if err != nil { + return false + } + if bStat.Dev != dStat.Dev { + return false + } + if bStat.Ino != dStat.Ino { + return false + } + // if we made it here, we must be ok + return true + +} + // GetNlink returns the number of links for path. For directories, that is // number of entries. For regular files, that is number of hardlinks. func GetNlink(path string) (uint64, error) { diff --git a/main.go b/main.go index 75f7da7..6dd9e17 100644 --- a/main.go +++ b/main.go @@ -83,7 +83,7 @@ func main() { fmt.Printf("%s [%d] %s\n", fi.Hash, fi.Size, fi.Path) } else { if os.Getenv("DEBUG") != "" { - fmt.Printf("%q: %t\n", fi.Path, ourbase.HasBlob(fi.Hash)) + fmt.Printf("[%s] exists? %t (%q)\n", fi.Hash, ourbase.HasBlob(fi.Hash), fi.Path) } if ourbase.HasBlob(fi.Hash) && !ourbase.SameFile(fi.Hash, fi.Path) { if err := ourbase.LinkTo(fi.Path, fi.Hash); err != nil {