diff_linux.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 (
  15. "errors"
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "syscall"
  20. "github.com/containerd/continuity/devices"
  21. "github.com/containerd/continuity/sysx"
  22. "golang.org/x/sys/unix"
  23. )
  24. const (
  25. // whiteoutPrefix prefix means file is a whiteout. If this is followed
  26. // by a filename this means that file has been removed from the base
  27. // layer.
  28. //
  29. // See https://github.com/opencontainers/image-spec/blob/master/layer.md#whiteouts
  30. whiteoutPrefix = ".wh."
  31. )
  32. // overlayFSWhiteoutConvert detects whiteouts and opaque directories.
  33. //
  34. // It returns deleted indicator if the file is a character device with 0/0
  35. // device number. And call changeFn with ChangeKindDelete for opaque
  36. // directories.
  37. //
  38. // Check: https://www.kernel.org/doc/Documentation/filesystems/overlayfs.txt
  39. func overlayFSWhiteoutConvert(diffDir, path string, f os.FileInfo, changeFn ChangeFunc) (deleted bool, _ error) {
  40. if f.Mode()&os.ModeCharDevice != 0 {
  41. if _, ok := f.Sys().(*syscall.Stat_t); !ok {
  42. return false, nil
  43. }
  44. maj, min, err := devices.DeviceInfo(f)
  45. if err != nil {
  46. return false, err
  47. }
  48. return (maj == 0 && min == 0), nil
  49. }
  50. if f.IsDir() {
  51. originalPath := filepath.Join(diffDir, path)
  52. opaque, err := getOpaqueValue(originalPath)
  53. if err != nil {
  54. if errors.Is(err, unix.ENODATA) {
  55. return false, nil
  56. }
  57. return false, err
  58. }
  59. if len(opaque) == 1 && opaque[0] == 'y' {
  60. opaqueDirPath := filepath.Join(path, whiteoutPrefix+".opq")
  61. return false, changeFn(ChangeKindDelete, opaqueDirPath, nil, nil)
  62. }
  63. }
  64. return false, nil
  65. }
  66. // getOpaqueValue returns opaque value for a given file.
  67. func getOpaqueValue(filePath string) ([]byte, error) {
  68. for _, xattr := range []string{
  69. "trusted.overlay.opaque",
  70. // TODO(fuweid):
  71. //
  72. // user.overlay.* is available since 5.11. We should check
  73. // kernel version before read.
  74. //
  75. // REF: https://github.com/torvalds/linux/commit/2d2f2d7322ff43e0fe92bf8cccdc0b09449bf2e1
  76. "user.overlay.opaque",
  77. } {
  78. opaque, err := sysx.LGetxattr(filePath, xattr)
  79. if err != nil {
  80. if errors.Is(err, unix.ENODATA) || errors.Is(err, unix.ENOTSUP) {
  81. continue
  82. }
  83. return nil, fmt.Errorf("failed to retrieve %s attr: %w", xattr, err)
  84. }
  85. return opaque, nil
  86. }
  87. return nil, unix.ENODATA
  88. }