hardlink.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package fs
  14. import "os"
  15. // GetLinkInfo returns an identifier representing the node a hardlink is pointing
  16. // to. If the file is not hard linked then 0 will be returned.
  17. func GetLinkInfo(fi os.FileInfo) (uint64, bool) {
  18. return getLinkInfo(fi)
  19. }
  20. // getLinkSource returns a path for the given name and
  21. // file info to its link source in the provided inode
  22. // map. If the given file name is not in the map and
  23. // has other links, it is added to the inode map
  24. // to be a source for other link locations.
  25. func getLinkSource(name string, fi os.FileInfo, inodes map[uint64]string) (string, error) {
  26. inode, isHardlink := getLinkInfo(fi)
  27. if !isHardlink {
  28. return "", nil
  29. }
  30. path, ok := inodes[inode]
  31. if !ok {
  32. inodes[inode] = name
  33. }
  34. return path, nil
  35. }