hardlinks_unix.go 982 B

123456789101112131415161718192021222324252627282930313233343536
  1. // +build linux darwin freebsd solaris
  2. package continuity
  3. import (
  4. "fmt"
  5. "os"
  6. "syscall"
  7. )
  8. // hardlinkKey provides a tuple-key for managing hardlinks. This is system-
  9. // specific.
  10. type hardlinkKey struct {
  11. dev uint64
  12. inode uint64
  13. }
  14. // newHardlinkKey returns a hardlink key for the provided file info. If the
  15. // resource does not represent a possible hardlink, errNotAHardLink will be
  16. // returned.
  17. func newHardlinkKey(fi os.FileInfo) (hardlinkKey, error) {
  18. sys, ok := fi.Sys().(*syscall.Stat_t)
  19. if !ok {
  20. return hardlinkKey{}, fmt.Errorf("cannot resolve (*syscall.Stat_t) from os.FileInfo")
  21. }
  22. if sys.Nlink < 2 {
  23. // NOTE(stevvooe): This is not always true for all filesystems. We
  24. // should somehow detect this and provided a slow "polyfill" that
  25. // leverages os.SameFile if we detect a filesystem where link counts
  26. // is not really supported.
  27. return hardlinkKey{}, errNotAHardLink
  28. }
  29. return hardlinkKey{dev: uint64(sys.Dev), inode: uint64(sys.Ino)}, nil
  30. }