github.com/moby/docker@v26.1.3+incompatible/pkg/system/xattrs_linux.go (about)

     1  package system // import "github.com/docker/docker/pkg/system"
     2  
     3  import (
     4  	"golang.org/x/sys/unix"
     5  )
     6  
     7  // Lgetxattr retrieves the value of the extended attribute identified by attr
     8  // and associated with the given path in the file system.
     9  // It will returns a nil slice and nil error if the xattr is not set.
    10  func Lgetxattr(path string, attr string) ([]byte, error) {
    11  	sysErr := func(err error) ([]byte, error) {
    12  		return nil, &XattrError{Op: "lgetxattr", Attr: attr, Path: path, Err: err}
    13  	}
    14  
    15  	// Start with a 128 length byte array
    16  	dest := make([]byte, 128)
    17  	sz, errno := unix.Lgetxattr(path, attr, dest)
    18  
    19  	for errno == unix.ERANGE {
    20  		// Buffer too small, use zero-sized buffer to get the actual size
    21  		sz, errno = unix.Lgetxattr(path, attr, []byte{})
    22  		if errno != nil {
    23  			return sysErr(errno)
    24  		}
    25  		dest = make([]byte, sz)
    26  		sz, errno = unix.Lgetxattr(path, attr, dest)
    27  	}
    28  
    29  	switch {
    30  	case errno == unix.ENODATA:
    31  		return nil, nil
    32  	case errno != nil:
    33  		return sysErr(errno)
    34  	}
    35  
    36  	return dest[:sz], nil
    37  }
    38  
    39  // Lsetxattr sets the value of the extended attribute identified by attr
    40  // and associated with the given path in the file system.
    41  func Lsetxattr(path string, attr string, data []byte, flags int) error {
    42  	err := unix.Lsetxattr(path, attr, data, flags)
    43  	if err != nil {
    44  		return &XattrError{Op: "lsetxattr", Attr: attr, Path: path, Err: err}
    45  	}
    46  	return nil
    47  }