github.com/grange74/docker@v1.6.0-rc3/pkg/system/xattrs_linux.go (about)

     1  package system
     2  
     3  import (
     4  	"syscall"
     5  	"unsafe"
     6  )
     7  
     8  // Returns a nil slice and nil error if the xattr is not set
     9  func Lgetxattr(path string, attr string) ([]byte, error) {
    10  	pathBytes, err := syscall.BytePtrFromString(path)
    11  	if err != nil {
    12  		return nil, err
    13  	}
    14  	attrBytes, err := syscall.BytePtrFromString(attr)
    15  	if err != nil {
    16  		return nil, err
    17  	}
    18  
    19  	dest := make([]byte, 128)
    20  	destBytes := unsafe.Pointer(&dest[0])
    21  	sz, _, errno := syscall.Syscall6(syscall.SYS_LGETXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(unsafe.Pointer(attrBytes)), uintptr(destBytes), uintptr(len(dest)), 0, 0)
    22  	if errno == syscall.ENODATA {
    23  		return nil, nil
    24  	}
    25  	if errno == syscall.ERANGE {
    26  		dest = make([]byte, sz)
    27  		destBytes := unsafe.Pointer(&dest[0])
    28  		sz, _, errno = syscall.Syscall6(syscall.SYS_LGETXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(unsafe.Pointer(attrBytes)), uintptr(destBytes), uintptr(len(dest)), 0, 0)
    29  	}
    30  	if errno != 0 {
    31  		return nil, errno
    32  	}
    33  
    34  	return dest[:sz], nil
    35  }
    36  
    37  var _zero uintptr
    38  
    39  func Lsetxattr(path string, attr string, data []byte, flags int) error {
    40  	pathBytes, err := syscall.BytePtrFromString(path)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	attrBytes, err := syscall.BytePtrFromString(attr)
    45  	if err != nil {
    46  		return err
    47  	}
    48  	var dataBytes unsafe.Pointer
    49  	if len(data) > 0 {
    50  		dataBytes = unsafe.Pointer(&data[0])
    51  	} else {
    52  		dataBytes = unsafe.Pointer(&_zero)
    53  	}
    54  	_, _, errno := syscall.Syscall6(syscall.SYS_LSETXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(unsafe.Pointer(attrBytes)), uintptr(dataBytes), uintptr(len(data)), uintptr(flags), 0)
    55  	if errno != 0 {
    56  		return errno
    57  	}
    58  	return nil
    59  }