diff_unix.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //go:build !windows
  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 fs
  15. import (
  16. "bytes"
  17. "fmt"
  18. "os"
  19. "syscall"
  20. "github.com/containerd/continuity/sysx"
  21. )
  22. // compareSysStat returns whether the stats are equivalent,
  23. // whether the files are considered the same file, and
  24. // an error
  25. func compareSysStat(s1, s2 interface{}) (bool, error) {
  26. ls1, ok := s1.(*syscall.Stat_t)
  27. if !ok {
  28. return false, nil
  29. }
  30. ls2, ok := s2.(*syscall.Stat_t)
  31. if !ok {
  32. return false, nil
  33. }
  34. return ls1.Mode == ls2.Mode && ls1.Uid == ls2.Uid && ls1.Gid == ls2.Gid && ls1.Rdev == ls2.Rdev, nil
  35. }
  36. func compareCapabilities(p1, p2 string) (bool, error) {
  37. c1, err := sysx.LGetxattr(p1, "security.capability")
  38. if err != nil && err != sysx.ENODATA {
  39. return false, fmt.Errorf("failed to get xattr for %s: %w", p1, err)
  40. }
  41. c2, err := sysx.LGetxattr(p2, "security.capability")
  42. if err != nil && err != sysx.ENODATA {
  43. return false, fmt.Errorf("failed to get xattr for %s: %w", p2, err)
  44. }
  45. return bytes.Equal(c1, c2), nil
  46. }
  47. func isLinked(f os.FileInfo) bool {
  48. s, ok := f.Sys().(*syscall.Stat_t)
  49. if !ok {
  50. return false
  51. }
  52. return !f.IsDir() && s.Nlink > 1
  53. }