*.go: firm up whether it is the same file

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2023-04-11 14:31:28 -04:00
parent 022dad64e7
commit 2835d1a77d
3 changed files with 23 additions and 16 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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 {