github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/runc/libcontainer/xattr/xattr_linux.go (about)

     1  // +build linux
     2  
     3  package xattr
     4  
     5  import (
     6  	"syscall"
     7  
     8  	"github.com/opencontainers/runc/libcontainer/system"
     9  )
    10  
    11  func XattrEnabled(path string) bool {
    12  	if Setxattr(path, "user.test", "") == syscall.ENOTSUP {
    13  		return false
    14  	}
    15  	return true
    16  }
    17  
    18  func stringsfromByte(buf []byte) (result []string) {
    19  	offset := 0
    20  	for index, b := range buf {
    21  		if b == 0 {
    22  			result = append(result, string(buf[offset:index]))
    23  			offset = index + 1
    24  		}
    25  	}
    26  	return
    27  }
    28  
    29  func Listxattr(path string) ([]string, error) {
    30  	size, err := system.Llistxattr(path, nil)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	buf := make([]byte, size)
    35  	read, err := system.Llistxattr(path, buf)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	names := stringsfromByte(buf[:read])
    40  	return names, nil
    41  }
    42  
    43  func Getxattr(path, attr string) (string, error) {
    44  	value, err := system.Lgetxattr(path, attr)
    45  	if err != nil {
    46  		return "", err
    47  	}
    48  	return string(value), nil
    49  }
    50  
    51  func Setxattr(path, xattr, value string) error {
    52  	return system.Lsetxattr(path, xattr, []byte(value), 0)
    53  }