diff_unix.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //go:build !windows
  2. // +build !windows
  3. /*
  4. Copyright The containerd Authors.
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. package fs
  16. import (
  17. "bytes"
  18. "fmt"
  19. "os"
  20. "syscall"
  21. "github.com/containerd/continuity/sysx"
  22. )
  23. // detectDirDiff returns diff dir options if a directory could
  24. // be found in the mount info for upper which is the direct
  25. // diff with the provided lower directory
  26. func detectDirDiff(upper, lower string) *diffDirOptions {
  27. // TODO: get mount options for upper
  28. // TODO: detect AUFS
  29. // TODO: detect overlay
  30. return nil
  31. }
  32. // compareSysStat returns whether the stats are equivalent,
  33. // whether the files are considered the same file, and
  34. // an error
  35. func compareSysStat(s1, s2 interface{}) (bool, error) {
  36. ls1, ok := s1.(*syscall.Stat_t)
  37. if !ok {
  38. return false, nil
  39. }
  40. ls2, ok := s2.(*syscall.Stat_t)
  41. if !ok {
  42. return false, nil
  43. }
  44. return ls1.Mode == ls2.Mode && ls1.Uid == ls2.Uid && ls1.Gid == ls2.Gid && ls1.Rdev == ls2.Rdev, nil
  45. }
  46. func compareCapabilities(p1, p2 string) (bool, error) {
  47. c1, err := sysx.LGetxattr(p1, "security.capability")
  48. if err != nil && err != sysx.ENODATA {
  49. return false, fmt.Errorf("failed to get xattr for %s: %w", p1, err)
  50. }
  51. c2, err := sysx.LGetxattr(p2, "security.capability")
  52. if err != nil && err != sysx.ENODATA {
  53. return false, fmt.Errorf("failed to get xattr for %s: %w", p2, err)
  54. }
  55. return bytes.Equal(c1, c2), nil
  56. }
  57. func isLinked(f os.FileInfo) bool {
  58. s, ok := f.Sys().(*syscall.Stat_t)
  59. if !ok {
  60. return false
  61. }
  62. return !f.IsDir() && s.Nlink > 1
  63. }