hardlinks_unix.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // +build linux darwin freebsd solaris
  2. /*
  3. Copyright The containerd Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package continuity
  15. import (
  16. "fmt"
  17. "os"
  18. "syscall"
  19. )
  20. // hardlinkKey provides a tuple-key for managing hardlinks. This is system-
  21. // specific.
  22. type hardlinkKey struct {
  23. dev uint64
  24. inode uint64
  25. }
  26. // newHardlinkKey returns a hardlink key for the provided file info. If the
  27. // resource does not represent a possible hardlink, errNotAHardLink will be
  28. // returned.
  29. func newHardlinkKey(fi os.FileInfo) (hardlinkKey, error) {
  30. sys, ok := fi.Sys().(*syscall.Stat_t)
  31. if !ok {
  32. return hardlinkKey{}, fmt.Errorf("cannot resolve (*syscall.Stat_t) from os.FileInfo")
  33. }
  34. if sys.Nlink < 2 {
  35. // NOTE(stevvooe): This is not always true for all filesystems. We
  36. // should somehow detect this and provided a slow "polyfill" that
  37. // leverages os.SameFile if we detect a filesystem where link counts
  38. // is not really supported.
  39. return hardlinkKey{}, errNotAHardLink
  40. }
  41. return hardlinkKey{dev: uint64(sys.Dev), inode: uint64(sys.Ino)}, nil
  42. }