1// +build linux darwin 2 3/* 4 Copyright The containerd Authors. 5 6 Licensed under the Apache License, Version 2.0 (the "License"); 7 you may not use this file except in compliance with the License. 8 You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17*/ 18 19package sysx 20 21import ( 22 "bytes" 23 24 "golang.org/x/sys/unix" 25) 26 27// Listxattr calls syscall listxattr and reads all content 28// and returns a string array 29func Listxattr(path string) ([]string, error) { 30 return listxattrAll(path, unix.Listxattr) 31} 32 33// Removexattr calls syscall removexattr 34func Removexattr(path string, attr string) (err error) { 35 return unix.Removexattr(path, attr) 36} 37 38// Setxattr calls syscall setxattr 39func Setxattr(path string, attr string, data []byte, flags int) (err error) { 40 return unix.Setxattr(path, attr, data, flags) 41} 42 43// Getxattr calls syscall getxattr 44func Getxattr(path, attr string) ([]byte, error) { 45 return getxattrAll(path, attr, unix.Getxattr) 46} 47 48// LListxattr lists xattrs, not following symlinks 49func LListxattr(path string) ([]string, error) { 50 return listxattrAll(path, unix.Llistxattr) 51} 52 53// LRemovexattr removes an xattr, not following symlinks 54func LRemovexattr(path string, attr string) (err error) { 55 return unix.Lremovexattr(path, attr) 56} 57 58// LSetxattr sets an xattr, not following symlinks 59func LSetxattr(path string, attr string, data []byte, flags int) (err error) { 60 return unix.Lsetxattr(path, attr, data, flags) 61} 62 63// LGetxattr gets an xattr, not following symlinks 64func LGetxattr(path, attr string) ([]byte, error) { 65 return getxattrAll(path, attr, unix.Lgetxattr) 66} 67 68const defaultXattrBufferSize = 128 69 70type listxattrFunc func(path string, dest []byte) (int, error) 71 72func listxattrAll(path string, listFunc listxattrFunc) ([]string, error) { 73 buf := make([]byte, defaultXattrBufferSize) 74 n, err := listFunc(path, buf) 75 for err == unix.ERANGE { 76 // Buffer too small, use zero-sized buffer to get the actual size 77 n, err = listFunc(path, []byte{}) 78 if err != nil { 79 return nil, err 80 } 81 buf = make([]byte, n) 82 n, err = listFunc(path, buf) 83 } 84 if err != nil { 85 return nil, err 86 } 87 88 ps := bytes.Split(bytes.TrimSuffix(buf[:n], []byte{0}), []byte{0}) 89 var entries []string 90 for _, p := range ps { 91 if len(p) > 0 { 92 entries = append(entries, string(p)) 93 } 94 } 95 96 return entries, nil 97} 98 99type getxattrFunc func(string, string, []byte) (int, error) 100 101func getxattrAll(path, attr string, getFunc getxattrFunc) ([]byte, error) { 102 buf := make([]byte, defaultXattrBufferSize) 103 n, err := getFunc(path, attr, buf) 104 for err == unix.ERANGE { 105 // Buffer too small, use zero-sized buffer to get the actual size 106 n, err = getFunc(path, attr, []byte{}) 107 if err != nil { 108 return nil, err 109 } 110 buf = make([]byte, n) 111 n, err = getFunc(path, attr, buf) 112 } 113 if err != nil { 114 return nil, err 115 } 116 return buf[:n], nil 117} 118