xattr.go 3.1 KB

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