github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/utils/archive/attr.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 2 // 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 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 // This file is extracted from "github.com/docker/docker/pkg/system" 16 17 package archive 18 19 import "golang.org/x/sys/unix" 20 21 // Lgetxattr retrieves the value of the extended attribute identified by attr 22 // and associated with the given path in the file system. 23 // It will return a nil slice and nil error if the xattr is not set. 24 func Lgetxattr(path string, attr string) ([]byte, error) { 25 // Start with a 128 length byte array 26 dest := make([]byte, 128) 27 sz, errno := unix.Lgetxattr(path, attr, dest) 28 29 switch { 30 case errno == unix.ENODATA: 31 return nil, nil 32 case errno == unix.ERANGE: 33 // 128 byte array might just not be good enough. A dummy buffer is used 34 // to get the real size of the xattrs on disk 35 sz, errno = unix.Lgetxattr(path, attr, []byte{}) 36 if errno != nil { 37 return nil, errno 38 } 39 dest = make([]byte, sz) 40 sz, errno = unix.Lgetxattr(path, attr, dest) 41 if errno != nil { 42 return nil, errno 43 } 44 case errno != nil: 45 return nil, errno 46 } 47 return dest[:sz], nil 48 } 49 50 // Lsetxattr sets the value of the extended attribute identified by attr 51 // and associated with the given path in the file system. 52 func Lsetxattr(path string, attr string, data []byte, flags int) error { 53 return unix.Lsetxattr(path, attr, data, flags) 54 }