xattr.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //go:build linux || darwin
  2. // +build linux darwin
  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 sysx
  16. import (
  17. "bytes"
  18. "golang.org/x/sys/unix"
  19. )
  20. // Listxattr calls syscall listxattr and reads all content
  21. // and returns a string array
  22. func Listxattr(path string) ([]string, error) {
  23. return listxattrAll(path, unix.Listxattr)
  24. }
  25. // Removexattr calls syscall removexattr
  26. func Removexattr(path string, attr string) (err error) {
  27. return unix.Removexattr(path, attr)
  28. }
  29. // Setxattr calls syscall setxattr
  30. func Setxattr(path string, attr string, data []byte, flags int) (err error) {
  31. return unix.Setxattr(path, attr, data, flags)
  32. }
  33. // Getxattr calls syscall getxattr
  34. func Getxattr(path, attr string) ([]byte, error) {
  35. return getxattrAll(path, attr, unix.Getxattr)
  36. }
  37. // LListxattr lists xattrs, not following symlinks
  38. func LListxattr(path string) ([]string, error) {
  39. return listxattrAll(path, unix.Llistxattr)
  40. }
  41. // LRemovexattr removes an xattr, not following symlinks
  42. func LRemovexattr(path string, attr string) (err error) {
  43. return unix.Lremovexattr(path, attr)
  44. }
  45. // LSetxattr sets an xattr, not following symlinks
  46. func LSetxattr(path string, attr string, data []byte, flags int) (err error) {
  47. return unix.Lsetxattr(path, attr, data, flags)
  48. }
  49. // LGetxattr gets an xattr, not following symlinks
  50. func LGetxattr(path, attr string) ([]byte, error) {
  51. return getxattrAll(path, attr, unix.Lgetxattr)
  52. }
  53. const defaultXattrBufferSize = 128
  54. type listxattrFunc func(path string, dest []byte) (int, error)
  55. func listxattrAll(path string, listFunc listxattrFunc) ([]string, error) {
  56. buf := make([]byte, defaultXattrBufferSize)
  57. n, err := listFunc(path, buf)
  58. for err == unix.ERANGE {
  59. // Buffer too small, use zero-sized buffer to get the actual size
  60. n, err = listFunc(path, []byte{})
  61. if err != nil {
  62. return nil, err
  63. }
  64. buf = make([]byte, n)
  65. n, err = listFunc(path, buf)
  66. }
  67. if err != nil {
  68. return nil, err
  69. }
  70. ps := bytes.Split(bytes.TrimSuffix(buf[:n], []byte{0}), []byte{0})
  71. var entries []string
  72. for _, p := range ps {
  73. if len(p) > 0 {
  74. entries = append(entries, string(p))
  75. }
  76. }
  77. return entries, nil
  78. }
  79. type getxattrFunc func(string, string, []byte) (int, error)
  80. func getxattrAll(path, attr string, getFunc getxattrFunc) ([]byte, error) {
  81. buf := make([]byte, defaultXattrBufferSize)
  82. n, err := getFunc(path, attr, buf)
  83. for err == unix.ERANGE {
  84. // Buffer too small, use zero-sized buffer to get the actual size
  85. n, err = getFunc(path, attr, []byte{})
  86. if err != nil {
  87. return nil, err
  88. }
  89. buf = make([]byte, n)
  90. n, err = getFunc(path, attr, buf)
  91. }
  92. if err != nil {
  93. return nil, err
  94. }
  95. return buf[:n], nil
  96. }